From 176c76c4310bfbba64d1f1f0f8679de5dbcbe78e Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Fri, 18 Jun 2010 14:30:35 -0400
Subject: [PATCH] - unset env.messages on mailbox change to save some browser's memory

---
 program/js/app.js |  558 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 360 insertions(+), 198 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index d70c132..402e1f9 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -50,7 +50,6 @@
   this.env.request_timeout = 180;  // seconds
   this.env.draft_autosave = 0;     // seconds
   this.env.comm_path = './';
-  this.env.bin_path = './bin/';
   this.env.blankpage = 'program/blank.gif';
 
   // set jQuery ajax options
@@ -87,7 +86,7 @@
     if (sel) button_prop.sel = sel;
     if (over) button_prop.over = over;
 
-    this.buttons[command][this.buttons[command].length] = button_prop;
+    this.buttons[command].push(button_prop);
   };
 
   // register a specific gui object
@@ -121,7 +120,7 @@
   // execute the given script on load
   this.add_onload = function(f)
   {
-    this.onloads[this.onloads.length] = f;
+    this.onloads.push(f);
   };
 
   // initialize webmail client
@@ -167,7 +166,7 @@
 
           this.message_list = new rcube_list_widget(this.gui_objects.messagelist, {
             multiselect:true, multiexpand:true, draggable:true, keyboard:true,
-            column_movable:this.env.col_movable, column_fixed:0, dblclick_time:this.dblclick_time
+            column_movable:this.env.col_movable, dblclick_time:this.dblclick_time
             });
           this.message_list.row_init = function(o){ p.init_message_row(o); };
           this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
@@ -200,17 +199,17 @@
         if (this.env.trash_mailbox && this.env.mailbox != this.env.trash_mailbox)
           this.set_alttext('delete', 'movemessagetotrash');
 
+        this.env.message_commands = ['show', 'reply', 'reply-all', 'forward', 'moveto', 'copy', 'delete',
+          'open', 'mark', 'edit', 'viewsource', 'download', 'print', 'load-attachment', 'load-headers'];
+
         if (this.env.action=='show' || this.env.action=='preview') {
-          this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'copy', 'delete',
-            'open', 'mark', 'edit', 'viewsource', 'download', 'print', 'load-attachment', 'load-headers', true);
+          this.enable_command(this.env.message_commands, this.env.uid);
 
           if (this.env.next_uid) {
-            this.enable_command('nextmessage', true);
-            this.enable_command('lastmessage', true);
+            this.enable_command('nextmessage', 'lastmessage', true);
           }
           if (this.env.prev_uid) {
-            this.enable_command('previousmessage', true);
-            this.enable_command('firstmessage', true);
+            this.enable_command('previousmessage', 'firstmessage', true);
           }
 
           if (this.env.blockedobjects) {
@@ -244,7 +243,7 @@
           this.init_messageform();
         }
         // show printing dialog
-        else if (this.env.action == 'print')
+        else if (this.env.action == 'print' && this.env.uid)
           window.print();
 
         // get unread count for each mailbox
@@ -327,8 +326,7 @@
         else if (this.env.action=='folders')
           this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', 'enable-threading', 'disable-threading', true);
 
