From 1c7b97e81bea919c26bfe878312c5118c02ac0a9 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Tue, 13 Mar 2007 19:17:20 -0400
Subject: [PATCH] Fixed flag for deletion bug #1484264 + code style

---
 program/js/app.js |  235 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 135 insertions(+), 100 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index 4bb261c..c19a3e1 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -28,9 +28,10 @@
   this.commands = new Object();
   this.onloads = new Array();
 
-  // create public reference to myself
+  // create protected reference to myself
   rcube_webmail_client = this;
   this.ref = 'rcube_webmail_client';
+  var ref = this;
  
   // webmail client settings
   this.dblclick_time = 500;
@@ -315,9 +316,9 @@
   this.start_keepalive = function()
     {
     if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.messagelist)
-      this._int = setInterval(this.ref+'.check_for_recent()', this.env.keep_alive * 1000);
+      this._int = setInterval(function(){ ref.check_for_recent(); }, this.env.keep_alive * 1000);
     else if (this.env.keep_alive && !this.env.framed && this.task!='login')
-      this._int = setInterval(this.ref+'.send_keep_alive()', this.env.keep_alive * 1000);    
+      this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);    
     }
 
 
@@ -669,10 +670,12 @@
         // open attachment in frame if it's of a supported mimetype
         if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
           {
+          if (props.mimetype == 'text/html')
+            qstring += '&_safe=1';
           this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
           if (this.attachment_win)
             {
-            setTimeout(this.ref+'.attachment_win.focus()', 10);
+            setTimeout(function(){ ref.attachment_win.focus(); }, 10);
             break;
             }
           }
@@ -849,19 +852,19 @@
         var uid;
         if (uid = this.get_single_uid())
           {
-          this.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
+          ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
           if (this.printwin)
-            setTimeout(this.ref+'.printwin.focus()', 20);
+            setTimeout(function(){ ref.printwin.focus(); }, 20);
           }
         break;
 
       case 'viewsource':
         var uid;
         if (uid = this.get_single_uid())
-          {          
-          this.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox));
+          {
+          ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox));
           if (this.sourcewin)
-            setTimeout(this.ref+'.sourcewin.focus()', 20);
+            setTimeout(function(){ ref.sourcewin.focus(); }, 20);
           }
         break;
 
@@ -983,7 +986,7 @@
 
     // set timer for requests
     if (a && this.env.request_timeout)
-      this.request_timer = setTimeout(this.ref+'.request_timed_out()', this.env.request_timeout * 1000);
+      this.request_timer = setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
     };
 
 
@@ -1088,13 +1091,13 @@
       {
       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
       this.enable_command('delete', 'moveto', list.selection.length>0 ? true : false);
-
-      // start timer for message preview (wait for double click)
-      if (selected && this.env.contentframe)
-        this.preview_timer = setTimeout(this.ref+'.msglist_get_preview()', this.dblclick_time + 10);
-      else if (this.env.contentframe)
-        this.show_messageframe(false);
       }
+
+    // start timer for message preview (wait for double click)
+    if (selected && this.env.contentframe)
+      this.preview_timer = setTimeout(function(){ ref.msglist_get_preview(); }, this.dblclick_time + 10);
+    else if (this.env.contentframe)
+      this.show_messageframe(false);
     };
 
 
@@ -1223,7 +1226,8 @@
       page = 1;
       add_url += '&_refresh=1';
       this.env.current_page = page;
-      this.message_list.clear_selection();
+      if (this.message_list)
+        this.message_list.clear_selection();
       this.show_messageframe(false);
       }
     
@@ -1333,24 +1337,8 @@
         return;
       }
 
-    var a_uids = new Array();
-
-    if (this.env.uid)
-      a_uids[a_uids.length] = this.env.uid;
-    else
-      {
-      var selection = this.message_list.get_selection();
-      for (var n=0, id; n<selection.length; n++)
-        {
-        id = selection[n];
-        a_uids[a_uids.length] = id;
-        this.message_list.remove_row(id);
-        }
-
-        this.show_messageframe(false);
-      }
-
     var lock = false;
+    var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
 
     // show wait message
     if (this.env.action=='show')
@@ -1358,22 +1346,67 @@
       lock = true;
       this.set_busy(true, 'movingmessage');
       }
+    else
+      this.show_messageframe(false);
 
-    // send request to server
-    this.http_request('moveto', '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+'&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : ''), lock);
+    this._with_selected_messages('moveto', lock, add_url);
     };
 
