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

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


The following commit(s) were added to refs/heads/release22.01 by this push:
     new ad98d78076 Improve: Resolve uiLabels through ajax with dedicated screen
ad98d78076 is described below

commit ad98d7807682ad346b200f08b90436b10082416f
Author: Nicolas Malin <[email protected]>
AuthorDate: Thu Dec 14 19:14:19 2023 +0100

    Improve: Resolve uiLabels through ajax with dedicated screen
    
    Before this improvement, you can resolve a list of labels to display with 
the uiLabel resource file
      like : "CommonUiLabels" : ["CommonUpload", "CommonSave", 
"CommonCompleted"]
    
    We remove the possibility to specify a uiLabel resource file and make it 
work by default with CommonUiLabels.
    After that we implement the json return on extend theme screen so we can 
add some uiLabel ressource by theme extension or component screen definition.
    
    Other enhancement, on getJSONuiLabels instead resolve the label with a list 
position you can call your label with it key.
          resolve : uiLabelJsonObjects.CommonUiLabels[1]
           become : uiLabelJsonObjects.CommonSave
---
 .../webapp/partymgr/static/PartyProfileContent.js  | 14 ++---
 .../java/org/apache/ofbiz/common/CommonEvents.java | 67 ----------------------
 .../common/webcommon/WEB-INF/common-controller.xml | 14 +----
 framework/common/widget/CommonScreens.xml          |  8 +++
 themes/common-theme/template/GetUiLabels.ftl       |  1 +
 .../common/js/plugins/date/FromThruDateCheck.js    |  8 +--
 .../webapp/common/js/util/OfbizUtil.js             | 10 ++--
 .../webapp/common/js/util/fieldlookup.js           |  2 +-
 themes/common-theme/widget/CommonScreens.xml       | 11 ++++
 themes/common-theme/widget/Theme.xml               |  1 +
 10 files changed, 37 insertions(+), 99 deletions(-)

diff --git a/applications/party/webapp/partymgr/static/PartyProfileContent.js 
b/applications/party/webapp/partymgr/static/PartyProfileContent.js
index 59e2e04fed..b7105b56e9 100644
--- a/applications/party/webapp/partymgr/static/PartyProfileContent.js
+++ b/applications/party/webapp/partymgr/static/PartyProfileContent.js
@@ -22,11 +22,9 @@ under the License.
  */
 var uiLabelJsonObject = null;
 jQuery(document).ready(function() {
-    var labelObject = {
-            "CommonUiLabels" : ["CommonUpload", "CommonSave", 
"CommonCompleted"]
-          };
+    var labelObject = ["CommonUpload", "CommonSave", "CommonCompleted"];
     getJSONuiLabels(labelObject, function(result){
-       uiLabelJsonObjects = result.responseJSON;
+        uiLabelJsonObjects = result.responseJSON;
     });
     jQuery("#progress_bar").progressbar({value: 0});
 });
