summaryrefslogtreecommitdiff
path: root/frontend/delta/js/Clipperz/Base.js
Unidiff
Diffstat (limited to 'frontend/delta/js/Clipperz/Base.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/Clipperz/Base.js514
1 files changed, 514 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/Base.js b/frontend/delta/js/Clipperz/Base.js
new file mode 100644
index 0000000..84b2172
--- a/dev/null
+++ b/frontend/delta/js/Clipperz/Base.js
@@ -0,0 +1,514 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer 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
24if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
25if (typeof(Clipperz.Base) == 'undefined') { Clipperz.Base = {}; }
26
27Clipperz.Base.VERSION = "0.2";
28Clipperz.Base.NAME = "Clipperz.Base";
29
30MochiKit.Base.update(Clipperz.Base, {
31
32 //-------------------------------------------------------------------------
33
34 '__repr__': function () {
35 return "[" + this.NAME + " " + this.VERSION + "]";
36 },
37
38 //-------------------------------------------------------------------------
39
40 'toString': function () {
41 return this.__repr__();
42 },
43
44 //-------------------------------------------------------------------------
45
46 'itemgetter': function (aKeyPath) {
47 // return MochiKit.Base.compose.apply(null, [MochiKit.Base.itemgetter('key3')]);
48 return MochiKit.Base.compose.apply(null,
49 MochiKit.Base.map(
50 MochiKit.Base.itemgetter,
51 MochiKit.Iter.reversed(
52 aKeyPath.split('.')
53 )
54 )
55 );
56 },
57
58 //-------------------------------------------------------------------------
59
60 'isUrl': function (aValue) {
61 return (MochiKit.Base.urlRegExp.test(aValue));
62 },
63
64 'isEmail': function (aValue) {
65 return (MochiKit.Base.emailRegExp.test(aValue));
66 },
67
68 //-------------------------------------------------------------------------
69
70 'caseInsensitiveCompare': function (a, b) {
71 return MochiKit.Base.compare(a.toLowerCase(), b.toLowerCase());
72 },
73
74 'reverseComparator': function (aComparator) {
75 return MochiKit.Base.compose(function(aResult) { return -aResult; }, aComparator);
76 },
77
78 'caseInsensitiveKeyComparator': function (aKey) {
79 return function (a, b) {
80 return MochiKit.Base.compare(a[aKey].toLowerCase(), b[aKey].toLowerCase());
81 }
82 },
83 //-------------------------------------------------------------------------
84/*
85 'dependsOn': function(module, deps) {
86 if (!(module in Clipperz)) {
87 MochiKit[module] = {};
88 }
89
90 if (typeof(dojo) != 'undefined') {
91 dojo.provide('Clipperz.' + module);
92 }
93 for (var i = 0; i < deps.length; i++) {
94 if (typeof(dojo) != 'undefined') {
95 dojo.require('Clipperz.' + deps[i]);
96 }
97 if (typeof(JSAN) != 'undefined') {
98 JSAN.use('Clipperz.' + deps[i], []);
99 }
100 if (!(deps[i] in Clipperz)) {
101 throw 'Clipperz.' + module + ' depends on Clipperz.' + deps[i] + '!'
102 }
103 }
104 },
105*/
106 //-------------------------------------------------------------------------
107
108 'trim': function (aValue) {
109 return aValue.replace(/^\s+|\s+$/g, "");
110 },
111
112 //-------------------------------------------------------------------------
113
114 'stringToByteArray': function (aValue) {
115 varresult;
116 var i, c;
117
118 result = [];
119
120 c = aValue.length;
121 for (i=0; i<c; i++) {
122 result[i] = aValue.charCodeAt(i);
123 }
124
125 return result;
126 },
127
128 //.........................................................................
129
130 'byteArrayToString': function (anArrayOfBytes) {
131 varresult;
132 var i, c;
133
134 result = "";
135
136 c = anArrayOfBytes.length;
137 for (i=0; i<c; i++) {
138 result += String.fromCharCode(anArrayOfBytes[i]);
139 }
140
141 return result;
142 },
143
144 //-------------------------------------------------------------------------
145
146 'getValueForKeyInFormContent': function (aFormContent, aKey) {
147 return aFormContent[1][MochiKit.Base.find(aFormContent[0], aKey)];
148 },
149
150 //-------------------------------------------------------------------------
151
152 'indexOfObjectInArray': function(anObject, anArray) {
153 varresult;
154 vari, c;
155
156 result = -1;
157
158 c = anArray.length;
159 for (i=0; ((i<c) && (result < 0)); i++) {
160 if (anArray[i] === anObject) {
161 result = i;
162 }
163 }
164
165 return result;
166 },
167
168 //-------------------------------------------------------------------------
169
170 'removeObjectAtIndexFromArray': function(anIndex, anArray) {
171 anArray.splice(anIndex, 1);
172 },
173
174 //-------------------------------------------------------------------------
175
176 'removeObjectFromArray': function(anObject, anArray) {
177 varobjectIndex;
178
179 objectIndex = Clipperz.Base.indexOfObjectInArray(anObject, anArray);
180 if (objectIndex > -1) {
181 Clipperz.Base.removeObjectAtIndexFromArray(objectIndex, anArray);
182 } else {
183 Clipperz.log("Trying to remove an object not present in the array");
184 throw Clipperz.Base.exception.ObjectNotFound;
185 }
186 },
187
188 'removeFromArray': function(anArray, anObject) {
189 return Clipperz.Base.removeObjectFromArray(anObject, anArray);
190 },
191
192 //-------------------------------------------------------------------------
193
194 'splitStringAtFixedTokenSize': function(aString, aTokenSize) {
195 var result;
196 varstringToProcess;
197
198 stringToProcess = aString;
199 result = [];
200 if (stringToProcess != null) {
201 while (stringToProcess.length > aTokenSize) {
202 result.push(stringToProcess.substring(0, aTokenSize));
203 stringToProcess = stringToProcess.substring(aTokenSize);
204 }
205
206 result.push(stringToProcess);
207 }
208
209 return result;
210 },
211
212 //-------------------------------------------------------------------------
213
214 'objectType': function(anObject) {
215 var result;
216
217 if (anObject == null) {
218 result = null;
219 } else {
220 result = typeof(anObject);
221
222 if (result == "object") {
223 if (anObject instanceof Array) {
224 result = 'array'
225 } else if (anObject.constructor == Boolean) {
226 result = 'boolean'
227 } else if (anObject instanceof Date) {
228 result = 'date'
229 } else if (anObject instanceof Error) {
230 result = 'error'
231 } else if (anObject instanceof Function) {
232 result = 'function'
233 } else if (anObject.constructor == Number) {
234 result = 'number'
235 } else if (anObject.constructor == String) {
236 result = 'string'
237 } else if (anObject instanceof Object) {
238 result = 'object'
239 } else {
240 throw Clipperz.Base.exception.UnknownType;
241 }
242 }
243 }
244
245 return result;
246 },
247
248 //-------------------------------------------------------------------------
249
250 'escapeHTML': function(aValue) {
251 var result;
252
253 result = aValue;
254 result = result.replace(/</g, "&lt;");
255 result = result.replace(/>/g, "&gt;");
256
257 return result;
258 },
259
260 //-------------------------------------------------------------------------
261
262 'deepClone': function(anObject) {
263 var result;
264
265 result = Clipperz.Base.evalJSON(Clipperz.Base.serializeJSON(anObject));
266
267 return result;
268 },
269
270 //-------------------------------------------------------------------------
271
272 //'deepCompare': function (aObject, bObject) {
273 // return (Clipperz.Base.serializeJSON(aObject) == Clipperz.Base.serializeJSON(bObject));
274 //},
275
276 //-------------------------------------------------------------------------
277
278 'evalJSON': function(aString) {
279 return JSON.parse(aString);
280 },
281
282 'serializeJSON': function(anObject) {
283 return JSON.stringify(anObject);
284 },
285
286 'formatJSON': function (anObject, sIndent) {
287 var realTypeOf = function (v) {
288 if (typeof(v) == "object") {
289 if (v === null) return "null";
290 if (v.constructor == (new Array).constructor) return "array";
291 if (v.constructor == (new Date).constructor) return "date";
292 if (v.constructor == (new RegExp).constructor) return "regex";
293 return "object";
294 }
295 return typeof(v);
296 };
297
298 //function FormatJSON(oData, sIndent) {
299 if (arguments.length < 2) {
300 var sIndent = "";
301 }
302 // var sIndentStyle = " ";
303 var sIndentStyle = " ";
304 var sDataType = realTypeOf(anObject);
305
306 // open object
307 if (sDataType == "array") {
308 if (anObject.length == 0) {
309 return "[]";
310 }
311 var sHTML = "[";
312 } else if (sDataType == "object") {
313 var sHTML = "{";
314 } else {
315 return "{}";
316 }
317 // } else {
318 // var iCount = 0;
319 // $.each(anObject, function() {
320 // iCount++;
321 // return;
322 // });
323 // if (iCount == 0) { // object is empty
324 // return "{}";
325 // }
326 // var sHTML = "{";
327 // }
328
329 // loop through items
330 var iCount = 0;
331 // $.each(anObject, function(sKey, vValue) {
332 MochiKit.Iter.forEach(MochiKit.Base.keys(anObject), function(sKey) {
333 var vValue = anObject[sKey];
334
335 if (iCount > 0) {
336 sHTML += ",";
337 }
338 if (sDataType == "array") {
339 sHTML += ("\n" + sIndent + sIndentStyle);
340 } else {
341 sHTML += ("\n" + sIndent + sIndentStyle + "\"" + sKey + "\"" + ": ");
342 }
343
344 // display relevant data type
345 switch (realTypeOf(vValue)) {
346 case "array":
347 case "object":
348 sHTML += Clipperz.Base.formatJSON(vValue, (sIndent + sIndentStyle));
349 break;
350 case "boolean":
351 case "number":
352 sHTML += vValue.toString();
353 break;
354 case "null":
355 sHTML += "null";
356 break;
357 case "string":
358 sHTML += ("\"" + vValue + "\"");
359 break;
360 default:
361 sHTML += ("TYPEOF: " + typeof(vValue));
362 }
363
364 // loop
365 iCount++;
366 });
367
368 // close object
369 if (sDataType == "array") {
370 sHTML += ("\n" + sIndent + "]");
371 } else {
372 sHTML += ("\n" + sIndent + "}");
373 }
374
375 // return
376 return sHTML;
377 },
378
379 //-------------------------------------------------------------------------
380
381 'mergeItems': function (anArrayOfValues) {
382 var result;
383 var i, c;
384
385 result = {};
386
387 c = anArrayOfValues.length;
388 for (i=0; i<c; i++) {
389 result[anArrayOfValues[i][0]] = anArrayOfValues[i][1];
390 }
391
392 return result;
393 },
394
395 //-------------------------------------------------------------------------
396
397 'map': function (fn, lstObj/*, lst... */) {
398 var result;
399
400 if (MochiKit.Base.isArrayLike(lstObj)) {
401 result = MochiKit.Base.map.apply(this, arguments);
402 } else {
403 varkeys;
404 var values;
405 var computedValues;
406
407 keys = MochiKit.Base.keys(lstObj);
408 values = MochiKit.Base.values(lstObj);
409 computedValues = MochiKit.Base.map(fn, values);
410
411 result = Clipperz.Base.mergeItems(MochiKit.Base.zip(keys, computedValues));
412 }
413
414 return result;
415 },
416
417 //-------------------------------------------------------------------------
418
419 'sanitizeString': function(aValue) {
420 var result;
421
422 if (Clipperz.Base.objectType(aValue) == 'string') {
423 result = aValue;
424 result = result.replace(/</img,"&lt;");
425 result = result.replace(/>/img,"&gt;");
426 } else {
427 result = aValue;
428 }
429
430 return result;
431 },
432
433 //-------------------------------------------------------------------------
434
435 'module': function(aValue) {
436 // aValue = 'Clipperz.PM.Compact'
437//
438 // if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
439 // if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; }
440 // if (typeof(Clipperz.PM.UI.Common.Components) == 'undefined') { Clipperz.PM.UI.Common.Components = {}; }
441
442 var currentScope;
443 var pathElements;
444 var i,c;
445
446 currentScope = window;
447 pathElements = aValue.split('.');
448 c = pathElements.length;
449 for (i=0; i<c; i++) {
450 if (typeof(currentScope[pathElements[i]]) == 'undefined') {
451 currentScope[pathElements[i]] = {};
452 }
453
454 currentScope = currentScope[pathElements[i]];
455 }
456 },
457
458 //-------------------------------------------------------------------------
459
460 'exception': {
461 'AbstractMethod': new MochiKit.Base.NamedError("Clipperz.Base.exception.AbstractMethod"),
462 'UnknownType': new MochiKit.Base.NamedError("Clipperz.Base.exception.UnknownType"),
463 'VulnerabilityIssue':new MochiKit.Base.NamedError("Clipperz.Base.exception.VulnerabilityIssue"),
464 'MandatoryParameter':new MochiKit.Base.NamedError("Clipperz.Base.exception.MandatoryParameter"),
465 'ObjectNotFound': new MochiKit.Base.NamedError("Clipperz.Base.exception.ObjectNotFound"),
466 'raise': function (aName) {
467 throw Clipperz.Base.exception[aName];
468 }
469 },
470
471 //-------------------------------------------------------------------------
472
473 'extend': YAHOO.extendX,
474
475 //-------------------------------------------------------------------------
476 __syntaxFix__: "syntax fix"
477
478});
479
480 //Original regExp courtesy of John Gruber: http://daringfireball.net/2009/11/liberal_regex_for_matching_urls
481 //Updated to match Clipperz usage pattern.
482//MochiKit.Base.urlRegExp = new RegExp(/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/);
483MochiKit.Base.urlRegExp = new RegExp(/^((([\w-]+:\/\/?)|(www\.))[^\s()<>]+((?:\([\w\d]+\)|([^[:punct:]\s]|\/)))?)/);
484
485 //RegExp found here: http://www.tipsntracks.com/117/validate-an-email-address-using-regular-expressions.html
486MochiKit.Base.emailRegExp = new RegExp(/^([a-zA-Z0-9_\-\.]+)@(([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|25[0-5]|2[0-4]\d))$/);
487
488
489MochiKit.Base.registerComparator('Object dummy comparator',
490 function(a, b) {
491 return ((a.constructor == Object) && (b.constructor == Object));
492 },
493 function(a, b) {
494 var result;
495 var aKeys;
496 var bKeys;
497
498 aKeys = MochiKit.Base.keys(a).sort();
499 bKeys = MochiKit.Base.keys(b).sort();
500 result = MochiKit.Base.compare(aKeys, bKeys);
501
502 if (result == 0) {
503 vari, c;
504
505 c = aKeys.length;
506 for (i=0; (i<c) && (result == 0); i++) {
507 result = MochiKit.Base.compare(a[aKeys[i]], b[bKeys[i]]);
508 }
509 }
510
511 return result;
512 },
513 true
514);