-        if (this.gui_objects.identitieslist)
-          {
+        if (this.gui_objects.identitieslist) {
           this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
           this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
           this.identity_list.init();
@@ -336,9 +334,8 @@
 
           if (this.env.iid)
             this.identity_list.highlight_row(this.env.iid);
-          }
-        else if (this.gui_objects.sectionslist)
-          {
+        }
+        else if (this.gui_objects.sectionslist) {
           this.sections_list = new rcube_list_widget(this.gui_objects.sectionslist, {multiselect:false, draggable:false, keyboard:false});
           this.sections_list.addEventListener('select', function(o){ p.section_select(o); });
           this.sections_list.init();
@@ -389,7 +386,7 @@
 
     // execute all foreign onload scripts
     // @deprecated
-    for (var i=0; i<this.onloads.length; i++) {
+    for (var i in this.onloads) {
       if (typeof(this.onloads[i]) == 'string')
         eval(this.onloads[i]);
       else if (typeof(this.onloads[i]) == 'function')
@@ -440,7 +437,8 @@
       return ret !== null ? ret : (obj ? false : true);
     }
 
-    // trigger plugin hook
+    // trigger plugin hooks
+    this.triggerEvent('actionbefore', {props:props, action:command});
     var event_ret = this.triggerEvent('before'+command, props);
     if (typeof event_ret != 'undefined') {
       // abort if one the handlers returned false
@@ -804,12 +802,12 @@
           // use contact_id passed as command parameter
           var a_cids = [];
           if (props)
-            a_cids[a_cids.length] = props;
+            a_cids.push(props);
           // get selected contacts
           else if (this.contact_list) {
             var selection = this.contact_list.get_selection();
             for (var n=0; n<selection.length; n++)
-              a_cids[a_cids.length] = selection[n];
+              a_cids.push(selection[n]);
           }
 
           if (a_cids.length)
@@ -866,7 +864,7 @@
         // all checks passed, send message
         this.set_busy(true, 'sendingmessage');
         var form = this.gui_objects.messageform;
-        form.target = "savetarget";     
+        form.target = 'savetarget';
         form._draft.value = '';
         form.submit();
 
@@ -881,7 +879,7 @@
         // Reset the auto-save timer
         self.clearTimeout(this.save_timer);
 
-        this.upload_file(props)      
+        this.upload_file(props)
         break;
 
       case 'remove-attachment':
@@ -897,7 +895,7 @@
         var uid;
         if (uid = this.get_single_uid())
           this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
-        break;      
+        break;
 
       case 'forward':
         var uid;
@@ -1046,25 +1044,30 @@
     }
 
     this.triggerEvent('after'+command, props);
+    this.triggerEvent('actionafter', {props:props, action:command});
 
     return obj ? false : true;
   };
 
-  // set command enabled or disabled
+  // set command(s) enabled or disabled
   this.enable_command = function()
   {
-    var args = arguments;
-    if (!args.length)
-      return -1;
+    var args = Array.prototype.slice.call(arguments),
+      enable = args.pop(), cmd;
 
-    var command, enable = args[args.length-1];
-
-    for (var n=0; n<args.length-1; n++) {
-      command = args[n];
-      this.commands[command] = enable;
-      this.set_button(command, (enable ? 'act' : 'pas'));
+    for (var n=0; n<args.length; n++) {
+      cmd = args[n];
+      // argument of type array
+      if (typeof cmd === 'string') {
+        this.commands[cmd] = enable;
+        this.set_button(cmd, (enable ? 'act' : 'pas'));
+      }
+      // push array elements into commands array
+      else {
+        for (var i in cmd)
+          args.push(cmd[i]);
+      }
     }
-    return true;
   };
 
   // lock/unlock interface
@@ -1072,7 +1075,7 @@
   {
     if (a && message) {
       var msg = this.get_label(message);
-      if (msg==message)        
+      if (msg == message)
         msg = 'Loading...';
 
       this.display_message(msg, 'loading', true);
@@ -1240,7 +1243,11 @@
       var boffset = bw.ie ? -document.documentElement.scrollTop : this.initialBodyScrollTop;
       var moffset = this.initialListScrollTop-this.gui_objects.folderlist.parentNode.scrollTop;
       var toffset = -moffset-boffset;
-      var li, div, pos, mouse;
+      var li, div, pos, mouse, check, oldclass,
+        layerclass = 'draglayernormal';
+      
+      if (this.contact_list && this.contact_list.draglayer)
+        oldclass = this.contact_list.draglayer.attr('class');
 
       mouse = rcube_event.get_mouse_pos(e);
       pos = this.env.folderlist_coords;
@@ -1253,6 +1260,8 @@
           this.env.folder_coords[this.env.last_folder_target].on = 0;
           this.env.last_folder_target = null;
         }
+        if (layerclass != oldclass && this.contact_list && this.contact_list.draglayer)
+          this.contact_list.draglayer.attr('class', layerclass);
         return;
       }
 
@@ -1260,7 +1269,7 @@
       for (var k in this.env.folder_coords) {
         pos = this.env.folder_coords[k];
         if (mouse.x >= pos.x1 && mouse.x < pos.x2 && mouse.y >= pos.y1 && mouse.y < pos.y2){
-         if (this.check_droptarget(k)) {
+         if ((check = this.check_droptarget(k))) {
             li = this.get_folder_li(k);
             div = $(li.getElementsByTagName('div')[0]);
 
@@ -1283,6 +1292,7 @@
             $(li).addClass('droptarget');
             this.env.folder_coords[k].on = 1;
             this.env.last_folder_target = k;
+            layerclass = 'draglayer' + (check > 1 ? 'copy' : 'normal');
           } else { // Clear target, otherwise drag end will trigger move into last valid droptarget
             this.env.last_folder_target = null;
           }
@@ -1292,6 +1302,9 @@
           this.env.folder_coords[k].on = 0;
         }
       }
+
+      if (layerclass != oldclass && this.contact_list && this.contact_list.draglayer)
+        this.contact_list.draglayer.attr('class', layerclass);
     }
   };
 
@@ -1401,9 +1414,12 @@
 
     var selected = list.get_single_selection() != null;
 
+    this.enable_command(this.env.message_commands, selected);
     // Hide certain command buttons when Drafts folder is selected
-    this.enable_command('reply', 'reply-all', 'forward', this.env.mailbox == this.env.drafts_mailbox ? false : selected);
-    this.enable_command('show', 'print', 'open', 'edit', 'download', 'viewsource', selected);
+    if (selected && this.env.mailbox == this.env.drafts_mailbox) {
+      this.enable_command('reply', 'reply-all', 'forward', false);
+    }
+    // Multi-message commands
     this.enable_command('delete', 'moveto', 'copy', 'mark', (list.selection.length > 0 ? true : false));
 
     // reset all-pages-selection
@@ -1479,37 +1495,51 @@
     if (this.env.messages[row.uid])
       this.env.messages[row.uid].expanded = row.expanded;
   };
-  
+
   this.msglist_set_coltypes = function(list)
   {
     var i, found, name, cols = list.list.tHead.rows[0].cells;
-    
+
     this.env.coltypes = [];
-    
+
     for (i=0; i<cols.length; i++)
       if (cols[i].id && cols[i].id.match(/^rcm/)) {
         name = cols[i].id.replace(/^rcm/, '');
-        this.env.coltypes[this.env.coltypes.length] = name == 'to' ? 'from' : name;
+        this.env.coltypes.push(name == 'to' ? 'from' : name);
       }
 
     if ((found = $.inArray('flag', this.env.coltypes)) >= 0)
       this.set_env('flagged_col', found);
+
+    if ((found = $.inArray('subject', this.env.coltypes)) >= 0)
+      this.set_env('subject_col', found);
 
     this.http_post('save-pref', { '_name':'list_cols', '_value':this.env.coltypes });
   };
 
   this.check_droptarget = function(id)
   {
+    var allow = false, copy = false;
+
     if (this.task == 'mail')
-      return (this.env.mailboxes[id] && this.env.mailboxes[id].id != this.env.mailbox && !this.env.mailboxes[id].virtual);
-    else if (this.task == 'addressbook')
-      return (id != this.env.source && this.env.contactfolders[id] && !this.env.contactfolders[id].readonly &&
-        !(!this.env.source && this.env.contactfolders[id].group) &&
-        !(this.env.contactfolders[id].type == 'group' && this.env.contactfolders[this.env.source].readonly) &&
-        !(this.env.contactfolders[id].type == 'group' && this.env.contactfolders[id].source != this.env.source) &&
-        !(this.env.contactfolders[id].type == 'group' && this.env.contactfolders[id].id == this.env.group));
+      allow = (this.env.mailboxes[id] && this.env.mailboxes[id].id != this.env.mailbox && !this.env.mailboxes[id].virtual);
     else if (this.task == 'settings')
-      return (id != this.env.folder);
+      allow = (id != this.env.folder);
+    else if (this.task == 'addressbook') {
+      if (id != this.env.source && this.env.contactfolders[id]) {
+        if (this.env.contactfolders[id].type == 'group') {
+          var target_abook = this.env.contactfolders[id].source;
+          allow = this.env.contactfolders[id].id != this.env.group && !this.env.contactfolders[target_abook].readonly;
+          copy = target_abook != this.env.source;
+        }
+        else {
+          allow = !this.env.contactfolders[id].readonly;
+          copy = true;
+        }
+      }
+    }
+
+    return allow ? (copy ? 2 : 1) : 0;
   };
 
 
@@ -1537,6 +1567,7 @@
     }
 
     if (!row.depth && row.has_children && (expando = document.getElementById('rcmexpando'+row.uid))) {
+      row.expando = expando;
       expando.onmousedown = function(e) { return self.expand_message_row(e, uid); };
     }
 