+  // delete selected messages from the current mailbox
+  this.delete_messages = function()
+    {
+    var selection = this.message_list ? this.message_list.get_selection() : new Array();
+    
+    // exit if no mailbox specified or if selection is empty
+    if (!this.env.uid && !selection.length)
+        return;
+
+    // if there is a trash mailbox defined and we're not currently in it:
+    if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() != String(this.env.trash_mailbox).toLowerCase())
+      {
+      // if shift was pressed delete it immediately
+      if (this.message_list && this.message_list.shiftkey)
+        {
+        if (confirm(this.get_label('deletemessagesconfirm')))
+          this.permanently_remove_messages();
+        }
+      else
+        this.move_messages(this.env.trash_mailbox);
+      }
+    // if there is a trash mailbox defined but we *are* in it:
+    else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
+      this.permanently_remove_messages();
+    // if there isn't a defined trash mailbox and the config is set to flag for deletion
+    else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
+      {
+      this.mark_message('delete');
+      if(this.env.action=="show")
+        this.command('nextmessage','',this);
+      else if (selection.length == 1)
+        this.message_list.select_next();
+      }
+    // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
+    else if (!this.env.trash_mailbox) 
+      this.permanently_remove_messages();
+  };
+
+
+  // delete the selected messages permanently
   this.permanently_remove_messages = function()
     {
     // exit if no mailbox specified or if selection is empty
-    if (!this.env.uid)
-      {
-      if (!this.message_list || !this.message_list.get_selection().length)
-        return;
-      }
+    if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
+      return;
+      
+    this.show_messageframe(false);
+    this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
+    };
 
+  // Send a specifc request with UIDs of all selected messages
+  // @private
+  this._with_selected_messages = function(action, lock, add_url)
+    {
     var a_uids = new Array();
-
     if (this.env.uid)
       a_uids[a_uids.length] = this.env.uid;
     else
@@ -1389,49 +1422,14 @@
 
       this.message_list.select_next();
       }
-      
-    // send request to server
-    this.http_request('delete', '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+'&_from='+(this.env.action ? this.env.action : ''));
-  }
-    
-    
-  // delete selected messages from the current mailbox
-  this.delete_messages = function()
-    {
-    // exit if no mailbox specified or if selection is empty
-    if (!this.env.uid)
-      {
-      if (!this.message_list || !this.message_list.get_selection().length)
-        return;
-      }
 
-    // if there is a trash mailbox defined and we're not currently in it:
-    if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase()!=String(this.env.trash_mailbox).toLowerCase())
-      // if shift was pressed delete it immediately
-      if (this.message_list && this.message_list.shiftkey)
-        {
-        if (confirm(this.get_label('deletemessagesconfirm')))
-          this.permanently_remove_messages();
-        }
-      else
-        this.move_messages(this.env.trash_mailbox);
-    // if there is a trash mailbox defined but we *are* in it:
-    else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
-      this.permanently_remove_messages();
-    // if there isn't a defined trash mailbox and the config is set to flag for deletion
-    else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
-      {
-      flag = 'delete';
-      this.mark_message(flag);
-      if(this.env.action=="show")
-        this.command('nextmessage','',this);
-      else if (selection.length == 1)
-        this.message_list.select_next();
-      }
-    // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
-    else if (!this.env.trash_mailbox) 
-      this.permanently_remove_messages();
-  };
+    // also send search request to get the right messages
+    if (this.env.search_request)
+      add_url += '&_search='+this.env.search_request;
+
+    // send request to server
+    this.http_request(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
+    };
 
 
   // set a specific flag to one or more messages
@@ -1444,13 +1442,13 @@
       a_uids[0] = uid;
     else if (this.env.uid)
       a_uids[0] = this.env.uid;
-    else
+    else if (this.message_list)
       {
-      var id;
-      for (var n=0; n<selection.length; n++)
+      for (var id, n=0; n<selection.length; n++)
         {
         id = selection[n];
-        if ((flag=='read' && this.message_list.rows[id].unread) || (flag=='unread' && !this.message_list.rows[id].unread))
+        if ((flag=='read' && this.message_list.rows[id].unread) || (flag=='unread' && !this.message_list.rows[id].unread)
+            || (flag=='delete' && !this.message_list.rows[id].deleted) || (flag=='undelete' && this.message_list.rows[id].deleted))
           a_uids[a_uids.length] = id;
         }
       }
@@ -1523,7 +1521,8 @@
       return false;
 
     var rows = this.message_list.rows;
