diff plugins/thunderbird_labels/tb_label.js @ 26:b2b6c0af2383

remove _ from name, lots of other hacks to get label context menu working
author Charlie Root
date Sun, 28 Jan 2018 11:32:20 -0500
parents 4869fae20b88
children f8b3ac77e951
line wrap: on
line diff
--- a/plugins/thunderbird_labels/tb_label.js	Thu Jan 18 07:54:45 2018 -0500
+++ b/plugins/thunderbird_labels/tb_label.js	Sun Jan 28 11:32:20 2018 -0500
@@ -14,14 +14,14 @@
 	if (typeof rcmail_ui == "undefined")
 		rcmail_ui = UI;
 	if (!rcmail_ui.check_tb_popup())
-		rcmail_ui.tb_label_popup_add();
+		rcmail_ui.tblabelpopup_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');
+		rcmail_ui.show_popup('tblabelpopup');
 	else
-		rcmail_ui.show_popupmenu('tb_label_popup');
+		rcmail_ui.show_popupmenu('tblabelpopup');
 
 	return false;
 }
@@ -82,12 +82,12 @@
 	
 	// -- create sensible popup, using roundcubes internals
 	if (!rcmail_ui.check_tb_popup())
-		rcmail_ui.tb_label_popup_add();
+		rcmail_ui.tblabelpopup_add();
 	// -- skin larry vs classic
 	if (typeof rcmail_ui.show_popupmenu == "undefined")
-		rcmail_ui.show_popup('tb_label_popup');
+		rcmail_ui.show_popup('tblabelpopup');
 	else
-		rcmail_ui.show_popupmenu('tb_label_popup');
+		rcmail_ui.show_popupmenu('tblabelpopup');
 	return false;
 }
 
@@ -209,124 +209,127 @@
 	}
 }
 
+function tblabeldoit(props,obj,event) {
+  var toggle_label = props;
+  var toggle_label_no = parseInt(toggle_label.replace('label', ''));
+  var label_name = rcmail.env.tb_label_custom_labels[toggle_label_no];
+  var hasParm = rcmail.env.tb_label_parm_labels[toggle_label_no];
+  var selection = rcm_tb_label_get_selection();
+				
+  if (!selection.length)
+    return;
+				
+  var from = toggle_label_no;
+  var to = toggle_label_no;
+  var unset_all = false;
+  // special case flag 0 means remove all flags
+  if (toggle_label_no == 0)
+    {
+      from = 1;
+      to = rcmail.env.tb_max_label;
+      ;
+      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';
+	}
+      if (hasParm && first_toggle_mode == 'on') {
+	var dates=prompt("Booking dates in form d/m-d/m : ","25/7-1/8");
+	if (dates!=null){
+	  toggle_label+='_'+dates;
+	}
+	else {
+	  return;
+	}
+      }
+      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');
+      // call PHP set_flags to set the flags in IMAP server
+      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 rcm_tb_label_init_onclick()
 {
   var max_id = rcmail.env.tb_max_label;
 	for (i = 0; i <= max_id ; i++)
 	{
 	  // find the "HTML a tags" of tb-label submenus
-		var cur_a = $('#tb_label_popup li.label' + i +' a');
+		var cur_a = $('#tblabelpopup 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 label_name = rcmail.env.tb_label_custom_labels[toggle_label_no];
-				var hasParm = rcmail.env.tb_label_parm_labels[toggle_label_no];
-				var selection = rcm_tb_label_get_selection();
-				
-				if (!selection.length)
-					return;
-				
-				var from = toggle_label_no;
-				var to = toggle_label_no;
-				var unset_all = false;
-				// special case flag 0 means remove all flags
-				if (toggle_label_no == 0)
-				{
-					from = 1;
-					to = rcmail.env.tb_max_label;
-;
-					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';
-					}
-					if (hasParm && first_toggle_mode == 'on') {
-					  var dates=prompt("Booking dates in form d/m-d/m : ","25/7-1/8");
-					  if (dates!=null){
-					    toggle_label+='_'+dates;
-					  }
-					  else {
-					    return;
-					  }
-					}
-					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');
-					// call PHP set_flags to set the flags in IMAP server
-					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);
-				}
-		});
+		cur_a.attr('onclick',"rcmail.command('tblabeldoit','label"+i+"',this,event)");
 	}
