JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
653c755a81429634b7ed8c7d64aeb455e102191b
[ckeditor.git] / _source / plugins / scayt / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Spell Check As You Type (SCAYT).\r
8  * Button name : Scayt.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         var commandName  = 'scaytcheck',\r
14                 openPage = '';\r
15 \r
16         // Checks if a value exists in an array\r
17         function in_array( needle, haystack )\r
18         {\r
19                 var found = 0,\r
20                         key;\r
21                 for ( key in haystack )\r
22                 {\r
23                         if ( haystack[ key ] == needle )\r
24                         {\r
25                                 found = 1;\r
26                                 break;\r
27                         }\r
28                 }\r
29                 return found;\r
30         }\r
31 \r
32         var onEngineLoad = function()\r
33         {\r
34                 var editor = this;\r
35 \r
36                 var createInstance = function() // Create new instance every time Document is created.\r
37                 {\r
38                         var config = editor.config;\r
39                         // Initialise Scayt instance.\r
40                         var oParams = {};\r
41                         // Get the iframe.\r
42                         oParams.srcNodeRef = editor.document.getWindow().$.frameElement;\r
43                         // syntax : AppName.AppVersion@AppRevision\r
44                         oParams.assocApp  = 'CKEDITOR.' + CKEDITOR.version + '@' + CKEDITOR.revision;\r
45                         oParams.customerid = config.scayt_customerid  || '1:WvF0D4-UtPqN1-43nkD4-NKvUm2-daQqk3-LmNiI-z7Ysb4-mwry24-T8YrS3-Q2tpq2';\r
46                         oParams.customDictionaryIds = config.scayt_customDictionaryIds || '';\r
47                         oParams.userDictionaryName = config.scayt_userDictionaryName || '';\r
48                         oParams.sLang = config.scayt_sLang || 'en_US';\r
49 \r
50                         // Introduce SCAYT onLoad callback. (#5632)\r
51                         oParams.onLoad = function()\r
52                                 {\r
53                                         // Draw down word marker to avoid being covered by background-color style.(#5466)\r
54                                         if ( !( CKEDITOR.env.ie && CKEDITOR.env.version < 8 ) )\r
55                                                 this.addStyle( this.selectorCss(), 'padding-bottom: 2px !important;' );\r
56 \r
57                                         // Call scayt_control.focus when SCAYT loaded\r
58                                         // and only if editor has focus and scayt control creates at first time (#5720)\r
59                                         if ( editor.focusManager.hasFocus && !plugin.isControlRestored( editor ) )\r
60                                                 this.focus();\r
61 \r
62                                 };\r
63 \r
64                         oParams.onBeforeChange = function()\r
65                         {\r
66                                 if ( plugin.getScayt( editor ) && !editor.checkDirty() )\r
67                                         setTimeout( function(){ editor.resetDirty(); }, 0 );\r
68                         };\r
69 \r
70                         var scayt_custom_params = window.scayt_custom_params;\r
71                         if ( typeof scayt_custom_params == 'object' )\r
72                         {\r
73                                 for ( var k in scayt_custom_params )\r
74                                         oParams[ k ] = scayt_custom_params[ k ];\r
75                         }\r
76                         // needs for restoring a specific scayt control settings\r
77                         if ( plugin.getControlId( editor ) )\r
78                                 oParams.id = plugin.getControlId( editor );\r
79 \r
80                         var scayt_control = new window.scayt( oParams );\r
81 \r
82                         scayt_control.afterMarkupRemove.push( function( node )\r
83                         {\r
84                                 ( new CKEDITOR.dom.element( node, scayt_control.document ) ).mergeSiblings();\r
85                         } );\r
86 \r
87                         // Copy config.\r
88                         var lastInstance = plugin.instances[ editor.name ];\r
89                         if ( lastInstance )\r
90                         {\r
91                                 scayt_control.sLang = lastInstance.sLang;\r
92                                 scayt_control.option( lastInstance.option() );\r
93                                 scayt_control.paused = lastInstance.paused;\r
94                         }\r
95 \r
96                         plugin.instances[ editor.name ] = scayt_control;\r
97 \r
98                         //window.scayt.uiTags\r
99                         var menuGroup = 'scaytButton';\r
100                         var uiTabs = window.scayt.uiTags;\r
101                         var fTabs  = [];\r
102 \r
103                         for ( var i = 0, l=4; i < l; i++ )\r
104                             fTabs.push( uiTabs[i] && plugin.uiTabs[i] );\r
105 \r
106                         plugin.uiTabs = fTabs;\r
107                         try {\r
108                                 scayt_control.setDisabled( plugin.isPaused( editor ) === false );\r
109                         } catch (e) {}\r
110 \r
111                         editor.fire( 'showScaytState' );\r
112                 };\r
113 \r
114                 editor.on( 'contentDom', createInstance );\r
115                 editor.on( 'contentDomUnload', function()\r
116                         {\r
117                                 // Remove scripts.\r
118                                 var scripts = CKEDITOR.document.getElementsByTag( 'script' ),\r
119                                         scaytIdRegex =  /^dojoIoScript(\d+)$/i,\r
120                                         scaytSrcRegex =  /^https?:\/\/svc\.spellchecker\.net\/spellcheck\/script\/ssrv\.cgi/i;\r
121 \r
122                                 for ( var i=0; i < scripts.count(); i++ )\r
123                                 {\r
124                                         var script = scripts.getItem( i ),\r
125                                                 id = script.getId(),\r
126                                                 src = script.getAttribute( 'src' );\r
127 \r
128                                         if ( id && src && id.match( scaytIdRegex ) && src.match( scaytSrcRegex ))\r
129                                                 script.remove();\r
130                                 }\r
131                         });\r
132 \r
133                 editor.on( 'beforeCommandExec', function( ev )          // Disable SCAYT before Source command execution.\r
134                         {\r
135                                 if ( ( ev.data.name == 'source' || ev.data.name == 'newpage' ) && editor.mode == 'wysiwyg' )\r
136                                 {\r
137                                         var scayt_instance = plugin.getScayt( editor );\r
138                                         if ( scayt_instance )\r
139                                         {\r
140                                                 plugin.setPaused( editor, !scayt_instance.disabled );\r
141                                                 // store a control id for restore a specific scayt control settings\r
142                                                 plugin.setControlId( editor, scayt_instance.id );\r
143                                                 scayt_instance.destroy( true );\r
144                                                 delete plugin.instances[ editor.name ];\r
145                                         }\r
146                                 }\r
147                                 // Catch on source mode switch off (#5720)\r
148                                 else if ( ev.data.name == 'source'  && editor.mode == 'source' )\r
149                                         plugin.markControlRestore( editor );\r
150                         });\r
151 \r
152                 editor.on( 'afterCommandExec', function( ev )\r
153                         {\r
154                                 if ( !plugin.isScaytEnabled( editor ) )\r
155                                         return;\r
156 \r
157                                 if ( editor.mode == 'wysiwyg' && ( ev.data.name == 'undo' || ev.data.name == 'redo' ) )\r
158                                         window.setTimeout( function() { plugin.getScayt( editor ).refresh(); }, 10 );\r
159                         });\r
160 \r
161                 editor.on( 'destroy', function( ev )\r
162                         {\r
163                                 var editor = ev.editor,\r
164                                         scayt_instance = plugin.getScayt( editor );\r
165 \r
166                                 // SCAYT instance might already get destroyed by mode switch (#5744).\r
167                                 if ( !scayt_instance )\r
168                                         return;\r
169 \r
170                                 delete plugin.instances[ editor.name ];\r
171                                 // store a control id for restore a specific scayt control settings\r
172                                 plugin.setControlId( editor, scayt_instance.id );\r
173                                 scayt_instance.destroy( true );\r
174                         });\r
175 \r
176                 // Listen to data manipulation to reflect scayt markup.\r
177                 editor.on( 'afterSetData', function()\r
178                         {\r
179                                 if ( plugin.isScaytEnabled( editor ) ) {\r
180                                         window.setTimeout( function()\r
181                                                 {\r
182                                                         var instance = plugin.getScayt( editor );\r
183                                                         instance && instance.refresh();\r
184                                                 }, 10 );\r
185                                 }\r
186                         });\r
187 \r
188                 // Reload spell-checking for current word after insertion completed.\r
189                 editor.on( 'insertElement', function()\r
190                         {\r
191                                 var scayt_instance = plugin.getScayt( editor );\r
192                                 if ( plugin.isScaytEnabled( editor ) )\r
193                                 {\r
194                                         // Unlock the selection before reload, SCAYT will take\r
195                                         // care selection update.\r
196                                         if ( CKEDITOR.env.ie )\r
197                                                 editor.getSelection().unlock( true );\r
198 \r
199                                         // Return focus to the editor and refresh SCAYT markup (#5573).\r
200                                         window.setTimeout( function()\r
201                                         {\r
202                                                 scayt_instance.focus();\r
203                                                 scayt_instance.refresh();\r
204                                         }, 10 );\r
205                                 }\r
206                         }, this, null, 50 );\r
207 \r
208                 editor.on( 'insertHtml', function()\r
209                         {\r
210                                 var scayt_instance = plugin.getScayt( editor );\r
211                                 if ( plugin.isScaytEnabled( editor ) )\r
212                                 {\r
213                                         // Unlock the selection before reload, SCAYT will take\r
214                                         // care selection update.\r
215                                         if ( CKEDITOR.env.ie )\r
216                                                 editor.getSelection().unlock( true );\r
217 \r
218                                         // Return focus to the editor (#5573)\r
219                                         // Refresh SCAYT markup\r
220                                         window.setTimeout( function()\r
221                                         {\r
222                                                 scayt_instance.focus();\r
223                                                 scayt_instance.refresh();\r
224                                         }, 10 );\r
225                                 }\r
226                         }, this, null, 50 );\r
227 \r
228                 editor.on( 'scaytDialog', function( ev )        // Communication with dialog.\r
229                         {\r
230                                 ev.data.djConfig = window.djConfig;\r
231                                 ev.data.scayt_control = plugin.getScayt( editor );\r
232                                 ev.data.tab = openPage;\r
233                                 ev.data.scayt = window.scayt;\r
234                         });\r
235 \r
236                 var dataProcessor = editor.dataProcessor,\r
237                         htmlFilter = dataProcessor && dataProcessor.htmlFilter;\r
238 \r
239                 if ( htmlFilter )\r
240                 {\r
241                         htmlFilter.addRules(\r
242                                 {\r
243                                         elements :\r
244                                         {\r
245                                                 span : function( element )\r
246                                                 {\r
247                                                         if ( element.attributes.scayt_word && element.attributes.scaytid )\r
248                                                         {\r
249                                                                 delete element.name;    // Write children, but don't write this node.\r
250                                                                 return element;\r
251                                                         }\r
252                                                 }\r
253                                         }\r
254                                 }\r
255                         );\r
256                 }\r
257 \r
258                 // Override Image.equals method avoid CK snapshot module to add SCAYT markup to snapshots. (#5546)\r
259                 var undoImagePrototype = CKEDITOR.plugins.undo.Image.prototype;\r
260                 undoImagePrototype.equals = CKEDITOR.tools.override( undoImagePrototype.equals, function( org )\r
261                 {\r
262                         return function( otherImage )\r
263                         {\r
264                                 var thisContents = this.contents,\r
265                                         otherContents = otherImage.contents;\r
266                                 var scayt_instance = plugin.getScayt( this.editor );\r
267                                 // Making the comparison based on content without SCAYT word markers.\r
268                                 if ( scayt_instance && plugin.isScaytReady( this.editor ) )\r
269                                 {\r
270                                         // scayt::reset might return value undefined. (#5742)\r
271                                         this.contents = scayt_instance.reset( thisContents ) || '';\r
272                                         otherImage.contents = scayt_instance.reset( otherContents ) || '';\r
273                                 }\r
274 \r
275                                 var retval = org.apply( this, arguments );\r
276 \r
277                                 this.contents = thisContents;\r
278                                 otherImage.contents = otherContents;\r
279                                 return retval;\r
280                         };\r
281                 });\r
282 \r
283                 if ( editor.document )\r
284                         createInstance();\r
285         };\r
286 \r
287 CKEDITOR.plugins.scayt =\r
288         {\r
289                 engineLoaded : false,\r
290                 instances : {},\r
291                 // Data storage for SCAYT control, based on editor instances\r
292                 controlInfo : {},\r
293                 setControlInfo : function( editor, o )\r
294                 {\r
295                         if ( editor && editor.name && typeof ( this.controlInfo[ editor.name ] ) != 'object' )\r
296                                 this.controlInfo[ editor.name ] = {};\r
297 \r
298                         for ( var infoOpt in o )\r
299                                 this.controlInfo[ editor.name ][ infoOpt ] = o[ infoOpt ];\r
300                 },\r
301                 isControlRestored : function( editor )\r
302                 {\r
303                         if ( editor &&\r
304                                         editor.name &&\r
305                                         this.controlInfo[ editor.name ] )\r
306                         {\r
307                                 return this.controlInfo[ editor.name ].restored ;\r
308                         }\r
309                         return false;\r
310                 },\r
311                 markControlRestore : function( editor )\r
312                 {\r
313                         this.setControlInfo( editor, { restored:true } );\r
314                 },\r
315                 setControlId: function( editor, id )\r
316                 {\r
317                         this.setControlInfo( editor, { id:id } );\r
318                 },\r
319                 getControlId: function( editor )\r
320                 {\r
321                         if ( editor &&\r
322                                         editor.name &&\r
323                                         this.controlInfo[ editor.name ] &&\r
324                                         this.controlInfo[ editor.name ].id )\r
325                         {\r
326                                 return this.controlInfo[ editor.name ].id;\r
327                         }\r
328                         return null;\r
329                 },\r
330                 setPaused: function( editor , bool )\r
331                 {\r
332                         this.setControlInfo( editor, { paused:bool } );\r
333                 },\r
334                 isPaused: function( editor )\r
335                 {\r
336                         if ( editor &&\r
337                                         editor.name &&\r
338                                         this.controlInfo[editor.name] )\r
339                         {\r
340                                 return this.controlInfo[editor.name].paused;\r
341                         }\r
342                         return undefined;\r
343                 },\r
344                 getScayt : function( editor )\r
345                 {\r
346                         return this.instances[ editor.name ];\r
347                 },\r
348                 isScaytReady : function( editor )\r
349                 {\r
350                         return this.engineLoaded === true &&\r
351                                 'undefined' !== typeof window.scayt && this.getScayt( editor );\r
352                 },\r
353                 isScaytEnabled : function( editor )\r
354                 {\r
355                         var scayt_instance = this.getScayt( editor );\r
356                         return ( scayt_instance ) ? scayt_instance.disabled === false : false;\r
357                 },\r
358                 loadEngine : function( editor )\r
359                 {\r
360                         // SCAYT doesn't work with Firefox2, Opera.\r
361                         if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 || CKEDITOR.env.opera )\r
362                                 return editor.fire( 'showScaytState' );\r
363 \r
364                         if ( this.engineLoaded === true )\r
365                                 return onEngineLoad.apply( editor );    // Add new instance.\r
366                         else if ( this.engineLoaded == -1 )                     // We are waiting.\r
367                                 return CKEDITOR.on( 'scaytReady', function(){ onEngineLoad.apply( editor ); } );        // Use function(){} to avoid rejection as duplicate.\r
368 \r
369                         CKEDITOR.on( 'scaytReady', onEngineLoad, editor );\r
370                         CKEDITOR.on( 'scaytReady', function()\r
371                                 {\r
372                                         this.engineLoaded = true;\r
373                                 },\r
374                                 this,\r
375                                 null,\r
376                                 0\r
377                         );      // First to run.\r
378 \r
379                         this.engineLoaded = -1; // Loading in progress.\r
380 \r
381                         // compose scayt url\r
382                         var protocol = document.location.protocol;\r
383                         // Default to 'http' for unknown.\r
384                         protocol = protocol.search( /https?:/) != -1? protocol : 'http:';\r
385                         var baseUrl  = 'svc.spellchecker.net/scayt25/loader__base.js';\r
386 \r
387                         var scaytUrl  =  editor.config.scayt_srcUrl || ( protocol + '//' + baseUrl );\r
388                         var scaytConfigBaseUrl =  plugin.parseUrl( scaytUrl ).path +  '/';\r
389 \r
390                         if( window.scayt == undefined )\r
391                         {\r
392                                 CKEDITOR._djScaytConfig =\r
393                                 {\r
394                                         baseUrl: scaytConfigBaseUrl,\r
395                                         addOnLoad:\r
396                                         [\r
397                                                 function()\r
398                                                 {\r
399                                                         CKEDITOR.fireOnce( 'scaytReady' );\r
400                                                 }\r
401                                         ],\r
402                                         isDebug: false\r
403                                 };\r
404                                 // Append javascript code.\r
405                                 CKEDITOR.document.getHead().append(\r
406                                         CKEDITOR.document.createElement( 'script',\r
407                                                 {\r
408                                                         attributes :\r
409                                                                 {\r
410                                                                         type : 'text/javascript',\r
411                                                                         async : 'true',\r
412                                                                         src : scaytUrl\r
413                                                                 }\r
414                                                 })\r
415                                 );\r
416                         }\r
417                         else\r
418                                 CKEDITOR.fireOnce( 'scaytReady' );\r
419 \r
420                         return null;\r
421                 },\r
422                 parseUrl : function ( data )\r
423                 {\r
424                         var match;\r
425                         if ( data.match && ( match = data.match(/(.*)[\/\\](.*?\.\w+)$/) ) )\r
426                                 return { path: match[1], file: match[2] };\r
427                         else\r
428                                 return data;\r
429                 }\r
430         };\r
431 \r
432         var plugin = CKEDITOR.plugins.scayt;\r
433 \r
434         // Context menu constructing.\r
435         var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, command, menugroup, menuOrder )\r
436         {\r
437                 editor.addCommand( commandName, command );\r
438 \r
439                 // If the "menu" plugin is loaded, register the menu item.\r
440                 editor.addMenuItem( commandName,\r
441                         {\r
442                                 label : buttonLabel,\r
443                                 command : commandName,\r
444                                 group : menugroup,\r
445                                 order : menuOrder\r
446                         });\r
447         };\r
448 \r
449         var commandDefinition =\r
450         {\r
451                 preserveState : true,\r
452                 editorFocus : false,\r
453                 canUndo : false,\r
454 \r
455                 exec: function( editor )\r
456                 {\r
457                         if ( plugin.isScaytReady( editor ) )\r
458                         {\r
459                                 var isEnabled = plugin.isScaytEnabled( editor );\r
460 \r
461                                 this.setState( isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_ON );\r
462 \r
463                                 var scayt_control = plugin.getScayt( editor );\r
464                                 // the place where the status of editor focus should be restored\r
465                                 // after there will be ability to store its state before SCAYT button click\r
466                                 // if (storedFocusState is focused )\r
467                                 //   scayt_control.focus();\r
468                                 //\r
469                                 // now focus is set certainly\r
470                                 scayt_control.focus();\r
471                                 scayt_control.setDisabled( isEnabled );\r
472                         }\r
473                         else if ( !editor.config.scayt_autoStartup && plugin.engineLoaded >= 0 )        // Load first time\r
474                         {\r
475                                 this.setState( CKEDITOR.TRISTATE_DISABLED );\r
476                                 plugin.loadEngine( editor );\r
477                         }\r
478                 }\r
479         };\r
480 \r
481         // Add scayt plugin.\r
482         CKEDITOR.plugins.add( 'scayt',\r
483         {\r
484                 requires : [ 'menubutton' ],\r
485 \r
486                 beforeInit : function( editor )\r
487                 {\r
488                         var items_order = editor.config.scayt_contextMenuItemsOrder\r
489                                         || 'suggest|moresuggest|control',\r
490                                 items_order_str = "";\r
491 \r
492                         items_order = items_order.split( '|' );\r
493 \r
494                         if ( items_order && items_order.length )\r
495                         {\r
496                                 for ( var pos = 0 ; pos < items_order.length ; pos++ )\r
497                                         items_order_str += 'scayt_' + items_order[ pos ] + ( items_order.length != parseInt( pos, 10 ) + 1 ? ',' : '' );\r
498                         }\r
499 \r
500                         // Put it on top of all context menu items (#5717)\r
501                         editor.config.menu_groups =  items_order_str + ',' + editor.config.menu_groups;\r
502                 },\r
503 \r
504                 init : function( editor )\r
505                 {\r
506                         var moreSuggestions = {},\r
507                                 mainSuggestions = {};\r
508 \r
509                         // Scayt command.\r
510                         var command = editor.addCommand( commandName, commandDefinition );\r
511 \r
512                         // Add Options dialog.\r
513                         CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );\r
514                         // read ui tags\r
515                         var confuiTabs = editor.config.scayt_uiTabs || '1,1,1';\r
516                         var uiTabs =[];\r
517                         // string to array convert\r
518                         confuiTabs = confuiTabs.split( ',' );\r
519                         // check array length ! always must be 3 filled with 1 or 0\r
520                         for ( var i=0, l=3; i < l; i++ )\r
521                         {\r
522                                 var flag = parseInt( confuiTabs[i] || '1', 10 );\r
523                                 uiTabs.push( flag );\r
524                         }\r
525 \r
526                         var menuGroup = 'scaytButton';\r
527                         editor.addMenuGroup( menuGroup );\r
528                         // combine menu items to render\r
529                         var uiMuneItems = {};\r
530 \r
531                         var lang = editor.lang.scayt;\r
532 \r
533                         // always added\r
534                         uiMuneItems.scaytToggle =\r
535                                 {\r
536                                         label : lang.enable,\r
537                                         command : commandName,\r
538                                         group : menuGroup\r
539                                 };\r
540 \r
541                         if ( uiTabs[0] == 1 )\r
542                                 uiMuneItems.scaytOptions =\r
543                                 {\r
544                                         label : lang.options,\r
545                                         group : menuGroup,\r
546                                         onClick : function()\r
547                                         {\r
548                                                 openPage = 'options';\r
549                                                 editor.openDialog( commandName );\r
550                                         }\r
551                                 };\r
552 \r
553                         if ( uiTabs[1] == 1 )\r
554                                 uiMuneItems.scaytLangs =\r
555                                 {\r
556                                         label : lang.langs,\r
557                                         group : menuGroup,\r
558                                         onClick : function()\r
559                                         {\r
560                                                 openPage = 'langs';\r
561                                                 editor.openDialog( commandName );\r
562                                         }\r
563                                 };\r
564                         if ( uiTabs[2] == 1 )\r
565                                 uiMuneItems.scaytDict =\r
566                                 {\r
567                                         label : lang.dictionariesTab,\r
568                                         group : menuGroup,\r
569                                         onClick : function()\r
570                                         {\r
571                                                 openPage = 'dictionaries';\r
572                                                 editor.openDialog( commandName );\r
573                                         }\r
574                                 };\r
575                         // always added\r
576                         uiMuneItems.scaytAbout =\r
577                                 {\r
578                                         label : editor.lang.scayt.about,\r
579                                         group : menuGroup,\r
580                                         onClick : function()\r
581                                         {\r
582                                                 openPage = 'about';\r
583                                                 editor.openDialog( commandName );\r
584                                         }\r
585                                 };\r
586 \r
587                         uiTabs[3] = 1; // about us tab is always on\r
588                         plugin.uiTabs = uiTabs;\r
589 \r
590                         editor.addMenuItems( uiMuneItems );\r
591 \r
592                                 editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,\r
593                                         {\r
594                                                 label : lang.title,\r
595                                                 title : CKEDITOR.env.opera ? lang.opera_title : lang.title,\r
596                                                 className : 'cke_button_scayt',\r
597                                                 onRender: function()\r
598                                                 {\r
599                                                         command.on( 'state', function()\r
600                                                         {\r
601                                                                 this.setState( command.state );\r
602                                                         },\r
603                                                         this);\r
604                                                 },\r
605                                                 onMenu : function()\r
606                                                 {\r
607                                                         var isEnabled = plugin.isScaytEnabled( editor );\r
608 \r
609                                                         editor.getMenuItem( 'scaytToggle' ).label = lang[ isEnabled ? 'disable' : 'enable' ];\r
610 \r
611                                                         return {\r
612                                                                 scaytToggle  : CKEDITOR.TRISTATE_OFF,\r
613                                                                 scaytOptions : isEnabled && plugin.uiTabs[0] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
614                                                                 scaytLangs   : isEnabled && plugin.uiTabs[1] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
615                                                                 scaytDict    : isEnabled && plugin.uiTabs[2] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
616                                                                 scaytAbout   : isEnabled && plugin.uiTabs[3] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
617                                                         };\r
618                                                 }\r
619                                         });\r
620 \r
621                         // If the "contextmenu" plugin is loaded, register the listeners.\r
622                         if ( editor.contextMenu && editor.addMenuItems )\r
623                         {\r
624                                 editor.contextMenu.addListener( function( element, selection )\r
625                                         {\r
626                                                 if ( !plugin.isScaytEnabled( editor )\r
627                                                                 || selection.getCommonAncestor().isReadOnly() )\r
628                                                         return null;\r
629 \r
630                                                 var scayt_control = plugin.getScayt( editor ),\r
631                                                         node = scayt_control.getScaytNode();\r
632 \r
633                                                 if ( !node )\r
634                                                         return null;\r
635 \r
636                                                         var word = scayt_control.getWord( node );\r
637 \r
638                                                 if ( !word )\r
639                                                         return null;\r
640 \r
641                                                 var sLang = scayt_control.getLang(),\r
642                                                         _r = {},\r
643                                                         items_suggestion = window.scayt.getSuggestion( word, sLang );\r
644                                                 if ( !items_suggestion || !items_suggestion.length )\r
645                                                         return null;\r
646                                                 // Remove unused commands and menuitems\r
647                                                 for ( i in moreSuggestions )\r
648                                                 {\r
649                                                         delete editor._.menuItems[ i ];\r
650                                                         delete editor._.commands[ i ];\r
651                                                 }\r
652                                                 for ( i in mainSuggestions )\r
653                                                 {\r
654                                                         delete editor._.menuItems[ i ];\r
655                                                         delete editor._.commands[ i ];\r
656                                                 }\r
657                                                 moreSuggestions = {};           // Reset items.\r
658                                                 mainSuggestions = {};\r
659 \r
660                                                 var moreSuggestionsUnable = editor.config.scayt_moreSuggestions || 'on';\r
661                                                 var moreSuggestionsUnableAdded = false;\r
662 \r
663                                                 var maxSuggestions = editor.config.scayt_maxSuggestions;\r
664                                                 ( typeof maxSuggestions != 'number' ) && ( maxSuggestions = 5 );\r
665                                                 !maxSuggestions && ( maxSuggestions = items_suggestion.length );\r
666 \r
667                                                 var contextCommands = editor.config.scayt_contextCommands || 'all';\r
668                                                 contextCommands = contextCommands.split( '|' );\r
669 \r
670                                                 for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )\r
671                                                 {\r
672                                                         var commandName = 'scayt_suggestion_' + items_suggestion[i].replace( ' ', '_' );\r
673                                                         var exec = ( function( el, s )\r
674                                                                 {\r
675                                                                         return {\r
676                                                                                 exec: function()\r
677                                                                                 {\r
678                                                                                         scayt_control.replace( el, s );\r
679                                                                                 }\r
680                                                                         };\r
681                                                                 })( node, items_suggestion[i] );\r
682 \r
683                                                         if ( i < maxSuggestions )\r
684                                                         {\r
685                                                                 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],\r
686                                                                         commandName, exec, 'scayt_suggest', i + 1 );\r
687                                                                 _r[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
688                                                                 mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
689                                                         }\r
690                                                         else if ( moreSuggestionsUnable == 'on' )\r
691                                                         {\r
692                                                                 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],\r
693                                                                         commandName, exec, 'scayt_moresuggest', i + 1 );\r
694                                                                 moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
695                                                                 moreSuggestionsUnableAdded = true;\r
696                                                         }\r
697                                                 }\r
698 \r
699                                                 if ( moreSuggestionsUnableAdded )\r
700                                                 {\r
701                                                         // Register the More suggestions group;\r
702                                                         editor.addMenuItem( 'scayt_moresuggest',\r
703                                                         {\r
704                                                                 label : lang.moreSuggestions,\r
705                                                                 group : 'scayt_moresuggest',\r
706                                                                 order : 10,\r
707                                                                 getItems : function()\r
708                                                                 {\r
709                                                                         return moreSuggestions;\r
710                                                                 }\r
711                                                         });\r
712                                                         mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;\r
713                                                 }\r
714 \r
715                                                 if ( in_array( 'all', contextCommands )  || in_array( 'ignore', contextCommands)  )\r
716                                                 {\r
717                                                         var ignore_command = {\r
718                                                                 exec: function(){\r
719                                                                         scayt_control.ignore( node );\r
720                                                                 }\r
721                                                         };\r
722                                                         addButtonCommand( editor, 'ignore', lang.ignore, 'scayt_ignore', ignore_command, 'scayt_control', 1 );\r
723                                                         mainSuggestions[ 'scayt_ignore' ] = CKEDITOR.TRISTATE_OFF;\r
724                                                 }\r
725 \r
726                                                 if ( in_array( 'all', contextCommands )  || in_array( 'ignoreall', contextCommands ) )\r
727                                                 {\r
728                                                         var ignore_all_command = {\r
729                                                                 exec: function(){\r
730                                                                         scayt_control.ignoreAll( node );\r
731                                                                 }\r
732                                                         };\r
733                                                         addButtonCommand(editor, 'ignore_all', lang.ignoreAll, 'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);\r
734                                                         mainSuggestions['scayt_ignore_all'] = CKEDITOR.TRISTATE_OFF;\r
735                                                 }\r
736 \r
737                                                 if ( in_array( 'all', contextCommands )  || in_array( 'add', contextCommands ) )\r
738                                                 {\r
739                                                         var addword_command = {\r
740                                                                 exec: function(){\r
741                                                                         window.scayt.addWordToUserDictionary( node );\r
742                                                                 }\r
743                                                         };\r
744                                                         addButtonCommand(editor, 'add_word', lang.addWord, 'scayt_add_word', addword_command, 'scayt_control', 3);\r
745                                                         mainSuggestions['scayt_add_word'] = CKEDITOR.TRISTATE_OFF;\r
746                                                 }\r
747 \r
748                                                 if ( scayt_control.fireOnContextMenu )\r
749                                                         scayt_control.fireOnContextMenu( editor );\r
750 \r
751                                                 return mainSuggestions;\r
752                                         });\r
753                         }\r
754 \r
755                         var showInitialState = function()\r
756                                 {\r
757                                         editor.removeListener( 'showScaytState', showInitialState );\r
758 \r
759                                         if ( !CKEDITOR.env.opera )\r
760                                                 command.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
761                                         else\r
762                                                 command.setState( CKEDITOR.TRISTATE_DISABLED );\r
763                                 };\r
764 \r
765                         editor.on( 'showScaytState', showInitialState );\r
766 \r
767                         if ( CKEDITOR.env.opera )\r
768                         {\r
769                                 editor.on( 'instanceReady', function()\r
770                                 {\r
771                                         showInitialState();\r
772                                 });\r
773                         }\r
774 \r
775                         // Start plugin\r
776                         if ( editor.config.scayt_autoStartup )\r
777                         {\r
778                                 editor.on( 'instanceReady', function()\r
779                                 {\r
780                                         plugin.loadEngine( editor );\r
781                                 });\r
782                         }\r
783                 },\r
784 \r
785                 afterInit : function( editor )\r
786                 {\r
787                         // Prevent word marker line from displaying in elements path and been removed when cleaning format. (#3570) (#4125)\r
788                         var elementsPathFilters,\r
789                                         scaytFilter = function( element )\r
790                                         {\r
791                                                 if ( element.hasAttribute( 'scaytid' ) )\r
792                                                         return false;\r
793                                         };\r
794 \r
795                         if ( editor._.elementsPath && ( elementsPathFilters = editor._.elementsPath.filters ) )\r
796                                 elementsPathFilters.push( scaytFilter );\r
797 \r
798                         editor.addRemoveFormatFilter && editor.addRemoveFormatFilter( scaytFilter );\r
799 \r
800                 }\r
801         });\r
802 })();\r
803 \r
804 /**\r
805  * If enabled (true), turns on SCAYT automatically after loading the editor.\r
806  * @name CKEDITOR.config.scayt_autoStartup\r
807  * @type Boolean\r
808  * @default false\r
809  * @example\r
810  * config.scayt_autoStartup = true;\r
811  */\r
812 \r
813 /**\r
814  * Defines the number of SCAYT suggestions to show in the main context menu.\r
815  * The possible values are:\r
816  * <ul>\r
817  *      <li>0 (zero): All suggestions are displayed in the main context menu.</li>\r
818  *      <li>Positive number: The maximum number of suggestions to shown in context\r
819  *              menu. Other entries will be shown in "More Suggestions" sub-menu.</li>\r
820  *      <li>Negative number: No suggestions are shown in the main context menu. All\r
821  *              entries will be listed in the "Suggestions" sub-menu.</li>\r
822  * </ul>\r
823  * @name CKEDITOR.config.scayt_maxSuggestions\r
824  * @type Number\r
825  * @default 5\r
826  * @example\r
827  * // Display only three suggestions in the main context menu.\r
828  * config.scayt_maxSuggestions = 3;\r
829  * @example\r
830  * // Do not show the suggestions directly.\r
831  * config.scayt_maxSuggestions = -1;\r
832  */\r
833 \r
834 /**\r
835  * Sets the customer ID for SCAYT. Required for migration from free version\r
836  * with banner to paid version.\r
837  * @name CKEDITOR.config.scayt_customerid\r
838  * @type String\r
839  * @default ''\r
840  * @example\r
841  * // Load SCAYT using my customer ID.\r
842  * config.scayt_customerid  = 'your-encrypted-customer-id';\r
843  */\r
844 \r
845 /**\r
846  * Enables/disables the "More Suggestions" sub-menu in the context menu.\r
847  * The possible values are "on" or "off".\r
848  * @name CKEDITOR.config.scayt_moreSuggestions\r
849  * @type String\r
850  * @default 'on'\r
851  * @example\r
852  * // Disables the "More Suggestions" sub-menu.\r
853  * config.scayt_moreSuggestions = 'off';\r
854  */\r
855 \r
856 /**\r
857  * Customizes the display of SCAYT context menu commands ("Add Word", "Ignore"\r
858  * and "Ignore All"). It must be a string with one or more of the following\r
859  * words separated by a pipe ("|"):\r
860  * <ul>\r
861  *      <li>"off": disables all options.</li>\r
862  *      <li>"all": enables all options.</li>\r
863  *      <li>"ignore": enables the "Ignore" option.</li>\r
864  *      <li>"ignoreall": enables the "Ignore All" option.</li>\r
865  *      <li>"add": enables the "Add Word" option.</li>\r
866  * </ul>\r
867  * @name CKEDITOR.config.scayt_contextCommands\r
868  * @type String\r
869  * @default 'all'\r
870  * @example\r
871  * // Show only "Add Word" and "Ignore All" in the context menu.\r
872  * config.scayt_contextCommands = 'add|ignoreall';\r
873  */\r
874 \r
875 /**\r
876  * Sets the default spellchecking language for SCAYT.\r
877  * @name CKEDITOR.config.scayt_sLang\r
878  * @type String\r
879  * @default 'en_US'\r
880  * @example\r
881  * // Sets SCAYT to German.\r
882  * config.scayt_sLang = 'de_DE';\r
883  */\r
884 \r
885 /**\r
886  * Sets the visibility of the SCAYT tabs in the settings dialog and toolbar\r
887  * button. The value must contain a "1" (enabled) or "0" (disabled) number for\r
888  * each of the following entries, in this precise order, separated by a\r
889  * comma (","): "Options", "Languages" and "Dictionary".\r
890  * @name CKEDITOR.config.scayt_uiTabs\r
891  * @type String\r
892  * @default '1,1,1'\r
893  * @example\r
894  * // Hide the "Languages" tab.\r
895  * config.scayt_uiTabs = '1,0,1';\r
896  */\r
897 \r
898 \r
899 /**\r
900  * Set the URL to SCAYT core. Required to switch to licensed version of SCAYT application.\r
901  * Further details at http://wiki.spellchecker.net/doku.php?id=3rd:wysiwyg:fckeditor:wscckf3l .\r
902  * @name CKEDITOR.config.scayt_srcUrl\r
903  * @type String\r
904  * @default ''\r
905  * @example\r
906  * config.scayt_srcUrl = "http://my-host/spellcheck/lf/scayt/scayt.js";\r
907  */\r
908 \r
909 /**\r
910  * Links SCAYT to custom dictionaries. It's a string containing dictionary ids\r
911  * separared by commas (","). Available only for licensed version.\r
912  * Further details at http://wiki.spellchecker.net/doku.php?id=custom_dictionary_support .\r
913  * @name CKEDITOR.config.scayt_customDictionaryIds\r
914  * @type String\r
915  * @default ''\r
916  * @example\r
917  * config.scayt_customDictionaryIds = '3021,3456,3478"';\r
918  */\r
919 \r
920 /**\r
921  * Makes it possible to activate a custom dictionary on SCAYT. The user\r
922  * dictionary name must be used. Available only for licensed version.\r
923  * @name CKEDITOR.config.scayt_userDictionaryName\r
924  * @type String\r
925  * @default ''\r
926  * @example\r
927  * config.scayt_userDictionaryName = 'MyDictionary';\r
928  */\r
929 \r
930 /**\r
931  * Define order of placing of SCAYT context menu items by groups.\r
932  * It must be a string with one or more of the following\r
933  * words separated by a pipe ("|"):\r
934  * <ul>\r
935  *     <li>'suggest'     - main suggestion word list,</li>\r
936  *     <li>'moresuggest' - more suggestions word list,</li>\r
937  *     <li>'control'     - SCAYT commands, such as 'Ignore' and 'Add Word'</li>\r
938  * </ul>\r
939  *\r
940  * @name CKEDITOR.config.scayt_contextMenuItemsOrder\r
941  * @type String\r
942  * @default 'suggest|moresuggest|control'\r
943  * @example\r
944  * config.scayt_contextMenuItemsOrder = 'moresuggest|control|suggest';\r
945  */\r