source: sandbox/filemanager/tp/fckeditor/editor/_source/internals/fckscayt.js @ 1575

Revision 1575, 10.8 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

  • Property svn:executable set to *
Line 
1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2009 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 */
21
22var FCKScayt;
23
24(function()
25{
26        var scaytOnLoad = [] ;
27        var isEngineLoaded = ( FCK && FCK.EditorWindow && FCK.EditorWindow.parent.parent.scayt)
28                                                ? true : false ;
29        var scaytEnable = false;
30        var scaytReady  = false;
31
32        function ScaytEngineLoad( callback )
33        {
34                if ( isEngineLoaded )
35                        return ;
36
37                isEngineLoaded = true ;
38                var top = FCK.EditorWindow.parent.parent;
39
40                var init = function ()
41                {
42                        window.scayt = top.scayt ;
43                        InitScayt() ;
44                        var ScaytCombobox =  FCKToolbarItems.LoadedItems[ 'ScaytCombobox' ] ;
45                        ScaytCombobox && ScaytCombobox.SetEnabled( scyt_control && scyt_control.disabled ) ;
46                        InitSetup() ;
47                };
48
49                if ( top.scayt )
50                {
51                        init() ;
52                        return ;
53                }
54
55                // Compose the scayt url.
56                if (FCK.Config.ScaytCustomUrl)
57                        FCK.Config.ScaytCustomUrl = new String(FCK.Config.ScaytCustomUrl).replace( new RegExp( "^http[s]*:\/\/"),"") ;
58
59                var protocol    = document.location.protocol ;
60                var baseUrl             = FCK.Config.ScaytCustomUrl ||'svc.spellchecker.net/spellcheck3/lf/scayt/scayt4.js' ;
61                var scaytUrl    = protocol + '//' + baseUrl ;
62                var scaytConfigBaseUrl =  ParseUrl( scaytUrl ).path +  '/' ;
63
64                // SCAYT is targetted to CKEditor, so we need this trick to make it work here.
65                var CKEDITOR = top.window.CKEDITOR || ( top.window.CKEDITOR = {} ) ;
66                CKEDITOR._djScaytConfig =
67                {
68                        baseUrl : scaytConfigBaseUrl,
69                        addOnLoad : function()
70                        {
71                                init();
72                        },
73                        isDebug : false
74                };
75
76
77                if ( callback )
78                        scaytOnLoad.push( callback ) ;
79
80                DoLoadScript( scaytUrl ) ;
81        }
82
83        /**
84         * DoLoadScript - load scripts with dinamic tag script creating
85         * @param string url
86         */
87        function DoLoadScript( url )
88        {
89                if (!url)
90            return false ;
91                var top = FCK.EditorWindow.parent.parent;
92        var s = top.document.createElement('script') ;
93        s.type = 'text/javascript' ;
94        s.src = url ;
95        top.document.getElementsByTagName('head')[0].appendChild(s) ;
96
97        return true ;
98        }
99
100        function ParseUrl( data )
101        {
102                var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/) ;
103                return m ? { path: m[1], file: m[2] } : data ;
104        }
105
106        function createScaytControl ()
107        {
108                // Get public scayt params.
109                var oParams = {} ;
110                var top = FCK.EditorWindow.parent.parent;
111                oParams.srcNodeRef                              = FCK.EditingArea.IFrame;               // Get the iframe.
112                // syntax : AppName.AppVersion@AppRevision
113                //oParams.assocApp  = "FCKEDITOR." + FCKeditorAPI.Varsion + "@" + FCKeditorAPI.VersionBuild;
114                oParams.customerid                              = FCK.Config.ScaytCustomerid ;
115                oParams.customDictionaryName    = FCK.Config.ScaytCustomDictionaryName ;
116                oParams.userDictionaryName              = FCK.Config.ScaytUserDictionaryName ;
117                oParams.defLang                                 = FCK.Config.ScaytDefLang ;
118
119                var scayt = top.scayt;
120                var scayt_control = window.scayt_control = new scayt( oParams ) ;
121        }
122
123        function InitScayt()
124        {
125                createScaytControl();
126               
127                var scayt_control = window.scayt_control ;
128
129                if ( scayt_control )
130                {
131                        scayt_control.setDisabled( false ) ;
132                        scaytReady = true;
133                        scaytEnable = !scayt_control.disabled ;
134
135                        // set default scayt status
136                        var ScaytCombobox = FCKToolbarItems.LoadedItems[ 'ScaytCombobox' ] ;
137                        ScaytCombobox && ScaytCombobox.Enable() ;
138                        ShowScaytState() ;
139                }
140
141                for ( var i = 0 ; i < scaytOnLoad.length ; i++ )
142                {
143                        try
144                        {
145                                scaytOnLoad[i].call( this ) ;
146                        }
147                        catch(err)
148                        {}
149                }
150        }
151
152        // ###
153        // SCAYT command class.
154        var ScaytCommand  = function()
155        {
156                name = 'Scayt' ;
157        }
158
159        ScaytCommand.prototype.Execute = function( action )
160        {
161                switch ( action )
162                {
163                        case 'Options' :
164                        case 'Langs' :
165                        case 'About' :
166                                if ( isEngineLoaded && scaytReady && !scaytEnable )
167                                {
168                                        ScaytMessage( 'SCAYT is not enabled' );
169                                        break;
170                                }
171
172                                if ( isEngineLoaded && scaytReady )
173                                        FCKDialog.OpenDialog( 'Scayt', 'SCAYT Settings', 'dialog/fck_scayt.html?' + action.toLowerCase(), 343, 343 );
174                                break;
175
176                        default :
177                                if ( !isEngineLoaded )
178                                {
179                                        var me = this;
180                                        ScaytEngineLoad( function ()
181                                                {
182                                                        me.SetEnabled( !window.scayt_control.disabled ) ;
183                                                }) ;
184
185                                        return true;
186                                }
187                                else if ( scaytReady )
188                                {
189                                        // Switch the current scayt state.
190                                        if ( scaytEnable )
191                                                this.Disable() ;
192                                        else
193                                                this.Enable() ;
194
195                                        ShowScaytState() ;
196                                }
197
198                }
199
200                if ( !isEngineLoaded )
201                        return ScaytMessage( 'SCAYT is not loaded' ) || false;
202
203                if ( !scaytReady )
204                        return ScaytMessage( 'SCAYT is not ready' ) || false;
205
206
207                return true;
208        }
209
210        ScaytCommand.prototype.Enable = function()
211        {
212                window.scayt_control.setDisabled( false ) ;
213                scaytEnable = true;
214        }
215
216        ScaytCommand.prototype.Disable = function()
217        {
218                window.scayt_control.setDisabled( true ) ;
219                scaytEnable = false;
220        }
221
222        ScaytCommand.prototype.SetEnabled = function( state )
223        {
224                if ( state )
225                        this.Enable() ;
226                else
227                        this.Disable() ;
228
229                ShowScaytState() ;
230                return true;
231        }
232
233        ScaytCommand.prototype.GetState = function()
234        {
235                return FCK_TRISTATE_OFF;
236        }
237
238        function ShowScaytState()
239        {
240                var combo = FCKToolbarItems.GetItem( 'SpellCheck' ) ;
241
242                if ( !combo || !combo._Combo || !combo._Combo._OuterTable )
243                        return;
244
245                var bItem = combo._Combo._OuterTable.getElementsByTagName( 'img' )[1] ;
246                var dNode = combo._Combo.Items['trigger'] ;
247
248                if ( scaytEnable )
249                {
250                        bItem.style.opacity = '1' ;
251                        dNode.innerHTML = GetStatusLabel() ;
252                }
253                else
254                {
255                        bItem.style.opacity = '0.5' ;
256                        dNode.innerHTML = GetStatusLabel() ;
257                }
258        }
259
260        function GetStatusLabel()
261        {
262                if ( !scaytReady )
263                        return  '<b>Enable SCAYT</b>' ;
264
265                return scaytEnable ? '<b>Disable SCAYT</b>' : '<b>Enable SCAYT</b>' ;
266        }
267
268        // ###
269        // Class for the toolbar item.
270        var ToolbarScaytComboBox = function( tooltip, style )
271        {
272                this.Command = FCKCommands.GetCommand( 'Scayt' ) ;
273                this.CommandName = 'Scayt' ;
274                this.Label = this.GetLabel() ;
275                this.Tooltip = FCKLang.ScaytTitle ;
276                this.Style = FCK_TOOLBARITEM_ONLYTEXT ; //FCK_TOOLBARITEM_ICONTEXT OR FCK_TOOLBARITEM_ONLYTEXT
277        }
278
279        ToolbarScaytComboBox.prototype = new FCKToolbarSpecialCombo ;
280
281        //Add the items to the combo list
282        ToolbarScaytComboBox.prototype.CreateItems = function()
283        {
284                this._Combo.AddItem( 'Trigger', '<b>Enable SCAYT</b>' );
285                this._Combo.AddItem( 'Options', FCKLang.ScaytTitleOptions || "Options"  );
286                this._Combo.AddItem( 'Langs', FCKLang.ScaytTitleLangs || "Languages");
287                this._Combo.AddItem( 'About', FCKLang.ScaytTitleAbout || "About");
288        }
289
290        // Label shown in the toolbar.
291        ToolbarScaytComboBox.prototype.GetLabel = function()
292        {
293                var strip = FCKConfig.SkinPath + 'fck_strip.gif';
294
295                return FCKBrowserInfo.IsIE ?
296                                '<div class="TB_Button_Image"><img src="' + strip + '" style="top:-192px"></div>'
297                        :
298                                '<img class="TB_Button_Image" src="' + FCK_SPACER_PATH + '" style="background-position: 0px -192px;background-image: url(' + strip + ');">';
299        }
300
301        function ScaytMessage( m )
302        {
303                m && alert( m ) ;
304        }
305
306        var ScaytContextCommand = function()
307        {
308                name = 'ScaytContext' ;
309        }
310
311        ScaytContextCommand.prototype.Execute = function( contextInfo )
312        {
313                var action = contextInfo && contextInfo.action,
314                        node = action && contextInfo.node,
315                        scayt_control = window.scayt_control;
316
317                if ( node )
318                {
319                        switch ( action )
320                        {
321                                case 'Suggestion' :
322                                        scayt_control.replace( node, contextInfo.suggestion ) ;
323                                        break ;
324                                case 'Ignore' :
325                                        scayt_control.ignore( node ) ;
326                                        break ;
327                                case 'Ignore All' :
328                                        scayt_control.ignoreAll( node ) ;
329                                        break ;
330                                case 'Add Word' :
331                                        var top = FCK.EditorWindow.parent.parent ;
332                                        top.scayt.addWordToUserDictionary( node ) ;
333                                        break ;
334                        }
335                }
336        }
337
338        // Register context menu listeners.
339        function InitSetup()
340        {
341                FCK.ContextMenu.RegisterListener(
342                        {
343                                AddItems : function( menu )
344                                {
345                                        var top = FCK.EditorWindow.parent.parent;
346
347                                        var scayt_control = window.scayt_control,
348                                                scayt = top.scayt;
349
350                                        if ( !scayt_control )
351                                                return;
352
353                                        var node = scayt_control.getScaytNode() ;
354
355                                        if ( !node )
356                                                return;
357
358                                        var suggestions = scayt.getSuggestion( scayt_control.getWord( node ), scayt_control.getLang() ) ;
359
360                                        if ( !suggestions || !suggestions.length )
361                                                return;
362
363                                        menu.AddSeparator() ;
364
365                                        var maxSuggestions = FCK.Config.ScaytMaxSuggestions || 5 ;
366                                        var suggAveCount = ( maxSuggestions == -1 ) ? suggestions.length : maxSuggestions ;
367
368                                        for ( var i = 0 ; i < suggAveCount ; i += 1 )
369                                        {
370                                                if ( suggestions[i] )
371                                                {
372                                                        menu.AddItem( 'ScaytContext', suggestions[i], null, false, {
373                                                                'action' : 'Suggestion',
374                                                                'node' : node,
375                                                                'suggestion' : suggestions[i] } ) ;
376                                                }
377                                        }
378
379                                        menu.AddSeparator() ;
380
381                                        menu.AddItem( 'ScaytContext', 'Ignore', null, false, { 'action' : 'Ignore', 'node' : node } );
382                                        menu.AddItem( 'ScaytContext', 'Ignore All', null, false, { 'action' : 'Ignore All', 'node' : node } );
383                                        menu.AddItem( 'ScaytContext', 'Add Word', null, false, { 'action' : 'Add Word', 'node' : node } );
384                                        try
385                                        {
386                                                if (scaytReady && scaytEnable)
387                                                        scayt_control.fireOnContextMenu( null, FCK.ContextMenu._InnerContextMenu);
388
389                                        }
390                                        catch( err ) {}
391                                }
392                        }) ;
393
394                FCK.Events.AttachEvent( 'OnPaste', function()
395                        {
396                                        window.scayt_control.refresh() ;
397                                        return true;
398                        } ) ;
399        }
400
401        // ##
402        // Register event listeners.
403
404        FCK.Events.AttachEvent( 'OnAfterSetHTML', function()
405                {
406                        if ( FCKConfig.SpellChecker == 'SCAYT' )
407                        {
408                                if ( !isEngineLoaded && FCK.Config.ScaytAutoStartup )
409                                        ScaytEngineLoad() ;
410
411                                if ( FCK.EditMode == FCK_EDITMODE_WYSIWYG && isEngineLoaded && scaytReady )
412                                        createScaytControl();
413
414                                ShowScaytState() ;
415                        }
416                } ) ;
417
418        FCK.Events.AttachEvent( 'OnBeforeGetData', function()
419                {
420                        scaytReady && window.scayt_control.reset();
421                } ) ;
422
423        FCK.Events.AttachEvent( 'OnAfterGetData', function()
424                {
425                        scaytReady && window.scayt_control.refresh();
426                } ) ;
427
428        // ###
429        // The main object that holds the SCAYT interaction in the code.
430        FCKScayt =
431        {
432                CreateCommand : function()
433                {
434                        return new ScaytCommand();
435                },
436
437                CreateContextCommand : function()
438                {
439                        return new ScaytContextCommand();
440                },
441
442                CreateToolbarItem : function()
443                {
444                        return new ToolbarScaytComboBox() ;
445                }
446        } ;
447})() ;
Note: See TracBrowser for help on using the repository browser.