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

Revision 2550, 47.1 KB checked in by paula.franceschini, 14 years ago (diff)

Ticket #891 - modificações para o corretor ortográfico

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;
46
47cp = new cpaint();
48cp.set_transfer_mode('post');
49cp.set_response_type('text');
50
51function beginSpellCheck(){
52
53        position = "body_position_" + currentTab;
54        area = "body_" + currentTab;
55        aba = currentTab;
56        spellingSuggestionsDiv = null;
57        positionEditor = position;
58        areaEditor = area;
59        abaEditor = aba;
60
61        if(window.onload)
62        {
63                window.onload = new setupSpellCheckers();
64
65        }
66        else
67        {
68                window.onload = new setupSpellCheckers();
69        }
70
71} // close BeginSpellCheck
72
73
74
75
76/*************************************************************
77 * function setupSpellCheckers()
78 *
79 * This function obtain the iframe of the page (that is the area editor)
80 * and then adds a spellchecker to the iframe.
81 *************************************************************/
82function setupSpellCheckers()
83{
84    var ifr = document.getElementsByTagName('iframe')[0];
85    //alert(area);
86    //var ifr = document.getElementById(area);
87    var numSpellCheckers = abaEditor;
88
89    if(ifr.contentWindow)
90        ifr=ifr.contentWindow.document;
91    else
92        ifr=ifr.contentDocument;
93
94     if (document.addEventListener) {
95        ifr.addEventListener("click", checkClickLocation , true); //add the click event
96     }   
97     else {
98        ifr.attachEvent("onclick", checkClickLocation); //add the click event
99       
100     }
101   
102
103    var texto = ifr.body.innerHTML;
104
105    var tempWidth = "99%";
106    var tempHeight = "300";
107
108    eval('spellCheckers' + numSpellCheckers + '= new ajaxSpell("spellCheckers' + numSpellCheckers + '", tempWidth, tempHeight, "spell_checker/spell_checker.php", positionEditor, "Nome", areaEditor, "Titulo", texto);');
109       
110}; // end setInit
111
112function setupSpellChecker(){
113
114
115        currObj = this;
116       
117        currObj.config               = new Array();
118       
119        currObj.config['divId'] = "body_position_" + currentTab;
120        currObj.config['width'] = "99%";
121        currObj.config['id'] = "body_" + currentTab;
122
123        spellContainer = document.createElement('DIV');
124        spellContainer.id = currObj.config['divId'];
125        spellContainer.className = 'spell_container';
126        spellContainer.style.width = currObj.config['width'];
127
128        //oldElement = document.getElementById(currObj.config['id']);
129
130        //oldElement.parentNode.replaceChild(spellContainer, oldElement);
131
132        //generate the div to hold the spell checker controls
133        currObj.controlPanelDiv = document.createElement('DIV');
134        currObj.controlPanelDiv.className = 'control_panel';
135        currObj.controlPanelDiv.id = 'control_panel_' + currentTab;
136        document.getElementById(currObj.config['divId']).appendChild(currObj.controlPanelDiv);
137
138        //if (document.getElementById(currObj.config['id']))
139        //    alert("OK");
140
141        //document.getElementById(currObj.config['divId']).insertBefore(currObj.controlPanelDiv, document.getElementById(currObj.config['id']));
142        //currObj.controlPanelDiv.parentNode.insertBefore(currObj.controlPanelDiv, document.getElementById(currObj.config["id"]));
143
144     
145       
146       
147
148        //the span that toggles between spell checking and editing
149        currObj.actionSpan = document.createElement('SPAN');
150        currObj.actionSpan.className = "action";
151        currObj.actionSpan.id = "action";       
152        /*if(currObj.config['useIcons'])
153        {
154                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>";
155        }
156        else
157        {
158                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\">Verificar Ortografia</a>";
159        }*/
160        currObj.controlPanelDiv.appendChild(currObj.actionSpan);
161
162        //the span that lets the user know of the status of the spell checker
163        currObj.statusSpan = document.createElement('SPAN');
164        currObj.statusSpan.className = "status";
165        currObj.statusSpan.id = "status";
166        currObj.statusSpan.innerHTML = "";
167        currObj.controlPanelDiv.style.display = "none";
168        currObj.controlPanelDiv.appendChild(currObj.statusSpan);
169
170
171       //document.getElementById(currObj.config['divId']).insertBefore(currObj.controlPanelDiv, document.getElementById(currObj.config['id'])); //test
172
173        //document.getElementById("body_position_" + currentTab).insertBefore(currObj.controlPanelDiv, document.getElementById("viewsource_rt_checkbox"));
174        //the textarea to be spell checked
175        //oldElement.value = currObj.config['value'];
176        //document.getElementById(currObj.config['divId']).appendChild(oldElement);
177        currObj.controlPanelDiv.parentNode.insertBefore(currObj.controlPanelDiv, document.getElementById("viewsource_rt_checkbox_" + currentTab));
178        //currObj.controlPanelDiv.parentNode.insertBefore(currObj.controlPanelDiv, document.getElementById(currObj.config["id"]));
179     
180
181 }
182
183
184/*************************************************************
185 * ajaxSpell(varName, width, height, spellUrl, divId, name, id)
186 *
187 * This is the constructor that creates a new ajaxSpell object.
188 * All of it is dynamically generated so the user doesn't have
189 * to add a bunch of crap to their site.
190 *
191 * @param varName The name of the variable that the object is
192 *                assigned to (must be unique and the same as the variable)
193 * @param width The width of the spell checker
194 * @param height The height of the spell checker
195 * @param spellUrl The url of the spell_checker.php code
196 * @param divId The id of the div that the spell checker is
197 *              contained in (must be unique)
198 * @param name The name of the textarea form element
199 * @param id The id of the spell checker textarea (must be unique)
200 *************************************************************/
201function ajaxSpell(varName, width, height, spellUrl, divId, name, id, title, value)
202{
203
204        currObj = this;
205
206
207        currObj.config               = new Array();         //the array of configuration options
208        currObj.config['varName']    = varName;             //the name of the variable that this instance is stored in
209        currObj.config['width']      = width;               //the width of the textarea
210        currObj.config['height']     = height;              //the height of the textarea
211        currObj.config['spellUrl']   = spellUrl;            //url to spell checker php code (spell_checker.php by default);
212        currObj.config['divId']      = divId;               //the id of the div that the spell checker element is in
213        currObj.config['name']       = name;                //what you want the form element's name to be
214        currObj.config['id']         = id;                  //the unique id of the spell_checker textarea
215        currObj.config['title']      = title;               //the title (specifies whether to use icons or not);
216        currObj.config['value']      = value;               //the value of the text box when the page was loaded
217        currObj.config['aba']      = abaEditor;
218
219        //currObj.config['value']      = currObj.config['value'].replace(/<br *\/?>/gi, "\n"); // Comment from the original by Nathalie
220       
221        currObj.config['useIcons'] = false;
222       
223        if(currObj.config['title'] == "spellcheck_icons")
224        {
225                currObj.config['useIcons'] = true;
226        }
227       
228        /*spellContainer = document.createElement('DIV');
229        spellContainer.id = currObj.config['divId'];
230        spellContainer.className = 'spell_container';
231        spellContainer.style.width = currObj.config['width'];
232
233        oldElement = document.getElementById(currObj.config['id']);
234
235        //oldElement.parentNode.replaceChild(spellContainer, oldElement);
236       
237        //generate the div to hold the spell checker controls
238        currObj.controlPanelDiv = document.createElement('DIV');
239        currObj.controlPanelDiv.className = 'control_panel';
240        document.getElementById(currObj.config['divId']).appendChild(currObj.controlPanelDiv);
241       
242        //the span that toggles between spell checking and editing
243        currObj.actionSpan = document.createElement('SPAN');
244        currObj.actionSpan.className = "action";
245        currObj.actionSpan.id = "action";
246        /*if(currObj.config['useIcons'])
247        {
248                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>";
249        }
250        else
251        {
252                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\">Verificar Ortografia</a>";
253        }*/
254        /*currObj.controlPanelDiv.appendChild(currObj.actionSpan);
255       
256        //the span that lets the user know of the status of the spell checker
257        currObj.statusSpan = document.createElement('SPAN');
258        currObj.statusSpan.className = "status";
259        currObj.statusSpan.id = "status";
260        currObj.statusSpan.innerHTML = "";
261        currObj.controlPanelDiv.appendChild(currObj.statusSpan);
262
263        //document.getElementById("body_position_" + currentTab).insertBefore(currObj.controlPanelDiv, document.getElementById("viewsource_rt_checkbox"));
264        //the textarea to be spell checked
265        oldElement.value = currObj.config['value'];
266        //document.getElementById(currObj.config['divId']).appendChild(oldElement);
267        currObj.controlPanelDiv.parentNode.insertBefore(currObj.controlPanelDiv, document.getElementById("viewsource_rt_checkbox_" + currentTab));
268        */
269
270        currObj.controlPanelDiv = document.getElementById('control_panel_' + currentTab);
271        currObj.statusSpan = currObj.controlPanelDiv.childNodes[1];
272        currObj.actionSpan = currObj.controlPanelDiv.childNodes[0] ;
273
274        currObj.objToCheck              = document.getElementById(currObj.config['id']);      //the actual object we're spell checking
275        currObj.spellingResultsDiv      = null;                                               // Auto-generated results div
276               
277        //prototypes for the ajaxSpell objects
278        ajaxSpell.prototype.spellCheck           = spellCheck;
279        ajaxSpell.prototype.spellCheck_cb        = spellCheck_cb;
280        ajaxSpell.prototype.showSuggestions      = showSuggestions;
281        ajaxSpell.prototype.showSuggestions_cb   = showSuggestions_cb;
282        ajaxSpell.prototype.replaceWord          = replaceWord;
283        ajaxSpell.prototype.switchText           = switchText;
284        ajaxSpell.prototype.switchText_cb        = switchText_cb;
285        ajaxSpell.prototype.resumeEditing        = resumeEditing;
286        ajaxSpell.prototype.resetSpellChecker    = resetSpellChecker;
287        ajaxSpell.prototype.resetAction          = resetAction;
288        ajaxSpell.prototype.ignore               = ignore;
289
290
291        //set the contentEditable to false for IE browsers
292        var iframe= currObj.objToCheck;
293        if(iframe.contentWindow)
294            iframe=iframe.contentWindow.document;
295        else
296            iframe=iframe.contentDocument;
297
298        var browser = checkBrowser();
299        if (browser == "ie"){
300            iframe.body.contentEditable="false" ;
301
302        }
303
304       
305     
306       
307}; // end ajaxSpell
308
309
310/*************************************************************
311 * setCurrentObject
312 *
313 * This sets the current object to be the spell checker that
314 * the user is currently using.
315 *
316 * @param obj The spell checker currently being used
317 *************************************************************/
318function setCurrentObject(obj)
319{
320        currObj  = obj;
321       
322       
323}; // end setCurrentObject
324
325
326/*************************************************************
327 * showMenu
328 *
329 * This function is associated with the click event
330 *  of all the span tags with correctd_word class.
331 *
332 *************************************************************/
333function showMenu(){
334
335    var iframe= currObj.objToCheck;
336    if(iframe.contentWindow)
337        iframe=iframe.contentWindow.document;
338    else
339        iframe=iframe.contentDocument;
340
341    var browser = checkBrowser();
342    if (browser == "ie"){
343       
344        iframe.body.contentEditable="true" ;
345        var ifr= currObj.objToCheck;
346        if(!e){var e = ifr.contentWindow.event;}
347        if(!e.target){e.target = e.srcElement;}
348        var evento = e.target.onclick.toString();
349
350    }
351    else {
352        iframe.designMode = "off";
353        iframe.designMode = "on";
354        var evento = this.onclick.toString();
355    } 
356    evento = evento.replace("function onclick(event) {", "");
357    evento = evento.replace("}", "");
358    var array_func = evento.split(";");
359    eval(array_func[0]);
360    eval(array_func[1]);
361    return false;   
362       
363 
364//
365}
366
367/*************************************************************
368 * replaceMenu
369 *
370 * This function is associated with the click event
371 *  of all the div tags with suggestion class.
372 *
373 *************************************************************/
374function replaceMenu(){     
375    var iframe= currObj.objToCheck;
376    if(iframe.contentWindow)
377        iframe=iframe.contentWindow.document;
378    else
379        iframe=iframe.contentDocument;
380    var browser = checkBrowser();
381    if (browser == "ie"){
382        iframe.body.contentEditable="false" ;
383        var ifr= currObj.objToCheck;
384        if(!e){var e = ifr.contentWindow.event;}
385        if(!e.target){e.target = e.srcElement;}
386        var evento = e.target.onclick.toString();
387    }
388    else {
389        iframe.designMode = "off";
390        iframe.designMode = "on";
391        var evento = this.onclick.toString();
392    }
393    evento = evento.replace("function onclick(event) {", "");
394    evento = evento.replace("}", "");
395    var array_func = evento.split(";");
396    eval(array_func[0]);
397    return false;
398}
399
400
401
402/*************************************************************
403 * spellCheck_cb
404 *
405 * This is the callback function that the spellCheck php function
406 * returns the spell checked data to.  It sets the results div
407 * to contain the markedup misspelled data and changes the status
408 * message.  It also sets the width and height of the results
409 * div to match the element that's being checked.
410 * If there are no misspellings then new_data is the empty
411 * string and the status is set to "No Misspellings Found".
412 *
413 * @param new_data The marked up misspelled data returned from php.
414 *************************************************************/
415function spellCheck_cb(new_data)
416{
417         
418        var ifr= currObj.objToCheck;
419        if(ifr.contentWindow)
420            ifr=ifr.contentWindow.document;
421        else
422            ifr=ifr.contentDocument;
423
424
425        with(currObj);
426
427        new_data = new_data.toString();
428        var isThereAMisspelling = new_data.charAt(0);
429        new_data = new_data.substring(1);
430
431        if(currObj.spellingResultsDiv)
432        {
433           
434                ifr.removeChild(currObj.spellingResultsDiv.id);
435               
436        }
437
438
439         var ifr= currObj.objToCheck;
440         if(ifr.contentWindow)
441            ifr=ifr.contentWindow.document;
442         else
443            ifr=ifr.contentDocument;
444         ifr.body.innerHTML =  "";
445         var cssLink = ifr.createElement("link");
446         cssLink.href = "spell_checker/css/spell_checker.css";
447         cssLink .rel = "stylesheet";
448         cssLink .type = "text/css";           
449              //Testes nathalie - substituido
450        currObj.spellingResultsDiv =  ifr.createElement('span');
451        currObj.spellingResultsDiv.className = 'edit_box';
452        currObj.spellingResultsDiv.id = 'edit_box'; //Added the id property - By Nathalie
453        currObj.spellingResultsDiv.style.width =  "99%";//the width of the textarea
454        currObj.spellingResultsDiv.style.height = 300;  //   = height; //currObj.objToCheck.style.height;
455        currObj.spellingResultsDiv.innerHTML = new_data;
456        currObj.spellingResultsDiv.border = 0;
457        //currObj.spellingResultsDiv.style.display = "none";
458        //currObj.objToCheck.style.display = "none";
459        currObj.statusSpan.innerHTML = ""; 
460       
461
462
463       
464         //ifr.body.appendChild(currObj.spellingResultsDiv);
465         ifr.body.innerHTML = new_data;
466         ifr.body.appendChild(cssLink);
467
468
469        // Obtain all the span tags which have highlight className
470        //    and add the eventListener for the click - This event shows the suggestions menu
471        var nodeArray = ifr.getElementsByTagName("div");
472        var totArray = nodeArray.length;
473        for (var i = 0 ; i < totArray ; i++) {
474            var node = nodeArray[i];
475            if (node.className == "highlight") {
476                if (document.addEventListener) {
477                    ifr.getElementsByTagName("div")[i].addEventListener("click", showMenu , true);
478                    ifr.getElementsByTagName("div")[i].contentEditable="false" ;
479                }
480                else {
481                    ifr.getElementsByTagName("div")[i].attachEvent("onclick", showMenu );
482                    ifr.getElementsByTagName("div")[i].contentEditable="false" ;
483                }
484
485            }
486        }
487
488
489        currObj.statusSpan.innerHTML = "";
490
491       
492        if(currObj.config['useIcons'])
493        {
494                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>";
495        }
496        else
497        {
498                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".resumeEditing();\">Continuar Editando </a>";
499        }
500               
501        if(isThereAMisspelling != "1")
502        {
503                if(currObj.config['useIcons'])
504                {
505                        currObj.statusSpan.innerHTML = "<img src=\"images/accept.png\" width=\"16\" height=\"16\" title=\"Nenhum Erro Encontrado\" alt=\"Nenhum Erro Encontrado\" border=\"0\" />";
506                }
507                else
508                {
509                        currObj.statusSpan.innerHTML = "Nenhum Erro Encontrado";
510                }
511                currObj.objToCheck.disabled = false;
512        }
513
514       currObj.controlPanelDiv.style.display = "block";
515
516}; // end spellCheck_cb
517
518
519/*************************************************************
520 * spellCheck()
521 *
522 * The spellCheck javascript function sends the text entered by
523 * the user in the text box to php to be spell checked.  It also
524 * sets the status message to "Checking..." because it's currently
525 * checking the spelling.
526 *************************************************************/
527function spellCheck() {
528// adicionado nathalie
529/*        with(currObj);
530        var query;
531
532        if(currObj.spellingResultsDiv)
533        {
534                currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
535                currObj.spellingResultsDiv = null;
536        }
537       
538        if(currObj.config['useIcons'])
539        {
540                currObj.actionSpan.innerHTML = "<img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Verificar Ortografia\" alt=\"Verificar Ortografia\" border=\"0\" />";
541        }
542        else
543        {
544                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\">Verificar Ortografia</a>";
545        }
546       
547        if(currObj.config['useIcons'])
548        {
549                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Verificando Ortografia...\" alt=\"Verificando Ortografia...\" border=\"0\" />";
550        }
551        else
552        {
553                currObj.statusSpan.innerHTML = "Verificando Ortografia...";
554        }       
555       
556        // Obtain the HTML content from iframe (edit area)
557         var ifr= currObj.objToCheck;
558         if(ifr.contentWindow)
559            ifr=ifr.contentWindow.document;
560         else
561            ifr=ifr.contentDocument;
562         var texto = ifr.body.innerHTML;
563       
564       
565        query = texto;
566        //query = currObj.objToCheck.value;       
567       
568        //query = query.replace(/\r?\n/gi, "<br />"); //  Commented from the original by Nathalie
569        query = query.replace(/\r?\n/gi, " ");  // replace \n with " " - By Nathalie
570       
571       
572        cp.call(currObj.config['spellUrl'], 'spellCheck', spellCheck_cb, query, currObj.config['varName']);
573*/
574//adicionado Paula
575         languageId =  document.getElementById("selectLanguage");
576            languageId = languageId[document.getElementById("selectLanguage").selectedIndex].value;
577
578            with(currObj);
579            var query;
580
581            currObj.controlPanelDiv.style.display = "block";
582
583            if(currObj.spellingResultsDiv)
584            {
585                    currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
586                    currObj.spellingResultsDiv = null;
587            }
588
589            /*if(currObj.config['useIcons'])
590            {
591                    currObj.actionSpan.innerHTML = "<img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Verificar Ortografia\" alt=\"Verificar Ortografia\" border=\"0\" />";
592            }
593            else
594            {
595                    currObj.actionSpan.innerHTML = "<a class=\"check_spelling\">Verificar Ortografia</a>";
596            }*/
597
598            /*if(currObj.config['useIcons'])
599            {
600                    currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Verificando Ortografia...\" alt=\"Verificando Ortografia...\" border=\"0\" />";
601            }
602            else
603            {
604                    currObj.statusSpan.innerHTML = "Verificando Ortografia...";
605            }*/
606
607
608            // Obtain the HTML content from iframe (edit area)
609             var ifr= currObj.objToCheck;
610             if(ifr.contentWindow)
611                ifr=ifr.contentWindow.document;
612             else
613                ifr=ifr.contentDocument;
614             var texto = ifr.body.innerHTML;
615
616            query = texto;
617            //query = currObj.objToCheck.value;
618
619            //query = query.replace(/\r?\n/gi, "<br />"); //  Commented from the original by Nathalie
620            query = query.replace(/\r?\n/gi, " ");  // replace \n with " " - By Nathalie
621
622            cp.call(currObj.config['spellUrl'], 'spellCheck', spellCheck_cb, query, currObj.config['varName'], languageId);
623
624}; // end spellcheck
625
626
627
628/*************************************************************
629 * addWord
630 *
631 * The addWord function adds a word to the custom dictionary
632 * file.
633 *
634 * @param id The id of the span that contains the word to be added
635 *************************************************************/
636function addWord(id)
637{
638        var wordToAdd = document.getElementById(id).innerHTML;
639       
640        with(currObj);
641       
642        if(spellingSuggestionsDiv)
643        {
644                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
645                spellingSuggestionsDiv = null;
646        }
647
648        currObj.controlPanelDiv.style.display = "block";
649
650        if(currObj.config['useIcons'])
651        {
652                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Adding Word...\" alt=\"Adding Word...\" border=\"0\" />";
653        }
654        else
655        {
656                currObj.statusSpan.innerHTML = "Adding Word...";
657        }
658       
659        cp.call(currObj.config['spellUrl'], 'addWord', addWord_cb, wordToAdd);
660
661}; // end addWord
662
663/*************************************************************
664 * addWord_cb
665 *
666 * The addWord_cb function is a callback function that
667 * PHP's addWord function returns to.  It recieves the
668 * return status of the add to word to personal dictionary call.
669 * It hides the status item.
670 *
671 * @param returnedData The return code from PHP.
672 *************************************************************/
673function addWord_cb(returnedData)
674{
675        with(currObj);
676        currObj.statusSpan.innerHTML = "";
677        resumeEditing();
678        spellCheck();
679}; // end addWord_cb
680
681
682
683/*************************************************************
684 * checkClickLocation(e)
685 *
686 * This function is called by the event listener when the user
687 * clicks on anything.  It is used to close the suggestion div
688 * if the user clicks anywhere that's not inside the suggestion
689 * div.  It just checks to see if the name of what the user clicks
690 * on is not "suggestions" then hides the div if it's not.
691 *
692 * @param e The event, in this case the user clicking somewhere on
693 *          the page.
694 *************************************************************/
695function checkClickLocation(e)
696{
697
698    var browser = checkBrowser();
699    if(spellingSuggestionsDiv)
700        {
701                // Bah.  There's got to be a better way to deal with this, but the click
702                // on a word to get suggestions starts up a race condition between
703                // showing and hiding the suggestion box, so we'll ignore the first
704                // click. Problem with IE browser               
705                if(spellingSuggestionsDiv.ignoreNextClick && browser == "ie"){
706                        spellingSuggestionsDiv.ignoreNextClick = false;
707                }
708                else
709                {
710                        var theTarget = getTarget(e);
711                       
712                        if(theTarget != spellingSuggestionsDiv)
713                        {
714                                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
715                                spellingSuggestionsDiv = null;
716                               
717                                if (browser == "ie"){
718                                     var iframe= currObj.objToCheck;
719                                    if(iframe.contentWindow)
720                                        iframe=iframe.contentWindow.document;
721                                    else
722                                        iframe=iframe.contentDocument;
723                                    iframe.body.contentEditable="false" ;
724                                }
725                         }
726                }
727        }
728       
729        return true; // Allow other handlers to continue.
730}; //end checkClickLocation
731
732
733/*************************************************************
734 * getTarget
735 *
736 * The get target function gets the correct target of the event.
737 * This function is required because IE handles the events in
738 * a different (wrong) manner than the rest of the browsers.
739 *
740 * @param e The target, in this case the user clicking somewhere on
741 *     the page.
742 *
743 *************************************************************/
744function getTarget(e)
745{
746       
747        var value;
748        if(checkBrowser() == "ie")
749        {
750            if(!e){var e = ifr.contentWindow.event;}
751            if(!e.target){value = e.srcElement;}
752           
753        }
754        else
755        {
756            value = e.target;
757        }
758        return value;
759}; //end getTarget
760
761
762/*************************************************************
763 * checkBrowser()
764 *
765 * The checkBrowser function simply checks to see what browser
766 * the user is using and returns a string containing the browser
767 * type.
768 *
769 * @return string The browser type
770 *************************************************************/
771function checkBrowser()
772{
773        var theAgent = navigator.userAgent.toLowerCase();
774        if(theAgent.indexOf("msie") != -1)
775        {
776                if(theAgent.indexOf("opera") != -1)
777                {
778                        return "opera";
779                }
780                else
781                {
782                        return "ie";
783                }
784        }
785        else if(theAgent.indexOf("netscape") != -1)
786        {
787                return "netscape";
788        }
789        else if(theAgent.indexOf("firefox") != -1)
790        {
791                return "firefox";
792        }
793        else if(theAgent.indexOf("mozilla/5.0") != -1)
794        {
795                return "mozilla";
796        }
797        else if(theAgent.indexOf("\/") != -1)
798        {
799                if(theAgent.substr(0,theAgent.indexOf('\/')) != 'mozilla')
800                {
801                        return navigator.userAgent.substr(0,theAgent.indexOf('\/'));
802                }
803                else
804                {
805                        return "netscape";
806                }
807        }
808        else if(theAgent.indexOf(' ') != -1)
809        {
810                return navigator.userAgent.substr(0,theAgent.indexOf(' '));
811        }
812        else
813        {
814                return navigator.userAgent;
815        }
816}; // end checkBrowser
817
818
819/*************************************************************
820 * showSuggestions_cb
821 *
822 * The showSuggestions_cb function is a callback function that
823 * php's showSuggestions function returns to.  It sets the
824 * suggestions table to contain the new data and then displays
825 * the suggestions div.  It also adds the event listener - click -
826 * for all the suggestions tags and sets te property to be not editable.
827 *
828 * @param new_data The suggestions table returned from php.
829 *************************************************************/
830function showSuggestions_cb(new_data)
831{
832       
833        with(currObj);
834        spellingSuggestionsDiv.innerHTML = new_data;
835        spellingSuggestionsDiv.style.display = 'block';
836        currObj.statusSpan.innerHTML = "";
837
838        var ifr= currObj.objToCheck;
839        if(ifr.contentWindow)
840           ifr=ifr.contentWindow.document;
841        else
842           ifr=ifr.contentDocument;
843
844        var nodeArray = ifr.getElementsByTagName("div");
845        var totArray = nodeArray.length;
846        for (var i = 0 ; i < totArray ; i++) {
847            var node = nodeArray[i];
848            if (node.className == "suggestion" || node.className == "ignore" ) {
849                 if (document.addEventListener){
850                    ifr.getElementsByTagName("div")[i].addEventListener("click", replaceMenu , true);
851                    ifr.getElementsByTagName("div")[i].contentEditable="false" ;
852                 }
853                 else {
854                    ifr.getElementsByTagName("div")[i].attachEvent("onclick", replaceMenu);
855                    ifr.getElementsByTagName("div")[i].contentEditable="false" ;
856                 }
857                 
858
859            }
860        }
861
862
863}; //end showSuggestions_cb
864
865
866/*************************************************************
867 * showSuggestions
868 *
869 * The showSuggestions function calls the showSuggestions php
870 * function to get suggestions for the misspelled word that the
871 * user has clicked on.  It sets the status to "Searching...",
872 * hides the suggestions div, finds the x and y position of the
873 * span containing the misspelled word that user clicked on so
874 * the div can be displayed in the correct location, and then
875 * calls the showSuggestions php function with the misspelled word
876 * and the id of the span containing it.
877 * This function is only executed when the word is in red (the word id misspelled).
878 *
879 * @param word The misspelled word that the user clicked on
880 * @param id The id of the span that contains the misspelled word
881 *************************************************************/
882function showSuggestions(word, id)
883{
884               
885         var ifr= currObj.objToCheck;
886         if(ifr.contentWindow)
887            ifr=ifr.contentWindow.document;
888         else
889            ifr=ifr.contentDocument;
890
891         if (ifr.getElementById(id).className == "highlight"){ //show the suggestion box only if the words are in red
892
893   
894            languageId =  document.getElementById("selectLanguage");
895            languageId = languageId[document.getElementById("selectLanguage").selectedIndex].value;
896
897
898            with(currObj);
899            if(currObj.config['useIcons'])
900            {
901                    currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Procurando...\" alt=\"Procurando...\" border=\"0\" />";
902            }
903            else
904            {
905                    currObj.statusSpan.innerHTML = "Procurando...";
906            }
907
908            var x = findPosXById(id);
909            var y = findPosYById(id);
910
911            var scrollPos = 0;
912            if(checkBrowser() != "ie")
913            {
914
915
916                    //scrollPos = ifr.getElementById(currObj.spellingResultsDiv.id).scrollTop;
917                    scrollPos = ifr.getElementsByTagName("body")[0].scrollTop;
918
919            }
920
921            if(spellingSuggestionsDiv)
922            {
923                    spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
924            }
925            spellingSuggestionsDiv = ifr.createElement('div');
926            spellingSuggestionsDiv.style.display = "none";
927            spellingSuggestionsDiv.className = 'suggestion_box';
928            spellingSuggestionsDiv.style.position = 'absolute';
929            spellingSuggestionsDiv.style.left = x + 'px';
930            spellingSuggestionsDiv.style.top = (y+16-scrollPos) + 'px';
931            spellingSuggestionsDiv.id = 'suggestion_box';
932            spellingSuggestionsDiv.contentEditable="false" ;
933
934
935            // Bah. There's got to be a better way to deal with this, but the click
936            // on a word to get suggestions starts up a race condition between
937            // showing and hiding the suggestion box, so we'll ignore the first
938            // click.
939            spellingSuggestionsDiv.ignoreNextClick = true;
940
941
942            //document.body.appendChild(spellingSuggestionsDiv);
943            ifr.body.appendChild(spellingSuggestionsDiv);
944
945
946            cp.call(currObj.config['spellUrl'], 'showSuggestions', showSuggestions_cb, word, id, languageId);
947        } // end if - show the suggestion box only if the words are in red
948
949}; // end showSuggestions
950
951
952
953/*************************************************************
954 * replaceWord
955 *
956 * The replaceWord function replaces the innerHTML of all the span tags
957 * that contains the old word with the new word that the user selects
958 * from the suggestion div.  It hides the suggestions div and changes the color of
959 * the previously misspelled word to green to let the user know
960 * it has been changed.  It then calls the switchText php function
961 * with the innerHTML of the div to update the text of the text box.
962 *
963 * @param id The id of the element to be checked
964 * @param newword The word the user selected from the suggestions div
965 *                to replace the misspelled word.
966 *************************************************************/
967function replaceWord(id, newWord)
968{
969
970
971    var ifr= currObj.objToCheck;
972    if(ifr.contentWindow)
973        ifr=ifr.contentWindow.document;
974    else
975        ifr=ifr.contentDocument;
976
977    var valorNo = trim(ifr.getElementById(id).innerHTML);
978
979       
980    // Obtain all span tags which have highlight className and contais the old word
981    var nodeArray = ifr.getElementsByTagName("div");
982    var totArray = nodeArray.length;
983    var nodeRemove = new Array(totArray);
984    var j = -1;
985    for (var i = 0 ; i < totArray ; i++) {
986        var node = nodeArray[i];
987        if (node.className == "highlight" && trim(node.innerHTML) == valorNo ) {
988            j++;
989            nodeRemove[j] = node.id;
990        }
991    }
992
993    //Replace the class of the span tags with highlight and the innerHTML with the new_word
994    for (var i = 0 ; i <= j ; i++) {
995        ifr.getElementById(nodeRemove[i]).innerHTML = newWord;
996        ifr.getElementById(nodeRemove[i]).className = "corrected_word";
997    }   
998
999
1000    if(spellingSuggestionsDiv)
1001    {
1002        spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
1003        spellingSuggestionsDiv = null;
1004    }   
1005
1006    return false;           
1007
1008
1009}; // end replaceWord
1010
1011
1012/*************************************************************
1013 * ignore
1014 *
1015 * The ignore function removes the span tags and mantain
1016 * the original word. So, the word comes back to the original, without
1017 * css.
1018 *
1019 * @param id The id of the element to be checked
1020 * @param word The original word.
1021 *************************************************************/
1022
1023function ignore(id, word){
1024
1025 
1026    var ifr= currObj.objToCheck;
1027    if(ifr.contentWindow)
1028        ifr=ifr.contentWindow.document;
1029    else
1030        ifr=ifr.contentDocument;
1031
1032    var valorNo = trim(word);
1033    if(spellingSuggestionsDiv)
1034    {
1035
1036        spellingSuggestionsDiv.style.display = 'none';
1037        spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
1038        spellingSuggestionsDiv = null;
1039    }
1040
1041    var fake = ifr.createTextNode(valorNo);
1042
1043    var parent =  ifr.getElementById(id).parentNode;
1044
1045
1046    parent.replaceChild(fake, ifr.getElementById(id));
1047
1048    return false;
1049
1050
1051
1052};
1053
1054
1055
1056
1057/*************************************************************
1058 * switchText
1059 *
1060 * The switchText function is a funtion is called when the user
1061 * clicks on resume editing (or submits the form).  It calls the
1062 * php function to switchText and uncomments the html and replaces
1063 * breaks and everything.  Here all the breaks that the user has
1064 * typed are replaced with %u2026.  Firefox does this goofy thing
1065 * where it cleans up the display of your html, which adds in \n's
1066 * where you don't want them.  So I replace the user-entered returns
1067 * with something unique so that I can rip out all the breaks that
1068 * the browser might add and we don't want.
1069 *************************************************************/
1070function switchText()
1071{
1072       
1073        with(currObj);
1074        var ifr= currObj.objToCheck;
1075        if(ifr.contentWindow)
1076            ifr=ifr.contentWindow.document;
1077        else
1078            ifr=ifr.contentDocument;
1079     
1080        // Obtain all span tags which have highlight className or corrected_word className
1081        var nodeArray = ifr.getElementsByTagName("div");
1082        var totArray = nodeArray.length;
1083        var nodeRemove = new Array(totArray);
1084        var j = -1;
1085
1086        for (var i = 0 ; i < totArray ; i++) {
1087            var node = nodeArray[i];
1088            if (node.className == "highlight" || node.className == "corrected_word") {
1089                j++;
1090                nodeRemove[j] = node.id;
1091               
1092               
1093            }
1094        }
1095
1096        //Remove span tags which have highlight className or corrected_word className
1097        for (var i = 0 ; i <= j ; i++) {
1098            var valorNo = ifr.getElementById(nodeRemove[i]).innerHTML;
1099            var fake = ifr.createTextNode(valorNo);
1100            var parent =  ifr.getElementById(nodeRemove[i]).parentNode;
1101            parent.replaceChild(fake, ifr.getElementById(nodeRemove[i]));
1102        }
1103
1104        var text = ifr.body.innerHTML;
1105
1106        text = text.replace(/&nbsp;/gi, " %u2026 "); // Replace &nbsp; with the code %u2026
1107        text = '*' + text;
1108
1109        cp.call(currObj.config['spellUrl'], 'switchText', switchText_cb, text);
1110       
1111}; // end switchText
1112
1113
1114/*************************************************************
1115 * switchText_cb
1116 *
1117 * The switchText_cb function is a call back funtion that the
1118 * switchText php function returns to.  I replace all the %u2026's
1119 * with returns.  It then replaces the text in the text box with
1120 * the corrected text fromt he div.
1121 *
1122 * @param new_string The corrected text from the div.
1123 *
1124 *************************************************************/
1125function switchText_cb(new_string)
1126{
1127
1128
1129    with(currObj);
1130
1131    new_string = new_string.replace(/ %u2026/gi, "&nbsp;"); // Replace the code %u2026 with &nbsp;
1132
1133    new_string = new_string.replace(/~~~/gi, "\n");
1134
1135    // Remove the prefixed asterisk that was added in switchText().
1136    new_string = new_string.substr(1);
1137
1138
1139    currObj.objToCheck.style.display = "none";
1140    var ifr= currObj.objToCheck;
1141    if(ifr.contentWindow)
1142        ifr=ifr.contentWindow.document;
1143    else
1144        ifr=ifr.contentDocument;
1145    var texto = ifr.body.innerHTML;
1146
1147    ifr.body.innerHTM = "";
1148    ifr.body.innerHTML = new_string;
1149    currObj.spellingResultsDiv = null;
1150
1151    currObj.objToCheck.disabled = false;
1152
1153
1154    currObj.objToCheck.style.display = "block";
1155    currObj.resetAction();
1156}; // end switchText_cb
1157
1158
1159/*************************************************************
1160 * resumeEditing
1161 *
1162 * The resumeEditing function is called when the user is in the
1163 * correction mode and wants to return to the editing mode.  It
1164 * hides the results div and the suggestions div, then enables
1165 * the text box and unhides the text box.  It also calls
1166 * resetAction() to reset the status message.
1167 *************************************************************/
1168function resumeEditing()
1169{
1170       
1171        with(currObj);
1172
1173        currObj.controlPanelDiv.style.display = "block";
1174        if(currObj.config['useIcons'])
1175        {
1176                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>";
1177        }
1178        else
1179        {
1180                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\">Continuar Editando</a>";
1181        }
1182        if(currObj.config['useIcons'])
1183        {
1184                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Carregando...\" alt=\"Carregando...\" border=\"0\" />";
1185        }
1186        else
1187        {
1188                currObj.statusSpan.innerHTML = "Carregando...";
1189        }
1190       
1191       
1192        if(spellingSuggestionsDiv)
1193        {
1194               
1195                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
1196                spellingSuggestionsDiv = null;
1197        }
1198
1199        //set the contentEditable to true for IE browsers to continue editing
1200        var iframe= currObj.objToCheck;
1201        if(iframe.contentWindow)
1202        iframe=iframe.contentWindow.document;
1203        else
1204        iframe=iframe.contentDocument;
1205
1206        var browser = checkBrowser();
1207        if (browser == "ie"){
1208            iframe.body.contentEditable="true" ;
1209       
1210        }
1211       
1212        currObj.switchText();
1213}; // end resumeEditing
1214
1215
1216/*************************************************************
1217 * resetAction
1218 *
1219 * The resetAction function just resets the status message to
1220 * the default action of "Check Spelling".
1221 *************************************************************/
1222function resetAction()
1223{
1224
1225
1226        with(currObj);
1227        /*if(currObj.config['useIcons'])
1228        {
1229                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>";
1230        }
1231        else
1232        {
1233                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\">Verificar Ortografia</a>";
1234        }*/
1235
1236        currObj.actionSpan.innerHTML = "";
1237        currObj.statusSpan.innerHTML = "";
1238        currObj.controlPanelDiv.style.display = "none";
1239
1240}; // end resetAction
1241
1242
1243/*************************************************************
1244 * resetSpellChecker
1245 *
1246 * The resetSpellChecker function resets the entire spell checker
1247 * to the defaults.
1248 *************************************************************/
1249function resetSpellChecker()
1250{
1251       
1252       
1253        var ifr= currObj.objToCheck;
1254        if(ifr.contentWindow)
1255            ifr=ifr.contentWindow.document;
1256        else
1257            ifr=ifr.contentDocument;
1258
1259
1260        with(currObj);
1261        currObj.resetAction();
1262       
1263        currObj.objToCheck.value = "";
1264        currObj.objToCheck.style.display = "block";
1265        currObj.objToCheck.disabled = false;
1266       
1267        if(currObj.spellingResultsDiv)
1268        {
1269                ifr.removeChild(currObj.spellingResultsDiv);
1270                currObj.spellingResultsDiv = null;
1271        }
1272        if(spellingSuggestionsDiv)
1273        {
1274                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
1275                spellingSuggestionsDiv = null;
1276        }
1277        currObj.statusSpan.style.display = "none";
1278       
1279}; // end resetSpellChecker
1280
1281
1282/*************************************************************
1283 * findPosX
1284 *
1285 * The findPosX function just finds the X offset of the top left
1286 * corner of the object id it's given.
1287 *
1288 * @param object The id of the object that you want to find the
1289 *               upper left X coordinate of.
1290 * @return int The X coordinate of the object
1291 *************************************************************/
1292    function findPosXById(object)
1293{
1294               
1295        var ifr= currObj.objToCheck;
1296        if(ifr.contentWindow)
1297            ifr=ifr.contentWindow.document;
1298        else
1299            ifr=ifr.contentDocument;
1300        var obj = ifr.getElementById(object);
1301
1302
1303        var curleft = 0;
1304       
1305        if(obj.offsetParent)
1306        {
1307                while(obj.offsetParent)
1308                {
1309                        curleft += obj.offsetLeft - obj.scrollLeft;
1310                        obj = obj.offsetParent;
1311                }
1312        }
1313        else if(obj.x)
1314        {
1315                curleft += obj.x;
1316        }
1317        return curleft;
1318}; // end findPosX
1319
1320
1321/*************************************************************
1322 * findPosY
1323 *
1324 * The findPosY function just finds the Y offset of the top left
1325 * corner of the object id it's given.
1326 *
1327 * @param object The id of the object that you want to find the
1328 *               upper left Y coordinate of.
1329 * @return int The Y coordinate of the object
1330 *************************************************************/
1331function findPosYById(object)
1332{     
1333        var ifr= currObj.objToCheck;
1334        if(ifr.contentWindow)
1335            ifr=ifr.contentWindow.document;
1336        else
1337            ifr=ifr.contentDocument;
1338        var obj = ifr.getElementById(object);
1339   
1340        var curtop = 0;var curtop = 0;
1341        if(obj.offsetParent)
1342        {
1343                while(obj.offsetParent)
1344                {
1345                        curtop += obj.offsetTop - obj.scrollTop;
1346                        obj = obj.offsetParent;
1347                }
1348        }
1349        else if(obj.y)
1350        {
1351                curtop += obj.y;
1352        }
1353        return curtop;
1354}; // end findPosY
1355
1356
1357/*************************************************************
1358 * trim
1359 *
1360 * Trims white space from a string.
1361 *
1362 * @param s The string you want to trim.
1363 * @return string The trimmed string.
1364 *************************************************************/
1365function trim(s)
1366{
1367       
1368        while(s.substring(0,1) == ' ')
1369        {
1370        s = s.substring(1,s.length);
1371        }
1372        while(s.substring(s.length-1,s.length) == ' ')
1373        {
1374        s = s.substring(0,s.length-1);
1375        }
1376        return s;
1377}; // end trim
Note: See TracBrowser for help on using the repository browser.