PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/hypervm/httpdocs/htmllib/fckeditor/editor/dialog/fck_spellerpages/spellerpages/spellChecker.js

https://github.com/yourhty/hypervm
JavaScript | 463 lines | 333 code | 50 blank | 80 comment | 102 complexity | 725c35ba6f14f6b1fddf7127cc711974 MD5 | raw file
  1. ////////////////////////////////////////////////////
  2. // spellChecker.js
  3. //
  4. // spellChecker object
  5. //
  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.
  8. //
  9. ////////////////////////////////////////////////////
  10. // constructor
  11. function spellChecker( textObject ) {
  12. // public properties - configurable
  13. // this.popUpUrl = '/speller/spellchecker.html'; // by FredCK
  14. this.popUpUrl = 'fck_spellerpages/spellerpages/spellchecker.html'; // by FredCK
  15. this.popUpName = 'spellchecker';
  16. // this.popUpProps = "menu=no,width=440,height=350,top=70,left=120,resizable=yes,status=yes"; // by FredCK
  17. this.popUpProps = null ; // by FredCK
  18. // this.spellCheckScript = '/speller/server-scripts/spellchecker.php'; // by FredCK
  19. //this.spellCheckScript = '/cgi-bin/spellchecker.pl';
  20. // values used to keep track of what happened to a word
  21. this.replWordFlag = "R"; // single replace
  22. this.ignrWordFlag = "I"; // single ignore
  23. this.replAllFlag = "RA"; // replace all occurances
  24. this.ignrAllFlag = "IA"; // ignore all occurances
  25. this.fromReplAll = "~RA"; // an occurance of a "replace all" word
  26. this.fromIgnrAll = "~IA"; // an occurance of a "ignore all" word
  27. // properties set at run time
  28. this.wordFlags = new Array();
  29. this.currentTextIndex = 0;
  30. this.currentWordIndex = 0;
  31. this.spellCheckerWin = null;
  32. this.controlWin = null;
  33. this.wordWin = null;
  34. this.textArea = textObject; // deprecated
  35. this.textInputs = arguments;
  36. // private methods
  37. this._spellcheck = _spellcheck;
  38. this._getSuggestions = _getSuggestions;
  39. this._setAsIgnored = _setAsIgnored;
  40. this._getTotalReplaced = _getTotalReplaced;
  41. this._setWordText = _setWordText;
  42. this._getFormInputs = _getFormInputs;
  43. // public methods
  44. this.openChecker = openChecker;
  45. this.startCheck = startCheck;
  46. this.checkTextBoxes = checkTextBoxes;
  47. this.checkTextAreas = checkTextAreas;
  48. this.spellCheckAll = spellCheckAll;
  49. this.ignoreWord = ignoreWord;
  50. this.ignoreAll = ignoreAll;
  51. this.replaceWord = replaceWord;
  52. this.replaceAll = replaceAll;
  53. this.terminateSpell = terminateSpell;
  54. this.undo = undo;
  55. // set the current window's "speller" property to the instance of this class.
  56. // this object can now be referenced by child windows/frames.
  57. window.speller = this;
  58. }
  59. // call this method to check all text boxes (and only text boxes) in the HTML document
  60. function checkTextBoxes() {
  61. this.textInputs = this._getFormInputs( "^text$" );
  62. this.openChecker();
  63. }
  64. // call this method to check all textareas (and only textareas ) in the HTML document
  65. function checkTextAreas() {
  66. this.textInputs = this._getFormInputs( "^textarea$" );
  67. this.openChecker();
  68. }
  69. // call this method to check all text boxes and textareas in the HTML document
  70. function spellCheckAll() {
  71. this.textInputs = this._getFormInputs( "^text(area)?$" );
  72. this.openChecker();
  73. }
  74. // call this method to check text boxe(s) and/or textarea(s) that were passed in to the
  75. // object's constructor or to the textInputs property
  76. function openChecker() {
  77. this.spellCheckerWin = window.open( this.popUpUrl, this.popUpName, this.popUpProps );
  78. if( !this.spellCheckerWin.opener ) {
  79. this.spellCheckerWin.opener = window;
  80. }
  81. }
  82. function startCheck( wordWindowObj, controlWindowObj ) {
  83. // set properties from args
  84. this.wordWin = wordWindowObj;
  85. this.controlWin = controlWindowObj;
  86. // reset properties
  87. this.wordWin.resetForm();
  88. this.controlWin.resetForm();
  89. this.currentTextIndex = 0;
  90. this.currentWordIndex = 0;
  91. // initialize the flags to an array - one element for each text input
  92. this.wordFlags = new Array( this.wordWin.textInputs.length );
  93. // each element will be an array that keeps track of each word in the text
  94. for( var i=0; i<this.wordFlags.length; i++ ) {
  95. this.wordFlags[i] = [];
  96. }
  97. // start
  98. this._spellcheck();
  99. return true;
  100. }
  101. function ignoreWord() {
  102. var wi = this.currentWordIndex;
  103. var ti = this.currentTextIndex;
  104. if( !this.wordWin ) {
  105. alert( 'Error: Word frame not available.' );
  106. return false;
  107. }
  108. if( !this.wordWin.getTextVal( ti, wi )) {
  109. alert( 'Error: "Not in dictionary" text is missing.' );
  110. return false;
  111. }
  112. // set as ignored
  113. if( this._setAsIgnored( ti, wi, this.ignrWordFlag )) {
  114. this.currentWordIndex++;
  115. this._spellcheck();
  116. }
  117. return true;
  118. }
  119. function ignoreAll() {
  120. var wi = this.currentWordIndex;
  121. var ti = this.currentTextIndex;
  122. if( !this.wordWin ) {
  123. alert( 'Error: Word frame not available.' );
  124. return false;
  125. }
  126. // get the word that is currently being evaluated.
  127. var s_word_to_repl = this.wordWin.getTextVal( ti, wi );
  128. if( !s_word_to_repl ) {
  129. alert( 'Error: "Not in dictionary" text is missing' );
  130. return false;
  131. }
  132. // set this word as an "ignore all" word.
  133. this._setAsIgnored( ti, wi, this.ignrAllFlag );
  134. // loop through all the words after this word
  135. for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {
  136. for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {
  137. if(( i == ti && j > wi ) || i > ti ) {
  138. // future word: set as "from ignore all" if
  139. // 1) do not already have a flag and
  140. // 2) have the same value as current word
  141. if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )
  142. && ( !this.wordFlags[i][j] )) {
  143. this._setAsIgnored( i, j, this.fromIgnrAll );
  144. }
  145. }
  146. }
  147. }
  148. // finally, move on
  149. this.currentWordIndex++;
  150. this._spellcheck();
  151. return true;
  152. }
  153. function replaceWord() {
  154. var wi = this.currentWordIndex;
  155. var ti = this.currentTextIndex;
  156. if( !this.wordWin ) {
  157. alert( 'Error: Word frame not available.' );
  158. return false;
  159. }
  160. if( !this.wordWin.getTextVal( ti, wi )) {
  161. alert( 'Error: "Not in dictionary" text is missing' );
  162. return false;
  163. }
  164. if( !this.controlWin.replacementText ) {
  165. return false ;
  166. }
  167. var txt = this.controlWin.replacementText;
  168. if( txt.value ) {
  169. var newspell = new String( txt.value );
  170. if( this._setWordText( ti, wi, newspell, this.replWordFlag )) {
  171. this.currentWordIndex++;
  172. this._spellcheck();
  173. }
  174. }
  175. return true;
  176. }
  177. function replaceAll() {
  178. var ti = this.currentTextIndex;
  179. var wi = this.currentWordIndex;
  180. if( !this.wordWin ) {
  181. alert( 'Error: Word frame not available.' );
  182. return false;
  183. }
  184. var s_word_to_repl = this.wordWin.getTextVal( ti, wi );
  185. if( !s_word_to_repl ) {
  186. alert( 'Error: "Not in dictionary" text is missing' );
  187. return false;
  188. }
  189. var txt = this.controlWin.replacementText;
  190. if( !txt.value ) return false;
  191. var newspell = new String( txt.value );
  192. // set this word as a "replace all" word.
  193. this._setWordText( ti, wi, newspell, this.replAllFlag );
  194. // loop through all the words after this word
  195. for( var i = ti; i < this.wordWin.textInputs.length; i++ ) {
  196. for( var j = 0; j < this.wordWin.totalWords( i ); j++ ) {
  197. if(( i == ti && j > wi ) || i > ti ) {
  198. // future word: set word text to s_word_to_repl if
  199. // 1) do not already have a flag and
  200. // 2) have the same value as s_word_to_repl
  201. if(( this.wordWin.getTextVal( i, j ) == s_word_to_repl )
  202. && ( !this.wordFlags[i][j] )) {
  203. this._setWordText( i, j, newspell, this.fromReplAll );
  204. }
  205. }
  206. }
  207. }
  208. // finally, move on
  209. this.currentWordIndex++;
  210. this._spellcheck();
  211. return true;
  212. }
  213. function terminateSpell() {
  214. // called when we have reached the end of the spell checking.
  215. var msg = ""; // by FredCK
  216. var numrepl = this._getTotalReplaced();
  217. if( numrepl == 0 ) {
  218. // see if there were no misspellings to begin with
  219. if( !this.wordWin ) {
  220. msg = "";
  221. } else {
  222. if( this.wordWin.totalMisspellings() ) {
  223. // msg += "No words changed."; // by FredCK
  224. msg += FCKLang.DlgSpellNoChanges ; // by FredCK
  225. } else {
  226. // msg += "No misspellings found."; // by FredCK
  227. msg += FCKLang.DlgSpellNoMispell ; // by FredCK
  228. }
  229. }
  230. } else if( numrepl == 1 ) {
  231. // msg += "One word changed."; // by FredCK
  232. msg += FCKLang.DlgSpellOneChange ; // by FredCK
  233. } else {
  234. // msg += numrepl + " words changed."; // by FredCK
  235. msg += FCKLang.DlgSpellManyChanges.replace( /%1/g, numrepl ) ;
  236. }
  237. if( msg ) {
  238. // msg += "\n"; // by FredCK
  239. alert( msg );
  240. }
  241. if( numrepl > 0 ) {
  242. // update the text field(s) on the opener window
  243. for( var i = 0; i < this.textInputs.length; i++ ) {
  244. // this.textArea.value = this.wordWin.text;
  245. if( this.wordWin ) {
  246. if( this.wordWin.textInputs[i] ) {
  247. this.textInputs[i].value = this.wordWin.textInputs[i];
  248. }
  249. }
  250. }
  251. }
  252. // return back to the calling window
  253. // this.spellCheckerWin.close(); // by FredCK
  254. if ( typeof( this.OnFinished ) == 'function' ) // by FredCK
  255. this.OnFinished(numrepl) ; // by FredCK
  256. return true;
  257. }
  258. function undo() {
  259. // skip if this is the first word!
  260. var ti = this.currentTextIndex;
  261. var wi = this.currentWordIndex;
  262. if( this.wordWin.totalPreviousWords( ti, wi ) > 0 ) {
  263. this.wordWin.removeFocus( ti, wi );
  264. // go back to the last word index that was acted upon
  265. do {
  266. // if the current word index is zero then reset the seed
  267. if( this.currentWordIndex == 0 && this.currentTextIndex > 0 ) {
  268. this.currentTextIndex--;
  269. this.currentWordIndex = this.wordWin.totalWords( this.currentTextIndex )-1;
  270. if( this.currentWordIndex < 0 ) this.currentWordIndex = 0;
  271. } else {
  272. if( this.currentWordIndex > 0 ) {
  273. this.currentWordIndex--;
  274. }
  275. }
  276. } while (
  277. this.wordWin.totalWords( this.currentTextIndex ) == 0
  278. || this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromIgnrAll
  279. || this.wordFlags[this.currentTextIndex][this.currentWordIndex] == this.fromReplAll
  280. );
  281. var text_idx = this.currentTextIndex;
  282. var idx = this.currentWordIndex;
  283. var preReplSpell = this.wordWin.originalSpellings[text_idx][idx];
  284. // if we got back to the first word then set the Undo button back to disabled
  285. if( this.wordWin.totalPreviousWords( text_idx, idx ) == 0 ) {
  286. this.controlWin.disableUndo();
  287. }
  288. var i, j, origSpell ;
  289. // examine what happened to this current word.
  290. switch( this.wordFlags[text_idx][idx] ) {
  291. // replace all: go through this and all the future occurances of the word
  292. // and revert them all to the original spelling and clear their flags
  293. case this.replAllFlag :
  294. for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {
  295. for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {
  296. if(( i == text_idx && j >= idx ) || i > text_idx ) {
  297. origSpell = this.wordWin.originalSpellings[i][j];
  298. if( origSpell == preReplSpell ) {
  299. this._setWordText ( i, j, origSpell, undefined );
  300. }
  301. }
  302. }
  303. }
  304. break;
  305. // ignore all: go through all the future occurances of the word
  306. // and clear their flags
  307. case this.ignrAllFlag :
  308. for( i = text_idx; i < this.wordWin.textInputs.length; i++ ) {
  309. for( j = 0; j < this.wordWin.totalWords( i ); j++ ) {
  310. if(( i == text_idx && j >= idx ) || i > text_idx ) {
  311. origSpell = this.wordWin.originalSpellings[i][j];
  312. if( origSpell == preReplSpell ) {
  313. this.wordFlags[i][j] = undefined;
  314. }
  315. }
  316. }
  317. }
  318. break;
  319. // replace: revert the word to its original spelling
  320. case this.replWordFlag :
  321. this._setWordText ( text_idx, idx, preReplSpell, undefined );
  322. break;
  323. }
  324. // For all four cases, clear the wordFlag of this word. re-start the process
  325. this.wordFlags[text_idx][idx] = undefined;
  326. this._spellcheck();
  327. }
  328. }
  329. function _spellcheck() {
  330. var ww = this.wordWin;
  331. // check if this is the last word in the current text element
  332. if( this.currentWordIndex == ww.totalWords( this.currentTextIndex) ) {
  333. this.currentTextIndex++;
  334. this.currentWordIndex = 0;
  335. // keep going if we're not yet past the last text element
  336. if( this.currentTextIndex < this.wordWin.textInputs.length ) {
  337. this._spellcheck();
  338. return;
  339. } else {
  340. this.terminateSpell();
  341. return;
  342. }
  343. }
  344. // if this is after the first one make sure the Undo button is enabled
  345. if( this.currentWordIndex > 0 ) {
  346. this.controlWin.enableUndo();
  347. }
  348. // skip the current word if it has already been worked on
  349. if( this.wordFlags[this.currentTextIndex][this.currentWordIndex] ) {
  350. // increment the global current word index and move on.
  351. this.currentWordIndex++;
  352. this._spellcheck();
  353. } else {
  354. var evalText = ww.getTextVal( this.currentTextIndex, this.currentWordIndex );
  355. if( evalText ) {
  356. this.controlWin.evaluatedText.value = evalText;
  357. ww.setFocus( this.currentTextIndex, this.currentWordIndex );
  358. this._getSuggestions( this.currentTextIndex, this.currentWordIndex );
  359. }
  360. }
  361. }
  362. function _getSuggestions( text_num, word_num ) {
  363. this.controlWin.clearSuggestions();
  364. // add suggestion in list for each suggested word.
  365. // get the array of suggested words out of the
  366. // three-dimensional array containing all suggestions.
  367. var a_suggests = this.wordWin.suggestions[text_num][word_num];
  368. if( a_suggests ) {
  369. // got an array of suggestions.
  370. for( var ii = 0; ii < a_suggests.length; ii++ ) {
  371. this.controlWin.addSuggestion( a_suggests[ii] );
  372. }
  373. }
  374. this.controlWin.selectDefaultSuggestion();
  375. }
  376. function _setAsIgnored( text_num, word_num, flag ) {
  377. // set the UI
  378. this.wordWin.removeFocus( text_num, word_num );
  379. // do the bookkeeping
  380. this.wordFlags[text_num][word_num] = flag;
  381. return true;
  382. }
  383. function _getTotalReplaced() {
  384. var i_replaced = 0;
  385. for( var i = 0; i < this.wordFlags.length; i++ ) {
  386. for( var j = 0; j < this.wordFlags[i].length; j++ ) {
  387. if(( this.wordFlags[i][j] == this.replWordFlag )
  388. || ( this.wordFlags[i][j] == this.replAllFlag )
  389. || ( this.wordFlags[i][j] == this.fromReplAll )) {
  390. i_replaced++;
  391. }
  392. }
  393. }
  394. return i_replaced;
  395. }
  396. function _setWordText( text_num, word_num, newText, flag ) {
  397. // set the UI and form inputs
  398. this.wordWin.setText( text_num, word_num, newText );
  399. // keep track of what happened to this word:
  400. this.wordFlags[text_num][word_num] = flag;
  401. return true;
  402. }
  403. function _getFormInputs( inputPattern ) {
  404. var inputs = new Array();
  405. for( var i = 0; i < document.forms.length; i++ ) {
  406. for( var j = 0; j < document.forms[i].elements.length; j++ ) {
  407. if( document.forms[i].elements[j].type.match( inputPattern )) {
  408. inputs[inputs.length] = document.forms[i].elements[j];
  409. }
  410. }
  411. }
  412. return inputs;
  413. }