浏览代码

improve locale menu handler

jrivard@gmail.com 6 年之前
父节点
当前提交
866be0e861

+ 13 - 0
server/src/main/java/password/pwm/http/tag/conditional/PwmIfTest.java

@@ -40,6 +40,7 @@ import password.pwm.http.PwmRequestFlag;
 import password.pwm.http.servlet.peoplesearch.PeopleSearchConfiguration;
 import password.pwm.ldap.UserInfo;
 import password.pwm.svc.PwmService;
+import password.pwm.util.java.StringUtil;
 
 import java.util.Arrays;
 
@@ -61,6 +62,7 @@ public enum PwmIfTest
     permission( new BooleanPermissionTest() ),
     otpSetupEnabled( new SetupOTPEnabled() ),
     hasStoredOtpTimestamp( new HasStoredOtpTimestamp() ),
+    hasCustomJavascript( new HasCustomJavascript() ),
     setupChallengeEnabled( new BooleanPwmSettingTest( PwmSetting.CHALLENGE_ENABLE ) ),
     shortcutsEnabled( new BooleanPwmSettingTest( PwmSetting.SHORTCUT_ENABLE ) ),
     peopleSearchEnabled( new BooleanPwmSettingTest( PwmSetting.PEOPLE_SEARCH_ENABLE ) ),
@@ -511,4 +513,15 @@ public enum PwmIfTest
             return setupOtpProfile != null && setupOtpProfile.readSettingAsBoolean( PwmSetting.OTP_ALLOW_SETUP );
         }
     }
+
+    private static class HasCustomJavascript implements Test
+    {
+        @Override
+        public boolean test( final PwmRequest pwmRequest, final PwmIfOptions options )
+        {
+            final String customJs = pwmRequest.getConfig().readSettingAsString( PwmSetting.DISPLAY_CUSTOM_JAVASCRIPT );
+            return !StringUtil.isEmpty( customJs );
+        }
+    }
+
 }

+ 9 - 3
webapp/src/main/webapp/WEB-INF/jsp/fragment/footer.jsp

@@ -68,11 +68,17 @@
 </pwm:if>
 <pwm:script>
     <script type="text/javascript">
-        PWM_GLOBAL['startupFunctions'].push(function() {
-            <pwm:value name="<%=PwmValue.customJavascript%>"/>
-        });
         var dojoConfig = { has: { "csp-restrictions":false }, async:true }
     </script>
 </pwm:script>
+<pwm:if test="<%=PwmIfTest.hasCustomJavascript%>">
+    <pwm:script>
+        <script type="text/javascript">
+            PWM_GLOBAL['startupFunctions'].push(function() {
+                <pwm:value name="<%=PwmValue.customJavascript%>"/>
+            });
+        </script>
+    </pwm:script>
+</pwm:if>
 <script nonce="<pwm:value name="<%=PwmValue.cspNonce%>"/>" dojo-sync-loader="false" type="text/javascript" src="<pwm:url addContext="true" url='/public/resources/webjars/dojo/dojo.js'/>"></script><noscript></noscript>
 <pwm:script-ref url="/public/resources/js/main.js"/>

+ 19 - 11
webapp/src/main/webapp/public/resources/js/main.js

@@ -655,20 +655,28 @@ PWM_MAIN.showLocaleSelectionMenu = function(nextFunction, options) {
 
 
 PWM_MAIN.initLocaleSelectorMenu = function(attachNode) {
-
     PWM_MAIN.addEventHandler(attachNode,'click',function(){
         PWM_MAIN.showLocaleSelectionMenu(function(localeKey){
-            require(["dojo"], function (dojo) {
-                var nextUrl = window.location.toString();
-                if (window.location.toString().indexOf('?') > 0) {
-                    var params = dojo.queryToObject(window.location.search.substring(1, window.location.search.length));
-                    params['locale'] = localeKey;
-                    nextUrl = window.location.toString().substring(0, window.location.toString().indexOf('?') + 1)
-                        + dojo.objectToQuery(params);
-                } else {
-                    nextUrl = PWM_MAIN.addParamToUrl(nextUrl, 'locale', localeKey)
+
+            PWM_MAIN.showWaitDialog({loadFunction:function(){
+                    var url = PWM_GLOBAL['url-context'] + '/public/api?locale=' + localeKey;
+                    PWM_MAIN.ajaxRequest(url, function(){
+                        try {
+                            var newLocation = window.location;
+                            var searchParams = new URLSearchParams(newLocation.search);
+                            if ( searchParams.has('locale')) {
+                                searchParams.set('locale', localeKey);
+                                newLocation.search = searchParams.toString();
+                                window.location = newLocation;
+                                return;
+                            }
+                        } catch (e) {
+                            PWM_MAIN.log('error replacing locale parameter on existing url: ' + e);
+                        }
+
+                        window.location.reload(true);
+                    });
                 }
-                PWM_MAIN.gotoUrl(nextUrl);
             });
         });
     });