Skip to content

Commit

Permalink
#853 performance optimization (added Kyle's improvements)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Oct 5, 2018
1 parent 55406fb commit 8a5e20d
Showing 1 changed file with 42 additions and 58 deletions.
100 changes: 42 additions & 58 deletions src/main/java/net/bootsfaces/listeners/AddResourcesListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ public void processEvent(SystemEvent event) throws AbortProcessingException {
* If it found nothing, it check for components that has as a resource lib, the
* "bsf" lib.
*
* @param root
* <- the UIViewRoot
* @param context
* <- the faces context
* @param root the UIViewRoot
* @param context the faces context
* @return
*/
private boolean ensureExistBootsfacesComponent(UIViewRoot root, FacesContext context) {
Expand All @@ -178,10 +176,8 @@ private boolean ensureExistBootsfacesComponent(UIViewRoot root, FacesContext con
* target library. I use this method to check existence of a BsF component
* because, at this level, the getComponentResource returns always null
*
* @param parent
* the parent component
* @param targetLib
* the lib to search
* @param parent the parent component
* @param targetLib the lib to search
* @return
*/
public static UIComponent findBsfComponent(UIComponent parent, String targetLib) {
Expand Down Expand Up @@ -232,10 +228,8 @@ private void addMetaTags(UIViewRoot root, FacesContext context) {
/**
* Add the required CSS files and the FontAwesome CDN link.
*
* @param root
* The UIViewRoot of the JSF tree.
* @param context
* The current FacesContext
* @param root The UIViewRoot of the JSF tree.
* @param context The current FacesContext
*/
private void addCSS(UIViewRoot root, FacesContext context) {
// The following code is needed to diagnose the warning "Unable to save dynamic
Expand Down Expand Up @@ -386,10 +380,8 @@ private String getTheme(FacesContext context) {
/**
* Add the required Javascript files and the FontAwesome CDN link.
*
* @param root
* The UIViewRoot of the JSF tree.
* @param context
* The current FacesContext
* @param root The UIViewRoot of the JSF tree.
* @param context The current FacesContext
*/
private void addJavascript(UIViewRoot root, FacesContext context) {
// The following code is needed to diagnose the warning "Unable to save dynamic
Expand Down Expand Up @@ -583,7 +575,7 @@ public static void setNeedsFontsAwesome(Object uiComponent) {
}
list.add(uiComponent);
}

private boolean needsFontAwesome4() {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot root = context.getViewRoot();
Expand All @@ -602,9 +594,9 @@ private boolean needsFontAwesome4() {
return true;
}
boolean v4 = false;
for (Object o: all) {
for (Object o : all) {
if (!v5.containsKey(o)) {
v4=true;
v4 = true;
break;
}
}
Expand All @@ -621,10 +613,8 @@ private boolean needsFontAwesome4() {
* TODO: Verify if the duplicate resource files are a bug of BootsFaces or of
* the Mojarra library itself.
*
* @param root
* The current UIViewRoot
* @param context
* The current FacesContext
* @param root The current UIViewRoot
* @param context The current FacesContext
*/
private void removeDuplicateResources(UIViewRoot root, FacesContext context) {
List<UIComponent> resourcesToRemove = new ArrayList<UIComponent>();
Expand Down Expand Up @@ -669,10 +659,8 @@ public int compare(UIComponent o1, UIComponent o2) {
* Theme (Bootstrap) and custom themes, other themes would get default theme
* look and feel otherwise.
*
* @param root
* The current UIViewRoot
* @param context
* The current FacesContext
* @param root The current UIViewRoot
* @param context The current FacesContext
*/
private void removeBootstrapResources(UIViewRoot root, FacesContext context) {
String theme = getTheme(context);
Expand Down Expand Up @@ -708,10 +696,8 @@ private void removeBootstrapResources(UIViewRoot root, FacesContext context) {
* prior to other resource files, giving the developer the opportunity to
* overwrite a CSS or JS file.
*
* @param root
* The current UIViewRoot
* @param context
* The current FacesContext
* @param root The current UIViewRoot
* @param context The current FacesContext
*/
private void enforceCorrectLoadOrder(UIViewRoot root, FacesContext context) {
// // first, handle the CSS files.
Expand Down Expand Up @@ -820,8 +806,7 @@ private boolean isFontAwesomeComponentUsedAndRemoveIt() {
/**
* Looks for the header in the JSF tree.
*
* @param root
* The root of the JSF tree.
* @param root The root of the JSF tree.
* @return null, if the head couldn't be found.
*/
private UIComponent findHeader(UIViewRoot root) {
Expand All @@ -843,10 +828,8 @@ private UIComponent findHeader(UIViewRoot root) {
* Registers a JS file that needs to be included in the header of the HTML file,
* but after jQuery and AngularJS.
*
* @param library
* The name of the sub-folder of the resources folder.
* @param resource
* The name of the resource file within the library folder.
* @param library The name of the sub-folder of the resources folder.
* @param resource The name of the resource file within the library folder.
*/
public static void addResourceToHeadButAfterJQuery(String library, String resource) {
addResource(resource, library, library + "#" + resource, RESOURCE_KEY);
Expand All @@ -856,10 +839,8 @@ public static void addResourceToHeadButAfterJQuery(String library, String resour
* Registers a core JS file that needs to be included in the header of the HTML
* file, but after jQuery and AngularJS.
*
* @param library
* The name of the sub-folder of the resources folder.
* @param resource
* The name of the resource file within the library folder.
* @param library The name of the sub-folder of the resources folder.
* @param resource The name of the resource file within the library folder.
*/
public static void addBasicJSResource(String library, String resource) {
addResource(resource, library, resource, BASIC_JS_RESOURCE_KEY);
Expand All @@ -869,8 +850,7 @@ public static void addBasicJSResource(String library, String resource) {
* Registers a themed CSS file that needs to be included in the header of the
* HTML file.
*
* @param resource
* The name of the resource file within the library folder.
* @param resource The name of the resource file within the library folder.
*/
public static void addThemedCSSResource(String resource) {
Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
Expand All @@ -890,8 +870,7 @@ public static void addThemedCSSResource(String resource) {
* Registers a Extension CSS file that needs to be included in the header of the
* HTML file.
*
* @param resource
* The name of the resource file within the library folder.
* @param resource The name of the resource file within the library folder.
*/
public static void addExtCSSResource(String resource) {
Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
Expand Down Expand Up @@ -964,27 +943,32 @@ private boolean isFalseOrNo(String param) {
}

/**
* Add the default datatables.net resource if and only if the user doesn't bring their own copy, and if they didn't disallow it in the web.xml
* by setting the context paramter net.bootsfaces.get_datatable_from_cdn to true.
* Add the default datatables.net resource if and only if the user doesn't bring
* their own copy, and if they didn't disallow it in the web.xml by setting the
* context paramter net.bootsfaces.get_datatable_from_cdn to true.
*
* @param defaultFilename The URL of the file to be loaded
* @param type either "js" or "css"
* @param type either "js" or "css"
*/
public static void addDatatablesResourceIfNecessary(String defaultFilename, String type) {
boolean loadDatatables = shouldLibraryBeLoaded(P_GET_DATATABLE_FROM_CDN, true);
// Do we have to add jQuery, or are the resources already there?
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot root = context.getViewRoot();

String[] positions = {"head", "body", "form"};
for (String position: positions) {
List<UIComponent> availableResources = root.getComponentResources(context, position);
for (UIComponent ava : availableResources) {
String name = (String) ava.getAttributes().get("name");
if (null != name) {
System.out.println(name);
name = name.toLowerCase();
if (name.contains("datatables") && name.endsWith("."+type)) {
loadDatatables = false;
String[] positions = { "head", "body", "form" };
for (String position : positions) {
if (loadDatatables) {
List<UIComponent> availableResources = root.getComponentResources(context, position);
for (UIComponent ava : availableResources) {
String name = (String) ava.getAttributes().get("name");
if (null != name) {
System.out.println(name);
name = name.toLowerCase();
if (name.contains("datatables") && name.endsWith("." + type)) {
loadDatatables = false;
break;
}
}
}
}
Expand Down

0 comments on commit 8a5e20d

Please sign in to comment.