From 7ce0bd69a8a347bcb5f48ea1afd94285af6961c0 Mon Sep 17 00:00:00 2001 From: Brian Federle Date: Tue, 9 Jul 2013 15:44:06 -0700 Subject: [PATCH] UI plugin framework: Fix load order Fixes potential issue where plugins could load after cloudStack UI is initialized, preventing their functionality from showing up. Now, the main UI is only loaded after all plugins have loaded; this is via 'cloudStack.pluginReady' event. --- ui/scripts/cloudStack.js | 248 +++++++++++++++++----------------- ui/scripts/plugins.js | 10 ++ ui/scripts/ui-custom/login.js | 3 + 3 files changed, 137 insertions(+), 124 deletions(-) diff --git a/ui/scripts/cloudStack.js b/ui/scripts/cloudStack.js index 12d2c0cc61f..13856070388 100644 --- a/ui/scripts/cloudStack.js +++ b/ui/scripts/cloudStack.js @@ -64,48 +64,48 @@ } }); - $(function() { + $(window).bind('cloudStack.pluginReady', function() { // Get language g_lang = $.cookie('lang') ? $.cookie('lang') : 'en'; /** * Generic error handling */ - + $.ajaxSetup({ - url: clientApiUrl, + url: clientApiUrl, async: true, dataType: 'json', cache: false, error: function(data) { - var clickAction = false; - if (isValidJsonString(data.responseText)) { - var json = JSON.parse(data.responseText); - if (json != null) { - var property; - for(property in json) {} - var errorObj = json[property]; - if(errorObj.errorcode == 401 && errorObj.errortext == "unable to verify user credentials and/or request signature") { - clickAction = function() { - $('#user-options a').eq(0).trigger('click'); - }; - } - } - } - cloudStack.dialog.notice({ message: parseXMLHttpResponse(data), clickAction: clickAction }); + var clickAction = false; + if (isValidJsonString(data.responseText)) { + var json = JSON.parse(data.responseText); + if (json != null) { + var property; + for(property in json) {} + var errorObj = json[property]; + if(errorObj.errorcode == 401 && errorObj.errortext == "unable to verify user credentials and/or request signature") { + clickAction = function() { + $('#user-options a').eq(0).trigger('click'); + }; + } + } + } + cloudStack.dialog.notice({ message: parseXMLHttpResponse(data), clickAction: clickAction }); }, - beforeSend: function(XMLHttpRequest) { - if (g_mySession == $.cookie("JSESSIONID")) { - return true; - } - else { - var clickAction = function() { - $('#user-options a').eq(0).trigger('click'); - }; - cloudStack.dialog.notice({ message: _l('label.session.expired'), clickAction: clickAction }); - return false; - } - } + beforeSend: function(XMLHttpRequest) { + if (g_mySession == $.cookie("JSESSIONID")) { + return true; + } + else { + var clickAction = function() { + $('#user-options a').eq(0).trigger('click'); + }; + cloudStack.dialog.notice({ message: _l('label.session.expired'), clickAction: clickAction }); + return false; + } + } }); var $container = $('#cloudStack3-container'); @@ -115,42 +115,42 @@ // Use this for checking the session, to bypass login screen bypassLoginCheck: function(args) { //determine to show or bypass login screen - if (g_loginResponse == null) { //show login screen - /* - but if this is a 2nd browser window (of the same domain), login screen still won't show because $.cookie('sessionKey') is valid for 2nd browser window (of the same domain) as well. - i.e. calling listCapabilities API with g_sessionKey from $.cookie('sessionKey') will succeed, - then userValid will be set to true, then an user object (instead of "false") will be returned, then login screen will be bypassed. - */ - g_mySession = $.cookie('JSESSIONID'); - g_sessionKey = $.cookie('sessionKey'); - g_role = $.cookie('role'); - g_username = $.cookie('username'); - g_userid = $.cookie('userid'); - g_account = $.cookie('account'); - g_domainid = $.cookie('domainid'); - g_userfullname = $.cookie('userfullname'); - g_timezone = $.cookie('timezone'); - if($.cookie('timezoneoffset') != null) - g_timezoneoffset = isNaN($.cookie('timezoneoffset'))? null: parseFloat($.cookie('timezoneoffset')); - else - g_timezoneoffset = null; + if (g_loginResponse == null) { //show login screen + /* + but if this is a 2nd browser window (of the same domain), login screen still won't show because $.cookie('sessionKey') is valid for 2nd browser window (of the same domain) as well. + i.e. calling listCapabilities API with g_sessionKey from $.cookie('sessionKey') will succeed, + then userValid will be set to true, then an user object (instead of "false") will be returned, then login screen will be bypassed. + */ + g_mySession = $.cookie('JSESSIONID'); + g_sessionKey = $.cookie('sessionKey'); + g_role = $.cookie('role'); + g_username = $.cookie('username'); + g_userid = $.cookie('userid'); + g_account = $.cookie('account'); + g_domainid = $.cookie('domainid'); + g_userfullname = $.cookie('userfullname'); + g_timezone = $.cookie('timezone'); + if($.cookie('timezoneoffset') != null) + g_timezoneoffset = isNaN($.cookie('timezoneoffset'))? null: parseFloat($.cookie('timezoneoffset')); + else + g_timezoneoffset = null; } - else { //single-sign-on (bypass login screen) - g_mySession = $.cookie('JSESSIONID'); - g_sessionKey = encodeURIComponent(g_loginResponse.sessionkey); - g_role = g_loginResponse.type; - g_username = g_loginResponse.username; - g_userid = g_loginResponse.userid; - g_account = g_loginResponse.account; - g_domainid = g_loginResponse.domainid; - g_userfullname = g_loginResponse.firstname + ' ' + g_loginResponse.lastname; - g_timezone = g_loginResponse.timezone; - if(g_loginResponse.timezoneoffset != null) - g_timezoneoffset = isNaN(g_loginResponse.timezoneoffset)? null: parseFloat(g_loginResponse.timezoneoffset); - else - g_timezoneoffset = null; - } - + else { //single-sign-on (bypass login screen) + g_mySession = $.cookie('JSESSIONID'); + g_sessionKey = encodeURIComponent(g_loginResponse.sessionkey); + g_role = g_loginResponse.type; + g_username = g_loginResponse.username; + g_userid = g_loginResponse.userid; + g_account = g_loginResponse.account; + g_domainid = g_loginResponse.domainid; + g_userfullname = g_loginResponse.firstname + ' ' + g_loginResponse.lastname; + g_timezone = g_loginResponse.timezone; + if(g_loginResponse.timezoneoffset != null) + g_timezoneoffset = isNaN(g_loginResponse.timezoneoffset)? null: parseFloat(g_loginResponse.timezoneoffset); + else + g_timezoneoffset = null; + } + var userValid = false; $.ajax({ url: createURL("listCapabilities"), @@ -158,35 +158,35 @@ async: false, success: function(json) { g_capabilities = json.listcapabilitiesresponse.capability; - $.cookie('capabilities', g_capabilities, { expires: 1}); - + $.cookie('capabilities', g_capabilities, { expires: 1}); + g_supportELB = json.listcapabilitiesresponse.capability.supportELB.toString(); //convert boolean to string if it's boolean $.cookie('supportELB', g_supportELB, { expires: 1}); - + if (json.listcapabilitiesresponse.capability.userpublictemplateenabled != null) { g_userPublicTemplateEnabled = json.listcapabilitiesresponse.capability.userpublictemplateenabled.toString(); //convert boolean to string if it's boolean $.cookie('userpublictemplateenabled', g_userPublicTemplateEnabled, { expires: 1}); - } - - g_userProjectsEnabled = json.listcapabilitiesresponse.capability.allowusercreateprojects; + } + + g_userProjectsEnabled = json.listcapabilitiesresponse.capability.allowusercreateprojects; $.cookie('userProjectsEnabled', g_userProjectsEnabled, { expires: 1 }); - + g_cloudstackversion = json.listcapabilitiesresponse.capability.cloudstackversion; - - if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) { - var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side - //intervalLimit = 9999; //this line is for testing only, comment it before check in - if(intervalLimit > g_queryAsyncJobResultInterval) - g_queryAsyncJobResultInterval = intervalLimit; - } - + + if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) { + var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side + //intervalLimit = 9999; //this line is for testing only, comment it before check in + if(intervalLimit > g_queryAsyncJobResultInterval) + g_queryAsyncJobResultInterval = intervalLimit; + } + userValid = true; }, - error: function(xmlHTTP) { //override default error handling, do nothing instead of showing error "unable to verify user credentials" on login screen + error: function(xmlHTTP) { //override default error handling, do nothing instead of showing error "unable to verify user credentials" on login screen }, - beforeSend : function(XMLHttpResponse) { - return true; - } + beforeSend : function(XMLHttpResponse) { + return true; + } }); if (userValid && isAdmin()) { @@ -228,7 +228,7 @@ username: g_username, account: g_account, name: g_userfullname, - role: g_role, + role: g_role, domainid: g_domainid } } : false; @@ -259,20 +259,20 @@ else { array1.push("&domain=" + encodeURIComponent("/")); } - - var loginCmdText = array1.join(""); - + + var loginCmdText = array1.join(""); + $.ajax({ type: "POST", - data: "command=login" + loginCmdText + "&response=json", + data: "command=login" + loginCmdText + "&response=json", dataType: "json", async: false, - success: function(json) { + success: function(json) { var loginresponse = json.loginresponse; g_mySession = $.cookie('JSESSIONID'); g_sessionKey = encodeURIComponent(loginresponse.sessionkey); - g_role = loginresponse.type; + g_role = loginresponse.type; g_username = loginresponse.username; g_userid = loginresponse.userid; g_account = loginresponse.account; @@ -285,20 +285,20 @@ $.cookie('username', g_username, { expires: 1}); $.cookie('account', g_account, { expires: 1}); $.cookie('domainid', g_domainid, { expires: 1}); - $.cookie('role', g_role, { expires: 1}); + $.cookie('role', g_role, { expires: 1}); $.cookie('timezoneoffset', g_timezoneoffset, { expires: 1}); $.cookie('timezone', g_timezone, { expires: 1}); $.cookie('userfullname', g_userfullname, { expires: 1 }); $.cookie('userid', g_userid, { expires: 1 }); $.ajax({ - url: createURL("listCapabilities"), + url: createURL("listCapabilities"), dataType: "json", async: false, success: function(json) { g_capabilities = json.listcapabilitiesresponse.capability; - $.cookie('capabilities', g_capabilities, { expires: 1}); - + $.cookie('capabilities', g_capabilities, { expires: 1}); + g_supportELB = json.listcapabilitiesresponse.capability.supportELB.toString(); //convert boolean to string if it's boolean $.cookie('supportELB', g_supportELB, { expires: 1}); @@ -309,16 +309,16 @@ g_userProjectsEnabled = json.listcapabilitiesresponse.capability.allowusercreateprojects; $.cookie('userProjectsEnabled', g_userProjectsEnabled, { expires: 1 }); - + g_cloudstackversion = json.listcapabilitiesresponse.capability.cloudstackversion; - - if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) { - var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side - //intervalLimit = 8888; //this line is for testing only, comment it before check in - if(intervalLimit > g_queryAsyncJobResultInterval) - g_queryAsyncJobResultInterval = intervalLimit; - } - + + if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) { + var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side + //intervalLimit = 8888; //this line is for testing only, comment it before check in + if(intervalLimit > g_queryAsyncJobResultInterval) + g_queryAsyncJobResultInterval = intervalLimit; + } + args.response.success({ data: { user: $.extend(true, {}, loginresponse, { @@ -331,7 +331,7 @@ }, error: function(xmlHTTP) { args.response.error(); - } + } }); if (isAdmin()) { @@ -371,17 +371,17 @@ // TEMPORARY -- replace w/ output of capability response, etc., once implemented window.g_projectsInviteRequired = false; }, - error: function(XMLHttpRequest) { - var errorMsg = parseXMLHttpResponse(XMLHttpRequest); - if(errorMsg.length == 0 && XMLHttpRequest.status == 0) - errorMsg = dictionary['error.unable.to.reach.management.server']; - else - errorMsg = _l('error.invalid.username.password'); //override error message - args.response.error(errorMsg); - }, - beforeSend : function(XMLHttpResponse) { - return true; - } + error: function(XMLHttpRequest) { + var errorMsg = parseXMLHttpResponse(XMLHttpRequest); + if(errorMsg.length == 0 && XMLHttpRequest.status == 0) + errorMsg = dictionary['error.unable.to.reach.management.server']; + else + errorMsg = _l('error.invalid.username.password'); //override error message + args.response.error(errorMsg); + }, + beforeSend : function(XMLHttpResponse) { + return true; + } }); }, @@ -413,16 +413,16 @@ if(onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen. document.location.reload(); //when onLogoutCallback() returns true, reload the current document. - } + } }, error: function() { - if(onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen. + if(onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen. document.location.reload(); //when onLogoutCallback() returns true, reload the current document. - } - }, - beforeSend : function(XMLHttpResponse) { - return true; - } + } + }, + beforeSend : function(XMLHttpResponse) { + return true; + } }); }, @@ -479,7 +479,7 @@ // SSO loginArgs.hideLoginScreen = true; } - + cloudStack.uiCustom.login(loginArgs); // Localization diff --git a/ui/scripts/plugins.js b/ui/scripts/plugins.js index 6a886ba0ae6..3c5bc0f73d9 100644 --- a/ui/scripts/plugins.js +++ b/ui/scripts/plugins.js @@ -65,6 +65,9 @@ show: cloudStack.uiCustom.pluginListing }; + var loadedPlugins = 0; + var pluginTotal = cloudStack.plugins.length + cloudStack.modules.length; + // Load $(['modules', 'plugins']).each(function() { var type = this; @@ -96,6 +99,13 @@ } }) ); + + loadedPlugins = loadedPlugins + 1; + + if (loadedPlugins === pluginTotal) { + $(window).trigger('cloudStack.pluginReady'); + } + loadCSS(css); }); } diff --git a/ui/scripts/ui-custom/login.js b/ui/scripts/ui-custom/login.js index 7a3b887d783..0dbbf822dbd 100644 --- a/ui/scripts/ui-custom/login.js +++ b/ui/scripts/ui-custom/login.js @@ -31,6 +31,7 @@ complete({ user: bypass.user }); + $(window).trigger('cloudStack.init'); return; } @@ -140,5 +141,7 @@ ) )); } + + $(window).trigger('cloudStack.init'); }; })(jQuery, cloudStack);