JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / plugins / scayt / plugin.js
index bb0df05..718aee6 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -11,7 +11,23 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 (function()\r
 {\r
        var commandName         = 'scaytcheck',\r
-               openPage                = '';\r
+               openPage                = '',\r
+               scayt_paused    = null;\r
+\r
+       // Checks if a value exists in an array\r
+       function in_array(needle, haystack)\r
+       {\r
+               var found = false, key;\r
+               for (key in haystack)\r
+               {\r
+                       if ((haystack[key] === needle) || ( haystack[key] == needle))\r
+                       {\r
+                               found = true;\r
+                               break;\r
+                       }\r
+               }\r
+               return found;\r
+       }\r
 \r
        var onEngineLoad = function()\r
        {\r
@@ -24,11 +40,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        oParams.srcNodeRef = editor.document.getWindow().$.frameElement;                // Get the iframe.\r
                        // syntax : AppName.AppVersion@AppRevision\r
                        oParams.assocApp  = "CKEDITOR." + CKEDITOR.version + "@" + CKEDITOR.revision;\r
-\r
-                       oParams.customerid = editor.config.scayt_customerid  || "1:11111111111111111111111111111111111111";\r
-                       oParams.customDictionaryName = editor.config.scayt_customDictionaryName;\r
+                       oParams.customerid = editor.config.scayt_customerid  || "1:WvF0D4-UtPqN1-43nkD4-NKvUm2-daQqk3-LmNiI-z7Ysb4-mwry24-T8YrS3-Q2tpq2";\r
+                       oParams.customDictionaryIds = editor.config.scayt_customDictionaryIds;\r
                        oParams.userDictionaryName = editor.config.scayt_userDictionaryName;\r
-                       oParams.defLang = editor.scayt_defLang;\r
+                       oParams.sLang = editor.config.scayt_sLang || "en_US";\r
 \r
                        if ( CKEDITOR._scaytParams )\r
                        {\r
@@ -51,9 +66,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        plugin.instances[ editor.name ] = scayt_control;\r
 \r
+                       //window.scayt.uiTags\r
+                       var menuGroup = 'scaytButton';\r
+                       var uiTabs = window.scayt.uiTags;\r
+                       var fTabs  = [];\r
+\r
+                       for (var i = 0,l=4; i<l; i++)\r
+                               fTabs.push( uiTabs[i] && plugin.uiTabs[i] );\r
+\r
+                       plugin.uiTabs = fTabs;\r
                        try {\r
-                               scayt_control.setDisabled( scayt_control.paused === false );                            // I really don't know why it causes JS error in IE\r
+                               scayt_control.setDisabled( scayt_paused === false );\r
                        } catch (e) {}\r
+\r
                        editor.fire( 'showScaytState' );\r
                };\r
 \r
@@ -83,13 +108,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        var scayt_instanse = plugin.getScayt( editor );\r
                                        if ( scayt_instanse )\r
                                        {\r
-                                               scayt_instanse.paused = !scayt_instanse.disabled;\r
+                                               scayt_paused = scayt_instanse.paused = !scayt_instanse.disabled;\r
                                                scayt_instanse.destroy();\r
                                                delete plugin.instances[ editor.name ];\r
                                        }\r
                                }\r
                        });\r
 \r
+\r
+               editor.on( 'destroy', function()\r
+                       {\r
+                               plugin.getScayt( editor ).destroy();\r
+                       });\r
                // Listen to data manipulation to reflect scayt markup.\r
                editor.on( 'afterSetData', function()\r
                        {\r
@@ -116,6 +146,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }\r
                        }, this, null, 50 );\r
 \r
