Mercurial > hg > rc1
annotate plugins/thunderbird_labels/tb_label.js @ 1:5821049f1791
most of the way to allowing arbitrarily many labels
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 16:23:36 -0500 |
parents | 1e000243b222 |
children | 7a7f68b4358e |
rev | line source |
---|---|
0 | 1 /** |
2 * Version: | |
3 * $Revision$ | |
4 * Author: | |
5 * Michael Kefeder | |
6 * http://code.google.com/p/rcmail-thunderbird-labels/ | |
7 */ | |
8 | |
9 // global variable for contextmenu actions | |
10 rcmail.tb_label_no = ''; | |
11 | |
12 function rcmail_tb_label_menu(p) | |
13 { | |
14 if (typeof rcmail_ui == "undefined") | |
15 rcmail_ui = UI; | |
16 if (!rcmail_ui.check_tb_popup()) | |
17 rcmail_ui.tb_label_popup_add(); | |
18 | |
19 // Show the popup menu with tags | |
20 // -- skin larry vs classic | |
21 if (typeof rcmail_ui.show_popupmenu == "undefined") | |
22 rcmail_ui.show_popup('tb_label_popup'); | |
23 else | |
24 rcmail_ui.show_popupmenu('tb_label_popup'); | |
25 | |
26 return false; | |
27 } | |
28 | |
29 /** | |
30 * Shows the colors based on flag info like in Thunderbird | |
31 * (called when a new message in inserted in list of messages) | |
32 * maybe slow ? called for each message in mailbox at init | |
33 */ | |
34 function rcm_tb_label_insert(uid, row) | |
35 { | |
36 if (typeof rcmail.env == 'undefined' || typeof rcmail.env.messages == 'undefined') | |
37 return; | |
38 var message = rcmail.env.messages[uid]; | |
39 var rowobj = $(row.obj); | |
40 // add span container for little colored bullets | |
41 rowobj.find("td.subject").append("<span class='tb_label_dots'></span>"); | |
42 | |
43 if (message.flags && message.flags.tb_labels) { | |
44 if (message.flags.tb_labels.length) { | |
45 var spanobj = rowobj.find("td.subject span.tb_label_dots"); | |
46 message.flags.tb_labels.sort(function(a,b) {return a-b;}); | |
47 if (rcmail.env.tb_label_style=='bullets') { | |
48 // bullets UI style | |
49 for (idx in message.flags.tb_labels) { | |
50 spanobj.append("<span class='label"+message.flags.tb_labels[idx]+"'>•</span>"); | |
51 } | |
52 } else { | |
53 // thunderbird UI style | |
54 for (idx in message.flags.tb_labels) { | |
55 rowobj.addClass('label' + message.flags.tb_labels[idx]); | |
56 } | |
57 } | |
58 } | |
59 } | |
60 } | |
61 | |
62 /** | |
63 * Shows the submenu of thunderbird labels | |
64 */ | |
65 function rcm_tb_label_submenu(p) | |
66 { | |
67 if (typeof rcmail_ui == "undefined") | |
68 rcmail_ui = UI; | |
69 // setup onclick and active/non active classes | |
70 rcm_tb_label_create_popupmenu(); | |
71 | |
72 // -- create sensible popup, using roundcubes internals | |
73 if (!rcmail_ui.check_tb_popup()) | |
74 rcmail_ui.tb_label_popup_add(); | |
75 // -- skin larry vs classic | |
76 if (typeof rcmail_ui.show_popupmenu == "undefined") | |
77 rcmail_ui.show_popup('tb_label_popup'); | |
78 else | |
79 rcmail_ui.show_popupmenu('tb_label_popup'); | |
80 return false; | |
81 } | |
82 | |
83 function rcm_tb_label_flag_toggle(flag_uids, toggle_label_no, onoff) | |
84 { | |
85 var headers_table = $('table.headers-table'); | |
86 var preview_frame = $('#messagecontframe'); | |
87 // preview frame exists, simulate environment of single message view | |
88 if (preview_frame.length) | |
89 { | |
90 tb_labels_for_message = preview_frame.get(0).contentWindow.tb_labels_for_message; | |
91 headers_table = preview_frame.contents().find('table.headers-table'); | |
92 } | |
93 | |
94 if (!rcmail.message_list | |
95 && !headers_table) | |
96 return; | |
97 // for single message view | |
98 if (headers_table.length && flag_uids.length) { | |
99 if (onoff == true) { | |
100 if (rcmail.env.tb_label_style=='bullets') { | |
101 $('#labelbox').append("<span class='tb_label_span"+toggle_label_no+"'>" + | |
102 rcmail.env.tb_label_custom_labels[toggle_label_no] + "</span>"); | |
103 } else { | |
104 headers_table.addClass('label'+toggle_label_no); | |
105 } | |
106 // add to flag list | |
107 tb_labels_for_message.push(toggle_label_no); | |
108 | |
109 } | |
110 else | |
111 { | |
112 if (rcmail.env.tb_label_style=='bullets') { | |
113 $("span.tb_label_span"+toggle_label_no).remove(); | |
114 } else { | |
115 headers_table.removeClass('label'+toggle_label_no); | |
116 } | |
117 | |
118 var pos = jQuery.inArray(toggle_label_no, tb_labels_for_message); | |
119 if (pos > -1) { | |
120 tb_labels_for_message.splice(pos, 1); | |
121 } | |
122 } | |
123 // exit function when in detail mode. when preview is active keep going | |
124 if (!rcmail.env.messages) { | |
125 return; | |
126 } | |
127 } | |
128 jQuery.each(flag_uids, function (idx, uid) { | |
129 var message = rcmail.env.messages[uid]; | |
130 var row = rcmail.message_list.rows[uid]; | |
131 if (onoff == true) | |
132 { | |
133 // add colors | |
134 var rowobj = $(row.obj); | |
135 var spanobj = rowobj.find("td.subject span.tb_label_dots"); | |
136 if (rcmail.env.tb_label_style=='bullets') { | |
137 spanobj.append("<span class='label"+toggle_label_no+"'>•</span>"); | |
138 } else { | |
139 rowobj.addClass('label'+toggle_label_no); | |
140 } | |
141 | |
142 // add to flag list | |
143 message.flags.tb_labels.push(toggle_label_no); | |
144 } | |
145 else | |
146 { | |
147 // remove colors | |
148 var rowobj = $(row.obj); | |
149 if (rcmail.env.tb_label_style=='bullets') { | |
150 rowobj.find("td.subject span.tb_label_dots span.label"+toggle_label_no).remove(); | |
151 } else { | |
152 rowobj.removeClass('label'+toggle_label_no); | |
153 } | |
154 | |
155 // remove from flag list | |
156 var pos = jQuery.inArray(toggle_label_no, message.flags.tb_labels); | |
157 if (pos > -1) | |
158 message.flags.tb_labels.splice(pos, 1); | |
159 } | |
160 }); | |
161 } | |
162 | |
163 function rcm_tb_label_flag_msgs(flag_uids, toggle_label_no) | |
164 { | |
165 rcm_tb_label_flag_toggle(flag_uids, toggle_label_no, true); | |
166 } | |
167 | |
168 function rcm_tb_label_unflag_msgs(unflag_uids, toggle_label_no) | |
169 { | |
170 rcm_tb_label_flag_toggle(unflag_uids, toggle_label_no, false); | |
171 } | |
172 | |
173 // helper function to get selected/active messages | |
174 function rcm_tb_label_get_selection() | |
175 { | |
176 var selection = rcmail.message_list ? rcmail.message_list.get_selection() : []; | |
177 if (selection.length == 0 && rcmail.env.uid) | |
178 selection = [rcmail.env.uid, ]; | |
179 return selection; | |
180 } | |
181 | |
182 function rcm_tb_label_create_popupmenu() | |
183 { | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
184 for (i = 0; i < 7; i++) |
0 | 185 { |
186 var cur_a = $('li.label' + i +' a'); | |
187 | |
188 // add/remove active class | |
189 var selection = rcm_tb_label_get_selection(); | |
190 | |
191 if (selection.length == 0) | |
192 cur_a.removeClass('active'); | |
193 else | |
194 cur_a.addClass('active'); | |
195 } | |
196 } | |
197 | |
198 function rcm_tb_label_init_onclick() | |
199 { | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
200 for (i = 0; i < 7; i++) |
0 | 201 { |
202 // find the "HTML a tags" of tb-label submenus | |
203 var cur_a = $('#tb_label_popup li.label' + i +' a'); | |
204 | |
205 // TODO check if click event is defined instead of unbinding? | |
206 cur_a.unbind('click'); | |
207 cur_a.click(function() { | |
208 var toggle_label = $(this).parent().attr('class'); | |
209 var toggle_label_no = parseInt(toggle_label.replace('label', '')); | |
210 var selection = rcm_tb_label_get_selection(); | |
211 | |
212 if (!selection.length) | |
213 return; | |
214 | |
215 var from = toggle_label_no; | |
216 var to = toggle_label_no + 1; | |
217 var unset_all = false; | |
218 // special case flag 0 means remove all flags | |
219 if (toggle_label_no == 0) | |
220 { | |
221 from = 1; | |
1
5821049f1791
most of the way to allowing arbitrarily many labels
Charlie Root
parents:
0
diff
changeset
|
222 to = 7; |
0 | 223 unset_all = true; |
224 } | |
225 for (i = from; i < to; i++) | |
226 { | |
227 toggle_label = 'label' + i; | |
228 toggle_label_no = i; | |
229 // compile list of unflag and flag msgs and then send command | |
230 // Thunderbird modifies multiple message flags like it did the first in the selection | |
231 // e.g. first message has flag1, you click flag1, every message select loses flag1, the ones not having flag1 don't get it! | |
232 var first_toggle_mode = 'on'; | |
233 if (rcmail.env.messages) | |
234 { | |
235 var first_message = rcmail.env.messages[selection[0]]; | |
236 if (first_message.flags | |
237 && jQuery.inArray(toggle_label_no, | |
238 first_message.flags.tb_labels) >= 0 | |
239 ) | |
240 first_toggle_mode = 'off'; | |
241 else | |
242 first_toggle_mode = 'on'; | |
243 } | |
244 else // single message display | |
245 { | |
246 // flag already set? | |
247 if (jQuery.inArray(toggle_label_no, | |
248 tb_labels_for_message) >= 0) | |
249 first_toggle_mode = 'off'; | |
250 } | |
251 var flag_uids = []; | |
252 var unflag_uids = []; | |
253 jQuery.each(selection, function (idx, uid) { | |
254 // message list not available (example: in detailview) | |
255 if (!rcmail.env.messages) | |
256 { | |
257 if (first_toggle_mode == 'on') | |
258 flag_uids.push(uid); | |
259 else | |
260 unflag_uids.push(uid); | |
261 // make sure for unset all there is the single message id | |
262 if (unset_all && unflag_uids.length == 0) | |
263 unflag_uids.push(uid); | |
264 return; | |
265 } | |
266 var message = rcmail.env.messages[uid]; | |
267 if (message.flags | |
268 && jQuery.inArray(toggle_label_no, | |
269 message.flags.tb_labels) >= 0 | |
270 ) | |
271 { | |
272 if (first_toggle_mode == 'off') | |
273 unflag_uids.push(uid); | |
274 } | |
275 else | |
276 { | |
277 if (first_toggle_mode == 'on') | |
278 flag_uids.push(uid); | |
279 } | |
280 }); | |
281 | |
282 if (unset_all) | |
283 flag_uids = []; | |
284 | |
285 // skip sending flags to backend that are not set anywhere | |
286 if (flag_uids.length == 0 | |
287 && unflag_uids.length == 0) | |
288 continue; | |
289 | |
290 var str_flag_uids = flag_uids.join(','); | |
291 var str_unflag_uids = unflag_uids.join(','); | |
292 | |
293 var lock = rcmail.set_busy(true, 'loading'); | |
294 // call PHP set_flags to set the flags in IMAP server | |
295 rcmail.http_request('plugin.thunderbird_labels.set_flags', '_flag_uids=' + str_flag_uids + '&_unflag_uids=' + str_unflag_uids + '&_mbox=' + urlencode(rcmail.env.mailbox) + "&_toggle_label=" + toggle_label, lock); | |
296 | |
297 // remove/add classes and tb labels from messages in JS | |
298 rcm_tb_label_flag_msgs(flag_uids, toggle_label_no); | |
299 rcm_tb_label_unflag_msgs(unflag_uids, toggle_label_no); | |
300 } | |
301 }); | |
302 } | |
303 } | |
304 | |
305 function rcmail_ctxm_label(command, el, pos) | |
306 { | |
307 // my code works only on selected rows, contextmenu also on unselected | |
308 // so if no selection is available, use the uid set by contextmenu plugin | |
309 var selection = rcmail.message_list ? rcmail.message_list.get_selection() : []; | |
310 | |
311 if (!selection.length && !rcmail.env.uid) | |
312 return; | |
313 if (!selection.length && rcmail.env.uid) | |
314 rcmail.message_list.select_row(rcmail.env.uid); | |
315 | |
316 var cur_a = $('#tb_label_popup li.label' + rcmail.tb_label_no +' a'); | |
317 if (cur_a) | |
318 { | |
319 cur_a.click(); | |
320 } | |
321 | |
322 return; | |
323 } | |
324 | |
325 function rcmail_ctxm_label_set(which) | |
326 { | |
327 // hack for my contextmenu submenu hack to propagate the selected label-no | |
328 rcmail.tb_label_no = which; | |
329 } | |
330 | |
331 | |
332 $(document).ready(function() { | |
333 rcm_tb_label_init_onclick(); | |
334 // add keyboard shortcuts for keyboard and keypad if pref tb_label_enable_shortcuts=true | |
335 if (rcmail.env.tb_label_enable_shortcuts) { | |
336 $(document).keyup(function(e) { | |
337 //console.log('Handler for .keyup() called.' + e.which); | |
338 var k = e.which; | |
339 if ((k > 47 && k < 58) || (k > 95 && k < 106)) | |
340 { | |
341 var label_no = k % 48; | |
342 var cur_a = $('#tb_label_popup li.label' + label_no + ' a'); | |
343 | |
344 if (cur_a) | |
345 { | |
346 cur_a.click(); | |
347 } | |
348 } | |
349 }); | |
350 } | |
351 | |
352 // if exists add contextmenu entries | |
353 if (window.rcm_contextmenu_register_command) { | |
354 rcm_contextmenu_register_command('ctxm_tb_label', rcmail_ctxm_label, $('#tb_label_ctxm_mainmenu'), 'moreacts', 'after', true); | |
355 } | |
356 | |
357 // single message displayed? | |
358 if (window.tb_labels_for_message) | |
359 { | |
360 var labelbox_parent = $('div.message-headers'); // larry skin | |
361 if (!labelbox_parent.length) { | |
362 labelbox_parent = $("table.headers-table tbody tr:first-child"); // classic skin | |
363 } | |
364 labelbox_parent.append("<div id='labelbox'></div>"); | |
365 tb_labels_for_message.sort(function(a,b) {return a-b;}); | |
366 jQuery.each(tb_labels_for_message, function(idx, val) | |
367 { | |
368 rcm_tb_label_flag_msgs([-1,], val); | |
369 } | |
370 ); | |
371 } | |
372 | |
373 // add roundcube events | |
374 rcmail.addEventListener('insertrow', function(event) { rcm_tb_label_insert(event.uid, event.row); }); | |
375 | |
376 rcmail.addEventListener('init', function(evt) { | |
377 // create custom button, JS method, broken layout in Firefox 9 using PHP method now | |
378 /*var button = $('<A>').attr('href', '#').attr('id', 'tb_label_popuplink').attr('title', rcmail.gettext('label', 'thunderbird_labels')).html(''); | |
379 | |
380 button.bind('click', function(e) { | |
381 rcmail.command('plugin.thunderbird_labels.rcm_tb_label_submenu', this); | |
382 return false; | |
383 }); | |
384 | |
385 // add and register | |
386 rcmail.add_element(button, 'toolbar'); | |
387 rcmail.register_button('plugin.thunderbird_labels.rcm_tb_label_submenu', 'tb_label_popuplink', 'link'); | |
388 */ | |
389 //rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, true); | |
390 rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, rcmail.env.uid); | |
391 | |
392 // add event-listener to message list | |
393 if (rcmail.message_list) { | |
394 rcmail.message_list.addEventListener('select', function(list){ | |
395 rcmail.enable_command('plugin.thunderbird_labels.rcm_tb_label_submenu', list.get_selection().length > 0); | |
396 }); | |
397 } | |
398 }); | |
399 | |
400 // -- add my submenu to roundcubes UI (for roundcube classic only?) | |
401 if (window.rcube_mail_ui) | |
402 rcube_mail_ui.prototype.tb_label_popup_add = function() { | |
403 add = { | |
404 tb_label_popup: {id:'tb_label_popup'} | |
405 }; | |
406 this.popups = $.extend(this.popups, add); | |
407 var obj = $('#'+this.popups.tb_label_popup.id); | |
408 if (obj.length) | |
409 this.popups.tb_label_popup.obj = obj; | |
410 else | |
411 delete this.popups.tb_label_popup; | |
412 }; | |
413 | |
414 if (window.rcube_mail_ui) | |
415 rcube_mail_ui.prototype.check_tb_popup = function() { | |
416 // larry skin doesn't have that variable, popup works automagically, return true | |
417 if (typeof this.popups == 'undefined') | |
418 return true; | |
419 if (this.popups.tb_label_popup) | |
420 return true; | |
421 else | |
422 return false; | |
423 }; | |
424 | |
425 }); | |
426 |