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