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 EMOTION_URL = SERVER_URL + "/emotions/emotion.html";
37
38 var FireinputEmotions =
39 {
40 initialized: false,
41
42 initializeRemote: false,
43
44 mouseTooltips: "",
45
46 userEmotionList: [],
47
48 addContextSelectedImage: function()
49 {
50 if(FireinputEmotionUpdater.save(gContextMenu.imageURL))
51 {
52 this.loadUserEmotionURL();
53 }
54 },
55
56 load: function(forceLoad)
57 {
58 if(!this.initialized || forceLoad)
59 {
60 // get default language first
61 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
62 this.mouseTooltip = FireinputUtils.getLocaleString("fireinput.emotion.mouse.tooltips" + defaultLanguage);
63
64 var element = document.getElementById("fireinputEmotionMenu");
65 var label = FireinputUtils.getLocaleString("fireinput.emotion.label" + defaultLanguage);
66 element.setAttribute("label", label);
67
68 this.initialized = true;
69 this.initializeRemote = false;
70 this.loadUserEmotionURL();
71
72 // if forceLoad happens, don't register observer second time
73 if(forceLoad)
74 return;
75
76 // register an observer
77 var os = FireinputXPC.getService("@mozilla.org/observer-service;1", "nsIObserverService");
78 os.addObserver(this, "fireinput-user-emotion-changed", false);
79 }
80
81 },
82
83 addUserEmotionMenu: function()
84 {
85 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
86 var menuElement = document.getElementById("fireinputEmotionMenuItems");
87
88 // user action menu
89 var id = "fireinput.emotion.user.action";
90 var label = FireinputUtils.getLocaleString(id + defaultLanguage);
91 var menuID = document.getElementById(id);
92 if(!menuID)
93 {
94 var subMenu = document.createElement("menuitem");
95 subMenu.setAttribute("label", label);
96 subMenu.setAttribute("id", id);
97 subMenu.onclick=bind(function(event)
98 {
99 this.showAddEmotionDialog(event);
100 }, this);
101
102 menuElement.appendChild(subMenu);
103
104 }
105
106 // user img list
107 id = "fireinput.emotion.user.list";
108 menuID = document.getElementById(id);
109 label = FireinputUtils.getLocaleString(id + defaultLanguage);
110 if(!menuID)
111 {
112 var subMenu = document.createElement("menu");
113 var subMenupopup = document.createElement("menupopup");
114 subMenu.setAttribute("label", label);
115 subMenu.setAttribute("id", id);
116
117 this.addGroupEmotion(subMenupopup, this.userEmotionList);
118 subMenupopup.setAttribute("id", id + "_popupmenu");
119
120 subMenu.appendChild(subMenupopup);
121 menuElement.appendChild(subMenu);
122 }
123 else
124 {
125 while(menuID.hasChildNodes())
126 {
127 menuID.removeChild(menuID.lastChild);
128 }
129
130 var subMenupopup = document.createElement("menupopup");
131
132 this.addGroupEmotion(subMenupopup, this.userEmotionList);
133 subMenupopup.setAttribute("id", id + "_popupmenu");
134 menuID.appendChild(subMenupopup);
135
136 }
137
138 // remote emotion
139 if(!this.initializeRemote)
140 this.loadRemoteEmotions();
141 },
142
143 loadUserEmotionURL: function()
144 {
145 var ios = FireinputXPC.getIOService();
146 var fileHandler = ios.getProtocolHandler("file")
147 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
148
149 var path = FireinputUtils.getAppRootPath();
150 var datafile = fileHandler.getFileFromURLSpec(path + "/useremotion.fireinput");
151 this.userEmotionList.length = 0;
152 if(!datafile.exists())
153 {
154 // add user's other menu
155 this.addUserEmotionMenu();
156 return;
157 }
158
159 var options = {
160 caller: this,
161 oncomplete: this.addUserEmotionMenu,
162 onavailable: this.getUserEmotionURL
163 };
164 FireinputStream.loadDataAsync(datafile, options);
165 },
166
167 getUserEmotionURL: function(str)
168 {
169 this.userEmotionList[this.userEmotionList.length] = str;
170 },
171
172 showAddEmotionDialog: function()
173 {
174 FireinputUtils.loadURI("chrome://fireinput/content/emotionmgr/emotionmgr.html");
175 },
176
177 loadRemoteEmotions: function()
178 {
179 var ajax = new Ajax();
180 if(!ajax)
181 return;
182
183 var self = this;
184
185 ajax.setOptions(
186 {
187 method: 'get',
188 onSuccess: function(p) { self.displayEmotionMenu(p); },
189 onFailure: function(p) { self.displayEmotionMenu(p); }
190 });
191 ajax.request(EMOTION_URL);
192 },
193
194 displayEmotionMenu: function(p)
195 {
196 if(!p)
197 return;
198 if(p.responseText.length <= 0)
199 return;
200
201 var jsonArray;
202 try {
203 jsonArray = eval('(' + p.responseText + ')');
204 }
205 catch(e) { };
206
207 if(typeof(jsonArray) == 'undefined')
208 return;
209
210 this.initializeRemote = true;
211 this.addGroup(jsonArray);
212 },
213
214 refreshMenu: function()
215 {
216 if(!this.initialized)
217 return;
218
219 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
220
221 this.mouseTooltip = FireinputUtils.getLocaleString("fireinput.emotion.mouse.tooltips" + defaultLanguage);
222
223 var myMenuID = document.getElementById("fireinput.emotion.user.list");
224 var myLabel = FireinputUtils.getLocaleString("fireinput.emotion.user.list" + defaultLanguage);
225 myMenuID.setAttribute("label", myLabel);
226
227 var actionMenuID = document.getElementById("fireinput.emotion.user.action");
228 var aLabel = FireinputUtils.getLocaleString("fireinput.emotion.user.action" + defaultLanguage);
229 actionMenuID.setAttribute("label", aLabel);
230
231 var element = document.getElementById("fireinputEmotionMenu");
232 var label = FireinputUtils.getLocaleString("fireinput.emotion.label" + defaultLanguage);
233 element.setAttribute("label", label);
234
235 element = document.getElementById("fireinputEmotionMenuItems");
236
237 for (var child = element.firstChild; child; child = child.nextSibling)
238 {
239 var label = "";
240 if(defaultLanguage != "")
241 label = child.getAttribute("categoryname");
242 else
243 label = child.getAttribute("category");
244
245 if(label && label.length > 0)
246 child.setAttribute("label", label);
247
248 var images = child.getElementsByTagName("image");
249 if(!images)
250 continue;
251
252 for(var i=images.length-1; i>=0; i--)
253 {
254 images[i].setAttribute("tooltiptext", this.mouseTooltip);
255 }
256 }
257 },
258
259
260 addGroup: function(jsonArray)
261 {
262 // get default language first
263 var defaultLanguage = fireinputPrefGetDefault("interfaceLanguage");
264 var menuElement = document.getElementById("fireinputEmotionMenuItems");
265
266 for(var i=0; i < jsonArray.length; i++)
267 {
268 var data = jsonArray[i];
269 var groupName = data.name;
270
271 if(defaultLanguage.indexOf(LANGUAGE_ZH) < 0)
272 groupName = data.category;
273
274 var id = "fireinput.emotion." + data.category;
275 var menuID = document.getElementById(id);
276 var label = groupName;
277 if(!menuID)
278 {
279 var subMenu = document.createElement("menu");
280 var subMenupopup = document.createElement("menupopup");
281 subMenu.setAttribute("label", label);
282 subMenu.setAttribute("categoryname", data.name);
283 subMenu.setAttribute("category", data.category);
284 subMenu.setAttribute("id", id);
285
286 this.addGroupEmotion(subMenupopup, data.urllist);
287 subMenupopup.setAttribute("id", id +"_menupopup");
288
289 subMenu.appendChild(subMenupopup);
290 menuElement.appendChild(subMenu);
291
292 }
293 else
294 {
295 menuID.setAttribute("label", label);
296 }
297 }
298 },
299
300 addGroupEmotion: function(menuItem, urllist)
301 {
302 var num = 5;
303
304 if(urllist.length > 50)
305 num = 10;
306
307 for(var i=urllist.length-1; i>=0;)
308 {
309
310 var toolbar = document.createElement("toolbar");
311 toolbar.setAttribute("class", "specialcharbar");
312 var j=0;
313 for(j=0; j<num && (i-j) >=0; j++)
314 {
315 var label = document.createElement("label");
316 label.setAttribute("class", "specialcharlabel");
317 label.setAttribute("hiddenvalue", urllist[i-j]);
318
319 label.onclick=bind(function(event)
320 { if(event.button == 2)
321 Fireinput.insertSpecialCharAt(event, IMAGE_SOURCE_TYPE, IMAGE_INSERT_BBCODE_URL);
322 else
323 Fireinput.insertSpecialCharAt(event, IMAGE_SOURCE_TYPE, IMAGE_INSERT_URL);
324 }, this);
325 var img = document.createElement("image");
326 img.setAttribute("src", urllist[i-j]);
327 img.setAttribute("height", 32);
328 img.setAttribute("width", 32);
329 img.setAttribute("value", urllist[i-j]);
330 img.setAttribute("tooltiptext", this.mouseTooltip);
331 label.appendChild(img);
332 toolbar.appendChild(label);
333 }
334 i-= j;
335
336 menuItem.appendChild(toolbar);
337 }
338 },
339
340 copyIntoClipboard: function(event)
341 {
342 var clickTarget = event.target;
343 var value = clickTarget.getAttribute("hiddenvalue");
344 if(!value)
345 value = clickTarget.getAttribute("value");
346
347 try
348 {
349 var clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
350 .getService(Components.interfaces.nsIClipboardHelper);
351 clipboard.copyString(value);
352 }
353 catch(e) {}
354 },
355
356 observe: function(subject, topic, data)
357 {
358 if(topic != 'fireinput-user-emotion-changed')
359 return;
360
361 this.loadUserEmotionURL();
362 }
363 };
syntax highlighted by Code2HTML, v. 0.9.1