@@ -1548,11 +1579,6 @@
   {
     if (!this.gui_objects.messagelist || !this.message_list)
       return false;
-
-    if (this.message_list.background)
-      var tbody = this.message_list.background;
-    else
-      var tbody = this.gui_objects.messagelist.tBodies[0];
 
     if (!this.env.messages[uid])
       this.env.messages[uid] = {};
@@ -1566,14 +1592,17 @@
       flagged: flags.flagged?1:0,
       has_children: flags.has_children?1:0,
       depth: flags.depth?flags.depth:0,
-      unread_children: flags.unread_children,
-      parent_uid: flags.parent_uid,
-      selected: this.select_all_mode || this.message_list.in_selection(uid)
+      unread_children: flags.unread_children?flags.unread_children:0,
+      parent_uid: flags.parent_uid?flags.parent_uid:0,
+      selected: this.select_all_mode || this.message_list.in_selection(uid),
+      // flags from plugins
+      flags: flags.extra_flags
     });
 
     var c, tree = expando = '',
       list = this.message_list,
       rows = list.rows,
+      tbody = this.gui_objects.messagelist.tBodies[0],
       rowcount = tbody.rows.length,
       even = rowcount%2,
       message = this.env.messages[uid],
@@ -1642,11 +1671,6 @@
 
     tree += icon ? '<img id="msgicn'+uid+'" src="'+icon+'" alt="" class="msgicon" />' : '';
 
-    // first col is always there
-    col.className = 'threads';
-    col.innerHTML = expando;
-    row.appendChild(col);
-
     // build subject link 
     if (!bw.ie && cols.subject) {
       var action = flags.mbox == this.env.drafts_mailbox ? 'compose' : 'show';
@@ -1656,7 +1680,7 @@
     }
 
     // add each submitted col
-    for (var n = 0; n < this.env.coltypes.length; n++) {
+    for (var n in this.env.coltypes) {
       c = this.env.coltypes[n];
       col = document.createElement('td');
       col.className = String(c).toLowerCase();
@@ -1668,6 +1692,8 @@
         else if(!flags.flagged && this.env.unflaggedicon)
           html = '<img id="flaggedicn'+uid+'" src="'+this.env.unflaggedicon+'" class="flagicon" alt="" />';
       }
+      else if (c == 'threads')
+        html = expando;
       else if (c == 'attachment')
         html = flags.attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '&nbsp;';
       else if (c == 'subject')
@@ -1727,15 +1753,15 @@
         name = oldcols[i] == 'to' ? 'from' : oldcols[i];
         idx = $.inArray(name, cols);
         if (idx != -1) {
-          newcols[newcols.length] = name;
+          newcols.push(name);
           delete cols[idx];
         }
       }
       for (i=0; i<cols.length; i++)
         if (cols[i])
-          newcols[newcols.length] = cols[i];
+          newcols.push(cols[i]);
 
-      if (newcols.join() != this.env.coltypes.join()) {
+      if (newcols.join() != oldcols.join()) {
         update = 1;
         add_url += '&_cols=' + newcols.join(',');
       }
@@ -1870,6 +1896,7 @@
       this.env.current_page = page;
       this.select_all_mode = false;
       this.show_contentframe(false);
+      this.env.messages = {};
     }
 
     if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
