author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
commit | ef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff) | |
tree | c403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/gamma/js/MochiKit/LoggingPane.js | |
parent | 597ecfbc0249d83e1b856cbd558340c01237a360 (diff) | |
download | clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2 |
First version of the newly restructured repository
Diffstat (limited to 'frontend/gamma/js/MochiKit/LoggingPane.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/MochiKit/LoggingPane.js | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/frontend/gamma/js/MochiKit/LoggingPane.js b/frontend/gamma/js/MochiKit/LoggingPane.js new file mode 100644 index 0000000..c960c21 --- a/dev/null +++ b/frontend/gamma/js/MochiKit/LoggingPane.js | |||
@@ -0,0 +1,327 @@ | |||
1 | /*** | ||
2 | |||
3 | MochiKit.LoggingPane 1.5 | ||
4 | |||
5 | See <http://mochikit.com/> for documentation, downloads, license, etc. | ||
6 | |||
7 | (c) 2005 Bob Ippolito. All rights Reserved. | ||
8 | |||
9 | ***/ | ||
10 | |||
11 | MochiKit.Base._module('LoggingPane', '1.5', ['Base', 'Logging']); | ||
12 | |||
13 | /** @id MochiKit.LoggingPane.createLoggingPane */ | ||
14 | MochiKit.LoggingPane.createLoggingPane = function (inline/* = false */) { | ||
15 | var m = MochiKit.LoggingPane; | ||
16 | inline = !(!inline); | ||
17 | if (m._loggingPane && m._loggingPane.inline != inline) { | ||
18 | m._loggingPane.closePane(); | ||
19 | m._loggingPane = null; | ||
20 | } | ||
21 | if (!m._loggingPane || m._loggingPane.closed) { | ||
22 | m._loggingPane = new m.LoggingPane(inline, MochiKit.Logging.logger); | ||
23 | } | ||
24 | return m._loggingPane; | ||
25 | }; | ||
26 | |||
27 | /** @id MochiKit.LoggingPane.LoggingPane */ | ||
28 | MochiKit.LoggingPane.LoggingPane = function (inline/* = false */, logger/* = MochiKit.Logging.logger */) { | ||
29 | |||
30 | /* Use a div if inline, pop up a window if not */ | ||
31 | /* Create the elements */ | ||
32 | if (typeof(logger) == "undefined" || logger === null) { | ||
33 | logger = MochiKit.Logging.logger; | ||
34 | } | ||
35 | this.logger = logger; | ||
36 | var update = MochiKit.Base.update; | ||
37 | var updatetree = MochiKit.Base.updatetree; | ||
38 | var bind = MochiKit.Base.bind; | ||
39 | var clone = MochiKit.Base.clone; | ||
40 | var win = window; | ||
41 | var uid = "_MochiKit_LoggingPane"; | ||
42 | if (typeof(MochiKit.DOM) != "undefined") { | ||
43 | win = MochiKit.DOM.currentWindow(); | ||
44 | } | ||
45 | if (!inline) { | ||
46 | // name the popup with the base URL for uniqueness | ||
47 | var url = win.location.href.split("?")[0].replace(/[#:\/.><&%-]/g, "_"); | ||
48 | var name = uid + "_" + url; | ||
49 | var nwin = win.open("", name, "dependent,resizable,height=200"); | ||
50 | if (!nwin) { | ||
51 | alert("Not able to open debugging window due to pop-up blocking."); | ||
52 | return undefined; | ||
53 | } | ||
54 | nwin.document.write( | ||
55 | '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ' | ||
56 | + '"http://www.w3.org/TR/html4/loose.dtd">' | ||
57 | + '<html><head><title>[MochiKit.LoggingPane]</title></head>' | ||
58 | + '<body></body></html>' | ||
59 | ); | ||
60 | nwin.document.close(); | ||
61 | nwin.document.title += ' ' + win.document.title; | ||
62 | win = nwin; | ||
63 | } | ||
64 | var doc = win.document; | ||
65 | this.doc = doc; | ||
66 | |||
67 | // Connect to the debug pane if it already exists (i.e. in a window orphaned by the page being refreshed) | ||
68 | var debugPane = doc.getElementById(uid); | ||
69 | var existing_pane = !!debugPane; | ||
70 | if (debugPane && typeof(debugPane.loggingPane) != "undefined") { | ||
71 | debugPane.loggingPane.logger = this.logger; | ||
72 | debugPane.loggingPane.buildAndApplyFilter(); | ||
73 | return debugPane.loggingPane; | ||
74 | } | ||
75 | |||
76 | if (existing_pane) { | ||
77 | // clear any existing contents | ||
78 | var child; | ||
79 | while ((child = debugPane.firstChild)) { | ||
80 | debugPane.removeChild(child); | ||
81 | } | ||
82 | } else { | ||
83 | debugPane = doc.createElement("div"); | ||
84 | debugPane.id = uid; | ||
85 | } | ||
86 | debugPane.loggingPane = this; | ||
87 | var levelFilterField = doc.createElement("input"); | ||
88 | var infoFilterField = doc.createElement("input"); | ||
89 | var filterButton = doc.createElement("button"); | ||
90 | var loadButton = doc.createElement("button"); | ||
91 | var clearButton = doc.createElement("button"); | ||
92 | var closeButton = doc.createElement("button"); | ||
93 | var logPaneArea = doc.createElement("div"); | ||
94 | var logPane = doc.createElement("div"); | ||
95 | |||
96 | /* Set up the functions */ | ||
97 | var listenerId = uid + "_Listener"; | ||
98 | this.colorTable = clone(this.colorTable); | ||
99 | var messages = []; | ||
100 | var messageFilter = null; | ||
101 | |||
102 | /** @id MochiKit.LoggingPane.messageLevel */ | ||
103 | var messageLevel = function (msg) { | ||
104 | var level = msg.level; | ||
105 | if (typeof(level) == "number") { | ||
106 | level = MochiKit.Logging.LogLevel[level]; | ||
107 | } | ||
108 | return level; | ||
109 | }; | ||
110 | |||
111 | /** @id MochiKit.LoggingPane.messageText */ | ||
112 | var messageText = function (msg) { | ||
113 | return msg.info.join(" "); | ||
114 | }; | ||
115 | |||
116 | /** @id MochiKit.LoggingPane.addMessageText */ | ||
117 | var addMessageText = bind(function (msg) { | ||
118 | var level = messageLevel(msg); | ||
119 | var text = messageText(msg); | ||
120 | var c = this.colorTable[level]; | ||
121 | var p = doc.createElement("span"); | ||
122 | p.className = "MochiKit-LogMessage MochiKit-LogLevel-" + level; | ||
123 | 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; | ||
124 | p.appendChild(doc.createTextNode(level + ": " + text)); | ||
125 | logPane.appendChild(p); | ||
126 | logPane.appendChild(doc.createElement("br")); | ||
127 | if (logPaneArea.offsetHeight > logPaneArea.scrollHeight) { | ||
128 | logPaneArea.scrollTop = 0; | ||
129 | } else { | ||
130 | logPaneArea.scrollTop = logPaneArea.scrollHeight; | ||
131 | } | ||
132 | }, this); | ||
133 | |||
134 | /** @id MochiKit.LoggingPane.addMessage */ | ||
135 | var addMessage = function (msg) { | ||
136 | messages[messages.length] = msg; | ||
137 | addMessageText(msg); | ||
138 | }; | ||
139 | |||
140 | /** @id MochiKit.LoggingPane.buildMessageFilter */ | ||
141 | var buildMessageFilter = function () { | ||
142 | var levelre, infore; | ||
143 | try { | ||
144 | /* Catch any exceptions that might arise due to invalid regexes */ | ||
145 | levelre = new RegExp(levelFilterField.value); | ||
146 | infore = new RegExp(infoFilterField.value); | ||
147 | } catch(e) { | ||
148 | /* If there was an error with the regexes, do no filtering */ | ||
149 | logDebug("Error in filter regex: " + e.message); | ||
150 | return null; | ||
151 | } | ||
152 | |||
153 | return function (msg) { | ||
154 | return ( | ||
155 | levelre.test(messageLevel(msg)) && | ||
156 | infore.test(messageText(msg)) | ||
157 | ); | ||
158 | }; | ||
159 | }; | ||
160 | |||
161 | /** @id MochiKit.LoggingPane.clearMessagePane */ | ||
162 | var clearMessagePane = function () { | ||
163 | while (logPane.firstChild) { | ||
164 | logPane.removeChild(logPane.firstChild); | ||
165 | } | ||
166 | }; | ||
167 | |||
168 | /** @id MochiKit.LoggingPane.clearMessages */ | ||
169 | var clearMessages = function () { | ||
170 | messages = []; | ||
171 | clearMessagePane(); | ||
172 | }; | ||
173 | |||
174 | /** @id MochiKit.LoggingPane.closePane */ | ||
175 | var closePane = bind(function () { | ||
176 | if (this.closed) { | ||
177 | return; | ||
178 | } | ||
179 | this.closed = true; | ||
180 | if (MochiKit.LoggingPane._loggingPane == this) { | ||
181 | MochiKit.LoggingPane._loggingPane = null; | ||
182 | } | ||
183 | this.logger.removeListener(listenerId); | ||
184 | try { | ||
185 | try { | ||
186 | debugPane.loggingPane = null; | ||
187 | } catch(e) { logFatal("Bookmarklet was closed incorrectly."); } | ||
188 | if (inline) { | ||
189 | debugPane.parentNode.removeChild(debugPane); | ||
190 | } else { | ||
191 | this.win.close(); | ||
192 | } | ||
193 | } catch(e) {} | ||
194 | }, this); | ||
195 | |||
196 | /** @id MochiKit.LoggingPane.filterMessages */ | ||
197 | var filterMessages = function () { | ||
198 | clearMessagePane(); | ||
199 | |||
200 | for (var i = 0; i < messages.length; i++) { | ||
201 | var msg = messages[i]; | ||
202 | if (messageFilter === null || messageFilter(msg)) { | ||
203 | addMessageText(msg); | ||
204 | } | ||
205 | } | ||
206 | }; | ||
207 | |||
208 | this.buildAndApplyFilter = function () { | ||
209 | messageFilter = buildMessageFilter(); | ||
210 | |||
211 | filterMessages(); | ||
212 | |||
213 | this.logger.removeListener(listenerId); | ||
214 | this.logger.addListener(listenerId, messageFilter, addMessage); | ||
215 | }; | ||
216 | |||
217 | |||
218 | /** @id MochiKit.LoggingPane.loadMessages */ | ||
219 | var loadMessages = bind(function () { | ||
220 | messages = this.logger.getMessages(); | ||
221 | filterMessages(); | ||
222 | }, this); | ||
223 | |||
224 | /** @id MochiKit.LoggingPane.filterOnEnter */ | ||
225 | var filterOnEnter = bind(function (event) { | ||
226 | event = event || window.event; | ||
227 | key = event.which || event.keyCode; | ||
228 | if (key == 13) { | ||
229 | this.buildAndApplyFilter(); | ||
230 | } | ||
231 | }, this); | ||
232 | |||
233 | /* Create the debug pane */ | ||
234 | var style = "display: block; z-index: 1000; left: 0px; bottom: 0px; position: fixed; width: 100%; background-color: white; font: " + this.logFont; | ||
235 | if (inline) { | ||
236 | style += "; height: 10em; border-top: 2px solid black"; | ||
237 | } else { | ||
238 | style += "; height: 100%;"; | ||
239 | } | ||
240 | debugPane.style.cssText = style; | ||
241 | |||
242 | if (!existing_pane) { | ||
243 | doc.body.appendChild(debugPane); | ||
244 | } | ||
245 | |||
246 | /* Create the filter fields */ | ||
247 | style = {"cssText": "width: 33%; display: inline; font: " + this.logFont}; | ||
248 | |||
249 | updatetree(levelFilterField, { | ||
250 | "value": "FATAL|ERROR|WARNING|INFO|DEBUG", | ||
251 | "onkeypress": filterOnEnter, | ||
252 | "style": style | ||
253 | }); | ||
254 | debugPane.appendChild(levelFilterField); | ||
255 | |||
256 | updatetree(infoFilterField, { | ||
257 | "value": ".*", | ||
258 | "onkeypress": filterOnEnter, | ||
259 | "style": style | ||
260 | }); | ||
261 | debugPane.appendChild(infoFilterField); | ||
262 | |||
263 | /* Create the buttons */ | ||
264 | style = "width: 8%; display:inline; font: " + this.logFont; | ||
265 | |||
266 | filterButton.appendChild(doc.createTextNode("Filter")); | ||
267 | filterButton.onclick = bind("buildAndApplyFilter", this); | ||
268 | filterButton.style.cssText = style; | ||
269 | debugPane.appendChild(filterButton); | ||
270 | |||
271 | loadButton.appendChild(doc.createTextNode("Load")); | ||
272 | loadButton.onclick = loadMessages; | ||
273 | loadButton.style.cssText = style; | ||
274 | debugPane.appendChild(loadButton); | ||
275 | |||
276 | clearButton.appendChild(doc.createTextNode("Clear")); | ||
277 | clearButton.onclick = clearMessages; | ||
278 | clearButton.style.cssText = style; | ||
279 | debugPane.appendChild(clearButton); | ||
280 | |||
281 | closeButton.appendChild(doc.createTextNode("Close")); | ||
282 | closeButton.onclick = closePane; | ||
283 | closeButton.style.cssText = style; | ||
284 | debugPane.appendChild(closeButton); | ||
285 | |||
286 | /* Create the logging pane */ | ||
287 | logPaneArea.style.cssText = "overflow: auto; width: 100%"; | ||
288 | logPane.style.cssText = "width: 100%; height: " + (inline ? "8em" : "100%"); | ||
289 | |||
290 | logPaneArea.appendChild(logPane); | ||
291 | debugPane.appendChild(logPaneArea); | ||
292 | |||
293 | this.buildAndApplyFilter(); | ||
294 | loadMessages(); | ||
295 | |||
296 | if (inline) { | ||
297 | this.win = undefined; | ||
298 | } else { | ||
299 | this.win = win; | ||
300 | } | ||
301 | this.inline = inline; | ||
302 | this.closePane = closePane; | ||
303 | this.closed = false; | ||
304 | |||
305 | |||
306 | return this; | ||
307 | }; | ||
308 | |||
309 | MochiKit.LoggingPane.LoggingPane.prototype = { | ||
310 | "logFont": "8pt Verdana,sans-serif", | ||
311 | "colorTable": { | ||
312 | "ERROR": "red", | ||
313 | "FATAL": "darkred", | ||
314 | "WARNING": "blue", | ||
315 | "INFO": "black", | ||
316 | "DEBUG": "green" | ||
317 | } | ||
318 | }; | ||
319 | |||
320 | MochiKit.LoggingPane.__new__ = function () { | ||
321 | MochiKit.Base.nameFunctions(this); | ||
322 | MochiKit.LoggingPane._loggingPane = null; | ||
323 | }; | ||
324 | |||
325 | MochiKit.LoggingPane.__new__(); | ||
326 | |||
327 | MochiKit.Base._exportSymbols(this, MochiKit.LoggingPane); | ||