Skip to content

Commit

Permalink
Improve: Resolve uiLabels through ajax with dedicated screen
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nmalin committed Dec 14, 2023
1 parent 212fd5f commit b87ff9e
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 99 deletions.
14 changes: 6 additions & 8 deletions applications/party/webapp/partymgr/static/PartyProfileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@
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;
Expand Down Expand Up @@ -268,69 +264,6 @@ private static void writeJSONtoResponse(JSON json, HttpServletRequest request, H
}
}

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");
Expand Down
14 changes: 2 additions & 12 deletions framework/common/webcommon/WEB-INF/common-controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
8 changes: 8 additions & 0 deletions framework/common/widget/CommonScreens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions themes/common-theme/template/GetUiLabels.ftl
Original file line number Diff line number Diff line change
@@ -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>}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
});
Expand All @@ -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");
}
}
}
10 changes: 4 additions & 6 deletions themes/common-theme/webapp/common-theme/js/util/OfbizUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -1297,9 +1297,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;
});
Expand Down Expand Up @@ -1437,10 +1435,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);
}
Expand Down Expand Up @@ -1475,7 +1473,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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 11 additions & 0 deletions themes/common-theme/widget/CommonScreens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions themes/common-theme/widget/Theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down

0 comments on commit b87ff9e

Please sign in to comment.