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: 1, 
 42 
 43     userLongTable: null, 
 44 
 45     // user current typing long table 
 46     typinglongTable: null, 
 47 
 48     init: function()
 49     {
 50        this.loadLongTable(); 
 51     }, 
 52 
 53     showSelection: function()
 54     {
 55        const showSelectionLen = 10; 
 56 
 57        var selectedText = this.getSelection();
 58        if (!selectedText)
 59           return null; 
 60 
 61        if (selectedText.length  < 5)
 62           return null; 
 63 
 64        if (selectedText.length > (showSelectionLen-1))
 65           selectedText = selectedText.substr(0,showSelectionLen) + "...";
 66        
 67        return selectedText; 
 68     }, 
 69 
 70     getSelection: function()
 71     {
 72        var focusedWindow = document.commandDispatcher.focusedWindow;
 73        var selection = focusedWindow.getSelection().toString();
 74 
 75        if (selection) {
 76           if (selection.length > MaxSelectionLen) {
 77           var pattern = new RegExp("^(?:\\s*.){0," + MaxSelectionLen + "}");
 78           pattern.test(selection);
 79           selection = RegExp.lastMatch;
 80           }
 81        }
 82        selection = selection.replace(/^\s+/, "")
 83                          .replace(/\s+$/, "")
 84                          .replace(/^#+/, "")
 85                          .replace(/#+$/, "")
 86                          .replace(/\s+/g, " ");
 87 
 88        if (selection.length > MaxSelectionLen)
 89           selection = selection.substr(0, MaxSelectionLen);
 90 
 91 
 92        return selection; 
 93     },
 94 
 95     hashThisKey: function(key, value)
 96     {
 97        if(this.userLongTable.hasItem(key))
 98        {
 99           var check = new RegExp("/^" + value + "/", "g");
100           if(!check.test(this.userLongTable.getItem(key)))
101              this.userLongTable.setItem(key, this.userLongTable.getItem(key) + "," + value);
102        }
103        else
104           this.userLongTable.setItem(key, value);
105          
106     }, 
107 
108     hashLongTableLine: function(line)
109     {
110        line = FireinputUnicode.getUnicodeString(line);
111        if(line.length > 10)
112        {
113           // use first few chars as hash key 
114           var key1 = line.substr(0, 1); 
115           var key2 = line.substr(0, 2); 
116           var key3 = line.substr(0, 4); 
117           this.hashThisKey(key1, line);
118           this.hashThisKey(key2, line);
119           this.hashThisKey(key3, line);
120        }
121        else
122        {
123           var key1 = line.substr(0, 2); 
124           this.hashThisKey(key1, line);
125        }
126 
127     }, 
128 
129     loadLongTable: function()
130     {
131        var ios = IOService.getService(Components.interfaces.nsIIOService);
132        var fileHandler = ios.getProtocolHandler("file")
133                          .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
134 
135        var path = FireinputUtils.getAppRootPath();
136        var datafile = fileHandler.getFileFromURLSpec(path + "/userlargetable.fireinput");
137        this.userLongTable = new FireinputHash();
138 
139        if(!datafile.exists())
140           return;
141 
142        var options = {
143           caller: this,
144           onavailable: this.hashLongTableLine
145        };
146        FireinputStream.loadDataAsync(datafile, options);
147 
148     }, 
149 
150     addSelectionIntoTable: function()
151     {
152        var selectedText = this.getSelection();
153        if (!selectedText)
154           return false;
155          
156        this.hashLongTableLine(selectedText);
157    
158        FireinputLongTableSaver.save(selectedText); 
159 
160        return true; 
161     },
162 
163     getLongTableByKey: function(key)
164     {
165        if(!this.userLongTable)
166           return null; 
167 
168        for(var i=key.length; i>=1; i--)
169        {
170           var skey = key.substr(0, i); 
171           if(this.userLongTable.hasItem(skey))
172              return this.userLongTable.getItem(skey).split(","); 
173        }
174 
175        return null; 
176     },
177 
178     addToPanel: function()
179     {
180        var result = Fireinput.getCharByPos(1); 
181        if(!result)
182        { 
183           this.hidePanel(); 
184           return; 
185        }
186 
187        var codeArray = this.getLongTableByKey(result.value); 
188        if(!codeArray)
189        {  
190           this.hidePanel(); 
191           return; 
192        }
193 
194        this.sendToPanel(codeArray); 
195     }, 
196 
197     hidePanel: function(start)
198     {
199        start = start || 0;
200 
201        var inputPanelElement = document.getElementById('fireinputLongPanel');
202        for (var i = start; i <= 5; i++)
203        {
204           var elementId = "fireinputLongPanel_label" + (i+1) + "_hbox";
205           var element = document.getElementById(elementId); 
206           if(element)
207           {
208              element.style.display = "none";
209           }
210        }
211     },
212 
213     sendToPanel: function(codeArray)
214     {
215        
216        if(!codeArray || codeArray.length <= 0)
217        {
218           this.hidePanel();
219           return;
220        }
221 
222        var inputPanelElement = document.getElementById('fireinputLongPanel');
223 
224        for (var i = 0; i < codeArray.length && i < 5; i++)
225        {
226           var elementId = "fireinputLongPanel_label" + (i+1);
227           if(document.getElementById(elementId + "_hbox"))
228           {
229               var element = document.getElementById(elementId);
230               element.setAttribute("value",  codeArray[i]);
231               element.setAttribute("tooltiptext", "右点搜索“"+codeArray[i]+"”");
232               element.setAttribute("class", "charinputlabel");
233               document.getElementById(elementId + "_hbox").style.display = "";
234               continue;
235 
236           }
237 
238           var hboxElement = document.createElement("hbox");
239           hboxElement.setAttribute("id", elementId + "_hbox");
240 
241           var element = document.createElement("label");
242           element.setAttribute("value", "Ctrl+" + (i+1) + ".");
243           element.setAttribute("class", "largetablelabel"); 
244          
245           hboxElement.appendChild(element); 
246 
247           element = document.createElement("label");
248           element.setAttribute("value",  codeArray[i]);
249           element.setAttribute("tooltiptext", "右点搜索“"+codeArray[i]+"”");
250           element.setAttribute("class", "charinputlabel");
251           element.setAttribute("id", elementId);
252           var self = this;
253           element.onclick = function(event) { self.insertCharToTargetByMouse(event);};
254           hboxElement.appendChild(element);
255           inputPanelElement.appendChild(hboxElement);
256        }
257 
258        // hide all other values 
259        this.hidePanel(codeArray.length);
260     },
261 
262     insertCharToTargetByMouse: function(event)
263     {
264        var charstr = event.target.value; 
265        if(event.button == 2)
266        {
267           FireinputWebSearch.loadByMouse(charstr);
268           return;
269        }
270 
271        if(!charstr)
272           return; 
273        Fireinput.insertCharToTargetByValue(charstr); 
274        Fireinput.hideAndCleanInput(); 
275     },
276 
277     insertCharToTarget: function(pos)
278     {
279        var elementId = "fireinputLongPanel_label" + pos;
280        var handle = document.getElementById(elementId + "_hbox"); 
281        if(!handle || handle.style.display == "none")
282           return; 
283 
284 
285        handle = document.getElementById(elementId); 
286        if(!handle)
287           return; 
288        Fireinput.insertCharToTargetByValue(handle.getAttribute("value")); 
289        Fireinput.hideAndCleanInput(); 
290     }, 
291 
292     // collect long table 
293     addIntoLongTable: function(target, value)
294     {
295        if(value.length <= 0)
296           return; 
297        if(!this.typinglongTable)
298        {
299           this.typinglongTable = {target: target, insertTimes: 1, longTable: value};
300        }
301        else
302        {
303           if(this.typinglongTable.target == target)
304           {
305              this.typinglongTable.longTable += value; 
306              this.typinglongTable.insertTimes += 1; 
307              if(this.typinglongTable.longTable.length > MaxSelectionLen)
308              {
309                 this.flushLongTable(); 
310              }
311 
312           } 
313           else 
314           {
315              this.flushLongTable(); 
316              this.typinglongTable = {target: target, insertTimes: 1, longTable: value};
317           } 
318        }
319     }, 
320  
321     flushLongTable: function()
322     {
323        if(!this.typinglongTable)
324           return; 
325 
326        if(this.typinglongTable.longTable.length > 5 && this.typinglongTable.insertTimes > 1)
327        {
328           this.hashLongTableLine(this.typinglongTable.longTable);
329           FireinputLongTableSaver.save(this.typinglongTable.longTable);
330        }        
331 
332        this.typinglongTable = null; 
333     }
334  
335 }; 


syntax highlighted by Code2HTML, v. 0.9.1