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/beta/js/Clipperz/Profile.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/beta/js/Clipperz/Profile.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Profile.js | 485 |
1 files changed, 485 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/Profile.js b/frontend/beta/js/Clipperz/Profile.js new file mode 100644 index 0000000..31888a9 --- a/dev/null +++ b/frontend/beta/js/Clipperz/Profile.js | |||
@@ -0,0 +1,485 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | addEvent(window, "load", sortables_init); | ||
30 | |||
31 | var SORT_COLUMN_INDEX; | ||
32 | |||
33 | function sortables_init() { | ||
34 | // Find all tables with class sortable and make them sortable | ||
35 | if (!document.getElementsByTagName) return; | ||
36 | tbls = document.getElementsByTagName("table"); | ||
37 | for (ti=0;ti<tbls.length;ti++) { | ||
38 | thisTbl = tbls[ti]; | ||
39 | if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) { | ||
40 | //initTable(thisTbl.id); | ||
41 | ts_makeSortable(thisTbl); | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | function ts_makeSortable(table) { | ||
47 | if (table.rows && table.rows.length > 0) { | ||
48 | var firstRow = table.rows[0]; | ||
49 | } | ||
50 | if (!firstRow) return; | ||
51 | |||
52 | // We have a first row: assume it's the header, and make its contents clickable links | ||
53 | for (var i=0;i<firstRow.cells.length;i++) { | ||
54 | var cell = firstRow.cells[i]; | ||
55 | var txt = ts_getInnerText(cell); | ||
56 | cell.innerHTML = '<a href="#" class="sortheader" '+ | ||
57 | 'onclick="ts_resortTable(this, '+i+');return false;">' + | ||
58 | txt+'<span class="sortarrow"> </span></a>'; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | function ts_getInnerText(el) { | ||
63 | if (typeof el == "string") return el; | ||
64 | if (typeof el == "undefined") { return el }; | ||
65 | if (el.innerText) return el.innerText;//Not needed but it is faster | ||
66 | var str = ""; | ||
67 | |||
68 | var cs = el.childNodes; | ||
69 | var l = cs.length; | ||
70 | for (var i = 0; i < l; i++) { | ||
71 | switch (cs[i].nodeType) { | ||
72 | case 1: //ELEMENT_NODE | ||
73 | str += ts_getInnerText(cs[i]); | ||
74 | break; | ||
75 | case 3://TEXT_NODE | ||
76 | str += cs[i].nodeValue; | ||
77 | break; | ||
78 | } | ||
79 | } | ||
80 | return str; | ||
81 | } | ||
82 | |||
83 | function ts_resortTable(lnk,clid) { | ||
84 | // get the span | ||
85 | var span; | ||
86 | for (var ci=0;ci<lnk.childNodes.length;ci++) { | ||
87 | if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci]; | ||
88 | } | ||
89 | var spantext = ts_getInnerText(span); | ||
90 | var td = lnk.parentNode; | ||
91 | var column = clid || td.cellIndex; | ||
92 | var table = getParent(td,'TABLE'); | ||
93 | |||
94 | // Work out a type for the column | ||
95 | if (table.rows.length <= 1) return; | ||
96 | var itm = ts_getInnerText(table.rows[1].cells[column]); | ||
97 | sortfn = ts_sort_caseinsensitive; | ||
98 | if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) { | ||
99 | sortfn = ts_sort_date; | ||
100 | } | ||
101 | if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) { | ||
102 | sortfn = ts_sort_date; | ||
103 | } | ||
104 | if (itm.match(/^[£$]/)) { | ||
105 | sortfn = ts_sort_currency; | ||
106 | } | ||
107 | if (itm.match(/^[\d\.]+$/)) { | ||
108 | sortfn = ts_sort_numeric; | ||
109 | } | ||
110 | SORT_COLUMN_INDEX = column; | ||
111 | var firstRow = new Array(); | ||
112 | var newRows = new Array(); | ||
113 | for (i=0;i<table.rows[0].length;i++) { | ||
114 | firstRow[i] = table.rows[0][i]; | ||
115 | } | ||
116 | |||
117 | for (j=1;j<table.rows.length;j++) { | ||
118 | newRows[j-1] = table.rows[j]; | ||
119 | } | ||
120 | newRows.sort(sortfn); | ||
121 | |||
122 | if (span.getAttribute("sortdir") == 'down') { | ||
123 | ARROW = ' ↑'; | ||
124 | newRows.reverse(); | ||
125 | span.setAttribute('sortdir','up'); | ||
126 | } else { | ||
127 | ARROW = ' ↓'; | ||
128 | span.setAttribute('sortdir','down'); | ||
129 | } | ||
130 | |||
131 | // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones | ||
132 | // don't do sortbottom rows | ||
133 | for (i=0;i<newRows.length;i++) { | ||
134 | if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) { | ||
135 | table.tBodies[0].appendChild(newRows[i]); | ||
136 | } | ||
137 | } | ||
138 | // do sortbottom rows only | ||
139 | for (i=0;i<newRows.length;i++) { | ||
140 | if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1)) { | ||
141 | table.tBodies[0].appendChild(newRows[i]); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | // Delete any other arrows there may be showing | ||
146 | var allspans = document.getElementsByTagName("span"); | ||
147 | for (var ci=0;ci<allspans.length;ci++) { | ||
148 | if (allspans[ci].className == 'sortarrow') { | ||
149 | if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us? | ||
150 | allspans[ci].innerHTML = ' '; | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | |||
155 | span.innerHTML = ARROW; | ||
156 | } | ||
157 | |||
158 | function getParent(el, pTagName) { | ||
159 | if (el == null) return null; | ||
160 | else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())// Gecko bug, supposed to be uppercase | ||
161 | return el; | ||
162 | else | ||
163 | return getParent(el.parentNode, pTagName); | ||
164 | } | ||
165 | function ts_sort_date(a,b) { | ||
166 | // y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX | ||
167 | aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); | ||
168 | bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]); | ||
169 | if (aa.length == 10) { | ||
170 | dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2); | ||
171 | } else { | ||
172 | yr = aa.substr(6,2); | ||
173 | if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; } | ||
174 | dt1 = yr+aa.substr(3,2)+aa.substr(0,2); | ||
175 | } | ||
176 | if (bb.length == 10) { | ||
177 | dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2); | ||
178 | } else { | ||
179 | yr = bb.substr(6,2); | ||
180 | if (parseInt(yr) < 50) { yr = '20'+yr; } else { yr = '19'+yr; } | ||
181 | dt2 = yr+bb.substr(3,2)+bb.substr(0,2); | ||
182 | } | ||
183 | if (dt1==dt2) return 0; | ||
184 | if (dt1<dt2) return -1; | ||
185 | return 1; | ||
186 | } | ||
187 | |||
188 | function ts_sort_currency(a,b) { | ||
189 | aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''); | ||
190 | bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g,''); | ||
191 | return parseFloat(aa) - parseFloat(bb); | ||
192 | } | ||
193 | |||
194 | function ts_sort_numeric(a,b) { | ||
195 | aa = parseFloat(ts_getInnerText(a.cells[SORT_COLUMN_INDEX])); | ||
196 | if (isNaN(aa)) aa = 0; | ||
197 | bb = parseFloat(ts_getInnerText(b.cells[SORT_COLUMN_INDEX])); | ||
198 | if (isNaN(bb)) bb = 0; | ||
199 | return aa-bb; | ||
200 | } | ||
201 | |||
202 | function ts_sort_caseinsensitive(a,b) { | ||
203 | aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase(); | ||
204 | bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase(); | ||
205 | if (aa==bb) return 0; | ||
206 | if (aa<bb) return -1; | ||
207 | return 1; | ||
208 | } | ||
209 | |||
210 | function ts_sort_default(a,b) { | ||
211 | aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]); | ||
212 | bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]); | ||
213 | if (aa==bb) return 0; | ||
214 | if (aa<bb) return -1; | ||
215 | return 1; | ||
216 | } | ||
217 | |||
218 | |||
219 | function addEvent(elm, evType, fn, useCapture) | ||
220 | // addEvent and removeEvent | ||
221 | // cross-browser event handling for IE5+, NS6 and Mozilla | ||
222 | // By Scott Andrew | ||
223 | { | ||
224 | if (elm.addEventListener){ | ||
225 | elm.addEventListener(evType, fn, useCapture); | ||
226 | return true; | ||
227 | } else if (elm.attachEvent){ | ||
228 | var r = elm.attachEvent("on"+evType, fn); | ||
229 | return r; | ||
230 | } else { | ||
231 | alert("Handler could not be removed"); | ||
232 | } | ||
233 | } | ||
234 | |||
235 | |||
236 | |||
237 | |||
238 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
239 | if (typeof(Clipperz.Profile) == 'undefined') { Clipperz.Profile = {}; } | ||
240 | |||
241 | Clipperz.Profile.VERSION = "0.1"; | ||
242 | Clipperz.Profile.NAME = "Clipperz.Profile"; | ||
243 | |||
244 | MochiKit.Base.update(Clipperz.Profile, { | ||
245 | |||
246 | //------------------------------------------------------------------------- | ||
247 | |||
248 | '__repr__': function () { | ||
249 | varstatus; | ||
250 | |||
251 | if (Clipperz.Profile.isEnabled == true) { | ||
252 | status = ENABLED; | ||
253 | } else { | ||
254 | status = DISABLED; | ||
255 | } | ||
256 | |||
257 | return "[" + this.NAME + " " + this.VERSION + " - " + status + "]"; | ||
258 | }, | ||
259 | |||
260 | //------------------------------------------------------------------------- | ||
261 | |||
262 | 'toString': function () { | ||
263 | return this.__repr__(); | ||
264 | }, | ||
265 | |||
266 | //------------------------------------------------------------------------- | ||
267 | |||
268 | 'isEnabled': function() { | ||
269 | return false; | ||
270 | }, | ||
271 | |||
272 | //------------------------------------------------------------------------- | ||
273 | |||
274 | 'initialValues': function() { | ||
275 | return {iters:0, total:0, min:Number.MAX_VALUE, max:0} | ||
276 | }, | ||
277 | |||
278 | //------------------------------------------------------------------------- | ||
279 | |||
280 | 'start': function(aName) {}, | ||
281 | 'stop': function(aName) {}, | ||
282 | 'dump': function(aName) {}, | ||
283 | 'profileData': function(aName, aKey) { | ||
284 | varresult; | ||
285 | |||
286 | if (typeof(aName) != 'undefined') { | ||
287 | result = this.initialValues(); | ||
288 | |||
289 | if (typeof(aKey) != 'undefined') { | ||
290 | result = result[aKey]; | ||
291 | } | ||
292 | } else { | ||
293 | result = null; | ||
294 | } | ||
295 | |||
296 | return result; | ||
297 | |||
298 | }, | ||
299 | 'resetProfileData': function() {}, | ||
300 | //------------------------------------------------------------------------- | ||
301 | |||
302 | 'end': function(aName) { | ||
303 | Clipperz.Profile.stop(aName); | ||
304 | }, | ||
305 | |||
306 | //------------------------------------------------------------------------- | ||
307 | |||
308 | __syntaxFix__: "syntax fix" | ||
309 | }); | ||
310 | |||
311 | |||
312 | |||
313 | if ((typeof(clipperz_profiling_enabled) != 'undefined') && (clipperz_profiling_enabled == true)) { | ||
314 | |||
315 | var _clipperz_profile_profiles = {}; | ||
316 | var _clipperz_profile_pns = []; | ||
317 | |||
318 | |||
319 | MochiKit.Base.update(Clipperz.Profile, { | ||
320 | |||
321 | //------------------------------------------------------------------------- | ||
322 | |||
323 | 'isEnabled': function() { | ||
324 | return true; | ||
325 | }, | ||
326 | |||
327 | //------------------------------------------------------------------------- | ||
328 | |||
329 | 'start': function(aName) { | ||
330 | if (!_clipperz_profile_profiles[aName]) { | ||
331 | _clipperz_profile_profiles[aName] = this.initialValues(); | ||
332 | _clipperz_profile_pns[_clipperz_profile_pns.length] = aName; | ||
333 | } else { | ||
334 | if (_clipperz_profile_profiles[aName]["start"]) { | ||
335 | Clipperz.Profile.stop(aName); | ||
336 | } | ||
337 | } | ||
338 | |||
339 | _clipperz_profile_profiles[aName].end = null; | ||
340 | _clipperz_profile_profiles[aName].start = new Date(); | ||
341 | }, | ||
342 | |||
343 | //------------------------------------------------------------------------- | ||
344 | |||
345 | 'stop': function(aName) { | ||
346 | if ((_clipperz_profile_profiles[aName]) && (_clipperz_profile_profiles[aName]["start"])) { | ||
347 | with(_clipperz_profile_profiles[aName]) { | ||
348 | var now; | ||
349 | varelapsedTime; | ||
350 | |||
351 | now = new Date(); | ||
352 | elapsedTime = (now - start); | ||
353 | |||
354 | end = now; | ||
355 | min = Math.min(min, elapsedTime); | ||
356 | max = Math.max(max, elapsedTime); | ||
357 | total += elapsedTime; | ||
358 | start = null; | ||
359 | iters++; | ||
360 | } | ||
361 | } else { | ||
362 | // oops! bad call to end(), what should we do here? | ||
363 | return true; | ||
364 | } | ||
365 | }, | ||
366 | |||
367 | //------------------------------------------------------------------------- | ||
368 | |||
369 | 'dump': function(appendToDoc) { | ||
370 | // var tbl = document.createElement("table"); | ||
371 | var tbl = MochiKit.DOM.TABLE(null, MochiKit.DOM.TBODY()); | ||
372 | tbl.className = 'sortable'; | ||
373 | tbl.id = "profileOutputTable_table"; | ||
374 | with(tbl.style){ | ||
375 | border = "1px solid black"; | ||
376 | borderCollapse = "collapse"; | ||
377 | } | ||
378 | var hdr = tbl.createTHead(); | ||
379 | var hdrtr = hdr.insertRow(0); | ||
380 | // document.createElement("tr"); | ||
381 | var cols = ["Identifier","#","Min", "Avg","Max","Total"]; | ||
382 | for(var x=0; x<cols.length; x++){ | ||
383 | var ntd = hdrtr.insertCell(x); | ||
384 | with(ntd.style){ | ||
385 | backgroundColor = "#225d94"; | ||
386 | color = "white"; | ||
387 | borderBottom = "1px solid black"; | ||
388 | borderRight = "1px solid black"; | ||
389 | fontFamily = "tahoma"; | ||
390 | fontWeight = "bolder"; | ||
391 | paddingLeft = paddingRight = "5px"; | ||
392 | } | ||
393 | ntd.appendChild(document.createTextNode(cols[x])); | ||
394 | } | ||
395 | |||
396 | for(var x=0; x < _clipperz_profile_pns.length; x++){ | ||
397 | var prf = _clipperz_profile_profiles[_clipperz_profile_pns[x]]; | ||
398 | this.end(_clipperz_profile_pns[x]); | ||
399 | if(prf.iters>0){ | ||
400 | var bdytr = tbl.insertRow(true); | ||
401 | var vals = [_clipperz_profile_pns[x], prf.iters, prf.min, parseInt(Math.round(prf.total/prf.iters)), prf.max, prf.total]; | ||
402 | for(var y=0; y<vals.length; y++){ | ||
403 | var cc = bdytr.insertCell(y); | ||
404 | cc.appendChild(document.createTextNode(vals[y])); | ||
405 | with(cc.style){ | ||
406 | borderBottom = "1px solid gray"; | ||
407 | paddingLeft = paddingRight = "5px"; | ||
408 | if(x%2){ | ||
409 | backgroundColor = "#e1f1ff"; | ||
410 | } | ||
411 | if(y>0){ | ||
412 | textAlign = "right"; | ||
413 | borderRight = "1px solid gray"; | ||
414 | }else{ | ||
415 | borderRight = "1px solid black"; | ||
416 | } | ||
417 | } | ||
418 | } | ||
419 | } | ||
420 | } | ||
421 | |||
422 | if(appendToDoc){ | ||
423 | var ne = document.createElement("div"); | ||
424 | ne.id = "profileOutputTable"; | ||
425 | with(ne.style){ | ||
426 | fontFamily = "Courier New, monospace"; | ||
427 | fontSize = "12px"; | ||
428 | lineHeight = "16px"; | ||
429 | borderTop = "1px solid black"; | ||
430 | padding = "10px"; | ||
431 | } | ||
432 | if(document.getElementById("profileOutputTable")){ | ||
433 | MochiKit.DOM.swapDOM("profileOutputTable", ne); | ||
434 | }else{ | ||
435 | document.body.appendChild(ne); | ||
436 | } | ||
437 | ne.appendChild(tbl); | ||
438 | } | ||
439 | |||
440 | return tbl; | ||
441 | }, | ||
442 | |||
443 | //------------------------------------------------------------------------- | ||
444 | |||
445 | 'profileData': function(aName, aKey) { | ||
446 | varresult; | ||
447 | |||
448 | if (typeof(aName) == 'undefined') { | ||
449 | result = _clipperz_profile_profiles; | ||
450 | } else { | ||
451 | if (typeof(_clipperz_profile_profiles[aName]) != 'undefined') { | ||
452 | result = _clipperz_profile_profiles[aName]; | ||
453 | } else { | ||
454 | result = {}; | ||
455 | } | ||
456 | } | ||
457 | |||
458 | if (typeof(aKey) != 'undefined') { | ||
459 | if (aKey == "average") { | ||
460 | result = Math.round(Clipperz.Profile.profileData(aName, 'total')/Clipperz.Profile.profileData(aName, 'iters')); | ||
461 | } else { | ||
462 | if (typeof(result[aKey]) != 'undefined') { | ||
463 | result = result[aKey]; | ||
464 | } else { | ||
465 | result = 0; | ||
466 | } | ||
467 | } | ||
468 | } | ||
469 | |||
470 | return result; | ||
471 | }, | ||
472 | |||
473 | //------------------------------------------------------------------------- | ||
474 | |||
475 | 'resetProfileData': function() { | ||
476 | _clipperz_profile_profiles = {}; | ||
477 | _clipperz_profile_pns = []; | ||
478 | }, | ||
479 | |||
480 | //------------------------------------------------------------------------- | ||
481 | |||
482 | __syntaxFix__: "syntax fix" | ||
483 | }); | ||
484 | |||
485 | } | ||