[ofbiz-framework] branch trunk updated: Improved: Load language js for date.js only when required (OFBIZ-12064)

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: Load language js for date.js only when required (OFBIZ-12064)

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 bc5b0d5  Improved: Load language js for date.js only when required (OFBIZ-12064)
bc5b0d5 is described below

commit bc5b0d595cab9103e9ec0c03234cec81c80f47c3
Author: James Yong <[hidden email]>
AuthorDate: Fri Nov 27 21:51:35 2020 +0800

    Improved: Load language js for date.js only when required (OFBIZ-12064)
   
    Tested on
    1) date time field at /ordermgr/control/orderentry and
    2) date find field at /ordermgr/control/findreturn
---
 .../ofbiz/common/JsLanguageFilesMappingUtil.java   |  44 +++++++
 .../ftl/JsLanguageFilesMappingTransform.java       |  20 +--
 .../template/macro/HtmlFormMacroLibrary.ftl        |   5 +
 .../webapp/common/js/util/OfbizUtil.js             | 139 +++++++++++----------
 themes/common-theme/widget/CommonScreens.xml       |   2 -
 5 files changed, 122 insertions(+), 88 deletions(-)

diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFilesMappingUtil.java b/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFilesMappingUtil.java
new file mode 100644
index 0000000..aa0699a
--- /dev/null
+++ b/framework/common/src/main/java/org/apache/ofbiz/common/JsLanguageFilesMappingUtil.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.apache.ofbiz.common;
+
+/**
+ * Allow access to language files mapping from freemarker template
+ */
+public final class JsLanguageFilesMappingUtil {
+
+    private JsLanguageFilesMappingUtil() { }
+
+    public static String getFile(String libraryName, String localeString) {
+        switch (libraryName) {
+        case "datejs":
+            return JsLanguageFilesMapping.DateJs.getFilePath(localeString);
+        case "dateTime":
+            return JsLanguageFilesMapping.DateTime.getFilePath(localeString);
+        case "jquery":
+            return JsLanguageFilesMapping.JQuery.getFilePath(localeString);
+        case "select2":
+            return JsLanguageFilesMapping.Select2.getFilePath(localeString);
+        case "validation":
+            return JsLanguageFilesMapping.Validation.getFilePath(localeString);
+        default:
+            return "";
+        }
+    }
+}
diff --git a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/JsLanguageFilesMappingTransform.java b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/JsLanguageFilesMappingTransform.java
index 05198b6..53243e4 100644
--- a/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/JsLanguageFilesMappingTransform.java
+++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/JsLanguageFilesMappingTransform.java
@@ -31,6 +31,7 @@ import org.apache.ofbiz.common.JsLanguageFilesMapping;
 import freemarker.core.Environment;
 import freemarker.ext.beans.BeanModel;
 import freemarker.template.TemplateTransformModel;
