Total Complexity | 149 |
Complexity/F | 3.31 |
Lines of Code | 732 |
Function Count | 45 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Complex classes like framework/Web/Javascripts/source/prado/logger/logger.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 TJavascriptLogger javascript file | github.com/pradosoft/prado */ |
||
18 | CustomEvent = jQuery.klass({ |
||
|
|||
19 | initialize : function() { |
||
20 | this.listeners = [] |
||
21 | }, |
||
22 | |||
23 | addListener : function(method) { |
||
24 | this.listeners.push(method) |
||
25 | }, |
||
26 | |||
27 | removeListener : function(method) { |
||
28 | var foundIndexes = this._findListenerIndexes(method) |
||
29 | |||
30 | for(var i = 0; i < foundIndexes.length; i++) { |
||
31 | this.listeners.splice(foundIndexes[i], 1) |
||
32 | } |
||
33 | }, |
||
34 | |||
35 | dispatch : function(handler) { |
||
36 | for(var i = 0; i < this.listeners.length; i++) { |
||
37 | try { |
||
38 | this.listeners[i](handler) |
||
39 | } |
||
40 | catch (e) { |
||
41 | alert("Could not run the listener " + this.listeners[i] + ". " + e) |
||
42 | } |
||
43 | } |
||
44 | }, |
||
45 | |||
46 | // Private Methods |
||
47 | // --------------- |
||
48 | _findListenerIndexes : function(method) { |
||
49 | var indexes = [] |
||
50 | for(var i = 0; i < this.listeners.length; i++) { |
||
51 | if (this.listeners[i] == method) { |
||
52 | indexes.push(i) |
||
53 | } |
||
54 | } |
||
55 | |||
56 | return indexes |
||
57 | } |
||
58 | }); |
||
59 | |||
60 | // ------ |
||
61 | // Cookie |
||
62 | // ------ |
||
63 | |||
64 | var Cookie = { |
||
65 | set : function(name, value, expirationInDays, path) { |
||
66 | var cookie = escape(name) + "=" + escape(value) |
||
67 | |||
68 | if (expirationInDays) { |
||
69 | var date = new Date() |
||
70 | date.setDate(date.getDate() + expirationInDays) |
||
71 | cookie += "; expires=" + date.toGMTString() |
||
72 | } |
||
73 | |||
74 | if (path) { |
||
75 | cookie += ";path=" + path |
||
76 | } |
||
77 | |||
78 | document.cookie = cookie |
||
79 | |||
80 | if (value && (expirationInDays == undefined || expirationInDays > 0) && !this.get(name)) { |
||
81 | Logger.error("Cookie (" + name + ") was not set correctly... The value was " + value.toString().length + " charachters long (This may be over the cookie limit)"); |
||
1 ignored issue
–
show
|
|||
82 | } |
||
83 | }, |
||
84 | |||
85 | get : function(name) { |
||
86 | var pattern = "(^|;)\\s*" + escape(name) + "=([^;]+)" |
||
87 | |||
88 | var m = document.cookie.match(pattern) |
||
89 | if (m && m[2]) { |
||
90 | return unescape(m[2]) |
||
91 | } |
||
92 | else return null |
||
1 ignored issue
–
show
|
|||
93 | }, |
||
94 | |||
95 | getAll : function() { |
||
96 | var cookies = document.cookie.split(';') |
||
97 | var cookieArray = [] |
||
98 | |||
99 | for (var i = 0; i < cookies.length; i++) { |
||
100 | try { |
||
101 | var name = unescape(cookies[i].match(/^\s*([^=]+)/m)[1]) |
||
102 | var value = unescape(cookies[i].match(/=(.*$)/m)[1]) |
||
103 | } |
||
104 | catch (e) { |
||
105 | continue |
||
106 | } |
||
107 | |||
108 | cookieArray.push({name : name, value : value}) |
||
109 | |||
110 | if (cookieArray[name] != undefined) { |
||
111 | Logger.waring("Trying to retrieve cookie named(" + name + "). There appears to be another property with this name though."); |
||
1 ignored issue
–
show
|
|||
112 | } |
||
113 | |||
114 | cookieArray[name] = value |
||
115 | } |
||
116 | |||
117 | return cookieArray |
||
118 | }, |
||
119 | |||
120 | clear : function(name) { |
||
121 | this.set(name, "", -1) |
||
122 | }, |
||
123 | |||
124 | clearAll : function() { |
||
125 | var cookies = this.getAll() |
||
126 | |||
127 | for(var i = 0; i < cookies.length; i++) { |
||
128 | this.clear(cookies[i].name) |
||
129 | } |
||
130 | |||
131 | } |
||
132 | }; |
||
133 | |||
134 | // ------ |
||
135 | // Logger |
||
136 | // ----- |
||
137 | |||
138 | Logger = { |
||
139 | logEntries : [], |
||
140 | |||
141 | onupdate : new CustomEvent(), |
||
142 | onclear : new CustomEvent(), |
||
143 | |||
144 | |||
145 | // Logger output |
||
146 | log : function(message, tag) { |
||
147 | var logEntry = new LogEntry(message, tag || "info") |
||
148 | this.logEntries.push(logEntry) |
||
149 | this.onupdate.dispatch(logEntry) |
||
150 | }, |
||
151 | |||
152 | info : function(message) { |
||
153 | this.log(message, 'info') |
||
154 | if(typeof(console) != "undefined") |
||
155 | console.info(message); |
||
1 ignored issue
–
show
|
|||
156 | }, |
||
157 | |||
158 | debug : function(message) { |
||
159 | this.log(message, 'debug') |
||
160 | if(typeof(console) != "undefined") |
||
161 | console.debug(message); |
||
1 ignored issue
–
show
|
|||
162 | }, |
||
163 | |||
164 | warn : function(message) { |
||
165 | this.log(message, 'warning') |
||
166 | if(typeof(console) != "undefined") |
||
167 | console.warn(message); |
||
1 ignored issue
–
show
|
|||
168 | }, |
||
169 | |||
170 | error : function(message, error) { |
||
171 | this.log(message + ": \n" + error, 'error') |
||
172 | if(typeof(console) != "undefined") |
||
173 | console.error(message + ": \n" + error); |
||
1 ignored issue
–
show
|
|||
174 | |||
175 | }, |
||
176 | |||
177 | clear : function () { |
||
178 | this.logEntries = [] |
||
179 | this.onclear.dispatch() |
||
180 | } |
||
181 | }; |
||
182 | |||
183 | LogEntry = jQuery.klass({ |
||
184 | initialize : function(message, tag) { |
||
185 | this.message = message |
||
186 | this.tag = tag |
||
187 | } |
||
188 | }); |
||
189 | |||
190 | LogConsole = jQuery.klass({ |
||
191 | |||
192 | // Properties |
||
193 | // ---------- |
||
194 | commandHistory : [], |
||
195 | commandIndex : 0, |
||
196 | |||
197 | hidden : true, |
||
198 | |||
199 | // Methods |
||
200 | // ------- |
||
201 | |||
202 | initialize : function(toggleKey) { |
||
203 | this.outputCount = 0 |
||
204 | this.tagPattern = Cookie.get('tagPattern') || ".*" |
||
205 | |||
206 | // I hate writing javascript in HTML... but what's a better alternative |
||
207 | this.logElement = document.createElement('div') |
||
208 | document.body.appendChild(this.logElement) |
||
209 | jQuery(this.logElement).hide(); |
||
210 | |||
211 | this.logElement.style.position = "absolute" |
||
212 | this.logElement.style.left = '0px' |
||
213 | this.logElement.style.width = '100%' |
||
214 | |||
215 | this.logElement.style.textAlign = "left" |
||
216 | this.logElement.style.fontFamily = "lucida console" |
||
217 | this.logElement.style.fontSize = "100%" |
||
218 | this.logElement.style.backgroundColor = 'darkgray' |
||
219 | this.logElement.style.opacity = 0.9 |
||
220 | this.logElement.style.zIndex = 2000 |
||
221 | |||
222 | // Add toolbarElement |
||
223 | this.toolbarElement = document.createElement('div') |
||
224 | this.logElement.appendChild(this.toolbarElement) |
||
225 | this.toolbarElement.style.padding = "0 0 0 2px" |
||
226 | |||
227 | // Add buttons |
||
228 | this.buttonsContainerElement = document.createElement('span') |
||
229 | this.toolbarElement.appendChild(this.buttonsContainerElement) |
||
230 | |||
231 | this.buttonsContainerElement.innerHTML += '<button onclick="logConsole.toggle()" style="float:right;color:black">close</button>' |
||
232 | this.buttonsContainerElement.innerHTML += '<button onclick="Logger.clear()" style="float:right;color:black">clear</button>' |
||
233 | if(!Prado.Inspector.disabled) |
||
234 | this.buttonsContainerElement.innerHTML += '<button onclick="Prado.Inspector.inspect()" style="float:right;color:black; margin-right:15px;">Object Tree</button>' |
||
1 ignored issue
–
show
|
|||
235 | |||
236 | |||
237 | //Add Tag Filter |
||
238 | this.tagFilterContainerElement = document.createElement('span') |
||
239 | this.toolbarElement.appendChild(this.tagFilterContainerElement) |
||
240 | this.tagFilterContainerElement.style.cssFloat = 'left' |
||
241 | this.tagFilterContainerElement.appendChild(document.createTextNode("Log Filter")) |
||
242 | |||
243 | this.tagFilterElement = document.createElement('input') |
||
244 | this.tagFilterContainerElement.appendChild(this.tagFilterElement) |
||
245 | this.tagFilterElement.style.width = '200px' |
||
246 | this.tagFilterElement.value = this.tagPattern |
||
247 | this.tagFilterElement.setAttribute('autocomplete', 'off') // So Firefox doesn't flip out |
||
248 | |||
249 | jQuery(this.tagFilterElement).on('keyup', this.updateTags.bind(this)); |
||
250 | jQuery(this.tagFilterElement).on('click', function() {this.tagFilterElement.select()}.bind(this)); |
||
251 | |||
252 | // Add outputElement |
||
253 | this.outputElement = document.createElement('div') |
||
254 | this.logElement.appendChild(this.outputElement) |
||
255 | this.outputElement.style.overflow = "auto" |
||
256 | this.outputElement.style.clear = "both" |
||
257 | this.outputElement.style.height = "200px" |
||
258 | this.outputElement.style.backgroundColor = 'black' |
||
259 | |||
260 | this.inputContainerElement = document.createElement('div') |
||
261 | this.inputContainerElement.style.width = "100%" |
||
262 | this.logElement.appendChild(this.inputContainerElement) |
||
263 | |||
264 | this.inputElement = document.createElement('input') |
||
265 | this.inputContainerElement.appendChild(this.inputElement) |
||
266 | this.inputElement.style.width = '100%' |
||
267 | this.inputElement.style.borderWidth = '0px' // Inputs with 100% width always seem to be too large (I HATE THEM) they only work if the border, margin and padding are 0 |
||
268 | this.inputElement.style.margin = '0px' |
||
269 | this.inputElement.style.padding = '0px' |
||
270 | this.inputElement.value = 'Type command here' |
||
271 | this.inputElement.setAttribute('autocomplete', 'off') // So Firefox doesn't flip out |
||
272 | |||
273 | jQuery(this.inputElement).on('keyup', this.handleInput.bind(this)); |
||
274 | jQuery(this.inputElement).on('click', function() {this.inputElement.select()}.bind(this)); |
||
275 | |||
276 | if(document.all && !window.opera) |
||
277 | { |
||
278 | window.setInterval(this.repositionWindow.bind(this), 500) |
||
279 | } |
||
280 | else |
||
281 | { |
||
282 | this.logElement.style.position="fixed"; |
||
283 | this.logElement.style.bottom="0px"; |
||
284 | } |
||
285 | var self=this; |
||
286 | jQuery(document).on('keydown', function(e) |
||
287 | { |
||
288 | if((e.altKey==true) && e.keyCode == toggleKey ) //Alt+J | Ctrl+J |
||
289 | self.toggle(); |
||
1 ignored issue
–
show
|
|||
290 | }); |
||
291 | |||
292 | // Listen to the logger.... |
||
293 | Logger.onupdate.addListener(this.logUpdate.bind(this)) |
||
294 | Logger.onclear.addListener(this.clear.bind(this)) |
||
295 | |||
296 | // Preload log element with the log entries that have been entered |
||
297 | for (var i = 0; i < Logger.logEntries.length; i++) { |
||
298 | this.logUpdate(Logger.logEntries[i]) |
||
299 | } |
||
300 | |||
301 | // Feed all errors into the logger (For some unknown reason I can only get this to work |
||
302 | // with an inline event declaration) |
||
303 | jQuery(window).on('error', function(msg, url, lineNumber) {Logger.error("Error in (" + (url || location) + ") on line "+lineNumber+"", msg)}); |
||
304 | |||
305 | // Allow acess key link |
||
306 | var accessElement = document.createElement('span') |
||
307 | accessElement.innerHTML = '<button style="position:absolute;top:-100px" onclick="javascript:logConsole.toggle()" accesskey="d"></button>' |
||
308 | document.body.appendChild(accessElement) |
||
309 | |||
310 | if (Cookie.get('ConsoleVisible') == 'true') { |
||
311 | this.toggle() |
||
312 | } |
||
313 | }, |
||
314 | |||
315 | toggle : function() { |
||
316 | if (this.logElement.style.display == 'none') { |
||
317 | this.show(); |
||
318 | } |
||
319 | else { |
||
320 | this.hide(); |
||
321 | } |
||
322 | }, |
||
323 | |||
324 | show : function() { |
||
325 | jQuery(this.logElement).show(); |
||
326 | this.outputElement.scrollTop = this.outputElement.scrollHeight // Scroll to bottom when toggled |
||
327 | if(document.all && !window.opera) |
||
328 | this.repositionWindow(); |
||
1 ignored issue
–
show
|
|||
329 | Cookie.set('ConsoleVisible', 'true') |
||
330 | this.inputElement.select(); |
||
331 | this.hidden = false; |
||
332 | }, |
||
333 | |||
334 | hide : function() { |
||
335 | this.hidden = true; |
||
336 | jQuery(this.logElement).hide(); |
||
337 | Cookie.set('ConsoleVisible', 'false'); |
||
338 | }, |
||
339 | |||
340 | output : function(message, style) { |
||
341 | // If we are at the bottom of the window, then keep scrolling with the output |
||
342 | var shouldScroll = (this.outputElement.scrollTop + (2 * this.outputElement.clientHeight)) >= this.outputElement.scrollHeight |
||
343 | |||
344 | this.outputCount++ |
||
345 | style = (style ? style += ';' : '') |
||
346 | style += 'padding:1px;margin:0 0 5px 0' |
||
347 | |||
348 | if (this.outputCount % 2 == 0) style += ";background-color:#101010" |
||
1 ignored issue
–
show
|
|||
349 | |||
350 | message = message || "undefined" |
||
351 | message = message.toString().replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); |
||
352 | |||
353 | this.outputElement.innerHTML += "<pre style='" + style + "'>" + message + "</pre>" |
||
354 | |||
355 | if (shouldScroll) { |
||
356 | this.outputElement.scrollTop = this.outputElement.scrollHeight |
||
357 | } |
||
358 | }, |
||
359 | |||
360 | updateTags : function() { |
||
361 | var pattern = this.tagFilterElement.value |
||
362 | |||
363 | if (this.tagPattern == pattern) return |
||
1 ignored issue
–
show
|
|||
364 | |||
365 | try { |
||
366 | new RegExp(pattern) |
||
367 | } |
||
368 | catch (e) { |
||
369 | return |
||
370 | } |
||
371 | |||
372 | this.tagPattern = pattern |
||
373 | Cookie.set('tagPattern', this.tagPattern) |
||
374 | |||
375 | this.outputElement.innerHTML = "" |
||
376 | |||
377 | // Go through each log entry again |
||
378 | this.outputCount = 0; |
||
379 | for (var i = 0; i < Logger.logEntries.length; i++) { |
||
380 | this.logUpdate(Logger.logEntries[i]) |
||
381 | } |
||
382 | }, |
||
383 | |||
384 | repositionWindow : function() { |
||
385 | var offset = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop |
||
386 | var pageHeight = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight |
||
387 | this.logElement.style.top = (offset + pageHeight - Element.getHeight(this.logElement)) + "px" |
||
388 | }, |
||
389 | |||
390 | // Event Handlers |
||
391 | // -------------- |
||
392 | |||
393 | logUpdate : function(logEntry) { |
||
394 | if (logEntry.tag.search(new RegExp(this.tagPattern, 'igm')) == -1) return |
||
1 ignored issue
–
show
|
|||
395 | var style = '' |
||
396 | if (logEntry.tag.search(/error/) != -1) style += 'color:red' |
||
1 ignored issue
–
show
|
|||
397 | else if (logEntry.tag.search(/warning/) != -1) style += 'color:orange' |
||
1 ignored issue
–
show
|
|||
398 | else if (logEntry.tag.search(/debug/) != -1) style += 'color:green' |
||
1 ignored issue
–
show
|
|||
399 | else if (logEntry.tag.search(/info/) != -1) style += 'color:white' |
||
1 ignored issue
–
show
|
|||
400 | else style += 'color:yellow' |
||
401 | |||
402 | this.output(logEntry.message, style) |
||
403 | }, |
||
404 | |||
405 | clear : function(e) { |
||
406 | this.outputElement.innerHTML = "" |
||
407 | }, |
||
408 | |||
409 | handleInput : function(e) { |
||
410 | if (e.keyCode == 13 ) { |
||
411 | var command = this.inputElement.value |
||
412 | |||
413 | switch(command) { |
||
414 | case "clear": |
||
415 | Logger.clear() |
||
416 | break |
||
417 | |||
418 | default: |
||
419 | var consoleOutput = "" |
||
420 | |||
421 | try { |
||
422 | consoleOutput = eval(this.inputElement.value) |
||
423 | } |
||
424 | catch (e) { |
||
425 | Logger.error("Problem parsing input <" + command + ">", e) |
||
426 | break |
||
427 | } |
||
428 | |||
429 | Logger.log(consoleOutput) |
||
430 | break |
||
431 | } |
||
432 | |||
433 | if (this.inputElement.value != "" && this.inputElement.value != this.commandHistory[0]) { |
||
434 | this.commandHistory.unshift(this.inputElement.value) |
||
435 | } |
||
436 | |||
437 | this.commandIndex = 0 |
||
438 | this.inputElement.value = "" |
||
439 | } |
||
440 | else if (e.keyCode == 38 && this.commandHistory.length > 0) { |
||
441 | this.inputElement.value = this.commandHistory[this.commandIndex] |
||
442 | |||
443 | if (this.commandIndex < this.commandHistory.length - 1) { |
||
444 | this.commandIndex += 1 |
||
445 | } |
||
446 | } |
||
447 | else if (e.keyCode == 40 && this.commandHistory.length > 0) { |
||
448 | if (this.commandIndex > 0) { |
||
449 | this.commandIndex -= 1 |
||
450 | } |
||
451 | |||
452 | this.inputElement.value = this.commandHistory[this.commandIndex] |
||
453 | } |
||
454 | else { |
||
455 | this.commandIndex = 0 |
||
456 | } |
||
457 | } |
||
458 | }); |
||
459 | |||
460 | |||
461 | // ------------------------- |
||
462 | // Helper Functions And Junk |
||
463 | // ------------------------- |
||
464 | function inspect(o) |
||
465 | { |
||
466 | var objtype = typeof(o); |
||
467 | if (objtype == "undefined") { |
||
468 | return "undefined"; |
||
469 | } else if (objtype == "number" || objtype == "boolean") { |
||
470 | return o + ""; |
||
471 | } else if (o === null) { |
||
472 | return "null"; |
||
473 | } |
||
474 | |||
475 | try { |
||
476 | var ostring = (o + ""); |
||
477 | } catch (e) { |
||
478 | return "[" + typeof(o) + "]"; |
||
479 | } |
||
480 | |||
481 | if (typeof(o) == "function") |
||
482 | { |
||
483 | o = ostring.replace(/^\s+/, ""); |
||
484 | var idx = o.indexOf("{"); |
||
485 | if (idx != -1) { |
||
486 | o = o.substr(0, idx) + "{...}"; |
||
487 | } |
||
488 | return o; |
||
489 | } |
||
490 | |||
491 | var reprString = function (o) |
||
492 | { |
||
493 | return ('"' + o.replace(/(["\\])/g, '\\$1') + '"' |
||
494 | ).replace(/[\f]/g, "\\f" |
||
495 | ).replace(/[\b]/g, "\\b" |
||
496 | ).replace(/[\n]/g, "\\n" |
||
497 | ).replace(/[\t]/g, "\\t" |
||
498 | ).replace(/[\r]/g, "\\r"); |
||
499 | }; |
||
500 | |||
501 | if (objtype == "string") { |
||
502 | return reprString(o); |
||
503 | } |
||
504 | // recurse |
||
505 | var me = arguments.callee; |
||
506 | // short-circuit for objects that support "json" serialization |
||
507 | // if they return "self" then just pass-through... |
||
508 | var newObj; |
||
509 | if (typeof(o.__json__) == "function") { |
||
510 | newObj = o.__json__(); |
||
511 | if (o !== newObj) { |
||
512 | return me(newObj); |
||
513 | } |
||
514 | } |
||
515 | if (typeof(o.json) == "function") { |
||
516 | newObj = o.json(); |
||
517 | if (o !== newObj) { |
||
518 | return me(newObj); |
||
519 | } |
||
520 | } |
||
521 | // array |
||
522 | if (objtype != "function" && typeof(o.length) == "number") { |
||
523 | var res = []; |
||
524 | for (var i = 0; i < o.length; i++) { |
||
525 | var val = me(o[i]); |
||
526 | if (typeof(val) != "string") { |
||
527 | val = "undefined"; |
||
528 | } |
||
529 | res.push(val); |
||
530 | } |
||
531 | return "[" + res.join(", ") + "]"; |
||
532 | } |
||
533 | |||
534 | // generic object code path |
||
535 | res = []; |
||
536 | for (var k in o) { |
||
537 | var useKey; |
||
538 | if (typeof(k) == "number") { |
||
539 | useKey = '"' + k + '"'; |
||
540 | } else if (typeof(k) == "string") { |
||
541 | useKey = reprString(k); |
||
542 | } else { |
||
543 | // skip non-string or number keys |
||
544 | continue; |
||
545 | } |
||
546 | val = me(o[k]); |
||
547 | if (typeof(val) != "string") { |
||
548 | // skip non-serializable values |
||
549 | continue; |
||
550 | } |
||
551 | res.push(useKey + ":" + val); |
||
552 | } |
||
553 | return "{" + res.join(", ") + "}"; |
||
554 | }; |
||
555 | |||
556 | Array.prototype.contains = function(object) { |
||
557 | for(var i = 0; i < this.length; i++) { |
||
558 | if (object == this[i]) return true |
||
1 ignored issue
–
show
|
|||
559 | } |
||
560 | |||
561 | return false |
||
562 | }; |
||
563 | |||
564 | // Helper Alias for simple logging |
||
565 | var puts = function() {return Logger.log(arguments[0], arguments[1])}; |
||
566 | |||
567 | /************************************* |
||
568 | |||
569 | Javascript Object Tree |
||
570 | version 1.0 |
||
571 | last revision:04.11.2004 |
||
572 | [email protected] |
||
573 | http://slayeroffice.com |
||
574 | |||
575 | (c)2004 S.G. Chipman |
||
576 | |||
577 | Please notify me of any modifications |
||
578 | you make to this code so that I can |
||
579 | update the version hosted on slayeroffice.com |
||
580 | |||
581 | |||
582 | ************************************/ |
||
583 | if(typeof Prado == "undefined") |
||
584 | var Prado = {}; |
||
1 ignored issue
–
show
|
|||
585 | Prado.Inspector = |
||
586 | { |
||
587 | d : document, |
||
588 | types : new Array(), |
||
589 | objs : new Array(), |
||
590 | hidden : new Array(), |
||
591 | opera : window.opera, |
||
592 | displaying : '', |
||
593 | nameList : new Array(), |
||
594 | |||
595 | format : function(str) { |
||
596 | if(typeof(str) != "string") return str; |
||
1 ignored issue
–
show
|
|||
597 | str=str.replace(/</g,"<"); |
||
598 | str=str.replace(/>/g,">"); |
||
599 | return str; |
||
600 | }, |
||
601 | |||
602 | parseJS : function(obj) { |
||
603 | var name; |
||
604 | if(typeof obj == "string") { name = obj; obj = eval(obj); } |
||
605 | win= typeof obj == 'undefined' ? window : obj; |
||
606 | this.displaying = name ? name : win.toString(); |
||
607 | for(js in win) { |
||
608 | try { |
||
609 | if(win[js] && js.toString().indexOf("Inspector")==-1 && (win[js]+"").indexOf("[native code]")==-1) { |
||
610 | |||
611 | t=typeof(win[js]); |
||
612 | if(!this.objs[t.toString()]) { |
||
613 | this.types[this.types.length]=t; |
||
614 | this.objs[t]={}; |
||
615 | this.nameList[t] = new Array(); |
||
616 | } |
||
617 | this.nameList[t].push(js); |
||
618 | this.objs[t][js] = this.format(win[js]+""); |
||
619 | } |
||
620 | } catch(err) { } |
||
621 | } |
||
622 | |||
623 | for(i=0;i<this.types.length;i++) |
||
624 | this.nameList[this.types[i]].sort(); |
||
1 ignored issue
–
show
|
|||
625 | }, |
||
626 | |||
627 | show : function(objID) { |
||
628 | this.d.getElementById(objID).style.display=this.hidden[objID]?"none":"block"; |
||
629 | this.hidden[objID]=this.hidden[objID]?0:1; |
||
630 | }, |
||
631 | |||
632 | changeSpan : function(spanID) { |
||
633 | if(this.d.getElementById(spanID).innerHTML.indexOf("+")>-1){ |
||
634 | this.d.getElementById(spanID).innerHTML="[-]"; |
||
635 | } else { |
||
636 | this.d.getElementById(spanID).innerHTML="[+]"; |
||
637 | } |
||
638 | }, |
||
639 | |||
640 | buildInspectionLevel : function() |
||
641 | { |
||
642 | var display = this.displaying; |
||
643 | var list = display.split("."); |
||
644 | var links = ["<a href=\"javascript:var_dump()\">[object Window]</a>"]; |
||
645 | var name = ''; |
||
646 | if(display.indexOf("[object ") >= 0) return links.join("."); |
||
1 ignored issue
–
show
|
|||
647 | for(var i = 0; i < list.length; i++) |
||
648 | { |
||
649 | name += (name.length ? "." : "") + list[i]; |
||
650 | links[i+1] = "<a href=\"javascript:var_dump('"+name+"')\">"+list[i]+"</a>"; |
||
651 | } |
||
652 | return links.join("."); |
||
653 | }, |
||
654 | |||
655 | buildTree : function() { |
||
656 | mHTML = "<div>Inspecting "+this.buildInspectionLevel()+"</div>"; |
||
657 | mHTML +="<ul class=\"topLevel\">"; |
||
658 | this.types.sort(); |
||
659 | var so_objIndex=0; |
||
660 | for(i=0;i<this.types.length;i++) |
||
661 | { |
||
662 | mHTML+="<li style=\"cursor:pointer;\" onclick=\"Prado.Inspector.show('ul"+i+"');Prado.Inspector.changeSpan('sp" + i + "')\"><span id=\"sp" + i + "\">[+]</span><b>" + this.types[i] + "</b> (" + this.nameList[this.types[i]].length + ")</li><ul style=\"display:none;\" id=\"ul"+i+"\">"; |
||
663 | this.hidden["ul"+i]=0; |
||
664 | for(e=0;e<this.nameList[this.types[i]].length;e++) |
||
665 | { |
||
666 | var prop = this.nameList[this.types[i]][e]; |
||
667 | var value = this.objs[this.types[i]][prop] |
||
668 | var more = ""; |
||
669 | if(value.indexOf("[object ") >= 0 && /^[a-zA-Z_]/.test(prop)) |
||
670 | { |
||
671 | if(this.displaying.indexOf("[object ") < 0) |
||
672 | more = " <a href=\"javascript:var_dump('"+this.displaying+"."+prop+"')\"><b>more</b></a>"; |
||
1 ignored issue
–
show
|
|||
673 | else if(this.displaying.indexOf("[object Window]") >= 0) |
||
674 | more = " <a href=\"javascript:var_dump('"+prop+"')\"><b>more</b></a>"; |
||
1 ignored issue
–
show
|
|||
675 | } |
||
676 | mHTML+="<li style=\"cursor:pointer;\" onclick=\"Prado.Inspector.show('mul" + so_objIndex + "');Prado.Inspector.changeSpan('sk" + so_objIndex + "')\"><span id=\"sk" + so_objIndex + "\">[+]</span>" + prop + "</li><ul id=\"mul" + so_objIndex + "\" style=\"display:none;\"><li style=\"list-style-type:none;\"><pre>" + value + more + "</pre></li></ul>"; |
||
677 | this.hidden["mul"+so_objIndex]=0; |
||
678 | so_objIndex++; |
||
679 | } |
||
680 | mHTML+="</ul>"; |
||
681 | } |
||
682 | mHTML+="</ul>"; |
||
683 | this.d.getElementById("so_mContainer").innerHTML =mHTML; |
||
684 | }, |
||
685 | |||
686 | handleKeyEvent : function(e) { |
||
687 | keyCode=document.all?window.event.keyCode:e.keyCode; |
||
688 | if(keyCode==27) { |
||
689 | this.cleanUp(); |
||
690 | } |
||
691 | }, |
||
692 | |||
693 | cleanUp : function() |
||
694 | { |
||
695 | if(this.d.getElementById("so_mContainer")) |
||
696 | { |
||
697 | this.d.body.removeChild(this.d.getElementById("so_mContainer")); |
||
698 | this.d.body.removeChild(this.d.getElementById("so_mStyle")); |
||
699 | jQuery(this.d).unbind("keydown", this.dKeyDownEvent); |
||
700 | this.types = new Array(); |
||
701 | this.objs = new Array(); |
||
702 | this.hidden = new Array(); |
||
703 | } |
||
704 | }, |
||
705 | |||
706 | disabled : document.all && !this.opera, |
||
707 | |||
708 | inspect : function(obj) |
||
709 | { |
||
710 | if(this.disabled)return alert("Sorry, this only works in Mozilla and Firefox currently."); |
||
1 ignored issue
–
show
|
|||
711 | this.cleanUp(); |
||
712 | mObj=this.d.body.appendChild(this.d.createElement("div")); |
||
713 | mObj.id="so_mContainer"; |
||
714 | sObj=this.d.body.appendChild(this.d.createElement("style")); |
||
715 | sObj.id="so_mStyle"; |
||
716 | sObj.type="text/css"; |
||
717 | sObj.innerHTML = this.style; |
||
718 | this.dKeyDownEvent = this.handleKeyEvent.bind(this); |
||
719 | jQuery(this.d).on("keydown", this.dKeyDownEvent); |
||
720 | |||
721 | this.parseJS(obj); |
||
722 | this.buildTree(); |
||
723 | |||
724 | cObj=mObj.appendChild(this.d.createElement("div")); |
||
725 | cObj.className="credits"; |
||
726 | cObj.innerHTML = "<b>[esc] to <a href=\"javascript:Prado.Inspector.cleanUp();\">close</a></b><br />Javascript Object Tree V2.0."; |
||
727 | |||
728 | window.scrollTo(0,0); |
||
729 | }, |
||
730 | |||
731 | style : "#so_mContainer { position:absolute; top:5px; left:5px; background-color:#E3EBED; text-align:left; font:9pt verdana; width:85%; border:2px solid #000; padding:5px; z-index:1000; color:#000; } " + |
||
732 | "#so_mContainer ul { padding-left:20px; } " + |
||
733 | "#so_mContainer ul li { display:block; list-style-type:none; list-style-image:url(); line-height:2em; -moz-border-radius:.75em; font:10px verdana; padding:0; margin:2px; color:#000; } " + |
||
734 | "#so_mContainer li:hover { background-color:#E3EBED; } " + |
||
735 | "#so_mContainer ul li span { position:relative; width:15px; height:15px; margin-right:4px; } " + |
||
736 | "#so_mContainer pre { background-color:#F9FAFB; border:1px solid #638DA1; height:auto; padding:5px; font:9px verdana; color:#000; } " + |
||
737 | "#so_mContainer .topLevel { margin:0; padding:0; } " + |
||
738 | "#so_mContainer .credits { float:left; width:200px; font:6.5pt verdana; color:#000; padding:2px; margin-left:5px; text-align:left; border-top:1px solid #000; margin-top:15px; width:75%; } " + |
||
739 | "#so_mContainer .credits a { font:9px verdana; font-weight:bold; color:#004465; text-decoration:none; background-color:transparent; }" |
||
740 | }; |
||
741 | |||
742 | //similar function to var_dump in PHP, brings up the javascript object tree UI. |
||
743 | function var_dump(obj) |
||
744 | { |
||
745 | Prado.Inspector.inspect(obj); |
||
746 | } |
||
747 | |||
748 | //similar function to print_r for PHP |
||
749 | var print_r = inspect; |
||
750 | |||
751 |