~registry/clubdistro/anoochit-clubdistro

« back to all changes in this revision

Viewing changes to concrete5-5.2ubuntu1/concrete5.2.0RC2/concrete/js/ccm.spellchecker.js

  • Committer: Anuchit Chalothorn
  • Date: 2009-01-19 08:26:13 UTC
  • Revision ID: anoochit@gmail.com-20090119082613-jyxv9tam9ktfa73t
add concrete5 package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var SpellChecker={
 
2
 
 
3
        checkField:function(field,trigger,editingTxt){
 
4
                
 
5
                
 
6
                if(field.toString() === field) { // if it's a string, we'll consider it an id
 
7
                        var f = $('#'+field); 
 
8
                } else {
 
9
                        var f = field; // expecting jquery element
 
10
                        field = f.attr('id');
 
11
                }
 
12
                
 
13
                //determine and display mode
 
14
                if(!editingTxt) editingTxt= ccmi18n_spellchecker.resumeEditing; //'<img src="'+CCM_REL+'/images/btn_resume_editing.gif" />';
 
15
                
 
16
                //second click - switch back to edit mode 
 
17
                if( trigger.innerHTML.indexOf(ccmi18n_spellchecker.resumeEditing)>=0 ) {
 
18
                        trigger.innerHTML=trigger.initTxt;
 
19
                        f.css('display','block');
 
20
                        $('#'+field+'SuggestBox').css('display','none');
 
21
                        return false;
 
22
                }
 
23
                
 
24
                trigger.initTxt=trigger.innerHTML;
 
25
                trigger.innerHTML=editingTxt;
 
26
                
 
27
                //query spelling suggestions
 
28
                qStr='fieldId='+field+'&txt='+f.val();  
 
29
                var url=CCM_TOOLS_PATH+"/spellchecker_service.php";
 
30
                //CCM_REL+'/concrete/tools/spellchecker_service.php'
 
31
                $.ajax({type:"POST",url:url,data:qStr,
 
32
                        success: function(json){
 
33
                                eval('var jobj='+json);
 
34
                                SpellChecker.suggestMode(jobj,f);
 
35
                        }
 
36
                });
 
37
        },
 
38
 
 
39
        suggestMode:function(jobj,field){ 
 
40
                if(!jobj || !jobj.fieldId) return false;
 
41
 
 
42
                var f = field; // expecting jquery element
 
43
                field = f.attr('id');
 
44
                
 
45
                //swap text area / field with suggest box div
 
46
                var suggestId=jobj.fieldId+'SuggestBox';
 
47
                suggestBox=document.getElementById(suggestId)
 
48
                if( !suggestBox ){
 
49
                        suggestBox=document.createElement('div');
 
50
                        suggestBox.id=suggestId;
 
51
                        suggestBox.className='spellingSuggestBox';
 
52
                        suggestBox.onclick=function(){ 
 
53
                                if(this.popupShown!=true) return false;
 
54
                                var pu=$('#suggestPopup',this); 
 
55
                                if(pu.css('display')=='block'){  
 
56
                                        pu.css('display','none'); 
 
57
                                }
 
58
                                this.popupShown=false; 
 
59
                        }
 
60
                        suggestBox.style.height=f.height()+'px';
 
61
                        suggestBox.style.width=f.width()+'px';
 
62
                        suggestBox.masterRegion=f;
 
63
                        f.after(suggestBox);
 
64
                }else suggestBox.style.display='block';
 
65
                suggestBox.innerHTML=jobj.html;
 
66
                var correctedHTML=$('.correctedHTML',suggestBox);
 
67
                correctedHTML.css('height',f.height()+'px' ); 
 
68
                correctedHTML.css('width',f.width() );
 
69
                f.css('display','none');
 
70
                suggestBox.suggestions = jobj.suggestions;
 
71
                
 
72
                //add misspelled words suggestions
 
73
                $('.misspelled',suggestBox).each( function(pos,el){
 
74
                        el.wordNumber=el.id.substring(14);
 
75
                        el.onclick=function(){ SpellChecker.suggestPopup( this ) }; 
 
76
                });
 
77
        },
 
78
 
 
79
        suggestPopup:function(el){
 
80
                el.parentNode.parentNode.popupShown=false;              
 
81
                var pos=$(el).position();
 
82
                var popup=$('#suggestPopup',el.parentNode.parentNode);
 
83
                popup.css('top',pos.top+$(el).height()+2);
 
84
                popup.css('left',pos.left);
 
85
                //eval('var suggestions=el.parentNode.parentNode.suggestions.'+el.innerHTML); 
 
86
                eval('var suggestions=el.parentNode.parentNode.suggestions.word'+el.wordNumber); 
 
87
                
 
88
                var html='';
 
89
                for(var i=0;i<suggestions.length;i++){
 
90
                        if(suggestions[i].length==0) continue;
 
91
                        var params='\''+el.parentNode.parentNode.id+'\',\''+this.addSlashes(el.innerHTML)+'\',\''+this.addSlashes(suggestions[i])+'\','+parseInt(el.wordNumber); 
 
92
                        var newhtml='<div class="suggestion"><a onclick="SpellChecker.replaceWord('+(params)+')">'+suggestions[i]+"</a></div>";
 
93
                        html=html+newhtml; 
 
94
                        if(i>8) break;                  
 
95
                }
 
96
                if(html=='') html='<div>' + ccmi18n_spellchecker.noSuggestions + '</div>'; 
 
97
                popup.html(html);
 
98
                popup.css('display','block');
 
99
                setTimeout( 'document.getElementById("'+el.parentNode.parentNode.id+'").popupShown=true;',5 );
 
100
        },
 
101
 
 
102
        replaceWord:function(suggestBoxId,originalWord,newWord,wordNumber){
 
103
                var suggestBox=document.getElementById(suggestBoxId);
 
104
                var word=$('#misspelledWord'+wordNumber,suggestBox);
 
105
                word.html(newWord);
 
106
                word.addClass("fixed");
 
107
                word.click(function(){})
 
108
                var popup=$('#suggestPopup',suggestBox);
 
109
                popup.html('');
 
110
                var tarea = document.createElement('textarea');
 
111
                
 
112
                var tempDiv=document.createElement('div');
 
113
                tempDiv.innerHTML=suggestBox.innerHTML;
 
114
                $('.correctedHTML',tempDiv).each(function(i,el){ $(el).after(el.innerHTML); $(el).remove() })
 
115
                $('.misspelled',tempDiv).each(function(i,el){ $(el).after(el.innerHTML); $(el).remove() })
 
116
                $('#suggestPopup',tempDiv).each(function(i,el){ $(el).remove() })               
 
117
                tarea.innerHTML = tempDiv.innerHTML.replace(/(<(br[^>]?)>)/ig," \r\n"); 
 
118
                suggestBox.masterRegion.val( tarea.value ); 
 
119
                popup.css('display','none'); 
 
120
                suggestBox.popupShown=false;
 
121
        },
 
122
        
 
123
        addSlashes:function(str){
 
124
                return str.replace(/\'/g,"\\'");
 
125
        }
 
126
}
 
 
b'\\ No newline at end of file'