2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Spell Check As You Type (SCAYT).
\r
8 * Button name : Scayt.
\r
13 var commandName = 'scaytcheck',
\r
15 scayt_paused = null,
\r
16 scayt_control_id = null;
\r
18 // Checks if a value exists in an array
\r
19 function in_array(needle, haystack)
\r
21 var found = false, key;
\r
22 for (key in haystack)
\r
24 if ((haystack[key] === needle) || ( haystack[key] == needle))
\r
33 var onEngineLoad = function()
\r
37 var createInstance = function() // Create new instance every time Document is created.
\r
39 // Initialise Scayt instance.
\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 = editor.config.scayt_customerid || '1:WvF0D4-UtPqN1-43nkD4-NKvUm2-daQqk3-LmNiI-z7Ysb4-mwry24-T8YrS3-Q2tpq2';
\r
46 oParams.customDictionaryIds = editor.config.scayt_customDictionaryIds || '';
\r
47 oParams.userDictionaryName = editor.config.scayt_userDictionaryName || '';
\r
48 oParams.sLang = editor.config.scayt_sLang || 'en_US';
\r
50 oParams.onBeforeChange = function()
\r
52 if ( !editor.checkDirty() )
\r
53 setTimeout( function(){ editor.resetDirty(); } );
\r
56 var scayt_custom_params = window.scayt_custom_params;
\r
57 if ( typeof scayt_custom_params == 'object')
\r
59 for ( var k in scayt_custom_params )
\r
61 oParams[ k ] = scayt_custom_params[ k ];
\r
64 // needs for restoring a specific scayt control settings
\r
65 if ( scayt_control_id )
\r
66 oParams.id = scayt_control_id;
\r
68 var scayt_control = new window.scayt( oParams );
\r
71 var lastInstance = plugin.instances[ editor.name ];
\r
74 scayt_control.sLang = lastInstance.sLang;
\r
75 scayt_control.option( lastInstance.option() );
\r
76 scayt_control.paused = lastInstance.paused;
\r
79 plugin.instances[ editor.name ] = scayt_control;
\r
81 //window.scayt.uiTags
\r
82 var menuGroup = 'scaytButton';
\r
83 var uiTabs = window.scayt.uiTags;
\r
86 for (var i = 0,l=4; i<l; i++)
\r
87 fTabs.push( uiTabs[i] && plugin.uiTabs[i] );
\r
89 plugin.uiTabs = fTabs;
\r
91 scayt_control.setDisabled( scayt_paused === false );
\r
94 editor.fire( 'showScaytState' );
\r
97 editor.on( 'contentDom', createInstance );
\r
98 editor.on( 'contentDomUnload', function()
\r
101 var scripts = CKEDITOR.document.getElementsByTag( 'script' ),
\r
102 scaytIdRegex = /^dojoIoScript(\d+)$/i,
\r
103 scaytSrcRegex = /^https?:\/\/svc\.spellchecker\.net\/spellcheck\/script\/ssrv\.cgi/i;
\r
105 for ( var i=0; i < scripts.count(); i++ )
\r
107 var script = scripts.getItem( i ),
\r
108 id = script.getId(),
\r
109 src = script.getAttribute( 'src' );
\r
111 if ( id && src && id.match( scaytIdRegex ) && src.match( scaytSrcRegex ))
\r
116 editor.on( 'beforeCommandExec', function( ev ) // Disable SCAYT before Source command execution.
\r
118 if ( (ev.data.name == 'source' || ev.data.name == 'newpage') && editor.mode == 'wysiwyg' )
\r
120 var scayt_instance = plugin.getScayt( editor );
\r
121 if ( scayt_instance )
\r
123 scayt_paused = scayt_instance.paused = !scayt_instance.disabled;
\r
124 // store a control id for restore a specific scayt control settings
\r
125 scayt_control_id = scayt_instance.id;
\r
126 scayt_instance.destroy( true );
\r
127 delete plugin.instances[ editor.name ];
\r
132 editor.on( 'destroy', function( ev )
\r
134 var editor = ev.editor,
\r
135 scayt_instance = plugin.getScayt( editor );
\r
136 // store a control id for restore a specific scayt control settings
\r
137 scayt_control_id = scayt_instance.id;
\r
138 scayt_instance.destroy( true );
\r
139 delete plugin.instances[ editor.name ];
\r
142 // Listen to data manipulation to reflect scayt markup.
\r
143 editor.on( 'afterSetData', function()
\r
145 if ( plugin.isScaytEnabled( editor ) ) {
\r
146 window.setTimeout( function(){ plugin.getScayt( editor ).refresh(); }, 10 );
\r
150 // Reload spell-checking for current word after insertion completed.
\r
151 editor.on( 'insertElement', function()
\r
153 var scayt_instance = plugin.getScayt( editor );
\r
154 if ( plugin.isScaytEnabled( editor ) )
\r
156 // Unlock the selection before reload, SCAYT will take
\r
157 // care selection update.
\r
158 if ( CKEDITOR.env.ie )
\r
159 editor.getSelection().unlock( true );
\r
161 // Swallow any SCAYT engine errors.
\r
162 window.setTimeout( function(){ scayt_instance.refresh(); }, 10 );
\r
164 }, this, null, 50 );
\r
166 editor.on( 'insertHtml', function()
\r
168 var scayt_instance = plugin.getScayt( editor );
\r
169 if ( plugin.isScaytEnabled( editor ) )
\r
171 // Unlock the selection before reload, SCAYT will take
\r
172 // care selection update.
\r
173 if ( CKEDITOR.env.ie )
\r
174 editor.getSelection().unlock( true );
\r
176 // Swallow any SCAYT engine errors.
\r
177 window.setTimeout( function(){ scayt_instance.refresh(); },10 );
\r
179 }, this, null, 50 );
\r
181 editor.on( 'scaytDialog', function( ev ) // Communication with dialog.
\r
183 ev.data.djConfig = window.djConfig;
\r
184 ev.data.scayt_control = plugin.getScayt( editor );
\r
185 ev.data.tab = openPage;
\r
186 ev.data.scayt = window.scayt;
\r
189 var dataProcessor = editor.dataProcessor,
\r
190 htmlFilter = dataProcessor && dataProcessor.htmlFilter;
\r
194 htmlFilter.addRules(
\r
198 span : function( element )
\r
200 if ( element.attributes.scayt_word && element.attributes.scaytid )
\r
202 delete element.name; // Write children, but don't write this node.
\r
211 if ( editor.document )
\r
215 CKEDITOR.plugins.scayt =
\r
217 engineLoaded : false,
\r
219 getScayt : function( editor )
\r
221 return this.instances[ editor.name ];
\r
223 isScaytReady : function( editor )
\r
225 return this.engineLoaded === true &&
\r
226 'undefined' !== typeof window.scayt && this.getScayt( editor );
\r
228 isScaytEnabled : function( editor )
\r
230 var scayt_instance = this.getScayt( editor );
\r
231 return ( scayt_instance ) ? scayt_instance.disabled === false : false;
\r
233 loadEngine : function( editor )
\r
235 // SCAYT doesn't work with Opera.
\r
236 if ( CKEDITOR.env.opera )
\r
239 if ( this.engineLoaded === true )
\r
240 return onEngineLoad.apply( editor ); // Add new instance.
\r
241 else if ( this.engineLoaded == -1 ) // We are waiting.
\r
242 return CKEDITOR.on( 'scaytReady', function(){ onEngineLoad.apply( editor ); } ); // Use function(){} to avoid rejection as duplicate.
\r
244 CKEDITOR.on( 'scaytReady', onEngineLoad, editor );
\r
245 CKEDITOR.on( 'scaytReady', function()
\r
247 this.engineLoaded = true;
\r
252 ); // First to run.
\r
254 this.engineLoaded = -1; // Loading in progress.
\r
256 // compose scayt url
\r
257 var protocol = document.location.protocol;
\r
258 // Default to 'http' for unknown.
\r
259 protocol = protocol.search( /https?:/) != -1? protocol : 'http:';
\r
260 var baseUrl = 'svc.spellchecker.net/spellcheck31/lf/scayt/scayt22.js';
\r
262 var scaytUrl = editor.config.scayt_srcUrl || ( protocol + '//' + baseUrl );
\r
263 var scaytConfigBaseUrl = plugin.parseUrl( scaytUrl ).path + '/';
\r
265 CKEDITOR._djScaytConfig =
\r
267 baseUrl: scaytConfigBaseUrl,
\r
272 CKEDITOR.fireOnce( 'scaytReady' );
\r
277 // Append javascript code.
\r
278 CKEDITOR.document.getHead().append(
\r
279 CKEDITOR.document.createElement( 'script',
\r
283 type : 'text/javascript',
\r
291 parseUrl : function ( data )
\r
294 if ( data.match && ( match = data.match(/(.*)[\/\\](.*?\.\w+)$/) ) )
\r
295 return { path: match[1], file: match[2] };
\r
301 var plugin = CKEDITOR.plugins.scayt;
\r
303 // Context menu constructing.
\r
304 var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, command, menugroup, menuOrder )
\r
306 editor.addCommand( commandName, command );
\r
308 // If the "menu" plugin is loaded, register the menu item.
\r
309 editor.addMenuItem( commandName,
\r
311 label : buttonLabel,
\r
312 command : commandName,
\r
318 var commandDefinition =
\r
320 preserveState : true,
\r
321 editorFocus : false,
\r
323 exec: function( editor )
\r
325 if ( plugin.isScaytReady( editor ) )
\r
327 var isEnabled = plugin.isScaytEnabled( editor );
\r
329 this.setState( isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_ON );
\r
331 var scayt_control = plugin.getScayt( editor );
\r
332 scayt_control.setDisabled( isEnabled );
\r
334 else if ( !editor.config.scayt_autoStartup && plugin.engineLoaded >= 0 ) // Load first time
\r
336 this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
338 editor.on( 'showScaytState', function()
\r
340 this.removeListener();
\r
341 this.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
345 plugin.loadEngine( editor );
\r
350 // Add scayt plugin.
\r
351 CKEDITOR.plugins.add( 'scayt',
\r
353 requires : [ 'menubutton' ],
\r
355 beforeInit : function( editor )
\r
357 // Register own rbc menu group.
\r
358 editor.config.menu_groups = 'scayt_suggest,scayt_moresuggest,scayt_control,' + editor.config.menu_groups;
\r
361 init : function( editor )
\r
363 var moreSuggestions = {};
\r
364 var mainSuggestions = {};
\r
367 var command = editor.addCommand( commandName, commandDefinition );
\r
369 // Add Options dialog.
\r
370 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );
\r
372 var confuiTabs = editor.config.scayt_uiTabs || '1,1,1';
\r
374 // string to array convert
\r
375 confuiTabs = confuiTabs.split( ',' );
\r
376 // check array length ! always must be 3 filled with 1 or 0
\r
377 for (var i=0,l=3; i<l; i++)
\r
379 var flag = parseInt(confuiTabs[i] || '1' ,10);
\r
380 uiTabs.push( flag );
\r
383 var menuGroup = 'scaytButton';
\r
384 editor.addMenuGroup( menuGroup );
\r
385 // combine menu items to render
\r
386 var uiMuneItems = {};
\r
389 uiMuneItems.scaytToggle =
\r
391 label : editor.lang.scayt.enable,
\r
392 command : commandName,
\r
396 if (uiTabs[0] == 1)
\r
397 uiMuneItems.scaytOptions =
\r
399 label : editor.lang.scayt.options,
\r
401 onClick : function()
\r
403 openPage = 'options';
\r
404 editor.openDialog( commandName );
\r
408 if (uiTabs[1] == 1)
\r
409 uiMuneItems.scaytLangs =
\r
411 label : editor.lang.scayt.langs,
\r
413 onClick : function()
\r
415 openPage = 'langs';
\r
416 editor.openDialog( commandName );
\r
419 if (uiTabs[2] == 1)
\r
420 uiMuneItems.scaytDict =
\r
422 label : editor.lang.scayt.dictionariesTab,
\r
424 onClick : function()
\r
426 openPage = 'dictionaries';
\r
427 editor.openDialog( commandName );
\r
431 uiMuneItems.scaytAbout =
\r
433 label : editor.lang.scayt.about,
\r
435 onClick : function()
\r
437 openPage = 'about';
\r
438 editor.openDialog( commandName );
\r
443 uiTabs[3] = 1; // about us tab is always on
\r
444 plugin.uiTabs = uiTabs;
\r
446 editor.addMenuItems( uiMuneItems );
\r
448 editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,
\r
450 label : editor.lang.scayt.title,
\r
451 title : editor.lang.scayt.title,
\r
452 className : 'cke_button_scayt',
\r
453 onRender: function()
\r
455 command.on( 'state', function()
\r
457 this.setState( command.state );
\r
461 onMenu : function()
\r
463 var isEnabled = plugin.isScaytEnabled( editor );
\r
465 editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];
\r
468 scaytToggle : CKEDITOR.TRISTATE_OFF,
\r
469 scaytOptions : isEnabled && plugin.uiTabs[0] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
470 scaytLangs : isEnabled && plugin.uiTabs[1] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
471 scaytDict : isEnabled && plugin.uiTabs[2] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
472 scaytAbout : isEnabled && plugin.uiTabs[3] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
\r
477 // If the "contextmenu" plugin is loaded, register the listeners.
\r
478 if ( editor.contextMenu && editor.addMenuItems )
\r
480 editor.contextMenu.addListener( function( )
\r
482 if ( !plugin.isScaytEnabled( editor ) )
\r
485 var scayt_control = plugin.getScayt( editor ),
\r
486 node = scayt_control.getScaytNode();
\r
491 var word = scayt_control.getWord( node );
\r
496 var sLang = scayt_control.getLang(),
\r
498 items_suggestion = window.scayt.getSuggestion( word, sLang );
\r
499 if ( !items_suggestion || !items_suggestion.length )
\r
501 // Remove unused commands and menuitems
\r
502 for ( i in moreSuggestions )
\r
504 delete editor._.menuItems[ i ];
\r
505 delete editor._.commands[ i ];
\r
507 for ( i in mainSuggestions )
\r
509 delete editor._.menuItems[ i ];
\r
510 delete editor._.commands[ i ];
\r
512 moreSuggestions = {}; // Reset items.
\r
513 mainSuggestions = {};
\r
515 var moreSuggestionsUnable = editor.config.scayt_moreSuggestions || 'on';
\r
516 var moreSuggestionsUnableAdded = false;
\r
518 var maxSuggestions = editor.config.scayt_maxSuggestions;
\r
519 ( typeof maxSuggestions != 'number' ) && ( maxSuggestions = 5 );
\r
520 !maxSuggestions && ( maxSuggestions = items_suggestion.length );
\r
522 var contextCommands = editor.config.scayt_contextCommands || 'all';
\r
523 contextCommands = contextCommands.split( '|' );
\r
525 for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )
\r
527 var commandName = 'scayt_suggestion_' + items_suggestion[i].replace( ' ', '_' );
\r
528 var exec = ( function( el, s )
\r
533 scayt_control.replace(el, s);
\r
536 })( node, items_suggestion[i] );
\r
538 if ( i < maxSuggestions )
\r
540 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
\r
541 commandName, exec, 'scayt_suggest', i + 1 );
\r
542 _r[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
543 mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
545 else if ( moreSuggestionsUnable == 'on' )
\r
547 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
\r
548 commandName, exec, 'scayt_moresuggest', i + 1 );
\r
549 moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
550 moreSuggestionsUnableAdded = true;
\r
554 if ( moreSuggestionsUnableAdded )
\r
556 // Register the More suggestions group;
\r
557 editor.addMenuItem( 'scayt_moresuggest',
\r
559 label : editor.lang.scayt.moreSuggestions,
\r
560 group : 'scayt_moresuggest',
\r
562 getItems : function()
\r
564 return moreSuggestions;
\r
567 mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;
\r
570 if ( in_array( 'all', contextCommands ) || in_array( 'ignore', contextCommands) )
\r
572 var ignore_command = {
\r
574 scayt_control.ignore( node );
\r
577 addButtonCommand( editor, 'ignore', editor.lang.scayt.ignore, 'scayt_ignore', ignore_command, 'scayt_control', 1 );
\r
578 mainSuggestions[ 'scayt_ignore' ] = CKEDITOR.TRISTATE_OFF;
\r
581 if ( in_array( 'all', contextCommands ) || in_array( 'ignoreall', contextCommands ) )
\r
583 var ignore_all_command = {
\r
585 scayt_control.ignoreAll( node );
\r
588 addButtonCommand(editor, 'ignore_all', editor.lang.scayt.ignoreAll, 'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);
\r
589 mainSuggestions['scayt_ignore_all'] = CKEDITOR.TRISTATE_OFF;
\r
592 if ( in_array( 'all', contextCommands ) || in_array( 'add', contextCommands ) )
\r
594 var addword_command = {
\r
596 window.scayt.addWordToUserDictionary( node );
\r
599 addButtonCommand(editor, 'add_word', editor.lang.scayt.addWord, 'scayt_add_word', addword_command, 'scayt_control', 3);
\r
600 mainSuggestions['scayt_add_word'] = CKEDITOR.TRISTATE_OFF;
\r
603 if ( scayt_control.fireOnContextMenu )
\r
604 scayt_control.fireOnContextMenu( editor );
\r
606 return mainSuggestions;
\r
611 if ( editor.config.scayt_autoStartup )
\r
613 var showInitialState = function()
\r
615 editor.removeListener( 'showScaytState', showInitialState );
\r
616 command.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
618 editor.on( 'showScaytState', showInitialState );
\r
619 editor.on( 'instanceReady', function()
\r
621 plugin.loadEngine( editor );
\r
626 afterInit : function( editor )
\r
628 // Prevent word marker line from displaying in elements path. (#3570)
\r
629 var elementsPathFilters;
\r
630 if ( editor._.elementsPath && ( elementsPathFilters = editor._.elementsPath.filters ) )
\r
632 elementsPathFilters.push( function( element )
\r
634 if ( element.hasAttribute( 'scaytid' ) )
\r
644 * If enabled (true), turns on SCAYT automatically after loading the editor.
\r
645 * @name CKEDITOR.config.scayt_autoStartup
\r
649 * config.scayt_autoStartup = true;
\r
653 * Defines the number of SCAYT suggestions to show in the main context menu.
\r
654 * The possible values are:
\r
656 * <li>0 (zero): All suggestions are displayed in the main context menu.</li>
\r
657 * <li>Positive number: The maximum number of suggestions to shown in context
\r
658 * menu. Other entries will be shown in "More Suggestions" sub-menu.</li>
\r
659 * <li>Negative number: No suggestions are shown in the main context menu. All
\r
660 * entries will be listed in the "Suggestions" sub-menu.</li>
\r
662 * @name CKEDITOR.config.scayt_maxSuggestions
\r
666 * // Display only three suggestions in the main context menu.
\r
667 * config.scayt_maxSuggestions = 3;
\r
669 * // Do not show the suggestions directly.
\r
670 * config.scayt_maxSuggestions = -1;
\r
674 * Sets the customer ID for SCAYT. Required for migration from free version
\r
675 * with banner to paid version.
\r
676 * @name CKEDITOR.config.scayt_customerid
\r
680 * // Load SCAYT using my customer ID.
\r
681 * config.scayt_customerid = 'your-encrypted-customer-id';
\r
685 * Enables/disables the "More Suggestions" sub-menu in the context menu.
\r
686 * The possible values are "on" or "off".
\r
687 * @name CKEDITOR.config.scayt_moreSuggestions
\r
691 * // Disables the "More Suggestions" sub-menu.
\r
692 * config.scayt_moreSuggestions = 'off';
\r
696 * Customizes the display of SCAYT context menu commands ("Add Word", "Ignore"
\r
697 * and "Ignore All"). It must be a string with one or more of the following
\r
698 * words separated by a pipe ("|"):
\r
700 * <li>"off": disables all options.</li>
\r
701 * <li>"all": enables all options.</li>
\r
702 * <li>"ignore": enables the "Ignore" option.</li>
\r
703 * <li>"ignoreall": enables the "Ignore All" option.</li>
\r
704 * <li>"add": enables the "Add Word" option.</li>
\r
706 * @name CKEDITOR.config.scayt_contextCommands
\r
710 * // Show only "Add Word" and "Ignore All" in the context menu.
\r
711 * config.scayt_contextCommands = 'add|ignoreall';
\r
715 * Sets the default spellchecking language for SCAYT.
\r
716 * @name CKEDITOR.config.scayt_sLang
\r
720 * // Sets SCAYT to German.
\r
721 * config.scayt_sLang = 'de_DE';
\r
725 * Sets the visibility of the SCAYT tabs in the settings dialog and toolbar
\r
726 * button. The value must contain a "1" (enabled) or "0" (disabled) number for
\r
727 * each of the following entries, in this precise order, separated by a
\r
728 * comma (","): "Options", "Languages" and "Dictionary".
\r
729 * @name CKEDITOR.config.scayt_uiTabs
\r
733 * // Hide the "Languages" tab.
\r
734 * config.scayt_uiTabs = '1,0,1';
\r
739 * Set the URL to SCAYT core. Required to switch to licensed version of SCAYT application.
\r
740 * Further details at http://wiki.spellchecker.net/doku.php?id=3rd:wysiwyg:fckeditor:wscckf3l .
\r
741 * @name CKEDITOR.config.scayt_srcUrl
\r
745 * config.scayt_srcUrl = "http://my-host/spellcheck/lf/scayt/scayt.js";
\r
749 * Links SCAYT to custom dictionaries. It's a string containing dictionary ids
\r
750 * separared by commas (","). Available only for licensed version.
\r
751 * Further details at http://wiki.spellchecker.net/doku.php?id=custom_dictionary_support .
\r
752 * @name CKEDITOR.config.scayt_customDictionaryIds
\r
756 * config.scayt_customDictionaryIds = '3021,3456,3478"';
\r
760 * Makes it possible to activate a custom dictionary on SCAYT. The user
\r
761 * dictionary name must be used. Available only for licensed version.
\r
762 * @name CKEDITOR.config.scayt_userDictionaryName
\r
766 * config.scayt_userDictionaryName = 'MyDictionary';
\r