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