summaryrefslogtreecommitdiff
path: root/plugins/thunderbird_labels/tb_label.js
blob: a9239f30f14d5cf3d5301164e51408558ca98ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * Version:
 * $Revision: 26 $
 * Author:
 * Michael Kefeder
 * http://code.google.com/p/rcmail-thunderbird-labels/
 */

// global variable for contextmenu actions
rcmail.tb_label_no = '';

function rcmail_tb_label_menu(p)
{
	if (typeof rcmail_ui == "undefined")
		rcmail_ui = UI;
	if (!rcmail_ui.check_tb_popup())
		rcmail_ui.tb_label_popup_add();
	
	// Show the popup menu with tags
	// -- skin larry vs classic
	if (typeof rcmail_ui.show_popupmenu == "undefined")
		rcmail_ui.show_popup('tb_label_popup');
	else
		rcmail_ui.show_popupmenu('tb_label_popup');

	return false;
}

/**
* Shows the colors based on flag info like in Thunderbird
*/
function rcm_tb_label_insert(uid, row)
{
	var message = rcmail.env.messages[uid];
	
	if (message.flags && message.flags.tb_labels)
	{
		var rowobj = $(row.obj);
		for (idx in message.flags.tb_labels)
			rowobj.addClass('label' + message.flags.tb_labels[idx]);
	}
}

/**
* Shows the submenu of thunderbird labels
*/
function rcm_tb_label_submenu(p)
{
	if (typeof rcmail_ui == "undefined")
		rcmail_ui = UI;
	// setup onclick and active/non active classes
	rcm_tb_label_create_popupmenu();
	
	// -- create sensible popup, using roundcubes internals
	if (!rcmail_ui.check_tb_popup())
		rcmail_ui.tb_label_popup_add();
	// -- skin larry vs classic
	if (typeof rcmail_ui.show_popupmenu == "undefined")
		rcmail_ui.show_popup('tb_label_popup');
	else
		rcmail_ui.show_popupmenu('tb_label_popup');
	return false;
}

function rcm_tb_label_flag_toggle(flag_uids, toggle_label_no, onoff)
{
	var headers_table = $('table.headers-table');
	var preview_frame = $('#messagecontframe');
	// preview frame exists, simulate environment of single message view
	if (preview_frame.length)
	{
		tb_labels_for_message = preview_frame.get(0).contentWindow.tb_labels_for_message;
		headers_table = preview_frame.contents().find('table.headers-table');
	}
	
	if (!rcmail.message_list
		&& !headers_table)
		return;
	// for single message view
	if (headers_table.length && flag_uids.length)
	{
		if (onoff == true)
		{
			// add color
			headers_table.addClass('label'+toggle_label_no);
			// add to flag list
			tb_labels_for_message.push(toggle_label_no);
		}
		else
		{
			// remove color
			headers_table.removeClass('label'+toggle_label_no);
			var pos = jQuery.inArray(toggle_label_no, tb_labels_for_message);
			if (pos > -1)
				tb_labels_for_message.splice(pos, 1);
		}
		// exit function when in detail mode. when preview is active keep going
		if (!rcmail.env.messages)
			return;
	}
	jQuery.each(flag_uids, function (idx, uid) {
			var message = rcmail.env.messages[uid];
			var row = rcmail.message_list.rows[uid];
			if (onoff == true)
			{
				// add colors
				var rowobj = $(row.obj);
				rowobj.addClass('label'+toggle_label_no);
				// add to flag list
				message.flags.tb_labels.push(toggle_label_no);
			}
			else
			{
				// remove colors
				var rowobj = $(row.obj);
				rowobj.removeClass('label'+toggle_label_no);
				// remove from flag list
				var pos = jQuery.inArray(toggle_label_no, message.flags.tb_labels);
				if (pos > -1)
					message.flags.tb_labels.splice(pos, 1);
			}
	});
}

function rcm_tb_label_flag_msgs(flag_uids, toggle_label_no)
{
	rcm_tb_label_flag_toggle(flag_uids, toggle_label_no, true);
}

function rcm_tb_label_unflag_msgs(unflag_uids, toggle_label_no)
{
	rcm_tb_label_flag_toggle(unflag_uids, toggle_label_no, false);
}

// helper function to get selected/active messages
function rcm_tb_label_get_selection()
{
	var selection = rcmail.message_list ? rcmail.message_list.get_selection() : [];
	if (selection.length == 0 && rcmail.env.uid)
		selection = [rcmail.env.uid, ];
	return selection;
}

function rcm_tb_label_create_popupmenu()
{
	for (i = 0; i < 6; i++)
	{
		var cur_a = $('li.label' + i +' a');
		
		// add/remove active class
		var selection = rcm_tb_label_get_selection();
		
		if (selection.length == 0)
			cur_a.removeClass('active');
		else
			cur_a.addClass('active');
	}
}

