source: sandbox/2.3-MailArchiver/expressoMail1_2/spell_checker/js/spell_checker.js @ 6779

Revision 6779, 43.9 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado Expresso(branch 2.3) integrado ao MailArchiver?.

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