@@ -1930,17 +1957,13 @@
   // expand all threads with unread children
   this.expand_unread = function()
   {
-    var r, expando,
-      tbody = this.gui_objects.messagelist.tBodies[0],
+    var r, tbody = this.gui_objects.messagelist.tBodies[0],
       new_row = tbody.firstChild;
 
     while (new_row) {
       if (new_row.nodeType == 1 && (r = this.message_list.rows[new_row.uid])
 	    && r.unread_children) {
 	    this.message_list.expand_all(r);
-	    expando = document.getElementById('rcmexpando' + r.uid);
-	    if (expando)
-	      expando.className = 'expanded';
 	    this.set_unread_children(r.uid);
       }
       new_row = new_row.nextSibling;
@@ -1971,6 +1994,99 @@
       case 2: this.expand_unread(); break;
       case 1: this.message_list.expand_all(); break;
     }
+  };
+
+  // Initializes threads indicators/expanders after list update
+  this.init_threads = function(roots)
+  {
+    for (var n=0, len=roots.length; n<len; n++)
+      this.add_tree_icons(roots[n]);
+    this.expand_threads();
+  };
+
+  // adds threads tree icons to the list (or specified thread)
+  this.add_tree_icons = function(root)
+  {
+    var i, l, r, n, len, pos, tmp = [], uid = [],
+      row, rows = this.message_list.rows;
+
+    if (root)
+      row = rows[root] ? rows[root].obj : null;
+    else
+      row = this.message_list.list.tBodies[0].firstChild;
+
+    while (row) {
+      if (row.nodeType == 1 && (r = rows[row.uid])) {
+        if (r.depth) {
+          for (i=tmp.length-1; i>=0; i--) {
+            len = tmp[i].length;
+            if (len > r.depth) {
+              pos = len - r.depth;
+              if (!(tmp[i][pos] & 2))
+                tmp[i][pos] = tmp[i][pos] ? tmp[i][pos]+2 : 2;
+            }
+            else if (len == r.depth) {
+              if (!(tmp[i][0] & 2))
+                tmp[i][0] += 2;
+            }
+            if (r.depth > len)
+              break;
+          }
+
+          tmp.push(new Array(r.depth));
+          tmp[tmp.length-1][0] = 1;
+          uid.push(r.uid);
+        }
+        else {
+          if (tmp.length) {
+            for (i in tmp) {
+              this.set_tree_icons(uid[i], tmp[i]);
+            }
+            tmp = [];
+            uid = [];
+          }
+          if (root && row != rows[root].obj)
+            break;
+        }
+      }
+      row = row.nextSibling;
+    }
+
+    if (tmp.length) {
+      for (i in tmp) {
+        this.set_tree_icons(uid[i], tmp[i]);
+      }
+    }
+  };
+
+  // adds tree icons to specified message row
+  this.set_tree_icons = function(uid, tree)
+  {
+    var i, divs = [], html = '', len = tree.length;
+
+    for (i=0; i<len; i++) {
+      if (tree[i] > 2)
+        divs.push({'class': 'l3', width: 15});
+      else if (tree[i] > 1)
+        divs.push({'class': 'l2', width: 15});
+      else if (tree[i] > 0)
+        divs.push({'class': 'l1', width: 15});
+      // separator div
+      else if (divs.length && !divs[divs.length-1]['class'])
+        divs[divs.length-1].width += 15;
+      else
+        divs.push({'class': null, width: 15});
+    }
+
+    for (i=divs.length-1; i>=0; i--) {
+      if (divs[i]['class'])
+        html += '<div class="tree '+divs[i]['class']+'" />';
+      else
+        html += '<div style="width:'+divs[i].width+'px" />';
+    }
+
+    if (html)
+      $('#rcmtab'+uid).html(html);
   };
 
   // update parent in a thread
@@ -2033,7 +2149,8 @@
 	      break;
 
 	    r.depth--; // move left
-	    $('#rcmtab'+r.uid).width(r.depth * 15);
+        // reset width and clear the content of a tab, icons will be added later
+	    $('#rcmtab'+r.uid).width(r.depth * 15).html('');
         if (!r.depth) { // a new root
 	      count++; // increase roots count
 	      r.parent_uid = 0;
@@ -2046,7 +2163,7 @@
 	            function(e) { return e.data.p.expand_message_row(e, e.data.uid); });
 
 	        r.unread_children = 0;
-	        roots[roots.length] = r;
+	        roots.push(r);
 	      }
 	      // show if it was hidden
 	      if (r.obj.style.display == 'none')
@@ -2144,8 +2261,6 @@
       rows[uid].forwarded = status;
     else if (flag == 'flagged')
       rows[uid].flagged = status;
-
-//    this.env.messages[uid] = rows[uid];
   };
 
   // set message row status, class and icon
@@ -2184,7 +2299,7 @@
   {
     var row = this.message_list.rows[uid];
 
-    if (row.parent_uid || !row.has_children)
+    if (row.parent_uid)
       return;
 
     if (!row.unread && row.unread_children && !row.expanded)
@@ -2203,17 +2318,15 @@
     if (!mbox || mbox == this.env.mailbox || (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length)))
       return;
 
-    var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
-    var a_uids = [];
+    var a_uids = [],
+      add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
 
     if (this.env.uid)
       a_uids[0] = this.env.uid;
     else {
       var selection = this.message_list.get_selection();
-      var id;
-      for (var n=0; n<selection.length; n++) {
-        id = selection[n];
-        a_uids[a_uids.length] = id;
+      for (var n in selection) {
+        a_uids.push(selection[n]);
       }
     }
 
@@ -2243,7 +2356,7 @@
       this.show_contentframe(false);
 
     // Hide message command buttons until a message is selected
-    this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', 'open', 'edit', 'viewsource', 'download', false);
+    this.enable_command(this.env.message_commands, false);
 
     this._with_selected_messages('moveto', lock, add_url);
   };
