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

Revision 2430, 31.8 KB checked in by nathalie.silva, 14 years ago (diff)

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