[ofbiz-framework] branch trunk updated: Improved: Freemarker macro for JsLanguageFilesMapping (OFBIZ-11958)

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: Freemarker macro for JsLanguageFilesMapping (OFBIZ-11958)

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 49e365d  Improved: Freemarker macro for JsLanguageFilesMapping (OFBIZ-11958)
49e365d is described below

commit 49e365d9a280973af7c92fc4c6af12ba3059f9d3
Author: James Yong <[hidden email]>
AuthorDate: Sat Aug 15 18:42:52 2020 +0800

    Improved: Freemarker macro for JsLanguageFilesMapping (OFBIZ-11958)
   
    A macro is needed to get info from JsLanguageFilesMapping class.
    <@jsLangFilesMap>select2</@jsLangFilesMap>
---
 .../ftl/JsLanguageFilesMappingTransform.java       | 70 ++++++++++++++++++++++
 .../ofbiz/webapp/freemarkerTransforms.properties   |  1 +
 .../widget/model/MultiBlockHtmlTemplateUtil.java   |  8 ---
 3 files changed, 71 insertions(+), 8 deletions(-)

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
new file mode 100644
index 0000000..643dd2a
--- /dev/null
+++ b/framework/webapp/src/main/java/org/apache/ofbiz/webapp/ftl/JsLanguageFilesMappingTransform.java
@@ -0,0 +1,70 @@
+package org.apache.ofbiz.webapp.ftl;
+
+import freemarker.core.Environment;
+import freemarker.ext.beans.BeanModel;
+import freemarker.template.TemplateTransformModel;
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilHttp;
+import org.apache.ofbiz.common.JsLanguageFilesMapping;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Map;
+
+/**
+ * access JsLanguageFilesMapping from ftl using macro
+ */
+public class JsLanguageFilesMappingTransform implements TemplateTransformModel {
+
+    private static final String MODULE = JsLanguageFilesMappingTransform.class.getName();
+
+    @Override
+    public Writer getWriter(Writer out, Map args) {
+        final StringBuilder buf = new StringBuilder();
+        return new Writer(out) {
+            @Override
+            public void close() throws IOException {
+                try {
+                    Environment env = Environment.getCurrentEnvironment();
+                    BeanModel req = (BeanModel) env.getVariable("request");
+                    String libraryName = buf.toString();
+                    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:
+                        }
+                    }
+                } catch (Exception e) {
+                    Debug.logWarning(e, "Exception thrown while running " + MODULE, MODULE);
+                    throw new IOException(e);
+                }
+            }
+            @Override
+            public void flush() throws IOException {
+                out.flush();
+            }
+
+            @Override
+            public void write(char cbuf[], int off, int len) {
+                buf.append(cbuf, off, len);
+            }
+        };
+    }
+}
diff --git a/framework/webapp/src/main/resources/org/apache/ofbiz/webapp/freemarkerTransforms.properties b/framework/webapp/src/main/resources/org/apache/ofbiz/webapp/freemarkerTransforms.properties
index caf421d..755ef77 100644
--- a/framework/webapp/src/main/resources/org/apache/ofbiz/webapp/freemarkerTransforms.properties
+++ b/framework/webapp/src/main/resources/org/apache/ofbiz/webapp/freemarkerTransforms.properties
@@ -32,3 +32,4 @@ setContextField=org.apache.ofbiz.webapp.ftl.SetContextFieldTransform
 csrfTokenAjax=org.apache.ofbiz.webapp.ftl.CsrfTokenAjaxTransform
 csrfTokenPair=org.apache.ofbiz.webapp.ftl.CsrfTokenPairNonAjaxTransform
 scriptTagsFooter=org.apache.ofbiz.webapp.ftl.ScriptTagsFooterTransform
+jsLangFilesMap=org.apache.ofbiz.webapp.ftl.JsLanguageFilesMappingTransform
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 216412c..3717c58 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
@@ -343,14 +343,6 @@ public final class MultiBlockHtmlTemplateUtil {
                 // 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);