@@ -56,7 +54,7 @@ function uploadCompleted(){
     // to the page partyContentList
     jQuery("#partyContentList").html(iframePartyContentList);
 
-    jQuery('#progressBarSavingMsg').html(uiLabelJsonObjects.CommonUiLabels[2]);
+    jQuery('#progressBarSavingMsg').html(uiLabelJsonObjects.CommonCompleted);
     // reset progressbar
     jQuery("#progress_bar").progressbar("option", "value", 0);
 
@@ -85,7 +83,7 @@ function checkIframeStatus() {
 
 function getUploadProgressStatus(event){
     importLibrary(["/common/js/jquery/plugins/fjTimer/jquerytimer-min.js"], 
function(){
-        jQuery('#uploadPartyContent').append("<span id='progressBarSavingMsg' 
class='label'>" + uiLabelJsonObjects.CommonUiLabels[0] + "...</span>");
+        jQuery('#uploadPartyContent').append("<span id='progressBarSavingMsg' 
class='label'>" + uiLabelJsonObjects.CommonUpload + "...</span>");
         var i=0;
         jQuery.fjTimer({
             interval: 1000,
@@ -105,9 +103,9 @@ function getUploadProgressStatus(event){
                          } else {
                             var readPercent = data.readPercent;
                             jQuery("#progress_bar").progressbar("option", 
"value", readPercent);
-                            
jQuery('#progressBarSavingMsg').html(uiLabelJsonObjects.CommonUiLabels[0] + 
"... (" + readPercent + "%)");
+                            
jQuery('#progressBarSavingMsg').html(uiLabelJsonObjects.CommonUpload + "... (" 
+ readPercent + "%)");
                             if(readPercent > 99){
-                                
jQuery('#progressBarSavingMsg').html(uiLabelJsonObjects.CommonUiLabels[1] + 
"...");
+                                
jQuery('#progressBarSavingMsg').html(uiLabelJsonObjects.CommonSave + "...");
                                 // stop the fjTimer
                                 timerId.stop();
                                 // call the upload complete method to do final 
stuff
diff --git 
a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java 
b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
index 0940e37969..8fa3efba25 100644
--- a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
+++ b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
@@ -33,12 +33,8 @@ import java.io.LineNumberReader;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
 import java.util.Map;
-import java.util.Set;
 
 import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletRequest;
@@ -268,69 +264,6 @@ public class CommonEvents {
         }
     }
 
-    public static String getJSONuiLabelArray(HttpServletRequest request, 
HttpServletResponse response)
-            throws UnsupportedEncodingException, IOException {
-        // Format - {resource1 : [key1, key2 ...], resource2 : [key1, key2, 
...], ...}
-        String jsonString = request.getParameter("requiredLabels");
-        Map<String, List<String>> uiLabelObject = null;
-        if (UtilValidate.isNotEmpty(jsonString)) {
-            JSON json = JSON.from(jsonString);
-            uiLabelObject = UtilGenerics.cast(json.toObject(Map.class));
-        }
-        if (UtilValidate.isEmpty(uiLabelObject)) {
-            Debug.logError("No resource and labels found in JSON string: " + 
jsonString, MODULE);
-            return "error";
-        }
-        Locale locale = UtilHttp.getLocale(request);
-        Map<String, List<String>> uiLabelMap = new HashMap<>();
-        Set<Map.Entry<String, List<String>>> entrySet = 
uiLabelObject.entrySet();
-        for (Map.Entry<String, List<String>> entry : entrySet) {
-            String resource = entry.getKey();
-            List<String> resourceKeys = entry.getValue();
-            if (resourceKeys != null) {
-                List<String> labels = new ArrayList<>(resourceKeys.size());
-                for (String resourceKey : resourceKeys) {
-                    String label = UtilProperties.getMessage(resource, 
resourceKey, locale);
-                    labels.add(label);
-                }
-                uiLabelMap.put(resource, labels);
-            }
-        }
-        writeJSONtoResponse(JSON.from(uiLabelMap), request, response);
-        return "success";
-    }
-
-    public static String getJSONuiLabel(HttpServletRequest request, 
HttpServletResponse response)
-            throws UnsupportedEncodingException, IOException {
-        // Format - {resource : key}
-        String jsonString = request.getParameter("requiredLabel");
-        Map<String, String> uiLabelObject = null;
-        if (UtilValidate.isNotEmpty(jsonString)) {
-            JSON json = JSON.from(jsonString);
-            uiLabelObject = UtilGenerics.cast(json.toObject(Map.class));
-        }
-        if (UtilValidate.isEmpty(uiLabelObject)) {
-            Debug.logError("No resource and labels found in JSON string: " + 
jsonString, MODULE);
-            return "error";
-        } else if (uiLabelObject.size() > 1) {
-            Debug.logError("More than one resource found, please use the 
method: getJSONuiLabelArray", MODULE);
-            return "error";
-        }
-        Locale locale = UtilHttp.getLocale(request);
-        Map<String, String> uiLabelMap = new HashMap<>();
-        Set<Map.Entry<String, String>> entrySet = uiLabelObject.entrySet();
-        for (Map.Entry<String, String> entry : entrySet) {
-            String resource = entry.getKey();
-            String resourceKey = entry.getValue();
-            if (resourceKey != null) {
-                String label = UtilProperties.getMessage(resource, 
resourceKey, locale);
-                uiLabelMap.put(resource, label);
-            }
-        }
-        writeJSONtoResponse(JSON.from(uiLabelMap), request, response);
-        return "success";
-    }
-
     public static String getCaptcha(HttpServletRequest request, 
HttpServletResponse response) {
         try {
             Delegator delegator = (Delegator) 
request.getAttribute("delegator");
diff --git a/framework/common/webcommon/WEB-INF/common-controller.xml 
b/framework/common/webcommon/WEB-INF/common-controller.xml
index e35662c761..902d0961c5 100644
--- a/framework/common/webcommon/WEB-INF/common-controller.xml
+++ b/framework/common/webcommon/WEB-INF/common-controller.xml
@@ -267,18 +267,7 @@ under the License.
     </request-map>
 
     <!-- Common JavaScript uiLabel Request, to receive uiLabels within regular 
JS Code -->
-    <request-map uri="getJSONuiLabelArray">
-        <security https="true" auth="false"/>
-        <event type="java" path="org.apache.ofbiz.common.CommonEvents" 
invoke="getJSONuiLabelArray"/>
-        <response name="success" type="none" />
-        <response name="error" type="none" />
-    </request-map>
-    <request-map uri="getJSONuiLabel">
-        <security https="true" auth="false"/>
-        <event type="java" path="org.apache.ofbiz.common.CommonEvents" 
invoke="getJSONuiLabel"/>
-        <response name="success" type="none" />
-        <response name="error" type="none" />
-    </request-map>
+    <request-map uri="getUiLabels"><security https="true" 
auth="false"/><response name="success" type="view" 
value="GetUiLabels"/></request-map>
 
     <!-- Common JavaScript loadJWT Request, to receive JwtToken within regular 
JS Code -->
     <request-map uri="loadJWT">
@@ -377,5 +366,6 @@ under the License.
     <view-map name="LookupGeoName" type="screen" 
page="component://common/widget/LookupScreens.xml#LookupGeoName"/>
     <view-map name="LookupLocale" type="screen" 
page="component://common/widget/LookupScreens.xml#LookupLocale"/>
     <view-map name="forgotPassword" type="screen" 
page="component://common/widget/CommonScreens.xml#forgotPassword"/>
+    <view-map name="GetUiLabels" type="screentext" 
page="component://common/widget/CommonScreens.xml#GetUiLabels" 
content-type="application/json"/>
 
 </site-conf>
diff --git a/framework/common/widget/CommonScreens.xml 
b/framework/common/widget/CommonScreens.xml
index ca005a16c0..39539e8171 100644
--- a/framework/common/widget/CommonScreens.xml
+++ b/framework/common/widget/CommonScreens.xml
@@ -282,6 +282,14 @@ under the License.
             </widgets>
         </section>
     </screen>
+    <screen name="GetUiLabels">
+        <section>
+            <widgets>
+                <include-screen name="MinimalActions" />
+                <include-screen name="GetUiLabels" 
location="${groovy:commonScreenLocations.GetUiLabels?commonScreenLocations.GetUiLabels:commonDecoratorLocation}"/>
+            </widgets>
+        </section>
+    </screen>
 
     <screen name="EventMessages">
         <section>
diff --git a/themes/common-theme/template/GetUiLabels.ftl 
b/themes/common-theme/template/GetUiLabels.ftl
new file mode 100644
index 0000000000..cd506a0cfe
--- /dev/null
+++ b/themes/common-theme/template/GetUiLabels.ftl
@@ -0,0 +1 @@
+{<#if requiredLabels?has_content && requiredLabels?is_sequence><#list 
requiredLabels as label>"${label}": 
"${Static["org.apache.ofbiz.base.util.StringUtil"].wrapString(uiLabelMap[label])}"<#sep>,</#list></#if>}
\ No newline at end of file
diff --git 
a/themes/common-theme/webapp/common/js/plugins/date/FromThruDateCheck.js 
b/themes/common-theme/webapp/common/js/plugins/date/FromThruDateCheck.js
index 1c41e0bb19..8721cab7a6 100644
--- a/themes/common-theme/webapp/common/js/plugins/date/FromThruDateCheck.js
+++ b/themes/common-theme/webapp/common/js/plugins/date/FromThruDateCheck.js
@@ -26,11 +26,9 @@ jQuery(document).ready( function() {
   jQuery("input[name*='thruDate']").on('focusout', checkDate);
   if (jQuery("input[name*='fromDate']").length !== 0) {
     // retrieve label for date control
-    labelObject = {
-      "CommonUiLabels" : ["CommonFromDateThruDateCheck"],
-    };
+    labelObject = ["CommonFromDateThruDateCheck"];
     getJSONuiLabels(labelObject, function(result){
-      labelObject   = result.responseJSON;
+      labelObject = result.responseJSON;
     });
   }
 });
@@ -41,7 +39,7 @@ function checkDate() {
 
   if(a.val() !="" && b.val() !="") {
     if (a.val() >= b.val()) {
-      showjGrowlMessage(labelObject.CommonUiLabels[0], 'errorMessageJGrowl', 
true, null, null, null, "center");
+      showjGrowlMessage(labelObject.CommonFromDateThruDateCheck, 
'errorMessageJGrowl', true, null, null, null, "center");
     }
   }
 }
diff --git a/themes/common-theme/webapp/common/js/util/OfbizUtil.js 
b/themes/common-theme/webapp/common/js/util/OfbizUtil.js
index a56289725d..ba84c70a70 100644
--- a/themes/common-theme/webapp/common/js/util/OfbizUtil.js
+++ b/themes/common-theme/webapp/common/js/util/OfbizUtil.js
@@ -1281,9 +1281,7 @@ function showjGrowl(showAllLabel, collapseLabel, 
hideAllLabel, jGrowlPosition, j
 
 function showjGrowlMessage(errMessage, classEvent, stickyValue, showAllLabel, 
collapseLabel, hideAllLabel, jGrowlPosition, jGrowlWidth, jGrowlHeight, 
jGrowlSpeed) {
     if (!showAllLabel || !collapseLabel || !hideAllLabel) {
-        var jGrowlLabelObject = {
-            "CommonUiLabels": ["CommonHideAllNotifications", "CommonShowAll", 
"CommonCollapse"],
-        };
+        var jGrowlLabelObject = ["CommonHideAllNotifications", 
"CommonShowAll", "CommonCollapse"];
         getJSONuiLabels(jGrowlLabelObject, function (result) {
             jGrowlLabelObject = result.responseJSON.CommonUiLabels;
         });
@@ -1421,10 +1419,10 @@ function getJSONuiLabels(requiredLabels, callback) {
 
     if (requiredLabels != null && requiredLabels != "") {
         jQuery.ajax({
-            url: "getJSONuiLabelArray",
+            url: "getUiLabels",
             type: "POST",
             async: false,
-            data: {"requiredLabels" : requiredLabelsStr},
+            data: {"requiredLabels" : requiredLabelsStr, "widgetVerbose": 
false},
             complete: function(data) {
                 callback(data);
             }
@@ -1459,7 +1457,7 @@ function getJSONuiLabel(uiResource, errUiLabel) {
 }
 
 /**
- * Opens an alert alert box. This function is for a direct call from the ftl 
files where you can direcetly resolve youre labels
+ * Opens an alert box. This function is for a direct call from the ftl files 
where you can directly resolve your labels
  * @param errBoxTitle String - Can be empty
  * @param errMessage String - Required - i18n Error Message
  */
diff --git a/themes/common-theme/webapp/common/js/util/fieldlookup.js 
b/themes/common-theme/webapp/common/js/util/fieldlookup.js
index 0e8bb118b2..75eb22005d 100644
--- a/themes/common-theme/webapp/common/js/util/fieldlookup.js
+++ b/themes/common-theme/webapp/common/js/util/fieldlookup.js
@@ -36,7 +36,7 @@ function getViewNameWithSeparator(view_name) {
 }
 
 function lookup_error(str_message) {
-    var CommonErrorMessage2 = getJSONuiLabel("CommonUiLabels", 
"CommonErrorMessage2");
+    var CommonErrorMessage2 = getJSONuiLabel("CommonErrorMessage2");
     showErrorAlert(CommonErrorMessage2, str_message);
 }
 
diff --git a/themes/common-theme/widget/CommonScreens.xml 
b/themes/common-theme/widget/CommonScreens.xml
index 61ddbe5f1c..8a9b5d8124 100644
--- a/themes/common-theme/widget/CommonScreens.xml
+++ b/themes/common-theme/widget/CommonScreens.xml
@@ -658,6 +658,17 @@ under the License.
             </widgets>
         </section>
     </screen>
+    <screen name="GetUiLabels">
+        <section>
+            <actions>
+                <property-map resource="CommonUiLabels" map-name="uiLabelMap" 
global="true"/>
+                <set field="requiredLabels" value="${groovy: new 
groovy.json.JsonSlurper().parseText(parameters.requiredLabels)}"/>
+            </actions>
+            <widgets>
+                <platform-specific><text><html-template 
location="component://common-theme/template/GetUiLabels.ftl"/></text></platform-specific>
+            </widgets>
+        </section>
+    </screen>
 
     <screen name="EventMessages">
         <section>
diff --git a/themes/common-theme/widget/Theme.xml 
b/themes/common-theme/widget/Theme.xml
index a4bdf2861d..7ecb3848ed 100644
--- a/themes/common-theme/widget/Theme.xml
+++ b/themes/common-theme/widget/Theme.xml
@@ -146,6 +146,7 @@ under the License.
             <screen name="ajaxNotLoggedIn"/>
             <screen name="requirePasswordChange"/>
             <screen name="forgotPassword"/>
+            <screen name="GetUiLabels"/>
             <screen name="help"/>
             <screen name="viewBlocked"/>
             <screen name="TimeDuration" 
location="component://common-theme/widget/LookupScreens.xml"/>

Reply via email to