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