event refactor, landing on HEAD!
[freeside.git] / httemplate / elements / fckeditor / editor / dialog / fck_spellerpages / spellerpages / spellChecker.js
1 ////////////////////////////////////////////////////\r
2 // spellChecker.js\r
3 //\r
4 // spellChecker object\r
5 //\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
8 //\r
9 ////////////////////////////////////////////////////\r
10 \r
11 \r
12 // constructor\r
13 function spellChecker( textObject ) {\r
14 \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
23 \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
40 \r
41         // private methods\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
48 \r
49         // public methods\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
60         this.undo = undo;\r
61 \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
65 }\r
66 \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
70         this.openChecker();\r
71 }\r
72 \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
76         this.openChecker();\r
77 }\r
78 \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
82         this.openChecker();\r
83 }\r
84 \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
91         }\r
92 }\r
93 \r
94 function startCheck( wordWindowObj, controlWindowObj ) {\r
95 \r
96         // set properties from args\r
97         this.wordWin = wordWindowObj;\r
98         this.controlWin = controlWindowObj;\r
99 \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
110         }\r
111 \r
112         // start\r
113         this._spellcheck();\r
114 \r
115         return true;\r
116 }\r
117 \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
123                 return false;\r
124         }\r
125         if( !this.wordWin.getTextVal( ti, wi )) {\r
126                 alert( 'Error: "Not in dictionary" text is missing.' );\r
127                 return false;\r
128         }\r
129         // set as ignored\r
130         if( this._setAsIgnored( ti, wi, this.ignrWordFlag )) {\r
131                 this.currentWordIndex++;\r
132                 this._spellcheck();\r
133         }\r
134         return true;\r
135 }\r
136 \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
142                 return false;\r
143         }\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
148                 return false;\r
149         }\r
150 \r
151         // set this word as an "ignore all" word.\r
152         this._setAsIgnored( ti, wi, this.ignrAllFlag );\r
153 \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
164                                 }\r
165                         }\r
166                 }\r
167         }\r
168 \r
169         // finally, move on\r
170         this.currentWordIndex++;\r
171         this._spellcheck();\r
172         return true;\r
173 }\r
174 \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
180                 return false;\r
181         }\r
182         if( !this.wordWin.getTextVal( ti, wi )) {\r
183                 alert( 'Error: "Not in dictionary" text is missing' );\r
184                 return false;\r
185         }\r
186         if( !this.controlWin.replacementText ) {\r
187                 return false ;\r
188         }\r
189         var txt = this.controlWin.replacementText;\r
190         if( txt.value ) {\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
195                 }\r
196         }\r
197         return true;\r
198 }\r
199 \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
205                 return false;\r
206         }\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
210                 return false;\r
211         }\r
212         var txt = this.controlWin.replacementText;\r
213         if( !txt.value ) return false;\r
214         var newspell = new String( txt.value );\r
215 \r
216         // set this word as a "replace all" word.\r
217         this._setWordText( ti, wi, newspell, this.replAllFlag );\r
218 \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
229                                 }\r
230                         }\r
231                 }\r
232         }\r
233 \r
234         // finally, move on\r
235         this.currentWordIndex++;\r
236         this._spellcheck();\r
237         return true;\r
238 }\r
239 \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
247                         msg = "";\r
248                 } else {\r
249                         if( this.wordWin.totalMisspellings() ) {\r
250 //                              msg += "No words changed.";                     // by FredCK\r
251                                 msg += FCKLang.DlgSpellNoChanges ;      // by FredCK\r
252                         } else {\r
253 //                              msg += "No misspellings found.";        // by FredCK\r
254                                 msg += FCKLang.DlgSpellNoMispell ;      // by FredCK\r
255                         }\r
256                 }\r
257         } else if( numrepl == 1 ) {\r
258 //              msg += "One word changed.";                     // by FredCK\r
259                 msg += FCKLang.DlgSpellOneChange ;      // by FredCK\r
260         } else {\r
261 //              msg += numrepl + " words changed.";     // by FredCK\r
262                 msg += FCKLang.DlgSpellManyChanges.replace( /%1/g, numrepl ) ;\r
263         }\r
264         if( msg ) {\r
265 //              msg += "\n";    // by FredCK\r
266                 alert( msg );\r
267         }\r
268 \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
276                                 }\r
277                         }\r
278                 }\r
279         }\r
280 \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
285 \r
286         return true;\r
287 }\r
288 \r
289 function undo() {\r
290         // skip if this is the first word!\r
291         var ti = this.currentTextIndex;\r
292         var wi = this.currentWordIndex;\r
293 \r
294         if( this.wordWin.totalPreviousWords( ti, wi ) > 0 ) {\r
295                 this.wordWin.removeFocus( ti, wi );\r
296 \r
297                 // go back to the last word index that was acted upon\r
298                 do {\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
304                         } else {\r
305                                 if( this.currentWordIndex > 0 ) {\r
306                                         this.currentWordIndex--;\r
307                                 }\r
308                         }\r
309                 } while (\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
313                 );\r
314 \r
315                 var text_idx = this.currentTextIndex;\r
316                 var idx = this.currentWordIndex;\r
317                 var preReplSpell = this.wordWin.originalSpellings[text_idx][idx];\r
318 \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
322                 }\r
323 \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
336                                                         }\r
337                                                 }\r
338                                         }\r
339                                 }\r
340                                 break;\r
341 \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
351                                                         }\r
352                                                 }\r
353                                         }\r
354                                 }\r
355                                 break;\r
356 \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
360                                 break;\r
361                 }\r
362 \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
366         }\r
367 }\r
368 \r
369 function _spellcheck() {\r
370         var ww = this.wordWin;\r
371 \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
379                         return;\r
380                 } else {\r
381                         this.terminateSpell();\r
382                         return;\r
383                 }\r
384         }\r
385 \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
389         }\r
390 \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
396         } else {\r
397                 var evalText = ww.getTextVal( this.currentTextIndex, this.currentWordIndex );\r
398                 if( evalText ) {\r
399                         this.controlWin.evaluatedText.value = evalText;\r
400                         ww.setFocus( this.currentTextIndex, this.currentWordIndex );\r
401                         this._getSuggestions( this.currentTextIndex, this.currentWordIndex );\r
402                 }\r
403         }\r
404 }\r
405 \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
412         if( a_suggests ) {\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
416                 }\r
417         }\r
418         this.controlWin.selectDefaultSuggestion();\r
419 }\r
420 \r
421 function _setAsIgnored( text_num, word_num, flag ) {\r
422         // set the UI\r
423         this.wordWin.removeFocus( text_num, word_num );\r
424         // do the bookkeeping\r
425         this.wordFlags[text_num][word_num] = flag;\r
426         return true;\r
427 }\r
428 \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
436                                 i_replaced++;\r
437                         }\r
438                 }\r
439         }\r
440         return i_replaced;\r
441 }\r
442 \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
448         return true;\r
449 }\r
450 \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
457                         }\r
458                 }\r
459         }\r
460         return inputs;\r
461 }\r
462 \r