1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/ajax/mochikit/MochiKit/LoggingPane.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,374 @@ 1.4 +/*** 1.5 + 1.6 +MochiKit.LoggingPane 1.4 1.7 + 1.8 +See <http://mochikit.com/> for documentation, downloads, license, etc. 1.9 + 1.10 +(c) 2005 Bob Ippolito. All rights Reserved. 1.11 + 1.12 +***/ 1.13 + 1.14 +if (typeof(dojo) != 'undefined') { 1.15 + dojo.provide('MochiKit.LoggingPane'); 1.16 + dojo.require('MochiKit.Logging'); 1.17 + dojo.require('MochiKit.Base'); 1.18 +} 1.19 + 1.20 +if (typeof(JSAN) != 'undefined') { 1.21 + JSAN.use("MochiKit.Logging", []); 1.22 + JSAN.use("MochiKit.Base", []); 1.23 +} 1.24 + 1.25 +try { 1.26 + if (typeof(MochiKit.Base) == 'undefined' || typeof(MochiKit.Logging) == 'undefined') { 1.27 + throw ""; 1.28 + } 1.29 +} catch (e) { 1.30 + throw "MochiKit.LoggingPane depends on MochiKit.Base and MochiKit.Logging!"; 1.31 +} 1.32 + 1.33 +if (typeof(MochiKit.LoggingPane) == 'undefined') { 1.34 + MochiKit.LoggingPane = {}; 1.35 +} 1.36 + 1.37 +MochiKit.LoggingPane.NAME = "MochiKit.LoggingPane"; 1.38 +MochiKit.LoggingPane.VERSION = "1.4"; 1.39 +MochiKit.LoggingPane.__repr__ = function () { 1.40 + return "[" + this.NAME + " " + this.VERSION + "]"; 1.41 +}; 1.42 + 1.43 +MochiKit.LoggingPane.toString = function () { 1.44 + return this.__repr__(); 1.45 +}; 1.46 + 1.47 +/** @id MochiKit.LoggingPane.createLoggingPane */ 1.48 +MochiKit.LoggingPane.createLoggingPane = function (inline/* = false */) { 1.49 + var m = MochiKit.LoggingPane; 1.50 + inline = !(!inline); 1.51 + if (m._loggingPane && m._loggingPane.inline != inline) { 1.52 + m._loggingPane.closePane(); 1.53 + m._loggingPane = null; 1.54 + } 1.55 + if (!m._loggingPane || m._loggingPane.closed) { 1.56 + m._loggingPane = new m.LoggingPane(inline, MochiKit.Logging.logger); 1.57 + } 1.58 + return m._loggingPane; 1.59 +}; 1.60 + 1.61 +/** @id MochiKit.LoggingPane.LoggingPane */ 1.62 +MochiKit.LoggingPane.LoggingPane = function (inline/* = false */, logger/* = MochiKit.Logging.logger */) { 1.63 + 1.64 + /* Use a div if inline, pop up a window if not */ 1.65 + /* Create the elements */ 1.66 + if (typeof(logger) == "undefined" || logger === null) { 1.67 + logger = MochiKit.Logging.logger; 1.68 + } 1.69 + this.logger = logger; 1.70 + var update = MochiKit.Base.update; 1.71 + var updatetree = MochiKit.Base.updatetree; 1.72 + var bind = MochiKit.Base.bind; 1.73 + var clone = MochiKit.Base.clone; 1.74 + var win = window; 1.75 + var uid = "_MochiKit_LoggingPane"; 1.76 + if (typeof(MochiKit.DOM) != "undefined") { 1.77 + win = MochiKit.DOM.currentWindow(); 1.78 + } 1.79 + if (!inline) { 1.80 + // name the popup with the base URL for uniqueness 1.81 + var url = win.location.href.split("?")[0].replace(/[#:\/.><&-]/g, "_"); 1.82 + var name = uid + "_" + url; 1.83 + var nwin = win.open("", name, "dependent,resizable,height=200"); 1.84 + if (!nwin) { 1.85 + alert("Not able to open debugging window due to pop-up blocking."); 1.86 + return undefined; 1.87 + } 1.88 + nwin.document.write( 1.89 + '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ' 1.90 + + '"http://www.w3.org/TR/html4/loose.dtd">' 1.91 + + '<html><head><title>[MochiKit.LoggingPane]</title></head>' 1.92 + + '<body></body></html>' 1.93 + ); 1.94 + nwin.document.close(); 1.95 + nwin.document.title += ' ' + win.document.title; 1.96 + win = nwin; 1.97 + } 1.98 + var doc = win.document; 1.99 + this.doc = doc; 1.100 + 1.101 + // Connect to the debug pane if it already exists (i.e. in a window orphaned by the page being refreshed) 1.102 + var debugPane = doc.getElementById(uid); 1.103 + var existing_pane = !!debugPane; 1.104 + if (debugPane && typeof(debugPane.loggingPane) != "undefined") { 1.105 + debugPane.loggingPane.logger = this.logger; 1.106 + debugPane.loggingPane.buildAndApplyFilter(); 1.107 + return debugPane.loggingPane; 1.108 + } 1.109 + 1.110 + if (existing_pane) { 1.111 + // clear any existing contents 1.112 + var child; 1.113 + while ((child = debugPane.firstChild)) { 1.114 + debugPane.removeChild(child); 1.115 + } 1.116 + } else { 1.117 + debugPane = doc.createElement("div"); 1.118 + debugPane.id = uid; 1.119 + } 1.120 + debugPane.loggingPane = this; 1.121 + var levelFilterField = doc.createElement("input"); 1.122 + var infoFilterField = doc.createElement("input"); 1.123 + var filterButton = doc.createElement("button"); 1.124 + var loadButton = doc.createElement("button"); 1.125 + var clearButton = doc.createElement("button"); 1.126 + var closeButton = doc.createElement("button"); 1.127 + var logPaneArea = doc.createElement("div"); 1.128 + var logPane = doc.createElement("div"); 1.129 + 1.130 + /* Set up the functions */ 1.131 + var listenerId = uid + "_Listener"; 1.132 + this.colorTable = clone(this.colorTable); 1.133 + var messages = []; 1.134 + var messageFilter = null; 1.135 + 1.136 + /** @id MochiKit.LoggingPane.messageLevel */ 1.137 + var messageLevel = function (msg) { 1.138 + var level = msg.level; 1.139 + if (typeof(level) == "number") { 1.140 + level = MochiKit.Logging.LogLevel[level]; 1.141 + } 1.142 + return level; 1.143 + }; 1.144 + 1.145 + /** @id MochiKit.LoggingPane.messageText */ 1.146 + var messageText = function (msg) { 1.147 + return msg.info.join(" "); 1.148 + }; 1.149 + 1.150 + /** @id MochiKit.LoggingPane.addMessageText */ 1.151 + var addMessageText = bind(function (msg) { 1.152 + var level = messageLevel(msg); 1.153 + var text = messageText(msg); 1.154 + var c = this.colorTable[level]; 1.155 + var p = doc.createElement("span"); 1.156 + p.className = "MochiKit-LogMessage MochiKit-LogLevel-" + level; 1.157 + p.style.cssText = "margin: 0px; white-space: -moz-pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; white-space: pre-line; word-wrap: break-word; wrap-option: emergency; color: " + c; 1.158 + p.appendChild(doc.createTextNode(level + ": " + text)); 1.159 + logPane.appendChild(p); 1.160 + logPane.appendChild(doc.createElement("br")); 1.161 + if (logPaneArea.offsetHeight > logPaneArea.scrollHeight) { 1.162 + logPaneArea.scrollTop = 0; 1.163 + } else { 1.164 + logPaneArea.scrollTop = logPaneArea.scrollHeight; 1.165 + } 1.166 + }, this); 1.167 + 1.168 + /** @id MochiKit.LoggingPane.addMessage */ 1.169 + var addMessage = function (msg) { 1.170 + messages[messages.length] = msg; 1.171 + addMessageText(msg); 1.172 + }; 1.173 + 1.174 + /** @id MochiKit.LoggingPane.buildMessageFilter */ 1.175 + var buildMessageFilter = function () { 1.176 + var levelre, infore; 1.177 + try { 1.178 + /* Catch any exceptions that might arise due to invalid regexes */ 1.179 + levelre = new RegExp(levelFilterField.value); 1.180 + infore = new RegExp(infoFilterField.value); 1.181 + } catch(e) { 1.182 + /* If there was an error with the regexes, do no filtering */ 1.183 + logDebug("Error in filter regex: " + e.message); 1.184 + return null; 1.185 + } 1.186 + 1.187 + return function (msg) { 1.188 + return ( 1.189 + levelre.test(messageLevel(msg)) && 1.190 + infore.test(messageText(msg)) 1.191 + ); 1.192 + }; 1.193 + }; 1.194 + 1.195 + /** @id MochiKit.LoggingPane.clearMessagePane */ 1.196 + var clearMessagePane = function () { 1.197 + while (logPane.firstChild) { 1.198 + logPane.removeChild(logPane.firstChild); 1.199 + } 1.200 + }; 1.201 + 1.202 + /** @id MochiKit.LoggingPane.clearMessages */ 1.203 + var clearMessages = function () { 1.204 + messages = []; 1.205 + clearMessagePane(); 1.206 + }; 1.207 + 1.208 + /** @id MochiKit.LoggingPane.closePane */ 1.209 + var closePane = bind(function () { 1.210 + if (this.closed) { 1.211 + return; 1.212 + } 1.213 + this.closed = true; 1.214 + if (MochiKit.LoggingPane._loggingPane == this) { 1.215 + MochiKit.LoggingPane._loggingPane = null; 1.216 + } 1.217 + this.logger.removeListener(listenerId); 1.218 + try { 1.219 + try { 1.220 + debugPane.loggingPane = null; 1.221 + } catch(e) { logFatal("Bookmarklet was closed incorrectly."); } 1.222 + if (inline) { 1.223 + debugPane.parentNode.removeChild(debugPane); 1.224 + } else { 1.225 + this.win.close(); 1.226 + } 1.227 + } catch(e) {} 1.228 + }, this); 1.229 + 1.230 + /** @id MochiKit.LoggingPane.filterMessages */ 1.231 + var filterMessages = function () { 1.232 + clearMessagePane(); 1.233 + 1.234 + for (var i = 0; i < messages.length; i++) { 1.235 + var msg = messages[i]; 1.236 + if (messageFilter === null || messageFilter(msg)) { 1.237 + addMessageText(msg); 1.238 + } 1.239 + } 1.240 + }; 1.241 + 1.242 + this.buildAndApplyFilter = function () { 1.243 + messageFilter = buildMessageFilter(); 1.244 + 1.245 + filterMessages(); 1.246 + 1.247 + this.logger.removeListener(listenerId); 1.248 + this.logger.addListener(listenerId, messageFilter, addMessage); 1.249 + }; 1.250 + 1.251 + 1.252 + /** @id MochiKit.LoggingPane.loadMessages */ 1.253 + var loadMessages = bind(function () { 1.254 + messages = this.logger.getMessages(); 1.255 + filterMessages(); 1.256 + }, this); 1.257 + 1.258 + /** @id MochiKit.LoggingPane.filterOnEnter */ 1.259 + var filterOnEnter = bind(function (event) { 1.260 + event = event || window.event; 1.261 + key = event.which || event.keyCode; 1.262 + if (key == 13) { 1.263 + this.buildAndApplyFilter(); 1.264 + } 1.265 + }, this); 1.266 + 1.267 + /* Create the debug pane */ 1.268 + var style = "display: block; z-index: 1000; left: 0px; bottom: 0px; position: fixed; width: 100%; background-color: white; font: " + this.logFont; 1.269 + if (inline) { 1.270 + style += "; height: 10em; border-top: 2px solid black"; 1.271 + } else { 1.272 + style += "; height: 100%;"; 1.273 + } 1.274 + debugPane.style.cssText = style; 1.275 + 1.276 + if (!existing_pane) { 1.277 + doc.body.appendChild(debugPane); 1.278 + } 1.279 + 1.280 + /* Create the filter fields */ 1.281 + style = {"cssText": "width: 33%; display: inline; font: " + this.logFont}; 1.282 + 1.283 + updatetree(levelFilterField, { 1.284 + "value": "FATAL|ERROR|WARNING|INFO|DEBUG", 1.285 + "onkeypress": filterOnEnter, 1.286 + "style": style 1.287 + }); 1.288 + debugPane.appendChild(levelFilterField); 1.289 + 1.290 + updatetree(infoFilterField, { 1.291 + "value": ".*", 1.292 + "onkeypress": filterOnEnter, 1.293 + "style": style 1.294 + }); 1.295 + debugPane.appendChild(infoFilterField); 1.296 + 1.297 + /* Create the buttons */ 1.298 + style = "width: 8%; display:inline; font: " + this.logFont; 1.299 + 1.300 + filterButton.appendChild(doc.createTextNode("Filter")); 1.301 + filterButton.onclick = bind("buildAndApplyFilter", this); 1.302 + filterButton.style.cssText = style; 1.303 + debugPane.appendChild(filterButton); 1.304 + 1.305 + loadButton.appendChild(doc.createTextNode("Load")); 1.306 + loadButton.onclick = loadMessages; 1.307 + loadButton.style.cssText = style; 1.308 + debugPane.appendChild(loadButton); 1.309 + 1.310 + clearButton.appendChild(doc.createTextNode("Clear")); 1.311 + clearButton.onclick = clearMessages; 1.312 + clearButton.style.cssText = style; 1.313 + debugPane.appendChild(clearButton); 1.314 + 1.315 + closeButton.appendChild(doc.createTextNode("Close")); 1.316 + closeButton.onclick = closePane; 1.317 + closeButton.style.cssText = style; 1.318 + debugPane.appendChild(closeButton); 1.319 + 1.320 + /* Create the logging pane */ 1.321 + logPaneArea.style.cssText = "overflow: auto; width: 100%"; 1.322 + logPane.style.cssText = "width: 100%; height: " + (inline ? "8em" : "100%"); 1.323 + 1.324 + logPaneArea.appendChild(logPane); 1.325 + debugPane.appendChild(logPaneArea); 1.326 + 1.327 + this.buildAndApplyFilter(); 1.328 + loadMessages(); 1.329 + 1.330 + if (inline) { 1.331 + this.win = undefined; 1.332 + } else { 1.333 + this.win = win; 1.334 + } 1.335 + this.inline = inline; 1.336 + this.closePane = closePane; 1.337 + this.closed = false; 1.338 + 1.339 + 1.340 + return this; 1.341 +}; 1.342 + 1.343 +MochiKit.LoggingPane.LoggingPane.prototype = { 1.344 + "logFont": "8pt Verdana,sans-serif", 1.345 + "colorTable": { 1.346 + "ERROR": "red", 1.347 + "FATAL": "darkred", 1.348 + "WARNING": "blue", 1.349 + "INFO": "black", 1.350 + "DEBUG": "green" 1.351 + } 1.352 +}; 1.353 + 1.354 + 1.355 +MochiKit.LoggingPane.EXPORT_OK = [ 1.356 + "LoggingPane" 1.357 +]; 1.358 + 1.359 +MochiKit.LoggingPane.EXPORT = [ 1.360 + "createLoggingPane" 1.361 +]; 1.362 + 1.363 +MochiKit.LoggingPane.__new__ = function () { 1.364 + this.EXPORT_TAGS = { 1.365 + ":common": this.EXPORT, 1.366 + ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK) 1.367 + }; 1.368 + 1.369 + MochiKit.Base.nameFunctions(this); 1.370 + 1.371 + MochiKit.LoggingPane._loggingPane = null; 1.372 + 1.373 +}; 1.374 + 1.375 +MochiKit.LoggingPane.__new__(); 1.376 + 1.377 +MochiKit.Base._exportSymbols(this, MochiKit.LoggingPane);