[ofbiz-framework] branch trunk updated: Improved: Move page-specific script links to html template (OFBIZ-11799)

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: Move page-specific script links to html template (OFBIZ-11799)

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 d5140ca  Improved: Move page-specific script links to html template (OFBIZ-11799)
d5140ca is described below

commit d5140ca65dd74c71baa6121f625649560b558362
Author: James Yong <[hidden email]>
AuthorDate: Thu Aug 6 22:39:59 2020 +0800

    Improved: Move page-specific script links to html template (OFBIZ-11799)
   
    Moved select2 script and link tags to html template when multi-block=true.
    select2 language js will be auto added by MultiBlockHtmlTemplateUtil#addLinksToLayoutSettings
   
    Tested on
    https://localhost:8443/example/control/FormWidgetExamples
    https://localhost:8443/ordermgr/control/FindRequest
---
 .../widget/model/MultiBlockHtmlTemplateUtil.java   | 26 ++++++++++++++++------
 .../template/includes/SetMultipleSelectJs.ftl      |  6 +++++
 .../template/includes/SetMultipleSelectJsList.ftl  |  6 +++++
 themes/common-theme/widget/CommonScreens.xml       |  6 +----
 themes/common-theme/widget/Theme.xml               |  2 --
 5 files changed, 32 insertions(+), 14 deletions(-)

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 05a2001..702ca83 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
@@ -25,6 +25,7 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
@@ -208,15 +209,18 @@ public final class MultiBlockHtmlTemplateUtil {
             return;
         }
         List<String> layoutSettingsStyleSheets = UtilGenerics.cast(layoutSettings.get("styleSheets"));