@@ -2258,7 +2371,7 @@
       return;
 
     // also select childs of collapsed rows
-    for (var uid, i=0; i < selection.length; i++) {
+    for (var uid, i=0, len=selection.length; i<len; i++) {
       uid = selection[i];
       if (this.message_list.rows[uid].has_children && !this.message_list.rows[uid].expanded)
         this.message_list.select_childs(uid);
@@ -2306,17 +2419,29 @@
     if (this.env.uid)
       a_uids[0] = this.env.uid;
     else {
-      var selection = this.message_list.get_selection();
-      var id;
-      for (var n=0; n<selection.length; n++) {
+      var n, id, root, roots = [],
+        selection = this.message_list.get_selection();
+
+      for (n=0, len=selection.length; n<len; n++) {
         id = selection[n];
-        a_uids[a_uids.length] = id;
-        count += this.update_thread(id);
+        a_uids.push(id);
+
+        if (this.env.threading) {
+          count += this.update_thread(id);
+          root = this.message_list.find_root(id);
+          if (root != id && $.inArray(root, roots) < 0) {
+            roots.push(root);
+          }
+        }
         this.message_list.remove_row(id, (this.env.display_next && n == selection.length-1));
       }
       // make sure there are no selected rows
       if (!this.env.display_next)
         this.message_list.clear_selection();
+      // update thread tree icons
+      for (n=0, len=roots.length; n<len; n++) {
+        this.add_tree_icons(roots[n]);
+      }
     }
 
     // also send search request to get the right messages 
@@ -2341,7 +2466,7 @@
   // set a specific flag to one or more messages
   this.mark_message = function(flag, uid)
   {
-    var a_uids = [], r_uids = [],
+    var a_uids = [], r_uids = [], len, n, id,
       selection = this.message_list ? this.message_list.get_selection() : [];
 
     if (uid)
@@ -2349,15 +2474,15 @@
     else if (this.env.uid)
       a_uids[0] = this.env.uid;
     else if (this.message_list) {
-      for (var n=0; n<selection.length; n++) {
-          a_uids[a_uids.length] = selection[n];
+      for (n=0, len=selection.length; n<len; n++) {
+          a_uids.push(selection[n]);
       }
     }
 
     if (!this.message_list)
       r_uids = a_uids;
     else
-      for (var id, n=0; n<a_uids.length; n++) {
+      for (n=0, len=a_uids.length; n<len; n++) {
         id = a_uids[n];
         if ((flag=='read' && this.message_list.rows[id].unread) 
             || (flag=='unread' && !this.message_list.rows[id].unread)
@@ -2366,7 +2491,7 @@
             || (flag=='flagged' && !this.message_list.rows[id].flagged)
             || (flag=='unflagged' && this.message_list.rows[id].flagged))
         {
-          r_uids[r_uids.length] = id;
+          r_uids.push(id);
         }
       }
 
@@ -2439,8 +2564,8 @@
       return true;
     }
 
-    var all_deleted = true;
-    for (var uid, i=0; i<a_uids.length; i++) {
+    var uid, all_deleted = true;
+    for (var i=0, len=a_uids.length; i<len; i++) {
       uid = a_uids[i];
       if (rows[uid] && !rows[uid].deleted) {
         all_deleted = false;
@@ -2458,7 +2583,7 @@
 
   this.flag_as_undeleted = function(a_uids)
   {
-    for (var i=0; i<a_uids.length; i++)
+    for (var i=0, len=a_uids.length; i<len; i++)
       this.set_message(a_uids[i], 'deleted', false);
 
     var url = '_uid='+this.uids_to_list(a_uids)+'&_flag=undelete';
@@ -2478,7 +2603,7 @@
       rows = this.message_list ? this.message_list.rows : [],
       count = 0;
 
-    for (var i=0; i<a_uids.length; i++) {
+    for (var i=0, len=a_uids.length; i<len; i++) {
       uid = a_uids[i];
       if (rows[uid]) {
         if (rows[uid].unread)
@@ -2592,8 +2717,8 @@
   // test if purge command is allowed
   this.purge_mailbox_test = function()
   {
-    return (this.env.messagecount && (this.env.mailbox == this.env.trash_mailbox || this.env.mailbox == this.env.junk_mailbox 
-      || this.env.mailbox.match('^' + RegExp.escape(this.env.trash_mailbox) + RegExp.escape(this.env.delimiter)) 
+    return (this.env.messagecount && (this.env.mailbox == this.env.trash_mailbox || this.env.mailbox == this.env.junk_mailbox
+      || this.env.mailbox.match('^' + RegExp.escape(this.env.trash_mailbox) + RegExp.escape(this.env.delimiter))
       || this.env.mailbox.match('^' + RegExp.escape(this.env.junk_mailbox) + RegExp.escape(this.env.delimiter))));
   };
 
@@ -3005,20 +3130,20 @@
       }
 
       // handle upload errors, parsing iframe content in onload
-      var fr = document.getElementsByName(frame_name)[0];
-      $(fr).bind('load', {ts:ts}, function(e) {
-        var content = '';
+      $(frame_name).bind('load', {ts:ts}, function(e) {
+        var d, content = '';
         try {
           if (this.contentDocument) {
-            var d = this.contentDocument;
+            d = this.contentDocument;
           } else if (this.contentWindow) {
-            var d = this.contentWindow.document;
+            d = this.contentWindow.document;
           }
           content = d.childNodes[0].innerHTML;
         } catch (e) {}
 
-        if (!String(content).match(/add2attachment/) && (!bw.opera || (rcmail.env.uploadframe && rcmail.env.uploadframe == e.data.ts))) {
-          rcmail.display_message(rcmail.get_label('fileuploaderror'), 'error');
+        if (!content.match(/add2attachment/) && (!bw.opera || (rcmail.env.uploadframe && rcmail.env.uploadframe == e.data.ts))) {
+          if (!content.match(/display_message/))
+            rcmail.display_message(rcmail.get_label('fileuploaderror'), 'error');
           rcmail.remove_from_attachment_list(e.data.ts);
         }
         // Opera hack: handle double onload
@@ -3352,7 +3477,7 @@
       for (i=0; i < a_results.length; i++) {
         text = typeof a_results[i] == 'object' ? a_results[i].name : a_results[i];
         li = document.createElement('LI');
-        li.innerHTML = text.replace(new RegExp('('+s_val+')', 'ig'), '##$1%%').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/##([^%]+)%%/g, '<b>$1</b>');
+        li.innerHTML = text.replace(new RegExp('('+RegExp.escape(s_val)+')', 'ig'), '##$1%%').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/##([^%]+)%%/g, '<b>$1</b>');
         li.onmouseover = function(){ ref.ksearch_select(this); };
         li.onmouseup = function(){ ref.ksearch_click(this) };
         li._rcm_id = i;
@@ -3530,10 +3655,24 @@
     if (!cid)
       cid = this.contact_list.get_selection().join(',');
 
-    if (to.type == 'group')
-      this.http_post('group-addmembers', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_gid='+urlencode(to.id));
-    else if (to.id != this.env.source && cid && this.env.address_sources[to.id] && !this.env.address_sources[to.id].readonly)
-      this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to.id)+(this.env.group ? '&_gid='+urlencode(this.env.group) : ''));
+    if (to.type == 'group' && to.source == this.env.source) {
+      this.http_post('group-addmembers', '_cid='+urlencode(cid)
+        + '&_source='+urlencode(this.env.source)
+        + '&_gid='+urlencode(to.id));
+    }
+    else if (to.type == 'group' && !this.env.address_sources[to.source].readonly) {
+      this.http_post('copy', '_cid='+urlencode(cid)
+        + '&_source='+urlencode(this.env.source)
+        + '&_to='+urlencode(to.source)
+        + '&_togid='+urlencode(to.id)
+        + (this.env.group ? '&_gid='+urlencode(this.env.group) : ''));
+    }
+    else if (to.id != this.env.source && cid && this.env.address_sources[to.id] && !this.env.address_sources[to.id].readonly) {
+      this.http_post('copy', '_cid='+urlencode(cid)
+        + '&_source='+urlencode(this.env.source)
+        + '&_to='+urlencode(to.id)
+        + (this.env.group ? '&_gid='+urlencode(this.env.group) : ''));
+    }
   };
 
 
@@ -3544,15 +3683,14 @@
     if (!(selection.length || this.env.cid) || (!this.env.group && !confirm(this.get_label('deletecontactconfirm'))))
       return;
 
-    var a_cids = [], qs = '';
+    var id, a_cids = [], qs = '';
 
     if (this.env.cid)
-      a_cids[a_cids.length] = this.env.cid;
+      a_cids.push(this.env.cid);
     else {
-      var id;
       for (var n=0; n<selection.length; n++) {
         id = selection[n];
-        a_cids[a_cids.length] = id;
+        a_cids.push(id);
         this.contact_list.remove_row(id, (n == selection.length-1));
       }
 
@@ -3635,12 +3773,12 @@
       return;
 
     if (!this.name_input) {
-      this.name_input = document.createElement('input');
-      this.name_input.type = 'text';
-      this.name_input.onkeypress = function(e){ return rcmail.add_input_keypress(e); };
+      this.name_input = $('<input>').attr('type', 'text');
+      this.name_input.bind('keypress', function(e){ return rcmail.add_input_keypress(e); });
+      this.name_input_li = $('<li>').addClass('contactgroup').append(this.name_input);
 
       var li = this.get_folder_li(this.env.source)
-      $(this.name_input).insertAfter(li);
+      this.name_input_li.insertAfter(li);
     }
 
     this.name_input.select();
@@ -3653,16 +3791,13 @@
 
     if (!this.name_input) {
       this.enable_command('list', 'listgroup', false);
-      this.name_input = document.createElement('input');
-      this.name_input.type = 'text';
-      this.name_input.value = this.env.contactgroups['G'+this.env.source+this.env.group].name;
-      this.name_input.onkeypress = function(e){ return rcmail.add_input_keypress(e); };
+      this.name_input = $('<input>').attr('type', 'text').val(this.env.contactgroups['G'+this.env.source+this.env.group].name);
+      this.name_input.bind('keypress', function(e){ return rcmail.add_input_keypress(e); });
       this.env.group_renaming = true;
 
-      var li = this.get_folder_li(this.env.source+this.env.group, 'rcmliG');
+      var link, li = this.get_folder_li(this.env.source+this.env.group, 'rcmliG');
       if (li && (link = li.firstChild)) {
-        $(link).hide();
-        li.insertBefore(this.name_input, link);
+        $(link).hide().before(this.name_input);
       }
     }
 
@@ -3680,7 +3815,7 @@
   {
     var li, key = 'G'+prop.source+prop.id;
     if ((li = this.get_folder_li(key))) {
-      this.triggerEvent('removegroup', { source:prop.source, id:prop.id, li:li });
+      this.triggerEvent('group_delete', { source:prop.source, id:prop.id, li:li });
 
       li.parentNode.removeChild(li);
       delete this.env.contactfolders[key];
@@ -3697,7 +3832,7 @@
 
     // enter
     if (key == 13) {
-      var newname = this.name_input.value;
+      var newname = this.name_input.val();
 
       if (newname) {
         this.set_busy(true, 'loading');
@@ -3719,13 +3854,17 @@
   {
     if (this.name_input) {
       if (this.env.group_renaming) {
-        var li = this.name_input.parentNode;
-        $(li.lastChild).show();
+        var li = this.name_input.parent();
+        li.children().last().show();
         this.env.group_renaming = false;
       }
 
-      this.name_input.parentNode.removeChild(this.name_input);
-      this.name_input = null;
+      this.name_input.remove();
+
+      if (this.name_input_li)
+        this.name_input_li.remove();
+
+      this.name_input = this.name_input_li = null;
     }
 
     this.enable_command('list', 'listgroup', true);
@@ -3743,11 +3882,12 @@
     var link = $('<a>').attr('href', '#')
       .bind('click', function() { return rcmail.command('listgroup', prop, this);})
       .html(prop.name);
-    var li = $('<li>').attr('id', 'rcmli'+key).addClass('contactgroup').append(link);
-    var pli = this.get_folder_li(prop.source)
-    $(li).insertAfter(pli);
+    var li = $('<li>').attr('id', 'rcmli'+key)
+      .addClass('contactgroup')
+      .append(link)
+      .insertAfter(this.get_folder_li(prop.source));
 
-    this.triggerEvent('insertgroup', { id:prop.id, source:prop.source, name:prop.name, li:li[0] });
+    this.triggerEvent('group_insert', { id:prop.id, source:prop.source, name:prop.name, li:li[0] });
   };
 
   // callback for renaming a contact group
@@ -3761,7 +3901,7 @@
       link.innerHTML = prop.name;
 
     this.env.contactfolders[key].name = this.env.contactgroups[key].name = name;
-    this.triggerEvent('updategroup', { id:prop.id, source:prop.source, name:prop.name, li:li[0] });
+    this.triggerEvent('group_update', { id:prop.id, source:prop.source, name:prop.name, li:li[0] });
   };
 
 
@@ -4212,10 +4352,8 @@
   // enable/disable buttons for page shifting
   this.set_page_buttons = function()
   {
-    this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
-    this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
-    this.enable_command('previouspage', (this.env.current_page > 1));
-    this.enable_command('firstpage', (this.env.current_page > 1));
+    this.enable_command('nextpage', 'lastpage', (this.env.pagecount > this.env.current_page));
+    this.enable_command('previouspage', 'firstpage', (this.env.current_page > 1));
   };
 
   // set event handlers on registered buttons
@@ -4464,58 +4602,54 @@
   // for reordering column array (Konqueror workaround)
   // and for setting some message list global variables
   this.set_message_coltypes = function(coltypes, repl)
-  { 
+  {
     this.env.coltypes = coltypes;
 
     // set correct list titles
-    var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
+    var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null,
+      cell, col, n, len;
 
     // replace old column headers
-    if (thead && repl) {
-      for (var cell, c=0; c < repl.length; c++) {
-        cell = thead.rows[0].cells[c];
-        if (!cell) {
+    if (thead) {
+      if (repl) {
+        var th = document.createElement('thead'),
+          tr = document.createElement('tr');
+        for (c=0, len=repl.length; c < len; c++) {
           cell = document.createElement('td');
-          thead.rows[0].appendChild(cell);
+          cell.innerHTML = repl[c].html;
+          if (repl[c].id) cell.id = repl[c].id;
+          if (repl[c].className) cell.className = repl[c].className;
+          tr.appendChild(cell);
         }
-        cell.innerHTML = repl[c].html;
-        if (repl[c].id) cell.id = repl[c].id;
-        if (repl[c].className) cell.className = repl[c].className;
+        th.appendChild(tr);
+        thead.parentNode.replaceChild(th, thead);
+      }
+
+      for (n=0, len=this.env.coltypes.length; n<len; n++) {
+        col = this.env.coltypes[n];
+        if ((cell = thead.rows[0].cells[n]) && (col=='from' || col=='to')) {
+          cell.id = 'rcm'+col;
+          // if we have links for sorting, it's a bit more complicated...
+          if (cell.firstChild && cell.firstChild.tagName.toLowerCase()=='a') {
+            cell = cell.firstChild;
+            cell.onclick = function(){ return rcmail.command('sort', this.__col, this); };
+            cell.__col = col;
+          }
+          cell.innerHTML = this.get_label(col);
+        }
       }
     }
-
-    var cell, col, n;
-    for (n=0; thead && n<this.env.coltypes.length; n++) {
-      col = this.env.coltypes[n];
-      if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to')) {
-        // if we have links for sorting, it's a bit more complicated...
-        if (cell.firstChild && cell.firstChild.tagName.toLowerCase()=='a') {
-          cell.firstChild.innerHTML = this.get_label(this.env.coltypes[n]);
-          cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
-          cell.firstChild.__col = col;
-        }
-        else
-          cell.innerHTML = this.get_label(this.env.coltypes[n]);
-
-        cell.id = 'rcm'+col;
-      }
-    }
-
-    // remove excessive columns
-    for (var i=n+1; thead && i<thead.rows[0].cells.length; i++)
-      thead.rows[0].removeChild(thead.rows[0].cells[i]);
 
     this.env.subject_col = null;
     this.env.flagged_col = null;
 
-    var found;
-    if ((found = $.inArray('subject', this.env.coltypes)) >= 0) {
-      this.set_env('subject_col', found);
+    if ((n = $.inArray('subject', this.env.coltypes)) >= 0) {
+      this.set_env('subject_col', n);
       if (this.message_list)
-        this.message_list.subject_col = found+1;
+        this.message_list.subject_col = n;
     }
-    if ((found = $.inArray('flag', this.env.coltypes)) >= 0)
-      this.set_env('flagged_col', found);
+    if ((n = $.inArray('flag', this.env.coltypes)) >= 0)
+      this.set_env('flagged_col', n);
 
     this.message_list.init_header();
   };
@@ -4729,8 +4863,7 @@
     }
 
     // replace quota image
-    obj.innerHTML = '';
-    $(obj).append(bar1).append(bar2).append(main);
+    $(obj).html('').append(bar1).append(bar2).append(main);
   };
 
   /********************************************************/
@@ -4740,7 +4873,7 @@
   this.html2plain = function(htmlText, id)
   {
     var rcmail = this,
-      url = this.env.bin_path + 'html2text.php';
+      url = '?_task=utils&_action=html2text';
 
     this.set_busy(true, 'converting');
     console.log('HTTP POST: ' + url);
@@ -4783,8 +4916,18 @@
   // send a http request to the server
   this.http_request = function(action, querystring, lock)
   {
+    // trigger plugin hook
+    var result = this.triggerEvent('request'+action, querystring);
+    if (typeof result != 'undefined') {
+      // abort if one the handlers returned false
+      if (result === false)
+        return false;
+      else
+        querystring = result;
+    }
+
     querystring += (querystring ? '&' : '') + '_remote=1';
-    var url = this.env.comm_path + '&_action=' + action + '&' + querystring
+    var url = this.env.comm_path + '&_action=' + action + '&' + querystring;
 
     // send request
     console.log('HTTP GET: ' + url);
@@ -4803,6 +4946,16 @@
     else
       postdata += (postdata ? '&' : '') + '_remote=1' + (lock ? '&_unlock=1' : '');
 
+    // trigger plugin hook
+    var result = this.triggerEvent('request'+action, postdata);
+    if (typeof result != 'undefined') {
+      // abort if one the handlers returned false
+      if (result === false)
+        return false;
+      else
+        postdata = result;
+    }
+
     // send request
     console.log('HTTP POST: ' + url);
     $.post(url, postdata, function(data){ ref.http_response(data); }, 'json');
@@ -4813,6 +4966,9 @@
   {
     if (response.unlock)
       this.set_busy(false);
+
+    this.triggerEvent('responsebefore', {response: response});
+    this.triggerEvent('responsebefore'+response.action, {response: response});
 
     // set env vars
     if (response.env)
@@ -4850,21 +5006,25 @@
       case 'moveto':
         if (this.env.action == 'show') {
           // re-enable commands on move/delete error
-          this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', 'open', 'edit', 'viewsource', 'download', true);
+          this.enable_command(this.env.message_commands, true);
         }
-        break;
+        else if (this.task == 'addressbook') {
+          this.triggerEvent('listupdate', { folder:this.env.source, rowcount:this.contact_list.rowcount });
+        }
 
       case 'purge':
       case 'expunge':
-        if (!this.env.messagecount && this.task == 'mail') {
-          // clear preview pane content
-          if (this.env.contentframe)
-            this.show_contentframe(false);
-          // disable commands useless when mailbox is empty
-          this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'copy', 'delete', 
-            'mark', 'viewsource', 'open', 'edit', 'download', 'print', 'load-attachment', 
-            'purge', 'expunge', 'select-all', 'select-none', 'sort',
-            'expand-all', 'expand-unread', 'collapse-all', false);
+        if (this.task == 'mail') {
+          if (!this.env.messagecount) {
+            // clear preview pane content
+            if (this.env.contentframe)
+              this.show_contentframe(false);
+            // disable commands useless when mailbox is empty
+            this.enable_command(this.env.message_commands, 'purge', 'expunge',
+              'select-all', 'select-none', 'sort', 'expand-all', 'expand-unread', 'collapse-all', false);
+          }
+          if (this.message_list)
+            this.triggerEvent('listupdate', { folder:this.env.mailbox, rowcount:this.message_list.rowcount });
         }
         break;
 
@@ -4879,7 +5039,6 @@
 
           if (response.action == 'list' || response.action == 'search') {
             this.msglist_select(this.message_list);
-            this.expand_threads();
             this.triggerEvent('listupdate', { folder:this.env.mailbox, rowcount:this.message_list.rowcount });
           }
         }
@@ -4896,6 +5055,9 @@
         }
         break;
     }
+
+    this.triggerEvent('responseafter', {response: response});
+    this.triggerEvent('responseafter'+response.action, {response: response});
   };
 
   // handle HTTP request errors
@@ -5018,7 +5180,7 @@
       return;
 
     var type;
-    for (var n=0; n<form.elements.length; n++) {
+    for (var n=0, len=form.elements.length; n<len; n++) {
       type = form.elements[n];
       if (type == 'hidden')
         continue;

--
Gitblit v1.9.1