Skip to content

Commit 7ce0bd6

Browse files
committed
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.
1 parent f7f826d commit 7ce0bd6

3 files changed

Lines changed: 137 additions & 124 deletions

File tree

‎ui/scripts/cloudStack.js‎

Lines changed: 124 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -64,48 +64,48 @@
6464
}
6565
});
6666

67-
$(function() {
67+
$(window).bind('cloudStack.pluginReady', function() {
6868
// Get language
6969
g_lang = $.cookie('lang') ? $.cookie('lang') : 'en';
7070

7171
/**
7272
* Generic error handling
7373
*/
74-
74+
7575
$.ajaxSetup({
76-
url: clientApiUrl,
76+
url: clientApiUrl,
7777
async: true,
7878
dataType: 'json',
7979
cache: false,
8080
error: function(data) {
81-
var clickAction = false;
82-
if (isValidJsonString(data.responseText)) {
83-
var json = JSON.parse(data.responseText);
84-
if (json != null) {
85-
var property;
86-
for(property in json) {}
87-
var errorObj = json[property];
88-
if(errorObj.errorcode == 401 && errorObj.errortext == "unable to verify user credentials and/or request signature") {
89-
clickAction = function() {
90-
$('#user-options a').eq(0).trigger('click');
91-
};
92-
}
93-
}
94-
}
95-
cloudStack.dialog.notice({ message: parseXMLHttpResponse(data), clickAction: clickAction });
81+
var clickAction = false;
82+
if (isValidJsonString(data.responseText)) {
83+
var json = JSON.parse(data.responseText);
84+
if (json != null) {
85+
var property;
86+
for(property in json) {}
87+
var errorObj = json[property];
88+
if(errorObj.errorcode == 401 && errorObj.errortext == "unable to verify user credentials and/or request signature") {
89+
clickAction = function() {
90+
$('#user-options a').eq(0).trigger('click');
91+
};
92+
}
93+
}
94+
}
95+
cloudStack.dialog.notice({ message: parseXMLHttpResponse(data), clickAction: clickAction });
9696
},
97-
beforeSend: function(XMLHttpRequest) {
98-
if (g_mySession == $.cookie("JSESSIONID")) {
99-
return true;
100-
}
101-
else {
102-
var clickAction = function() {
103-
$('#user-options a').eq(0).trigger('click');
104-
};
105-
cloudStack.dialog.notice({ message: _l('label.session.expired'), clickAction: clickAction });
106-
return false;
107-
}
108-
}
97+
beforeSend: function(XMLHttpRequest) {
98+
if (g_mySession == $.cookie("JSESSIONID")) {
99+
return true;
100+
}
101+
else {
102+
var clickAction = function() {
103+
$('#user-options a').eq(0).trigger('click');
104+
};
105+
cloudStack.dialog.notice({ message: _l('label.session.expired'), clickAction: clickAction });
106+
return false;
107+
}
108+
}
109109
});
110110

111111
var $container = $('#cloudStack3-container');
@@ -115,78 +115,78 @@
115115

116116
// Use this for checking the session, to bypass login screen
117117
bypassLoginCheck: function(args) { //determine to show or bypass login screen
118-
if (g_loginResponse == null) { //show login screen
119-
/*
120-
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.
121-
i.e. calling listCapabilities API with g_sessionKey from $.cookie('sessionKey') will succeed,
122-
then userValid will be set to true, then an user object (instead of "false") will be returned, then login screen will be bypassed.
123-
*/
124-
g_mySession = $.cookie('JSESSIONID');
125-
g_sessionKey = $.cookie('sessionKey');
126-
g_role = $.cookie('role');
127-
g_username = $.cookie('username');
128-
g_userid = $.cookie('userid');
129-
g_account = $.cookie('account');
130-
g_domainid = $.cookie('domainid');
131-
g_userfullname = $.cookie('userfullname');
132-
g_timezone = $.cookie('timezone');
133-
if($.cookie('timezoneoffset') != null)
134-
g_timezoneoffset = isNaN($.cookie('timezoneoffset'))? null: parseFloat($.cookie('timezoneoffset'));
135-
else
136-
g_timezoneoffset = null;
118+
if (g_loginResponse == null) { //show login screen
119+
/*
120+
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.
121+
i.e. calling listCapabilities API with g_sessionKey from $.cookie('sessionKey') will succeed,
122+
then userValid will be set to true, then an user object (instead of "false") will be returned, then login screen will be bypassed.
123+
*/
124+
g_mySession = $.cookie('JSESSIONID');
125+
g_sessionKey = $.cookie('sessionKey');
126+
g_role = $.cookie('role');
127+
g_username = $.cookie('username');
128+
g_userid = $.cookie('userid');
129+
g_account = $.cookie('account');
130+
g_domainid = $.cookie('domainid');
131+
g_userfullname = $.cookie('userfullname');
132+
g_timezone = $.cookie('timezone');
133+
if($.cookie('timezoneoffset') != null)
134+
g_timezoneoffset = isNaN($.cookie('timezoneoffset'))? null: parseFloat($.cookie('timezoneoffset'));
135+
else
136+
g_timezoneoffset = null;
137137
}
138-
else { //single-sign-on (bypass login screen)
139-
g_mySession = $.cookie('JSESSIONID');
140-
g_sessionKey = encodeURIComponent(g_loginResponse.sessionkey);
141-
g_role = g_loginResponse.type;
142-
g_username = g_loginResponse.username;
143-
g_userid = g_loginResponse.userid;
144-
g_account = g_loginResponse.account;
145-
g_domainid = g_loginResponse.domainid;
146-
g_userfullname = g_loginResponse.firstname + ' ' + g_loginResponse.lastname;
147-
g_timezone = g_loginResponse.timezone;
148-
if(g_loginResponse.timezoneoffset != null)
149-
g_timezoneoffset = isNaN(g_loginResponse.timezoneoffset)? null: parseFloat(g_loginResponse.timezoneoffset);
150-
else
151-
g_timezoneoffset = null;
152-
}
153-
138+
else { //single-sign-on (bypass login screen)
139+
g_mySession = $.cookie('JSESSIONID');
140+
g_sessionKey = encodeURIComponent(g_loginResponse.sessionkey);
141+
g_role = g_loginResponse.type;
142+
g_username = g_loginResponse.username;
143+
g_userid = g_loginResponse.userid;
144+
g_account = g_loginResponse.account;
145+
g_domainid = g_loginResponse.domainid;
146+
g_userfullname = g_loginResponse.firstname + ' ' + g_loginResponse.lastname;
147+
g_timezone = g_loginResponse.timezone;
148+
if(g_loginResponse.timezoneoffset != null)
149+
g_timezoneoffset = isNaN(g_loginResponse.timezoneoffset)? null: parseFloat(g_loginResponse.timezoneoffset);
150+
else
151+
g_timezoneoffset = null;
152+
}
153+
154154
var userValid = false;
155155
$.ajax({
156156
url: createURL("listCapabilities"),
157157
dataType: "json",
158158
async: false,
159159
success: function(json) {
160160
g_capabilities = json.listcapabilitiesresponse.capability;
161-
$.cookie('capabilities', g_capabilities, { expires: 1});
162-
161+
$.cookie('capabilities', g_capabilities, { expires: 1});
162+
163163
g_supportELB = json.listcapabilitiesresponse.capability.supportELB.toString(); //convert boolean to string if it's boolean
164164
$.cookie('supportELB', g_supportELB, { expires: 1});
165-
165+
166166
if (json.listcapabilitiesresponse.capability.userpublictemplateenabled != null) {
167167
g_userPublicTemplateEnabled = json.listcapabilitiesresponse.capability.userpublictemplateenabled.toString(); //convert boolean to string if it's boolean
168168
$.cookie('userpublictemplateenabled', g_userPublicTemplateEnabled, { expires: 1});
169-
}
170-
171-
g_userProjectsEnabled = json.listcapabilitiesresponse.capability.allowusercreateprojects;
169+
}
170+
171+
g_userProjectsEnabled = json.listcapabilitiesresponse.capability.allowusercreateprojects;
172172
$.cookie('userProjectsEnabled', g_userProjectsEnabled, { expires: 1 });
173-
173+
174174
g_cloudstackversion = json.listcapabilitiesresponse.capability.cloudstackversion;
175-
176-
if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) {
177-
var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side
178-
//intervalLimit = 9999; //this line is for testing only, comment it before check in
179-
if(intervalLimit > g_queryAsyncJobResultInterval)
180-
g_queryAsyncJobResultInterval = intervalLimit;
181-
}
182-
175+
176+
if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) {
177+
var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side
178+
//intervalLimit = 9999; //this line is for testing only, comment it before check in
179+
if(intervalLimit > g_queryAsyncJobResultInterval)
180+
g_queryAsyncJobResultInterval = intervalLimit;
181+
}
182+
183183
userValid = true;
184184
},
185-
error: function(xmlHTTP) { //override default error handling, do nothing instead of showing error "unable to verify user credentials" on login screen
185+
error: function(xmlHTTP) { //override default error handling, do nothing instead of showing error "unable to verify user credentials" on login screen
186186
},
187-
beforeSend : function(XMLHttpResponse) {
188-
return true;
189-
}
187+
beforeSend : function(XMLHttpResponse) {
188+
return true;
189+
}
190190
});
191191

192192
if (userValid && isAdmin()) {
@@ -228,7 +228,7 @@
228228
username: g_username,
229229
account: g_account,
230230
name: g_userfullname,
231-
role: g_role,
231+
role: g_role,
232232
domainid: g_domainid
233233
}
234234
} : false;
@@ -259,20 +259,20 @@
259259
else {
260260
array1.push("&domain=" + encodeURIComponent("/"));
261261
}
262-
263-
var loginCmdText = array1.join("");
264-
262+
263+
var loginCmdText = array1.join("");
264+
265265
$.ajax({
266266
type: "POST",
267-
data: "command=login" + loginCmdText + "&response=json",
267+
data: "command=login" + loginCmdText + "&response=json",
268268
dataType: "json",
269269
async: false,
270-
success: function(json) {
270+
success: function(json) {
271271
var loginresponse = json.loginresponse;
272272

273273
g_mySession = $.cookie('JSESSIONID');
274274
g_sessionKey = encodeURIComponent(loginresponse.sessionkey);
275-
g_role = loginresponse.type;
275+
g_role = loginresponse.type;
276276
g_username = loginresponse.username;
277277
g_userid = loginresponse.userid;
278278
g_account = loginresponse.account;
@@ -285,20 +285,20 @@
285285
$.cookie('username', g_username, { expires: 1});
286286
$.cookie('account', g_account, { expires: 1});
287287
$.cookie('domainid', g_domainid, { expires: 1});
288-
$.cookie('role', g_role, { expires: 1});
288+
$.cookie('role', g_role, { expires: 1});
289289
$.cookie('timezoneoffset', g_timezoneoffset, { expires: 1});
290290
$.cookie('timezone', g_timezone, { expires: 1});
291291
$.cookie('userfullname', g_userfullname, { expires: 1 });
292292
$.cookie('userid', g_userid, { expires: 1 });
293293

294294
$.ajax({
295-
url: createURL("listCapabilities"),
295+
url: createURL("listCapabilities"),
296296
dataType: "json",
297297
async: false,
298298
success: function(json) {
299299
g_capabilities = json.listcapabilitiesresponse.capability;
300-
$.cookie('capabilities', g_capabilities, { expires: 1});
301-
300+
$.cookie('capabilities', g_capabilities, { expires: 1});
301+
302302
g_supportELB = json.listcapabilitiesresponse.capability.supportELB.toString(); //convert boolean to string if it's boolean
303303
$.cookie('supportELB', g_supportELB, { expires: 1});
304304

@@ -309,16 +309,16 @@
309309

310310
g_userProjectsEnabled = json.listcapabilitiesresponse.capability.allowusercreateprojects;
311311
$.cookie('userProjectsEnabled', g_userProjectsEnabled, { expires: 1 });
312-
312+
313313
g_cloudstackversion = json.listcapabilitiesresponse.capability.cloudstackversion;
314-
315-
if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) {
316-
var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side
317-
//intervalLimit = 8888; //this line is for testing only, comment it before check in
318-
if(intervalLimit > g_queryAsyncJobResultInterval)
319-
g_queryAsyncJobResultInterval = intervalLimit;
320-
}
321-
314+
315+
if(json.listcapabilitiesresponse.capability.apilimitinterval != null && json.listcapabilitiesresponse.capability.apilimitmax != null) {
316+
var intervalLimit = ((json.listcapabilitiesresponse.capability.apilimitinterval * 1000) / json.listcapabilitiesresponse.capability.apilimitmax ) * 3; //multiply 3 to be on safe side
317+
//intervalLimit = 8888; //this line is for testing only, comment it before check in
318+
if(intervalLimit > g_queryAsyncJobResultInterval)
319+
g_queryAsyncJobResultInterval = intervalLimit;
320+
}
321+
322322
args.response.success({
323323
data: {
324324
user: $.extend(true, {}, loginresponse, {
@@ -331,7 +331,7 @@
331331
},
332332
error: function(xmlHTTP) {
333333
args.response.error();
334-
}
334+
}
335335
});
336336

337337
if (isAdmin()) {
@@ -371,17 +371,17 @@
371371
// TEMPORARY -- replace w/ output of capability response, etc., once implemented
372372
window.g_projectsInviteRequired = false;
373373
},
374-
error: function(XMLHttpRequest) {
375-
var errorMsg = parseXMLHttpResponse(XMLHttpRequest);
376-
if(errorMsg.length == 0 && XMLHttpRequest.status == 0)
377-
errorMsg = dictionary['error.unable.to.reach.management.server'];
378-
else
379-
errorMsg = _l('error.invalid.username.password'); //override error message
380-
args.response.error(errorMsg);
381-
},
382-
beforeSend : function(XMLHttpResponse) {
383-
return true;
384-
}
374+
error: function(XMLHttpRequest) {
375+
var errorMsg = parseXMLHttpResponse(XMLHttpRequest);
376+
if(errorMsg.length == 0 && XMLHttpRequest.status == 0)
377+
errorMsg = dictionary['error.unable.to.reach.management.server'];
378+
else
379+
errorMsg = _l('error.invalid.username.password'); //override error message
380+
args.response.error(errorMsg);
381+
},
382+
beforeSend : function(XMLHttpResponse) {
383+
return true;
384+
}
385385
});
386386
},
387387

@@ -413,16 +413,16 @@
413413

414414
if(onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen.
415415
document.location.reload(); //when onLogoutCallback() returns true, reload the current document.
416-
}
416+
}
417417
},
418418
error: function() {
419-
if(onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen.
419+
if(onLogoutCallback()) { //onLogoutCallback() will set g_loginResponse(single-sign-on variable) to null, then bypassLoginCheck() will show login screen.
420420
document.location.reload(); //when onLogoutCallback() returns true, reload the current document.
421-
}
422-
},
423-
beforeSend : function(XMLHttpResponse) {
424-
return true;
425-
}
421+
}
422+
},
423+
beforeSend : function(XMLHttpResponse) {
424+
return true;
425+
}
426426
});
427427
},
428428

@@ -479,7 +479,7 @@
479479
// SSO
480480
loginArgs.hideLoginScreen = true;
481481
}
482-
482+
483483
cloudStack.uiCustom.login(loginArgs);
484484

485485
// Localization

‎ui/scripts/plugins.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
show: cloudStack.uiCustom.pluginListing
6666
};
6767

68+
var loadedPlugins = 0;
69+
var pluginTotal = cloudStack.plugins.length + cloudStack.modules.length;
70+
6871
// Load
6972
$(['modules', 'plugins']).each(function() {
7073
var type = this;
@@ -96,6 +99,13 @@
9699
}
97100
})
98101
);
102+
103+
loadedPlugins = loadedPlugins + 1;
104+
105+
if (loadedPlugins === pluginTotal) {
106+
$(window).trigger('cloudStack.pluginReady');
107+
}
108+
99109
loadCSS(css);
100110
});
101111
}

0 commit comments

Comments
 (0)