JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[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 = false, key;\r
20                 for (key in haystack)\r
21                 {\r
22                         if ((haystack[key] === needle) || ( haystack[key] == needle))\r
23                         {\r
24                                 found = true;\r
25                                 break;\r
26                         }\r
27                 }\r
28                 return found;\r
29         }\r
30 \r
31         var onEngineLoad = function()\r
32         {\r
33                 var editor = this;\r
34 \r
35                 var createInstance = function() // Create new instance every time Document is created.\r
36                 {\r
37                         // Initialise Scayt instance.\r
38                         var oParams = {};\r
39                         // Get the iframe.\r
40                         oParams.srcNodeRef = editor.document.getWindow().$.frameElement;\r
41                         // syntax : AppName.AppVersion@AppRevision\r
42                         oParams.assocApp  = 'CKEDITOR.' + CKEDITOR.version + '@' + CKEDITOR.revision;\r
43                         oParams.customerid = editor.config.scayt_customerid  || '1:WvF0D4-UtPqN1-43nkD4-NKvUm2-daQqk3-LmNiI-z7Ysb4-mwry24-T8YrS3-Q2tpq2';\r
44                         oParams.customDictionaryIds = editor.config.scayt_customDictionaryIds || '';\r
45                         oParams.userDictionaryName = editor.config.scayt_userDictionaryName || '';\r
46                         oParams.sLang = editor.config.scayt_sLang || 'en_US';\r
47 \r
48                         // Introduce SCAYT onLoad callback. (#5632)\r
49                         oParams.onLoad = function()\r
50                                 {\r
51                                         // Draw down word marker to avoid being covered by background-color style.(#5466)\r
52                                         if ( !( CKEDITOR.env.ie && CKEDITOR.env.version < 8 ) )\r
53                                                 this.addStyle( this.selectorCss(), 'padding-bottom: 2px !important;' );\r
54 \r
55                                         // Call scayt_control.focus when SCAYT loaded\r
56                                         // and only if editor has focus and scayt control creates at first time (#5720)\r
57                                         if ( editor.focusManager.hasFocus && !plugin.isControlRestored( editor ) )\r
58                                                 this.focus();\r
59 \r
60                                 };\r
61 \r
62                         oParams.onBeforeChange = function()\r
63                         {\r
64                                 if ( plugin.getScayt( editor ) && !editor.checkDirty() )\r
65                                         setTimeout( function(){ editor.resetDirty(); } );\r
66                         };\r
67 \r
68                         var scayt_custom_params = window.scayt_custom_params;\r
69                         if ( typeof scayt_custom_params == 'object')\r
70                         {\r
71                                 for ( var k in scayt_custom_params )\r
72                                 {\r
73                                         oParams[ k ] = scayt_custom_params[ k ];\r
74                                 }\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 Opera.\r
361                         if ( 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/spellcheck31/lf/scayt24/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                                                                         src : scaytUrl\r
412                                                                 }\r
413                                                 })\r
414                                 );\r
415                         }\r
416                         else\r
417                                 CKEDITOR.fireOnce( 'scaytReady' );\r
418 \r
419                         return null;\r
420                 },\r
421                 parseUrl : function ( data )\r
422                 {\r
423                         var match;\r
424                         if ( data.match && ( match = data.match(/(.*)[\/\\](.*?\.\w+)$/) ) )\r
425                                 return { path: match[1], file: match[2] };\r
426                         else\r
427                                 return data;\r
428                 }\r
429         };\r
430 \r
431         var plugin = CKEDITOR.plugins.scayt;\r
432 \r
433         // Context menu constructing.\r
434         var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, command, menugroup, menuOrder )\r
435         {\r
436                 editor.addCommand( commandName, command );\r
437 \r
438                 // If the "menu" plugin is loaded, register the menu item.\r
439                 editor.addMenuItem( commandName,\r
440                         {\r
441                                 label : buttonLabel,\r
442                                 command : commandName,\r
443                                 group : menugroup,\r
444                                 order : menuOrder\r
445                         });\r
446         };\r
447 \r
448         var commandDefinition =\r
449         {\r
450                 preserveState : true,\r
451                 editorFocus : false,\r
452 \r
453                 exec: function( editor )\r
454                 {\r
455                         var autoStartup = editor.config.scayt_autoStartup;\r
456                         autoStartup = ( autoStartup == undefined ) || autoStartup;\r
457 \r
458                         if ( plugin.isScaytReady( editor ) )\r
459                         {\r
460                                 var isEnabled = plugin.isScaytEnabled( editor );\r
461 \r
462                                 this.setState( isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_ON );\r
463 \r
464                                 var scayt_control = plugin.getScayt( editor );\r
465                                 // the place where the status of editor focus should be restored\r
466                                 // after there will be ability to store its state before SCAYT button click\r
467                                 // if (storedFocusState is focused )\r
468                                 //   scayt_control.focus();\r
469                                 //\r
470                                 // now focus is set certainly\r
471                                 scayt_control.focus( );\r
472                                 scayt_control.setDisabled( isEnabled );\r
473                         }\r
474                         else if ( !autoStartup && plugin.engineLoaded >= 0 )    // Load first time\r
475                         {\r
476                                 this.setState( CKEDITOR.TRISTATE_DISABLED );\r
477                                 plugin.loadEngine( editor );\r
478                         }\r
479                 }\r
480         };\r
481 \r
482         // Add scayt plugin.\r
483         CKEDITOR.plugins.add( 'scayt',\r
484         {\r
485                 requires : [ 'menubutton' ],\r
486 \r
487                 beforeInit : function( editor )\r
488                 {\r
489                         var items_order = editor.config.scayt_contextMenuItemsOrder\r
490                                         || 'suggest|moresuggest|control',\r
491                                 items_order_str = "";\r
492 \r
493                         items_order = items_order.split( '|' );\r
494 \r
495                         if ( items_order && items_order.length )\r
496                         {\r
497                                 for ( var pos in items_order )\r
498                                         items_order_str += 'scayt_' + items_order[ pos ] + ( items_order.length != parseInt( pos, 10 ) + 1 ? ',' : '' );\r
499                         }\r
500 \r
501                         // Register scayt rbc menu group.\r
502                         if ( editor.config.scayt_contextMenuOntop )\r
503                                 // Put it on top of all context menu items\r
504                                 editor.config.menu_groups =  items_order_str + ',' + editor.config.menu_groups;\r
505                         else\r
506                                 // Put it down\r
507                                 editor.config.menu_groups = editor.config.menu_groups + ',' +items_order_str;\r
508                 },\r
509 \r
510                 init : function( editor )\r
511                 {\r
512                         var moreSuggestions = {};\r
513                         var mainSuggestions = {};\r
514 \r
515                         // Scayt command.\r
516                         var command = editor.addCommand( commandName, commandDefinition );\r
517 \r
518                         // Add Options dialog.\r
519                         CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );\r
520                         // read ui tags\r
521                         var confuiTabs = editor.config.scayt_uiTabs || '1,1,1';\r
522                         var uiTabs =[];\r
523                         // string to array convert\r
524                         confuiTabs = confuiTabs.split( ',' );\r
525                         // check array length ! always must be 3 filled with 1 or 0\r
526                         for (var i=0,l=3; i<l; i++)\r
527                         {\r
528                                 var flag = parseInt(confuiTabs[i] || '1' ,10);\r
529                                 uiTabs.push( flag );\r
530                         }\r
531 \r
532                         var menuGroup = 'scaytButton';\r
533                         editor.addMenuGroup( menuGroup );\r
534                         // combine menu items to render\r
535                         var uiMuneItems = {};\r
536 \r
537                         // always added\r
538                         uiMuneItems.scaytToggle =\r
539                                 {\r
540                                         label : editor.lang.scayt.enable,\r
541                                         command : commandName,\r
542                                         group : menuGroup\r
543                                 };\r
544 \r
545                         if (uiTabs[0] == 1)\r
546                                 uiMuneItems.scaytOptions =\r
547                                 {\r
548                                         label : editor.lang.scayt.options,\r
549                                         group : menuGroup,\r
550                                         onClick : function()\r
551                                         {\r
552                                                 openPage = 'options';\r
553                                                 editor.openDialog( commandName );\r
554                                         }\r
555                                 };\r
556 \r
557                         if (uiTabs[1] == 1)\r
558                                 uiMuneItems.scaytLangs =\r
559                                 {\r
560                                         label : editor.lang.scayt.langs,\r
561                                         group : menuGroup,\r
562                                         onClick : function()\r
563                                         {\r
564                                                 openPage = 'langs';\r
565                                                 editor.openDialog( commandName );\r
566                                         }\r
567                                 };\r
568                         if (uiTabs[2] == 1)\r
569                                 uiMuneItems.scaytDict =\r
570                                 {\r
571                                         label : editor.lang.scayt.dictionariesTab,\r
572                                         group : menuGroup,\r
573                                         onClick : function()\r
574                                         {\r
575                                                 openPage = 'dictionaries';\r
576                                                 editor.openDialog( commandName );\r
577                                         }\r
578                                 };\r
579                         // always added\r
580                         uiMuneItems.scaytAbout =\r
581                                 {\r
582                                         label : editor.lang.scayt.about,\r
583                                         group : menuGroup,\r
584                                         onClick : function()\r
585                                         {\r
586                                                 openPage = 'about';\r
587                                                 editor.openDialog( commandName );\r
588                                         }\r
589                                 }\r
590                         ;\r
591 \r
592                         uiTabs[3] = 1; // about us tab is always on\r
593                         plugin.uiTabs = uiTabs;\r
594 \r
595                         editor.addMenuItems( uiMuneItems );\r
596 \r
597                                 editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,\r
598                                         {\r
599                                                 label : editor.lang.scayt.title,\r
600                                                 title : CKEDITOR.env.opera ? editor.lang.scayt.opera_title : editor.lang.scayt.title,\r
601                                                 className : 'cke_button_scayt',\r
602                                                 onRender: function()\r
603                                                 {\r
604                                                         command.on( 'state', function()\r
605                                                         {\r
606                                                                 this.setState( command.state );\r
607                                                         },\r
608                                                         this);\r
609                                                 },\r
610                                                 onMenu : function()\r
611                                                 {\r
612                                                         var isEnabled = plugin.isScaytEnabled( editor );\r
613 \r
614                                                         editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];\r
615 \r
616                                                         return {\r
617                                                                 scaytToggle  : CKEDITOR.TRISTATE_OFF,\r
618                                                                 scaytOptions : isEnabled && plugin.uiTabs[0] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
619                                                                 scaytLangs   : isEnabled && plugin.uiTabs[1] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
620                                                                 scaytDict    : isEnabled && plugin.uiTabs[2] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,\r
621                                                                 scaytAbout   : isEnabled && plugin.uiTabs[3] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED\r
622                                                         };\r
623                                                 }\r
624                                         });\r
625 \r
626                         // If the "contextmenu" plugin is loaded, register the listeners.\r
627                         if ( editor.contextMenu && editor.addMenuItems )\r
628                         {\r
629                                 editor.contextMenu.addListener( function(  )\r
630                                         {\r
631                                                 if ( !plugin.isScaytEnabled( editor ) )\r
632                                                         return null;\r
633 \r
634                                                 var scayt_control = plugin.getScayt( editor ),\r
635                                                         node = scayt_control.getScaytNode();\r
636 \r
637                                                 if ( !node )\r
638                                                         return null;\r
639 \r
640                                                         var word = scayt_control.getWord( node );\r
641 \r
642                                                 if ( !word )\r
643                                                         return null;\r
644 \r
645                                                 var sLang = scayt_control.getLang(),\r
646                                                         _r = {},\r
647                                                         items_suggestion = window.scayt.getSuggestion( word, sLang );\r
648                                                 if ( !items_suggestion || !items_suggestion.length )\r
649                                                         return null;\r
650                                                 // Remove unused commands and menuitems\r
651                                                 for ( i in moreSuggestions )\r
652                                                 {\r
653                                                         delete editor._.menuItems[ i ];\r
654                                                         delete editor._.commands[ i ];\r
655                                                 }\r
656                                                 for ( i in mainSuggestions )\r
657                                                 {\r
658                                                         delete editor._.menuItems[ i ];\r
659                                                         delete editor._.commands[ i ];\r
660                                                 }\r
661                                                 moreSuggestions = {};           // Reset items.\r
662                                                 mainSuggestions = {};\r
663 \r
664                                                 var moreSuggestionsUnable = editor.config.scayt_moreSuggestions || 'on';\r
665                                                 var moreSuggestionsUnableAdded = false;\r
666 \r
667                                                 var maxSuggestions = editor.config.scayt_maxSuggestions;\r
668                                                 ( typeof maxSuggestions != 'number' ) && ( maxSuggestions = 5 );\r
669                                                 !maxSuggestions && ( maxSuggestions = items_suggestion.length );\r
670 \r
671                                                 var contextCommands = editor.config.scayt_contextCommands || 'all';\r
672                                                 contextCommands = contextCommands.split( '|' );\r
673 \r
674                                                 for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )\r
675                                                 {\r
676                                                         var commandName = 'scayt_suggestion_' + items_suggestion[i].replace( ' ', '_' );\r
677                                                         var exec = ( function( el, s )\r
678                                                                 {\r
679                                                                         return {\r
680                                                                                 exec: function()\r
681                                                                                 {\r
682                                                                                         scayt_control.replace(el, s);\r
683                                                                                 }\r
684                                                                         };\r
685                                                                 })( node, items_suggestion[i] );\r
686 \r
687                                                         if ( i < maxSuggestions )\r
688                                                         {\r
689                                                                 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],\r
690                                                                         commandName, exec, 'scayt_suggest', i + 1 );\r
691                                                                 _r[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
692                                                                 mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
693                                                         }\r
694                                                         else if ( moreSuggestionsUnable == 'on' )\r
695                                                         {\r
696                                                                 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],\r
697                                                                         commandName, exec, 'scayt_moresuggest', i + 1 );\r
698                                                                 moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;\r
699                                                                 moreSuggestionsUnableAdded = true;\r
700                                                         }\r
701                                                 }\r
702 \r
703                                                 if ( moreSuggestionsUnableAdded )\r
704                                                 {\r
705                                                         // Register the More suggestions group;\r
706                                                         editor.addMenuItem( 'scayt_moresuggest',\r
707                                                         {\r
708                                                                 label : editor.lang.scayt.moreSuggestions,\r
709                                                                 group : 'scayt_moresuggest',\r
710                                                                 order : 10,\r
711                                                                 getItems : function()\r
712                                                                 {\r
713                                                                         return moreSuggestions;\r
714                                                                 }\r
715                                                         });\r
716                                                         mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;\r
717                                                 }\r
718 \r
719                                                 if ( in_array( 'all', contextCommands )  || in_array( 'ignore', contextCommands)  )\r
720                                                 {\r
721                                                         var ignore_command = {\r
722                                                                 exec: function(){\r
723                                                                         scayt_control.ignore( node );\r
724                                                                 }\r
725                                                         };\r
726                                                         addButtonCommand( editor, 'ignore', editor.lang.scayt.ignore, 'scayt_ignore', ignore_command, 'scayt_control', 1 );\r
727                                                         mainSuggestions[ 'scayt_ignore' ] = CKEDITOR.TRISTATE_OFF;\r
728                                                 }\r
729 \r
730                                                 if ( in_array( 'all', contextCommands )  || in_array( 'ignoreall', contextCommands ) )\r
731                                                 {\r
732                                                         var ignore_all_command = {\r
733                                                                 exec: function(){\r
734                                                                         scayt_control.ignoreAll( node );\r
735                                                                 }\r
736                                                         };\r
737                                                         addButtonCommand(editor, 'ignore_all', editor.lang.scayt.ignoreAll, 'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);\r
738                                                         mainSuggestions['scayt_ignore_all'] = CKEDITOR.TRISTATE_OFF;\r
739                                                 }\r
740 \r
741                                                 if ( in_array( 'all', contextCommands )  || in_array( 'add', contextCommands ) )\r
742                                                 {\r
743                                                         var addword_command = {\r
744                                                                 exec: function(){\r
745                                                                         window.scayt.addWordToUserDictionary( node );\r
746                                                                 }\r
747                                                         };\r
748                                                         addButtonCommand(editor, 'add_word', editor.lang.scayt.addWord, 'scayt_add_word', addword_command, 'scayt_control', 3);\r
749                                                         mainSuggestions['scayt_add_word'] = CKEDITOR.TRISTATE_OFF;\r
750                                                 }\r
751 \r
752                                                 if ( scayt_control.fireOnContextMenu )\r
753                                                         scayt_control.fireOnContextMenu( editor );\r
754 \r
755                                                 return mainSuggestions;\r
756                                         });\r
757                         }\r
758 \r
759                         var showInitialState = function()\r
760                                 {\r
761                                         editor.removeListener( 'showScaytState', showInitialState );\r
762 \r
763                                         if ( !CKEDITOR.env.opera )\r
764                                                 command.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
765                                         else\r
766                                                 command.setState( CKEDITOR.TRISTATE_DISABLED );\r
767                                 };\r
768 \r
769                         editor.on( 'showScaytState', showInitialState );\r
770 \r
771                         if ( CKEDITOR.env.opera )\r
772                         {\r
773                                 editor.on( 'instanceReady', function()\r
774                                 {\r
775                                         showInitialState();\r
776                                 });\r
777                         }\r
778 \r
779                         // Start plugin\r
780                         var autoStartup = editor.config.scayt_autoStartup;\r
781                         if ( ( autoStartup == undefined ) || autoStartup )\r
782                         {\r
783                                 editor.on( 'instanceReady', function()\r
784                                 {\r
785                                         plugin.loadEngine( editor );\r
786                                 });\r
787                         }\r
788                 },\r
789 \r
790                 afterInit : function( editor )\r
791                 {\r
792                         // Prevent word marker line from displaying in elements path and been removed when cleaning format. (#3570) (#4125)\r
793                         var elementsPathFilters,\r
794                                         scaytFilter = function( element )\r
795                                         {\r
796                                                 if ( element.hasAttribute( 'scaytid' ) )\r
797                                                         return false;\r
798                                         };\r
799 \r
800                         if ( editor._.elementsPath && ( elementsPathFilters = editor._.elementsPath.filters ) )\r
801                                 elementsPathFilters.push( scaytFilter );\r
802 \r
803                         editor.addRemoveFormatFilter && editor.addRemoveFormatFilter( scaytFilter );\r
804 \r
805                 }\r
806         });\r
807 })();\r
808 \r
809 /**\r
810  * If enabled (true), turns on SCAYT automatically after loading the editor.\r
811  * @name CKEDITOR.config.scayt_autoStartup\r
812  * @type Boolean\r
813  * @default true\r
814  * @example\r
815  * config.scayt_autoStartup = false;\r
816  */\r
817 \r
818 /**\r
819  * Defines the number of SCAYT suggestions to show in the main context menu.\r
820  * The possible values are:\r
821  * <ul>\r
822  *      <li>0 (zero): All suggestions are displayed in the main context menu.</li>\r
823  *      <li>Positive number: The maximum number of suggestions to shown in context\r
824  *              menu. Other entries will be shown in "More Suggestions" sub-menu.</li>\r
825  *      <li>Negative number: No suggestions are shown in the main context menu. All\r
826  *              entries will be listed in the "Suggestions" sub-menu.</li>\r
827  * </ul>\r
828  * @name CKEDITOR.config.scayt_maxSuggestions\r
829  * @type Number\r
830  * @default 5\r
831  * @example\r
832  * // Display only three suggestions in the main context menu.\r
833  * config.scayt_maxSuggestions = 3;\r
834  * @example\r
835  * // Do not show the suggestions directly.\r
836  * config.scayt_maxSuggestions = -1;\r
837  */\r
838 \r
839 /**\r
840  * Sets the customer ID for SCAYT. Required for migration from free version\r
841  * with banner to paid version.\r
842  * @name CKEDITOR.config.scayt_customerid\r
843  * @type String\r
844  * @default ''\r
845  * @example\r
846  * // Load SCAYT using my customer ID.\r
847  * config.scayt_customerid  = 'your-encrypted-customer-id';\r
848  */\r
849 \r
850 /**\r
851  * Enables/disables the "More Suggestions" sub-menu in the context menu.\r
852  * The possible values are "on" or "off".\r
853  * @name CKEDITOR.config.scayt_moreSuggestions\r
854  * @type String\r
855  * @default 'on'\r
856  * @example\r
857  * // Disables the "More Suggestions" sub-menu.\r
858  * config.scayt_moreSuggestions = 'off';\r
859  */\r
860 \r
861 /**\r
862  * Customizes the display of SCAYT context menu commands ("Add Word", "Ignore"\r
863  * and "Ignore All"). It must be a string with one or more of the following\r
864  * words separated by a pipe ("|"):\r
865  * <ul>\r
866  *      <li>"off": disables all options.</li>\r
867  *      <li>"all": enables all options.</li>\r
868  *      <li>"ignore": enables the "Ignore" option.</li>\r
869  *      <li>"ignoreall": enables the "Ignore All" option.</li>\r
870  *      <li>"add": enables the "Add Word" option.</li>\r
871  * </ul>\r
872  * @name CKEDITOR.config.scayt_contextCommands\r
873  * @type String\r
874  * @default 'all'\r
875  * @example\r
876  * // Show only "Add Word" and "Ignore All" in the context menu.\r
877  * config.scayt_contextCommands = 'add|ignoreall';\r
878  */\r
879 \r
880 /**\r
881  * Sets the default spellchecking language for SCAYT.\r
882  * @name CKEDITOR.config.scayt_sLang\r
883  * @type String\r
884  * @default 'en_US'\r
885  * @example\r
886  * // Sets SCAYT to German.\r
887  * config.scayt_sLang = 'de_DE';\r
888  */\r
889 \r
890 /**\r
891  * Sets the visibility of the SCAYT tabs in the settings dialog and toolbar\r
892  * button. The value must contain a "1" (enabled) or "0" (disabled) number for\r
893  * each of the following entries, in this precise order, separated by a\r
894  * comma (","): "Options", "Languages" and "Dictionary".\r
895  * @name CKEDITOR.config.scayt_uiTabs\r
896  * @type String\r
897  * @default '1,1,1'\r
898  * @example\r
899  * // Hide the "Languages" tab.\r
900  * config.scayt_uiTabs = '1,0,1';\r
901  */\r
902 \r
903 \r
904 /**\r
905  * Set the URL to SCAYT core. Required to switch to licensed version of SCAYT application.\r
906  * Further details at http://wiki.spellchecker.net/doku.php?id=3rd:wysiwyg:fckeditor:wscckf3l .\r
907  * @name CKEDITOR.config.scayt_srcUrl\r
908  * @type String\r
909  * @default ''\r
910  * @example\r
911  * config.scayt_srcUrl = "http://my-host/spellcheck/lf/scayt/scayt.js";\r
912  */\r
913 \r
914 /**\r
915  * Links SCAYT to custom dictionaries. It's a string containing dictionary ids\r
916  * separared by commas (","). Available only for licensed version.\r
917  * Further details at http://wiki.spellchecker.net/doku.php?id=custom_dictionary_support .\r
918  * @name CKEDITOR.config.scayt_customDictionaryIds\r
919  * @type String\r
920  * @default ''\r
921  * @example\r
922  * config.scayt_customDictionaryIds = '3021,3456,3478"';\r
923  */\r
924 \r
925 /**\r
926  * Makes it possible to activate a custom dictionary on SCAYT. The user\r
927  * dictionary name must be used. Available only for licensed version.\r
928  * @name CKEDITOR.config.scayt_userDictionaryName\r
929  * @type String\r
930  * @default ''\r
931  * @example\r
932  * config.scayt_userDictionaryName = 'MyDictionary';\r
933  */\r
934 \r
935 /**\r
936  * Makes it possible to place the SCAYT context menu items above others.\r
937  * @name CKEDITOR.config.scayt_contextMenuOntop\r
938  * @type Boolean\r
939  * @default false\r
940  * @example\r
941  * config.scayt_contextMenuOntop = true;\r
942  */\r
943 \r
944 /**\r
945  * Define order of placing of SCAYT context menu items by groups.\r
946  * It must be a string with one or more of the following\r
947  * words separated by a pipe ("|"):\r
948  * <ul>\r
949  *     <li>'suggest'     - main suggestion word list,</li>\r
950  *     <li>'moresuggest' - more suggestions word list,</li>\r
951  *     <li>'control'     - SCAYT commands, such as 'Ignore' and 'Add Word'</li>\r
952  * </ul>\r
953  *\r
954  * @name CKEDITOR.config.scayt_contextMenuItemsOrder\r
955  * @type String\r
956  * @default 'suggest|moresuggest|control'\r
957  * @example\r
958  * config.scayt_contextMenuItemsOrder = 'moresuggest|control|suggest';\r
959  */\r