source: sandbox/2.3-MailArchiver/filemanager/tp/ckeditor/_source/plugins/scayt/plugin.js @ 6779

Revision 6779, 14.1 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado Expresso(branch 2.3) integrado ao MailArchiver?.

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 Spell Check As You Type (SCAYT).
8 * Button name : Scayt.
9 */
10
11(function()
12{
13        var commandName         = 'scaytcheck',
14                openPage                = '';
15
16        var onEngineLoad = function()
17        {
18                var editor = this;
19
20                var createInstance = function() // Create new instance every time Document is created.
21                {
22                        // Initialise Scayt instance.
23                        var oParams = {};
24                        oParams.srcNodeRef = editor.document.getWindow().$.frameElement;                // Get the iframe.
25                        // syntax : AppName.AppVersion@AppRevision
26                        oParams.assocApp  = "CKEDITOR." + CKEDITOR.version + "@" + CKEDITOR.revision;
27
28                        oParams.customerid = editor.config.scayt_customerid  || "1:11111111111111111111111111111111111111";
29                        oParams.customDictionaryName = editor.config.scayt_customDictionaryName;
30                        oParams.userDictionaryName = editor.config.scayt_userDictionaryName;
31                        oParams.defLang = editor.scayt_defLang;
32
33                        if ( CKEDITOR._scaytParams )
34                        {
35                                for ( var k in CKEDITOR._scaytParams )
36                                {
37                                        oParams[ k ] = CKEDITOR._scaytParams[ k ];
38                                }
39                        }
40
41                        var scayt_control = new window.scayt( oParams );
42
43                        // Copy config.
44                        var     lastInstance = plugin.instances[ editor.name ];
45                        if ( lastInstance )
46                        {
47                                scayt_control.sLang = lastInstance.sLang;
48                                scayt_control.option( lastInstance.option() );
49                                scayt_control.paused = lastInstance.paused;
50                        }
51
52                        plugin.instances[ editor.name ] = scayt_control;
53
54                        try {
55                                scayt_control.setDisabled( scayt_control.paused === false );                            // I really don't know why it causes JS error in IE
56                        } catch (e) {}
57                        editor.fire( 'showScaytState' );
58                };
59
60                editor.on( 'contentDom', createInstance );
61                editor.on( 'contentDomUnload', function()
62                        {
63                                // Remove scripts.
64                                var scripts = CKEDITOR.document.getElementsByTag( 'script' ),
65                                        scaytIdRegex =  /^dojoIoScript(\d+)$/i,
66                                        scaytSrcRegex =  /^https?:\/\/svc\.spellchecker\.net\/spellcheck\/script\/ssrv\.cgi/i;
67
68                                for ( var i=0; i < scripts.count(); i++ )
69                                {
70                                        var script = scripts.getItem( i ),
71                                                id = script.getId(),
72                                                src = script.getAttribute( 'src' );
73
74                                        if ( id && src && id.match( scaytIdRegex ) && src.match( scaytSrcRegex ))
75                                                script.remove();
76                                }
77                        });
78
79                editor.on( 'beforeCommandExec', function( ev )          // Disable SCAYT before Source command execution.
80                        {
81                                if ( (ev.data.name == 'source' ||  ev.data.name == 'newpage') && editor.mode == 'wysiwyg' )
82                                {
83                                        var scayt_instanse = plugin.getScayt( editor );
84                                        if ( scayt_instanse )
85                                        {
86                                                scayt_instanse.paused = !scayt_instanse.disabled;
87                                                scayt_instanse.destroy();
88                                                delete plugin.instances[ editor.name ];
89                                        }
90                                }
91                        });
92
93                // Listen to data manipulation to reflect scayt markup.
94                editor.on( 'afterSetData', function()
95                        {
96                                if ( plugin.isScaytEnabled( editor ) )
97                                        plugin.getScayt( editor ).refresh();
98                        });
99
100                // Reload spell-checking for current word after insertion completed.
101                editor.on( 'insertElement', function()
102                        {
103                                var scayt_instance = plugin.getScayt( editor );
104                                if ( plugin.isScaytEnabled( editor ) )
105                                {
106                                        // Unlock the selection before reload, SCAYT will take
107                                        // care selection update.
108                                        if ( CKEDITOR.env.ie )
109                                                editor.getSelection().unlock( true );
110
111                                        // Swallow any SCAYT engine errors.
112                                        try{
113                                                scayt_instance.refresh();
114                                        }catch( er )
115                                        {}
116                                }
117                        }, this, null, 50 );
118
119                editor.on( 'scaytDialog', function( ev )        // Communication with dialog.
120                        {
121                                ev.data.djConfig = window.djConfig;
122                                ev.data.scayt_control = plugin.getScayt( editor );
123                                ev.data.tab = openPage;
124                                ev.data.scayt = window.scayt;
125                        });
126
127                var dataProcessor = editor.dataProcessor,
128                        htmlFilter = dataProcessor && dataProcessor.htmlFilter;
129                if ( htmlFilter )
130                {
131                        htmlFilter.addRules(
132                                {
133                                        elements :
134                                        {
135                                                span : function( element )
136                                                {
137                                                        if ( element.attributes.scayt_word && element.attributes.scaytid )
138                                                        {
139                                                                delete element.name;    // Write children, but don't write this node.
140                                                                return element;
141                                                        }
142                                                }
143                                        }
144                                }
145                        );
146                }
147
148                if ( editor.document )
149                        createInstance();
150        };
151
152        CKEDITOR.plugins.scayt =
153        {
154                engineLoaded : false,
155                instances : {},
156                getScayt : function( editor )
157                {
158                        return this.instances[ editor.name ];
159                },
160                isScaytReady : function( editor )
161                {
162                        return this.engineLoaded === true &&
163                                'undefined' !== typeof window.scayt && this.getScayt( editor );
164                },
165                isScaytEnabled : function( editor )
166                {
167                        var scayt_instanse = this.getScayt( editor );
168                        return ( scayt_instanse ) ? scayt_instanse.disabled === false : false;
169                },
170                loadEngine : function( editor )
171                {
172                        if ( this.engineLoaded === true )
173                                return onEngineLoad.apply( editor );    // Add new instance.
174                        else if ( this.engineLoaded == -1 )                     // We are waiting.
175                                return CKEDITOR.on( 'scaytReady', function(){ onEngineLoad.apply( editor );} ); // Use function(){} to avoid rejection as duplicate.
176
177                        CKEDITOR.on( 'scaytReady', onEngineLoad, editor );
178                        CKEDITOR.on( 'scaytReady', function()
179                                {
180                                        this.engineLoaded = true;
181                                },
182                                this,
183                                null,
184                                0 );    // First to run.
185
186                        this.engineLoaded = -1; // Loading in progress.
187
188                        // compose scayt url
189                        var protocol = document.location.protocol;
190                        // Default to 'http' for unknown.
191                        protocol = protocol.search( /https?:/) != -1? protocol : 'http:';
192                        var baseUrl  = "svc.spellchecker.net/spellcheck/lf/scayt/scayt1.js";
193
194                        var scaytUrl  =  editor.config.scayt_srcUrl || ( protocol + "//" + baseUrl );
195                        var scaytConfigBaseUrl =  plugin.parseUrl( scaytUrl ).path +  "/";
196
197                        CKEDITOR._djScaytConfig =
198                        {
199                                baseUrl: scaytConfigBaseUrl,
200                                addOnLoad:
201                                [
202                                        function()
203                                        {
204                                                CKEDITOR.fireOnce( "scaytReady" );
205                                        }
206                                ],
207                                isDebug: false
208                        };
209                        // Append javascript code.
210                        CKEDITOR.document.getHead().append(
211                                CKEDITOR.document.createElement( 'script',
212                                        {
213                                                attributes :
214                                                        {
215                                                                type : 'text/javascript',
216                                                                src : scaytUrl
217                                                        }
218                                        })
219                        );
220
221                        return null;
222                },
223                parseUrl : function ( data )
224                {
225                        var match;
226                        if ( data.match && ( match = data.match(/(.*)[\/\\](.*?\.\w+)$/) ) )
227                                return { path: match[1], file: match[2] };
228                        else
229                                return data;
230                }
231        };
232
233        var plugin = CKEDITOR.plugins.scayt;
234
235        // Context menu constructing.
236        var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, command, menugroup, menuOrder )
237        {
238                editor.addCommand( commandName, command );
239
240                // If the "menu" plugin is loaded, register the menu item.
241                editor.addMenuItem( commandName,
242                        {
243                                label : buttonLabel,
244                                command : commandName,
245                                group : menugroup,
246                                order : menuOrder
247                        });
248        };
249
250        var commandDefinition =
251        {
252                preserveState : true,
253                editorFocus : false,
254
255                exec: function( editor )
256                {
257                        if ( plugin.isScaytReady( editor ) )
258                        {
259                                var isEnabled = plugin.isScaytEnabled( editor );
260
261                                this.setState( isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_ON );
262
263                                var scayt_control = plugin.getScayt( editor );
264                                scayt_control.setDisabled( isEnabled );
265                        }
266                        else if ( !editor.config.scayt_autoStartup && plugin.engineLoaded >= 0 )        // Load first time
267                        {
268                                this.setState( CKEDITOR.TRISTATE_DISABLED );
269
270                                editor.on( 'showScaytState', function()
271                                        {
272                                                this.removeListener();
273                                                this.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
274                                        },
275                                        this);
276
277                                plugin.loadEngine( editor );
278                        }
279                }
280        };
281
282        // Add scayt plugin.
283        CKEDITOR.plugins.add( 'scayt',
284        {
285                requires : [ 'menubutton' ],
286
287                beforeInit : function( editor )
288                {
289                        // Register own rbc menu group.
290                        editor.config.menu_groups = 'scayt_suggest,scayt_moresuggest,scayt_control,' + editor.config.menu_groups;
291                },
292
293                init : function( editor )
294                {
295                        var moreSuggestions = {};
296                        var mainSuggestions = {};
297
298                        // Scayt command.
299                        var command = editor.addCommand( commandName, commandDefinition );
300
301                        // Add Options dialog.
302                        CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );
303
304                        var menuGroup = 'scaytButton';
305                        editor.addMenuGroup( menuGroup );
306                        editor.addMenuItems(
307                                {
308                                        scaytToggle :
309                                        {
310                                                label : editor.lang.scayt.enable,
311                                                command : commandName,
312                                                group : menuGroup
313                                        },
314
315                                        scaytOptions :
316                                        {
317                                                label : editor.lang.scayt.options,
318                                                group : menuGroup,
319                                                onClick : function()
320                                                {
321                                                        openPage = 'options';
322                                                        editor.openDialog( commandName );
323                                                }
324                                        },
325
326                                        scaytLangs :
327                                        {
328                                                label : editor.lang.scayt.langs,
329                                                group : menuGroup,
330                                                onClick : function()
331                                                {
332                                                        openPage = 'langs';
333                                                        editor.openDialog( commandName );
334                                                }
335                                        },
336
337                                        scaytAbout :
338                                        {
339                                                label : editor.lang.scayt.about,
340                                                group : menuGroup,
341                                                onClick : function()
342                                                {
343                                                        openPage = 'about';
344                                                        editor.openDialog( commandName );
345                                                }
346                                        }
347                                });
348
349                                editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,
350                                        {
351                                                label : editor.lang.scayt.title,
352                                                title : editor.lang.scayt.title,
353                                                className : 'cke_button_scayt',
354                                                onRender: function()
355                                                {
356                                                command.on( 'state', function()
357                                                        {
358                                                                this.setState( command.state );
359                                                        },
360                                                        this);
361                                        },
362                                        onMenu : function()
363                                        {
364                                                var isEnabled = plugin.isScaytEnabled( editor );
365
366                                                editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];
367
368                                                        return {
369                                                                scaytToggle : CKEDITOR.TRISTATE_OFF,
370                                                                scaytOptions : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
371                                                                scaytLangs : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
372                                                                scaytAbout : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
373                                                        };
374                                                }
375                                        });
376
377                        // If the "contextmenu" plugin is loaded, register the listeners.
378                        if ( editor.contextMenu && editor.addMenuItems )
379                        {
380                                editor.contextMenu.addListener( function( element )
381                                        {
382                                                if ( !( plugin.isScaytEnabled( editor ) && element ) )
383                                                        return null;
384
385                                                var scayt_control = plugin.getScayt( editor ),
386                                                        word = scayt_control.getWord( element.$ );
387
388                                                if ( !word )
389                                                        return null;
390
391                                                var sLang = scayt_control.getLang(),
392                                                        _r = {},
393                                                        items_suggestion = window.scayt.getSuggestion( word, sLang );
394                                                if (!items_suggestion || !items_suggestion.length )
395                                                        return null;
396                                                // Remove unused commands and menuitems
397                                                for ( i in moreSuggestions )
398                                                {
399                                                        delete editor._.menuItems[ i ];
400                                                        delete editor._.commands[ i ];
401                                                }
402                                                for ( i in mainSuggestions )
403                                                {
404                                                        delete editor._.menuItems[ i ];
405                                                        delete editor._.commands[ i ];
406                                                }
407                                                moreSuggestions = {};           // Reset items.
408                                                mainSuggestions = {};
409
410                                                var moreSuggestionsUnable = false;
411
412                                                for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )
413                                                {
414                                                        var commandName = 'scayt_suggestion_' + items_suggestion[i].replace( ' ', '_' );
415                                                        var exec = ( function( el, s )
416                                                                {
417                                                                        return {
418                                                                                exec: function()
419                                                                                {
420                                                                                        scayt_control.replace(el, s);
421                                                                                }
422                                                                        };
423                                                                })( element.$, items_suggestion[i] );
424
425                                                        if ( i < editor.config.scayt_maxSuggestions )
426                                                        {
427                                                                addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
428                                                                        commandName, exec, 'scayt_suggest', i + 1 );
429                                                                _r[ commandName ] = CKEDITOR.TRISTATE_OFF;
430                                                                mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
431                                                        }
432                                                        else
433                                                        {
434                                                                addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
435                                                                        commandName, exec, 'scayt_moresuggest', i + 1 );
436                                                                moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
437                                                                moreSuggestionsUnable = true;
438                                                        }
439                                                }
440                                                if ( moreSuggestionsUnable )
441                                                        // Rgister the More suggestions group;
442                                                        editor.addMenuItem( 'scayt_moresuggest',
443                                                                {
444                                                                        label : editor.lang.scayt.moreSuggestions,
445                                                                        group : 'scayt_moresuggest',
446                                                                        order : 10,
447                                                                        getItems : function()
448                                                                        {
449                                                                                return moreSuggestions;
450                                                                        }
451                                                                });
452
453
454                                                var ignore_command =
455                                                {
456                                                        exec: function()
457                                                        {
458                                                                scayt_control.ignore( element.$ );
459                                                        }
460                                                };
461                                                var ignore_all_command =
462                                                {
463                                                        exec: function()
464                                                        {
465                                                                scayt_control.ignoreAll( element.$ );
466                                                        }
467                                                };
468                                                var addword_command =
469                                                {
470                                                        exec: function()
471                                                        {
472                                                                window.scayt.addWordToUserDictionary( element.$ );
473                                                        }
474                                                };
475
476                                                addButtonCommand( editor, 'ignore', editor.lang.scayt.ignore,
477                                                        'scayt_ignore', ignore_command, 'scayt_control', 1);
478                                                addButtonCommand( editor, 'ignore_all', editor.lang.scayt.ignoreAll,
479                                                        'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);
480                                                addButtonCommand( editor, 'add_word', editor.lang.scayt.addWord,
481                                                        'scayt_add_word', addword_command, 'scayt_control', 3);
482
483                                                mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;
484                                                mainSuggestions[ 'scayt_ignore' ] = CKEDITOR.TRISTATE_OFF;
485                                                mainSuggestions[ 'scayt_ignore_all' ] = CKEDITOR.TRISTATE_OFF;
486                                                mainSuggestions[ 'scayt_add_word' ] = CKEDITOR.TRISTATE_OFF;
487
488                                                if ( scayt_control.fireOnContextMenu )
489                                                        scayt_control.fireOnContextMenu( editor );
490
491                                                return mainSuggestions;
492                                        });
493                        }
494
495                        // Start plugin
496                        if ( editor.config.scayt_autoStartup )
497                        {
498                                var showInitialState = function()
499                                {
500                                        editor.removeListener( 'showScaytState', showInitialState );
501                                        command.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
502                                };
503                                editor.on( 'showScaytState', showInitialState );
504
505                                plugin.loadEngine( editor );
506                        }
507                }
508        });
509})();
510
511CKEDITOR.config.scayt_maxSuggestions =  5;
512CKEDITOR.config.scayt_autoStartup = false;
Note: See TracBrowser for help on using the repository browser.