+               editor.on( 'insertHtml', function()\r
+                       {\r
+\r
+                               var scayt_instance = plugin.getScayt( editor );\r
+                               if ( plugin.isScaytEnabled( editor ) )\r
+                               {\r
+                                       // Unlock the selection before reload, SCAYT will take\r
+                                       // care selection update.\r
+                                       if ( CKEDITOR.env.ie )\r
+                                               editor.getSelection().unlock( true );\r
+\r
+                                       // Swallow any SCAYT engine errors.\r
+                                       try{\r
+                                               scayt_instance.refresh();\r
+                                       }catch( er )\r
+                                       {}\r
+                               }\r
+                       }, this, null, 50 );\r
+\r
                editor.on( 'scaytDialog', function( ev )        // Communication with dialog.\r
                        {\r
                                ev.data.djConfig = window.djConfig;\r
@@ -189,7 +238,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        var protocol = document.location.protocol;\r
                        // Default to 'http' for unknown.\r
                        protocol = protocol.search( /https?:/) != -1? protocol : 'http:';\r
-                       var baseUrl  = "svc.spellchecker.net/spellcheck/lf/scayt/scayt1.js";\r
+                       var baseUrl  = "svc.spellchecker.net/spellcheck3/lf/scayt/scayt21.js";\r
 \r
                        var scaytUrl  =  editor.config.scayt_srcUrl || ( protocol + "//" + baseUrl );\r
                        var scaytConfigBaseUrl =  plugin.parseUrl( scaytUrl ).path +  "/";\r
@@ -300,51 +349,81 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        // Add Options dialog.\r
                        CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );\r
+                       // read ui tags\r
+                       var confuiTabs = editor.config.scayt_uiTabs || "1,1,1";\r
+                       var uiTabs =[];\r
+                       // string tp array convert\r
+                       confuiTabs = confuiTabs.split(",");\r
+                       // check array length ! allwaays must be 3 filled with 1 or 0\r
+                       for (var i=0,l=3; i<l; i++){\r
+                               var flag = parseInt(confuiTabs[i] || "1" ,10);\r
+                               uiTabs.push(  flag  );\r
+                       }\r
 \r
                        var menuGroup = 'scaytButton';\r
                        editor.addMenuGroup( menuGroup );\r
-                       editor.addMenuItems(\r
+                       // combine menu items to render\r
+                       var uiMuneItems = {};\r
+\r
+                       // allways added\r
+                       uiMuneItems.scaytToggle =\r
                                {\r
-                                       scaytToggle :\r
-                                       {\r
-                                               label : editor.lang.scayt.enable,\r
-                                               command : commandName,\r
-                                               group : menuGroup\r
-                                       },\r
+                                       label : editor.lang.scayt.enable,\r
+                                       command : commandName,\r
+                                       group : menuGroup\r
+                               };\r
 \r
-                                       scaytOptions :\r
+                       if (uiTabs[0] == 1)\r
+                               uiMuneItems.scaytOptions =\r
+                               {\r
+                                       label : editor.lang.scayt.options,\r
+                                       group : menuGroup,\r
+                                       onClick : function()\r
                                        {\r
-                                               label : editor.lang.scayt.options,\r
-                                               group : menuGroup,\r
-                                               onClick : function()\r
-                                               {\r
-                                                       openPage = 'options';\r
-                                                       editor.openDialog( commandName );\r
-                                               }\r
-                                       },\r
+                                               openPage = 'options';\r
+                                               editor.openDialog( commandName );\r
+                                       }\r
+                               };\r
 \r
-                                       scaytLangs :\r
+                       if (uiTabs[1] == 1)\r
+                               uiMuneItems.scaytLangs =\r
+                               {\r
+                                       label : editor.lang.scayt.langs,\r
+                                       group : menuGroup,\r
+                                       onClick : function()\r
                                        {\r
-                                               label : editor.lang.scayt.langs,\r
-                                               group : menuGroup,\r
-                                               onClick : function()\r
-                                               {\r
-                                                       openPage = 'langs';\r
-                                                       editor.openDialog( commandName );\r
-                                               }\r
-                                       },\r
-\r
-                                       scaytAbout :\r
+                                               openPage = 'langs';\r
+                                               editor.openDialog( commandName );\r
+                                       }\r
+                               };\r
+                       if (uiTabs[2] == 1)\r
+                               uiMuneItems.scaytDict =\r
+                               {\r
+                                       label : editor.lang.scayt.dictionariesTab,\r
+                                       group : menuGroup,\r
+                                       onClick : function()\r
                                        {\r
-                                               label : editor.lang.scayt.about,\r
-                                               group : menuGroup,\r
-                                               onClick : function()\r
-                                               {\r
-                                                       openPage = 'about';\r
-                                                       editor.openDialog( commandName );\r
-                                               }\r
+                                               openPage = 'dictionaries';\r
+                                               editor.openDialog( commandName );\r
                                        }\r
-                               });\r
+                               };\r
+                       // allways added\r
+                       uiMuneItems.scaytAbout =\r
+                               {\r
+                                       label : editor.lang.scayt.about,\r
+                                       group : menuGroup,\r
+                                       onClick : function()\r
+                                       {\r
+                                               openPage = 'about';\r
+                                               editor.openDialog( commandName );\r
+                                       }\r
+                               }\r
+                       ;\r
+\r
+                       uiTabs[3] = 1; // about us tab is allways on\r
+                       plugin.uiTabs = uiTabs;\r
+\r
+                       editor.addMenuItems( uiMuneItems );\r
 \r
                                editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,\r
                                        {\r
@@ -353,23 +432,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                className : 'cke_button_scayt',\r
                                                onRender: function()\r
                                                {\r
-                                               command.on( 'state', function()\r
+                                                       command.on( 'state', function()\r
                                                        {\r
                                                                this.setState( command.state );\r
                                                        },\r
                                                        this);\r
-                                       },\r
-                                       onMenu : function()\r
-                                       {\r
-                                               var isEnabled = plugin.isScaytEnabled( editor );\r
+                                               },\r
+                                               onMenu : function()\r
+                                               {\r
+                                                       var isEnabled = plugin.isScaytEnabled( editor );\r
 \r
-                                               editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];\r
+                                                       editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];\r
 \r
                                                        return {\r
-                                                               scaytToggle : CKEDITOR.TRISTATE_OFF,\r
-                                                               scaytOptions : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
-                                                               scaytLangs : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
-                                                               scaytAbout : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
+                                                               scaytToggle  : CKEDITOR.TRISTATE_OFF,\r
+                                                               scaytOptions : isEnabled && plugin.uiTabs[0] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
+                                                               scaytLangs   : isEnabled && plugin.uiTabs[1] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
+                                                               scaytDict    : isEnabled && plugin.uiTabs[2] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
+                                                               scaytAbout   : isEnabled && plugin.uiTabs[3] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
                                                        };\r
                                                }\r
                                        });\r
