/*------------------------------------------------------------------------------------------------- Copyright (c) 2005 charliedigital.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The Software shall be used for Good, not Evil. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --------------------------------------------------------------------------------------------------- > Debug.js Charles Chen (http://www.charliedigital.com/) A debugging console for writing messages. A more convenient way to monitor running state than using window.alert(). Accompanying CSS: #debug_container { overflow-y: scroll; height:160px; background:#000; color:#ccc; font-size:9pt !important; font-family:ProFontWindows,lucida console,courier new; padding: 2px; word-wrap: break-word; } span.s { color: #6c6;} span.w { color: #fc0;} span.e { color: #f00;} span.g { } span.h { color: #ff6; } Suggested usage, in the general case, is to us position: absolute. Works with both IE5.5+ and FireFox 1.5. toggleOnClass and toggleOffClass are not fully realized yet. -------------------------------------------------------------------------------------------------*/ function Debug(enabled,htmlObj,timestamps,toggleObjId,toggleOnClass,toggleOffClass){ function Debug(){ this.Line = 0; this.Ref = htmlObj; this.HtmlObj = document.getElementById(htmlObj); this.TimestampsEnabled = timestamps; this.Enabled = enabled; this.Write = Write; this.Warn = Warn; this.Error = Error; this.Success = Success; this.Hilite = Hilite; this.ToggleColorScheme; this.ToggleVisibility; if(enabled) { this.HtmlObj.ondblclick = ToggleColorScheme; } this.ToggleObjId = toggleObjId; this.ToggleOnClass = toggleOnClass || ""; this.ToggleOffClass = toggleOffClass || ""; var toggle = document.getElementById(this.ToggleObjId); if(toggle) { toggle.onclick = ToggleVisibility; toggle.style.cursor = "pointer"; toggle.title = "Click to toggle the output console."; } Init(); } with(Debug){ var htmlHandle; function Init() { htmlHandle = this.HtmlObj; } function Warn(str) { this.Write(str,"w"); } function Error(str) { this.Write(str,"e"); } function Success(str) { this.Write(str,"s"); } function Hilite(str) { this.Write(str,"h"); } function Write(str,type){ if(this.Enabled){ if(!this.HtmlObj){ this.HtmlObj = document.body.insertBefore(document.createElement("div"), document.body.lastChild); this.HtmlObj.id = htmlObj; } htmlHandle = this.HtmlObj; var msg = "[" + PadZero(parseInt(this.Line)); if(this.TimestampsEnabled) { var date = new Date(); msg += "+" + PadSingleZero(date.getHours()) + ":" + PadSingleZero(date.getMinutes()) + ":" + PadSingleZero(date.getSeconds()); } msg += "]"; switch(type) { case "e" : msg += "[ERR]"; break; case "w" : msg += "[WRN]"; break; case "s" : msg += "[SUC]"; break; case "h" : msg += "[HLT]"; break; default: msg += "[GEN]" } msg += " " + str; var span = this.HtmlObj.insertBefore(document.createElement("span"), this.HtmlObj.firstChild); if(type) span.className = type; span.insertBefore(document.createTextNode(msg), null); this.HtmlObj.insertBefore(document.createElement("br"), this.HtmlObj.firstChild.nextSibling); this.Line++; } else{ if(this.HtmlObj){ this.HtmlObj.style.display = 'none'; } } } } function ToggleColorScheme(evt) { evt = (evt) ? evt : ((event) ? event : null); var obj = evt.srcElement; if(obj && obj.tagName && obj.tagName.toLowerCase() == "div") { if(obj.style.background && obj.style.background != "#000000") { obj.style.background = "#000000"; obj.style.color = "#cccccc"; } else { obj.style.background = "#ffffff"; obj.style.color = "#333333"; } } } function ToggleVisibility(evt) { evt = (evt) ? evt : ((event) ? event : null); var obj = evt.srcElement; if(htmlHandle.style.display == "block") { obj.className = toggleOnClass; htmlHandle.style.display = "none"; } else { obj.className = toggleOffClass; htmlHandle.style.display = "block"; } } function PadZero(num){ var output; if(num < 10) output = "000".concat(num); else if(10 <= num && num < 100) output = "00".concat(num); else if(100 <= num && num < 1000) output = "0".concat(num); else output = num; return output.toString(); } function PadSingleZero(num) { var output = String(num); if(num < 10) output = "0".concat(num); return output.toString(); } return new Debug(); }