1 ////////////////////////////////////////////////////
\r
4 // spellChecker object
\r
6 // This file is sourced on web pages that have a textarea object to evaluate
\r
7 // for spelling. It includes the implementation for the spellCheckObject.
\r
9 ////////////////////////////////////////////////////
\r
13 function spellChecker( textObject ) {
\r
15 // public properties - configurable
\r
16 // this.popUpUrl = '/speller/spellchecker.html'; // by FredCK
\r
17 this.popUpUrl = 'fck_spellerpages/spellerpages/spellchecker.html'; // by FredCK
\r
18 this.popUpName = 'spellchecker';
\r
19 // this.popUpProps = "menu=no,width=440,height=350,top=70,left=120,resizable=yes,status=yes"; // by FredCK
\r
20 this.popUpProps = null ; // by FredCK
\r
21 // this.spellCheckScript = '/speller/server-scripts/spellchecker.php'; // by FredCK
\r
22 //this.spellCheckScript = '/cgi-bin/spellchecker.pl';
\r
24 // values used to keep track of what happened to a word
\r
25 this.replWordFlag = "R"; // single replace
\r
26 this.ignrWordFlag = "I"; // single ignore
\r
27 this.replAllFlag = "RA"; // replace all occurances
\r
28 this.ignrAllFlag = "IA"; // ignore all occurances
\r
29 this.fromReplAll = "~RA"; // an occurance of a "replace all" word
\r
30 this.fromIgnrAll = "~IA"; // an occurance of a "ignore all" word
\r
31 // properties set at run time
\r
32 this.wordFlags = new Array();
\r
33 this.currentTextIndex = 0;
\r
34 this.currentWordIndex = 0;
\r
35 this.spellCheckerWin = null;
\r
36 this.controlWin = null;
\r
37 this.wordWin = null;
\r
38 this.textArea = textObject; // deprecated
\r
39 this.textInputs = arguments;
\r
42 this._spellcheck = _spellcheck;
\r
43 this._getSuggestions = _getSuggestions;
\r
44 this._setAsIgnored = _setAsIgnored;
\r
45 this._getTotalReplaced = _getTotalReplaced;
\r
46 this._setWordText = _setWordText;
\r
47 this._getFormInputs = _getFormInputs;
\r
50 this.openChecker = openChecker;
\r
51 this.startCheck = startCheck;
\r
52 this.checkTextBoxes = checkTextBoxes;
\r
53 this.checkTextAreas = checkTextAreas;
\r
54 this.spellCheckAll = spellCheckAll;
\r
55 this.ignoreWord = ignoreWord;
\r
56 this.ignoreAll = ignoreAll;
\r
57 this.replaceWord = replaceWord;
\r
58 this.replaceAll = replaceAll;
\r
59 this.terminateSpell = terminateSpell;
\r
62 // set the current window's "speller" property to the instance of this class.
\r
63 // this object can now be referenced by child windows/frames.
\r
64 window.speller = this;
\r
67 // call this method to check all text boxes (and only text boxes) in the HTML document
\r
68 function checkTextBoxes() {
\r
69 this.textInputs = this._getFormInputs( "^text$" );
\r
73 // call this method to check all textareas (and only textareas ) in the HTML document
\r
74 function checkTextAreas() {
\r
75 this.textInputs = this._getFormInputs( "^textarea$" );
\r
79 // call this method to check all text boxes and textareas in the HTML document
\r
80 function spellCheckAll() {
\r
81 this.textInputs = this._getFormInputs( "^text(area)?$" );
\r
85 // call this method to check text boxe(s) and/or textarea(s) that were passed in to the
\r
86 // object's constructor or to the textInputs property
\r
87 function openChecker() {
\r
88 this.spellCheckerWin = window.open( this.popUpUrl, this.popUpName, this.popUpProps );
\r
89 if( !this.spellCheckerWin.opener ) {
\r
90 this.spellCheckerWin.opener = window;
\r
94 function startCheck( wordWindowObj, controlWindowObj ) {
\r
96 // set properties from args
\r
97 this.wordWin = wordWindowObj;
\r
98 this.controlWin = controlWindowObj;
\r
100 // reset properties
\r
101 this.wordWin.resetForm();
\r
102 this.controlWin.resetForm();
\r
103 this.currentTextIndex = 0;
\r
104 this.currentWordIndex = 0;
\r
105 // initialize the flags to an array - one element for each text input
\r
106 this.wordFlags = new Array( this.wordWin.textInputs.length );
\r
107 // each element will be an array that keeps track of each word in the text
\r
108 for( var i=0; i<this.wordFlags.length; i++ ) {
\r
109 this.wordFlags[i] = [];
\r
113 this._spellcheck();
\r
118 function ignoreWord() {
\r
119 var wi = this.currentWordIndex;
\r
120 var ti = this.currentTextIndex;
\r
121 if( !this.wordWin ) {
\r
122 alert( 'Error: Word frame not available.' );
\r
125 if( !this.wordWin.getTextVal( ti, wi )) {
\r
126 alert( 'Error: "Not in dictionary" text is missing.' );
\r
130 if( this._setAsIgnored( ti, wi, this.ignrWordFlag )) {
\r
131 this.currentWordIndex++;
\r
132 this._spellcheck();
\r
137 function ignoreAll() {
\r
138 var wi = this.currentWordIndex;
\r
139 var ti = this.currentTextIndex;
\r
140 if( !this.wordWin ) {
\r
141 alert( 'Error: Word frame not available.' );
\r
144 // get the word that is currently being evaluated.
\r
145 var s_word_to_repl = this.wordWin.getTextVal( ti, wi );
\r
146 if( !s_word_to_repl ) {
\r
147 alert( 'Error: "Not in dictionary" text is missing' );
\r
151 // set this word as an "ignore all" word.
\r
152 this._setAsIgnored( ti, wi, this.ignrAllFlag );
\r
154 // loop through all the words after this word
\r
155 for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {
\r
156 for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {
\r
157 if(( i == ti && j > wi ) || i > ti ) {
\r
158 // future word: set as "from ignore all" if
\r
159 // 1) do not already have a flag and
\r
160 // 2) have the same value as current word
\r
161 if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )
\r
162 && ( !this.wordFlags[i][j] )) {
\r
163 this._setAsIgnored( i, j, this.fromIgnrAll );
\r
169 // finally, move on
\r
170 this.currentWordIndex++;
\r
171 this._spellcheck();
\r
175 function replaceWord() {
\r
176 var wi = this.currentWordIndex;
\r
177 var ti = this.currentTextIndex;
\r
178 if( !this.wordWin ) {
\r
179 alert( 'Error: Word frame not available.' );
\r
182 if( !this.wordWin.getTextVal( ti, wi )) {
\r
183 alert( 'Error: "Not in dictionary" text is missing' );
\r
186 if( !this.controlWin.replacementText ) {
\r
189 var txt = this.controlWin.replacementText;
\r
191 var newspell = new String( txt.value );
\r
192 if( this._setWordText( ti, wi, newspell, this.replWordFlag )) {
\r
193 this.currentWordIndex++;
\r
194 this._spellcheck();
\r
200 function replaceAll() {
\r
201 var ti = this.currentTextIndex;
\r
202 var wi = this.currentWordIndex;
\r
203 if( !this.wordWin ) {
\r
204 alert( 'Error: Word frame not available.' );
\r
207 var s_word_to_repl = this.wordWin.getTextVal( ti, wi );
\r
208 if( !s_word_to_repl ) {
\r
209 alert( 'Error: "Not in dictionary" text is missing' );
\r
212 var txt = this.controlWin.replacementText;
\r
213 if( !txt.value ) return false;
\r
214 var newspell = new String( txt.value );
\r
216 // set this word as a "replace all" word.
\r
217 this._setWordText( ti, wi, newspell, this.replAllFlag );
\r
219 // loop through all the words after this word
\r
220 for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {
\r
221 for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {
\r
222 if(( i == ti && j > wi ) || i > ti ) {
\r
223 // future word: set word text to s_word_to_repl if
\r
224 // 1) do not already have a flag and
\r
225 // 2) have the same value as s_word_to_repl
\r
226 if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )
\r
227 && ( !this.wordFlags[i][j] )) {
\r
228 this._setWordText( i, j, newspell, this.fromReplAll );
\r
234 // finally, move on
\r
235 this.currentWordIndex++;
\r
236 this._spellcheck();
\r
240 function terminateSpell() {
\r
241 // called when we have reached the end of the spell checking.
\r
242 var msg = ""; // by FredCK
\r
243 var numrepl = this._getTotalReplaced();
\r
244 if( numrepl == 0 ) {
\r
245 // see if there were no misspellings to begin with
\r
246 if( !this.wordWin ) {
\r
249 if( this.wordWin.totalMisspellings() ) {
\r
250 // msg += "No words changed."; // by FredCK
\r
251 msg += FCKLang.DlgSpellNoChanges ; // by FredCK
\r
253 // msg += "No misspellings found."; // by FredCK
\r
254 msg += FCKLang.DlgSpellNoMispell ; // by FredCK
\r
257 } else if( numrepl == 1 ) {
\r
258 // msg += "One word changed."; // by FredCK
\r
259 msg += FCKLang.DlgSpellOneChange ; // by FredCK
\r
261 // msg += numrepl + " words changed."; // by FredCK
\r
262 msg += FCKLang.DlgSpellManyChanges.replace( /%1/g, numrepl ) ;
\r
265 // msg += "\n"; // by FredCK
\r
269 if( numrepl > 0 ) {
\r
270 // update the text field(s) on the opener window
\r
271 for( var i = 0; i < this.textInputs.length; i++ ) {
\r
272 // this.textArea.value = this.wordWin.text;
\r
273 if( this.wordWin ) {
\r
274 if( this.wordWin.textInputs[i] ) {
\r
275 this.textInputs[i].value = this.wordWin.textInputs[i];
\r
281 // return back to the calling window
\r
282 // this.spellCheckerWin.close(); // by FredCK
\r
283 if ( typeof( this.OnFinished ) == 'function' ) // by FredCK
\r
284 this.OnFinished(numrepl) ; // by FredCK
\r
290 // skip if this is the first word!
\r
291 var ti = this.currentTextIndex;
\r
292 var wi = this.currentWordIndex;
\r
294 if( this.wordWin.totalPreviousWords( ti, wi ) > 0 ) {
\r
295 this.wordWin.removeFocus( ti, wi );
\r
297 // go back to the last word index that was acted upon
\r
299 // if the current word index is zero then reset the seed
\r
300 if( this.currentWordIndex == 0 && this.currentTextIndex > 0 ) {
\r
301 this.currentTextIndex--;
\r
302 this.currentWordIndex = this.wordWin.totalWords( this.currentTextIndex )-1;
\r
303 if( this.currentWordIndex < 0 ) this.currentWordIndex = 0;
\r
305 if( this.currentWordIndex > 0 ) {
\r
306 this.currentWordIndex--;
\r
310 this.wordWin.totalWords( this.currentTextIndex ) == 0
\r
311 || this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromIgnrAll
\r
312 || this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromReplAll
\r
315 var text_idx = this.currentTextIndex;
\r
316 var idx = this.currentWordIndex;
\r
317 var preReplSpell = this.wordWin.originalSpellings[text_idx][idx];
\r
319 // if we got back to the first word then set the Undo button back to disabled
\r
320 if( this.wordWin.totalPreviousWords( text_idx, idx ) == 0 ) {
\r
321 this.controlWin.disableUndo();
\r
324 var i, j, origSpell ;
\r
325 // examine what happened to this current word.
\r
326 switch( this.wordFlags[text_idx][idx] ) {
\r
327 // replace all: go through this and all the future occurances of the word
\r
328 // and revert them all to the original spelling and clear their flags
\r
329 case this.replAllFlag :
\r
330 for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {
\r
331 for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {
\r
332 if(( i == text_idx && j >= idx ) || i > text_idx ) {
\r
333 origSpell = this.wordWin.originalSpellings[i][j];
\r
334 if( origSpell == preReplSpell ) {
\r
335 this._setWordText ( i, j, origSpell, undefined );
\r
342 // ignore all: go through all the future occurances of the word
\r
343 // and clear their flags
\r
344 case this.ignrAllFlag :
\r
345 for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {
\r
346 for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {
\r
347 if(( i == text_idx && j >= idx ) || i > text_idx ) {
\r
348 origSpell = this.wordWin.originalSpellings[i][j];
\r
349 if( origSpell == preReplSpell ) {
\r
350 this.wordFlags[i][j] = undefined;
\r
357 // replace: revert the word to its original spelling
\r
358 case this.replWordFlag :
\r
359 this._setWordText ( text_idx, idx, preReplSpell, undefined );
\r
363 // For all four cases, clear the wordFlag of this word. re-start the process
\r
364 this.wordFlags[text_idx][idx] = undefined;
\r
365 this._spellcheck();
\r
369 function _spellcheck() {
\r
370 var ww = this.wordWin;
\r
372 // check if this is the last word in the current text element
\r
373 if( this.currentWordIndex == ww.totalWords( this.currentTextIndex) ) {
\r
374 this.currentTextIndex++;
\r
375 this.currentWordIndex = 0;
\r
376 // keep going if we're not yet past the last text element
\r
377 if( this.currentTextIndex < this.wordWin.textInputs.length ) {
\r
378 this._spellcheck();
\r
381 this.terminateSpell();
\r
386 // if this is after the first one make sure the Undo button is enabled
\r
387 if( this.currentWordIndex > 0 ) {
\r
388 this.controlWin.enableUndo();
\r
391 // skip the current word if it has already been worked on
\r
392 if( this.wordFlags[this.currentTextIndex][this.currentWordIndex] ) {
\r
393 // increment the global current word index and move on.
\r
394 this.currentWordIndex++;
\r
395 this._spellcheck();
\r
397 var evalText = ww.getTextVal( this.currentTextIndex, this.currentWordIndex );
\r
399 this.controlWin.evaluatedText.value = evalText;
\r
400 ww.setFocus( this.currentTextIndex, this.currentWordIndex );
\r
401 this._getSuggestions( this.currentTextIndex, this.currentWordIndex );
\r
406 function _getSuggestions( text_num, word_num ) {
\r
407 this.controlWin.clearSuggestions();
\r
408 // add suggestion in list for each suggested word.
\r
409 // get the array of suggested words out of the
\r
410 // three-dimensional array containing all suggestions.
\r
411 var a_suggests = this.wordWin.suggestions[text_num][word_num];
\r
413 // got an array of suggestions.
\r
414 for( var ii = 0; ii < a_suggests.length; ii++ ) {
\r
415 this.controlWin.addSuggestion( a_suggests[ii] );
\r
418 this.controlWin.selectDefaultSuggestion();
\r
421 function _setAsIgnored( text_num, word_num, flag ) {
\r
423 this.wordWin.removeFocus( text_num, word_num );
\r
424 // do the bookkeeping
\r
425 this.wordFlags[text_num][word_num] = flag;
\r
429 function _getTotalReplaced() {
\r
430 var i_replaced = 0;
\r
431 for( var i = 0; i < this.wordFlags.length; i++ ) {
\r
432 for( var j = 0; j < this.wordFlags[i].length; j++ ) {
\r
433 if(( this.wordFlags[i][j] == this.replWordFlag )
\r
434 || ( this.wordFlags[i][j] == this.replAllFlag )
\r
435 || ( this.wordFlags[i][j] == this.fromReplAll )) {
\r
443 function _setWordText( text_num, word_num, newText, flag ) {
\r
444 // set the UI and form inputs
\r
445 this.wordWin.setText( text_num, word_num, newText );
\r
446 // keep track of what happened to this word:
\r
447 this.wordFlags[text_num][word_num] = flag;
\r
451 function _getFormInputs( inputPattern ) {
\r
452 var inputs = new Array();
\r
453 for( var i = 0; i < document.forms.length; i++ ) {
\r
454 for( var j = 0; j < document.forms[i].elements.length; j++ ) {
\r
455 if( document.forms[i].elements[j].type.match( inputPattern )) {
\r
456 inputs[inputs.length] = document.forms[i].elements[j];
\r