@@ -407,7 +487,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                moreSuggestions = {};           // Reset items.\r
                                                mainSuggestions = {};\r
 \r
-                                               var moreSuggestionsUnable = false;\r
+                                               var moreSuggestionsUnable = editor.config.scayt_moreSuggestions || "on";\r
+                                               var moreSuggestionsUnableAdded = false;\r
+\r
+                                               var maxSuggestions = editor.config.scayt_maxSuggestions;\r
+                                               ( typeof maxSuggestions != 'number' ) && ( maxSuggestions = 5 );\r
+                                               !maxSuggestions && ( maxSuggestions = items_suggestion.length );\r
+\r
+                                               var contextCommands = editor.config.scayt_contextCommands || "all";\r
+                                               contextCommands = contextCommands.split("|");\r
 \r
                                                for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )\r
                                                {\r
@@ -422,68 +510,70 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        };\r
                                                                })( element.$, items_suggestion[i] );\r
 \r
-                                                       if ( i < editor.config.scayt_maxSuggestions )\r
+                                                       if ( i < maxSuggestions )\r
                                                        {\r
                                                                addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],\r
                                                                        commandName, exec, 'scayt_suggest', i + 1 );\r
                                                                _r[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
                                                                mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
                                                        }\r
-                                                       else\r
+                                                       else if ( moreSuggestionsUnable == "on" )\r
                                                        {\r
                                                                addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],\r
                                                                        commandName, exec, 'scayt_moresuggest', i + 1 );\r
                                                                moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
-                                                               moreSuggestionsUnable = true;\r
+                                                               moreSuggestionsUnableAdded = true;\r
                                                        }\r
                                                }\r
-                                               if ( moreSuggestionsUnable )\r
+\r
+                                               if ( moreSuggestionsUnableAdded ){\r
                                                        // Rgister the More suggestions group;\r
                                                        editor.addMenuItem( 'scayt_moresuggest',\r
+                                                       {\r
+                                                               label : editor.lang.scayt.moreSuggestions,\r
+                                                               group : 'scayt_moresuggest',\r
+                                                               order : 10,\r
+                                                               getItems : function()\r
                                                                {\r
-                                                                       label : editor.lang.scayt.moreSuggestions,\r
-                                                                       group : 'scayt_moresuggest',\r
-                                                                       order : 10,\r
-                                                                       getItems : function()\r
-                                                                       {\r
-                                                                               return moreSuggestions;\r
-                                                                       }\r
-                                                               });\r
+                                                                       return moreSuggestions;\r
+                                                               }\r
+                                                       });\r
+                                                       mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;\r
 \r
