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 const monthNumber =["零","一,正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二,腊"]; 
 37 const weekNumber = ["日,天", "一", "二", "三", "四", "五", "六"]; 
 38 
 39 
 40 const chineseBigNumber =  ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"]; 
 41 
 42 const symbolGroup  = ["\u2010","\u2013","\u2014","\u2015","\u2016","\u2018","\u2019","\u201C",
 43                       "\u201D","\u2025","\u2026","\u2030","\u2032","\u2033","\u2035","\u203B",
 44                       "\u20AC","\u2103","\u2105","\u2109","\u2116","\u2121","\u3001","\u3002",
 45                       "\u3003","\u3005","\u3006","\u3007","\u3008","\u3009","\u300A","\u300B",
 46                       "\u300C","\u300D","\u300E","\u300F","\u3010","\u3011","\u3012","\u3013",
 47                       "\u3014","\u3015","\u3016","\u3017","\u301D","\u301E","\u3021","\u3022",
 48                       "\u3023","\u3024","\u3025","\u3026","\u3027","\u3028","\u3029","\u303E",
 49                       "\uFE30","\uFE31","\uFE33","\uFE34","\uFE35","\uFE36","\uFE37","\uFE38",
 50                       "\uFE39","\uFE3A","\uFE3B","\uFE3C","\uFE3D","\uFE3E","\uFE3F","\uFE40",
 51                       "\uFE41","\uFE42","\uFE43","\uFE44","\uFE49","\uFE4A","\uFE4B","\uFE4C",
 52                       "\uFE4D","\uFE4E","\uFE4F","\uFE50","\uFE51","\uFE52","\uFE54","\uFE55",
 53                       "\uFE56","\uFE57","\uFE59","\uFE5A","\uFE5B","\uFE5C","\uFE5D","\uFE5E",
 54                       "\uFE5F","\uFE60","\uFE61","\uFE62","\uFE63","\uFE64","\uFE65","\uFE66",
 55                       "\uFE68","\uFE69","\uFE6A","\uFE6B","\uFF01","\uFF02","\uFF03","\uFF04",
 56                       "\uFF05","\uFF06","\uFF07","\uFF08","\uFF09","\uFF0A","\uFF0B","\uFF0C",
 57                       "\uFF0D","\uFF0E","\uFF0F","\uFF1A","\uFF1B","\uFF1C","\uFF1D","\uFF1E",
 58                       "\uFF1F","\uFF20","\uFF3B","\uFF3C","\uFF3D","\uFF3E","\uFF3F","\uFF40",
 59                       "\uFF5B","\uFF5C","\uFF5D","\uFF5E","\uFFE0","\uFFE1","\uFFE2","\uFFE3",
 60                       "\uFFE4","\uFFE5","\u00A7","\u00A8","\u00B0","\u00B1","\u00B7","\u00D7",
 61                       "\u00F7","\u3231","\u00AE","\u00BF","\u00BA","\u00BB"]; 
 62 
 63 const specialNumber = ["\u2160","\u2161","\u2162","\u2163","\u2164","\u2165","\u2166","\u2167",
 64                        "\u2168","\u2169","\u216A","\u216B","\u2170","\u2171","\u2172","\u2173",
 65                        "\u2174","\u2175","\u2176","\u2177","\u2178","\u2179","\u2460","\u2461",
 66                        "\u2462","\u2463","\u2464","\u2465","\u2466","\u2467","\u2468","\u2469",
 67                        "\u2474","\u2475","\u2476","\u2477","\u2478","\u2479","\u247A","\u247B",
 68                        "\u247C","\u247D","\u247E","\u247F","\u2480","\u2481","\u2482","\u2483",
 69                        "\u2484","\u2485","\u2486","\u2487","\u2488","\u2489","\u248A","\u248B",
 70                        "\u248C","\u248D","\u248E","\u248F","\u2490","\u2491","\u2492","\u2493",
 71                        "\u2494","\u2495","\u2496","\u2497","\u2498","\u2499","\u249A","\u249B",
 72                        "\u3220","\u3221","\u3222","\u3223","\u3224","\u3225","\u3226","\u3227",
 73                        "\u3228","\u3229","\u00BC","\u00BD","\u00BE"]; 
 74 
 75 const currencySymbol= ["\u5143","\u00A3","\u20AC","\uFF04","\uFFE0","\uFFE1","\uFFE5"]; 
 76 
 77 const arrowSymbol =   ["\u2190","\u2191","\u2192","\u2193","\u2194","\u2195","\u2196","\u2197"]; 
 78 
 79 const mathSymbol =    ["\u2200","\u2202","\u2203","\u2205","\u2206","\u2207","\u2208","\u2209",
 80                        "\u220B","\u220F","\u2211","\u2212","\u2215","\u2217","\u2219","\u221A",
 81                        "\u221D","\u221E","\u221F","\u2220","\u2223","\u2225","\u2227","\u2228",
 82                        "\u2229","\u222A","\u222B","\u222C","\u222E","\u2234","\u2235","\u2236",
 83                        "\u2237","\u223C","\u223D","\u2245","\u2248","\u224C","\u2252","\u2260",
 84                        "\u2261","\u2264","\u2265","\u2266","\u2267","\u226A","\u226B","\u226E",
 85                        "\u226F","\u2282","\u2283","\u2284","\u2286","\u2287","\u2295","\u2297",
 86                        "\u2299","\u22A5","\u22BF","\u22C5","\u2312","\u2320","\u2321","\u2329",
 87                        "\u232A","\u33D1","\u33D2","\u00B1","\u00D7","\u00F7"]; 
 88 
 89 const latinSymbol =   ["\u0101","\u00E1","\u0103","\u00E0","\u00E8","\u00E9","\u00EA","\u00EC",
 90                        "\u00ED","\u00F2","\u00F3","\u00F9","\u00FA","\u00FC","\u0101","\u0113",
 91                        "\u011B","\u012B","\u0144","\u0148","\u014D","\u016B","\u01CE","\u01D0",
 92                        "\u01D2","\u01D4","\u01D6","\u01D8","\u01DA","\u01DC","\u01F9","\u0251",
 93                        "\u0261"]; 
 94 
 95 const greekSymbol =   ["\u0391","\u0392","\u0393","\u0394","\u0395","\u0396","\u0397","\u0398",
 96                        "\u0399","\u039A","\u039B","\u039C","\u039D","\u039E","\u039F","\u03A0",
 97                        "\u03A1","\u03A3","\u03A4","\u03A5","\u03A6","\u03A7","\u03A8","\u03A9",
 98                        "\u03B1","\u03B2","\u03B3","\u03B4","\u03B5","\u03B6","\u03B7","\u03B8", 
 99                        "\u03B9","\u03BA","\u03BB","\u03BC","\u03BD","\u03BE","\u03BF","\u03C0", 
100                        "\u03C1","\u03C2","\u03C3","\u03C4","\u03C5","\u03C6","\u03C7","\u03C8", 
101                        "\u03C9","\u03D1","\u03D5"]; 
102 
103 const tableSymbol =   ["\u2500","\u2501","\u2502","\u2503","\u2504","\u2505","\u2506","\u2507",
104                        "\u2508","\u2509","\u250A","\u250B","\u250C","\u250D","\u250E","\u250F",
105                        "\u2510","\u2511","\u2512","\u2513","\u2514","\u2515","\u2516","\u2517",
106                        "\u2518","\u2519","\u251A","\u251B","\u251C","\u251D","\u251E","\u251F",
107                        "\u2520","\u2521","\u2522","\u2523","\u2524","\u2525","\u2526","\u2527",
108                        "\u2528","\u2529","\u252A","\u252B","\u252C","\u252D","\u252E","\u252F",
109                        "\u2530","\u2531","\u2532","\u2533","\u2534","\u2535","\u2536","\u2537",
110                        "\u2538","\u2539","\u253A","\u253B","\u253C","\u253D","\u253E","\u253F",
111                        "\u2540","\u2541","\u2542","\u2543","\u2544","\u2545","\u2546","\u2547",
112                        "\u2548","\u2549","\u254A","\u254B","\u2550","\u2551","\u2552","\u2553",
113                        "\u2554","\u2555","\u2556","\u2557","\u2558","\u2559","\u255A","\u255B",
114                        "\u255C","\u255D","\u255E","\u255F","\u2560","\u2561","\u2562","\u2563",
115                        "\u2564","\u2565","\u2566","\u2567","\u2568","\u2569","\u256A","\u256B",
116                        "\u256C","\u256D","\u256E","\u256F","\u2570","\u2571","\u2572","\u2573"]; 
117 
118 const blockSymbol =   ["\u2581","\u2582","\u2583","\u2584","\u2585","\u2586","\u2587","\u2588",
119                        "\u2589","\u258A","\u258B","\u258C","\u258D","\u258E","\u258F","\u2593",
120                        "\u2594","\u2595"]; 
121 
122 const geometrySymbol =    ["\u25A0","\u25A1","\u25B2","\u25B3","\u25BC","\u25BD","\u25E2","\u25E3",
123                        "\u25E4","\u25E5","\u25C6","\u25C7","\u25CB","\u25CE","\u25CF","\u2605",
124                        "\u2606","\u2609","\u2640","\u2641","\u2642"]; 
125 
126 const sideSymbol =    ["\u2E81","\u2E84","\u2E88","\u2E8B","\u2E8C","\u2E97","\u2EA7","\u2EAA",
127                        "\u2EAE","\u2EB3","\u2EB6","\u2EB7","\u2EBB","\u2ECA"]; 
128 
129 const measureSymbol=  ["\u338E","\u338F","\u339C","\u339D","\u339E","\u33A1","\u33C4","\u33CE","\u33D5"]; 
130 
131 const pinyinSymbol =  ["\u0101","\u00E1","\u01CE","\u00E0","\u0100","\u00C1","\u01CD","\u00C0","\u0113",
132                        "\u00E9","\u011B","\u00E8","\u0112","\u00C9","\u011A","\u00C8","\u012B","\u00ED",
133                        "\u01D0","\u00EC","\u012A","\u00CD","\u01CF","\u00CC","\u014D","\u00F3","\u01D2",
134                        "\u00F2", "\u014C","\u00D3","\u01D1","\u00D2","\u016B","\u00FA","\u01D4","\u00F9",
135                        "\u016A","\u00DA","\u01D3","\u00D9","\u01D6","\u01D8","\u01DA","\u01DC","\u00FC",
136                        "\u01D5","\u01D7","\u01D9","\u01DB","\u00DC"]; 
137 
138 const allSymbols = [ 
139                      {group: "fireinput.measure.label", key: "danwei", symbol: measureSymbol},
140                      {group: "fireinput.geometry.label", key: "jihe", symbol: geometrySymbol},
141                      {group: "fireinput.block.label", key: "tu'an", symbol: blockSymbol},
142                      {group: "fireinput.table.label", key: "zhibiao", symbol: tableSymbol},
143                      {group: "fireinput.greek.label", key: "greek,xila", symbol: greekSymbol},
144                      {group: "fireinput.latin.label", key: "latin", symbol: latinSymbol},
145                      {group: "fireinput.math.label", key: "shuxue", symbol: mathSymbol},
146                      {group: "fireinput.arrow.label", key: "jiantou", symbol: arrowSymbol},
147                      {group: "fireinput.currency.label", key: "huobi", symbol: currencySymbol},
148                      {group: "fireinput.special.number.label", key: "shuzi", symbol: specialNumber},
149                      {group: "fireinput.normal.symbol.label", key: "fuhao", symbol: symbolGroup},
150                      {group: "fireinput.chinese.number.label", key: "shuzi", symbol: chineseBigNumber}
151                 ]; 
152 
153 
154 // short key for date, week, time 
155 const iMode = [
156                 {key: 'rq', word: { fn: 'FireinputSpecialChar.getMonth'} },
157                 {key: 'sj', word: { fn: 'FireinputSpecialChar.getTime' } },
158                 {key: 'xq', word: { fn: 'FireinputSpecialChar.getWeek'} }
159 ]; 
160 
161 
162 var FireinputSpecialChar = 
163 {
164     initialized: false, 
165 
166     load: function(forceLoad)
167     {
168        if(!this.initialized || forceLoad)
169        {
170           var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
171           var element = document.getElementById("fireinputSpecialCharMenu"); 
172           var label = FireinputUtils.getLocaleString("fireinput.special.char.label" + defaultLanguage);
173           element.setAttribute("label", label);
174           this.initialized = true;
175 
176           this.addGroup(document.getElementById("fireinputSpecialCharMenuItems")); 
177        }
178     }, 
179 
180     refreshMenu: function()
181     {
182        // get default language first 
183        var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
184 
185        var element = document.getElementById("fireinputSpecialCharMenu");
186        var label = FireinputUtils.getLocaleString("fireinput.special.char.label" + defaultLanguage);
187        element.setAttribute("label", label);
188 
189        for(var i=allSymbols.length-1; i>=0; i--)
190        {
191           var groupName = allSymbols[i].group;
192 
193           var menuID = document.getElementById(allSymbols[i].group + "_menu");
194           if(!menuID)
195           {
196              continue; 
197           }
198           var label = FireinputUtils.getLocaleString(groupName + defaultLanguage);
199           menuID.setAttribute("label", label);
200        }
201 
202        // refresh for date/time 
203        var menuID = document.getElementById("fireinput.datetime.label" +"_menu");
204        if(menuID)
205        {
206           var label = FireinputUtils.getLocaleString("fireinput.datetime.label" + defaultLanguage);
207           menuID.setAttribute("label", label);
208        }
209 
210     }, 
211 
212     addGroup: function()
213     {
214        var menuElement = document.getElementById("fireinputSpecialCharMenuItems");
215 
216        // get default language first 
217        var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
218 
219        // add date/time 
220        var label = FireinputUtils.getLocaleString("fireinput.datetime.label" + defaultLanguage);
221        var subMenu = document.createElement("menu");
222        var subMenupopup = document.createElement("menupopup");
223        subMenu.setAttribute("label", label);
224        subMenu.setAttribute("id", "fireinput.datetime.label"+"_menu");
225        var self = this; 
226 
227        subMenu.addEventListener("DOMMenuItemActive", function(event) { self.updateDateTime(event); }, true); 
228 
229        subMenupopup.setAttribute("id", "fireinput.datetime.label"); 
230 
231        subMenu.appendChild(subMenupopup);
232        menuElement.appendChild(subMenu);
233  
234        for(var i=allSymbols.length-1; i>=0; i--)
235        {
236           var groupName = allSymbols[i].group;
237 
238           var menuID = document.getElementById(allSymbols[i].group); 
239           if(!menuID)
240           { 
241              var label = FireinputUtils.getLocaleString(groupName + defaultLanguage);
242              var subMenu = document.createElement("menu"); 
243              var subMenupopup = document.createElement("menupopup"); 
244              subMenu.setAttribute("label", label); 
245              subMenu.setAttribute("id", allSymbols[i].group+"_menu"); 
246 
247              this.addGroupSymbol(subMenupopup, allSymbols[i].symbol); 
248              subMenupopup.setAttribute("id", allSymbols[i].group); 
249 
250              subMenu.appendChild(subMenupopup); 
251              menuElement.appendChild(subMenu); 
252 
253           }
254           else 
255           {
256              this.addGroupSymbol(menuID, allSymbols[i].symbol); 
257           }
258        }
259     },
260 
261     addGroupSymbol: function(menuItem, symbols)
262     {
263        var num = 5; 
264 
265        if(symbols.length > 50)
266           num = 10; 
267        for(var i=symbols.length-1; i>=0;)
268        {
269  
270           var toolbar = document.createElement("toolbar"); 
271           toolbar.setAttribute("class", "specialcharbar"); 
272           var j=0; 
273           for(j=0; j<num && (i-j) >=0; j++)
274           {
275              var text = symbols[i-j]; 
276 
277              var label = document.createElement("label"); 
278              label.setAttribute("value", text); 
279              label.setAttribute("tooltiptext", this.toUnicodeHex(text)); 
280              label.setAttribute("class", "specialcharlabel");
281              label.onclick=function(event) { Fireinput.insertSpecialCharAt(event); }; 
282              toolbar.appendChild(label); 
283           }
284           i-= j; 
285           
286           menuItem.appendChild(toolbar); 
287        }
288     },
289 
290     updateDateTime: function(event)
291     {
292        var subMenupopup = document.getElementById("fireinput.datetime.label"); 
293        while(subMenupopup.hasChildNodes())
294        {
295           subMenupopup.removeChild(subMenupopup.lastChild);
296        }
297 
298        this.addGroupSymbol(subMenupopup, this.getMonth());
299        this.addGroupSymbol(subMenupopup, this.getWeek());
300        this.addGroupSymbol(subMenupopup, this.getTime());
301 
302     },
303 
304     getMonth: function()
305     {
306 
307        var current = new Date(); 
308        var year  = current.getFullYear(); 
309        var month = current.getMonth();
310        var day =  current.getDate(); 
311        var monthArray = new Array(); 
312 
313        var monthValue = monthNumber[month+1]; 
314        var monthValueArray = monthValue.split(","); 
315        // format: 2007年4月10日
316        monthArray.push(year + "年" + (month+1) + "月" + day + "日"); 
317 
318        var year1= year; 
319        var aYear = monthNumber[parseInt(year1/1000)].split(",")[0]; 
320        year1 %= 1000; 
321        aYear += monthNumber[parseInt(year1/100)].split(",")[0]; 
322        year1 %= 100; 
323        aYear += monthNumber[parseInt(year1/10)].split(",")[0]; 
324        year1 %= 10; 
325        aYear += monthNumber[year1].split(",")[0]; 
326 
327        var day1 = day; 
328        var aDay = "";
329        if(day1 < 10)
330           aDay = monthNumber[day].split(",")[0];
331        else
332        {
333           if(day1 >= 20)
334              aDay = monthNumber[parseInt(day1/10)].split(",")[0];
335 
336           aDay += monthNumber[10].split(",")[0];
337           if(day1 % 10)
338              aDay += monthNumber[day % 10].split(",")[0];
339        }
340 
341        // format: 二零零七年四月二十二日
342        for(var i=0; i<monthValueArray.length; i++)
343          monthArray.push(aYear + "年" + monthValueArray[i] + "月" + aDay + "日"); 
344 
345        var bigYear = chineseBigNumber[parseInt(year/1000)]; 
346        year %= 1000; 
347        bigYear += chineseBigNumber[parseInt(year/100)]; 
348        year %= 100; 
349        bigYear += chineseBigNumber[parseInt(year/10)]; 
350        year %= 10; 
351        bigYear += chineseBigNumber[year]; 
352 
353  
354        var bigMonth = ""; 
355        if(month < 10)
356           bigMonth = chineseBigNumber[month+1];
357        else 
358        {
359           bigMonth = chineseBigNumber[10]; 
360           bigMonth += chineseBigNumber[month % 10 +1]; 
361        }
362    
363        var bigDay = ""; 
364        if(day < 10)
365           bigDay = chineseBigNumber[day]; 
366        else 
367        {
368           if(day >= 20)
369              bigDay = chineseBigNumber[parseInt(day/10)]; 
370 
371           bigDay += chineseBigNumber[10]; 
372           if(day % 10)
373              bigDay += chineseBigNumber[day % 10]; 
374        }
375      
376        // format 贰零零柒年肆月贰拾贰日
377        monthArray.push(bigYear + "年" + bigMonth + "月" + bigDay + "日"); 
378 
379        return monthArray; 
380     },
381 
382     getWeek: function()
383     {
384        var current = new Date();
385        var day =  current.getDay(); 
386        var weekArray = new Array(); 
387        var weekValueArray = weekNumber[day].split(","); 
388        for(var i=0; i<weekValueArray.length; i++)
389        {  
390           weekArray.push("星期" + weekValueArray[i]); 
391           weekArray.push("礼拜" + weekValueArray[i]); 
392        }
393 
394        return weekArray; 
395     },
396 
397     getTime: function()
398     {
399        var current = new Date();
400        var hour =  current.getHours(); 
401        var minute =  current.getMinutes(); 
402        var second =  current.getSeconds(); 
403 
404        var timeArray = new Array(); 
405        timeArray.push(hour + "点" + minute + "分" + second + "秒"); 
406 
407        if(hour < 6)
408           timeArray.push("凌晨" + hour + "点" + minute + "分" + second + "秒"); 
409        else if(hour < 10)
410           timeArray.push("早上" + hour + "点" + minute + "分" + second + "秒"); 
411        else if(hour < 12)
412           timeArray.push("上午" + hour + "点" + minute + "分" + second + "秒"); 
413        else if (hour < 20)
414           timeArray.push("下午" + (hour-12) + "点" + minute + "分" + second + "秒"); 
415        else 
416           timeArray.push("晚上" + (hour-12) + "点" + minute + "分" + second + "秒"); 
417 
418        return timeArray; 
419     },
420 
421     toUnicodeHex: function(uniStr)
422     {
423        var uniHex = uniStr.charCodeAt(0).toString(16);
424        while(uniHex.length<4)
425        {
426           uniHex = "0" + uniHex;
427        }
428        return uniHex.toUpperCase();
429     }, 
430 
431     getIMode: function(key)
432     {
433        var ivalue = []; 
434 
435        for(var i = iMode.length-1; i>=0; i--)
436        {
437           var ikey = 'i' + iMode[i].key; 
438           if(('i' + iMode[i].key).indexOf(key) == 0)
439           {
440               var list = []; 
441               if((iMode[i].word).fn)
442                  list = eval((iMode[i].word).fn + "(" + ")"); 
443               else 
444                  list = iMode[i].word; 
445 
446               for(var j = 0; j<list.length; j++)
447               {
448                  // put it in a format which the display engine can recognize 
449                  ivalue[ivalue.length] = {key: ikey, word: FireinputUnicode.convertFromUnicode(list[j]), ufreq: 'false'}; 
450               }
451           }
452        }
453 
454        return ivalue; 
455     }
456 
457 };              


syntax highlighted by Code2HTML, v. 0.9.1