+	rcmail.register_command('tblabeldoit',function(props,obj,event) {tblabeldoit(props,obj,event);});
 }
 
 function rcmail_ctxm_label(command, el, pos)
@@ -340,7 +343,7 @@
 	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');
+	var cur_a = $('#tblabelpopup li.label' + rcmail.tb_label_no +' a');
 	if (cur_a)
 	{
 		cur_a.click();
@@ -366,7 +369,7 @@
       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');
+        var cur_a = $('#tblabelpopup li.label' + label_no + ' a');
       
         if (cur_a)
         {
@@ -375,11 +378,19 @@
       }
     });
   }
-	
+	rcmail.addEventListener('contextmenu_init', function(menu) {
+	    // identify the folder list context menu
+	    if (menu.menu_name == 'messagelist') {
+	      var mc = menu.menu_source.length;  // BREAK HERE
+	      //.push({label: 'Labels', command: 'folders', props: '', classes: 'managefolders'});    }
+	    }
+	  }
+	  );
 	// if exists add contextmenu entries
-	if (window.rcm_contextmenu_register_command) {
+	/*if (window.rcm_contextmenu_register_command) {
+              [Doesn't exist -- HST]
 		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)
@@ -399,10 +410,10 @@
 	
 	// 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('');
+		/*var button = $('<A>').attr('href', '#').attr('id', 'tblabelpopuplink').attr('title', rcmail.gettext('label', 'thunderbird_labels')).html('');
 		
 		button.bind('click', function(e) {
 			rcmail.command('plugin.thunderbird_labels.rcm_tb_label_submenu', this);
@@ -411,7 +422,7 @@
 		
 		// add and register
 		rcmail.add_element(button, 'toolbar');
-		rcmail.register_button('plugin.thunderbird_labels.rcm_tb_label_submenu', 'tb_label_popuplink', 'link');
+		rcmail.register_button('plugin.thunderbird_labels.rcm_tb_label_submenu', 'tblabelpopuplink', 'link');
 		*/
 		//rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, true);
 		rcmail.register_command('plugin.thunderbird_labels.rcm_tb_label_submenu', rcm_tb_label_submenu, rcmail.env.uid);
@@ -421,21 +432,24 @@
 		    rcmail.message_list.addEventListener('select', function(list){
 			    rcmail.enable_command('plugin.thunderbird_labels.rcm_tb_label_submenu', list.get_selection().length > 0);
 			});
+		rcmail.message_list.addEventListener('select', function(list) {
+		    rcmail.enable_command('tblabeldoit',list.get_selection().length > 0);
+		  });
 		}
-	});
+	  });
 	
 	// -- add my submenu to roundcubes UI (for roundcube classic only?)
 	if (window.rcube_mail_ui)
-	rcube_mail_ui.prototype.tb_label_popup_add = function() {
+	rcube_mail_ui.prototype.tblabelpopup_add = function() {
 		add = {
-			tb_label_popup:     {id:'tb_label_popup'}
+			tblabelpopup:     {id:'tblabelpopup'}
 		};
 		this.popups = $.extend(this.popups, add);
-		var obj = $('#'+this.popups.tb_label_popup.id);
+		var obj = $('#'+this.popups.tblabelpopup.id);
 		if (obj.length)
-			this.popups.tb_label_popup.obj = obj;
+			this.popups.tblabelpopup.obj = obj;
 		else
-			delete this.popups.tb_label_popup;
+			delete this.popups.tblabelpopup;
 	};
 	
 	if (window.rcube_mail_ui)
@@ -443,7 +457,7 @@
 		// 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)
+		if (this.popups.tblabelpopup)
 			return true;
 		else
 			return false;