1 ////////////////////////////////////////////////////
\r
3 ////////////////////////////////////////////////////
\r
4 function wordWindow() {
\r
5 // private properties
\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
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
24 this.textInputs = new Array();
\r
25 this.indexes = new Array();
\r
26 //this.speller = this._getSpellerObject();
\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
43 function resetForm() {
\r
45 for( var i = 0; i < this._forms.length; i++ ) {
\r
46 this._forms[i].reset();
\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
60 function totalWords( textIndex ) {
\r
61 return this.originalSpellings[textIndex].length;
\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
78 //function getTextObjectArray() {
\r
79 // return this._form.elements;
\r
82 function getTextVal( textIndex, wordIndex ) {
\r
83 var word = this._getWordObject( textIndex, wordIndex );
\r
89 function setFocus( textIndex, wordIndex ) {
\r
90 var word = this._getWordObject( textIndex, wordIndex );
\r
92 if( word.type == "text" ) {
\r
94 word.style.backgroundColor = this.checkWordBgColor;
\r
99 function removeFocus( textIndex, wordIndex ) {
\r
100 var word = this._getWordObject( textIndex, wordIndex );
\r
102 if( word.type == "text" ) {
\r
104 word.style.backgroundColor = this.normWordBgColor;
\r
109 function setText( textIndex, wordIndex, newText ) {
\r
110 var word = this._getWordObject( textIndex, wordIndex );
\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
122 this.textInputs[textIndex] = beginStr + newText + endStr;
\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
129 word.size = newText.length;
\r
130 word.value = newText;
\r
131 this.removeFocus( textIndex, wordIndex );
\r
136 function writeBody() {
\r
137 var d = window.document;
\r
138 var is_html = false;
\r
142 // iterate through each text input.
\r
143 for( var txtid = 0; txtid < this.textInputs.length; txtid++ ) {
\r
146 d.writeln( '<form name="textInput'+txtid+'">' );
\r
147 var wordtxt = this.textInputs[txtid];
\r
148 this.indexes[txtid] = [];
\r
151 var orig = this.originalSpellings[txtid];
\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
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
171 this._isWordChar( before_char )
\r
172 || this._isWordChar( after_char )
\r
175 // keep track of its position in the original text.
\r
176 this.indexes[txtid][i] = begin_idx;
\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
184 // write out the misspelled word.
\r
185 d.write( this._wordInputStr( orig[i] ));
\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
193 d.writeln( '</div>' );
\r
196 d.writeln( '</form>' );
\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
205 // set the _forms property
\r
206 this._forms = d.forms;
\r
210 // return the character index in the full text after the last word we evaluated
\r
211 function _lastPos( txtid, idx ) {
\r
213 return this.indexes[txtid][idx-1] + this.originalSpellings[txtid][idx-1].length;
\r
218 function printForHtml( n ) {
\r
219 return n ; // by FredCK
\r
222 if( htmlstr.length == 1 ) {
\r
223 // do simple case statement if it's just one character
\r
237 htmlstr = htmlstr.replace( /</g, '<' );
\r
238 htmlstr = htmlstr.replace( />/g, '>' );
\r
239 htmlstr = htmlstr.replace( /\n/g, '<br/>' );
\r
245 function _isWordChar( letter ) {
\r
246 if( letter.search( this.wordChar ) == -1 ) {
\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
262 function _wordInputStr( word ) {
\r
263 var str = '<input readonly ';
\r
264 str += 'class="blend" type="text" value="' + word + '" size="' + word.length + '">';
\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