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

Revision 2508, 45.4 KB checked in by paula.franceschini, 14 years ago (diff)

Ticket #891 - alterações no spell_checker.js para corretor ortografico (IE)

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
427        // Teste de iFrame - Nathalie
428         var ifr= currObj.objToCheck;
429         if(ifr.contentWindow)
430            ifr=ifr.contentWindow.document;
431         else
432            ifr=ifr.contentDocument;
433         ifr.body.innerHTML =  "";
434         var cssLink = ifr.createElement("link")
435         cssLink.href = "spell_checker/css/spell_checker.css";
436         cssLink .rel = "stylesheet";
437         cssLink .type = "text/css";           
438              //Testes nathalie - substituido
439        currObj.spellingResultsDiv =  ifr.createElement('DIV');
440        currObj.spellingResultsDiv.className = 'edit_box';
441        currObj.spellingResultsDiv.id = 'edit_box'; //Added the id property - By Nathalie
442        currObj.spellingResultsDiv.style.width =  "99%";//the width of the textarea
443        currObj.spellingResultsDiv.style.height = 300;  //   = height; //currObj.objToCheck.style.height;
444        currObj.spellingResultsDiv.innerHTML = new_data;
445        currObj.spellingResultsDiv.border = 0;
446        //currObj.spellingResultsDiv.style.display = "none";
447        //currObj.objToCheck.style.display = "none";
448        currObj.statusSpan.innerHTML = ""; 
449       
450
451
452         ifr.body.appendChild(cssLink);
453         ifr.body.appendChild(currObj.spellingResultsDiv);
454
455
456        // Obtain all the span tags which have highlight className
457        //    and add the eventListener for the click - This event shows the suggestions menu
458        var nodeArray = ifr.getElementsByTagName("span");
459        var totArray = nodeArray.length;
460        for (var i = 0 ; i < totArray ; i++) {
461            var node = nodeArray[i];
462            if (node.className == "highlight") {
463                ifr.getElementsByTagName("span")[i].addEventListener("click", showMenu , true);
464
465            }
466        }
467
468
469//adicionado Paula
470//    currObj.objToCheck.style.display = "none"; //Paula
471        //currObj.objToCheck.parentNode.
472        //currObj.objToCheck.parentNode.appendChild(currObj.spellingResultsDiv,currObj.objToCheck);
473//      currObj.objToCheck.parentNode.insertBefore(currObj.spellingResultsDiv, document.getElementById("viewsource_rt_checkbox_" + currentTab)); //Paula
474        currObj.statusSpan.innerHTML = "";   
475
476       
477        if(currObj.config['useIcons'])
478        {
479                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>";
480        }
481        else
482        {
483                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".resumeEditing();\">Continuar Editando </a>";
484        }
485               
486        if(isThereAMisspelling != "1")
487        {
488                if(currObj.config['useIcons'])
489                {
490                        currObj.statusSpan.innerHTML = "<img src=\"images/accept.png\" width=\"16\" height=\"16\" title=\"Nenhum Erro Encontrado\" alt=\"Nenhum Erro Encontrado\" border=\"0\" />";
491                }
492                else
493                {
494                        currObj.statusSpan.innerHTML = "Nenhum Erro Encontrado";
495                }
496                currObj.objToCheck.disabled = false;
497        }
498
499       currObj.controlPanelDiv.style.display = "block";
500
501}; // end spellCheck_cb
502
503
504/*************************************************************
505 * spellCheck()
506 *
507 * The spellCheck javascript function sends the text entered by
508 * the user in the text box to php to be spell checked.  It also
509 * sets the status message to "Checking..." because it's currently
510 * checking the spelling.
511 *************************************************************/
512function spellCheck() {
513// adicionado nathalie
514/*        with(currObj);
515        var query;
516
517        if(currObj.spellingResultsDiv)
518        {
519                currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
520                currObj.spellingResultsDiv = null;
521        }
522       
523        if(currObj.config['useIcons'])
524        {
525                currObj.actionSpan.innerHTML = "<img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Verificar Ortografia\" alt=\"Verificar Ortografia\" border=\"0\" />";
526        }
527        else
528        {
529                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\">Verificar Ortografia</a>";
530        }
531       
532        if(currObj.config['useIcons'])
533        {
534                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Verificando Ortografia...\" alt=\"Verificando Ortografia...\" border=\"0\" />";
535        }
536        else
537        {
538                currObj.statusSpan.innerHTML = "Verificando Ortografia...";
539        }       
540       
541        // Obtain the HTML content from iframe (edit area)
542         var ifr= currObj.objToCheck;
543         if(ifr.contentWindow)
544            ifr=ifr.contentWindow.document;
545         else
546            ifr=ifr.contentDocument;
547         var texto = ifr.body.innerHTML;
548       
549       
550        query = texto;
551        //query = currObj.objToCheck.value;       
552       
553        //query = query.replace(/\r?\n/gi, "<br />"); //  Commented from the original by Nathalie
554        query = query.replace(/\r?\n/gi, " ");  // replace \n with " " - By Nathalie
555       
556       
557        cp.call(currObj.config['spellUrl'], 'spellCheck', spellCheck_cb, query, currObj.config['varName']);
558*/
559//adicionado Paula
560        if(!blockSpellCheck) {
561            blockSpellCheck = true;
562
563         languageId =  document.getElementById("selectLanguage");
564            languageId = languageId[document.getElementById("selectLanguage").selectedIndex].value;
565
566            with(currObj);
567            var query;
568
569            currObj.controlPanelDiv.style.display = "block";
570
571            if(currObj.spellingResultsDiv)
572            {
573                    currObj.spellingResultsDiv.parentNode.removeChild(currObj.spellingResultsDiv);
574                    currObj.spellingResultsDiv = null;
575            }
576
577            /*if(currObj.config['useIcons'])
578            {
579                    currObj.actionSpan.innerHTML = "<img src=\"images/spellcheck.png\" width=\"16\" height=\"16\" title=\"Verificar Ortografia\" alt=\"Verificar Ortografia\" border=\"0\" />";
580            }
581            else
582            {
583                    currObj.actionSpan.innerHTML = "<a class=\"check_spelling\">Verificar Ortografia</a>";
584            }*/
585
586            /*if(currObj.config['useIcons'])
587            {
588                    currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Verificando Ortografia...\" alt=\"Verificando Ortografia...\" border=\"0\" />";
589            }
590            else
591            {
592                    currObj.statusSpan.innerHTML = "Verificando Ortografia...";
593            }*/
594
595            // Obtain the HTML content from iframe (edit area)
596             var ifr= currObj.objToCheck;
597             if(ifr.contentWindow)
598                ifr=ifr.contentWindow.document;
599             else
600                ifr=ifr.contentDocument;
601             var texto = ifr.body.innerHTML;
602
603            query = texto;
604            //query = currObj.objToCheck.value;
605
606            //query = query.replace(/\r?\n/gi, "<br />"); //  Commented from the original by Nathalie
607            query = query.replace(/\r?\n/gi, " ");  // replace \n with " " - By Nathalie
608
609            cp.call(currObj.config['spellUrl'], 'spellCheck', spellCheck_cb, query, currObj.config['varName'], languageId);
610        }
611}; // end spellcheck
612
613
614
615/*************************************************************
616 * addWord
617 *
618 * The addWord function adds a word to the custom dictionary
619 * file.
620 *
621 * @param id The id of the span that contains the word to be added
622 *************************************************************/
623function addWord(id)
624{
625        var wordToAdd = document.getElementById(id).innerHTML;
626       
627        with(currObj);
628       
629        if(spellingSuggestionsDiv)
630        {
631                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
632                spellingSuggestionsDiv = null;
633        }
634
635        currObj.controlPanelDiv.style.display = "block";
636
637        if(currObj.config['useIcons'])
638        {
639                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Adding Word...\" alt=\"Adding Word...\" border=\"0\" />";
640        }
641        else
642        {
643                currObj.statusSpan.innerHTML = "Adding Word...";
644        }
645       
646        cp.call(currObj.config['spellUrl'], 'addWord', addWord_cb, wordToAdd);
647
648}; // end addWord
649
650/*************************************************************
651 * addWord_cb
652 *
653 * The addWord_cb function is a callback function that
654 * PHP's addWord function returns to.  It recieves the
655 * return status of the add to word to personal dictionary call.
656 * It hides the status item.
657 *
658 * @param returnedData The return code from PHP.
659 *************************************************************/
660function addWord_cb(returnedData)
661{
662       
663        with(currObj);
664        currObj.statusSpan.innerHTML = "";
665        resumeEditing();
666        spellCheck();
667}; // end addWord_cb
668
669
670
671/*************************************************************
672 * checkClickLocation(e)
673 *
674 * This function is called by the event listener when the user
675 * clicks on anything.  It is used to close the suggestion div
676 * if the user clicks anywhere that's not inside the suggestion
677 * div.  It just checks to see if the name of what the user clicks
678 * on is not "suggestions" then hides the div if it's not.
679 *
680 * @param e The event, in this case the user clicking somewhere on
681 *          the page.
682 *************************************************************/
683function checkClickLocation(e)
684{
685   
686    if(spellingSuggestionsDiv)
687        {
688                // Bah.  There's got to be a better way to deal with this, but the click
689                // on a word to get suggestions starts up a race condition between
690                // showing and hiding the suggestion box, so we'll ignore the first
691                // click.
692                if(spellingSuggestionsDiv.ignoreNextClick){
693                        spellingSuggestionsDiv.ignoreNextClick = false;
694                }
695                else
696                {
697                        var theTarget = getTarget(e);
698                       
699                        if(theTarget != spellingSuggestionsDiv)
700                        {
701                                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
702                                spellingSuggestionsDiv = null;
703                        }
704                }
705        }
706       
707        return true; // Allow other handlers to continue.
708}; //end checkClickLocation
709
710
711/*************************************************************
712 * getTarget
713 *
714 * The get target function gets the correct target of the event.
715 * This function is required because IE handles the events in
716 * a different (wrong) manner than the rest of the browsers.
717 *
718 * @param e The target, in this case the user clicking somewhere on
719 *     the page.
720 *
721 *************************************************************/
722function getTarget(e)
723{
724       
725        var value;
726        if(checkBrowser() == "ie")
727        {
728                value = window.event.srcElement;
729        }
730        else
731        {
732                value = e.target;
733        }
734        return value;
735}; //end getTarget
736
737
738/*************************************************************
739 * checkBrowser()
740 *
741 * The checkBrowser function simply checks to see what browser
742 * the user is using and returns a string containing the browser
743 * type.
744 *
745 * @return string The browser type
746 *************************************************************/
747function checkBrowser()
748{
749        var theAgent = navigator.userAgent.toLowerCase();
750        if(theAgent.indexOf("msie") != -1)
751        {
752                if(theAgent.indexOf("opera") != -1)
753                {
754                        return "opera";
755                }
756                else
757                {
758                        return "ie";
759                }
760        }
761        else if(theAgent.indexOf("netscape") != -1)
762        {
763                return "netscape";
764        }
765        else if(theAgent.indexOf("firefox") != -1)
766        {
767                return "firefox";
768        }
769        else if(theAgent.indexOf("mozilla/5.0") != -1)
770        {
771                return "mozilla";
772        }
773        else if(theAgent.indexOf("\/") != -1)
774        {
775                if(theAgent.substr(0,theAgent.indexOf('\/')) != 'mozilla')
776                {
777                        return navigator.userAgent.substr(0,theAgent.indexOf('\/'));
778                }
779                else
780                {
781                        return "netscape";
782                }
783        }
784        else if(theAgent.indexOf(' ') != -1)
785        {
786                return navigator.userAgent.substr(0,theAgent.indexOf(' '));
787        }
788        else
789        {
790                return navigator.userAgent;
791        }
792}; // end checkBrowser
793
794
795/*************************************************************
796 * showSuggestions_cb
797 *
798 * The showSuggestions_cb function is a callback function that
799 * php's showSuggestions function returns to.  It sets the
800 * suggestions table to contain the new data and then displays
801 * the suggestions div.  It also clears the status message.
802 *
803 * @param new_data The suggestions table returned from php.
804 *************************************************************/
805function showSuggestions_cb(new_data)
806{
807       
808        with(currObj);
809        spellingSuggestionsDiv.innerHTML = new_data;
810        spellingSuggestionsDiv.style.display = 'block';
811        currObj.statusSpan.innerHTML = "";
812
813        //adicionado os listener - nathalie
814         var ifr= currObj.objToCheck;
815         if(ifr.contentWindow)
816            ifr=ifr.contentWindow.document;
817         else
818            ifr=ifr.contentDocument;
819
820        var nodeArray = ifr.getElementsByTagName("div");
821        var totArray = nodeArray.length;
822        for (var i = 0 ; i < totArray ; i++) {
823            var node = nodeArray[i];
824            if (node.className == "suggestion" || node.className == "ignore" ) {
825                ifr.getElementsByTagName("div")[i].addEventListener("click", replaceMenu , true);
826
827            }
828        }
829
830
831}; //end showSuggestions_cb
832
833
834/*************************************************************
835 * showSuggestions
836 *
837 * The showSuggestions function calls the showSuggestions php
838 * function to get suggestions for the misspelled word that the
839 * user has clicked on.  It sets the status to "Searching...",
840 * hides the suggestions div, finds the x and y position of the
841 * span containing the misspelled word that user clicked on so
842 * the div can be displayed in the correct location, and then
843 * calls the showSuggestions php function with the misspelled word
844 * and the id of the span containing it.
845 *
846 * @param word The misspelled word that the user clicked on
847 * @param id The id of the span that contains the misspelled word
848 *************************************************************/
849function showSuggestions(word, id)
850{
851       
852        //adicionado nathalie
853         var ifr= currObj.objToCheck;
854         if(ifr.contentWindow)
855            ifr=ifr.contentWindow.document;
856         else
857            ifr=ifr.contentDocument;
858
859//alterado - colocar ifr
860        languageId =  document.getElementById("selectLanguage");
861        languageId = languageId[document.getElementById("selectLanguage").selectedIndex].value;
862       
863     
864        with(currObj);
865        if(currObj.config['useIcons'])
866        {
867                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Procurando...\" alt=\"Procurando...\" border=\"0\" />";
868        }
869        else
870        {
871                currObj.statusSpan.innerHTML = "Procurando...";
872        }
873       
874        var x = findPosXById(id);
875        var y = findPosYById(id);     
876       
877        var scrollPos = 0;
878        if(checkBrowser() != "ie")
879        {
880               
881                //scrollPos = currObj.spellingResultsDiv.scrollTop;
882                scrollPos = ifr.getElementById(currObj.spellingResultsDiv.id).scrollTop;
883               
884        }
885
886        if(spellingSuggestionsDiv)
887        {
888                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
889        }
890        spellingSuggestionsDiv = document.createElement('DIV');
891        spellingSuggestionsDiv.style.display = "none";
892        spellingSuggestionsDiv.className = 'suggestion_box';
893        spellingSuggestionsDiv.style.position = 'absolute';
894        spellingSuggestionsDiv.style.left = x + 'px';
895        spellingSuggestionsDiv.style.top = (y+16-scrollPos) + 'px';
896       
897       
898               
899       
900        // Bah. There's got to be a better way to deal with this, but the click
901        // on a word to get suggestions starts up a race condition between
902        // showing and hiding the suggestion box, so we'll ignore the first
903        // click.
904        //spellingSuggestionsDiv.ignoreNextClick = true; // comentado Nathalie
905
906       
907        //document.body.appendChild(spellingSuggestionsDiv);
908        ifr.body.appendChild(spellingSuggestionsDiv); //alterado nathalie
909       
910
911
912
913       
914        cp.call(currObj.config['spellUrl'], 'showSuggestions', showSuggestions_cb, word, id, languageId);
915}; // end showSuggestions
916
917
918
919/*************************************************************
920 * replaceWord
921 *
922 * The replaceWord function replaces the innerHTML of all the span tags
923 * that contains the old word with the new word that the user selects
924 * from the suggestion div.  It hides the suggestions div and changes the color of
925 * the previously misspelled word to green to let the user know
926 * it has been changed.  It then calls the switchText php function
927 * with the innerHTML of the div to update the text of the text box.
928 *
929 * @param id The id of the element to be checked
930 * @param newword The word the user selected from the suggestions div
931 *                to replace the misspelled word.
932 *************************************************************/
933function replaceWord(id, newWord)
934{
935
936   
937    var ifr= currObj.objToCheck;
938    if(ifr.contentWindow)
939        ifr=ifr.contentWindow.document;
940    else
941        ifr=ifr.contentDocument;
942
943    var valorNo = trim(ifr.getElementById(id).innerHTML);
944   
945
946    if(spellingSuggestionsDiv)
947    {
948    spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
949    spellingSuggestionsDiv = null;
950    }
951
952
953    // Obtain all span tags which have highlight className and contais the old word
954    var nodeArray = ifr.getElementsByTagName("span");
955    var totArray = nodeArray.length;
956    var nodeRemove = new Array(totArray);
957    var j = -1;
958    for (var i = 0 ; i < totArray ; i++) {
959        var node = nodeArray[i];
960        if (node.className == "highlight" && trim(node.innerHTML) == valorNo ) {
961            j++;
962            nodeRemove[j] = node.id;
963        }
964    }
965
966    //Replace the class of the span tags with highlight and the innerHTML with the new_word
967    for (var i = 0 ; i <= j ; i++) {
968        ifr.getElementById(nodeRemove[i]).innerHTML = newWord;
969        ifr.getElementById(nodeRemove[i]).className = "corrected_word";
970    }
971
972
973    return false;
974
975
976// FIMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
977
978         //Função antiga - substitui apenas uma palavra
979   /*       var ifr= currObj.objToCheck;
980         if(ifr.contentWindow)
981            ifr=ifr.contentWindow.document;
982         else
983            ifr=ifr.contentDocument;
984
985        var valorNo = trim(newWord);       
986        if(spellingSuggestionsDiv)
987        {
988
989                spellingSuggestionsDiv.style.display = 'none';
990                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
991                spellingSuggestionsDiv = null;
992        }
993       
994        var fake = document.createTextNode(valorNo);
995       
996       var parent =  ifr.getElementById(id).parentNode;
997
998           
999       parent.replaceChild(fake, ifr.getElementById(id));
1000
1001        return false;*/
1002
1003           
1004
1005
1006}; // end replaceWord
1007
1008
1009/*************************************************************
1010 * switchText
1011 *
1012 * The switchText function is a funtion is called when the user
1013 * clicks on resume editing (or submits the form).  It calls the
1014 * php function to switchText and uncomments the html and replaces
1015 * breaks and everything.  Here all the breaks that the user has
1016 * typed are replaced with %u2026.  Firefox does this goofy thing
1017 * where it cleans up the display of your html, which adds in \n's
1018 * where you don't want them.  So I replace the user-entered returns
1019 * with something unique so that I can rip out all the breaks that
1020 * the browser might add and we don't want.
1021 *************************************************************/
1022function switchText()
1023{
1024       
1025        with(currObj);
1026        //By Nathalie - The Element means that this div belongs to the current tab
1027        //var parentElement = Element(currObj.spellingResultsDiv.id); //alterado nathalie
1028        var ifr= currObj.objToCheck;
1029        if(ifr.contentWindow)
1030            ifr=ifr.contentWindow.document;
1031        else
1032            ifr=ifr.contentDocument;
1033        var parentElement =ifr.getElementById(currObj.spellingResultsDiv.id); //alterado nathalie
1034        // Obtain all span tags which have highlight className or corrected_word className
1035        var nodeArray = ifr.getElementsByTagName("span");
1036        var totArray = nodeArray.length;
1037        var nodeRemove = new Array(totArray);
1038        var j = -1;
1039
1040        for (var i = 0 ; i < totArray ; i++) {
1041            var node = nodeArray[i];
1042            if (node.className == "highlight" || node.className == "corrected_word") {
1043                j++;
1044                nodeRemove[j] = node.id;
1045               
1046               
1047            }
1048        }
1049
1050        //Remove span tags which have highlight className or corrected_word className
1051        for (var i = 0 ; i <= j ; i++) {
1052            var valorNo = ifr.getElementById(nodeRemove[i]).innerHTML;
1053            var fake = document.createTextNode(valorNo);
1054            var parent =  ifr.getElementById(nodeRemove[i]).parentNode;
1055            parent.replaceChild(fake, ifr.getElementById(nodeRemove[i]));
1056        }
1057
1058        var text = ifr.getElementById(currObj.spellingResultsDiv.id).innerHTML;
1059
1060        text = text.replace(/&nbsp;/gi, " %u2026 "); // Replace &nbsp; with the code %u2026
1061        text = '*' + text;
1062
1063        cp.call(currObj.config['spellUrl'], 'switchText', switchText_cb, text);
1064       
1065}; // end switchText
1066
1067
1068/*************************************************************
1069 * switchText_cb
1070 *
1071 * The switchText_cb function is a call back funtion that the
1072 * switchText php function returns to.  I replace all the %u2026's
1073 * with returns.  It then replaces the text in the text box with
1074 * the corrected text fromt he div.
1075 *
1076 * @param new_string The corrected text from the div.
1077 *
1078 *************************************************************/
1079function switchText_cb(new_string)
1080{
1081
1082
1083//adicionado Paula
1084    with(currObj);
1085
1086        new_string = new_string.replace(/ %u2026/gi, "&nbsp;"); // Replace the code %u2026 with &nbsp;
1087             
1088        new_string = new_string.replace(/~~~/gi, "\n");
1089       
1090        // Remove the prefixed asterisk that was added in switchText().
1091        new_string = new_string.substr(1);
1092
1093
1094         currObj.objToCheck.style.display = "none";
1095         var ifr= currObj.objToCheck;
1096         if(ifr.contentWindow)
1097            ifr=ifr.contentWindow.document;
1098         else
1099            ifr=ifr.contentDocument;
1100         var texto = ifr.body.innerHTML;
1101
1102         ifr.body.innerHTM = ""; //adicionado nathalie
1103         ifr.body.innerHTML = new_string;
1104         currObj.spellingResultsDiv = null; //adicionado nathalie
1105
1106         
1107
1108        currObj.objToCheck.disabled = false;
1109       
1110        //Removido Nathalie
1111        /*if(currObj.spellingResultsDiv)
1112        {
1113                ifr.removeChild(currObj.spellingResultsDiv); //Alterado nathalie
1114                currObj.spellingResultsDiv = null;
1115        }*/
1116        currObj.objToCheck.style.display = "block";
1117        currObj.resetAction();
1118}; // end switchText_cb
1119
1120
1121/*************************************************************
1122 * resumeEditing
1123 *
1124 * The resumeEditing function is called when the user is in the
1125 * correction mode and wants to return to the editing mode.  It
1126 * hides the results div and the suggestions div, then enables
1127 * the text box and unhides the text box.  It also calls
1128 * resetAction() to reset the status message.
1129 *************************************************************/
1130function resumeEditing()
1131{
1132       
1133        with(currObj);
1134
1135        currObj.controlPanelDiv.style.display = "block";
1136        if(currObj.config['useIcons'])
1137        {
1138                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>";
1139        }
1140        else
1141        {
1142                currObj.actionSpan.innerHTML = "<a class=\"resume_editing\">Continuar Editando</a>";
1143        }
1144        if(currObj.config['useIcons'])
1145        {
1146                currObj.statusSpan.innerHTML = "<img src=\"images/working.gif\" width=\"16\" height=\"16\" title=\"Carregando...\" alt=\"Carregando...\" border=\"0\" />";
1147        }
1148        else
1149        {
1150                currObj.statusSpan.innerHTML = "Carregando...";
1151        }
1152       
1153       
1154        if(spellingSuggestionsDiv)
1155        {
1156               
1157                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
1158                spellingSuggestionsDiv = null;
1159        }
1160       
1161        currObj.switchText();
1162}; // end resumeEditing
1163
1164
1165/*************************************************************
1166 * resetAction
1167 *
1168 * The resetAction function just resets the status message to
1169 * the default action of "Check Spelling".
1170 *************************************************************/
1171function resetAction()
1172{
1173
1174//adicionado Paula
1175        with(currObj);
1176        /*if(currObj.config['useIcons'])
1177        {
1178                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>";
1179        }
1180        else
1181        {
1182                currObj.actionSpan.innerHTML = "<a class=\"check_spelling\" onclick=\"setCurrentObject(" + currObj.config['varName'] + "); " + currObj.config['varName'] + ".spellCheck();\">Verificar Ortografia</a>";
1183        }*/
1184
1185        currObj.actionSpan.innerHTML = "";
1186        currObj.statusSpan.innerHTML = "";
1187        currObj.controlPanelDiv.style.display = "none";
1188        blockSpellCheck = false;
1189}; // end resetAction
1190
1191
1192/*************************************************************
1193 * resetSpellChecker
1194 *
1195 * The resetSpellChecker function resets the entire spell checker
1196 * to the defaults.
1197 *************************************************************/
1198function resetSpellChecker()
1199{
1200       
1201        //adicionado nathalie
1202        var ifr= currObj.objToCheck;
1203         if(ifr.contentWindow)
1204            ifr=ifr.contentWindow.document;
1205         else
1206            ifr=ifr.contentDocument;
1207
1208
1209        with(currObj);
1210        currObj.resetAction();
1211       
1212        currObj.objToCheck.value = "";
1213        currObj.objToCheck.style.display = "block";
1214        currObj.objToCheck.disabled = false;
1215       
1216        if(currObj.spellingResultsDiv)
1217        {
1218                ifr.removeChild(currObj.spellingResultsDiv); //alterado nathalie
1219                currObj.spellingResultsDiv = null;
1220        }
1221        if(spellingSuggestionsDiv)
1222        {
1223                spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);
1224                spellingSuggestionsDiv = null;
1225        }
1226        currObj.statusSpan.style.display = "none";
1227       
1228}; // end resetSpellChecker
1229
1230
1231/*************************************************************
1232 * findPosX
1233 *
1234 * The findPosX function just finds the X offset of the top left
1235 * corner of the object id it's given.
1236 *
1237 * @param object The id of the object that you want to find the
1238 *               upper left X coordinate of.
1239 * @return int The X coordinate of the object
1240 *************************************************************/
1241    function findPosXById(object)
1242{
1243       
1244        //adicionado Nathalie
1245        var ifr= currObj.objToCheck;
1246         if(ifr.contentWindow)
1247            ifr=ifr.contentWindow.document;
1248         else
1249            ifr=ifr.contentDocument;
1250        var obj = ifr.getElementById(object);
1251
1252
1253        var curleft = 0;
1254        //var obj = document.getElementById(object); //removido nathalie
1255
1256
1257        if(obj.offsetParent)
1258        {
1259                while(obj.offsetParent)
1260                {
1261                        curleft += obj.offsetLeft - obj.scrollLeft;
1262                        obj = obj.offsetParent;
1263                }
1264        }
1265        else if(obj.x)
1266        {
1267                curleft += obj.x;
1268        }
1269        return curleft;
1270}; // end findPosX
1271
1272
1273/*************************************************************
1274 * findPosY
1275 *
1276 * The findPosY function just finds the Y offset of the top left
1277 * corner of the object id it's given.
1278 *
1279 * @param object The id of the object that you want to find the
1280 *               upper left Y coordinate of.
1281 * @return int The Y coordinate of the object
1282 *************************************************************/
1283function findPosYById(object)
1284{
1285       
1286        //adicionado Nathalie
1287        var ifr= currObj.objToCheck;
1288         if(ifr.contentWindow)
1289            ifr=ifr.contentWindow.document;
1290         else
1291            ifr=ifr.contentDocument;
1292        var obj = ifr.getElementById(object); //removido Nathalie
1293
1294   
1295        var curtop = 0;var curtop = 0;
1296        //var obj = document.getElementById(object); //removido Nathalie
1297        if(obj.offsetParent)
1298        {
1299                while(obj.offsetParent)
1300                {
1301                        curtop += obj.offsetTop - obj.scrollTop;
1302                        obj = obj.offsetParent;
1303                }
1304        }
1305        else if(obj.y)
1306        {
1307                curtop += obj.y;
1308        }
1309        return curtop;
1310}; // end findPosY
1311
1312
1313/*************************************************************
1314 * trim
1315 *
1316 * Trims white space from a string.
1317 *
1318 * @param s The string you want to trim.
1319 * @return string The trimmed string.
1320 *************************************************************/
1321function trim(s)
1322{
1323       
1324        while(s.substring(0,1) == ' ')
1325        {
1326        s = s.substring(1,s.length);
1327        }
1328        while(s.substring(s.length-1,s.length) == ' ')
1329        {
1330        s = s.substring(0,s.length-1);
1331        }
1332        return s;
1333}; // end trim
Note: See TracBrowser for help on using the repository browser.