1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is Fireinput Inc.
15 *
16 * Portions created by the Initial Developer are Copyright (C) 2007
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 * Olly Ja <ollyja@gmail.com>
21 *
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
33 *
34 * ***** END LICENSE BLOCK *****
35 */
36
37 const MaxSelectionLen = 30;
38
39 var FireinputLongTable =
40 {
41 debug: 0,
42
43 // to hash based on first few words
44 lookupHashTable: null,
45
46 // long table hash to avoid string duplication
47 userLongTable: null,
48
49 // user current typing long table
50 typinglongTable: null,
51
52 // is changed
53 longTableChanged: false,
54
55 init: function()
56 {
57 this.loadLongTable();
58 },
59
60 showSelection: function()
61 {
62 const showSelectionLen = 10;
63
64 var selectedText = this.getSelection();
65 if (!selectedText)
66 return null;
67
68 if (selectedText.length < 5)
69 return null;
70
71 if (selectedText.length > (showSelectionLen-1))
72 selectedText = selectedText.substr(0,showSelectionLen) + "...";
73
74 return selectedText;
75 },
76
77 getSelection: function()
78 {
79 var focusedWindow = document.commandDispatcher.focusedWindow;
80 var selection = focusedWindow.getSelection().toString();
81
82 if (selection) {
83 if (selection.length > MaxSelectionLen) {
84 var pattern = new RegExp("^(?:\\s*.){0," + MaxSelectionLen + "}");
85 pattern.test(selection);
86 selection = RegExp.lastMatch;
87 }
88 }
89 selection = selection.replace(/^\s+/, "")
90 .replace(/\s+$/, "")
91 .replace(/^#+/, "")
92 .replace(/#+$/, "")
93 .replace(/\s+/g, " ");
94
95 if (selection.length > MaxSelectionLen)
96 selection = selection.substr(0, MaxSelectionLen);
97
98
99 return selection;
100 },
101
102 hashThisKey: function(key, value)
103 {
104 if(this.lookupHashTable.hasItem(key))
105 {
106 var check = new RegExp("/^" + value + "/", "g");
107 if(!check.test(this.lookupHashTable.getItem(key)))
108 this.lookupHashTable.setItem(key, this.lookupHashTable.getItem(key) + "," + value);
109 }
110 else
111 this.lookupHashTable.setItem(key, value);
112
113 },
114
115 hashLongTableLine: function(line)
116 {
117 var wordArray = line.split(":");
118 var freq = 0;
119 if(wordArray.length > 1)
120 {
121 line = wordArray[0];
122 freq = wordArray[1];
123 }
124
125
126 line = FireinputUnicode.getUnicodeString(line);
127 if(this.userLongTable.hasItem(line))
128 return;
129
130 this.userLongTable.setItem(line, freq);
131
132 if(line.length > 10)
133 {
134 // use first few chars as hash key
135 var key1 = line.substr(0, 1);
136 var key2 = line.substr(0, 2);
137 var key3 = line.substr(0, 3);
138 this.hashThisKey(key1, line);
139 this.hashThisKey(key2, line);
140 this.hashThisKey(key3, line);
141 }
142 else
143 {
144 var key1 = line.substr(0, 2);
145 this.hashThisKey(key1, line);
146 }
147
148 },
149
150 loadLongTable: function()
151 {
152 var ios = FireinputXPC.getIOService();
153 var fileHandler = ios.getProtocolHandler("file")
154 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
155
156 var path = FireinputUtils.getAppRootPath();
157 var datafile = fileHandler.getFileFromURLSpec(path + "/userlargetable.fireinput");
158 this.lookupHashTable = new FireinputHash();
159 this.userLongTable = new FireinputHash();
160
161 if(!datafile.exists())
162 return;
163
164 var options = {
165 caller: this,
166 onavailable: this.hashLongTableLine
167 };
168 FireinputStream.loadDataAsync(datafile, options);
169
170 },
171
172 addSelectionIntoTable: function()
173 {
174 var selectedText = this.getSelection();
175 if (!selectedText)
176 return false;
177
178 this.hashLongTableLine(selectedText);
179
180 this.longTableChanged = true;
181 if(this.userLongTable.hasItem(selectedText))
182 {
183 var freq = this.userLongTable.getItem(selectedText);
184 freq += 10;
185 this.userLongTable.setItem(selectedText, freq);
186 }
187 else
188 {
189 this.userLongTable.setItem(selectedText, 10);
190 }
191
192 return true;
193 },
194
195 getLongTableByKey: function(key)
196 {
197 if(!this.lookupHashTable)
198 return null;
199
200 for(var i=key.length; i>=1; i--)
201 {
202 var skey = key.substr(0, i);
203 if(this.lookupHashTable.hasItem(skey))
204 return this.lookupHashTable.getItem(skey).split(",");
205 }
206
207 return null;
208 },
209
210 addToPanel: function()
211 {
212 var result = FireinputIMEPanel.getCharByPos(1);
213 if(!result)
214 {
215 this.hidePanel();
216 return;
217 }
218
219 var codeArray = this.getLongTableByKey(result.value);
220 if(!codeArray)
221 {
222 this.hidePanel();
223 return;
224 }
225
226 this.sendToPanel(codeArray);
227 },
228
229 hidePanel: function(start)
230 {
231 start = start || 0;
232
233 var inputPanelElement = document.getElementById('fireinputLongPanel');
234 for (var i = start; i <= 5; i++)
235 {
236 var elementId = "fireinputLongPanel_label" + (i+1) + "_hbox";
237 var element = document.getElementById(elementId);
238 if(element)
239 {
240 element.style.display = "none";
241 }
242 }
243 // nothing to display. hide this
244 if(start <= 0)
245 inputPanelElement.style.display="none";
246 },
247
248 sendToPanel: function(codeArray)
249 {
250
251 if(!codeArray || codeArray.length <= 0)
252 {
253 this.hidePanel();
254 return;
255 }
256
257 // get font size
258 var fontsize = fireinputPrefGetDefault("wordselectionFontsize");
259
260 var inputPanelElement = document.getElementById('fireinputLongPanel');
261
262 for (var i = 0; i < codeArray.length && i < 5; i++)
263 {
264 var elementId = "fireinputLongPanel_label" + (i+1);
265 if(document.getElementById(elementId + "_hbox"))
266 {
267 var element = document.getElementById(elementId);
268 element.setAttribute("value", codeArray[i]);
269 element.setAttribute("tooltiptext", "右点搜索“"+codeArray[i]+"”");
270 element.setAttribute("class", "charinputlabel");
271 elsement.style.fontSize = fontsize + "pt";
272 document.getElementById(elementId + "_hbox").style.display = "";
273 continue;
274
275 }
276
277 var hboxElement = document.createElement("hbox");
278 hboxElement.setAttribute("id", elementId + "_hbox");
279
280 var element = document.createElement("label");
281 element.setAttribute("value", "Alt+" + (i+1) + ".");
282 element.setAttribute("class", "largetablelabel");
283
284 hboxElement.appendChild(element);
285
286 element = document.createElement("label");
287 element.setAttribute("value", codeArray[i]);
288 element.setAttribute("tooltiptext", "右点搜索“"+codeArray[i]+"”");
289 element.setAttribute("class", "charinputlabel");
290 elsement.style.fontSize = fontsize + "pt";
291 element.setAttribute("id", elementId);
292 var self = this;
293 element.onclick = function(event) { self.insertCharToTargetByMouse(event);};
294 hboxElement.appendChild(element);
295 inputPanelElement.appendChild(hboxElement);
296 }
297 // show long table panel
298 inputPanelElement.style.display = "";
299 // hide all other values
300 this.hidePanel(codeArray.length);
301 },
302
303 insertCharToTargetByMouse: function(event)
304 {
305 var charstr = event.target.value;
306 if(event.button == 2)
307 {
308 FireinputWebSearch.loadByMouse(charstr);
309 return;
310 }
311
312 if(!charstr)
313 return;
314 FireinputIMEPanel.insertCharToTargetByValue(charstr);
315 FireinputIMEPanel.hideAndCleanInput();
316 if(this.userLongTable.hasItem(charstr))
317 {
318 var freq = this.userLongTable.getItem(charstr);
319 freq++;
320 this.userLongTable.setItem(charstr, freq);
321 this.longTableChanged = true;
322 }
323 },
324
325 insertCharToTarget: function(pos)
326 {
327 var elementId = "fireinputLongPanel_label" + pos;
328 var handle = document.getElementById(elementId + "_hbox");
329 if(!handle || handle.style.display == "none")
330 return;
331
332
333 handle = document.getElementById(elementId);
334 if(!handle)
335 return;
336 FireinputIMEPanel.insertCharToTargetByValue(handle.getAttribute("value"));
337 FireinputIMEPanel.hideAndCleanInput();
338 if(this.userLongTable.hasItem(handle.getAttribute("value")))
339 {
340 var freq = this.userLongTable.getItem(handle.getAttribute("value"));
341 freq++;
342 this.userLongTable.setItem(handle.getAttribute("value"), freq);
343 this.longTableChanged = true;
344 }
345
346 },
347
348 // collect long table
349 addIntoLongTable: function(target, value)
350 {
351 if(value.length <= 0)
352 return;
353 if(!this.typinglongTable)
354 {
355 this.typinglongTable = {target: target, insertTimes: 1, longTable: value};
356 }
357 else
358 {
359 if(this.typinglongTable.target == target)
360 {
361 this.typinglongTable.longTable += value;
362 this.typinglongTable.insertTimes += 1;
363 if(this.typinglongTable.longTable.length > MaxSelectionLen)
364 {
365 this.flush();
366 }
367
368 }
369 else
370 {
371 this.flush();
372 this.typinglongTable = {target: target, insertTimes: 1, longTable: value};
373 }
374 }
375 },
376
377 flush: function()
378 {
379 if(!this.typinglongTable)
380 return;
381
382 if(this.typinglongTable.longTable.length > 5 && this.typinglongTable.insertTimes > 1)
383 {
384 this.hashLongTableLine(this.typinglongTable.longTable);
385 this.longTableChanged = true;
386 if(this.userLongTable.hasItem(this.typinglongTable.longTable))
387 {
388 var freq = this.userLongTable.getItem(this.typinglongTable.longTable);
389 freq += 1;
390 this.userLongTable.setItem(this.typinglongTable.longTable, freq);
391 }
392 else
393 {
394 this.userLongTable.setItem(this.typinglongTable.longTable, 1);
395 }
396 }
397
398 this.typinglongTable = null;
399 },
400
401 flushLongTable: function()
402 {
403 if(!this.longTableChanged)
404 return;
405
406 if(this.userLongTable)
407 FireinputLongTableSaver.save(this.userLongTable);
408 }
409
410 };
syntax highlighted by Code2HTML, v. 0.9.1