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

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


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

commit 82c17376880a8fd8a11261e1e00dad45cfed344d
Author: Nicolas Malin <[email protected]>
AuthorDate: Thu Dec 14 18:10:28 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 | 66 ----------------------
 .../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    |  2 +-
 .../webapp/common/js/util/OfbizUtil.js             | 46 ++++++---------
 .../webapp/common/js/util/fieldlookup.js           |  2 +-
 themes/common-theme/widget/CommonScreens.xml       | 11 ++++
 themes/common-theme/widget/Theme.xml               |  1 +
 10 files changed, 49 insertions(+), 116 deletions(-)

diff --git a/applications/party/webapp/partymgr/static/PartyProfileContent.js 
b/applications/party/webapp/partymgr/static/PartyProfileContent.js
index 8765b8a24e..17f35dac14 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);
 
@@ -84,7 +82,7 @@ function checkIframeStatus() {
 }
 
 function getUploadProgressStatus(event){
-    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,
@@ -104,9 +102,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 d6b104cc50..b28af69fa0 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
@@ -27,12 +27,8 @@ import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
-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;
@@ -224,68 +220,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.<Map<String, List<String>>> 
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.<Map<String, 
String>>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 80407c67cc..250a7fccc0 100644
--- a/framework/common/webcommon/WEB-INF/common-controller.xml
+++ b/framework/common/webcommon/WEB-INF/common-controller.xml
@@ -251,18 +251,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">
@@ -354,5 +343,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 9379c33fe9..b1b4449ea0 100644
--- a/framework/common/widget/CommonScreens.xml
+++ b/framework/common/widget/CommonScreens.xml
@@ -277,6 +277,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..9bfcc08047
--- /dev/null
+++ b/themes/common-theme/template/GetUiLabels.ftl
@@ -0,0 +1 @@
+{<#list requiredLabels as label>"${label}": 
"${Static["org.apache.ofbiz.base.util.StringUtil"].wrapString(uiLabelMap[label])}"<#sep>,</#list>}
\ 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 4aa4b8a14e..1577737deb 100644
--- a/themes/common-theme/webapp/common/js/plugins/date/FromThruDateCheck.js
+++ b/themes/common-theme/webapp/common/js/plugins/date/FromThruDateCheck.js
@@ -31,7 +31,7 @@ function checkDate() {
 
   if(a.val() !="" && b.val() !="") {
     if (a.val() >= b.val()) {
-      showErrorAlertLoadUiLabel("", "", "CommonUiLabels", 
"CommonFromDateThruDateCheck")
+      showErrorAlertLoadUiLabel("", "CommonFromDateThruDateCheck")
     }
   }
 }
diff --git a/themes/common-theme/webapp/common/js/util/OfbizUtil.js 
b/themes/common-theme/webapp/common/js/util/OfbizUtil.js
index e7796efe01..2545fc97c5 100644
--- a/themes/common-theme/webapp/common/js/util/OfbizUtil.js
+++ b/themes/common-theme/webapp/common/js/util/OfbizUtil.js
@@ -1208,9 +1208,9 @@ function getJSONuiLabels(requiredLabels, callback) {
 
     if (requiredLabels != null && requiredLabels != "") {
         jQuery.ajax({
-            url: "getJSONuiLabelArray",
+            url: "getUiLabels",
             type: "POST",
-            data: {"requiredLabels" : requiredLabelsStr},
+            data: {"requiredLabels" : requiredLabelsStr, "widgetVerbose": 
false},
             complete: function(data) {
                 callback(data);
             }
@@ -1223,7 +1223,7 @@ function getJSONuiLabels(requiredLabels, callback) {
  * @param errUiLabel String
  * @returns String with Label
  */
-function getJSONuiLabel(uiResource, errUiLabel) {
+function getJSONuiLabel(errUiLabel) {
     requiredLabel = {};
     requiredLabel[uiResource] = errUiLabel;
 
@@ -1232,7 +1232,7 @@ function getJSONuiLabel(uiResource, errUiLabel) {
 
     if (requiredLabel != null && requiredLabel != "") {
         jQuery.ajax({
-            url: "getJSONuiLabel",
+            url: "getUiLabels",
             type: "POST",
             data: {"requiredLabel" : requiredLabelStr},
             success: function(data) {
@@ -1240,40 +1240,34 @@ function getJSONuiLabel(uiResource, errUiLabel) {
             }
         });
     }
-    return returnVal[arguments[0]];
+    return returnVal[errUiLabel];
 }
 
 /**
- * Opens an alert alert box with an i18n error message
+ * Opens an alert box with an i18n error message
  * @param errBoxTitleResource String - Can be empty
  * @param errBoxTitleLabel String - Can be empty
  * @param uiResource String - Required
  * @param errUiLabel String - Required
  */
-function showErrorAlertLoadUiLabel(errBoxTitleResource, errBoxTitleLabel, 
uiResource, errUiLabel) {
-    if (uiResource == null || uiResource == "" || uiResource == undefined || 
errUiLabel == null || errUiLabel == "" || errUiLabel == undefined) {
+function showErrorAlertLoadUiLabel(errBoxTitleLabel, errUiLabel) {
+    if (errUiLabel == null || errUiLabel == "" || errUiLabel == undefined) {
         // No Label Information are set, Error Msg Box can't be created
         return;
     }
 
-    var labels = {};
     var useTitle = false;
     // title can only be set when the resource and the label are set
-    if (errBoxTitleResource != null && errBoxTitleResource != "" && 
errBoxTitleLabel != null && errBoxTitleLabel != "") {
-        // create the JSON Object
-        if (errBoxTitleResource == uiResource) {
-            labels[errBoxTitleResource] = [errBoxTitleLabel, errUiLabel];
-        } else {
-            labels[errBoxTitleResource] = [errBoxTitleLabel];
-            labels[uiResource] = [errUiLabel];
-        }
+    var labels  = [errUiLabel]
+    var mapLabels = {}
+    if (errBoxTitleLabel != null && errBoxTitleLabel != "") {
         useTitle = true;
-    } else {
-        labels[uiResource] = [errUiLabel];
+        labels.add(errBoxTitleLabel)
     }
+
     // request the labels
     getJSONuiLabels(labels, function(result){
-        labels = result.responseJSON;
+        mapLabels = result.responseJSON;
     });
 
     var errMsgBox = jQuery("#contentarea").after(jQuery("<div 
id='errorAlertBox'></div>"));
@@ -1283,17 +1277,13 @@ function showErrorAlertLoadUiLabel(errBoxTitleResource, 
errBoxTitleLabel, uiReso
             modal: true,
             title: function() {
                 if (useTitle) {
-                    return labels[errBoxTitleResource][0]
+                    return mapLabels[errBoxTitleLabel]
                 } else {
                     return ""
                 }
             },
             open : function() {
-                var positionInArray = 0;
-                if (errBoxTitleResource == uiResource) {
-                    positionInArray = 1;
-                }
-                errMsgBox.html(labels[uiResource][positionInArray]);
+                errMsgBox.html(mapLabels[errUiLabel]);
             },
             buttons: {
                 Ok: function() {
@@ -1303,12 +1293,12 @@ function showErrorAlertLoadUiLabel(errBoxTitleResource, 
errBoxTitleLabel, uiReso
             }
         });
     } else {
-      alert(labels[uiResource][0]);
+      alert(mapLabels[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 fdb6f6a320..b5f5cede65 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 8ce789b2b0..0f82efdcd7 100644
--- a/themes/common-theme/widget/CommonScreens.xml
+++ b/themes/common-theme/widget/CommonScreens.xml
@@ -664,6 +664,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 8398a01a38..9a17942e32 100644
--- a/themes/common-theme/widget/Theme.xml
+++ b/themes/common-theme/widget/Theme.xml
@@ -154,6 +154,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