import torrus 1.0.9
[freeside.git] / httemplate / elements / fckeditor / editor / dialog / fck_spellerpages / spellerpages / wordWindow.js
1 ////////////////////////////////////////////////////\r
2 // wordWindow object\r
3 ////////////////////////////////////////////////////\r
4 function wordWindow() {\r
5         // private properties\r
6         this._forms = [];\r
7 \r
8         // private methods\r
9         this._getWordObject = _getWordObject;\r
10         //this._getSpellerObject = _getSpellerObject;\r
11         this._wordInputStr = _wordInputStr;\r
12         this._adjustIndexes = _adjustIndexes;\r
13         this._isWordChar = _isWordChar;\r
14         this._lastPos = _lastPos;\r
15 \r
16         // public properties\r
17         this.wordChar = /[a-zA-Z]/;\r
18         this.windowType = "wordWindow";\r
19         this.originalSpellings = new Array();\r
20         this.suggestions = new Array();\r
21         this.checkWordBgColor = "pink";\r
22         this.normWordBgColor = "white";\r
23         this.text = "";\r
24         this.textInputs = new Array();\r
25         this.indexes = new Array();\r
26         //this.speller = this._getSpellerObject();\r
27 \r
28         // public methods\r
29         this.resetForm = resetForm;\r
30         this.totalMisspellings = totalMisspellings;\r
31         this.totalWords = totalWords;\r
32         this.totalPreviousWords = totalPreviousWords;\r
33         //this.getTextObjectArray = getTextObjectArray;\r
34         this.getTextVal = getTextVal;\r
35         this.setFocus = setFocus;\r
36         this.removeFocus = removeFocus;\r
37         this.setText = setText;\r
38         //this.getTotalWords = getTotalWords;\r
39         this.writeBody = writeBody;\r
40         this.printForHtml = printForHtml;\r
41 }\r
42 \r
43 function resetForm() {\r
44         if( this._forms ) {\r
45                 for( var i = 0; i < this._forms.length; i++ ) {\r
46                         this._forms[i].reset();\r
47                 }\r
48         }\r
49         return true;\r
50 }\r
51 \r
52 function totalMisspellings() {\r
53         var total_words = 0;\r
54         for( var i = 0; i < this.textInputs.length; i++ ) {\r
55                 total_words += this.totalWords( i );\r
56         }\r
57         return total_words;\r
58 }\r
59 \r
60 function totalWords( textIndex ) {\r
61         return this.originalSpellings[textIndex].length;\r
62 }\r
63 \r
64 function totalPreviousWords( textIndex, wordIndex ) {\r
65         var total_words = 0;\r
66         for( var i = 0; i <= textIndex; i++ ) {\r
67                 for( var j = 0; j < this.totalWords( i ); j++ ) {\r
68                         if( i == textIndex && j == wordIndex ) {\r
69                                 break;\r
70                         } else {\r
71                                 total_words++;\r
72                         }\r
73                 }\r
74         }\r
75         return total_words;\r
76 }\r
77 \r
78 //function getTextObjectArray() {\r
79 //      return this._form.elements;\r
80 //}\r
81 \r
82 function getTextVal( textIndex, wordIndex ) {\r
83         var word = this._getWordObject( textIndex, wordIndex );\r
84         if( word ) {\r
85                 return word.value;\r
86         }\r
87 }\r
88 \r
89 function setFocus( textIndex, wordIndex ) {\r
90         var word = this._getWordObject( textIndex, wordIndex );\r
91         if( word ) {\r
92                 if( word.type == "text" ) {\r
93                         word.focus();\r
94                         word.style.backgroundColor = this.checkWordBgColor;\r
95                 }\r
96         }\r
97 }\r
98 \r
99 function removeFocus( textIndex, wordIndex ) {\r
100         var word = this._getWordObject( textIndex, wordIndex );\r
101         if( word ) {\r
102                 if( word.type == "text" ) {\r
103                         word.blur();\r
104                         word.style.backgroundColor = this.normWordBgColor;\r
105                 }\r
106         }\r
107 }\r
108 \r
109 function setText( textIndex, wordIndex, newText ) {\r
110         var word = this._getWordObject( textIndex, wordIndex );\r
111         var beginStr;\r
112         var endStr;\r
113         if( word ) {\r
114                 var pos = this.indexes[textIndex][wordIndex];\r
115                 var oldText = word.value;\r
116                 // update the text given the index of the string\r
117                 beginStr = this.textInputs[textIndex].substring( 0, pos );\r
118                 endStr = this.textInputs[textIndex].substring(\r
119                         pos + oldText.length,\r
120                         this.textInputs[textIndex].length\r
121                 );\r
122                 this.textInputs[textIndex] = beginStr + newText + endStr;\r
123 \r
124                 // adjust the indexes on the stack given the differences in\r
125                 // length between the new word and old word.\r
126                 var lengthDiff = newText.length - oldText.length;\r
127                 this._adjustIndexes( textIndex, wordIndex, lengthDiff );\r
128 \r
129                 word.size = newText.length;\r
130                 word.value = newText;\r
131                 this.removeFocus( textIndex, wordIndex );\r
132         }\r
133 }\r
134 \r
135 \r
136 function writeBody() {\r
137         var d = window.document;\r
138         var is_html = false;\r
139 \r
140         d.open();\r
141 \r
142         // iterate through each text input.\r
143         for( var txtid = 0; txtid < this.textInputs.length; txtid++ ) {\r
144                 var end_idx = 0;\r
145                 var begin_idx = 0;\r
146                 d.writeln( '<form name="textInput'+txtid+'">' );\r
147                 var wordtxt = this.textInputs[txtid];\r
148                 this.indexes[txtid] = [];\r
149 \r
150                 if( wordtxt ) {\r
151                         var orig = this.originalSpellings[txtid];\r
152                         if( !orig ) break;\r
153 \r
154                         //!!! plain text, or HTML mode?\r
155                         d.writeln( '<div class="plainText">' );\r
156                         // iterate through each occurrence of a misspelled word.\r
157                         for( var i = 0; i < orig.length; i++ ) {\r
158                                 // find the position of the current misspelled word,\r
159                                 // starting at the last misspelled word.\r
160                                 // and keep looking if it's a substring of another word\r
161                                 do {\r
162                                         begin_idx = wordtxt.indexOf( orig[i], end_idx );\r
163                                         end_idx = begin_idx + orig[i].length;\r
164                                         // word not found? messed up!\r
165                                         if( begin_idx == -1 ) break;\r
166                                         // look at the characters immediately before and after\r
167                                         // the word. If they are word characters we'll keep looking.\r
168                                         var before_char = wordtxt.charAt( begin_idx - 1 );\r
169                                         var after_char = wordtxt.charAt( end_idx );\r
170                                 } while (\r
171                                         this._isWordChar( before_char )\r
172                                         || this._isWordChar( after_char )\r
173                                 );\r
174 \r
175                                 // keep track of its position in the original text.\r
176                                 this.indexes[txtid][i] = begin_idx;\r
177 \r
178                                 // write out the characters before the current misspelled word\r
179                                 for( var j = this._lastPos( txtid, i ); j < begin_idx; j++ ) {\r
180                                         // !!! html mode? make it html compatible\r
181                                         d.write( this.printForHtml( wordtxt.charAt( j )));\r
182                                 }\r
183 \r
184                                 // write out the misspelled word.\r
185                                 d.write( this._wordInputStr( orig[i] ));\r
186 \r
187                                 // if it's the last word, write out the rest of the text\r
188                                 if( i == orig.length-1 ){\r
189                                         d.write( printForHtml( wordtxt.substr( end_idx )));\r
190                                 }\r
191                         }\r
192 \r
193                         d.writeln( '</div>' );\r
194 \r
195                 }\r
196                 d.writeln( '</form>' );\r
197         }\r
198         //for ( var j = 0; j < d.forms.length; j++ ) {\r
199         //      alert( d.forms[j].name );\r
200         //      for( var k = 0; k < d.forms[j].elements.length; k++ ) {\r
201         //              alert( d.forms[j].elements[k].name + ": " + d.forms[j].elements[k].value );\r
202         //      }\r
203         //}\r
204 \r
205         // set the _forms property\r
206         this._forms = d.forms;\r
207         d.close();\r
208 }\r
209 \r
210 // return the character index in the full text after the last word we evaluated\r
211 function _lastPos( txtid, idx ) {\r
212         if( idx > 0 )\r
213                 return this.indexes[txtid][idx-1] + this.originalSpellings[txtid][idx-1].length;\r
214         else\r
215                 return 0;\r
216 }\r
217 \r
218 function printForHtml( n ) {\r
219         return n ;              // by FredCK\r
220 /*\r
221         var htmlstr = n;\r
222         if( htmlstr.length == 1 ) {\r
223                 // do simple case statement if it's just one character\r
224                 switch ( n ) {\r
225                         case "\n":\r
226                                 htmlstr = '<br/>';\r
227                                 break;\r
228                         case "<":\r
229                                 htmlstr = '&lt;';\r
230                                 break;\r
231                         case ">":\r
232                                 htmlstr = '&gt;';\r
233                                 break;\r
234                 }\r
235                 return htmlstr;\r
236         } else {\r
237                 htmlstr = htmlstr.replace( /</g, '&lt' );\r
238                 htmlstr = htmlstr.replace( />/g, '&gt' );\r
239                 htmlstr = htmlstr.replace( /\n/g, '<br/>' );\r
240                 return htmlstr;\r
241         }\r
242 */\r
243 }\r
244 \r
245 function _isWordChar( letter ) {\r
246         if( letter.search( this.wordChar ) == -1 ) {\r
247                 return false;\r
248         } else {\r
249                 return true;\r
250         }\r
251 }\r
252 \r
253 function _getWordObject( textIndex, wordIndex ) {\r
254         if( this._forms[textIndex] ) {\r
255                 if( this._forms[textIndex].elements[wordIndex] ) {\r
256                         return this._forms[textIndex].elements[wordIndex];\r
257                 }\r
258         }\r
259         return null;\r
260 }\r
261 \r
262 function _wordInputStr( word ) {\r
263         var str = '<input readonly ';\r
264         str += 'class="blend" type="text" value="' + word + '" size="' + word.length + '">';\r
265         return str;\r
266 }\r
267 \r
268 function _adjustIndexes( textIndex, wordIndex, lengthDiff ) {\r
269         for( var i = wordIndex + 1; i < this.originalSpellings[textIndex].length; i++ ) {\r
270                 this.indexes[textIndex][i] = this.indexes[textIndex][i] + lengthDiff;\r
271         }\r
272 }\r