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 var FireinputEncoding = 
 38 {
 39     // debug option 
 40     debug: 0, 
 41 
 42     // pinyinEncodingTable object. 
 43     encoder: null, 
 44 
 45     // default encoding: simplified Chinese 
 46     encodingName: ENCODING_ZH, 
 47 
 48     // max chars one call can handle 
 49     maxStep: 3000, 
 50  
 51     // timer handler 
 52     stepTimer: null, 
 53 
 54     // current chars which have been encoded 
 55     currentStep: 0, 
 56    
 57     // the given string from focusedElement or focusedWindow 
 58     origString: "", 
 59 
 60     // the encoded string 
 61     encodedString: null, 
 62 
 63     // the callback option 
 64     options: null, 
 65   
 66     init: function()
 67     {
 68        if(!this.encoder)
 69           this.encoder = new PinyinEncodingTable();
 70     }, 
 71 
 72     getEncodedString: function(origStr, encoding)
 73     {
 74        var str = "";
 75        // our table is simplified based, no need to switch 
 76        try {
 77           str = FireinputUnicode.getUnicodeString(origStr); 
 78        } 
 79        catch(e) { return origStr; }
 80 
 81        if(encoding != ENCODING_BIG5)
 82           return str; 
 83 
 84        var eString = new Array(); 
 85 
 86        if(!this.encoder)
 87           this.encoder = new PinyinEncodingTable();
 88 
 89 
 90        for (var i =0; i <str.length; i++)
 91        {
 92           eString[eString.length] = this.encoder.switchToBig5(str[i]); 
 93        }
 94        return eString.join(""); 
 95     },
 96 
 97     switchToZH: function()
 98     {
 99        this.switchEncoding(ENCODING_ZH); 
100     }, 
101 
102     switchToBig5: function()
103     {
104        this.switchEncoding(ENCODING_BIG5); 
105     }, 
106 
107     switchEncoding: function(encoding)
108     {
109        var target = document.popupNode; 
110        var documentTarget = false; 
111 
112        // clean global variable before starting 
113        this.currentStep = 0; 
114        this.origString = ""; 
115        this.encodedString = new Array(); 
116        this.options = null; 
117 
118        if(Fireinput.isTargetATextBox(target))
119        {
120            this.switchEncodingAsync(target.value, encoding, 
121                                      { oncomplete: function(p) { target.value = p; }});
122            return; 
123        }
124 
125        var win = target.ownerDocument.defaultView;
126        if (win) 
127        {
128           var editingSession = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
129                                   .getInterface(Components.interfaces.nsIWebNavigation)
130                                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
131                                   .getInterface(Components.interfaces.nsIEditingSession);
132           if (!editingSession.windowIsEditable(win)) 
133              target = content.document.body; 
134 
135           this.switchEncodingAsync(target.innerHTML, encoding, 
136                                    { oncomplete: function(p) { target.innerHTML = p;}}); 
137        }
138     }, 
139 
140     switchEncodingAsync: function(str, encoding, options)
141     {
142       if(!this.encoder)
143           this.encoder = new PinyinEncodingTable();
144 
145        if(this.stepTimer)
146           clearTimeout(this.stepTimer); 
147     
148        if(typeof(str) == 'undefined')
149           str =  this.origString;
150        
151        if(typeof(encoding) != 'undefined')
152           this.encodingName = encoding;
153 
154        if(typeof(options) != 'undefined')
155           this.options = options;
156 
157        var strLength = str.length; 
158        for(var i=this.maxStep; this.currentStep<strLength && i>0; this.currentStep++)
159        {
160           var charCode = str.charAt(this.currentStep);
161           if(charCode >= '\u4e00')
162           {
163              if(this.encodingName == ENCODING_ZH)
164                 this.encodedString[this.encodedString.length] = this.encoder.switchToZH(charCode);
165              else 
166                 this.encodedString[this.encodedString.length] = this.encoder.switchToBig5(charCode);
167 
168              // control how many chars can still be translated 
169              i--; 
170           }
171           else 
172           { 
173              this.encodedString[this.encodedString.length] = charCode; 
174           }
175        }
176 
177        if(this.currentStep < strLength)
178        {
179           this.origString = str;
180           var self = this; 
181           this.stepTimer = setTimeout(function(){ self.switchEncodingAsync(); }, 500);
182        }
183        else if(this.options)
184        {
185           if(this.options.oncomplete)
186              this.options.oncomplete(this.encodedString.join(""));
187 
188           this.encodedString.length = 0; 
189        }
190     },
191 
192     validEncoding: function(charCode, encoding)
193     {
194        if(!this.encoder)
195           this.encoder = new PinyinEncodingTable();
196 
197        return this.encoder.validEncoding(charCode, encoding); 
198     }
199 
200 }


syntax highlighted by Code2HTML, v. 0.9.1