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

Revision 2557, 47.1 KB checked in by nathalie.silva, 14 years ago (diff)

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