source: sandbox/expressoMail1_2/corretor_ortografico/spell_checker/js/spell_checker.js @ 2404

Revision 2404, 32.5 KB checked in by nathalie.silva, 14 years ago (diff)

Ticket #891 - Correção de problemas quando utiliza HTML - aceita todas as tags. Nathalie

Line 
1/*************************************************************
2 * AJAX Spell Checker - Version 2.8
3 * (C) 2005 - Garrison Locke
4 *
5 * This spell checker is built in the style of the Gmail spell
6 * checker.  It uses AJAX to communicate with the backend without
7 * requiring the page be reloaded.  If you use this code, please
8 * give me credit and a link to my site would be nice.
9 * http://www.broken-notebook.com.
10 *
11 * Copyright (c) 2005, Garrison Locke
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are met:
16 *
17 *   * Redistributions of source code must retain the above copyright notice,
18 *     this list of conditions and the following disclaimer.
19 *   * Redistributions in binary form must reproduce the above copyright notice,
20 *     this list of conditions and the following disclaimer in the documentation
21 *     and/or other materials provided with the distribution.
22 *   * Neither the name of the http://www.broken-notebook.com nor the names of its
23 *     contributors may be used to endorse or promote products derived from this
24 *     software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
36 *
37 *************************************************************/
38
39var cp;
40var currObj; //the current spell checker being used
41var spellingSuggestionsDiv;
42var old_onclick
43cp = new cpaint();
44cp.set_transfer_mode('post');
45cp.set_response_type('text');
46function beginSpellCheck(){
47
48    //var cp = new cpaint();
49   // alert("Seta globals");
50    //cp = new cpaint();
51    //cp.set_transfer_mode('post');
52    //cp.set_response_type('text');
53    //cp.set_debug(1);
54
55    //var currObj; //the current spell checker being used
56    //var spellingSuggestionsDiv = null;  // Auto-generated suggestions div
57
58   
59    spellingSuggestionsDiv = null;
60
61    //alert("Seta globals");
62
63
64// If there are already any onclick handlers loaded in the page, we'll add
65// our onclick handler first and then call the old one, rather than completely
66// overriding it.  The checkClickLocation is used to hide the suggestions div
67// when the user clicks outside it.
68    /*if(document.onclick)
69    {
70        old_onclick = document.onclick;
71       
72        document.onclick = function(e)
73        {
74                checkClickLocation(e);
75                old_onclick(e);
76        }
77}
78else
79{
80        //document.onclick = checkClickLocation;
81}*/
82
83//alert("Inicio");
84// If there are already any onload handlers loaded in the page, we'll add our onload
85// handler first and then call the old one, rather than completely overriding it.
86if(window.onload)
87{
88        window.onload = new setupSpellCheckers();
89       
90        /*var old_onload = window.onload;
91        window.onload = function(e)
92        {
93                var test = new setupSpellCheckers(e);
94                old_onload(e);
95        }*/
96}
97else
98{
99        window.onload = new setupSpellCheckers();
100}
101
102//window.onload =new setupSpellCheckers();
103//alert("Fim");
104} // fecha funᅵᅵo
105
106
107
108
109
110/*************************************************************
111 * function setupSpellCheckers()
112 *
113 * This function goes through the page and finds all the
114 * textareas.  It then checks the title attribute for either
115 * spellcheck or spellcheck_icons to determine whether or not
116 * it should add a spellchecker to that textarea.
117 *************************************************************/
118function setupSpellCheckers()
119{
120        var ifr = document.getElementsByTagName('iframe')[0];
121        var numSpellCheckers = 0;
122        //var tempSpellCheckers = Array();
123               
124        if(ifr.contentWindow)
125           ifr=ifr.contentWindow.document;
126        else
127           ifr=ifr.contentDocument;
128        var texto = ifr.body.innerHTML;
129
130       //  alert("valor textarea");
131       //  alert(texto);
132        // //tempSpellCheckers[numSpellCheckers] = textareas;
133         var tempWidth = "300";
134         var tempHeight = "300";
135         
136        // alert("Antes AjaxSpell");
137         eval('spellCheckers' + numSpellCheckers + '= new ajaxSpell("spellCheckers' + numSpellCheckers + '", tempWidth, tempHeight, "spell_checker/spell_checker.php", "body_position_1", "Nome", "body_1", "Titulo", texto);');
138
139       
140         //new  ajaxSpell('spellTexto', tempWidth, tempHeight, 'spell_checker.php' ,'body_position_1', 'Nome','body_1', 'Titulo', texto);
141         
142         //ajaxSpell("", "", "", "" ,"", "","", "", "");
143
144        //}
145
146        /*for(var i=0; i < textareas.length; i++)
147        {
148                alert("ok iframe");
149                if(textareas[i].getAttribute("title") == "spellcheck" || textareas[i].getAttribute("title") == "spellcheck_icons")
150                {
151                        tempSpellCheckers[numSpellCheckers] = textareas[i];
152                       
153                        //create a new spellchecker for this textarea
154                        var tempWidth = tempSpellCheckers[numSpellCheckers].offsetWidth + 'px';
155                        var tempHeight = tempSpellCheckers[numSpellCheckers].offsetHeight + 'px';
156                        eval('spellCheckers' + numSpellCheckers + '= new ajaxSpell("spellCheckers' + numSpellCheckers + '", tempWidth, tempHeight, tempSpellCheckers[' + numSpellCheckers + '].getAttribute("accesskey"), "spellCheckDiv' + numSpellCheckers + '", tempSpellCheckers[' + numSpellCheckers + '].getAttribute("name"), tempSpellCheckers[' + numSpellCheckers + '].id, tempSpellCheckers[' + numSpellCheckers + '].title, tempSpellCheckers[' + numSpellCheckers + '].value);');
157                        numSpellCheckers++;
158                }
159        }*/
160       
161}; // end setInit
162
163
164/*************************************************************
165 * ajaxSpell(varName, width, height, spellUrl, divId, name, id)
166 *
167 * This is the constructor that creates a new ajaxSpell object.
168 * All of it is dynamically generated so the user doesn't have
169 * to add a bunch of crap to their site.
170 *
171 * @param varName The name of the variable that the object is
172 *                assigned to (must be unique and the same as the variable)
173 * @param width The width of the spell checker
174 * @param height The height of the spell checker
175 * @param spellUrl The url of the spell_checker.php code
176 * @param divId The id of the div that the spell checker is
177 *              contained in (must be unique)
178 * @param name The name of the textarea form element
179 * @param id The id of the spell checker textarea (must be unique)
180 *************************************************************/
181function ajaxSpell(varName, width, height, spellUrl, divId, name, id, title, value)
182{
183        //alert("ajaxSpell");
184
185        currObj = this;
186
187        currObj.config               = new Array();         //the array of configuration options
188        currObj.config['varName']    = varName;             //the name of the variable that this instance is stored in
189        currObj.config['width']      = width;               //the width of the textarea
190        currObj.config['height']     = height;              //the height of the textarea
191        currObj.config['spellUrl']   = spellUrl;            //url to spell checker php code (spell_checker.php by default);
192        currObj.config['divId']      = divId;               //the id of the div that the spell checker element is in
193        currObj.config['name']       = name;                //what you want the form element's name to be
194        currObj.config['id']         = id;                  //the unique id of the spell_checker textarea
195        currObj.config['title']      = title;               //the title (specifies whether to use icons or not);
196        currObj.config['value']      = value;               //the value of the text box when the page was loaded
197
198        currObj.config['value']      = currObj.config['value'].replace(/<br *\/?>/gi, "\n");
199       
200        currObj.config['useIcons'] = false;
201       
202        if(currObj.config['title'] == "spellcheck_icons")
203        {
204                currObj.config['useIcons'] = true;
205        }
206       
207        spellContainer = document.createElement('DIV');
208        spellContainer.id = currObj.config['divId'];
209        spellContainer.className = 'spell_container';
210        spellContainer.style.width = currObj.config['width'];
211
212        oldElement = document.getElementById(currObj.config['id']);
213
214        oldElement.parentNode.replaceChild(spellContainer, oldElement);
215       
216        //generate the div to hold the spell checker controls
217        currObj.controlPanelDiv = document.createElement('DIV');
218        currObj.controlPanelDiv.className = 'control_panel';
219        document.getElementById(currObj.config['divId']).appendChild(currObj.controlPanelDiv);
220       
221        //the span that toggles between spell checking and editing
222        currObj.actionSpan = document.createElement('SPAN');
223        currObj.actionSpan.className = "action";
224        currObj.actionSpan.id = "action";
225        if(currObj.config['useIcons'])
226        {
227                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\"><img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Check Spelling &amp; Preview\" alt=\"Check Spelling &amp; Preview\" border=\"0\" /></a>";
228        }
229        else
230        {
231                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\">Check Spelling &amp; Preview</a>";
232        }
233        currObj.controlPanelDiv.appendChild(currObj.actionSpan);
234       
235        //the span that lets the user know of the status of the spell checker
236        currObj.statusSpan = document.createElement('SPAN');
237        currObj.statusSpan.className = "status";
238        currObj.statusSpan.id = "status";
239        currObj.statusSpan.innerHTML = "";
240        currObj.controlPanelDiv.appendChild(currObj.statusSpan);
241       
242        //the textarea to be spell checked
243        oldElement.value = currObj.config['value'];
244        document.getElementById(currObj.config['divId']).appendChild(oldElement);
245       
246        currObj.objToCheck              = document.getElementById(currObj.config['id']);      //the actual object we're spell checking
247        currObj.spellingResultsDiv      = null;                                               // Auto-generated results div
248               
249        //prototypes for the ajaxSpell objects
250        ajaxSpell.prototype.spellCheck           = spellCheck;
251        ajaxSpell.prototype.spellCheck_cb        = spellCheck_cb;
252        ajaxSpell.prototype.showSuggestions      = showSuggestions;
253        ajaxSpell.prototype.showSuggestions_cb   = showSuggestions_cb;
254        ajaxSpell.prototype.replaceWord          = replaceWord;
255        ajaxSpell.prototype.switchText           = switchText;
256        ajaxSpell.prototype.switchText_cb        = switchText_cb;
257        ajaxSpell.prototype.resumeEditing        = resumeEditing;
258        ajaxSpell.prototype.resetSpellChecker    = resetSpellChecker;
259        ajaxSpell.prototype.resetAction          = resetAction;
260}; // end ajaxSpell
261
262
263/*************************************************************
264 * setCurrentObject
265 *
266 * This sets the current object to be the spell checker that
267 * the user is currently using.
268 *
269 * @param obj The spell checker currently being used
270 *************************************************************/
271function setCurrentObject(obj)
272{
273        currObj = obj;
274       
275}; // end setCurrentObject
276
277
278/*************************************************************
279 * spellCheck_cb
280 *
281 * This is the callback function that the spellCheck php function
282 * returns the spell checked data to.  It sets the results div
283 * to contain the markedup misspelled data and changes the status
284 * message.  It also sets the width and height of the results
285 * div to match the element that's being checked.
286 * If there are no misspellings then new_data is the empty
287 * string and the status is set to "No Misspellings Found".
288 *
289 * @param new_data The marked up misspelled data returned from php.
290 *************************************************************/
291function spellCheck_cb(new_data)
292{
293        with(currObj);
294        new_data = new_data.toString();
295        var isThereAMisspelling = new_data.charAt(0);
296        new_data = new_data.substring(1);
297               
298        if(currObj.spellingResultsDiv)
299        {
300                currObj.spellingResultsDiv.parentNode.removeChild(spellingResultsDiv);
301        }
302       
303        currObj.spellingResultsDiv = document.createElement('DIV');
304        currObj.spellingResultsDiv.className = 'edit_box';
305        currObj.spellingResultsDiv.style.width = currObj.objToCheck.style.width;
306        currObj.spellingResultsDiv.style.height = currObj.objToCheck.style.height;
307        currObj.spellingResultsDiv.innerHTML = new_data;
308       
309        currObj.objToCheck.style.display = "none";
310        currObj.objToCheck.parentNode.insertBefore(currObj.spellingResultsDiv,currObj.objToCheck);
311        currObj.statusSpan.innerHTML = "";
312       
313        if(currObj.config['useIcons'])
314        {
315                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".resumeEditing();\"><img src=\"images/page_white_edit.png\" width=\"16\" height=\"16\" title=\"Resume Editing\" alt=\"Resume Editing\" border=\"0\" /></a>";
316        }
317        else
318        {
319                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".resumeEditing();\">Resume Editing</a>";
320        }
321               
322        if(isThereAMisspelling != "1")
323        {
324                if(currObj.config['useIcons'])
325                {
326                        currObj.statusSpan.innerHTML = "<img src=\"images/accept.png\" width=\"16\" height=\"16\" title=\"No Misspellings Found\" alt=\"No Misspellings Found\" border=\"0\" />";
327                }
328                else
329                {
330                        currObj.statusSpan.innerHTML = "No Misspellings Found";
331                }
332                currObj.objToCheck.disabled = false;
333        }
334}; // end spellCheck_cb
335
336
337/*************************************************************
338 * spellCheck()
339 *
340 * The spellCheck javascript function sends the text entered by
341 * the user in the text box to php to be spell checked.  It also
342 * sets the status message to "Checking..." because it's currently
343 * checking the spelling.
344 *************************************************************/
345function spellCheck() {
346        with(currObj);
347        var query;
348       
349        if(currObj.spellingResultsDiv)
350        {
351                currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
352                currObj.spellingResultsDiv = null;
353        }
354       
355        if(currObj.config['useIcons'])
356        {
357                currObj.actionSpan.innerHTML = "<img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Check Spelling &amp; Preview\" alt=\"Check Spelling &amp; Preview\" border=\"0\" />";
358        }
359        else
360        {
361                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\">Check Spelling &amp; Preview</a>";
362        }
363       
364        if(currObj.config['useIcons'])
365        {
366                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Checking...\" alt=\"Checking...\" border=\"0\" />";
367        }
368        else
369        {
370                currObj.statusSpan.innerHTML = "Checking...";
371        }
372       
373       
374        // Obtain the HTML content from iframe (edit area)
375         var ifr= currObj.objToCheck;
376         if(ifr.contentWindow)
377            ifr=ifr.contentWindow.document;
378         else
379            ifr=ifr.contentDocument;
380         var texto = ifr.body.innerHTML;
381       
382       
383        query = texto;
384        //query = currObj.objToCheck.value;
385       
386        query = query.replace(/\r?\n/gi, "<br />");
387       
388        cp.call(currObj.config['spellUrl'], 'spellCheck', spellCheck_cb, query, currObj.config['varName']);
389}; // end spellcheck
390
391
392
393/*************************************************************
394 * addWord
395 *
396 * The addWord function adds a word to the custom dictionary
397 * file.
398 *
399 * @param id The id of the span that contains the word to be added
400 *************************************************************/
401function addWord(id)
402{
403        var wordToAdd = document.getElementById(id).innerHTML;
404       
405        with(currObj);
406       
407        if(spellingSuggestionsDiv)
408        {
409                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
410                spellingSuggestionsDiv = null;
411        }
412       
413        if(currObj.config['useIcons'])
414        {
415                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Adding Word...\" alt=\"Adding Word...\" border=\"0\" />";
416        }
417        else
418        {
419                currObj.statusSpan.innerHTML = "Adding Word...";
420        }
421       
422        cp.call(currObj.config['spellUrl'], 'addWord', addWord_cb, wordToAdd);
423
424}; // end addWord
425
426/*************************************************************
427 * addWord_cb
428 *
429 * The addWord_cb function is a callback function that
430 * PHP's addWord function returns to.  It recieves the
431 * return status of the add to word to personal dictionary call.
432 * It hides the status item.
433 *
434 * @param returnedData The return code from PHP.
435 *************************************************************/
436function addWord_cb(returnedData)
437{
438        //alert(returnedData);
439        with(currObj);
440        currObj.statusSpan.innerHTML = "";
441        resumeEditing();
442        spellCheck();
443}; // end addWord_cb
444
445
446
447/*************************************************************
448 * checkClickLocation(e)
449 *
450 * This function is called by the event listener when the user
451 * clicks on anything.  It is used to close the suggestion div
452 * if the user clicks anywhere that's not inside the suggestion
453 * div.  It just checks to see if the name of what the user clicks
454 * on is not "suggestions" then hides the div if it's not.
455 *
456 * @param e The event, in this case the user clicking somewhere on
457 *          the page.
458 *************************************************************/
459function checkClickLocation(e)
460{
461
462   // alert("checkClickLocation");
463    if(spellingSuggestionsDiv)
464        {
465                // Bah.  There's got to be a better way to deal with this, but the click
466                // on a word to get suggestions starts up a race condition between
467                // showing and hiding the suggestion box, so we'll ignore the first
468                // click.
469                if(spellingSuggestionsDiv.ignoreNextClick){
470                        spellingSuggestionsDiv.ignoreNextClick = false;
471                }
472                else
473                {
474                        var theTarget = getTarget(e);
475                       
476                        if(theTarget != spellingSuggestionsDiv)
477                        {
478                                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
479                                spellingSuggestionsDiv = null;
480                        }
481                }
482        }
483       
484        return true; // Allow other handlers to continue.
485}; //end checkClickLocation
486
487
488/*************************************************************
489 * getTarget
490 *
491 * The get target function gets the correct target of the event.
492 * This function is required because IE handles the events in
493 * a different (wrong) manner than the rest of the browsers.
494 *
495 * @param e The target, in this case the user clicking somewhere on
496 *     the page.
497 *
498 *************************************************************/
499function getTarget(e)
500{
501        var value;
502        if(checkBrowser() == "ie")
503        {
504                value = window.event.srcElement;
505        }
506        else
507        {
508                value = e.target;
509        }
510        return value;
511}; //end getTarget
512
513
514/*************************************************************
515 * checkBrowser()
516 *
517 * The checkBrowser function simply checks to see what browser
518 * the user is using and returns a string containing the browser
519 * type.
520 *
521 * @return string The browser type
522 *************************************************************/
523function checkBrowser()
524{
525        var theAgent = navigator.userAgent.toLowerCase();
526        if(theAgent.indexOf("msie") != -1)
527        {
528                if(theAgent.indexOf("opera") != -1)
529                {
530                        return "opera";
531                }
532                else
533                {
534                        return "ie";
535                }
536        }
537        else if(theAgent.indexOf("netscape") != -1)
538        {
539                return "netscape";
540        }
541        else if(theAgent.indexOf("firefox") != -1)
542        {
543                return "firefox";
544        }
545        else if(theAgent.indexOf("mozilla/5.0") != -1)
546        {
547                return "mozilla";
548        }
549        else if(theAgent.indexOf("\/") != -1)
550        {
551                if(theAgent.substr(0,theAgent.indexOf('\/')) != 'mozilla')
552                {
553                        return navigator.userAgent.substr(0,theAgent.indexOf('\/'));
554                }
555                else
556                {
557                        return "netscape";
558                }
559        }
560        else if(theAgent.indexOf(' ') != -1)
561        {
562                return navigator.userAgent.substr(0,theAgent.indexOf(' '));
563        }
564        else
565        {
566                return navigator.userAgent;
567        }
568}; // end checkBrowser
569
570
571/*************************************************************
572 * showSuggestions_cb
573 *
574 * The showSuggestions_cb function is a callback function that
575 * php's showSuggestions function returns to.  It sets the
576 * suggestions table to contain the new data and then displays
577 * the suggestions div.  It also clears the status message.
578 *
579 * @param new_data The suggestions table returned from php.
580 *************************************************************/
581function showSuggestions_cb(new_data)
582{
583        with(currObj);
584        spellingSuggestionsDiv.innerHTML = new_data;
585        spellingSuggestionsDiv.style.display = 'block';
586        currObj.statusSpan.innerHTML = "";
587}; //end showSuggestions_cb
588
589
590/*************************************************************
591 * showSuggestions
592 *
593 * The showSuggestions function calls the showSuggestions php
594 * function to get suggestions for the misspelled word that the
595 * user has clicked on.  It sets the status to "Searching...",
596 * hides the suggestions div, finds the x and y position of the
597 * span containing the misspelled word that user clicked on so
598 * the div can be displayed in the correct location, and then
599 * calls the showSuggestions php function with the misspelled word
600 * and the id of the span containing it.
601 *
602 * @param word The misspelled word that the user clicked on
603 * @param id The id of the span that contains the misspelled word
604 *************************************************************/
605function showSuggestions(word, id)
606{
607        with(currObj);
608        if(currObj.config['useIcons'])
609        {
610                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Searching...\" alt=\"Searching...\" border=\"0\" />";
611        }
612        else
613        {
614                currObj.statusSpan.innerHTML = "Searching...";
615        }
616        var x = findPosXById(id);
617        var y = findPosYById(id);
618       
619        var scrollPos = 0;
620        if(checkBrowser() != "ie")
621        {
622                scrollPos = currObj.spellingResultsDiv.scrollTop;
623        }
624
625        if(spellingSuggestionsDiv)
626        {
627                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
628        }
629        spellingSuggestionsDiv = document.createElement('DIV');
630        spellingSuggestionsDiv.style.display = "none";
631        spellingSuggestionsDiv.className = 'suggestion_box';
632        spellingSuggestionsDiv.style.position = 'absolute';
633        spellingSuggestionsDiv.style.left = x + 'px';
634        spellingSuggestionsDiv.style.top = (y+16-scrollPos) + 'px';
635       
636        // Bah. There's got to be a better way to deal with this, but the click
637        // on a word to get suggestions starts up a race condition between
638        // showing and hiding the suggestion box, so we'll ignore the first
639        // click.
640        spellingSuggestionsDiv.ignoreNextClick = true;
641       
642        document.body.appendChild(spellingSuggestionsDiv);
643       
644        cp.call(currObj.config['spellUrl'], 'showSuggestions', showSuggestions_cb, word, id);
645}; // end showSuggestions
646
647
648/*************************************************************
649 * replaceWord
650 *
651 * The replaceWord function takes the id of the misspelled word
652 * that the user clicked on and replaces the innerHTML of that
653 * span with the new word that the user selects from the suggestion
654 * div.  It hides the suggestions div and changes the color of
655 * the previously misspelled word to green to let the user know
656 * it has been changed.  It then calls the switchText php function
657 * with the innerHTML of the div to update the text of the text box.
658 *
659 * @param id The id of the span that contains the word to be replaced
660 * @param newWord The word the user selected from the suggestions div
661 *                to replace the misspelled word.
662 *************************************************************/
663function replaceWord(id, newWord)
664{
665        document.getElementById(id).innerHTML = trim(newWord);
666        if(spellingSuggestionsDiv)
667        {
668                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
669                spellingSuggestionsDiv = null;
670        }
671        document.getElementById(id).className = "corrected_word";
672}; // end replaceWord
673
674
675/*************************************************************
676 * switchText
677 *
678 * The switchText function is a funtion is called when the user
679 * clicks on resume editing (or submits the form).  It calls the
680 * php function to switchText and uncomments the html and replaces
681 * breaks and everything.  Here all the breaks that the user has
682 * typed are replaced with %u2026.  Firefox does this goofy thing
683 * where it cleans up the display of your html, which adds in \n's
684 * where you don't want them.  So I replace the user-entered returns
685 * with something unique so that I can rip out all the breaks that
686 * the browser might add and we don't want.
687 *************************************************************/
688function switchText()
689{
690        with(currObj);
691        var parentElement = currObj.spellingResultsDiv;
692
693        // Obtain all span tags which have highlight className or corrected_word className
694        var nodeArray = parentElement.getElementsByTagName("span");
695        var totArray = nodeArray.length;
696        var nodeRemove = new Array(totArray);
697        var j = -1;
698        for (var i = 0 ; i < totArray ; i++) {
699            var node = nodeArray[i];
700            if (node.className == "highlight" || node.className == "corrected_word") {
701                j++;
702                nodeRemove[j] = node.id;
703               
704               
705            }
706        }
707
708        //Remove span tags which have highlight className or corrected_word className
709        for (var i = 0 ; i <= j ; i++) {
710            var valorNo = document.getElementById(nodeRemove[i]).innerHTML;
711            var fake = document.createTextNode(valorNo);
712            var parent =  document.getElementById(nodeRemove[i]).parentNode;
713            parent.replaceChild(fake, document.getElementById(nodeRemove[i]));           
714        }
715        var text = currObj.spellingResultsDiv.innerHTML;
716
717        text = text.replace(/&nbsp;/gi, " %u2026 "); // Replace &nbsp; with the code %u2026
718        text = '*' + text;
719        cp.call(currObj.config['spellUrl'], 'switchText', switchText_cb, text);
720       
721}; // end switchText
722
723
724/*************************************************************
725 * switchText_cb
726 *
727 * The switchText_cb function is a call back funtion that the
728 * switchText php function returns to.  I replace all the %u2026's
729 * with returns.  It then replaces the text in the text box with
730 * the corrected text fromt he div.
731 *
732 * @param new_string The corrected text from the div.
733 *
734 *************************************************************/
735function switchText_cb(new_string)
736{
737        with(currObj);
738       
739        new_string = new_string.replace(/ %u2026/gi, "&nbsp;"); // Replace the code %u2026 with &nbsp;
740             
741        new_string = new_string.replace(/~~~/gi, "\n");
742       
743        // Remove the prefixed asterisk that was added in switchText().
744        new_string = new_string.substr(1);
745
746
747        currObj.objToCheck.style.display = "none";
748         var ifr= currObj.objToCheck;
749         if(ifr.contentWindow)
750            ifr=ifr.contentWindow.document;
751         else
752            ifr=ifr.contentDocument;
753         var texto = ifr.body.innerHTML;
754     
755         ifr.body.innerHTML = new_string;
756        currObj.objToCheck.disabled = false;
757        if(currObj.spellingResultsDiv)
758        {
759                currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
760                currObj.spellingResultsDiv = null;
761        }
762        currObj.objToCheck.style.display = "block";
763        currObj.resetAction();
764}; // end switchText_cb
765
766
767/*************************************************************
768 * resumeEditing
769 *
770 * The resumeEditing function is called when the user is in the
771 * correction mode and wants to return to the editing mode.  It
772 * hides the results div and the suggestions div, then enables
773 * the text box and unhides the text box.  It also calls
774 * resetAction() to reset the status message.
775 *************************************************************/
776function resumeEditing()
777{
778        with(currObj);
779        if(currObj.config['useIcons'])
780        {
781                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\"><img src=\"images/page_white_edit.png\" width=\"16\" height=\"16\" title=\"Resume Editing\" alt=\"Resume Editing\" border=\"0\" /></a>";
782        }
783        else
784        {
785                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\">Resume Editing</a>";
786        }
787        if(currObj.config['useIcons'])
788        {
789                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Working...\" alt=\"Working...\" border=\"0\" />";
790        }
791        else
792        {
793                currObj.statusSpan.innerHTML = "Working...";
794        }
795       
796       
797        if(spellingSuggestionsDiv)
798        {
799               
800                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
801                spellingSuggestionsDiv = null;
802        }
803       
804        currObj.switchText();
805}; // end resumeEditing
806
807
808/*************************************************************
809 * resetAction
810 *
811 * The resetAction function just resets the status message to
812 * the default action of "Check Spelling".
813 *************************************************************/
814function resetAction()
815{
816        with(currObj);
817        if(currObj.config['useIcons'])
818        {
819                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\"><img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Check Spelling &amp; Preview\" alt=\"Check Spelling &amp; Preview\" border=\"0\" /></a>";
820        }
821        else
822        {
823                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\">Check Spelling &amp; Preview</a>";
824        }
825
826        currObj.statusSpan.innerHTML = "";
827}; // end resetAction
828
829
830/*************************************************************
831 * resetSpellChecker
832 *
833 * The resetSpellChecker function resets the entire spell checker
834 * to the defaults.
835 *************************************************************/
836function resetSpellChecker()
837{
838        with(currObj);
839        currObj.resetAction();
840       
841        currObj.objToCheck.value = "";
842        currObj.objToCheck.style.display = "block";
843        currObj.objToCheck.disabled = false;
844       
845        if(currObj.spellingResultsDiv)
846        {
847                currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
848                currObj.spellingResultsDiv = null;
849        }
850        if(spellingSuggestionsDiv)
851        {
852                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
853                spellingSuggestionsDiv = null;
854        }
855        currObj.statusSpan.style.display = "none";
856       
857}; // end resetSpellChecker
858
859
860/*************************************************************
861 * findPosX
862 *
863 * The findPosX function just finds the X offset of the top left
864 * corner of the object id it's given.
865 *
866 * @param object The id of the object that you want to find the
867 *               upper left X coordinate of.
868 * @return int The X coordinate of the object
869 *************************************************************/
870function findPosXById(object)
871{
872        var curleft = 0;
873        var obj = document.getElementById(object);
874        if(obj.offsetParent)
875        {
876                while(obj.offsetParent)
877                {
878                        curleft += obj.offsetLeft - obj.scrollLeft;
879                        obj = obj.offsetParent;
880                }
881        }
882        else if(obj.x)
883        {
884                curleft += obj.x;
885        }
886        return curleft;
887}; // end findPosX
888
889
890/*************************************************************
891 * findPosY
892 *
893 * The findPosY function just finds the Y offset of the top left
894 * corner of the object id it's given.
895 *
896 * @param object The id of the object that you want to find the
897 *               upper left Y coordinate of.
898 * @return int The Y coordinate of the object
899 *************************************************************/
900function findPosYById(object)
901{
902        var curtop = 0;var curtop = 0;
903        var obj = document.getElementById(object);
904        if(obj.offsetParent)
905        {
906                while(obj.offsetParent)
907                {
908                        curtop += obj.offsetTop - obj.scrollTop;
909                        obj = obj.offsetParent;
910                }
911        }
912        else if(obj.y)
913        {
914                curtop += obj.y;
915        }
916        return curtop;
917}; // end findPosY
918
919
920/*************************************************************
921 * trim
922 *
923 * Trims white space from a string.
924 *
925 * @param s The string you want to trim.
926 * @return string The trimmed string.
927 *************************************************************/
928function trim(s)
929{
930        while(s.substring(0,1) == ' ')
931        {
932        s = s.substring(1,s.length);
933        }
934        while(s.substring(s.length-1,s.length) == ' ')
935        {
936        s = s.substring(0,s.length-1);
937        }
938        return s;
939}; // end trim
Note: See TracBrowser for help on using the repository browser.