-    if (a_uids.length==1) {
+    if (a_uids.length==1)
+    {
       if (rows[a_uids[0]] && rows[a_uids[0]].classname.indexOf('deleted') < 0)
         this.flag_as_deleted(a_uids);
       else
@@ -1533,10 +1532,12 @@
     }
     
     var all_deleted = true;
-    for (var i=0; i<a_uids.length; i++) {
+    for (var i=0; i<a_uids.length; i++)
+    {
       uid = a_uids[i];
       if (rows[uid]) {
-        if (rows[uid].classname.indexOf('deleted')<0) {
+        if (rows[uid].classname.indexOf('deleted')<0)
+        {
           all_deleted = false;
           break;
         }
@@ -1561,12 +1562,14 @@
     var icn_src;
     var rows = this.message_list.rows;
       
-    for (var i=0; i<a_uids.length; i++) {
+    for (var i=0; i<a_uids.length; i++)
+    {
       uid = a_uids[i];
       if (rows[uid]) {
         rows[uid].deleted = false;
         
-        if (rows[uid].classname.indexOf('deleted') > 0) {
+        if (rows[uid].classname.indexOf('deleted') > 0)
+        {
           rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
           this.set_classname(rows[uid].obj, 'deleted', false);
         }
@@ -1593,7 +1596,8 @@
       return false;
 
     var rows = this.message_list.rows;
-    for (var i=0; i<a_uids.length; i++) {
+    for (var i=0; i<a_uids.length; i++)
+    {
       uid = a_uids[i];
       if (rows[uid]) {
         rows[uid].deleted = true;
@@ -1686,7 +1690,7 @@
   this.auto_save_start = function()
     {
     if (this.env.draft_autosave)
-      this.save_timer = self.setTimeout(this.ref+'.command("savedraft")', this.env.draft_autosave * 1000);
+      this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
     };
 
 
@@ -2037,7 +2041,7 @@
       }
 
     // start timer
-    this.ksearch_timer = setTimeout(this.ref+'.ksearch_get_results()', 200);      
+    this.ksearch_timer = setTimeout(function(){ ref.ksearch_get_results(); }, 200);
     this.ksearch_input = obj;
     
     return true;
@@ -2585,6 +2589,7 @@
         form.elements['_folder_name'].value = ''; 
       }
 
+    this.sort_subscription_list();
     };
 
 
@@ -2731,6 +2736,32 @@
       }
     
     return new_row;
+    };
+
+  // sort subscription folder list
+  this.sort_subscription_list = function()
+    {
+    var index = new Array();
+    var tbody = this.gui_objects.subscriptionlist.tBodies[0];
+    var swapped = false;
+    for (var i = 0; i<(tbody.childNodes.length-1); i++)
+      if (this.env.subscriptionrows[tbody.childNodes[i].id]!=null)
+        index.push(i);
+    for (i = 0; i<(index.length-1); i++)
+      {
+      if (this.env.subscriptionrows[tbody.childNodes[index[i]].id][0]>
+          this.env.subscriptionrows[tbody.childNodes[index[i+1]].id][0])
+        {
+        var swap = tbody.replaceChild(tbody.childNodes[index[i]], tbody.childNodes[index[i+1]]);
+        if (typeof(tbody.childNodes[index[i]]) != 'undefined')
+          tbody.insertBefore(swap, tbody.childNodes[index[i]])
+        else
+          tbody.appendChild(swap);
+        swapped = true;
+        }
+      }
+    if (swapped)
+      this.sort_subscription_list();
     };
 
 
@@ -2901,7 +2932,7 @@
       this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
     
     if (!hold)
-      this.message_timer = setTimeout(this.ref+'.hide_message()', this.message_time);
+      this.message_timer = setTimeout(function(){ ref.hide_message(); }, this.message_time);
     };
 
 
@@ -3040,10 +3071,14 @@
     };
 
   // replace content of quota display
-   this.set_quota = function(text)
+   this.set_quota = function()
      {
-     if (this.gui_objects.quotadisplay)
-       this.gui_objects.quotadisplay.innerHTML = text;
+     if (this.gui_objects.quotadisplay &&
+         this.gui_objects.quotadisplay.attributes.getNamedItem('display') &&
+         this.gui_objects.quotadisplay.attributes.getNamedItem('id'))
+       this.http_request('quotadisplay', '_display='+
+         this.gui_objects.quotadisplay.attributes.getNamedItem('display').nodeValue+
+         '&_id='+this.gui_objects.quotadisplay.attributes.getNamedItem('id').nodeValue, false);
      };
 
 

--
Gitblit v1.9.1