From 89e50739b4ea47ef21e5d4864b7101585a94bea8 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <machniak@kolabsys.com>
Date: Fri, 25 Oct 2013 13:27:49 -0400
Subject: [PATCH] Refactored status/flag toggle code, added touch event support on flag and status icons. Fixed regression in commit 4e4c2511bc00cfc0 where click on flag/status/expando icons was selecting the message row.

---
 program/js/list.js |    5 +-
 program/js/app.js  |   62 +++++++++++++------------------
 2 files changed, 29 insertions(+), 38 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index ad00154..bfae977 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -862,37 +862,24 @@
         break;
 
       case 'toggle_status':
-        if (props && !props._row)
-          break;
+      case 'toggle_flag':
+        flag = command == 'toggle_flag' ? 'flagged' : 'read';
 
-        flag = 'read';
-
-        if (props._row.uid) {
-          uid = props._row.uid;
-
+        if (uid = props) {
+          // toggle flagged/unflagged
+          if (flag == 'flagged') {
+            if (this.message_list.rows[uid].flagged)
+              flag = 'unflagged';
+          }
           // toggle read/unread
-          if (this.message_list.rows[uid].deleted)
+          else if (this.message_list.rows[uid].deleted)
             flag = 'undelete';
           else if (!this.message_list.rows[uid].unread)
             flag = 'unread';
+
+          this.mark_message(flag, uid);
         }
 
-        this.mark_message(flag, uid);
-        break;
-
-      case 'toggle_flag':
-        if (props && !props._row)
-          break;
-
-        flag = 'flagged';
-
-        if (props._row.uid) {
-          uid = props._row.uid;
-          // toggle flagged/unflagged
-          if (this.message_list.rows[uid].flagged)
-            flag = 'unflagged';
-        }
-        this.mark_message(flag, uid);
         break;
 
       case 'always-load':
@@ -1752,7 +1739,7 @@
 
   this.init_message_row = function(row)
   {
-    var expando, self = this, uid = row.uid,
+    var i, fn = {}, self = this, uid = row.uid,
       status_icon = (this.env.status_col != null ? 'status' : 'msg') + 'icn' + row.uid;
 
     if (uid && this.env.messages[uid])
@@ -1760,8 +1747,7 @@
 
     // set eventhandler to status icon
     if (row.icon = document.getElementById(status_icon)) {
-      row.icon._row = row.obj;
-      row.icon.onclick = function(e) { self.command('toggle_status', this); return rcube_event.cancel(e); };
+      fn.icon = function(e) { self.command('toggle_status', uid); };
     }
 
     // save message icon position too
@@ -1770,24 +1756,28 @@
     else
       row.msgicon = row.icon;
 
-    // set eventhandler to flag icon, if icon found
+    // set eventhandler to flag icon
     if (this.env.flagged_col != null && (row.flagicon = document.getElementById('flagicn'+row.uid))) {
-      row.flagicon._row = row.obj;
-      row.flagicon.onclick = function(e) { self.command('toggle_flag', this); return rcube_event.cancel(e); };
+      fn.flagicon = function(e) { self.command('toggle_flag', uid); };
     }
 
-    if (!row.depth && row.has_children && (expando = document.getElementById('rcmexpando'+row.uid))) {
-      row.expando = expando;
-      expando.onclick = function(e) { return self.expand_message_row(e, uid); };
+    // set event handler to thread expand/collapse icon
+    if (!row.depth && row.has_children && (row.expando = document.getElementById('rcmexpando'+row.uid))) {
+      fn.expando = function(e) { self.expand_message_row(e, uid); };
+    }
+
+    // attach events
+    $.each(fn, function(i, f) {
+      row[i].onclick = function(e) { f(e); return rcube_event.cancel(e); };
       if (bw.touch) {
-        expando.addEventListener('touchend', function(e) {
+        row[i].addEventListener('touchend', function(e) {
           if (e.changedTouches.length == 1) {
-            self.expand_message_row(e, uid);
+            f(e);
             return rcube_event.cancel(e);
           }
         }, false);
       }
-    }
+    });
 
     this.triggerEvent('insertrow', { uid:uid, row:row });
   };
diff --git a/program/js/list.js b/program/js/list.js
index 33f88ab..0b6f416 100644
--- a/program/js/list.js
+++ b/program/js/list.js
@@ -457,10 +457,11 @@
   var dblclicked = now - this.rows[id].clicked < this.dblclick_time;
 
   // selects/unselects currently selected row
-  if (!this.drag_active && !dblclicked)
+  if (!this.drag_active && this.in_selection_before == id && !dblclicked)
     this.select_row(id, mod_key, true);
-    
+
   this.drag_start = false;
+  this.in_selection_before = false;
 
   // row was double clicked
   if (this.rowcount && dblclicked && this.in_selection(id)) {

--
Gitblit v1.9.1