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