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) {
|
|
63 for (parm in message.flags.tb_lparms) {
|
|
64 col="dates"; // should look up parm.number somewhere FIXME
|
|
65 var tdobj = rowobj.find("td."+col);
|
|
66 tdobj.append("<span class='labelparm"+parm.number+"'>"+
|
|
67 parm.parm+"</span>");
|
|
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();
|
|
163 } else {
|
|
164 rowobj.removeClass('label'+toggle_label_no);
|
|
165 }
|
|
166
|
|
167 // remove from flag list
|
|
168 var pos = jQuery.inArray(toggle_label_no, message.flags.tb_labels);
|
|
169 if (pos > -1)
|
|
170 message.flags.tb_labels.splice(pos, 1);
|
|
171 }
|
|
172 });
|
|
173 }
|
|
174
|
|
175 function rcm_tb_label_flag_msgs(flag_uids, toggle_label_no)
|
|
176 {
|
|
177 rcm_tb_label_flag_toggle(flag_uids, toggle_label_no, true);
|
|
178 }
|
|
179
|
|
180 function rcm_tb_label_unflag_msgs(unflag_uids, toggle_label_no)
|
|
181 {
|
|
182 rcm_tb_label_flag_toggle(unflag_uids, toggle_label_no, false);
|
|
183 }
|
|
184
|
|
185 // helper function to get selected/active messages
|
|
186 function rcm_tb_label_get_selection()
|
|
187 {
|
|
188 var selection = rcmail.message_list ? rcmail.message_list.get_selection() : [];
|
|
189 if (selection.length == 0 && rcmail.env.uid)
|
|
190 selection = [rcmail.env.uid, ];
|
|
191 return selection;
|
|
192 }
|
|
193
|
|
194 function rcm_tb_label_create_popupmenu()
|
|
195 {
|
9
|
196 var max_id = rcmail.env.tb_max_label;
|
|
197 for (i = 0; i <= max_id; i++)
|
0
|
198 {
|
|
199 var cur_a = $('li.label' + i +' a');
|
|
200
|
|
201 // add/remove active class
|
|
202 var selection = rcm_tb_label_get_selection();
|
|
203
|
|
204 if (selection.length == 0)
|
|
205 cur_a.removeClass('active');
|
|
206 else
|
|
207 cur_a.addClass('active');
|
|
208 }
|
|
209 }
|
|
210
|
|
211 function rcm_tb_label_init_onclick()
|
|
212 {
|
9
|
213 var max_id = rcmail.env.tb_max_label;
|
|
214 for (i = 0; i <= max_id ; i++)
|
0
|
215 {
|
|
216 // find the "HTML a tags" of tb-label submenus
|
|
217 var cur_a = $('#tb_label_popup li.label' + i +' a');
|
|
218
|
|
219 // TODO check if click event is defined instead of unbinding?
|
|
220 cur_a.unbind('click');
|
|
221 cur_a.click(function() {
|
|
222 var toggle_label = $(this).parent().attr('class');
|
|
223 var toggle_label_no = parseInt(toggle_label.replace('label', ''));
|
|
224 var selection = rcm_tb_label_get_selection();
|
|
225
|
|
226 if (!selection.length)
|
|
227 return;
|
|
228
|
|
229 var from = toggle_label_no;
|
9
|
230 var to = toggle_label_no;
|
0
|
231 var unset_all = false;
|
|
232 // special case flag 0 means remove all flags
|
|
233 if (toggle_label_no == 0)
|
|
234 {
|
|
235 from = 1;
|
9
|
236 to = rcmail.env.tb_max_label;
|
|
237 ;
|
0
|
238 unset_all = true;
|
|
239 }
|
9
|
240 for (i = from; i <= to; i++)
|
0
|
241 {
|
|
242 toggle_label = 'label' + i;
|
|
243 toggle_label_no = i;
|
|
244 // compile list of unflag and flag msgs and then send command
|
|
245 // Thunderbird modifies multiple message flags like it did the first in the selection
|
|
246 // e.g. first message has flag1, you click flag1, every message select loses flag1, the ones not having flag1 don't get it!
|
|
247 var first_toggle_mode = 'on';
|
|
248 if (rcmail.env.messages)
|
|
249 {
|
|
250 var first_message = rcmail.env.messages[selection[0]];
|
|
251 if (first_message.flags
|
|
252 && jQuery.inArray(toggle_label_no,
|
|
253 first_message.flags.tb_labels) >= 0
|
|
254 )
|
|
255 first_toggle_mode = 'off';
|
|
256 else
|
|
257 first_toggle_mode = 'on';
|
|
258 }
|
|
259 else // single message display
|
|
260 {
|
|
261 // flag already set?
|
|
262 if (jQuery.inArray(toggle_label_no,
|
|
263 tb_labels_for_message) >= 0)
|
|
264 first_toggle_mode = 'off';
|
|
265 }
|
|
266 var flag_uids = [];
|
|
267 var unflag_uids = [];
|
|
268 jQuery.each(selection, function (idx, uid) {
|
|
269 // message list not available (example: in detailview)
|
|
270 if (!rcmail.env.messages)
|
|
271 {
|
|
272 if (first_toggle_mode == 'on')
|
|
273 flag_uids.push(uid);
|
|
274 else
|
|
275 unflag_uids.push(uid);
|
|
276 // make sure for unset all there is the single message id
|
|
277 if (unset_all && unflag_uids.length == 0)
|
|
278 unflag_uids.push(uid);
|
|
279 return;
|
|
280 }
|
|
281 var message = rcmail.env.messages[uid];
|
|
282 if (message.flags
|
|
283 && jQuery.inArray(toggle_label_no,
|
|
284 message.flags.tb_labels) >= 0
|
|
285 )
|
|
286 {
|
|
287 if (first_toggle_mode == 'off')
|
|
288 unflag_uids.push(uid);
|
|
289 }
|
|
290 else
|
|
291 {
|
|
292 if (first_toggle_mode == 'on')
|
|
293 flag_uids.push(uid);
|
|
294 }
|
|
295 });
|
|
296
|
|
297 if (unset_all)
|
|
298 flag_uids = [];
|
|
299
|
|
300 // skip sending flags to backend that are not set anywhere
|
|
301 if (flag_uids.length == 0
|
|
302 && unflag_uids.length == 0)
|
|
303 continue;
|
|
304
|
|
305 var str_flag_uids = flag_uids.join(',');
|
|
306 var str_unflag_uids = unflag_uids.join(',');
|
|
307
|
|
308 var lock = rcmail.set_busy(true, 'loading');
|
|
309 // call PHP set_flags to set the flags in IMAP server
|
|
310 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);
|
|
311
|
|
312 // remove/add classes and tb labels from messages in JS
|
|
313 rcm_tb_label_flag_msgs(flag_uids, toggle_label_no);
|
|
314 rcm_tb_label_unflag_msgs(unflag_uids, toggle_label_no);
|
|
315 }
|
|
316 });
|
|
317 }
|
|
318 }
|
|
319
|
|
320 function rcmail_ctxm_label(command, el, pos)
|
|
321 {
|
|
322 // my code works only on selected rows, contextmenu also on unselected
|
|
323 // so if no selection is available, use the uid set by contextmenu plugin
|
|
324 var selection = rcmail.message_list ? rcmail.message_list.get_selection() : [];
|
|
325
|
|
326 if (!selection.length && !rcmail.env.uid)
|
|
327 return;
|
|
328 if (!selection.length && rcmail.env.uid)
|
|
329 rcmail.message_list.select_row(rcmail.env.uid);
|
|
330
|
|
331 var cur_a = $('#tb_label_popup li.label' + rcmail.tb_label_no +' a');
|
|
332 if (cur_a)
|
|
333 {
|
|
334 cur_a.click();
|
|
335 }
|
|
336
|
|
337 return;
|
|
338 }
|
|
339
|
|
340 function rcmail_ctxm_label_set(which)
|
|
341 {
|
|
342 // hack for my contextmenu submenu hack to propagate the selected label-no
|
|
343 rcmail.tb_label_no = which;
|
|
344 }
|
|
345
|
|
346
|
|
347 $(document).ready(function() {
|
|
348 rcm_tb_label_init_onclick();
|
|
349 // add keyboard shortcuts for keyboard and keypad if pref tb_label_enable_shortcuts=true
|
|
350 if (rcmail.env.tb_label_enable_shortcuts) {
|
|
351 $(document).keyup(function(e) {
|
|
352 //console.log('Handler for .keyup() called.' + e.which);
|
|
353 var k = e.which;
|
|
354 if ((k > 47 && k < 58) || (k > 95 && k < 106))
|
|
355 {
|
|
356 var label_no = k % 48;
|
|
357 var cur_a = $('#tb_label_popup li.label' + label_no + ' a');
|
|
358
|
|
359 if (cur_a)
|
|
360 {
|
|
361 cur_a.click();
|
|
362 }
|
|
363 }
|
|
364 });
|
|
365 }
|
|
366
|
|
367 // if exists add contextmenu entries
|
|
368 if (window.rcm_contextmenu_register_command) {
|
|
369 rcm_contextmenu_register_command('ctxm_tb_label', rcmail_ctxm_label, $('#tb_label_ctxm_mainmenu'), 'moreacts', 'after', true);
|
|
370 }
|
|
371
|
|
372 // single message displayed?
|
|
373 if (window.tb_labels_for_message)
|
|
374 {
|
|
375 var labelbox_parent = $('div.message-headers'); // larry skin
|
|
376 if (!labelbox_parent.length) {
|
|
377 labelbox_parent = $("table.headers-table tbody tr:first-child"); // classic skin
|
|
378 }
|
|
379 labelbox_parent.append("<div id='labelbox'></div>");
|
|
380 tb_labels_for_message.sort(function(a,b) {return a-b;});
|
|
381 jQuery.each(tb_labels_for_message, function(idx, val)
|
|
382 {
|
|
383 rcm_tb_label_flag_msgs([-1,], val);
|
|
384 }
|
|
385 );
|
|
386 }
|
|
387
|
|
388 // add roundcube events
|
|
389 rcmail.addEventListener('insertrow', function(event) { rcm_tb_label_insert(event.uid, event.row); });
|
|
390
|
|
391 rcmail.addEventListener('init', function(evt) {
|
|
392 // create custom button, JS method, broken layout in Firefox 9 using PHP method now
|
|
393 /*var button = $('<A>').attr('href', '#').attr('id', 'tb_label_popuplink').attr('title', rcmail.gettext('label', 'thunderbird_labels')).html('');
|
|
394
|
|
395 button.bind('click', function(e) {
|
|
396 rcmail.command('plugin.thunderbird_labels.rcm_tb_label_submenu', this);
|
|
397 return false;
|
|
398 });
|
|
399
|
|
400 // add and register
|
|
401 rcmail.add_element(button, 'toolbar');
|
|
402 rcmail.register_button('plugin.thunderbird_labels.rcm_tb_label_submenu', 'tb_label_popuplink', 'link');
|
|
403 */
|
|
404 //rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, true);
|
|
405 rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, rcmail.env.uid);
|
|
406
|
|
407 // add event-listener to message list
|
|
408 if (rcmail.message_list) {
|
|
409 rcmail.message_list.addEventListener('select', function(list){
|
|
410 rcmail.enable_command('plugin.thunderbird_labels.rcm_tb_label_submenu', list.get_selection().length > 0);
|
|
411 });
|
|
412 }
|
|
413 });
|
|
414
|
|
415 // -- add my submenu to roundcubes UI (for roundcube classic only?)
|
|
416 if (window.rcube_mail_ui)
|
|
417 rcube_mail_ui.prototype.tb_label_popup_add = function() {
|
|
418 add = {
|
|
419 tb_label_popup: {id:'tb_label_popup'}
|
|
420 };
|
|
421 this.popups = $.extend(this.popups, add);
|
|
422 var obj = $('#'+this.popups.tb_label_popup.id);
|
|
423 if (obj.length)
|
|
424 this.popups.tb_label_popup.obj = obj;
|
|
425 else
|
|
426 delete this.popups.tb_label_popup;
|
|
427 };
|
|
428
|
|
429 if (window.rcube_mail_ui)
|
|
430 rcube_mail_ui.prototype.check_tb_popup = function() {
|
|
431 // larry skin doesn't have that variable, popup works automagically, return true
|
|
432 if (typeof this.popups == 'undefined')
|
|
433 return true;
|
|
434 if (this.popups.tb_label_popup)
|
|
435 return true;
|
|
436 else
|
|
437 return false;
|
|
438 };
|
|
439
|
|
440 });
|
|
441
|