+                                               }\r
 \r
-                                               var ignore_command =\r
-                                               {\r
-                                                       exec: function()\r
-                                                       {\r
-                                                               scayt_control.ignore( element.$ );\r
-                                                       }\r
-                                               };\r
-                                               var ignore_all_command =\r
-                                               {\r
-                                                       exec: function()\r
-                                                       {\r
-                                                               scayt_control.ignoreAll( element.$ );\r
-                                                       }\r
-                                               };\r
-                                               var addword_command =\r
+                                               if ( in_array( "all",contextCommands )  || in_array("ignore",contextCommands)  )\r
                                                {\r
-                                                       exec: function()\r
-                                                       {\r
-                                                               window.scayt.addWordToUserDictionary( element.$ );\r
-                                                       }\r
-                                               };\r
+                                                       var ignore_command = {\r
+                                                               exec: function(){\r
+                                                                       scayt_control.ignore(element.$);\r
+                                                               }\r
+                                                       };\r
+                                                       addButtonCommand(editor, 'ignore', editor.lang.scayt.ignore, 'scayt_ignore', ignore_command, 'scayt_control', 1);\r
+                                                       mainSuggestions['scayt_ignore'] = CKEDITOR.TRISTATE_OFF;\r
+                                               }\r
 \r
-                                               addButtonCommand( editor, 'ignore', editor.lang.scayt.ignore,\r
-                                                       'scayt_ignore', ignore_command, 'scayt_control', 1);\r
-                                               addButtonCommand( editor, 'ignore_all', editor.lang.scayt.ignoreAll,\r
-                                                       'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);\r
-                                               addButtonCommand( editor, 'add_word', editor.lang.scayt.addWord,\r
-                                                       'scayt_add_word', addword_command, 'scayt_control', 3);\r
+                                               if ( in_array( "all",contextCommands )  || in_array("ignoreall",contextCommands)  )\r
+                                               {\r
+                                                       var ignore_all_command = {\r
+                                                               exec: function(){\r
+                                                                       scayt_control.ignoreAll(element.$);\r
+                                                               }\r
+                                                       };\r
+                                                       addButtonCommand(editor, 'ignore_all', editor.lang.scayt.ignoreAll, 'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);\r
+                                                       mainSuggestions['scayt_ignore_all'] = CKEDITOR.TRISTATE_OFF;\r
+                                               }\r
 \r
-                                               mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;\r
-                                               mainSuggestions[ 'scayt_ignore' ] = CKEDITOR.TRISTATE_OFF;\r
-                                               mainSuggestions[ 'scayt_ignore_all' ] = CKEDITOR.TRISTATE_OFF;\r
-                                               mainSuggestions[ 'scayt_add_word' ] = CKEDITOR.TRISTATE_OFF;\r
+                                               if ( in_array( "all",contextCommands )  || in_array("add",contextCommands)  )\r
+                                               {\r
+                                                       var addword_command = {\r
+                                                               exec: function(){\r
+                                                                       window.scayt.addWordToUserDictionary(element.$);\r
+                                                               }\r
+                                                       };\r
+                                                       addButtonCommand(editor, 'add_word', editor.lang.scayt.addWord, 'scayt_add_word', addword_command, 'scayt_control', 3);\r
+                                                       mainSuggestions['scayt_add_word'] = CKEDITOR.TRISTATE_OFF;\r
+                                               }\r
 \r
                                                if ( scayt_control.fireOnContextMenu )\r
                                                        scayt_control.fireOnContextMenu( editor );\r
@@ -508,5 +598,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        });\r
 })();\r
 \r
-CKEDITOR.config.scayt_maxSuggestions =  5;\r
-CKEDITOR.config.scayt_autoStartup = false;\r
+// TODO: Documentation\r
+// CKEDITOR.config.scayt_maxSuggestions\r
+// CKEDITOR.config.scayt_autoStartup\r