source: branches/2.2/filemanager/tp/ckeditor/_source/core/tools.js @ 3019

Revision 3019, 14.9 KB checked in by amuller, 14 years ago (diff)

Ticket #1135 - Corrigindo CSS e adicionando filemanager

Line 
1/*
2Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6/**
7 * @fileOverview Defines the {@link CKEDITOR.tools} object, which contains
8 *              utility functions.
9 */
10
11(function()
12{
13        var functions = [];
14
15        /**
16         * Utility functions.
17         * @namespace
18         * @example
19         */
20        CKEDITOR.tools =
21        {
22                arrayCompare : function( arrayA, arrayB )
23                {
24                        if ( !arrayA && !arrayB )
25                                return true;
26
27                        if ( !arrayA || !arrayB || arrayA.length != arrayB.length )
28                                return false;
29
30                        for ( var i = 0 ; i < arrayA.length ; i++ )
31                        {
32                                if ( arrayA[ i ] != arrayB[ i ] )
33                                        return false;
34                        }
35
36                        return true;
37                },
38
39                /**
40                 * Creates a deep copy of an object.
41                 * Attention: there is no support for recursive references.
42                 * @param {Object} object The object to be cloned.
43                 * @returns {Object} The object clone.
44                 * @example
45                 * var obj =
46                 *     {
47                 *         name : 'John',
48                 *         cars :
49                 *             {
50                 *                 Mercedes : { color : 'blue' },
51                 *                 Porsche : { color : 'red' }
52                 *             }
53                 *     };
54                 * var clone = CKEDITOR.tools.clone( obj );
55                 * clone.name = 'Paul';
56                 * clone.cars.Porsche.color = 'silver';
57                 * alert( obj.name );   // John
58                 * alert( clone.name ); // Paul
59                 * alert( obj.cars.Porsche.color );     // red
60                 * alert( clone.cars.Porsche.color );   // silver
61                 */
62                clone : function( obj )
63                {
64                        var clone;
65
66                        // Array.
67                        if ( obj && ( obj instanceof Array ) )
68                        {
69                                clone = [];
70
71                                for ( var i = 0 ; i < obj.length ; i++ )
72                                        clone[ i ] = this.clone( obj[ i ] );
73
74                                return clone;
75                        }
76
77                        // "Static" types.
78                        if ( obj === null
79                                || ( typeof( obj ) != 'object' )
80                                || ( obj instanceof String )
81                                || ( obj instanceof Number )
82                                || ( obj instanceof Boolean )
83                                || ( obj instanceof Date ) )
84                        {
85                                return obj;
86                        }
87
88                        // Objects.
89                        clone = new obj.constructor();
90
91                        for ( var propertyName in obj )
92                        {
93                                var property = obj[ propertyName ];
94                                clone[ propertyName ] = this.clone( property );
95                        }
96
97                        return clone;
98                },
99
100                /**
101                 * Copy the properties from one object to another. By default, properties
102                 * already present in the target object <strong>are not</strong> overwritten.
103                 * @param {Object} target The object to be extended.
104                 * @param {Object} source[,souce(n)] The objects from which copy
105                 *              properties. Any number of objects can be passed to this function.
106                 * @param {Boolean} [overwrite] If 'true' is specified it indicates that
107                 *            properties already present in the target object could be
108                 *            overwritten by subsequent objects.
109                 * @param {Object} [properties] Only properties within the specified names
110                 *            list will be received from the source object.
111                 * @returns {Object} the extended object (target).
112                 * @example
113                 * // Create the sample object.
114                 * var myObject =
115                 * {
116                 *     prop1 : true
117                 * };
118                 *
119                 * // Extend the above object with two properties.
120                 * CKEDITOR.tools.extend( myObject,
121                 *     {
122                 *         prop2 : true,
123                 *         prop3 : true
124                 *     } );
125                 *
126                 * // Alert "prop1", "prop2" and "prop3".
127                 * for ( var p in myObject )
128                 *     alert( p );
129                 */
130                extend : function( target )
131                {
132                        var argsLength = arguments.length,
133                                overwrite, propertiesList;
134
135                        if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')
136                                argsLength--;
137                        else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )
138                        {
139                                propertiesList = arguments [ argsLength -1 ];
140                                argsLength-=2;
141                        }
142                        for ( var i = 1 ; i < argsLength ; i++ )
143                        {
144                                var source = arguments[ i ];
145                                for ( var propertyName in source )
146                                {
147                                        // Only copy existed fields if in overwrite mode.
148                                        if ( overwrite === true || target[ propertyName ] == undefined )
149                                        {
150                                                // Only copy  specified fields if list is provided.
151                                                if ( !propertiesList || ( propertyName in propertiesList ) )
152                                                        target[ propertyName ] = source[ propertyName ];
153
154                                        }
155                                }
156                        }
157
158                        return target;
159                },
160
161                /**
162                 * Creates an object which is an instance of a class which prototype is a
163                 * predefined object. All properties defined in the source object are
164                 * automatically inherited by the resulting object, including future
165                 * changes to it.
166                 * @param {Object} source The source object to be used as the prototype for
167                 *              the final object.
168                 * @returns {Object} The resulting copy.
169                 */
170                prototypedCopy : function( source )
171                {
172                        var copy = function()
173                        {};
174                        copy.prototype = source;
175                        return new copy();
176                },
177
178                /**
179                 * Checks if an object is an Array.
180                 * @param {Object} object The object to be checked.
181                 * @type Boolean
182                 * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.
183                 * @example
184                 * alert( CKEDITOR.tools.isArray( [] ) );      // "true"
185                 * alert( CKEDITOR.tools.isArray( 'Test' ) );  // "false"
186                 */
187                isArray : function( object )
188                {
189                        return ( !!object && object instanceof Array );
190                },
191
192                /**
193                 * Transforms a CSS property name to its relative DOM style name.
194                 * @param {String} cssName The CSS property name.
195                 * @returns {String} The transformed name.
196                 * @example
197                 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) );  // "backgroundColor"
198                 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) );             // "cssFloat"
199                 */
200                cssStyleToDomStyle : ( function()
201                {
202                        var test = document.createElement( 'div' ).style;
203
204                        var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'
205                                : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'
206                                : 'float';
207
208                        return function( cssName )
209                        {
210                                if ( cssName == 'float' )
211                                        return cssFloat;
212                                else
213                                {
214                                        return cssName.replace( /-./g, function( match )
215                                                {
216                                                        return match.substr( 1 ).toUpperCase();
217                                                });
218                                }
219                        };
220                } )(),
221
222                /**
223                 * Replace special HTML characters in a string with their relative HTML
224                 * entity values.
225                 * @param {String} text The string to be encoded.
226                 * @returns {String} The encode string.
227                 * @example
228                 * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) );  // "A &amp;gt; B &amp;amp; C &amp;lt; D"
229                 */
230                htmlEncode : function( text )
231                {
232                        var standard = function( text )
233                        {
234                                var span = new CKEDITOR.dom.element( 'span' );
235                                span.setText( text );
236                                return span.getHtml();
237                        };
238
239                        var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?
240                                function( text )
241                                {
242                                        // #3874 IE and Safari encode line-break into <br>
243                                        return standard( text ).replace( /<br>/gi, '\n' );
244                                } :
245                                standard;
246
247                        var fix2 = ( standard( '>' ) == '>' ) ?
248                                function( text )
249                                {
250                                        // WebKit does't encode the ">" character, which makes sense, but
251                                        // it's different than other browsers.
252                                        return fix1( text ).replace( />/g, '&gt;' );
253                                } :
254                                fix1;
255
256                        var fix3 = ( standard( '  ' ) == '&nbsp; ' ) ?
257                                function( text )
258                                {
259                                        // #3785 IE8 changes spaces (>= 2) to &nbsp;
260                                        return fix2( text ).replace( /&nbsp;/g, ' ' );
261                                } :
262                                fix2;
263
264                        this.htmlEncode = fix3;
265
266                        return this.htmlEncode( text );
267                },
268
269                /**
270                 * Gets a unique number for this CKEDITOR execution session. It returns
271                 * progressive numbers starting at 1.
272                 * @function
273                 * @returns {Number} A unique number.
274                 * @example
275                 * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "1" (e.g.)
276                 * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "2"
277                 */
278                getNextNumber : (function()
279                {
280                        var last = 0;
281                        return function()
282                        {
283                                return ++last;
284                        };
285                })(),
286
287                /**
288                 * Creates a function override.
289                 * @param {Function} originalFunction The function to be overridden.
290                 * @param {Function} functionBuilder A function that returns the new
291                 *              function. The original function reference will be passed to this
292                 *              function.
293                 * @returns {Function} The new function.
294                 * @example
295                 * var example =
296                 * {
297                 *     myFunction : function( name )
298                 *     {
299                 *         alert( 'Name: ' + name );
300                 *     }
301                 * };
302                 *
303                 * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )
304                 *     {
305                 *         return function( name )
306                 *             {
307                 *                 alert( 'Override Name: ' + name );
308                 *                 myFunctionOriginal.call( this, name );
309                 *             };
310                 *     });
311                 */
312                override : function( originalFunction, functionBuilder )
313                {
314                        return functionBuilder( originalFunction );
315                },
316
317                /**
318                 * Executes a function after specified delay.
319                 * @param {Function} func The function to be executed.
320                 * @param {Number} [milliseconds] The amount of time (millisecods) to wait
321                 *              to fire the function execution. Defaults to zero.
322                 * @param {Object} [scope] The object to hold the function execution scope
323                 *              (the "this" object). By default the "window" object.
324                 * @param {Object|Array} [args] A single object, or an array of objects, to
325                 *              pass as arguments to the function.
326                 * @param {Object} [ownerWindow] The window that will be used to set the
327                 *              timeout. By default the current "window".
328                 * @returns {Object} A value that can be used to cancel the function execution.
329                 * @example
330                 * CKEDITOR.tools.<b>setTimeout(
331                 *     function()
332                 *     {
333                 *         alert( 'Executed after 2 seconds' );
334                 *     },
335                 *     2000 )</b>;
336                 */
337                setTimeout : function( func, milliseconds, scope, args, ownerWindow )
338                {
339                        if ( !ownerWindow )
340                                ownerWindow = window;
341
342                        if ( !scope )
343                                scope = ownerWindow;
344
345                        return ownerWindow.setTimeout(
346                                function()
347                                {
348                                        if ( args )
349                                                func.apply( scope, [].concat( args ) ) ;
350                                        else
351                                                func.apply( scope ) ;
352                                },
353                                milliseconds || 0 );
354                },
355
356                /**
357                 * Remove spaces from the start and the end of a string. The following
358                 * characters are removed: space, tab, line break, line feed.
359                 * @function
360                 * @param {String} str The text from which remove the spaces.
361                 * @returns {String} The modified string without the boundary spaces.
362                 * @example
363                 * alert( CKEDITOR.tools.trim( '  example ' );  // "example"
364                 */
365                trim : (function()
366                {
367                        // We are not using \s because we don't want "non-breaking spaces" to be caught.
368                        var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;
369                        return function( str )
370                        {
371                                return str.replace( trimRegex, '' ) ;
372                        };
373                })(),
374
375                /**
376                 * Remove spaces from the start (left) of a string. The following
377                 * characters are removed: space, tab, line break, line feed.
378                 * @function
379                 * @param {String} str The text from which remove the spaces.
380                 * @returns {String} The modified string excluding the removed spaces.
381                 * @example
382                 * alert( CKEDITOR.tools.ltrim( '  example ' );  // "example "
383                 */
384                ltrim : (function()
385                {
386                        // We are not using \s because we don't want "non-breaking spaces" to be caught.
387                        var trimRegex = /^[ \t\n\r]+/g;
388                        return function( str )
389                        {
390                                return str.replace( trimRegex, '' ) ;
391                        };
392                })(),
393
394                /**
395                 * Remove spaces from the end (right) of a string. The following
396                 * characters are removed: space, tab, line break, line feed.
397                 * @function
398                 * @param {String} str The text from which remove the spaces.
399                 * @returns {String} The modified string excluding the removed spaces.
400                 * @example
401                 * alert( CKEDITOR.tools.ltrim( '  example ' );  // "  example"
402                 */
403                rtrim : (function()
404                {
405                        // We are not using \s because we don't want "non-breaking spaces" to be caught.
406                        var trimRegex = /[ \t\n\r]+$/g;
407                        return function( str )
408                        {
409                                return str.replace( trimRegex, '' ) ;
410                        };
411                })(),
412
413                /**
414                 * Returns the index of an element in an array.
415                 * @param {Array} array The array to be searched.
416                 * @param {Object} entry The element to be found.
417                 * @returns {Number} The (zero based) index of the first entry that matches
418                 *              the entry, or -1 if not found.
419                 * @example
420                 * var letters = [ 'a', 'b', 0, 'c', false ];
421                 * alert( CKEDITOR.tools.indexOf( letters, '0' ) );  "-1" because 0 !== '0'
422                 * alert( CKEDITOR.tools.indexOf( letters, false ) );  "4" because 0 !== false
423                 */
424                indexOf :
425                        // #2514: We should try to use Array.indexOf if it does exist.
426                        ( Array.prototype.indexOf ) ?
427                                function( array, entry )
428                                        {
429                                                return array.indexOf( entry );
430                                        }
431                        :
432                                function( array, entry )
433                                {
434                                        for ( var i = 0, len = array.length ; i < len ; i++ )
435                                        {
436                                                if ( array[ i ] === entry )
437                                                        return i;
438                                        }
439                                        return -1;
440                                },
441
442                bind : function( func, obj )
443                {
444                        return function() { return func.apply( obj, arguments ); };
445                },
446
447                /**
448                 * Class creation based on prototype inheritance, with supports of the
449                 * following features:
450                 * <ul>
451                 * <li> Static fields </li>
452                 * <li> Private fields </li>
453                 * <li> Public(prototype) fields </li>
454                 * <li> Chainable base class constructor </li>
455                 * </ul>
456                 *
457                 * @param {Object} definiton (Optional)The class definiton object.
458                 */
459                createClass : function( definition )
460                {
461                        var $ = definition.$,
462                                baseClass = definition.base,
463                                privates = definition.privates || definition._,
464                                proto = definition.proto,
465                                statics = definition.statics;
466
467                        if ( privates )
468                        {
469                                var originalConstructor = $;
470                                $ = function()
471                                {
472                                        // Create (and get) the private namespace.
473                                        var _ = this._ || ( this._ = {} );
474
475                                        // Make some magic so "this" will refer to the main
476                                        // instance when coding private functions.
477                                        for ( var privateName in privates )
478                                        {
479                                                var priv = privates[ privateName ];
480
481                                                _[ privateName ] =
482                                                        ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;
483                                        }
484
485                                        originalConstructor.apply( this, arguments );
486                                };
487                        }
488
489                        if ( baseClass )
490                        {
491                                $.prototype = this.prototypedCopy( baseClass.prototype );
492                                $.prototype.constructor = $;
493                                $.prototype.base = function()
494                                {
495                                        this.base = baseClass.prototype.base;
496                                        baseClass.apply( this, arguments );
497                                        this.base = arguments.callee;
498                                };
499                        }
500
501                        if ( proto )
502                                this.extend( $.prototype, proto, true );
503
504                        if ( statics )
505                                this.extend( $, statics, true );
506
507                        return $;
508                },
509
510                addFunction : function( fn, scope )
511                {
512                        return functions.push( function()
513                                {
514                                        fn.apply( scope || this, arguments );
515                                }) - 1;
516                },
517
518                callFunction : function( index )
519                {
520                        var fn = functions[ index ];
521                        return fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );
522                },
523
524                cssLength : (function()
525                {
526                        var decimalRegex = /^\d+(?:\.\d+)?$/;
527                        return function( length )
528                        {
529                                return length + ( decimalRegex.test( length ) ? 'px' : '' );
530                        };
531                })(),
532
533                repeat : function( str, times )
534                {
535                        return new Array( times + 1 ).join( str );
536                }
537        };
538})();
539
540// PACKAGER_RENAME( CKEDITOR.tools )
Note: See TracBrowser for help on using the repository browser.