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
17 // Checks if a value exists in an array
\r
18 function in_array(needle, haystack)
\r
20 var found = false, key;
\r
21 for (key in haystack)
\r
23 if ((haystack[key] === needle) || ( haystack[key] == needle))
\r
32 var onEngineLoad = function()
\r
36 var createInstance = function() // Create new instance every time Document is created.
\r
38 // Initialise Scayt instance.
\r
40 oParams.srcNodeRef = editor.document.getWindow().$.frameElement; // Get the iframe.
\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
48 if ( CKEDITOR._scaytParams )
\r
50 for ( var k in CKEDITOR._scaytParams )
\r
52 oParams[ k ] = CKEDITOR._scaytParams[ k ];
\r
56 var scayt_control = new window.scayt( oParams );
\r
59 var lastInstance = plugin.instances[ editor.name ];
\r
62 scayt_control.sLang = lastInstance.sLang;
\r
63 scayt_control.option( lastInstance.option() );
\r
64 scayt_control.paused = lastInstance.paused;
\r
67 plugin.instances[ editor.name ] = scayt_control;
\r
69 //window.scayt.uiTags
\r
70 var menuGroup = 'scaytButton';
\r
71 var uiTabs = window.scayt.uiTags;
\r
74 for (var i = 0,l=4; i<l; i++)
\r
75 fTabs.push( uiTabs[i] && plugin.uiTabs[i] );
\r
77 plugin.uiTabs = fTabs;
\r
79 scayt_control.setDisabled( scayt_paused === false );
\r
82 editor.fire( 'showScaytState' );
\r
85 editor.on( 'contentDom', createInstance );
\r
86 editor.on( 'contentDomUnload', function()
\r
89 var scripts = CKEDITOR.document.getElementsByTag( 'script' ),
\r
90 scaytIdRegex = /^dojoIoScript(\d+)$/i,
\r
91 scaytSrcRegex = /^https?:\/\/svc\.spellchecker\.net\/spellcheck\/script\/ssrv\.cgi/i;
\r
93 for ( var i=0; i < scripts.count(); i++ )
\r
95 var script = scripts.getItem( i ),
\r
96 id = script.getId(),
\r
97 src = script.getAttribute( 'src' );
\r
99 if ( id && src && id.match( scaytIdRegex ) && src.match( scaytSrcRegex ))
\r
104 editor.on( 'beforeCommandExec', function( ev ) // Disable SCAYT before Source command execution.
\r
106 if ( (ev.data.name == 'source' || ev.data.name == 'newpage') && editor.mode == 'wysiwyg' )
\r
108 var scayt_instanse = plugin.getScayt( editor );
\r
109 if ( scayt_instanse )
\r
111 scayt_paused = scayt_instanse.paused = !scayt_instanse.disabled;
\r
112 scayt_instanse.destroy();
\r
113 delete plugin.instances[ editor.name ];
\r
119 editor.on( 'destroy', function()
\r
121 plugin.getScayt( editor ).destroy();
\r
123 // Listen to data manipulation to reflect scayt markup.
\r
124 editor.on( 'afterSetData', function()
\r
126 if ( plugin.isScaytEnabled( editor ) )
\r
127 plugin.getScayt( editor ).refresh();
\r
130 // Reload spell-checking for current word after insertion completed.
\r
131 editor.on( 'insertElement', function()
\r
133 var scayt_instance = plugin.getScayt( editor );
\r
134 if ( plugin.isScaytEnabled( editor ) )
\r
136 // Unlock the selection before reload, SCAYT will take
\r
137 // care selection update.
\r
138 if ( CKEDITOR.env.ie )
\r
139 editor.getSelection().unlock( true );
\r
141 // Swallow any SCAYT engine errors.
\r
143 scayt_instance.refresh();
\r
147 }, this, null, 50 );
\r
149 editor.on( 'insertHtml', function()
\r
152 var scayt_instance = plugin.getScayt( editor );
\r
153 if ( plugin.isScaytEnabled( editor ) )
\r
155 // Unlock the selection before reload, SCAYT will take
\r
156 // care selection update.
\r
157 if ( CKEDITOR.env.ie )
\r
158 editor.getSelection().unlock( true );
\r
160 // Swallow any SCAYT engine errors.
\r
162 scayt_instance.refresh();
\r
166 }, this, null, 50 );
\r
168 editor.on( 'scaytDialog', function( ev ) // Communication with dialog.
\r
170 ev.data.djConfig = window.djConfig;
\r
171 ev.data.scayt_control = plugin.getScayt( editor );
\r
172 ev.data.tab = openPage;
\r
173 ev.data.scayt = window.scayt;
\r
176 var dataProcessor = editor.dataProcessor,
\r
177 htmlFilter = dataProcessor && dataProcessor.htmlFilter;
\r
180 htmlFilter.addRules(
\r
184 span : function( element )
\r
186 if ( element.attributes.scayt_word && element.attributes.scaytid )
\r
188 delete element.name; // Write children, but don't write this node.
\r
197 if ( editor.document )
\r
201 CKEDITOR.plugins.scayt =
\r
203 engineLoaded : false,
\r
205 getScayt : function( editor )
\r
207 return this.instances[ editor.name ];
\r
209 isScaytReady : function( editor )
\r
211 return this.engineLoaded === true &&
\r
212 'undefined' !== typeof window.scayt && this.getScayt( editor );
\r
214 isScaytEnabled : function( editor )
\r
216 var scayt_instanse = this.getScayt( editor );
\r
217 return ( scayt_instanse ) ? scayt_instanse.disabled === false : false;
\r
219 loadEngine : function( editor )
\r
221 if ( this.engineLoaded === true )
\r
222 return onEngineLoad.apply( editor ); // Add new instance.
\r
223 else if ( this.engineLoaded == -1 ) // We are waiting.
\r
224 return CKEDITOR.on( 'scaytReady', function(){ onEngineLoad.apply( editor );} ); // Use function(){} to avoid rejection as duplicate.
\r
226 CKEDITOR.on( 'scaytReady', onEngineLoad, editor );
\r
227 CKEDITOR.on( 'scaytReady', function()
\r
229 this.engineLoaded = true;
\r
233 0 ); // First to run.
\r
235 this.engineLoaded = -1; // Loading in progress.
\r
237 // compose scayt url
\r
238 var protocol = document.location.protocol;
\r
239 // Default to 'http' for unknown.
\r
240 protocol = protocol.search( /https?:/) != -1? protocol : 'http:';
\r
241 var baseUrl = "svc.spellchecker.net/spellcheck3/lf/scayt/scayt21.js";
\r
243 var scaytUrl = editor.config.scayt_srcUrl || ( protocol + "//" + baseUrl );
\r
244 var scaytConfigBaseUrl = plugin.parseUrl( scaytUrl ).path + "/";
\r
246 CKEDITOR._djScaytConfig =
\r
248 baseUrl: scaytConfigBaseUrl,
\r
253 CKEDITOR.fireOnce( "scaytReady" );
\r
258 // Append javascript code.
\r
259 CKEDITOR.document.getHead().append(
\r
260 CKEDITOR.document.createElement( 'script',
\r
264 type : 'text/javascript',
\r
272 parseUrl : function ( data )
\r
275 if ( data.match && ( match = data.match(/(.*)[\/\\](.*?\.\w+)$/) ) )
\r
276 return { path: match[1], file: match[2] };
\r
282 var plugin = CKEDITOR.plugins.scayt;
\r
284 // Context menu constructing.
\r
285 var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, command, menugroup, menuOrder )
\r
287 editor.addCommand( commandName, command );
\r
289 // If the "menu" plugin is loaded, register the menu item.
\r
290 editor.addMenuItem( commandName,
\r
292 label : buttonLabel,
\r
293 command : commandName,
\r
299 var commandDefinition =
\r
301 preserveState : true,
\r
302 editorFocus : false,
\r
304 exec: function( editor )
\r
306 if ( plugin.isScaytReady( editor ) )
\r
308 var isEnabled = plugin.isScaytEnabled( editor );
\r
310 this.setState( isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_ON );
\r
312 var scayt_control = plugin.getScayt( editor );
\r
313 scayt_control.setDisabled( isEnabled );
\r
315 else if ( !editor.config.scayt_autoStartup && plugin.engineLoaded >= 0 ) // Load first time
\r
317 this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
319 editor.on( 'showScaytState', function()
\r
321 this.removeListener();
\r
322 this.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
326 plugin.loadEngine( editor );
\r
331 // Add scayt plugin.
\r
332 CKEDITOR.plugins.add( 'scayt',
\r
334 requires : [ 'menubutton' ],
\r
336 beforeInit : function( editor )
\r
338 // Register own rbc menu group.
\r
339 editor.config.menu_groups = 'scayt_suggest,scayt_moresuggest,scayt_control,' + editor.config.menu_groups;
\r
342 init : function( editor )
\r
344 var moreSuggestions = {};
\r
345 var mainSuggestions = {};
\r
348 var command = editor.addCommand( commandName, commandDefinition );
\r
350 // Add Options dialog.
\r
351 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );
\r
353 var confuiTabs = editor.config.scayt_uiTabs || "1,1,1";
\r
355 // string tp array convert
\r
356 confuiTabs = confuiTabs.split(",");
\r
357 // check array length ! allwaays must be 3 filled with 1 or 0
\r
358 for (var i=0,l=3; i<l; i++){
\r
359 var flag = parseInt(confuiTabs[i] || "1" ,10);
\r
360 uiTabs.push( flag );
\r
363 var menuGroup = 'scaytButton';
\r
364 editor.addMenuGroup( menuGroup );
\r
365 // combine menu items to render
\r
366 var uiMuneItems = {};
\r
369 uiMuneItems.scaytToggle =
\r
371 label : editor.lang.scayt.enable,
\r
372 command : commandName,
\r
376 if (uiTabs[0] == 1)
\r
377 uiMuneItems.scaytOptions =
\r
379 label : editor.lang.scayt.options,
\r
381 onClick : function()
\r
383 openPage = 'options';
\r
384 editor.openDialog( commandName );
\r
388 if (uiTabs[1] == 1)
\r
389 uiMuneItems.scaytLangs =
\r
391 label : editor.lang.scayt.langs,
\r
393 onClick : function()
\r
395 openPage = 'langs';
\r
396 editor.openDialog( commandName );
\r
399 if (uiTabs[2] == 1)
\r
400 uiMuneItems.scaytDict =
\r
402 label : editor.lang.scayt.dictionariesTab,
\r
404 onClick : function()
\r
406 openPage = 'dictionaries';
\r
407 editor.openDialog( commandName );
\r
411 uiMuneItems.scaytAbout =
\r
413 label : editor.lang.scayt.about,
\r
415 onClick : function()
\r
417 openPage = 'about';
\r
418 editor.openDialog( commandName );
\r
423 uiTabs[3] = 1; // about us tab is allways on
\r
424 plugin.uiTabs = uiTabs;
\r
426 editor.addMenuItems( uiMuneItems );
\r
428 editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,
\r
430 label : editor.lang.scayt.title,
\r
431 title : editor.lang.scayt.title,
\r
432 className : 'cke_button_scayt',
\r
433 onRender: function()
\r
435 command.on( 'state', function()
\r
437 this.setState( command.state );
\r
441 onMenu : function()
\r
443 var isEnabled = plugin.isScaytEnabled( editor );
\r
445 editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];
\r
448 scaytToggle : CKEDITOR.TRISTATE_OFF,
\r
449 scaytOptions : isEnabled && plugin.uiTabs[0] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
450 scaytLangs : isEnabled && plugin.uiTabs[1] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
451 scaytDict : isEnabled && plugin.uiTabs[2] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
452 scaytAbout : isEnabled && plugin.uiTabs[3] ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
\r
457 // If the "contextmenu" plugin is loaded, register the listeners.
\r
458 if ( editor.contextMenu && editor.addMenuItems )
\r
460 editor.contextMenu.addListener( function( element )
\r
462 if ( !( plugin.isScaytEnabled( editor ) && element ) )
\r
465 var scayt_control = plugin.getScayt( editor ),
\r
466 word = scayt_control.getWord( element.$ );
\r
471 var sLang = scayt_control.getLang(),
\r
473 items_suggestion = window.scayt.getSuggestion( word, sLang );
\r
474 if (!items_suggestion || !items_suggestion.length )
\r
476 // Remove unused commands and menuitems
\r
477 for ( i in moreSuggestions )
\r
479 delete editor._.menuItems[ i ];
\r
480 delete editor._.commands[ i ];
\r
482 for ( i in mainSuggestions )
\r
484 delete editor._.menuItems[ i ];
\r
485 delete editor._.commands[ i ];
\r
487 moreSuggestions = {}; // Reset items.
\r
488 mainSuggestions = {};
\r
490 var moreSuggestionsUnable = editor.config.scayt_moreSuggestions || "on";
\r
491 var moreSuggestionsUnableAdded = false;
\r
493 var maxSuggestions = editor.config.scayt_maxSuggestions;
\r
494 ( typeof maxSuggestions != 'number' ) && ( maxSuggestions = 5 );
\r
495 !maxSuggestions && ( maxSuggestions = items_suggestion.length );
\r
497 var contextCommands = editor.config.scayt_contextCommands || "all";
\r
498 contextCommands = contextCommands.split("|");
\r
500 for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )
\r
502 var commandName = 'scayt_suggestion_' + items_suggestion[i].replace( ' ', '_' );
\r
503 var exec = ( function( el, s )
\r
508 scayt_control.replace(el, s);
\r
511 })( element.$, items_suggestion[i] );
\r
513 if ( i < maxSuggestions )
\r
515 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
\r
516 commandName, exec, 'scayt_suggest', i + 1 );
\r
517 _r[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
518 mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
520 else if ( moreSuggestionsUnable == "on" )
\r
522 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
\r
523 commandName, exec, 'scayt_moresuggest', i + 1 );
\r
524 moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
525 moreSuggestionsUnableAdded = true;
\r
529 if ( moreSuggestionsUnableAdded ){
\r
530 // Rgister the More suggestions group;
\r
531 editor.addMenuItem( 'scayt_moresuggest',
\r
533 label : editor.lang.scayt.moreSuggestions,
\r
534 group : 'scayt_moresuggest',
\r
536 getItems : function()
\r
538 return moreSuggestions;
\r
541 mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;
\r
545 if ( in_array( "all",contextCommands ) || in_array("ignore",contextCommands) )
\r
547 var ignore_command = {
\r
549 scayt_control.ignore(element.$);
\r
552 addButtonCommand(editor, 'ignore', editor.lang.scayt.ignore, 'scayt_ignore', ignore_command, 'scayt_control', 1);
\r
553 mainSuggestions['scayt_ignore'] = CKEDITOR.TRISTATE_OFF;
\r
556 if ( in_array( "all",contextCommands ) || in_array("ignoreall",contextCommands) )
\r
558 var ignore_all_command = {
\r
560 scayt_control.ignoreAll(element.$);
\r
563 addButtonCommand(editor, 'ignore_all', editor.lang.scayt.ignoreAll, 'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);
\r
564 mainSuggestions['scayt_ignore_all'] = CKEDITOR.TRISTATE_OFF;
\r
567 if ( in_array( "all",contextCommands ) || in_array("add",contextCommands) )
\r
569 var addword_command = {
\r
571 window.scayt.addWordToUserDictionary(element.$);
\r
574 addButtonCommand(editor, 'add_word', editor.lang.scayt.addWord, 'scayt_add_word', addword_command, 'scayt_control', 3);
\r
575 mainSuggestions['scayt_add_word'] = CKEDITOR.TRISTATE_OFF;
\r
578 if ( scayt_control.fireOnContextMenu )
\r
579 scayt_control.fireOnContextMenu( editor );
\r
581 return mainSuggestions;
\r
586 if ( editor.config.scayt_autoStartup )
\r
588 var showInitialState = function()
\r
590 editor.removeListener( 'showScaytState', showInitialState );
\r
591 command.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
593 editor.on( 'showScaytState', showInitialState );
\r
595 plugin.loadEngine( editor );
\r
601 // TODO: Documentation
\r
602 // CKEDITOR.config.scayt_maxSuggestions
\r
603 // CKEDITOR.config.scayt_autoStartup
\r