function rcm_tb_label_init_onclick()
{
	for (i = 0; i < 6; i++)
	{
		var cur_a = $('#tb_label_popup li.label' + i +' a');
	
		// TODO check if click event is defined instead of unbinding?
		cur_a.unbind('click');
		cur_a.click(function() {
				var toggle_label = $(this).parent().attr('class');
				var toggle_label_no = parseInt(toggle_label.replace('label', ''));
				var selection = rcm_tb_label_get_selection();
				
				if (!selection.length)
					return;
				
				var from = toggle_label_no;
				var to = toggle_label_no + 1;
				var unset_all = false;
				// special case flag 0 means remove all flags
				if (toggle_label_no == 0)
				{
					from = 1;
					to = 6;
					unset_all = true;
				}
				for (i = from; i < to; i++)
				{
					toggle_label = 'label' + i;
					toggle_label_no = i;
					// compile list of unflag and flag msgs and then send command
					// Thunderbird modifies multiple message flags like it did the first in the selection
					// e.g. first message has flag1, you click flag1, every message select loses flag1, the ones not having flag1 don't get it!
					var first_toggle_mode = 'on';
					if (rcmail.env.messages)
					{
						var first_message = rcmail.env.messages[selection[0]];
						if (first_message.flags
							&& jQuery.inArray(toggle_label_no,
									first_message.flags.tb_labels) >= 0
							)
							first_toggle_mode = 'off';
						else
							first_toggle_mode = 'on';
					}
					else // single message display
					{
						// flag already set?
						if (jQuery.inArray(toggle_label_no,
									tb_labels_for_message) >= 0)
							first_toggle_mode = 'off';
					}
					var flag_uids = [];
					var unflag_uids = [];
					jQuery.each(selection, function (idx, uid) {
							// message list not available (example: in detailview)
							if (!rcmail.env.messages)
							{
								if (first_toggle_mode == 'on')
									flag_uids.push(uid);
								else
									unflag_uids.push(uid);
								// make sure for unset all there is the single message id
								if (unset_all && unflag_uids.length == 0)
									unflag_uids.push(uid);
								return;
							}
							var message = rcmail.env.messages[uid];
							if (message.flags
								&& jQuery.inArray(toggle_label_no,
										message.flags.tb_labels) >= 0
								)
							{
								if (first_toggle_mode == 'off')
									unflag_uids.push(uid);
							}
							else
							{
								if (first_toggle_mode == 'on')
									flag_uids.push(uid);
							}
					});
					
					if (unset_all)
						flag_uids = [];
					
					// skip sending flags to backend that are not set anywhere
					if (flag_uids.length == 0
						&& unflag_uids.length == 0)
							continue;
					
					var str_flag_uids = flag_uids.join(',');
					var str_unflag_uids = unflag_uids.join(',');
					
					var lock = rcmail.set_busy(true, 'loading');
					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);
					
					// remove/add classes and tb labels from messages in JS
					rcm_tb_label_flag_msgs(flag_uids, toggle_label_no);
					rcm_tb_label_unflag_msgs(unflag_uids, toggle_label_no);
				}
		});
	}
}

function rcmail_ctxm_label(command, el, pos)
{
	// my code works only on selected rows, contextmenu also on unselected
	// so if no selection is available, use the uid set by contextmenu plugin
	var selection = rcmail.message_list ? rcmail.message_list.get_selection() : [];
	
	if (!selection.length && !rcmail.env.uid)
		return;
	if (!selection.length && rcmail.env.uid)
		rcmail.message_list.select_row(rcmail.env.uid);
	
	var cur_a = $('#tb_label_popup li.label' + rcmail.tb_label_no +' a');
	if (cur_a)
	{
		cur_a.click();
	}
	
	return;
}

function rcmail_ctxm_label_set(which)
{
	// hack for my contextmenu submenu hack to propagate the selected label-no
	rcmail.tb_label_no = which;
}


$(document).ready(function() {
	rcm_tb_label_init_onclick();
	// add keyboard shortcuts for normal keyboard and keypad
	$(document).keyup(function(e) {
		//console.log('Handler for .keyup() called.' + e.which);
		var k = e.which;
		if ((k > 47 && k < 58) || (k > 95 && k < 106))
		{
			var label_no = k % 48;
			var cur_a = $('#tb_label_popup li.label' + label_no + ' a');
			
			if (cur_a)
			{
				cur_a.click();
			}
		}
	});
	
	// if exists add contextmenu entries
	if (window.rcm_contextmenu_register_command) {
		rcm_contextmenu_register_command('ctxm_tb_label', rcmail_ctxm_label, $('#tb_label_ctxm_mainmenu'), 'moreacts', 'after', true);
	}
	
	// single message displayed?
	if (window.tb_labels_for_message)
	{
		jQuery.each(tb_labels_for_message, function(idx, val)
			{
				rcm_tb_label_flag_msgs([-1,], val);
			}
		);
	}
	
	// add roundcube events
	rcmail.addEventListener('insertrow', function(event) { rcm_tb_label_insert(event.uid, event.row); });
	
	rcmail.addEventListener('init', function(evt) {
		// create custom button, JS method, broken layout in Firefox 9 using PHP method now
		/*var button = $('<A>').attr('href', '#').attr('id', 'tb_label_popuplink').attr('title', rcmail.gettext('label', 'thunderbird_labels')).html('');
		
		button.bind('click', function(e) {
			rcmail.command('plugin.thunderbird_labels.rcm_tb_label_submenu', this);
			return false;
		});
		
		// add and register
		rcmail.add_element(button, 'toolbar');
		rcmail.register_button('plugin.thunderbird_labels.rcm_tb_label_submenu', 'tb_label_popuplink', 'link');
		*/
		rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, true);
	});
	
	// -- add my submenu to roundcubes UI (for roundcube classic only?)
	if (window.rcube_mail_ui)
	rcube_mail_ui.prototype.tb_label_popup_add = function() {
		add = {
			tb_label_popup:     {id:'tb_label_popup'}
		};
		this.popups = $.extend(this.popups, add);
		var obj = $('#'+this.popups.tb_label_popup.id);
		if (obj.length)
			this.popups.tb_label_popup.obj = obj;
		else
			delete this.popups.tb_label_popup;
	};
	
	if (window.rcube_mail_ui)
	rcube_mail_ui.prototype.check_tb_popup = function() {
		// larry skin doesn't have that variable, popup works automagically, return true
		if (typeof this.popups == 'undefined')
			return true;
		if (this.popups.tb_label_popup)
			return true;
		else
			return false;
	};
});