2 Copyright (c) 2003-2009, 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
16 var onEngineLoad = function()
\r
20 var createInstance = function() // Create new instance every time Document is created.
\r
22 // Initialise Scayt instance.
\r
24 oParams.srcNodeRef = editor.document.getWindow().$.frameElement; // Get the iframe.
\r
25 // syntax : AppName.AppVersion@AppRevision
\r
26 oParams.assocApp = "CKEDITOR." + CKEDITOR.version + "@" + CKEDITOR.revision;
\r
28 oParams.customerid = editor.config.scayt_customerid || "1:11111111111111111111111111111111111111";
\r
29 oParams.customDictionaryName = editor.config.scayt_customDictionaryName;
\r
30 oParams.userDictionaryName = editor.config.scayt_userDictionaryName;
\r
31 oParams.defLang = editor.scayt_defLang;
\r
33 if ( CKEDITOR._scaytParams )
\r
35 for ( var k in CKEDITOR._scaytParams )
\r
37 oParams[ k ] = CKEDITOR._scaytParams[ k ];
\r
41 var scayt_control = new window.scayt( oParams );
\r
44 var lastInstance = plugin.instances[ editor.name ];
\r
47 scayt_control.sLang = lastInstance.sLang;
\r
48 scayt_control.option( lastInstance.option() );
\r
49 scayt_control.paused = lastInstance.paused;
\r
52 plugin.instances[ editor.name ] = scayt_control;
\r
55 scayt_control.setDisabled( scayt_control.paused === false ); // I really don't know why it causes JS error in IE
\r
57 editor.fire( 'showScaytState' );
\r
60 editor.on( 'contentDom', createInstance );
\r
61 editor.on( 'contentDomUnload', function()
\r
64 var scripts = CKEDITOR.document.getElementsByTag( 'script' ),
\r
65 scaytIdRegex = /^dojoIoScript(\d+)$/i,
\r
66 scaytSrcRegex = /^https?:\/\/svc\.spellchecker\.net\/spellcheck\/script\/ssrv\.cgi/i;
\r
68 for ( var i=0; i < scripts.count(); i++ )
\r
70 var script = scripts.getItem( i ),
\r
71 id = script.getId(),
\r
72 src = script.getAttribute( 'src' );
\r
74 if ( id && src && id.match( scaytIdRegex ) && src.match( scaytSrcRegex ))
\r
79 editor.on( 'beforeCommandExec', function( ev ) // Disable SCAYT before Source command execution.
\r
81 if ( (ev.data.name == 'source' || ev.data.name == 'newpage') && editor.mode == 'wysiwyg' )
\r
83 var scayt_instanse = plugin.getScayt( editor );
\r
84 if ( scayt_instanse )
\r
86 scayt_instanse.paused = !scayt_instanse.disabled;
\r
87 scayt_instanse.destroy();
\r
88 delete plugin.instances[ editor.name ];
\r
93 // Listen to data manipulation to reflect scayt markup.
\r
94 editor.on( 'afterSetData', function()
\r
96 if ( plugin.isScaytEnabled( editor ) )
\r
97 plugin.getScayt( editor ).refresh();
\r
100 // Reload spell-checking for current word after insertion completed.
\r
101 editor.on( 'insertElement', function()
\r
103 var scayt_instance = plugin.getScayt( editor );
\r
104 if ( plugin.isScaytEnabled( editor ) )
\r
106 // Unlock the selection before reload, SCAYT will take
\r
107 // care selection update.
\r
108 if ( CKEDITOR.env.ie )
\r
109 editor.getSelection().unlock( true );
\r
111 // Swallow any SCAYT engine errors.
\r
113 scayt_instance.refresh();
\r
117 }, this, null, 50 );
\r
119 editor.on( 'scaytDialog', function( ev ) // Communication with dialog.
\r
121 ev.data.djConfig = window.djConfig;
\r
122 ev.data.scayt_control = plugin.getScayt( editor );
\r
123 ev.data.tab = openPage;
\r
124 ev.data.scayt = window.scayt;
\r
127 var dataProcessor = editor.dataProcessor,
\r
128 htmlFilter = dataProcessor && dataProcessor.htmlFilter;
\r
131 htmlFilter.addRules(
\r
135 span : function( element )
\r
137 if ( element.attributes.scayt_word && element.attributes.scaytid )
\r
139 delete element.name; // Write children, but don't write this node.
\r
148 if ( editor.document )
\r
152 CKEDITOR.plugins.scayt =
\r
154 engineLoaded : false,
\r
156 getScayt : function( editor )
\r
158 return this.instances[ editor.name ];
\r
160 isScaytReady : function( editor )
\r
162 return this.engineLoaded === true &&
\r
163 'undefined' !== typeof window.scayt && this.getScayt( editor );
\r
165 isScaytEnabled : function( editor )
\r
167 var scayt_instanse = this.getScayt( editor );
\r
168 return ( scayt_instanse ) ? scayt_instanse.disabled === false : false;
\r
170 loadEngine : function( editor )
\r
172 if ( this.engineLoaded === true )
\r
173 return onEngineLoad.apply( editor ); // Add new instance.
\r
174 else if ( this.engineLoaded == -1 ) // We are waiting.
\r
175 return CKEDITOR.on( 'scaytReady', function(){ onEngineLoad.apply( editor );} ); // Use function(){} to avoid rejection as duplicate.
\r
177 CKEDITOR.on( 'scaytReady', onEngineLoad, editor );
\r
178 CKEDITOR.on( 'scaytReady', function()
\r
180 this.engineLoaded = true;
\r
184 0 ); // First to run.
\r
186 this.engineLoaded = -1; // Loading in progress.
\r
188 // compose scayt url
\r
189 var protocol = document.location.protocol;
\r
190 // Default to 'http' for unknown.
\r
191 protocol = protocol.search( /https?:/) != -1? protocol : 'http:';
\r
192 var baseUrl = "svc.spellchecker.net/spellcheck/lf/scayt/scayt1.js";
\r
194 var scaytUrl = editor.config.scayt_srcUrl || ( protocol + "//" + baseUrl );
\r
195 var scaytConfigBaseUrl = plugin.parseUrl( scaytUrl ).path + "/";
\r
197 CKEDITOR._djScaytConfig =
\r
199 baseUrl: scaytConfigBaseUrl,
\r
204 CKEDITOR.fireOnce( "scaytReady" );
\r
209 // Append javascript code.
\r
210 CKEDITOR.document.getHead().append(
\r
211 CKEDITOR.document.createElement( 'script',
\r
215 type : 'text/javascript',
\r
223 parseUrl : function ( data )
\r
226 if ( data.match && ( match = data.match(/(.*)[\/\\](.*?\.\w+)$/) ) )
\r
227 return { path: match[1], file: match[2] };
\r
233 var plugin = CKEDITOR.plugins.scayt;
\r
235 // Context menu constructing.
\r
236 var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, command, menugroup, menuOrder )
\r
238 editor.addCommand( commandName, command );
\r
240 // If the "menu" plugin is loaded, register the menu item.
\r
241 editor.addMenuItem( commandName,
\r
243 label : buttonLabel,
\r
244 command : commandName,
\r
250 var commandDefinition =
\r
252 preserveState : true,
\r
253 editorFocus : false,
\r
255 exec: function( editor )
\r
257 if ( plugin.isScaytReady( editor ) )
\r
259 var isEnabled = plugin.isScaytEnabled( editor );
\r
261 this.setState( isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_ON );
\r
263 var scayt_control = plugin.getScayt( editor );
\r
264 scayt_control.setDisabled( isEnabled );
\r
266 else if ( !editor.config.scayt_autoStartup && plugin.engineLoaded >= 0 ) // Load first time
\r
268 this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
270 editor.on( 'showScaytState', function()
\r
272 this.removeListener();
\r
273 this.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
277 plugin.loadEngine( editor );
\r
282 // Add scayt plugin.
\r
283 CKEDITOR.plugins.add( 'scayt',
\r
285 requires : [ 'menubutton' ],
\r
287 beforeInit : function( editor )
\r
289 // Register own rbc menu group.
\r
290 editor.config.menu_groups = 'scayt_suggest,scayt_moresuggest,scayt_control,' + editor.config.menu_groups;
\r
293 init : function( editor )
\r
295 var moreSuggestions = {};
\r
296 var mainSuggestions = {};
\r
299 var command = editor.addCommand( commandName, commandDefinition );
\r
301 // Add Options dialog.
\r
302 CKEDITOR.dialog.add( commandName, CKEDITOR.getUrl( this.path + 'dialogs/options.js' ) );
\r
304 var menuGroup = 'scaytButton';
\r
305 editor.addMenuGroup( menuGroup );
\r
306 editor.addMenuItems(
\r
310 label : editor.lang.scayt.enable,
\r
311 command : commandName,
\r
317 label : editor.lang.scayt.options,
\r
319 onClick : function()
\r
321 openPage = 'options';
\r
322 editor.openDialog( commandName );
\r
328 label : editor.lang.scayt.langs,
\r
330 onClick : function()
\r
332 openPage = 'langs';
\r
333 editor.openDialog( commandName );
\r
339 label : editor.lang.scayt.about,
\r
341 onClick : function()
\r
343 openPage = 'about';
\r
344 editor.openDialog( commandName );
\r
349 editor.ui.add( 'Scayt', CKEDITOR.UI_MENUBUTTON,
\r
351 label : editor.lang.scayt.title,
\r
352 title : editor.lang.scayt.title,
\r
353 className : 'cke_button_scayt',
\r
354 onRender: function()
\r
356 command.on( 'state', function()
\r
358 this.setState( command.state );
\r
362 onMenu : function()
\r
364 var isEnabled = plugin.isScaytEnabled( editor );
\r
366 editor.getMenuItem( 'scaytToggle' ).label = editor.lang.scayt[ isEnabled ? 'disable' : 'enable' ];
\r
369 scaytToggle : CKEDITOR.TRISTATE_OFF,
\r
370 scaytOptions : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
371 scaytLangs : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED,
\r
372 scaytAbout : isEnabled ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED
\r
377 // If the "contextmenu" plugin is loaded, register the listeners.
\r
378 if ( editor.contextMenu && editor.addMenuItems )
\r
380 editor.contextMenu.addListener( function( element )
\r
382 if ( !( plugin.isScaytEnabled( editor ) && element ) )
\r
385 var scayt_control = plugin.getScayt( editor ),
\r
386 word = scayt_control.getWord( element.$ );
\r
391 var sLang = scayt_control.getLang(),
\r
393 items_suggestion = window.scayt.getSuggestion( word, sLang );
\r
394 if (!items_suggestion || !items_suggestion.length )
\r
396 // Remove unused commands and menuitems
\r
397 for ( i in moreSuggestions )
\r
399 delete editor._.menuItems[ i ];
\r
400 delete editor._.commands[ i ];
\r
402 for ( i in mainSuggestions )
\r
404 delete editor._.menuItems[ i ];
\r
405 delete editor._.commands[ i ];
\r
407 moreSuggestions = {}; // Reset items.
\r
408 mainSuggestions = {};
\r
410 var moreSuggestionsUnable = false;
\r
412 for ( var i = 0, l = items_suggestion.length; i < l; i += 1 )
\r
414 var commandName = 'scayt_suggestion_' + items_suggestion[i].replace( ' ', '_' );
\r
415 var exec = ( function( el, s )
\r
420 scayt_control.replace(el, s);
\r
423 })( element.$, items_suggestion[i] );
\r
425 if ( i < editor.config.scayt_maxSuggestions )
\r
427 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
\r
428 commandName, exec, 'scayt_suggest', i + 1 );
\r
429 _r[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
430 mainSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
434 addButtonCommand( editor, 'button_' + commandName, items_suggestion[i],
\r
435 commandName, exec, 'scayt_moresuggest', i + 1 );
\r
436 moreSuggestions[ commandName ] = CKEDITOR.TRISTATE_OFF;
\r
437 moreSuggestionsUnable = true;
\r
440 if ( moreSuggestionsUnable )
\r
441 // Rgister the More suggestions group;
\r
442 editor.addMenuItem( 'scayt_moresuggest',
\r
444 label : editor.lang.scayt.moreSuggestions,
\r
445 group : 'scayt_moresuggest',
\r
447 getItems : function()
\r
449 return moreSuggestions;
\r
454 var ignore_command =
\r
458 scayt_control.ignore( element.$ );
\r
461 var ignore_all_command =
\r
465 scayt_control.ignoreAll( element.$ );
\r
468 var addword_command =
\r
472 window.scayt.addWordToUserDictionary( element.$ );
\r
476 addButtonCommand( editor, 'ignore', editor.lang.scayt.ignore,
\r
477 'scayt_ignore', ignore_command, 'scayt_control', 1);
\r
478 addButtonCommand( editor, 'ignore_all', editor.lang.scayt.ignoreAll,
\r
479 'scayt_ignore_all', ignore_all_command, 'scayt_control', 2);
\r
480 addButtonCommand( editor, 'add_word', editor.lang.scayt.addWord,
\r
481 'scayt_add_word', addword_command, 'scayt_control', 3);
\r
483 mainSuggestions[ 'scayt_moresuggest' ] = CKEDITOR.TRISTATE_OFF;
\r
484 mainSuggestions[ 'scayt_ignore' ] = CKEDITOR.TRISTATE_OFF;
\r
485 mainSuggestions[ 'scayt_ignore_all' ] = CKEDITOR.TRISTATE_OFF;
\r
486 mainSuggestions[ 'scayt_add_word' ] = CKEDITOR.TRISTATE_OFF;
\r
488 if ( scayt_control.fireOnContextMenu )
\r
489 scayt_control.fireOnContextMenu( editor );
\r
491 return mainSuggestions;
\r
496 if ( editor.config.scayt_autoStartup )
\r
498 var showInitialState = function()
\r
500 editor.removeListener( 'showScaytState', showInitialState );
\r
501 command.setState( plugin.isScaytEnabled( editor ) ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
\r
503 editor.on( 'showScaytState', showInitialState );
\r
505 plugin.loadEngine( editor );
\r
511 CKEDITOR.config.scayt_maxSuggestions = 5;
\r
512 CKEDITOR.config.scayt_autoStartup = false;
\r