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

/Quản lý website bán phụ tùng oto PHP/FCKeditor/editor/dialog/fck_spellerpages/spellerpages/spellChecker.js

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