[ofbiz-framework] branch trunk updated: Improved: Headerize external script in multi-block html template (OFBIZ-11741)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Improved: Headerize external script in multi-block html template (OFBIZ-11741)

James Yong-2
This is an automated email from the ASF dual-hosted git repository.

jamesyong pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new a5ab396  Improved: Headerize external script in multi-block html template (OFBIZ-11741)
a5ab396 is described below

commit a5ab3961faeda725bdd377e26f5383141958787a
Author: James Yong <[hidden email]>
AuthorDate: Sun May 31 23:31:49 2020 +0800

    Improved: Headerize external script in multi-block html template (OFBIZ-11741)
   
    Allow expression in template location.
---
 .../org/apache/ofbiz/widget/model/HtmlWidget.java  | 16 ++--------------
 .../ofbiz/widget/model/ModelScreenWidget.java      | 12 +++++-------
 .../widget/model/MultiBlockHtmlTemplateUtil.java   | 22 +++++++++++++++++-----
 3 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java
index 3e4f3cd..b7a9cc9 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/HtmlWidget.java
@@ -197,11 +197,6 @@ public class HtmlWidget extends ModelScreenWidget {
         Elements scriptElements = doc.select("script");
         if (scriptElements != null && scriptElements.size() > 0) {
             StringBuilder scripts = new StringBuilder();
-
-            // check if location contains variable
-            String originalLocation = locationExdr.getOriginal();
-            boolean isStaticLocation = !originalLocation.contains("${");
-
             for (org.jsoup.nodes.Element script : scriptElements) {
                 String type = script.attr("type");
                 String src = script.attr("src");
@@ -213,15 +208,8 @@ public class HtmlWidget extends ModelScreenWidget {
                 } else {
                     String dataImport = script.attr("data-import");
                     if ("head".equals(dataImport)) {
-                        if (isStaticLocation) {
-                            // remove external script in the template that is meant to be imported in the html header
-                            script.remove();
-                        } else {
-                            // throw error to the browser
-                            writer.append("<script>alert('Unable to headerize "
-                                    + UtilCodec.getEncoder("html").encode(script.toString())
-                                    + " when template location not is static');</script>");
-                        }
+                        // remove external script in the template that is meant to be imported in the html header
+                        script.remove();
                     }
                 }
             }
diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java
index c3701f3..c34dfc5 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelScreenWidget.java
@@ -270,13 +270,9 @@ public abstract class ModelScreenWidget extends ModelWidget {
         public void renderWidgetString(Appendable writer, Map<String, Object> context,
                                        ScreenStringRenderer screenStringRenderer) throws GeneralException, IOException {
 
-            try {
-                String location = getModelScreen().getSourceLocation();
-                String name = getModelScreen().getName();
-                MultiBlockHtmlTemplateUtil.addLinksToLayoutSettings(context, location, name);
-            } catch (Exception e) {
-                throw new GeneralException(e);
-            }
+            String location = getModelScreen().getSourceLocation();
+            String name = getModelScreen().getName();
+            MultiBlockHtmlTemplateUtil.storeScreenLocationName(context, location, name);
 
             // check the condition, if there is one
             boolean condTrue = true;
@@ -292,6 +288,8 @@ public abstract class ModelScreenWidget extends ModelWidget {
                 AbstractModelAction.runSubActions(this.actions, context);
 
                 try {
+                    MultiBlockHtmlTemplateUtil.addLinksToLayoutSettings(context);
+
                     // section by definition do not themselves do anything, so this method will generally do nothing, but we'll call it anyway
                     screenStringRenderer.renderSectionBegin(writer, context, this);
 
diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java
index a7bc33e..0262a70 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/MultiBlockHtmlTemplateUtil.java
@@ -151,19 +151,27 @@ public final class MultiBlockHtmlTemplateUtil {
     }
 
     /**
-     * Add html links to the header
+     * Store the 1st screen called by request
      * @param context
      * @param location screen location. Expression is not allowed.
      * @param name screen name. Expression is not allowed.
-     * @throws Exception
      */
-    public static void addLinksToLayoutSettings(final Map<String, Object> context, String location, String name) throws Exception {
+    public static void storeScreenLocationName(final Map<String, Object> context, String location, String name) {
         HttpServletRequest request = (HttpServletRequest) context.get("request");
         if (request.getAttribute(HTML_LINKS_FOR_HEAD) == null) {
             String currentLocationHashName = location + "#" + name;
             request.setAttribute(HTML_LINKS_FOR_HEAD, currentLocationHashName);
-            return;
         }
+    }
+
+    /**
+     * Add html links to the header
+     * @param context
+     * @throws Exception
+     */
+    public static void addLinksToLayoutSettings(final Map<String, Object> context) throws IOException {
+        HttpServletRequest request = (HttpServletRequest) context.get("request");
+
         // check "layoutSettings.javaScripts" is not empty
         Map<String, Object> layoutSettings = UtilGenerics.cast(context.get("layoutSettings"));
         if (UtilValidate.isEmpty(layoutSettings)) {
@@ -180,6 +188,7 @@ public final class MultiBlockHtmlTemplateUtil {
         }
         Object objValue = request.getAttribute(HTML_LINKS_FOR_HEAD);
         if (objValue instanceof String) {
+            Set<String> retryHtmlLinks = new LinkedHashSet<>();
             String currentLocationHashName = (String) request.getAttribute(HTML_LINKS_FOR_HEAD);
             Set<String> htmlLinks = new LinkedHashSet<>();
             Set<String> locHashNameList = getRelatedScreenLocationHashName(currentLocationHashName, context);
@@ -198,6 +207,7 @@ public final class MultiBlockHtmlTemplateUtil {
                             if (UtilValidate.isNotEmpty(expandUrl)) {
                                 htmlLinks.addAll(getHtmlImportsFromHtmlTemplate(expandUrl));
                             } else {
+                                retryHtmlLinks.add(url);
                                 Debug.log("Unable to expand " + url, MODULE);
                             }
                         } else {
@@ -216,7 +226,9 @@ public final class MultiBlockHtmlTemplateUtil {
                     }
                 }
             }
-            request.setAttribute(HTML_LINKS_FOR_HEAD, true);
+            if (UtilValidate.isEmpty(retryHtmlLinks)) {
+                request.setAttribute(HTML_LINKS_FOR_HEAD, true);
+            }
         }
 
     }