Total Complexity | 188 |
Complexity/F | 3.19 |
Lines of Code | 996 |
Function Count | 59 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like framework/Web/Javascripts/source/prado/activecontrols/ajax3.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*! PRADO Ajax javascript file | github.com/pradosoft/prado */ |
||
3 | Prado.CallbackRequestManager = |
||
1 ignored issue
–
show
|
|||
4 | { |
||
5 | /** |
||
6 | * Callback request target POST field name. |
||
7 | */ |
||
8 | FIELD_CALLBACK_TARGET : 'PRADO_CALLBACK_TARGET', |
||
9 | /** |
||
10 | * Callback request parameter POST field name. |
||
11 | */ |
||
12 | FIELD_CALLBACK_PARAMETER : 'PRADO_CALLBACK_PARAMETER', |
||
13 | /** |
||
14 | * Callback request page state field name, |
||
15 | */ |
||
16 | FIELD_CALLBACK_PAGESTATE : 'PRADO_PAGESTATE', |
||
17 | /** |
||
18 | * Response redirect header name. |
||
19 | */ |
||
20 | REDIRECT_HEADER : 'X-PRADO-REDIRECT', |
||
21 | /** |
||
22 | * Response data header name. |
||
23 | */ |
||
24 | DATA_HEADER : 'X-PRADO-DATA', |
||
25 | /** |
||
26 | * Response javascript execution statement header name. |
||
27 | */ |
||
28 | ACTION_HEADER : 'X-PRADO-ACTIONS', |
||
29 | /** |
||
30 | * Response errors/exceptions header name. |
||
31 | */ |
||
32 | ERROR_HEADER : 'X-PRADO-ERROR', |
||
33 | /** |
||
34 | * Page state header name. |
||
35 | */ |
||
36 | PAGESTATE_HEADER : 'X-PRADO-PAGESTATE', |
||
37 | /** |
||
38 | * Script list header name. |
||
39 | */ |
||
40 | SCRIPTLIST_HEADER : 'X-PRADO-SCRIPTLIST', |
||
41 | /** |
||
42 | * Stylesheet code header name. |
||
43 | */ |
||
44 | STYLESHEET_HEADER : 'X-PRADO-STYLESHEET', |
||
45 | /** |
||
46 | * Stylesheet list header name. |
||
47 | */ |
||
48 | STYLESHEETLIST_HEADER : 'X-PRADO-STYLESHEETLIST', |
||
49 | /** |
||
50 | * Hidden field list header name. |
||
51 | */ |
||
52 | HIDDENFIELDLIST_HEADER : 'X-PRADO-HIDDENFIELDLIST', |
||
53 | /** |
||
54 | * Log debug informations when a callback fails, default true |
||
55 | */ |
||
56 | LOG_ERROR : true, |
||
57 | /** |
||
58 | * Log debug informations when a callback succedes, default false |
||
59 | */ |
||
60 | LOG_SUCCESS : false, |
||
61 | |||
62 | /** |
||
63 | * Formats the exception message for display in console. |
||
64 | */ |
||
65 | formatException : function(e) |
||
66 | { |
||
67 | var msg = e.type + " with message \""+e.message+"\""; |
||
68 | msg += " in "+e.file+"("+e.line+")\n"; |
||
69 | msg += "Stack trace:\n"; |
||
70 | var trace = e.trace; |
||
71 | for(var i = 0; i<trace.length; i++) |
||
72 | { |
||
73 | msg += " #"+i+" "+trace[i].file; |
||
74 | msg += "("+trace[i].line+"): "; |
||
75 | msg += trace[i]["class"]+"->"+trace[i]["function"]+"()"+"\n"; |
||
76 | } |
||
77 | msg += e.version+" "+e.time+"\n"; |
||
78 | return msg; |
||
79 | }, |
||
80 | |||
81 | /*! jQuery Ajax Queue - v0.1.2pre - 2013-03-19 |
||
82 | * https://github.com/gnarf37/jquery-ajaxQueue |
||
83 | * Copyright (c) 2013 Corey Frang; Licensed MIT |
||
84 | * Slightly adapted for use within prado by Fabio Bas <[email protected]> |
||
85 | */ |
||
86 | |||
87 | // jQuery on an empty object, we are going to use this as our Queue |
||
88 | ajaxQueue : jQuery({}), |
||
89 | |||
90 | ajax : function( ajaxOpts ) { |
||
91 | var jqXHR, |
||
92 | dfd = jQuery.Deferred(), |
||
93 | promise = dfd.promise(); |
||
94 | |||
95 | // run the actual query |
||
96 | function doRequest( next ) { |
||
97 | jqXHR = jQuery.ajax( ajaxOpts ); |
||
98 | jqXHR.done( dfd.resolve ) |
||
99 | .fail( dfd.reject ) |
||
100 | .then( next, next ); |
||
101 | } |
||
102 | |||
103 | // queue our ajax request |
||
104 | Prado.CallbackRequestManager.ajaxQueue.queue( doRequest ); |
||
1 ignored issue
–
show
|
|||
105 | |||
106 | // add the abort method |
||
107 | promise.abort = function( statusText ) { |
||
108 | |||
109 | // proxy abort to the jqXHR if it is active |
||
110 | if ( jqXHR ) { |
||
111 | return jqXHR.abort( statusText ); |
||
112 | } |
||
113 | |||
114 | // if there wasn't already a jqXHR we need to remove from queue |
||
115 | var queue = Prado.CallbackRequestManager.ajaxQueue.queue(), |
||
1 ignored issue
–
show
|
|||
116 | index = jQuery.inArray( doRequest, queue ); |
||
117 | |||
118 | if ( index > -1 ) { |
||
119 | queue.splice( index, 1 ); |
||
120 | } |
||
121 | |||
122 | // and then reject the deferred |
||
123 | dfd.rejectWith( ajaxOpts.context || ajaxOpts, [ promise, statusText, "" ] ); |
||
124 | return promise; |
||
125 | }; |
||
126 | |||
127 | return promise; |
||
128 | } |
||
129 | }; |
||
130 | |||
131 | Prado.CallbackRequest = jQuery.klass(Prado.PostBack, |
||
132 | { |
||
133 | |||
134 | options : {}, |
||
135 | data : '', |
||
136 | |||
137 | initialize: function(id, options) |
||
138 | { |
||
139 | this.options = { |
||
140 | RequestTimeOut : 30000, // 30 second timeout. |
||
141 | EnablePageStateUpdate : true, |
||
142 | CausesValidation : true, |
||
143 | ValidationGroup : null, |
||
144 | PostInputs : true, |
||
145 | |||
146 | type: "POST", |
||
147 | context: this, |
||
148 | success: this.successHandler, |
||
149 | error: this.errorHandler, |
||
150 | complete: this.completeHandler |
||
151 | }; |
||
152 | |||
153 | jQuery.extend(this.options, options || {}); |
||
154 | |||
155 | if(this.options.onUninitialized) |
||
156 | this.options.onUninitialized(this,null); |
||
1 ignored issue
–
show
|
|||
157 | }, |
||
158 | |||
159 | /** |
||
160 | * Sets the request options |
||
161 | * @return {Array} request options. |
||
162 | */ |
||
163 | setOptions: function(options) { |
||
164 | jQuery.extend(this.options, options || { }); |
||
165 | }, |
||
166 | |||
167 | getForm: function() |
||
168 | { |
||
169 | return jQuery('#'+this.options.ID).parents('form:first').get(0) || jQuery('#PRADO_PAGESTATE').get(0).form; |
||
170 | }, |
||
171 | |||
172 | /** |
||
173 | * Gets the url from the forms that contains the PRADO_PAGESTATE |
||
174 | * @return {String} callback url. |
||
175 | */ |
||
176 | getCallbackUrl : function() |
||
177 | { |
||
178 | return this.getForm().action; |
||
179 | }, |
||
180 | |||
181 | /** |
||
182 | * Sets the request parameter |
||
183 | * @param {Object} parameter value |
||
184 | */ |
||
185 | setCallbackParameter : function(value) |
||
186 | { |
||
187 | this.options['CallbackParameter'] = value; |
||
188 | }, |
||
189 | |||
190 | /** |
||
191 | * @return {Object} request paramater value. |
||
192 | */ |
||
193 | getCallbackParameter : function() |
||
194 | { |
||
195 | return JSON.stringify(this.options['CallbackParameter']); |
||
196 | }, |
||
197 | |||
198 | /** |
||
199 | * Sets the callback request timeout. |
||
200 | * @param {integer} timeout in milliseconds |
||
201 | */ |
||
202 | setRequestTimeOut : function(timeout) |
||
203 | { |
||
204 | this.options['RequestTimeOut'] = timeout; |
||
205 | }, |
||
206 | |||
207 | /** |
||
208 | * @return {integer} request timeout in milliseconds |
||
209 | */ |
||
210 | getRequestTimeOut : function() |
||
211 | { |
||
212 | return this.options['RequestTimeOut']; |
||
213 | }, |
||
214 | |||
215 | /** |
||
216 | * Set true to enable validation on callback dispatch. |
||
217 | * @param {boolean} true to validate |
||
218 | */ |
||
219 | setCausesValidation : function(validate) |
||
220 | { |
||
221 | this.options['CausesValidation'] = validate; |
||
222 | }, |
||
223 | |||
224 | /** |
||
225 | * @return {boolean} validate on request dispatch |
||
226 | */ |
||
227 | getCausesValidation : function() |
||
228 | { |
||
229 | return this.options['CausesValidation']; |
||
230 | }, |
||
231 | |||
232 | /** |
||
233 | * Sets the validation group to validate during request dispatch. |
||
234 | * @param {string} validation group name |
||
235 | */ |
||
236 | setValidationGroup : function(group) |
||
237 | { |
||
238 | this.options['ValidationGroup'] = group; |
||
239 | }, |
||
240 | |||
241 | /** |
||
242 | * @return {string} validation group name. |
||
243 | */ |
||
244 | getValidationGroup : function() |
||
245 | { |
||
246 | return this.options['ValidationGroup']; |
||
247 | }, |
||
248 | |||
249 | dispatch: function() |
||
250 | { |
||
251 | //trigger tinyMCE to save data. |
||
252 | if(typeof tinyMCE != "undefined") |
||
1 ignored issue
–
show
|
|||
253 | tinyMCE.triggerSave(); |
||
1 ignored issue
–
show
|
|||
254 | |||
255 | if(this.options['CausesValidation'] && typeof(Prado.Validation) != "undefined") |
||
1 ignored issue
–
show
|
|||
256 | { |
||
257 | if(!Prado.Validation.validate(this.getForm().id, this.options['ValidationGroup'], this)) |
||
258 | return false; |
||
1 ignored issue
–
show
|
|||
259 | } |
||
260 | |||
261 | if(this.options.onPreDispatch) |
||
262 | this.options.onPreDispatch(this,null); |
||
1 ignored issue
–
show
|
|||
263 | |||
264 | // prepare callback paramters |
||
265 | this.options.data = this.getParameters(); |
||
266 | this.options.url = this.getCallbackUrl(); |
||
267 | this.options.timeout = this.getRequestTimeOut(); |
||
268 | |||
269 | // jQuery don't have all these states.. simulate them to avoid breaking old scripts |
||
270 | if (this.options.onLoading) |
||
271 | this.options.onLoading(this,null); |
||
1 ignored issue
–
show
|
|||
272 | if (this.options.onLoaded) |
||
273 | this.options.onLoaded(this,null); |
||
1 ignored issue
–
show
|
|||
274 | if (this.options.onInteractive) |
||
275 | this.options.onInteractive(this,null); |
||
1 ignored issue
–
show
|
|||
276 | |||
277 | this.request = Prado.CallbackRequestManager.ajax(this.options); |
||
278 | }, |
||
279 | |||
280 | abort : function() |
||
281 | { |
||
282 | if(this.request != "undefined") |
||
283 | this.request.abort(); |
||
1 ignored issue
–
show
|
|||
284 | }, |
||
285 | |||
286 | /** |
||
287 | * Collects the form inputs, encode the parameters, and sets the callback |
||
288 | * target id. The resulting string is the request content body. |
||
289 | * @return string request body content containing post data. |
||
290 | */ |
||
291 | getParameters : function() |
||
292 | { |
||
293 | var data = {}; |
||
294 | |||
295 | if(typeof(this.options.CallbackParameter) != "undefined") |
||
296 | data[Prado.CallbackRequestManager.FIELD_CALLBACK_PARAMETER] = this.getCallbackParameter(); |
||
2 ignored issues
–
show
|
|||
297 | if(this.options.EventTarget) |
||
298 | data[Prado.CallbackRequestManager.FIELD_CALLBACK_TARGET] = this.options.EventTarget; |
||
1 ignored issue
–
show
|
|||
299 | |||
300 | if(this.options.PostInputs != false) |
||
301 | { |
||
302 | var form = this.getForm(); |
||
303 | return jQuery('input, select, textarea').serialize() + '&' + jQuery.param(data); |
||
304 | } else { |
||
305 | var pagestate = jQuery("#"+Prado.CallbackRequestManager.FIELD_CALLBACK_PAGESTATE); |
||
306 | if(pagestate) |
||
307 | data[Prado.CallbackRequestManager.FIELD_CALLBACK_PAGESTATE] = pagestate.val(); |
||
1 ignored issue
–
show
|
|||
308 | return jQuery.param(data); |
||
309 | } |
||
310 | }, |
||
311 | |||
312 | /** |
||
313 | * Extract content from a text by its boundary id. |
||
314 | * Boundaries have this form: |
||
315 | * <pre> |
||
316 | * <!--123456-->Democontent<!--//123456--> |
||
317 | * </pre> |
||
318 | * @function {string} ? |
||
319 | * @param {string} boundary - Boundary id |
||
320 | * @returns Content from given boundaries |
||
321 | */ |
||
322 | extractContent: function (boundary) |
||
323 | { |
||
324 | var tagStart = '<!--'+boundary+'-->'; |
||
325 | var tagEnd = '<!--//'+boundary+'-->'; |
||
326 | var start = this.data.indexOf(tagStart); |
||
327 | if(start > -1) |
||
328 | { |
||
329 | start += tagStart.length; |
||
330 | var end = this.data.indexOf(tagEnd,start); |
||
331 | if(end > -1) |
||
332 | return this.data.substring(start,end); |
||
1 ignored issue
–
show
|
|||
333 | } |
||
334 | return null; |
||
335 | }, |
||
336 | |||
337 | getLogger: function() |
||
338 | { |
||
339 | if(typeof Logger != "undefined") |
||
1 ignored issue
–
show
|
|||
340 | return Logger; |
||
1 ignored issue
–
show
|
|||
341 | |||
342 | // use the browser console if no Logger is available |
||
343 | if(typeof console != "undefined") |
||
344 | return console; |
||
1 ignored issue
–
show
|
|||
345 | |||
346 | return null; |
||
347 | }, |
||
348 | |||
349 | errorHandler: function(request, textStatus, errorThrown) |
||
350 | { |
||
351 | this.data = request.responseText; |
||
352 | |||
353 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
1 ignored issue
–
show
|
|||
354 | { |
||
355 | log.error("PRADO Ajax callback error:", request.status, "(" + request.statusText + ")"); |
||
356 | if(request.status==500) |
||
357 | { |
||
358 | /** |
||
359 | * Server returns 500 exception. Just log it. |
||
360 | */ |
||
361 | var errorData = this.extractContent(Prado.CallbackRequestManager.ERROR_HEADER); |
||
362 | if (typeof(errorData) == "string" && errorData.length > 0) |
||
363 | { |
||
364 | errorData = jQuery.parseJSON(errorData); |
||
365 | if(typeof(errorData) == "object") |
||
366 | log.info(Prado.CallbackRequestManager.formatException(errorData)); |
||
1 ignored issue
–
show
|
|||
367 | } |
||
368 | } |
||
369 | } |
||
370 | |||
371 | if (this.options.onFailure) |
||
372 | this.options.onFailure(this,textStatus); |
||
1 ignored issue
–
show
|
|||
373 | }, |
||
374 | |||
375 | completeHandler: function(request, textStatus) |
||
376 | { |
||
377 | //"success", "notmodified", "error", "timeout", "abort", or "parsererror" |
||
378 | if (this.options.onComplete) |
||
379 | this.options.onComplete(this,textStatus); |
||
1 ignored issue
–
show
|
|||
380 | }, |
||
381 | |||
382 | /** |
||
383 | * Uncaught exceptions during callback response. |
||
384 | */ |
||
385 | exceptionHandler: function(e) |
||
386 | { |
||
387 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
1 ignored issue
–
show
|
|||
388 | { |
||
389 | log.error("Uncaught Callback Client Exception:", e.message); |
||
390 | log.info('Stack:', e.stack); |
||
391 | } else { |
||
392 | debugger; |
||
393 | } |
||
394 | |||
395 | if (this.options.onException) |
||
396 | this.options.onException(this,e); |
||
1 ignored issue
–
show
|
|||
397 | }, |
||
398 | |||
399 | /** |
||
400 | * Callback OnSuccess event,logs reponse and data to console. |
||
401 | */ |
||
402 | successHandler: function(data, textStatus, request) |
||
403 | { |
||
404 | this.data = data; |
||
405 | |||
406 | if(Prado.CallbackRequestManager.LOG_SUCCESS && (log = this.getLogger())) |
||
1 ignored issue
–
show
|
|||
407 | { |
||
408 | log.info('HTTP '+request.status+" with response : \n"); |
||
409 | |||
410 | var tagStart = '<!--'; |
||
411 | var tagEnd = '<!--//'; |
||
412 | var start = request.responseText.indexOf(tagStart); |
||
413 | while(start > -1) |
||
414 | { |
||
415 | var end = request.responseText.indexOf(tagEnd,start); |
||
416 | if(end > -1) |
||
417 | log.info(request.responseText.substring(start,end)+'\n'); |
||
1 ignored issue
–
show
|
|||
418 | start = request.responseText.indexOf(tagStart,end+6); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | if (this.options.onSuccess) |
||
423 | { |
||
424 | var customData=this.extractContent(Prado.CallbackRequestManager.DATA_HEADER); |
||
425 | if (typeof(customData) == "string" && customData.length > 0) |
||
426 | customData = jQuery.parseJSON(customData); |
||
1 ignored issue
–
show
|
|||
427 | |||
428 | this.options.onSuccess(this,customData); |
||
429 | } |
||
430 | |||
431 | var redirectUrl = this.extractContent(Prado.CallbackRequestManager.REDIRECT_HEADER); |
||
432 | if (redirectUrl) |
||
433 | document.location.href = redirectUrl; |
||
1 ignored issue
–
show
|
|||
434 | |||
435 | try { |
||
436 | this.updatePageState(this, data); |
||
437 | this.checkHiddenFields(this, data); |
||
438 | var obj = this; |
||
439 | this.loadAssets(this, data, function() |
||
440 | { |
||
441 | try { |
||
442 | obj.dispatchActions(obj, data); |
||
443 | } catch (e) { |
||
444 | obj.exceptionHandler(e); |
||
445 | } |
||
446 | } |
||
447 | ); |
||
448 | |||
449 | } catch (e) { |
||
450 | this.exceptionHandler(e); |
||
451 | } |
||
452 | }, |
||
453 | |||
454 | /** |
||
455 | * Updates the page state. It will update only if EnablePageStateUpdate is true. |
||
456 | */ |
||
457 | updatePageState : function(request, datain) |
||
458 | { |
||
459 | var pagestate = jQuery("#"+Prado.CallbackRequestManager.FIELD_CALLBACK_PAGESTATE); |
||
1 ignored issue
–
show
|
|||
460 | var enabled = request.options.EnablePageStateUpdate; |
||
461 | var aborted = false; //typeof(self.currentRequest) == 'undefined' || self.currentRequest == null; |
||
462 | if(enabled && !aborted && pagestate) |
||
463 | { |
||
464 | var data = this.extractContent(Prado.CallbackRequestManager.PAGESTATE_HEADER); |
||
465 | if(typeof(data) == "string" && data.length > 0) |
||
466 | pagestate.val(data); |
||
1 ignored issue
–
show
|
|||
467 | else |
||
468 | { |
||
469 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
470 | log.warn("Missing page state:"+data); |
||
1 ignored issue
–
show
|
|||
471 | //Logger.warn('## bad state: setting current request to null'); |
||
472 | //self.endCurrentRequest(); |
||
473 | //self.tryNextRequest(); |
||
474 | return false; |
||
475 | } |
||
476 | } |
||
477 | //self.endCurrentRequest(); |
||
478 | //Logger.warn('## state updated: setting current request to null'); |
||
479 | //self.tryNextRequest(); |
||
480 | return true; |
||
481 | }, |
||
482 | |||
483 | checkHiddenField: function(name, value) |
||
484 | { |
||
485 | var id = name.replace(':','_'); |
||
486 | if (!document.getElementById(id)) |
||
487 | { |
||
488 | var field = document.createElement('input'); |
||
489 | field.setAttribute('type','hidden'); |
||
490 | field.id = id; |
||
491 | field.name = name; |
||
492 | field.value = value; |
||
493 | document.body.appendChild(field); |
||
494 | } |
||
495 | }, |
||
496 | |||
497 | checkHiddenFields : function(request, datain) |
||
498 | { |
||
499 | var data = this.extractContent(Prado.CallbackRequestManager.HIDDENFIELDLIST_HEADER); |
||
1 ignored issue
–
show
|
|||
500 | if (typeof(data) == "string" && data.length > 0) |
||
501 | { |
||
502 | json = jQuery.parseJSON(data); |
||
503 | if(typeof(json) != "object") |
||
504 | { |
||
505 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
506 | log.warn("Invalid hidden field list:"+data); |
||
1 ignored issue
–
show
|
|||
507 | } else { |
||
508 | for(var key in json) |
||
509 | this.checkHiddenField(key,json[key]); |
||
1 ignored issue
–
show
|
|||
510 | } |
||
511 | } |
||
512 | }, |
||
513 | |||
514 | /* |
||
515 | * Checks which assets are used by the response and ensures they're loaded |
||
516 | */ |
||
517 | loadAssets : function(request, datain, callback) |
||
518 | { |
||
519 | /* |
||
520 | |||
521 | ! This is the callback-based loader for stylesheets, which loads them one-by-one, and |
||
522 | ! waits for all of them to be loaded before loading scripts and processing the rest of |
||
523 | ! the callback. |
||
524 | ! |
||
525 | ! That however is not neccessary, as stylesheets can be loaded asynchronously too. |
||
526 | ! |
||
527 | ! I leave this code here for the case that this turns out to be a compatibility issue |
||
528 | ! (for ex. I can imagine some scripts trying to access stylesheet properties and such) |
||
529 | ! so if need can be reactivated. If you do so, comment out the async stylesheet loader below! |
||
530 | |||
531 | var obj = this; |
||
532 | this.loadStyleSheets(request,transport, function() { |
||
533 | obj.loadScripts(request,transport,callback); |
||
534 | }); |
||
535 | |||
536 | */ |
||
537 | |||
538 | this.loadStyleSheetsCode(request,datain); |
||
539 | |||
540 | this.loadStyleSheetsAsync(request,datain); |
||
541 | |||
542 | this.loadScripts(request,datain,callback); |
||
543 | }, |
||
544 | |||
545 | /* |
||
546 | * Checks which scripts are used by the response and ensures they're loaded |
||
547 | */ |
||
548 | loadScripts : function(request, datain, callback) |
||
549 | { |
||
550 | var data = this.extractContent(Prado.CallbackRequestManager.SCRIPTLIST_HEADER); |
||
1 ignored issue
–
show
|
|||
551 | if (!this.ScriptsToLoad) this.ScriptsToLoad = new Array(); |
||
1 ignored issue
–
show
|
|||
552 | this.ScriptLoadFinishedCallback = callback; |
||
553 | if (typeof(data) == "string" && data.length > 0) |
||
554 | { |
||
555 | json = jQuery.parseJSON(data); |
||
556 | if(typeof(json) != "object") |
||
557 | { |
||
558 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
559 | log.warn("Invalid script list:"+data); |
||
1 ignored issue
–
show
|
|||
560 | } else { |
||
561 | for(var key in json) |
||
562 | if (/^\d+$/.test(key)) |
||
1 ignored issue
–
show
|
|||
563 | { |
||
564 | var url = json[key]; |
||
565 | if (!Prado.ScriptManager.isAssetLoaded(url)) |
||
566 | this.ScriptsToLoad.push(url); |
||
1 ignored issue
–
show
|
|||
567 | } |
||
568 | } |
||
569 | } |
||
570 | this.loadNextScript(); |
||
571 | }, |
||
572 | |||
573 | loadNextScript: function() |
||
574 | { |
||
575 | var done = (!this.ScriptsToLoad || (this.ScriptsToLoad.length==0)); |
||
576 | if (!done) |
||
577 | { |
||
578 | var url = this.ScriptsToLoad.shift(); var obj = this; |
||
579 | if ( |
||
580 | Prado.ScriptManager.ensureAssetIsLoaded(url, |
||
1 ignored issue
–
show
|
|||
581 | function() { |
||
582 | obj.loadNextScript(); |
||
583 | } |
||
584 | ) |
||
585 | ) |
||
586 | this.loadNextScript(); |
||
1 ignored issue
–
show
|
|||
587 | } |
||
588 | else |
||
589 | { |
||
590 | if (this.ScriptLoadFinishedCallback) |
||
591 | { |
||
592 | var cb = this.ScriptLoadFinishedCallback; |
||
593 | this.ScriptLoadFinishedCallback = null; |
||
594 | cb(); |
||
595 | } |
||
596 | } |
||
597 | }, |
||
598 | |||
599 | loadStyleSheetsCode : function(request, datain) |
||
600 | { |
||
601 | var data = this.extractContent(Prado.CallbackRequestManager.STYLESHEET_HEADER); |
||
1 ignored issue
–
show
|
|||
602 | if (typeof(data) == "string" && data.length > 0) |
||
603 | { |
||
604 | json = jQuery.parseJSON(data); |
||
605 | if(typeof(json) != "object") |
||
606 | { |
||
607 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
608 | log.warn("Invalid stylesheet list:"+data); |
||
1 ignored issue
–
show
|
|||
609 | } else { |
||
610 | for(var key in json) |
||
611 | if (/^\d+$/.test(key)) |
||
1 ignored issue
–
show
|
|||
612 | Prado.StyleSheetManager.createStyleSheetCode(json[key],null); |
||
1 ignored issue
–
show
|
|||
613 | } |
||
614 | } |
||
615 | }, |
||
616 | |||
617 | loadStyleSheetsAsync : function(request, datain) |
||
618 | { |
||
619 | var data = this.extractContent(Prado.CallbackRequestManager.STYLESHEETLIST_HEADER); |
||
1 ignored issue
–
show
|
|||
620 | if (typeof(data) == "string" && data.length > 0) |
||
621 | { |
||
622 | json = jQuery.parseJSON(data); |
||
623 | if(typeof(json) != "object") |
||
624 | { |
||
625 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
626 | log.warn("Invalid stylesheet list:"+data); |
||
1 ignored issue
–
show
|
|||
627 | } else { |
||
628 | for(var key in json) |
||
629 | if (/^\d+$/.test(key)) |
||
1 ignored issue
–
show
|
|||
630 | Prado.StyleSheetManager.ensureAssetIsLoaded(json[key],null); |
||
1 ignored issue
–
show
|
|||
631 | } |
||
632 | } |
||
633 | }, |
||
634 | |||
635 | loadStyleSheets : function(request, datain, callback) |
||
636 | { |
||
637 | var data = this.extractContent(Prado.CallbackRequestManager.STYLESHEETLIST_HEADER); |
||
1 ignored issue
–
show
|
|||
638 | if (!this.StyleSheetsToLoad) this.StyleSheetsToLoad = new Array(); |
||
1 ignored issue
–
show
|
|||
639 | this.StyleSheetLoadFinishedCallback = callback; |
||
640 | if (typeof(data) == "string" && data.length > 0) |
||
641 | { |
||
642 | json = jQuery.parseJSON(data); |
||
643 | if(typeof(json) != "object") |
||
644 | { |
||
645 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
646 | log.warn("Invalid stylesheet list:"+data); |
||
1 ignored issue
–
show
|
|||
647 | } else { |
||
648 | for(var key in json) |
||
649 | if (/^\d+$/.test(key)) |
||
1 ignored issue
–
show
|
|||
650 | { |
||
651 | var url = json[key]; |
||
652 | if (!Prado.StyleSheetManager.isAssetLoaded(url)) |
||
653 | this.StyleSheetsToLoad.push(url); |
||
1 ignored issue
–
show
|
|||
654 | } |
||
655 | } |
||
656 | } |
||
657 | this.loadNextStyleSheet(); |
||
658 | }, |
||
659 | |||
660 | loadNextStyleSheet: function() |
||
661 | { |
||
662 | var done = (!this.StyleSheetsToLoad || (this.StyleSheetsToLoad.length==0)); |
||
663 | if (!done) |
||
664 | { |
||
665 | var url = this.StyleSheetsToLoad.shift(); var obj = this; |
||
666 | if ( |
||
667 | Prado.StyleSheetManager.ensureAssetIsLoaded(url, |
||
1 ignored issue
–
show
|
|||
668 | function() { |
||
669 | obj.loadNextStyleSheet(); |
||
670 | } |
||
671 | ) |
||
672 | ) |
||
673 | this.loadNextStyleSheet(); |
||
1 ignored issue
–
show
|
|||
674 | } else { |
||
675 | if (this.StyleSheetLoadFinishedCallback) |
||
676 | { |
||
677 | var cb = this.StyleSheetLoadFinishedCallback; |
||
678 | this.StyleSheetLoadFinishedCallback = null; |
||
679 | cb(); |
||
680 | } |
||
681 | } |
||
682 | }, |
||
683 | |||
684 | /** |
||
685 | * Dispatch callback response actions. |
||
686 | */ |
||
687 | dispatchActions : function(request, datain) |
||
688 | { |
||
689 | var data = this.extractContent(Prado.CallbackRequestManager.ACTION_HEADER); |
||
1 ignored issue
–
show
|
|||
690 | if (typeof(data) == "string" && data.length > 0) |
||
691 | { |
||
692 | json = jQuery.parseJSON(data); |
||
693 | if(typeof(json) != "object") |
||
694 | { |
||
695 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
696 | log.warn("Invalid action:"+data); |
||
1 ignored issue
–
show
|
|||
697 | } else { |
||
698 | var that = this; |
||
699 | jQuery.each(json, function(idx, item){ |
||
700 | that.__run(that, item); |
||
701 | }); |
||
702 | } |
||
703 | } |
||
704 | }, |
||
705 | |||
706 | /** |
||
707 | * Prase and evaluate a Callback clien-side action |
||
708 | */ |
||
709 | __run : function(request, command) |
||
710 | { |
||
711 | for(var method in command) |
||
712 | { |
||
713 | try { |
||
714 | method.toFunction().apply(request,command[method]); |
||
715 | } catch(e) { |
||
716 | this.exceptionHandler(e); |
||
717 | } |
||
718 | } |
||
719 | } |
||
720 | }); |
||
721 | |||
722 | /** |
||
723 | * Create a new callback request using default settings. |
||
724 | * @param string callback handler unique ID. |
||
725 | * @param mixed parameter to pass to callback handler on the server side. |
||
726 | * @param function client side onSuccess event handler. |
||
727 | * @param object additional request options. |
||
728 | * @return Prado.CallbackRequest request that was created |
||
729 | */ |
||
730 | Prado.Callback = function(UniqueID, parameter, onSuccess, options) |
||
1 ignored issue
–
show
|
|||
731 | { |
||
732 | var callback = |
||
733 | { |
||
734 | 'EventTarget' : UniqueID || '', |
||
735 | 'CallbackParameter' : parameter || '', |
||
736 | 'onSuccess' : onSuccess || jQuery.noop() |
||
737 | }; |
||
738 | |||
739 | jQuery.extend(callback, options || {}); |
||
740 | |||
741 | var request = new Prado.CallbackRequest(UniqueID, callback); |
||
1 ignored issue
–
show
|
|||
742 | request.dispatch(); |
||
743 | return request; |
||
744 | }; |
||
745 | |||
746 | /** |
||
747 | * Create a new callback request initiated by jQuery-UI elements. |
||
748 | * @param event object as sent by jQuery-UI events |
||
749 | * @param ui object as sent by jQuery-UI events |
||
750 | * @return Prado.CallbackRequest request that was created |
||
751 | */ |
||
752 | Prado.JuiCallback = function(UniqueID, eventType, event, ui, target) |
||
753 | { |
||
754 | // Retuns an array of all properties of the object received as parameter and their values. |
||
755 | // If a property represent a jQuery element, its id is returnet instead |
||
756 | var cleanUi = {}; |
||
757 | jQuery.each( ui, function( key, value ) { |
||
758 | if(value instanceof jQuery) |
||
759 | cleanUi[key]=value[0].id; |
||
1 ignored issue
–
show
|
|||
760 | else |
||
761 | cleanUi[key]=value; |
||
762 | }); |
||
763 | |||
764 | target=jQuery(target); |
||
765 | cleanUi['target']= { |
||
766 | 'position' : target.position(), |
||
767 | 'offset' : target.offset() |
||
768 | }; |
||
769 | |||
770 | var callback = |
||
771 | { |
||
772 | 'EventTarget' : UniqueID, |
||
773 | 'CallbackParameter' : { |
||
774 | 'event' : eventType, |
||
775 | 'ui' : cleanUi |
||
776 | } |
||
777 | }; |
||
778 | |||
779 | var request = new Prado.CallbackRequest(UniqueID, callback); |
||
1 ignored issue
–
show
|
|||
780 | request.dispatch(); |
||
781 | return request; |
||
782 | }; |
||
783 | |||
784 | /** |
||
785 | * Asset manager classes for lazy loading of scripts and stylesheets |
||
786 | * @author Gabor Berczi ([email protected]) |
||
787 | */ |
||
788 | |||
789 | if (typeof(Prado.AssetManagerClass)=="undefined") { |
||
790 | |||
791 | Prado.AssetManagerClass = jQuery.klass(); |
||
792 | Prado.AssetManagerClass.prototype = { |
||
793 | |||
794 | initialize: function() { |
||
795 | this.loadedAssets = new Array(); |
||
796 | this.discoverLoadedAssets(); |
||
797 | }, |
||
798 | |||
799 | |||
800 | /** |
||
801 | * Detect which assets are already loaded by page markup. |
||
802 | * This is done by looking up all <asset> elements and registering the values of their src attributes. |
||
803 | */ |
||
804 | discoverLoadedAssets: function() { |
||
805 | |||
806 | // wait until document has finished loading to avoid javascript errors |
||
807 | if (!document.body) return; |
||
1 ignored issue
–
show
|
|||
808 | |||
809 | var assets = this.findAssetUrlsInMarkup(); |
||
810 | for(var i=0;i<assets.length;i++) |
||
811 | this.markAssetAsLoaded(assets[i]); |
||
1 ignored issue
–
show
|
|||
812 | }, |
||
813 | |||
814 | /** |
||
815 | * Extend url to a fully qualified url. |
||
816 | * @param string url |
||
817 | */ |
||
818 | makeFullUrl: function(url) { |
||
819 | |||
820 | // this is not intended to be a fully blown url "canonicalizator", |
||
821 | // just to handle the most common and basic asset paths used by Prado |
||
822 | |||
823 | if (!this.baseUri) this.baseUri = window.location; |
||
1 ignored issue
–
show
|
|||
824 | |||
825 | if (url.indexOf('://')==-1) |
||
826 | { |
||
827 | var a = document.createElement('a'); |
||
828 | a.href = url; |
||
829 | |||
830 | if (a.href.indexOf('://')!=-1) |
||
831 | url = a.href; |
||
1 ignored issue
–
show
|
|||
832 | else |
||
833 | { |
||
834 | var path = a.pathname; |
||
835 | if (path.substr(0,1)!='/') path = '/'+path; |
||
1 ignored issue
–
show
|
|||
836 | url = this.baseUri.protocol+'//'+this.baseUri.host+path; |
||
837 | } |
||
838 | } |
||
839 | return url; |
||
840 | }, |
||
841 | |||
842 | isAssetLoaded: function(url) { |
||
843 | url = this.makeFullUrl(url); |
||
844 | return (jQuery.inArray(url, this.loadedAssets)!=-1); |
||
845 | }, |
||
846 | |||
847 | /** |
||
848 | * Mark asset as being already loaded |
||
849 | * @param string url of the asset |
||
850 | */ |
||
851 | markAssetAsLoaded: function(url) { |
||
852 | url = this.makeFullUrl(url); |
||
853 | if (jQuery.inArray(url, this.loadedAssets)==-1) |
||
854 | this.loadedAssets.push(url); |
||
1 ignored issue
–
show
|
|||
855 | }, |
||
856 | |||
857 | assetReadyStateChanged: function(url, element, callback, finalevent) { |
||
858 | if (finalevent || (element.readyState == 'loaded') || (element.readyState == 'complete')) |
||
859 | if (!element.assetCallbackFired) |
||
1 ignored issue
–
show
|
|||
860 | { |
||
861 | element.assetCallbackFired = true; |
||
862 | callback(url,element); |
||
863 | } |
||
864 | }, |
||
865 | |||
866 | assetLoadFailed: function(url, element, callback) { |
||
867 | debugger; |
||
868 | element.assetCallbackFired = true; |
||
869 | if(Prado.CallbackRequestManager.LOG_ERROR && (log = this.getLogger())) |
||
1 ignored issue
–
show
|
|||
870 | log.error("Failed to load asset: "+url, this); |
||
1 ignored issue
–
show
|
|||
871 | if (!element.assetCallbackFired) |
||
872 | callback(url,element,false); |
||
1 ignored issue
–
show
|
|||
873 | }, |
||
874 | |||
875 | /** |
||
876 | * Load a new asset dynamically into the page. |
||
877 | * Please not thet loading is asynchronous and therefore you can't assume that |
||
878 | * the asset is loaded and ready when returning from this function. |
||
879 | * @param string url of the asset to load |
||
880 | * @param callback will be called when the asset has loaded (or failed to load) |
||
881 | */ |
||
882 | startAssetLoad: function(url, callback) { |
||
883 | |||
884 | // create new <asset> element in page header |
||
885 | var asset = this.createAssetElement(url); |
||
886 | |||
887 | if (callback) |
||
888 | { |
||
889 | asset.onreadystatechange = this.assetReadyStateChanged.bind(this, url, asset, callback, false); |
||
890 | asset.onload = this.assetReadyStateChanged.bind(this, url, asset, callback, true); |
||
891 | asset.onerror = this.assetLoadFailed.bind(this, url, asset, callback); |
||
892 | asset.assetCallbackFired = false; |
||
893 | } |
||
894 | |||
895 | var head = document.getElementsByTagName('head')[0]; |
||
896 | head.appendChild(asset); |
||
897 | |||
898 | // mark this asset as loaded |
||
899 | this.markAssetAsLoaded(url); |
||
900 | |||
901 | return (callback!=false); |
||
902 | }, |
||
903 | |||
904 | /** |
||
905 | * Check whether a asset is loaded into the page, and if itsn't, load it now |
||
906 | * @param string url of the asset to check/load |
||
907 | * @return boolean returns true if asset is already loaded, or false, if loading has just started. callback will be called when loading has finished. |
||
908 | */ |
||
909 | ensureAssetIsLoaded: function(url, callback) { |
||
910 | url = this.makeFullUrl(url); |
||
911 | if (jQuery.inArray(url, this.loadedAssets)==-1) |
||
912 | { |
||
913 | this.startAssetLoad(url,callback); |
||
914 | return false; |
||
915 | } |
||
916 | else |
||
917 | return true; |
||
1 ignored issue
–
show
|
|||
918 | } |
||
919 | |||
920 | } |
||
921 | |||
922 | }; |
||
923 | |||
924 | Prado.ScriptManagerClass = jQuery.klass(Prado.AssetManagerClass, { |
||
1 ignored issue
–
show
|
|||
925 | |||
926 | findAssetUrlsInMarkup: function() { |
||
927 | var urls = new Array(); |
||
928 | var scripts = document.getElementsByTagName('script'); |
||
929 | for(var i=0;i<scripts.length;i++) |
||
930 | { |
||
931 | var e = scripts[i]; var src = e.src; |
||
932 | if (src!="") |
||
933 | urls.push(src); |
||
1 ignored issue
–
show
|
|||
934 | } |
||
935 | return urls; |
||
936 | }, |
||
937 | |||
938 | createAssetElement: function(url) { |
||
939 | var asset = document.createElement('script'); |
||
940 | asset.type = 'text/javascript'; |
||
941 | asset.src = url; |
||
942 | // asset.async = false; // HTML5 only |
||
943 | return asset; |
||
944 | } |
||
945 | |||
946 | }); |
||
947 | |||
948 | Prado.StyleSheetManagerClass = jQuery.klass(Prado.AssetManagerClass, { |
||
1 ignored issue
–
show
|
|||
949 | |||
950 | findAssetUrlsInMarkup: function() { |
||
951 | var urls = new Array(); |
||
952 | var scripts = document.getElementsByTagName('link'); |
||
953 | for(var i=0;i<scripts.length;i++) |
||
954 | { |
||
955 | var e = scripts[i]; var href = e.href; |
||
956 | if ((e.rel=="stylesheet") && (href.length>0)) |
||
957 | urls.push(href); |
||
1 ignored issue
–
show
|
|||
958 | } |
||
959 | return urls; |
||
960 | }, |
||
961 | |||
962 | createAssetElement: function(url) { |
||
963 | var asset = document.createElement('link'); |
||
964 | asset.rel = 'stylesheet'; |
||
965 | asset.media = 'screen'; |
||
966 | asset.setAttribute('type', 'text/css'); |
||
967 | asset.href = url; |
||
968 | // asset.async = false; // HTML5 only |
||
969 | return asset; |
||
970 | }, |
||
971 | |||
972 | createStyleSheetCode: function(code) { |
||
973 | var asset = document.createElement('style'); |
||
974 | asset.setAttribute('type', 'text/css'); |
||
975 | |||
976 | if(asset.styleSheet) |
||
977 | asset.styleSheet.cssText = code; // IE7+IE8 |
||
1 ignored issue
–
show
|
|||
978 | else { |
||
979 | var cssCodeNode = document.createTextNode(code); |
||
980 | asset.appendChild(cssCodeNode); |
||
981 | } |
||
982 | |||
983 | var head = document.getElementsByTagName('head')[0]; |
||
984 | head.appendChild(asset); |
||
985 | } |
||
986 | |||
987 | }); |
||
988 | |||
989 | if (typeof(Prado.ScriptManager)=="undefined") Prado.ScriptManager = new Prado.ScriptManagerClass(); |
||
2 ignored issues
–
show
|
|||
990 | if (typeof(Prado.StyleSheetManager)=="undefined") Prado.StyleSheetManager = new Prado.StyleSheetManagerClass(); |
||
1 ignored issue
–
show
|
|||
991 | |||
992 | // make sure we scan for loaded scripts again when the page has been loaded |
||
993 | var discover = function() { |
||
994 | Prado.ScriptManager.discoverLoadedAssets(); |
||
1 ignored issue
–
show
|
|||
995 | Prado.StyleSheetManager.discoverLoadedAssets(); |
||
996 | } |
||
997 | if (window.attachEvent) window.attachEvent('onload', discover); |
||
1 ignored issue
–
show
|
|||
998 | else if (window.addEventListener) window.addEventListener('load', discover, false); |
||
1 ignored issue
–
show
|
|||
999 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.