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

Revision 2486, 45.4 KB checked in by nathalie.silva, 14 years ago (diff)

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