1 // The class this file defines and its required classes 2 R.Engine.define({ 3 "class":"R.util.console.HTML", 4 "requires":[ 5 "R.debug.ConsoleRef" 6 ] 7 }); 8 9 /** 10 * @class A debug console that will use a pre-defined element to display its output. The element with the id 11 * "debug-console" will be created an appended to the DOM for you. This object is created when no other 12 * option is available from the browser, or when developer tools cannot be accessed. 13 * @extends R.debug.ConsoleRef 14 */ 15 R.util.console.HTML = R.debug.ConsoleRef.extend(/** @scope R.util.console.HTML.prototype **/{ 16 17 msgStore:null, 18 19 firstTime:null, 20 21 constructor:function () { 22 this.msgStore = []; 23 this.firstTime = true; 24 $("head", document).append( 25 "<style> " + 26 "#debug-console { position: absolute; width: 400px; right: 10px; bottom: 5px; height: 98%; border: 1px solid; overflow: auto; " + 27 "font-family: 'Lucida Console',Courier; font-size: 8pt; color: black; } " + 28 "#debug-console .console-debug, #debug-console .console-info { background: white; } " + 29 "#debug-console .console-warn { font-style: italic; background: #00ffff; } " + 30 "#debug-console .console-error { color: red; background: yellow; font-weight: bold; } " + 31 "</style>" 32 ); 33 $(document).ready(function () { 34 $(document.body).append($("<div id='debug-console'><!-- --></div>")); 35 }); 36 37 // Redirect error logging to the console 38 window.onerror = function (err) { 39 if (err instanceof Error) { 40 this.error(err.message); 41 } else { 42 this.error(err); 43 } 44 }; 45 }, 46 47 /** @private */ 48 clean:function () { 49 if ($("#debug-console > span").length > 150) { 50 $("#debug-console > span:lt(150)").remove(); 51 } 52 }, 53 54 /** @private */ 55 scroll:function () { 56 var w = $("#debug-console")[0]; 57 if (w) { 58 $("#debug-console")[0].scrollTop = w.scrollHeight + 1; 59 } 60 }, 61 62 store:function (type, args) { 63 if (!this.firstTime) { 64 return; 65 } 66 if (!document.getElementById("debug-console")) { 67 this.msgStore.push({ 68 t:type, 69 a:this.fixArgs(args) 70 }); 71 } else { 72 this.firstTime = false; 73 for (var i = 0; i < this.msgStore.length; i++) { 74 switch (this.msgStore[i].t) { 75 case "i": 76 this.info(this.msgStore[i].a); 77 break; 78 case "d": 79 this.debug(this.msgStore[i].a); 80 break; 81 case "w": 82 this.warn(this.msgStore[i].a); 83 break; 84 case "e": 85 this.error(this.msgStore[i].a); 86 break; 87 } 88 } 89 this.msgStore = null; 90 } 91 }, 92 93 /** @private */ 94 fixArgs:function (a) { 95 var o = this.base(a); 96 return o.replace(/\n/g, "<br/>").replace(/ /g, " "); 97 }, 98 99 /** 100 * Write a debug message to the console. 101 */ 102 info:function () { 103 this.clean(); 104 this.store("i", arguments); 105 $("#debug-console").append($("<div class='console-info'>" + this.fixArgs(arguments) + "</div>")); 106 this.scroll(); 107 }, 108 109 /** 110 * Write a debug message to the console 111 */ 112 debug:function () { 113 this.clean(); 114 this.store("d", arguments); 115 $("#debug-console").append($("<div class='console-debug'>" + this.fixArgs(arguments) + "</div>")); 116 this.scroll(); 117 }, 118 119 /** 120 * Write a warning message to the console 121 */ 122 warn:function () { 123 this.clean(); 124 this.store("w", arguments); 125 $("#debug-console").append($("<div class='console-warn'>" + this.fixArgs(arguments) + "</div>")); 126 this.scroll(); 127 }, 128 129 /** 130 * Write an error message to the console 131 */ 132 error:function () { 133 this.clean(); 134 this.store("e", arguments); 135 $("#debug-console").append($("<div class='console-error'>" + this.fixArgs(arguments) + "</div>")); 136 this.scroll(); 137 }, 138 139 /** 140 * Get the class name of this object 141 * 142 * @return {String} The string "R.util.console.HTML" 143 */ 144 getClassName:function () { 145 return "R.util.console.HTML"; 146 } 147 }); 148