source: trunk/filemanager/tp/ckeditor/_source/core/editor.js @ 2000

Revision 2000, 19.9 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implementação do módulo gerenciador de arquivos

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.editor} class, which represents an
8 *              editor instance.
9 */
10
11(function()
12{
13        // The counter for automatic instance names.
14        var nameCounter = 0;
15
16        var getNewName = function()
17        {
18                var name = 'editor' + ( ++nameCounter );
19                return ( CKEDITOR.instances && CKEDITOR.instances[ name ] ) ? getNewName() : name;
20        };
21
22        // ##### START: Config Privates
23
24        // These function loads custom configuration files and cache the
25        // CKEDITOR.editorConfig functions defined on them, so there is no need to
26        // download them more than once for several instances.
27        var loadConfigLoaded = {};
28        var loadConfig = function( editor )
29        {
30                var customConfig = editor.config.customConfig;
31
32                // Check if there is a custom config to load.
33                if ( !customConfig )
34                        return false;
35
36                var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );
37
38                // If the custom config has already been downloaded, reuse it.
39                if ( loadedConfig.fn )
40                {
41                        // Call the cached CKEDITOR.editorConfig defined in the custom
42                        // config file for the editor instance depending on it.
43                        loadedConfig.fn.call( editor, editor.config );
44
45                        // If there is no other customConfig in the chain, fire the
46                        // "configLoaded" event.
47                        if ( editor.config.customConfig == customConfig || !loadConfig( editor ) )
48                                editor.fireOnce( 'customConfigLoaded' );
49                }
50                else
51                {
52                        // Load the custom configuration file.
53                        CKEDITOR.scriptLoader.load( customConfig, function()
54                                {
55                                        // If the CKEDITOR.editorConfig function has been properly
56                                        // defined in the custom configuration file, cache it.
57                                        if ( CKEDITOR.editorConfig )
58                                                loadedConfig.fn = CKEDITOR.editorConfig;
59                                        else
60                                                loadedConfig.fn = function(){};
61
62                                        // Call the load config again. This time the custom
63                                        // config is already cached and so it will get loaded.
64                                        loadConfig( editor );
65                                });
66                }
67
68                return true;
69        };
70
71        var initConfig = function( editor, instanceConfig )
72        {
73                // Setup the lister for the "customConfigLoaded" event.
74                editor.on( 'customConfigLoaded', function()
75                        {
76                                if ( instanceConfig )
77                                {
78                                        // Register the events that may have been set at the instance
79                                        // configuration object.
80                                        if ( instanceConfig.on )
81                                        {
82                                                for ( var eventName in instanceConfig.on )
83                                                {
84                                                        editor.on( eventName, instanceConfig.on[ eventName ] );
85                                                }
86                                        }
87
88                                        // Overwrite the settings from the in-page config.
89                                        CKEDITOR.tools.extend( editor.config, instanceConfig, true );
90
91                                        delete editor.config.on;
92                                }
93
94                                onConfigLoaded( editor );
95                        });
96
97                // The instance config may override the customConfig setting to avoid
98                // loading the default ~/config.js file.
99                if ( instanceConfig && instanceConfig.customConfig != undefined )
100                        editor.config.customConfig = instanceConfig.customConfig;
101
102                // Load configs from the custom configuration files.
103                if ( !loadConfig( editor ) )
104                        editor.fireOnce( 'customConfigLoaded' );
105        };
106
107        // ##### END: Config Privates
108
109        var onConfigLoaded = function( editor )
110        {
111                // Set config related properties.
112
113                var skin = editor.config.skin.split( ',' ),
114                        skinName = skin[ 0 ],
115                        skinPath = CKEDITOR.getUrl( skin[ 1 ] || (
116                                '_source/' +    // @Packager.RemoveLine
117                                'skins/' + skinName + '/' ) );
118
119                editor.skinName = skinName;
120                editor.skinPath = skinPath;
121                editor.skinClass = 'cke_skin_' + skinName;
122
123                // Fire the "configLoaded" event.
124                editor.fireOnce( 'configLoaded' );
125
126                // Load language file.
127                loadLang( editor );
128        };
129
130        var loadLang = function( editor )
131        {
132                CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )
133                        {
134                                editor.langCode = languageCode;
135
136                                // As we'll be adding plugin specific entries that could come
137                                // from different language code files, we need a copy of lang,
138                                // not a direct reference to it.
139                                editor.lang = CKEDITOR.tools.prototypedCopy( lang );
140
141                                // We're not able to support RTL in Firefox 2 at this time.
142                                if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )
143                                        editor.lang.dir = 'ltr';
144
145                                loadPlugins( editor );
146                        });
147        };
148
149        var loadPlugins = function( editor )
150        {
151                var config                      = editor.config,
152                        plugins                 = config.plugins,
153                        extraPlugins    = config.extraPlugins,
154                        removePlugins   = config.removePlugins;
155
156                if ( extraPlugins )
157                {
158                        // Remove them first to avoid duplications.
159                        var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
160                        plugins = plugins.replace( removeRegex, '' );
161
162                        plugins += ',' + extraPlugins;
163                }
164
165                if ( removePlugins )
166                {
167                        removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );
168                        plugins = plugins.replace( removeRegex, '' );
169                }
170
171                // Load all plugins defined in the "plugins" setting.
172                CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )
173                        {
174                                // The list of plugins.
175                                var pluginsArray = [];
176
177                                // The language code to get loaded for each plugin. Null
178                                // entries will be appended for plugins with no language files.
179                                var languageCodes = [];
180
181                                // The list of URLs to language files.
182                                var languageFiles = [];
183
184                                // Cache the loaded plugin names.
185                                editor.plugins = plugins;
186
187                                // Loop through all plugins, to build the list of language
188                                // files to get loaded.
189                                for ( var pluginName in plugins )
190                                {
191                                        var plugin = plugins[ pluginName ],
192                                                pluginLangs = plugin.lang,
193                                                pluginPath = CKEDITOR.plugins.getPath( pluginName ),
194                                                lang = null;
195
196                                        // Set the plugin path in the plugin.
197                                        plugin.path = pluginPath;
198
199                                        // If the plugin has "lang".
200                                        if ( pluginLangs )
201                                        {
202                                                // Resolve the plugin language. If the current language
203                                                // is not available, get the first one (default one).
204                                                lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );
205
206                                                if ( !plugin.lang[ lang ] )
207                                                {
208                                                        // Put the language file URL into the list of files to
209                                                        // get downloaded.
210                                                        languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );
211                                                }
212                                                else
213                                                {
214                                                        CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );
215                                                        lang = null;
216                                                }
217                                        }
218
219                                        // Save the language code, so we know later which
220                                        // language has been resolved to this plugin.
221                                        languageCodes.push( lang );
222
223                                        pluginsArray.push( plugin );
224                                }
225
226                                // Load all plugin specific language files in a row.
227                                CKEDITOR.scriptLoader.load( languageFiles, function()
228                                        {
229                                                // Initialize all plugins that have the "beforeInit" and "init" methods defined.
230                                                var methods = [ 'beforeInit', 'init', 'afterInit' ];
231                                                for ( var m = 0 ; m < methods.length ; m++ )
232                                                {
233                                                        for ( var i = 0 ; i < pluginsArray.length ; i++ )
234                                                        {
235                                                                var plugin = pluginsArray[ i ];
236
237                                                                // Uses the first loop to update the language entries also.
238                                                                if ( m === 0 && languageCodes[ i ] && plugin.lang )
239                                                                        CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );
240
241                                                                // Call the plugin method (beforeInit and init).
242                                                                if ( plugin[ methods[ m ] ] )
243                                                                        plugin[ methods[ m ] ]( editor );
244                                                        }
245                                                }
246
247                                                // Load the editor skin.
248                                                editor.fire( 'pluginsLoaded' );
249                                                loadSkin( editor );
250                                        });
251                        });
252        };
253
254        var loadSkin = function( editor )
255        {
256                CKEDITOR.skins.load( editor, 'editor', function()
257                        {
258                                loadTheme( editor );
259                        });
260        };
261
262        var loadTheme = function( editor )
263        {
264                var theme = editor.config.theme;
265                CKEDITOR.themes.load( theme, function()
266                        {
267                                var editorTheme = editor.theme = CKEDITOR.themes.get( theme );
268                                editorTheme.path = CKEDITOR.themes.getPath( theme );
269                                editorTheme.build( editor );
270
271                                if ( editor.config.autoUpdateElement )
272                                        attachToForm( editor );
273                        });
274        };
275
276        var attachToForm = function( editor )
277        {
278                var element = editor.element;
279
280                // If are replacing a textarea, we must
281                if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
282                {
283                        var form = element.$.form && new CKEDITOR.dom.element( element.$.form );
284                        if ( form )
285                        {
286                                function onSubmit()
287                                {
288                                        editor.updateElement();
289                                }
290                                form.on( 'submit',onSubmit );
291
292                                // Setup the submit function because it doesn't fire the
293                                // "submit" event.
294                                if ( !form.$.submit.nodeName )
295                                {
296                                        form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )
297                                                {
298                                                        return function()
299                                                                {
300                                                                        editor.updateElement();
301
302                                                                        // For IE, the DOM submit function is not a
303                                                                        // function, so we need thid check.
304                                                                        if ( originalSubmit.apply )
305                                                                                originalSubmit.apply( this, arguments );
306                                                                        else
307                                                                                originalSubmit();
308                                                                };
309                                                });
310                                }
311
312                                // Remove 'submit' events registered on form element before destroying.(#3988)
313                                editor.on( 'destroy', function()
314                                {
315                                        form.removeListener( 'submit', onSubmit );
316                                } );
317                        }
318                }
319        };
320
321        function updateCommandsMode()
322        {
323                var command,
324                        commands = this._.commands,
325                        mode = this.mode;
326
327                for ( var name in commands )
328                {
329                        command = commands[ name ];
330                        command[ command.modes[ mode ] ? 'enable' : 'disable' ]();
331                }
332        }
333
334        /**
335         * Initializes the editor instance. This function is called by the editor
336         * contructor (editor_basic.js).
337         * @private
338         */
339        CKEDITOR.editor.prototype._init = function()
340                {
341                        // Get the properties that have been saved in the editor_base
342                        // implementation.
343                        var element                     = CKEDITOR.dom.element.get( this._.element ),
344                                instanceConfig  = this._.instanceConfig;
345                        delete this._.element;
346                        delete this._.instanceConfig;
347
348                        this._.commands = {};
349                        this._.styles = [];
350
351                        /**
352                         * The DOM element that has been replaced by this editor instance. This
353                         * element holds the editor data on load and post.
354                         * @name CKEDITOR.editor.prototype.element
355                         * @type CKEDITOR.dom.element
356                         * @example
357                         * var editor = CKEDITOR.instances.editor1;
358                         * alert( <b>editor.element</b>.getName() );  "textarea"
359                         */
360                        this.element = element;
361
362                        /**
363                         * The editor instance name. It hay be the replaced element id, name or
364                         * a default name using a progressive counter (editor1, editor2, ...).
365                         * @name CKEDITOR.editor.prototype.name
366                         * @type String
367                         * @example
368                         * var editor = CKEDITOR.instances.editor1;
369                         * alert( <b>editor.name</b> );  "editor1"
370                         */
371                        this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
372                                                        && ( element.getId() || element.getNameAtt() ) )
373                                                || getNewName();
374
375                        if ( this.name in CKEDITOR.instances )
376                                throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';
377
378                        /**
379                         * The configurations for this editor instance. It inherits all
380                         * settings defined in (@link CKEDITOR.config}, combined with settings
381                         * loaded from custom configuration files and those defined inline in
382                         * the page when creating the editor.
383                         * @name CKEDITOR.editor.prototype.config
384                         * @type Object
385                         * @example
386                         * var editor = CKEDITOR.instances.editor1;
387                         * alert( <b>editor.config.theme</b> );  "default" e.g.
388                         */
389                        this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );
390
391                        /**
392                         * Namespace containing UI features related to this editor instance.
393                         * @name CKEDITOR.editor.prototype.ui
394                         * @type CKEDITOR.ui
395                         * @example
396                         */
397                        this.ui = new CKEDITOR.ui( this );
398
399                        /**
400                         * Controls the focus state of this editor instance. This property
401                         * is rarely used for normal API operations. It is mainly
402                         * destinated to developer adding UI elements to the editor interface.
403                         * @name CKEDITOR.editor.prototype.focusManager
404                         * @type CKEDITOR.focusManager
405                         * @example
406                         */
407                        this.focusManager = new CKEDITOR.focusManager( this );
408
409                        CKEDITOR.fire( 'instanceCreated', null, this );
410
411                        this.on( 'mode', updateCommandsMode, null, null, 1 );
412
413                        initConfig( this, instanceConfig );
414                };
415})();
416
417CKEDITOR.tools.extend( CKEDITOR.editor.prototype,
418        /** @lends CKEDITOR.editor.prototype */
419        {
420                /**
421                 * Adds a command definition to the editor instance. Commands added with
422                 * this function can be later executed with {@link #execCommand}.
423                 * @param {String} commandName The indentifier name of the command.
424                 * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.
425                 * @example
426                 * editorInstance.addCommand( 'sample',
427                 * {
428                 *     exec : function( editor )
429                 *     {
430                 *         alert( 'Executing a command for the editor name "' + editor.name + '"!' );
431                 *     }
432                 * });
433                 */
434                addCommand : function( commandName, commandDefinition )
435                {
436                        return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );
437                },
438
439                /**
440                 * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.
441                 * Note: This function should be called before editor is loaded to take effect.
442                 * @param css {String} CSS text.
443                 * @example
444                 * editorInstance.addCss( 'body { background-color: grey; }' );
445                 */
446                addCss : function( css )
447                {
448                        this._.styles.push( css );
449                },
450
451                /**
452                 * Destroys the editor instance, releasing all resources used by it.
453                 * If the editor replaced an element, the element will be recovered.
454                 * @param {Boolean} [noUpdate] If the instance is replacing a DOM
455                 *              element, this parameter indicates whether or not to update the
456                 *              element with the instance contents.
457                 * @example
458                 * alert( CKEDITOR.instances.editor1 );  e.g "object"
459                 * <b>CKEDITOR.instances.editor1.destroy()</b>;
460                 * alert( CKEDITOR.instances.editor1 );  "undefined"
461                 */
462                destroy : function( noUpdate )
463                {
464                        if ( !noUpdate )
465                                this.updateElement();
466
467                        this.theme.destroy( this );
468                        this.fire( 'destroy' );
469                        CKEDITOR.remove( this );
470                },
471
472                /**
473                 * Executes a command.
474                 * @param {String} commandName The indentifier name of the command.
475                 * @param {Object} [data] Data to be passed to the command
476                 * @returns {Boolean} "true" if the command has been successfuly
477                 *              executed, otherwise "false".
478                 * @example
479                 * editorInstance.execCommand( 'Bold' );
480                 */
481                execCommand : function( commandName, data )
482                {
483                        var command = this.getCommand( commandName );
484
485                        var eventData =
486                        {
487                                name: commandName,
488                                commandData: data,
489                                command: command
490                        };
491
492                        if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )
493                        {
494                                if ( this.fire( 'beforeCommandExec', eventData ) !== true )
495                                {
496                                        eventData.returnValue = command.exec( eventData.commandData );
497
498                                        // Fire the 'afterCommandExec' immediately if command is synchronous.
499                                        if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )
500                                                return eventData.returnValue;
501                                }
502                        }
503
504                        // throw 'Unknown command name "' + commandName + '"';
505                        return false;
506                },
507
508                /**
509                 * Gets one of the registered commands. Note that, after registering a
510                 * command definition with addCommand, it is transformed internally
511                 * into an instance of {@link CKEDITOR.command}, which will be then
512                 * returned by this function.
513                 * @param {String} commandName The name of the command to be returned.
514                 * This is the same used to register the command with addCommand.
515                 * @returns {CKEDITOR.command} The command object identified by the
516                 * provided name.
517                 */
518                getCommand : function( commandName )
519                {
520                        return this._.commands[ commandName ];
521                },
522
523                /**
524                 * Gets the editor data. The data will be in raw format. It is the same
525                 * data that is posted by the editor.
526                 * @type String
527                 * @returns (String) The editor data.
528                 * @example
529                 * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )
530                 *     alert( 'There is no data available' );
531                 */
532                getData : function()
533                {
534                        this.fire( 'beforeGetData' );
535
536                        var eventData = this._.data;
537
538                        if ( typeof eventData != 'string' )
539                        {
540                                var element = this.element;
541                                if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
542                                        eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
543                                else
544                                        eventData = '';
545                        }
546
547                        eventData = { dataValue : eventData };
548
549                        // Fire "getData" so data manipulation may happen.
550                        this.fire( 'getData', eventData );
551
552                        return eventData.dataValue;
553                },
554
555                getSnapshot : function()
556                {
557                        var data = this.fire( 'getSnapshot' );
558
559                        if ( typeof data != 'string' )
560                        {
561                                var element = this.element;
562                                if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
563                                        data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
564                        }
565
566                        return data;
567                },
568
569                loadSnapshot : function( snapshot )
570                {
571                        this.fire( 'loadSnapshot', snapshot );
572                },
573
574                /**
575                 * Sets the editor data. The data must be provided in raw format.
576                 * @param {String} data HTML code to replace the curent content in the editor.
577                 * @param {Function} callback Function to be called after the setData is completed.
578                 * @example
579                 * CKEDITOR.instances.editor1.<b>setData( '&lt;p&gt;This is the editor data.&lt;/p&gt;' )</b>;
580                 * CKEDITOR.instances.editor1.setData( '&lt;p&gt;Some other editor data.&lt;/p&gt;', function()
581                 * {
582                 *              CKEDITOR.instances.editor1.checkDirty();        // true
583                 * } );
584                 */
585                setData : function( data , callback )
586                {
587                        if( callback )
588                        {
589                                this.on( 'dataReady', function( evt )
590                                {
591                                        evt.removeListener();
592                                        callback.call( evt.editor );
593                                } );
594                        }
595                        // Fire "setData" so data manipulation may happen.
596                        var eventData = { dataValue : data };
597                        this.fire( 'setData', eventData );
598
599                        this._.data = eventData.dataValue;
600
601                        this.fire( 'afterSetData', eventData );
602                },
603
604                /**
605                 * Inserts HTML into the currently selected position in the editor.
606                 * @param {String} data HTML code to be inserted into the editor.
607                 * @example
608                 * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>;
609                 */
610                insertHtml : function( data )
611                {
612                        this.fire( 'insertHtml', data );
613                },
614
615                /**
616                 * Inserts an element into the currently selected position in the
617                 * editor.
618                 * @param {CKEDITOR.dom.element} element The element to be inserted
619                 *              into the editor.
620                 * @example
621                 * var element = CKEDITOR.dom.element.createFromHtml( '&lt;img src="hello.png" border="0" title="Hello" /&gt;' );
622                 * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
623                 */
624                insertElement : function( element )
625                {
626                        this.fire( 'insertElement', element );
627                },
628
629                checkDirty : function()
630                {
631                        return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
632                },
633
634                resetDirty : function()
635                {
636                        if ( this.mayBeDirty )
637                                this._.previousValue = this.getSnapshot();
638                },
639
640                /**
641                 * Updates the &lt;textarea&gt; element that has been replaced by the editor with
642                 * the current data available in the editor.
643                 * @example
644                 * CKEDITOR.instances.editor1.updateElement();
645                 * alert( document.getElementById( 'editor1' ).value );  // The current editor data.
646                 */
647                updateElement : function()
648                {
649                        var element = this.element;
650                        if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
651                        {
652                                if ( element.is( 'textarea' ) )
653                                        element.setValue( this.getData() );
654                                else
655                                        element.setHtml( this.getData() );
656                        }
657                }
658        });
659
660CKEDITOR.on( 'loaded', function()
661        {
662                // Run the full initialization for pending editors.
663                var pending = CKEDITOR.editor._pending;
664                if ( pending )
665                {
666                        delete CKEDITOR.editor._pending;
667
668                        for ( var i = 0 ; i < pending.length ; i++ )
669                                pending[ i ]._init();
670                }
671        });
Note: See TracBrowser for help on using the repository browser.