Subversion Repositories Applications.papyrus

Rev

Rev 1688 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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