event refactor, landing on HEAD!
[freeside.git] / httemplate / elements / fckeditor / editor / dialog / fck_spellerpages / spellerpages / spellChecker.js
diff --git a/httemplate/elements/fckeditor/editor/dialog/fck_spellerpages/spellerpages/spellChecker.js b/httemplate/elements/fckeditor/editor/dialog/fck_spellerpages/spellerpages/spellChecker.js
new file mode 100644 (file)
index 0000000..b5e55b7
--- /dev/null
@@ -0,0 +1,462 @@
+////////////////////////////////////////////////////\r
+// spellChecker.js\r
+//\r
+// spellChecker object\r
+//\r
+// This file is sourced on web pages that have a textarea object to evaluate\r
+// for spelling. It includes the implementation for the spellCheckObject.\r
+//\r
+////////////////////////////////////////////////////\r
+\r
+\r
+// constructor\r
+function spellChecker( textObject ) {\r
+\r
+       // public properties - configurable\r
+//     this.popUpUrl = '/speller/spellchecker.html';                                                   // by FredCK\r
+       this.popUpUrl = 'fck_spellerpages/spellerpages/spellchecker.html';              // by FredCK\r
+       this.popUpName = 'spellchecker';\r
+//     this.popUpProps = "menu=no,width=440,height=350,top=70,left=120,resizable=yes,status=yes";      // by FredCK\r
+       this.popUpProps = null ;                                                                                                                                        // by FredCK\r
+//     this.spellCheckScript = '/speller/server-scripts/spellchecker.php';             // by FredCK\r
+       //this.spellCheckScript = '/cgi-bin/spellchecker.pl';\r
+\r
+       // values used to keep track of what happened to a word\r
+       this.replWordFlag = "R";        // single replace\r
+       this.ignrWordFlag = "I";        // single ignore\r
+       this.replAllFlag = "RA";        // replace all occurances\r
+       this.ignrAllFlag = "IA";        // ignore all occurances\r
+       this.fromReplAll = "~RA";       // an occurance of a "replace all" word\r
+       this.fromIgnrAll = "~IA";       // an occurance of a "ignore all" word\r
+       // properties set at run time\r
+       this.wordFlags = new Array();\r
+       this.currentTextIndex = 0;\r
+       this.currentWordIndex = 0;\r
+       this.spellCheckerWin = null;\r
+       this.controlWin = null;\r
+       this.wordWin = null;\r
+       this.textArea = textObject;     // deprecated\r
+       this.textInputs = arguments;\r
+\r
+       // private methods\r
+       this._spellcheck = _spellcheck;\r
+       this._getSuggestions = _getSuggestions;\r
+       this._setAsIgnored = _setAsIgnored;\r
+       this._getTotalReplaced = _getTotalReplaced;\r
+       this._setWordText = _setWordText;\r
+       this._getFormInputs = _getFormInputs;\r
+\r
+       // public methods\r
+       this.openChecker = openChecker;\r
+       this.startCheck = startCheck;\r
+       this.checkTextBoxes = checkTextBoxes;\r
+       this.checkTextAreas = checkTextAreas;\r
+       this.spellCheckAll = spellCheckAll;\r
+       this.ignoreWord = ignoreWord;\r
+       this.ignoreAll = ignoreAll;\r
+       this.replaceWord = replaceWord;\r
+       this.replaceAll = replaceAll;\r
+       this.terminateSpell = terminateSpell;\r
+       this.undo = undo;\r
+\r
+       // set the current window's "speller" property to the instance of this class.\r
+       // this object can now be referenced by child windows/frames.\r
+       window.speller = this;\r
+}\r
+\r
+// call this method to check all text boxes (and only text boxes) in the HTML document\r
+function checkTextBoxes() {\r
+       this.textInputs = this._getFormInputs( "^text$" );\r
+       this.openChecker();\r
+}\r
+\r
+// call this method to check all textareas (and only textareas ) in the HTML document\r
+function checkTextAreas() {\r
+       this.textInputs = this._getFormInputs( "^textarea$" );\r
+       this.openChecker();\r
+}\r
+\r
+// call this method to check all text boxes and textareas in the HTML document\r
+function spellCheckAll() {\r
+       this.textInputs = this._getFormInputs( "^text(area)?$" );\r
+       this.openChecker();\r
+}\r
+\r
+// call this method to check text boxe(s) and/or textarea(s) that were passed in to the\r
+// object's constructor or to the textInputs property\r
+function openChecker() {\r
+       this.spellCheckerWin = window.open( this.popUpUrl, this.popUpName, this.popUpProps );\r
+       if( !this.spellCheckerWin.opener ) {\r
+               this.spellCheckerWin.opener = window;\r
+       }\r
+}\r
+\r
+function startCheck( wordWindowObj, controlWindowObj ) {\r
+\r
+       // set properties from args\r
+       this.wordWin = wordWindowObj;\r
+       this.controlWin = controlWindowObj;\r
+\r
+       // reset properties\r
+       this.wordWin.resetForm();\r
+       this.controlWin.resetForm();\r
+       this.currentTextIndex = 0;\r
+       this.currentWordIndex = 0;\r
+       // initialize the flags to an array - one element for each text input\r
+       this.wordFlags = new Array( this.wordWin.textInputs.length );\r
+       // each element will be an array that keeps track of each word in the text\r
+       for( var i=0; i<this.wordFlags.length; i++ ) {\r
+               this.wordFlags[i] = [];\r
+       }\r
+\r
+       // start\r
+       this._spellcheck();\r
+\r
+       return true;\r
+}\r
+\r
+function ignoreWord() {\r
+       var wi = this.currentWordIndex;\r
+       var ti = this.currentTextIndex;\r
+       if( !this.wordWin ) {\r
+               alert( 'Error: Word frame not available.' );\r
+               return false;\r
+       }\r
+       if( !this.wordWin.getTextVal( ti, wi )) {\r
+               alert( 'Error: "Not in dictionary" text is missing.' );\r
+               return false;\r
+       }\r
+       // set as ignored\r
+       if( this._setAsIgnored( ti, wi, this.ignrWordFlag )) {\r
+               this.currentWordIndex++;\r
+               this._spellcheck();\r
+       }\r
+       return true;\r
+}\r
+\r
+function ignoreAll() {\r
+       var wi = this.currentWordIndex;\r
+       var ti = this.currentTextIndex;\r
+       if( !this.wordWin ) {\r
+               alert( 'Error: Word frame not available.' );\r
+               return false;\r
+       }\r
+       // get the word that is currently being evaluated.\r
+       var s_word_to_repl = this.wordWin.getTextVal( ti, wi );\r
+       if( !s_word_to_repl ) {\r
+               alert( 'Error: "Not in dictionary" text is missing' );\r
+               return false;\r
+       }\r
+\r
+       // set this word as an "ignore all" word.\r
+       this._setAsIgnored( ti, wi, this.ignrAllFlag );\r
+\r
+       // loop through all the words after this word\r
+       for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {\r
+               for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {\r
+                       if(( i == ti && j > wi ) || i > ti ) {\r
+                               // future word: set as "from ignore all" if\r
+                               // 1) do not already have a flag and\r
+                               // 2) have the same value as current word\r
+                               if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )\r
+                               && ( !this.wordFlags[i][j] )) {\r
+                                       this._setAsIgnored( i, j, this.fromIgnrAll );\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       // finally, move on\r
+       this.currentWordIndex++;\r
+       this._spellcheck();\r
+       return true;\r
+}\r
+\r
+function replaceWord() {\r
+       var wi = this.currentWordIndex;\r
+       var ti = this.currentTextIndex;\r
+       if( !this.wordWin ) {\r
+               alert( 'Error: Word frame not available.' );\r
+               return false;\r
+       }\r
+       if( !this.wordWin.getTextVal( ti, wi )) {\r
+               alert( 'Error: "Not in dictionary" text is missing' );\r
+               return false;\r
+       }\r
+       if( !this.controlWin.replacementText ) {\r
+               return false ;\r
+       }\r
+       var txt = this.controlWin.replacementText;\r
+       if( txt.value ) {\r
+               var newspell = new String( txt.value );\r
+               if( this._setWordText( ti, wi, newspell, this.replWordFlag )) {\r
+                       this.currentWordIndex++;\r
+                       this._spellcheck();\r
+               }\r
+       }\r
+       return true;\r
+}\r
+\r
+function replaceAll() {\r
+       var ti = this.currentTextIndex;\r
+       var wi = this.currentWordIndex;\r
+       if( !this.wordWin ) {\r
+               alert( 'Error: Word frame not available.' );\r
+               return false;\r
+       }\r
+       var s_word_to_repl = this.wordWin.getTextVal( ti, wi );\r
+       if( !s_word_to_repl ) {\r
+               alert( 'Error: "Not in dictionary" text is missing' );\r
+               return false;\r
+       }\r
+       var txt = this.controlWin.replacementText;\r
+       if( !txt.value ) return false;\r
+       var newspell = new String( txt.value );\r
+\r
+       // set this word as a "replace all" word.\r
+       this._setWordText( ti, wi, newspell, this.replAllFlag );\r
+\r
+       // loop through all the words after this word\r
+       for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {\r
+               for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {\r
+                       if(( i == ti && j > wi ) || i > ti ) {\r
+                               // future word: set word text to s_word_to_repl if\r
+                               // 1) do not already have a flag and\r
+                               // 2) have the same value as s_word_to_repl\r
+                               if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )\r
+                               && ( !this.wordFlags[i][j] )) {\r
+                                       this._setWordText( i, j, newspell, this.fromReplAll );\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       // finally, move on\r
+       this.currentWordIndex++;\r
+       this._spellcheck();\r
+       return true;\r
+}\r
+\r
+function terminateSpell() {\r
+       // called when we have reached the end of the spell checking.\r
+       var msg = "";           // by FredCK\r
+       var numrepl = this._getTotalReplaced();\r
+       if( numrepl == 0 ) {\r
+               // see if there were no misspellings to begin with\r
+               if( !this.wordWin ) {\r
+                       msg = "";\r
+               } else {\r
+                       if( this.wordWin.totalMisspellings() ) {\r
+//                             msg += "No words changed.";                     // by FredCK\r
+                               msg += FCKLang.DlgSpellNoChanges ;      // by FredCK\r
+                       } else {\r
+//                             msg += "No misspellings found.";        // by FredCK\r
+                               msg += FCKLang.DlgSpellNoMispell ;      // by FredCK\r
+                       }\r
+               }\r
+       } else if( numrepl == 1 ) {\r
+//             msg += "One word changed.";                     // by FredCK\r
+               msg += FCKLang.DlgSpellOneChange ;      // by FredCK\r
+       } else {\r
+//             msg += numrepl + " words changed.";     // by FredCK\r
+               msg += FCKLang.DlgSpellManyChanges.replace( /%1/g, numrepl ) ;\r
+       }\r
+       if( msg ) {\r
+//             msg += "\n";    // by FredCK\r
+               alert( msg );\r
+       }\r
+\r
+       if( numrepl > 0 ) {\r
+               // update the text field(s) on the opener window\r
+               for( var i = 0; i < this.textInputs.length; i++ ) {\r
+                       // this.textArea.value = this.wordWin.text;\r
+                       if( this.wordWin ) {\r
+                               if( this.wordWin.textInputs[i] ) {\r
+                                       this.textInputs[i].value = this.wordWin.textInputs[i];\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       // return back to the calling window\r
+//     this.spellCheckerWin.close();                                   // by FredCK\r
+       if ( typeof( this.OnFinished ) == 'function' )  // by FredCK\r
+               this.OnFinished(numrepl) ;                                      // by FredCK\r
+\r
+       return true;\r
+}\r
+\r
+function undo() {\r
+       // skip if this is the first word!\r
+       var ti = this.currentTextIndex;\r
+       var wi = this.currentWordIndex;\r
+\r
+       if( this.wordWin.totalPreviousWords( ti, wi ) > 0 ) {\r
+               this.wordWin.removeFocus( ti, wi );\r
+\r
+               // go back to the last word index that was acted upon\r
+               do {\r
+                       // if the current word index is zero then reset the seed\r
+                       if( this.currentWordIndex == 0 && this.currentTextIndex > 0 ) {\r
+                               this.currentTextIndex--;\r
+                               this.currentWordIndex = this.wordWin.totalWords( this.currentTextIndex )-1;\r
+                               if( this.currentWordIndex < 0 ) this.currentWordIndex = 0;\r
+                       } else {\r
+                               if( this.currentWordIndex > 0 ) {\r
+                                       this.currentWordIndex--;\r
+                               }\r
+                       }\r
+               } while (\r
+                       this.wordWin.totalWords( this.currentTextIndex ) == 0\r
+                       || this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromIgnrAll\r
+                       || this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromReplAll\r
+               );\r
+\r
+               var text_idx = this.currentTextIndex;\r
+               var idx = this.currentWordIndex;\r
+               var preReplSpell = this.wordWin.originalSpellings[text_idx][idx];\r
+\r
+               // if we got back to the first word then set the Undo button back to disabled\r
+               if( this.wordWin.totalPreviousWords( text_idx, idx ) == 0 ) {\r
+                       this.controlWin.disableUndo();\r
+               }\r
+\r
+               var i, j, origSpell ;\r
+               // examine what happened to this current word.\r
+               switch( this.wordFlags[text_idx][idx] ) {\r
+                       // replace all: go through this and all the future occurances of the word\r
+                       // and revert them all to the original spelling and clear their flags\r
+                       case this.replAllFlag :\r
+                               for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {\r
+                                       for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {\r
+                                               if(( i == text_idx && j >= idx ) || i > text_idx ) {\r
+                                                       origSpell = this.wordWin.originalSpellings[i][j];\r
+                                                       if( origSpell == preReplSpell ) {\r
+                                                               this._setWordText ( i, j, origSpell, undefined );\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               }\r
+                               break;\r
+\r
+                       // ignore all: go through all the future occurances of the word\r
+                       // and clear their flags\r
+                       case this.ignrAllFlag :\r
+                               for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {\r
+                                       for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {\r
+                                               if(( i == text_idx && j >= idx ) || i > text_idx ) {\r
+                                                       origSpell = this.wordWin.originalSpellings[i][j];\r
+                                                       if( origSpell == preReplSpell ) {\r
+                                                               this.wordFlags[i][j] = undefined;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               }\r
+                               break;\r
+\r
+                       // replace: revert the word to its original spelling\r
+                       case this.replWordFlag :\r
+                               this._setWordText ( text_idx, idx, preReplSpell, undefined );\r
+                               break;\r
+               }\r
+\r
+               // For all four cases, clear the wordFlag of this word. re-start the process\r
+               this.wordFlags[text_idx][idx] = undefined;\r
+               this._spellcheck();\r
+       }\r
+}\r
+\r
+function _spellcheck() {\r
+       var ww = this.wordWin;\r
+\r
+       // check if this is the last word in the current text element\r
+       if( this.currentWordIndex == ww.totalWords( this.currentTextIndex) ) {\r
+               this.currentTextIndex++;\r
+               this.currentWordIndex = 0;\r
+               // keep going if we're not yet past the last text element\r
+               if( this.currentTextIndex < this.wordWin.textInputs.length ) {\r
+                       this._spellcheck();\r
+                       return;\r
+               } else {\r
+                       this.terminateSpell();\r
+                       return;\r
+               }\r
+       }\r
+\r
+       // if this is after the first one make sure the Undo button is enabled\r
+       if( this.currentWordIndex > 0 ) {\r
+               this.controlWin.enableUndo();\r
+       }\r
+\r
+       // skip the current word if it has already been worked on\r
+       if( this.wordFlags[this.currentTextIndex][this.currentWordIndex] ) {\r
+               // increment the global current word index and move on.\r
+               this.currentWordIndex++;\r
+               this._spellcheck();\r
+       } else {\r
+               var evalText = ww.getTextVal( this.currentTextIndex, this.currentWordIndex );\r
+               if( evalText ) {\r
+                       this.controlWin.evaluatedText.value = evalText;\r
+                       ww.setFocus( this.currentTextIndex, this.currentWordIndex );\r
+                       this._getSuggestions( this.currentTextIndex, this.currentWordIndex );\r
+               }\r
+       }\r
+}\r
+\r
+function _getSuggestions( text_num, word_num ) {\r
+       this.controlWin.clearSuggestions();\r
+       // add suggestion in list for each suggested word.\r
+       // get the array of suggested words out of the\r
+       // three-dimensional array containing all suggestions.\r
+       var a_suggests = this.wordWin.suggestions[text_num][word_num];\r
+       if( a_suggests ) {\r
+               // got an array of suggestions.\r
+               for( var ii = 0; ii < a_suggests.length; ii++ ) {\r
+                       this.controlWin.addSuggestion( a_suggests[ii] );\r
+               }\r
+       }\r
+       this.controlWin.selectDefaultSuggestion();\r
+}\r
+\r
+function _setAsIgnored( text_num, word_num, flag ) {\r
+       // set the UI\r
+       this.wordWin.removeFocus( text_num, word_num );\r
+       // do the bookkeeping\r
+       this.wordFlags[text_num][word_num] = flag;\r
+       return true;\r
+}\r
+\r
+function _getTotalReplaced() {\r
+       var i_replaced = 0;\r
+       for( var i = 0; i < this.wordFlags.length; i++ ) {\r
+               for( var j = 0; j < this.wordFlags[i].length; j++ ) {\r
+                       if(( this.wordFlags[i][j] == this.replWordFlag )\r
+                       || ( this.wordFlags[i][j] == this.replAllFlag )\r
+                       || ( this.wordFlags[i][j] == this.fromReplAll )) {\r
+                               i_replaced++;\r
+                       }\r
+               }\r
+       }\r
+       return i_replaced;\r
+}\r
+\r
+function _setWordText( text_num, word_num, newText, flag ) {\r
+       // set the UI and form inputs\r
+       this.wordWin.setText( text_num, word_num, newText );\r
+       // keep track of what happened to this word:\r
+       this.wordFlags[text_num][word_num] = flag;\r
+       return true;\r
+}\r
+\r
+function _getFormInputs( inputPattern ) {\r
+       var inputs = new Array();\r
+       for( var i = 0; i < document.forms.length; i++ ) {\r
+               for( var j = 0; j < document.forms[i].elements.length; j++ ) {\r
+                       if( document.forms[i].elements[j].type.match( inputPattern )) {\r
+                               inputs[inputs.length] = document.forms[i].elements[j];\r
+                       }\r
+               }\r
+       }\r
+       return inputs;\r
+}\r
+\r