-        if (UtilValidate.isEmpty(layoutSettingsStyleSheets)
-                && UtilValidate.isEmpty(layoutSettings.get("VT_STYLESHEET"))) {
-            return;
+        if (UtilValidate.isEmpty(layoutSettingsStyleSheets)) {
+            layoutSettingsStyleSheets = UtilGenerics.cast(layoutSettings.get("VT_STYLESHEET"));
+            if (UtilValidate.isEmpty(layoutSettingsStyleSheets)) {
+                return;
+            }
         }
         // ensure initTheme.groovy has run.
         Map<String, String> commonScreenLocations = UtilGenerics.cast(context.get("commonScreenLocations"));
         if (UtilValidate.isEmpty(commonScreenLocations)) {
             return;
         }
+        Locale locale = (Locale) context.get("locale");
         Object objValue = request.getAttribute(HTML_LINKS_FOR_HEAD);
         if (objValue instanceof String) {
             // store expressions for Template Location that is not expanded correctly, for retry.
@@ -255,7 +259,7 @@ public final class MultiBlockHtmlTemplateUtil {
                 }
             }
             if (UtilValidate.isNotEmpty(htmlLinks)) {
-                addLinksToLayoutSettings(htmlLinks, layoutSettingsJavaScripts, layoutSettingsStyleSheets);
+                addLinksToLayoutSettings(htmlLinks, layoutSettingsJavaScripts, layoutSettingsStyleSheets, locale);
             }
             if (UtilValidate.isEmpty(retryScreenLocHashNameExpressions) && UtilValidate.isEmpty(retryTemplateLocationExpressions)) {
                 request.setAttribute(HTML_LINKS_FOR_HEAD, true);
@@ -305,7 +309,7 @@ public final class MultiBlockHtmlTemplateUtil {
                 }
             }
             if (UtilValidate.isNotEmpty(htmlLinks)) {
-                addLinksToLayoutSettings(htmlLinks, layoutSettingsJavaScripts, layoutSettingsStyleSheets);
+                addLinksToLayoutSettings(htmlLinks, layoutSettingsJavaScripts, layoutSettingsStyleSheets, locale);
             }
             if (UtilValidate.isEmpty(retryScreenLocHashNameExpressions) && UtilValidate.isEmpty(retryTemplateLocationExpressions)) {
                 request.setAttribute(HTML_LINKS_FOR_HEAD, true);
@@ -320,13 +324,21 @@ public final class MultiBlockHtmlTemplateUtil {
 
     private static void addLinksToLayoutSettings(Set<String> htmlLinks,
                                                  List<String> layoutSettingsJavaScripts,
-                                                 List<String> layoutSettingsStyleSheets) {
+                                                 List<String> layoutSettingsStyleSheets, Locale locale) {
         for (String link : htmlLinks) {
             if (link.startsWith("script:")) {
                 String url = link.substring(7);
                 // check url is not already in layoutSettings.javaScripts
                 if (!layoutSettingsJavaScripts.contains(url)) {
                     layoutSettingsJavaScripts.add(url);
+                    if (url.contains("select2")) {
+                        // find and add select2 language js
+                        String localeString = locale.toString();
+                        String langJsUrl = org.apache.ofbiz.common.JsLanguageFilesMapping.select2.getFilePath(localeString);
+                        if (!layoutSettingsJavaScripts.contains(langJsUrl)) {
+                            layoutSettingsJavaScripts.add(langJsUrl);
+                        }
+                    }
                 }
             } else if (link.startsWith("link:")) {
                 String url = link.substring(5);
@@ -429,4 +441,4 @@ public final class MultiBlockHtmlTemplateUtil {
     public static void cleanupScriptCache(HttpSession session) {
         scriptCache.remove(session.getId());
     }
-}
+}
\ No newline at end of file
diff --git a/themes/common-theme/template/includes/SetMultipleSelectJs.ftl b/themes/common-theme/template/includes/SetMultipleSelectJs.ftl
index 5cfc77b..4f9f122 100644
--- a/themes/common-theme/template/includes/SetMultipleSelectJs.ftl
+++ b/themes/common-theme/template/includes/SetMultipleSelectJs.ftl
@@ -16,6 +16,12 @@ KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 -->
+<#-- Define select2 js and css tags to be added to html head tag when multi-block=true.
+     Note select2 language js will be auto added by MultiBlockHtmlTemplateUtil#addLinksToLayoutSettings -->
+<script data-import="head" type="application/javascript"
+        src="/common/js/jquery/plugins/select2/js/select2-4.0.6.js"></script>
+<link rel="stylesheet" type="text/css"
+      href="/common/js/jquery/plugins/select2/css/select2-4.0.6.css"/>
 <#if asm_multipleSelect??> <#-- we check only this var and suppose the others are also present -->
 <script type="application/javascript">
     jQuery(document).ready(function () {
diff --git a/themes/common-theme/template/includes/SetMultipleSelectJsList.ftl b/themes/common-theme/template/includes/SetMultipleSelectJsList.ftl
index 560b675..b82d708 100644
--- a/themes/common-theme/template/includes/SetMultipleSelectJsList.ftl
+++ b/themes/common-theme/template/includes/SetMultipleSelectJsList.ftl
@@ -16,6 +16,12 @@ KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 -->
+<#-- Define select2 js and css tags to be added to html head tag when multi-block=true.
+     Note select2 language js will be auto added by MultiBlockHtmlTemplateUtil#addLinksToLayoutSettings -->
+<script data-import="head" type="application/javascript"
+        src="/common/js/jquery/plugins/select2/js/select2-4.0.6.js"></script>
+<link rel="stylesheet" type="text/css"
+      href="/common/js/jquery/plugins/select2/css/select2-4.0.6.css"/>
 <#if asm_listField??> <#-- we check only this var and suppose the others are also present -->
   <#list asm_listField as row>
     <#if row.asm_multipleSelect??>
diff --git a/themes/common-theme/widget/CommonScreens.xml b/themes/common-theme/widget/CommonScreens.xml
index 2a49029..1e09877 100644
--- a/themes/common-theme/widget/CommonScreens.xml
+++ b/themes/common-theme/widget/CommonScreens.xml
@@ -126,7 +126,6 @@ under the License.
                 <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.jquery.getFilePath(initialLocaleComplete)}" global="true" />
                 <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.validation.getFilePath(initialLocaleComplete)}" global="true" />
                 <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.dateTime.getFilePath(initialLocaleComplete)}" global="true" />
-                <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.select2.getFilePath(initialLocaleComplete)}" global="true" />
                 <!-- Jgrowl -->
                 <set field="layoutSettings.javaScripts[]" value="/common/js/jquery/plugins/Readmore.js-master/readmore.js" global="true"/>
                 <set field="layoutSettings.javaScripts[]" value="/common/js/jquery/plugins/jquery-jgrowl/jquery.jgrowl-1.4.6.min.js" global="true"/>
@@ -353,7 +352,6 @@ under the License.
                         <set field="messagesTemplateLocation" from-field="layoutSettings.VT_MSG_TMPLT_LOC" default-value="component://common-theme/template/includes/Messages.ftl"/>
                         <set field="layoutSettings.styleSheets[]" value="/common/js/jquery/plugins/featherlight/featherlight-1.7.13.min.css" global="true"/>
                         <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/featherlight/featherlight-1.7.13.min.js" global="true"/>
-                        <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/select2/js/select2-4.0.6.js"/>
                         <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/datetimepicker/jquery-ui-timepicker-addon-1.6.3.min.js"/>
                         <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/ui/jquery-ui-1.12.1.min.js"/>
                         <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/validate/jquery.validate.min.js"/>
@@ -442,7 +440,6 @@ under the License.
                 <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.jquery.getFilePath(initialLocaleComplete)}" global="true"/>
                 <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.validation.getFilePath(initialLocaleComplete)}" global="true"/>
 
-                <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/select2/js/select2-4.0.6.js" global="true"/>
                 <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/datetimepicker/jquery-ui-timepicker-addon-1.6.3.min.js" global="true"/>
                 <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/ui/jquery-ui-1.12.1.min.js" global="true"/>
                 <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/jeditable/jquery.jeditable-1.7.3.js" global="true"/>
@@ -451,8 +448,7 @@ under the License.
                 <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/plugins/browser-plugin/jquery.browser-0.1.0.min.js" global="true"/>
                 <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/jquery-migrate-3.3.0.min.js" global="true" />
                 <set field="layoutSettings.javaScripts[+0]" value="/common/js/jquery/jquery-3.5.1.min.js" global="true"/>
-                    <!-- jQuery CSSs -->
-                <set field="layoutSettings.styleSheets[+0]" value="/common/js/jquery/plugins/select2/css/select2-4.0.6.css" global="true"/>
+                <!-- jQuery CSSs -->
                 <set field="layoutSettings.styleSheets[+0]" value="/common/js/jquery/ui/jquery-ui-1.12.1.min.css" global="true" />
                 <set field="layoutSettings.styleSheets[+0]" value="/common/js/jquery/plugins/datetimepicker/jquery-ui-timepicker-addon-1.6.3.min.css" global="true" />
 
diff --git a/themes/common-theme/widget/Theme.xml b/themes/common-theme/widget/Theme.xml
index a99fde3..150ea7e 100644
--- a/themes/common-theme/widget/Theme.xml
+++ b/themes/common-theme/widget/Theme.xml
@@ -61,7 +61,6 @@ under the License.
         <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/jquery-migrate-3.3.0.min.js"/>
         <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/plugins/browser-plugin/jquery.browser-0.1.0.min.js"/>
         <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/ui/jquery-ui-1.12.1.min.js"/>
-        <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/plugins/select2/js/select2-4.0.6.js"/>
         <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/plugins/datetimepicker/jquery-ui-timepicker-addon-1.6.3.min.js"/>
         <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/plugins/fjTimer/jquerytimer-min.js"/>
         <property name="VT_HDR_JAVASCRIPT['add']" value="/common/js/jquery/plugins/mask/jquery.mask-1.14.13.min.js"/>
@@ -83,7 +82,6 @@ under the License.
         <property name="VT_STYLESHEET['add']" value="/common/js/jquery/plugins/elrte-1.3/css/elrte.min.css"/>
         <property name="VT_STYLESHEET['add']" value="/common/js/jquery/ui/jquery-ui-1.12.1.min.css"/>
         <property name="VT_STYLESHEET['add']" value="/common/js/jquery/plugins/datetimepicker/jquery-ui-timepicker-addon-1.6.3.min.css"/>
-        <property name="VT_STYLESHEET['add']" value="/common/js/jquery/plugins/select2/css/select2-4.0.6.css"/>
     </theme-properties>
 
     <templates><!-- Freemarker template use by this theme to render widget model-->