]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/debian/missing-sources/framework/Web/Javascripts/source/tinymce-405/classes/util/Tools.js
baculum: Add missing-sources directory in debian metadata structure
[bacula/bacula] / gui / baculum / debian / missing-sources / framework / Web / Javascripts / source / tinymce-405 / classes / util / Tools.js
1 /**
2  * Tools.js
3  *
4  * Copyright, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://www.tinymce.com/license
8  * Contributing: http://www.tinymce.com/contributing
9  */
10
11 /**
12  * This class contains various utlity functions. These are also exposed
13  * directly on the tinymce namespace.
14  *
15  * @class tinymce.util.Tools
16  */
17 define("tinymce/util/Tools", [], function() {
18         /**
19          * Removes whitespace from the beginning and end of a string.
20          *
21          * @method trim
22          * @param {String} s String to remove whitespace from.
23          * @return {String} New string with removed whitespace.
24          */
25         var whiteSpaceRegExp = /^\s*|\s*$/g;
26         var trim = function(str) {
27                 return (str === null || str === undefined) ? '' : ("" + str).replace(whiteSpaceRegExp, '');
28         };
29
30         /**
31          * Returns true/false if the object is an array or not.
32          *
33          * @method isArray
34          * @param {Object} obj Object to check.
35          * @return {boolean} true/false state if the object is an array or not.
36          */
37         var isArray = Array.isArray || function(obj) {
38                 return Object.prototype.toString.call(obj) === "[object Array]";
39         };
40
41         /**
42          * Checks if a object is of a specific type for example an array.
43          *
44          * @method is
45          * @param {Object} o Object to check type of.
46          * @param {string} t Optional type to check for.
47          * @return {Boolean} true/false if the object is of the specified type.
48          */
49         function is(o, t) {
50                 if (!t) {
51                         return o !== undefined;
52                 }
53
54                 if (t == 'array' && isArray(o)) {
55                         return true;
56                 }
57
58                 return typeof(o) == t;
59         }
60
61         /**
62          * Converts the specified object into a real JavaScript array.
63          *
64          * @method toArray
65          * @param {Object} obj Object to convert into array.
66          * @return {Array} Array object based in input.
67          */
68         function toArray(obj) {
69                 var array = [], i, l;
70
71                 for (i = 0, l = obj.length; i < l; i++) {
72                         array[i] = obj[i];
73                 }
74
75                 return array;
76         }
77
78         /**
79          * Makes a name/object map out of an array with names.
80          *
81          * @method makeMap
82          * @param {Array/String} items Items to make map out of.
83          * @param {String} delim Optional delimiter to split string by.
84          * @param {Object} map Optional map to add items to.
85          * @return {Object} Name/value map of items.
86          */
87         function makeMap(items, delim, map) {
88                 var i;
89
90                 items = items || [];
91                 delim = delim || ',';
92
93                 if (typeof(items) == "string") {
94                         items = items.split(delim);
95                 }
96
97                 map = map || {};
98
99                 i = items.length;
100                 while (i--) {
101                         map[items[i]] = {};
102                 }
103
104                 return map;
105         }
106
107         /**
108          * Performs an iteration of all items in a collection such as an object or array. This method will execure the
109          * callback function for each item in the collection, if the callback returns false the iteration will terminate.
110          * The callback has the following format: cb(value, key_or_index).
111          *
112          * @method each
113          * @param {Object} o Collection to iterate.
114          * @param {function} cb Callback function to execute for each item.
115          * @param {Object} s Optional scope to execute the callback in.
116          * @example
117          * // Iterate an array
118          * tinymce.each([1,2,3], function(v, i) {
119          *     console.debug("Value: " + v + ", Index: " + i);
120          * });
121          *
122          * // Iterate an object
123          * tinymce.each({a: 1, b: 2, c: 3], function(v, k) {
124          *     console.debug("Value: " + v + ", Key: " + k);
125          * });
126          */
127         function each(o, cb, s) {
128                 var n, l;
129
130                 if (!o) {
131                         return 0;
132                 }
133
134                 s = s || o;
135
136                 if (o.length !== undefined) {
137                         // Indexed arrays, needed for Safari
138                         for (n=0, l = o.length; n < l; n++) {
139                                 if (cb.call(s, o[n], n, o) === false) {
140                                         return 0;
141                                 }
142                         }
143                 } else {
144                         // Hashtables
145                         for (n in o) {
146                                 if (o.hasOwnProperty(n)) {
147                                         if (cb.call(s, o[n], n, o) === false) {
148                                                 return 0;
149                                         }
150                                 }
151                         }
152                 }
153
154                 return 1;
155         }
156
157         /**
158          * Creates a new array by the return value of each iteration function call. This enables you to convert
159          * one array list into another.
160          *
161          * @method map
162          * @param {Array} a Array of items to iterate.
163          * @param {function} f Function to call for each item. It's return value will be the new value.
164          * @return {Array} Array with new values based on function return values.
165          */
166         function map(a, f) {
167                 var o = [];
168
169                 each(a, function(v) {
170                         o.push(f(v));
171                 });
172
173                 return o;
174         }
175
176         /**
177          * Filters out items from the input array by calling the specified function for each item.
178          * If the function returns false the item will be excluded if it returns true it will be included.
179          *
180          * @method grep
181          * @param {Array} a Array of items to loop though.
182          * @param {function} f Function to call for each item. Include/exclude depends on it's return value.
183          * @return {Array} New array with values imported and filtered based in input.
184          * @example
185          * // Filter out some items, this will return an array with 4 and 5
186          * var items = tinymce.grep([1,2,3,4,5], function(v) {return v > 3;});
187          */
188         function grep(a, f) {
189                 var o = [];
190
191                 each(a, function(v) {
192                         if (!f || f(v)) {
193                                 o.push(v);
194                         }
195                 });
196
197                 return o;
198         }
199
200         /**
201          * Creates a class, subclass or static singleton.
202          * More details on this method can be found in the Wiki.
203          *
204          * @method create
205          * @param {String} s Class name, inheritage and prefix.
206          * @param {Object} p Collection of methods to add to the class.
207          * @param {Object} root Optional root object defaults to the global window object.
208          * @example
209          * // Creates a basic class
210          * tinymce.create('tinymce.somepackage.SomeClass', {
211          *     SomeClass: function() {
212          *         // Class constructor
213          *     },
214          *
215          *     method: function() {
216          *         // Some method
217          *     }
218          * });
219          *
220          * // Creates a basic subclass class
221          * tinymce.create('tinymce.somepackage.SomeSubClass:tinymce.somepackage.SomeClass', {
222          *     SomeSubClass: function() {
223          *         // Class constructor
224          *         this.parent(); // Call parent constructor
225          *     },
226          *
227          *     method: function() {
228          *         // Some method
229          *         this.parent(); // Call parent method
230          *     },
231          *
232          *     'static': {
233          *         staticMethod: function() {
234          *             // Static method
235          *         }
236          *     }
237          * });
238          *
239          * // Creates a singleton/static class
240          * tinymce.create('static tinymce.somepackage.SomeSingletonClass', {
241          *     method: function() {
242          *         // Some method
243          *     }
244          * });
245          */
246         function create(s, p, root) {
247                 var t = this, sp, ns, cn, scn, c, de = 0;
248
249                 // Parse : <prefix> <class>:<super class>
250                 s = /^((static) )?([\w.]+)(:([\w.]+))?/.exec(s);
251                 cn = s[3].match(/(^|\.)(\w+)$/i)[2]; // Class name
252
253                 // Create namespace for new class
254                 ns = t.createNS(s[3].replace(/\.\w+$/, ''), root);
255
256                 // Class already exists
257                 if (ns[cn]) {
258                         return;
259                 }
260
261                 // Make pure static class
262                 if (s[2] == 'static') {
263                         ns[cn] = p;
264
265                         if (this.onCreate) {
266                                 this.onCreate(s[2], s[3], ns[cn]);
267                         }
268
269                         return;
270                 }
271
272                 // Create default constructor
273                 if (!p[cn]) {
274                         p[cn] = function() {};
275                         de = 1;
276                 }
277
278                 // Add constructor and methods
279                 ns[cn] = p[cn];
280                 t.extend(ns[cn].prototype, p);
281
282                 // Extend
283                 if (s[5]) {
284                         sp = t.resolve(s[5]).prototype;
285                         scn = s[5].match(/\.(\w+)$/i)[1]; // Class name
286
287                         // Extend constructor
288                         c = ns[cn];
289                         if (de) {
290                                 // Add passthrough constructor
291                                 ns[cn] = function() {
292                                         return sp[scn].apply(this, arguments);
293                                 };
294                         } else {
295                                 // Add inherit constructor
296                                 ns[cn] = function() {
297                                         this.parent = sp[scn];
298                                         return c.apply(this, arguments);
299                                 };
300                         }
301                         ns[cn].prototype[cn] = ns[cn];
302
303                         // Add super methods
304                         t.each(sp, function(f, n) {
305                                 ns[cn].prototype[n] = sp[n];
306                         });
307
308                         // Add overridden methods
309                         t.each(p, function(f, n) {
310                                 // Extend methods if needed
311                                 if (sp[n]) {
312                                         ns[cn].prototype[n] = function() {
313                                                 this.parent = sp[n];
314                                                 return f.apply(this, arguments);
315                                         };
316                                 } else {
317                                         if (n != cn) {
318                                                 ns[cn].prototype[n] = f;
319                                         }
320                                 }
321                         });
322                 }
323
324                 // Add static methods
325                 /*jshint sub:true*/
326                 t.each(p['static'], function(f, n) {
327                         ns[cn][n] = f;
328                 });
329         }
330
331         /**
332          * Returns the index of a value in an array, this method will return -1 if the item wasn't found.
333          *
334          * @method inArray
335          * @param {Array} a Array/Object to search for value in.
336          * @param {Object} v Value to check for inside the array.
337          * @return {Number/String} Index of item inside the array inside an object. Or -1 if it wasn't found.
338          * @example
339          * // Get index of value in array this will alert 1 since 2 is at that index
340          * alert(tinymce.inArray([1,2,3], 2));
341          */
342         function inArray(a, v) {
343                 var i, l;
344
345                 if (a) {
346                         for (i = 0, l = a.length; i < l; i++) {
347                                 if (a[i] === v) {
348                                         return i;
349                                 }
350                         }
351                 }
352
353                 return -1;
354         }
355
356         function extend(obj, ext) {
357                 var i, l, name, args = arguments, value;
358
359                 for (i = 1, l = args.length; i < l; i++) {
360                         ext = args[i];
361                         for (name in ext) {
362                                 if (ext.hasOwnProperty(name)) {
363                                         value = ext[name];
364
365                                         if (value !== undefined) {
366                                                 obj[name] = value;
367                                         }
368                                 }
369                         }
370                 }
371
372                 return obj;
373         }
374
375         /**
376          * Executed the specified function for each item in a object tree.
377          *
378          * @method walk
379          * @param {Object} o Object tree to walk though.
380          * @param {function} f Function to call for each item.
381          * @param {String} n Optional name of collection inside the objects to walk for example childNodes.
382          * @param {String} s Optional scope to execute the function in.
383          */
384         function walk(o, f, n, s) {
385                 s = s || this;
386
387                 if (o) {
388                         if (n) {
389                                 o = o[n];
390                         }
391
392                         each(o, function(o, i) {
393                                 if (f.call(s, o, i, n) === false) {
394                                         return false;
395                                 }
396
397                                 walk(o, f, n, s);
398                         });
399                 }
400         }
401
402         /**
403          * Creates a namespace on a specific object.
404          *
405          * @method createNS
406          * @param {String} n Namespace to create for example a.b.c.d.
407          * @param {Object} o Optional object to add namespace to, defaults to window.
408          * @return {Object} New namespace object the last item in path.
409          * @example
410          * // Create some namespace
411          * tinymce.createNS('tinymce.somepackage.subpackage');
412          *
413          * // Add a singleton
414          * var tinymce.somepackage.subpackage.SomeSingleton = {
415          *     method: function() {
416          *         // Some method
417          *     }
418          * };
419          */
420         function createNS(n, o) {
421                 var i, v;
422
423                 o = o || window;
424
425                 n = n.split('.');
426                 for (i=0; i<n.length; i++) {
427                         v = n[i];
428
429                         if (!o[v]) {
430                                 o[v] = {};
431                         }
432
433                         o = o[v];
434                 }
435
436                 return o;
437         }
438
439         /**
440          * Resolves a string and returns the object from a specific structure.
441          *
442          * @method resolve
443          * @param {String} n Path to resolve for example a.b.c.d.
444          * @param {Object} o Optional object to search though, defaults to window.
445          * @return {Object} Last object in path or null if it couldn't be resolved.
446          * @example
447          * // Resolve a path into an object reference
448          * var obj = tinymce.resolve('a.b.c.d');
449          */
450         function resolve(n, o) {
451                 var i, l;
452
453                 o = o || window;
454
455                 n = n.split('.');
456                 for (i = 0, l = n.length; i < l; i++) {
457                         o = o[n[i]];
458
459                         if (!o) {
460                                 break;
461                         }
462                 }
463
464                 return o;
465         }
466
467         /**
468          * Splits a string but removes the whitespace before and after each value.
469          *
470          * @method explode
471          * @param {string} s String to split.
472          * @param {string} d Delimiter to split by.
473          * @example
474          * // Split a string into an array with a,b,c
475          * var arr = tinymce.explode('a, b,   c');
476          */
477         function explode(s, d) {
478                 if (!s || is(s, 'array')) {
479                         return s;
480                 }
481
482                 return map(s.split(d || ','), trim);
483         }
484
485         return {
486                 trim: trim,
487                 isArray: isArray,
488                 is: is,
489                 toArray: toArray,
490                 makeMap: makeMap,
491                 each: each,
492                 map: map,
493                 grep: grep,
494                 inArray: inArray,
495                 extend: extend,
496                 create: create,
497                 walk: walk,
498                 createNS: createNS,
499                 resolve: resolve,
500                 explode: explode
501         };
502 });