+import org.apache.ofbiz.common.JsLanguageFilesMappingUtil;
 
 /**
  * access JsLanguageFilesMapping from ftl using macro
@@ -52,24 +53,7 @@ public class JsLanguageFilesMappingTransform implements TemplateTransformModel {
                     if (!libraryName.isEmpty()) {
                         HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                         String localeString = UtilHttp.getLocale(request).toString();
-                        switch (libraryName) {
-                            case "datejs":
-                                out.write(JsLanguageFilesMapping.DateJs.getFilePath(localeString));
-                                break;
-                            case "dateTime":
-                                out.write(JsLanguageFilesMapping.DateTime.getFilePath(localeString));
-                                break;
-                            case "jquery":
-                                out.write(JsLanguageFilesMapping.JQuery.getFilePath(localeString));
-                                break;
-                            case "select2":
-                                out.write(JsLanguageFilesMapping.Select2.getFilePath(localeString));
-                                break;
-                            case "validation":
-                                out.write(JsLanguageFilesMapping.Validation.getFilePath(localeString));
-                                break;
-                            default:
-                        }
+                        out.write(JsLanguageFilesMappingUtil.getFile(libraryName, localeString));
                     }
                 } catch (Exception e) {
                     Debug.logWarning(e, "Exception thrown while running " + MODULE, MODULE);
diff --git a/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl b/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl
index b174450..bec17c0 100644
--- a/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl
+++ b/themes/common-theme/template/macro/HtmlFormMacroLibrary.ftl
@@ -92,6 +92,7 @@ under the License.
 
 <#macro renderDateTimeField name className alert dateType timeDropdownParamName defaultDateTimeString localizedIconTitle timeHourName timeMinutesName minutes isTwelveHour ampmName amSelected pmSelected compositeType timeDropdown="" classString="" hour1="" hour2="" shortDateInput="" title="" value="" size="" maxlength="" id="" formName="" mask="" event="" action="" step="" timeValues="" tabindex="" disabled="">
   <span class="view-calendar">
+    <#local cultureInfo = Static["org.apache.ofbiz.common.JsLanguageFilesMappingUtil"].getFile("datejs", locale)/>
     <#if dateType!="time" >
       <input type="text" <#if tabindex?has_content> tabindex="${tabindex}"</#if> name="${name}_i18n" <@renderClass className alert /><#rt/>
         <#if title?has_content> title="${title}"</#if>
@@ -108,6 +109,7 @@ under the License.
       <#if size?has_content> size="${size}"</#if><#rt/>
       <#if maxlength?has_content>  maxlength="${maxlength}"</#if>
       <#if mask?has_content> data-mask="${mask}"</#if><#rt/>
+      <#if cultureInfo?has_content> data-cultureinfo="${cultureInfo}"</#if><#rt/>
       data-shortdate="${shortDateInput?string}"
       <#if id?has_content> id="${id}"</#if>/><#rt/>
     <#if timeDropdown?has_content && timeDropdown=="time-dropdown">
@@ -407,6 +409,7 @@ under the License.
     <#local className = className + " date-time-picker"/>
   </#if>
   <#local shortDateInput = "date" == dateType/>
+  <#local cultureInfo = Static["org.apache.ofbiz.common.JsLanguageFilesMappingUtil"].getFile("datejs", locale)/>
   <span class="view-calendar">
     <input id="${id}_fld0_value" type="text" <@renderClass className alert />
         <#if name?has_content> name="${name?html}_fld0_value"</#if>
@@ -415,6 +418,7 @@ under the License.
         <#if size?has_content> size="${size}"</#if>
         <#if maxlength?has_content> maxlength="${maxlength}"</#if>
         <#if tabindex?has_content> tabindex="${tabindex}"</#if><#rt/>
+        <#if cultureInfo?has_content> data-cultureinfo="${cultureInfo}"</#if><#rt/>
          data-shortdate="${shortDateInput?string}"
     />
     <#if titleStyle?has_content>
@@ -436,6 +440,7 @@ under the License.
         <#if value2?has_content> value="${value2}"</#if>
         <#if size?has_content> size="${size}"</#if>
         <#if maxlength?has_content> maxlength="${maxlength}"</#if>
+        <#if cultureInfo?has_content> data-cultureinfo="${cultureInfo}"</#if><#rt/>
          data-shortdate="${shortDateInput?string}"
     /><#rt/>
     <#if titleStyle?has_content>
diff --git a/themes/common-theme/webapp/common/js/util/OfbizUtil.js b/themes/common-theme/webapp/common/js/util/OfbizUtil.js
index c96ddb7..73c31f3 100644
--- a/themes/common-theme/webapp/common/js/util/OfbizUtil.js
+++ b/themes/common-theme/webapp/common/js/util/OfbizUtil.js
@@ -315,77 +315,80 @@ function bindObservers(bind_element) {
         var id = element.attr("id");
         var element_i18n = jQuery("#" + id + "_i18n");
         var shortDate = element.data("shortdate");
-        //If language specific lib is found, use date / time converter else just copy the value fields
-        if (Date.CultureInfo != undefined) {
-            var initDate = element.val();
-            if (initDate != "") {
-                var dateFormat;
-                var ofbizTime;
-                if (shortDate) {
-                    dateFormat = Date.CultureInfo.formatPatterns.shortDate;
-                    ofbizTime = "yyyy-MM-dd";
-                } else {
-                    dateFormat = Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.longTime;
-                    ofbizTime = "yyyy-MM-dd HH:mm:ss"
-                }
-                // The JS date parser doesn't understand the dot before ms in the date/time string. The ms here should be always 000
-                if (initDate.indexOf('.') != -1) {
-                    initDate = initDate.substring(0, initDate.indexOf('.'));
+        var libCultureInfo = [element.data("cultureinfo")];
+        importLibrary(libCultureInfo, function () {
+            //If language specific lib is found, use date / time converter else just copy the value fields
+            if (Date.CultureInfo != undefined) {
+                var initDate = element.val();
+                if (initDate != "") {
+                    var dateFormat;
+                    var ofbizTime;
+                    if (shortDate) {
+                        dateFormat = Date.CultureInfo.formatPatterns.shortDate;
+                        ofbizTime = "yyyy-MM-dd";
+                    } else {
+                        dateFormat = Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.longTime;
+                        ofbizTime = "yyyy-MM-dd HH:mm:ss"
+                    }
+                    // The JS date parser doesn't understand the dot before ms in the date/time string. The ms here should be always 000
+                    if (initDate.indexOf('.') != -1) {
+                        initDate = initDate.substring(0, initDate.indexOf('.'));
+                    }
+                    element.val(initDate);
+                    var dateObj = Date.parseExact(initDate, ofbizTime);
+                    var formatedObj = dateObj.toString(dateFormat);
+                    element_i18n.val(formatedObj);
                 }
-                element.val(initDate);
-                var dateObj = Date.parseExact(initDate, ofbizTime);
-                var formatedObj = dateObj.toString(dateFormat);
-                element_i18n.val(formatedObj);
-            }
 
-            element.change(function() {
-                var value = element.val();
-                var dateFormat;
-                var ofbizTime;
-                if (shortDate) {
-                    dateFormat = Date.CultureInfo.formatPatterns.shortDate;
-                    ofbizTime = "yyyy-MM-dd";
-                } else {
-                    dateFormat = Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.longTime;
-                    ofbizTime = "yyyy-MM-dd HH:mm:ss"
-                }
-                var newValue = ""
-                if (value != "") {
-                    var dateObj = Date.parseExact(value, ofbizTime);
-                    newValue = dateObj.toString(dateFormat);
-                }
-                element_i18n.val(newValue);
-            });
+                element.change(function () {
+                    var value = element.val();
+                    var dateFormat;
+                    var ofbizTime;
+                    if (shortDate) {
+                        dateFormat = Date.CultureInfo.formatPatterns.shortDate;
+                        ofbizTime = "yyyy-MM-dd";
+                    } else {
+                        dateFormat = Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.longTime;
+                        ofbizTime = "yyyy-MM-dd HH:mm:ss"
+                    }
+                    var newValue = ""
+                    if (value != "") {
+                        var dateObj = Date.parseExact(value, ofbizTime);
+                        newValue = dateObj.toString(dateFormat);
+                    }
+                    element_i18n.val(newValue);
+                });
 
-            element_i18n.change(function() {
-                var value = element_i18n.val();
-                var dateFormat;
-                var ofbizTime;
-                if (shortDate) {
-                    dateFormat = Date.CultureInfo.formatPatterns.shortDate;
-                    ofbizTime = "yyyy-MM-dd";
-                } else {
-                    dateFormat = Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.longTime;
-                    ofbizTime = "yyyy-MM-dd HH:mm:ss"
-                }
-                var newValue = "";
-                var dateObj = Date.parseExact(this.value, dateFormat);
-                if (value != "" && dateObj !== null) {
-                    newValue = dateObj.toString(ofbizTime);
-                } else { // invalid input
-                    element_i18n.val("");
-                }
-                element.val(newValue);
-            });
-        } else {
-            //fallback if no language specific js date file is found
-            element.change(function() {
-                element_i18n.val(this.value);
-            });
-            element_i18n.change(function() {
-                element.val(this.value);
-            });
-        }
+                element_i18n.change(function () {
+                    var value = element_i18n.val();
+                    var dateFormat;
+                    var ofbizTime;
+                    if (shortDate) {
+                        dateFormat = Date.CultureInfo.formatPatterns.shortDate;
+                        ofbizTime = "yyyy-MM-dd";
+                    } else {
+                        dateFormat = Date.CultureInfo.formatPatterns.shortDate + " " + Date.CultureInfo.formatPatterns.longTime;
+                        ofbizTime = "yyyy-MM-dd HH:mm:ss"
+                    }
+                    var newValue = "";
+                    var dateObj = Date.parseExact(this.value, dateFormat);
+                    if (value != "" && dateObj !== null) {
+                        newValue = dateObj.toString(ofbizTime);
+                    } else { // invalid input
+                        element_i18n.val("");
+                    }
+                    element.val(newValue);
+                });
+            } else {
+                //fallback if no language specific js date file is found
+                element.change(function () {
+                    element_i18n.val(this.value);
+                });
+                element_i18n.change(function () {
+                    element.val(this.value);
+                });
+            }
+        });
         if (shortDate) {
             element.datepicker({
                 showWeek: true,
diff --git a/themes/common-theme/widget/CommonScreens.xml b/themes/common-theme/widget/CommonScreens.xml
index 5a51420..381e5eb 100644
--- a/themes/common-theme/widget/CommonScreens.xml
+++ b/themes/common-theme/widget/CommonScreens.xml
@@ -122,7 +122,6 @@ under the License.
     <screen name="GlobalActions">
         <section>
             <actions>
-                <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.DateJs.getFilePath(initialLocaleComplete)}" global="true" />
                 <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" />
@@ -431,7 +430,6 @@ under the License.
                 <!-- The default (global) java scripts -->
                     <!-- jQuery part -->
                 <!-- Load available JS Language Files -->
-                <set field="layoutSettings.javaScripts[+0]" value="${groovy: org.apache.ofbiz.common.JsLanguageFilesMapping.DateJs.getFilePath(initialLocaleComplete)}" global="true"/>
                 <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"/>