From b649c49e64a487ac8ad347aed42336dec2e74cd7 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Fri, 25 Feb 2011 04:16:05 -0500
Subject: [PATCH] - Fix attachments uploading, broken with devel-addressbook branch merge

---
 program/js/app.js |  141 ++++++++++++++++++++++++++++------------------
 1 files changed, 85 insertions(+), 56 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index c81212f..92b7a67 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -17,7 +17,6 @@
   $Id$
 */
 
-
 function rcube_webmail()
 {
   this.env = {};
@@ -63,7 +62,7 @@
   // set environment variable(s)
   this.set_env = function(p, value)
   {
-    if (p != null && typeof(p) == 'object' && !value)
+    if (p != null && typeof p === 'object' && !value)
       for (var n in p)
         this.env[n] = p[n];
     else
@@ -332,7 +331,7 @@
             this.selectedIndex = 0;
           });
 
-          $("input[type='text']").first().focus();
+          $("input[type='text']:visible").first().focus();
         }
         else if (this.gui_objects.qsearchbox) {
           this.enable_command('search', 'reset-search', 'moveto', true);
@@ -428,9 +427,9 @@
     // execute all foreign onload scripts
     // @deprecated
     for (var i in this.onloads) {
-      if (typeof(this.onloads[i]) == 'string')
+      if (typeof this.onloads[i] === 'string')
         eval(this.onloads[i]);
-      else if (typeof(this.onloads[i]) == 'function')
+      else if (typeof this.onloads[i] === 'function')
         this.onloads[i]();
       }
 
@@ -468,19 +467,19 @@
     }
 
     // process external commands
-    if (typeof this.command_handlers[command] == 'function') {
+    if (typeof this.command_handlers[command] === 'function') {
       var ret = this.command_handlers[command](props, obj);
-      return ret !== null ? ret : (obj ? false : true);
+      return ret !== undefined ? ret : (obj ? false : true);
     }
-    else if (typeof this.command_handlers[command] == 'string') {
+    else if (typeof this.command_handlers[command] === 'string') {
       var ret = window[this.command_handlers[command]](props, obj);
-      return ret !== null ? ret : (obj ? false : true);
+      return ret !== undefined ? ret : (obj ? false : true);
     }
 
     // trigger plugin hooks
     this.triggerEvent('actionbefore', {props:props, action:command});
     var event_ret = this.triggerEvent('before'+command, props);
-    if (typeof event_ret != 'undefined') {
+    if (event_ret !== undefined) {
       // abort if one the handlers returned false
       if (event_ret === false)
         return false;
@@ -647,7 +646,7 @@
               input_name.focus();
               break;
             }
-            else if (input_email.length && !rcube_check_email(input_email.val())) {
+            else if (this.task == 'settings' && input_email.length && !rcube_check_email(input_email.val())) {
               alert(this.get_label('noemailwarning'));
               input_email.focus();
               break;
@@ -1034,7 +1033,7 @@
       // unified command call (command name == function name)
       default:
         var func = command.replace(/-/g, '_');
-        if (this[func] && typeof this[func] == 'function')
+        if (this[func] && typeof this[func] === 'function')
           this[func](props);
         break;
     }
@@ -1410,7 +1409,7 @@
     // reset 'pressed' buttons
     if (this.buttons_sel) {
       for (var id in this.buttons_sel)
-        if (typeof id != 'function')
+        if (typeof id !== 'function')
           this.button_out(this.buttons_sel[id], id);
       this.buttons_sel = {};
     }
@@ -1696,7 +1695,7 @@
           message.expanded = true;
       }
       else if (message.has_children) {
-        if (typeof(message.expanded) == 'undefined' && (this.env.autoexpand_threads == 1 || (this.env.autoexpand_threads == 2 && message.unread_children))) {
+        if (message.expanded === undefined && (this.env.autoexpand_threads == 1 || (this.env.autoexpand_threads == 2 && message.unread_children))) {
           message.expanded = true;
         }
       }
@@ -1784,7 +1783,7 @@
   {
     var update, add_url = '';
 
-    if (typeof sort_col == 'undefined')
+    if (sort_col === undefined)
       sort_col = this.env.sort_col;
     if (!sort_order)
       sort_order = this.env.sort_order;
@@ -2380,7 +2379,7 @@
   // copy selected messages to the specified mailbox
   this.copy_messages = function(mbox)
   {
-    if (mbox && typeof mbox == 'object')
+    if (mbox && typeof mbox === 'object')
       mbox = mbox.id;
 
     // exit if current or no mailbox specified or if selection is empty
@@ -2409,7 +2408,7 @@
   // move selected messages to the specified mailbox
   this.move_messages = function(mbox)
   {
-    if (mbox && typeof mbox == 'object')
+    if (mbox && typeof mbox === 'object')
       mbox = mbox.id;
 
     // exit if current or no mailbox specified or if selection is empty
@@ -2900,7 +2899,7 @@
 
     // check if all files has been uploaded
     for (var key in this.env.attachments) {
-      if (typeof this.env.attachments[key] == 'object' && !this.env.attachments[key].complete) {
+      if (typeof this.env.attachments[key] === 'object' && !this.env.attachments[key].complete) {
         alert(this.get_label('notuploadedwarning'));
         return false;
       }
@@ -3191,7 +3190,7 @@
 
     // create hidden iframe and post upload form
     if (send) {
-      this.async_upload_form(form, 'upload', function(e) {
+      var frame_name = this.async_upload_form(form, 'upload', function(e) {
         var d, content = '';
         try {
           if (this.contentDocument) {
@@ -3200,7 +3199,7 @@
             d = this.contentWindow.document;
           }
           content = d.childNodes[0].innerHTML;
-        } catch (e) {}
+        } catch (err) {}
 
         if (!content.match(/add2attachment/) && (!bw.opera || (rcmail.env.uploadframe && rcmail.env.uploadframe == e.data.ts))) {
           if (!content.match(/display_message/))
@@ -3213,7 +3212,9 @@
       });
 
       // display upload indicator and cancel button
-      var content = this.get_label('uploading');
+      var content = this.get_label('uploading'),
+        ts = frame_name.replace(/^rcmupload/, '');
+
       if (this.env.loadingicon)
         content = '<img src="'+this.env.loadingicon+'" alt="" />'+content;
       if (this.env.cancelicon)
@@ -3438,13 +3439,13 @@
       end = inp_value.substring(p+this.ksearch_value.length, inp_value.length);
 
     // insert all members of a group
-    if (typeof this.env.contacts[id] == 'object' && this.env.contacts[id].id) {
+    if (typeof this.env.contacts[id] === 'object' && this.env.contacts[id].id) {
       insert += this.env.contacts[id].name + ', ';
       this.group2expand = $.extend({}, this.env.contacts[id]);
       this.group2expand.input = this.ksearch_input;
       this.http_request('group-expand', '_source='+urlencode(this.env.contacts[id].source)+'&_gid='+urlencode(this.env.contacts[id].id), false);
     }
-    else if (typeof this.env.contacts[id] == 'string')
+    else if (typeof this.env.contacts[id] === 'string')
       insert = this.env.contacts[id] + ', ';
 
     this.ksearch_input.value = pre + insert + end;
@@ -3487,15 +3488,14 @@
     if (q == this.ksearch_value)
       return;
 
-    if (q.length < min) {
+    if (q.length && q.length < min) {
       if (!this.env.acinfo) {
-        var label = this.get_label('autocompletechars');
-        label = label.replace('$min', min);
-        this.env.acinfo = this.display_message(label);
+        this.env.acinfo = this.display_message(
+          this.get_label('autocompletechars').replace('$min', min));
       }
       return;
     }
-    else if (this.env.acinfo && q.length == min) {
+    else if (this.env.acinfo) {
       this.hide_message(this.env.acinfo);
     }
 
@@ -3543,7 +3543,7 @@
 
       // add each result line to list
       for (i=0; i < a_results.length; i++) {
-        text = typeof a_results[i] == 'object' ? a_results[i].name : a_results[i];
+        text = typeof a_results[i] === 'object' ? a_results[i].name : a_results[i];
         li = document.createElement('LI');
         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); };
@@ -3766,6 +3766,9 @@
         this.show_contentframe(false);
     }
 
+    if (this.env.group)
+      qs += '&_gid='+urlencode(this.env.group);
+
     // also send search request to get the right records from the next page
     if (this.env.search_request) 
       qs += '&_search='+this.env.search_request;
@@ -3779,6 +3782,9 @@
   // update a contact record in the list
   this.update_contact_row = function(cid, cols_arr, newcid)
   {
+    cid = String(cid).replace(this.identifier_expr, '_');
+    newcid = String(newcid).replace(this.identifier_expr, '_');
+
     var row;
     if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj)) {
       for (var c=0; c<cols_arr.length; c++)
@@ -3811,7 +3817,7 @@
       even = rowcount%2,
       row = document.createElement('tr');
 
-    row.id = 'rcmrow'+cid;
+    row.id = 'rcmrow'+String(cid).replace(this.identifier_expr, '_');
     row.className = 'contact '+(even ? 'even' : 'odd');
 
     if (this.contact_list.in_selection(cid))
@@ -3943,9 +3949,10 @@
     this.env.contactfolders[key] = this.env.contactgroups[key] = prop;
 
     var link = $('<a>').attr('href', '#')
+      .attr('rel', prop.source+':'+prop.id)
       .bind('click', function() { return rcmail.command('listgroup', prop, this);})
       .html(prop.name);
-    var li = $('<li>').attr('id', 'rcmli'+key)
+    var li = $('<li>').attr('id', 'rcmli'+key.replace(this.identifier_expr, '_'))
       .addClass('contactgroup')
       .append(link)
       .insertAfter(this.get_folder_li(prop.source));
@@ -3958,13 +3965,33 @@
   {
     this.reset_add_input();
 
-    var key = 'G'+prop.source+prop.id, link, li = this.get_folder_li(key);
+    var key = 'G'+prop.source+prop.id,
+      li = this.get_folder_li(key),
+      link;
 
-    if (li && (link = li.firstChild) && link.tagName.toLowerCase() == 'a')
+    // group ID has changed, replace link node and identifiers
+    if (li && prop.newid) {
+      var newkey = 'G'+prop.source+prop.newid;
+      li.id = String('rcmli'+newkey).replace(this.identifier_expr, '_');
+      this.env.contactfolders[newkey] = this.env.contactfolders[key];
+      this.env.group = prop.newid;
+      
+      var newprop = $.extend({}, prop);
+      newprop.id = prop.newid;
+      newprop.type = 'group';
+      
+      link = $('<a>').attr('href', '#')
+        .attr('rel', prop.source+':'+prop.newid)
+        .bind('click', function() { return rcmail.command('listgroup', newprop, this);})
+        .html(prop.name);
+      $(li).children().replaceWith(link);
+    }
+    // update displayed group name
+    else if (li && (link = li.firstChild) && link.tagName.toLowerCase() == 'a')
       link.innerHTML = prop.name;
 
     this.env.contactfolders[key].name = this.env.contactgroups[key].name = prop.name;
-    this.triggerEvent('group_update', { 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], newid:prop.newid });
   };
 
 
@@ -3975,7 +4002,7 @@
     
     elem.focus(function(){ ref.focus_textfield(this); })
       .blur(function(){ ref.blur_textfield(this); })
-      .each(function(){ this._placeholder = ref.env.coltypes[col].label; ref.blur_textfield(this); });
+      .each(function(){ this._placeholder = this.title = ref.env.coltypes[col].label; ref.blur_textfield(this); });
   };
 
   this.insert_edit_field = function(col, section, menu)
@@ -4492,7 +4519,7 @@
   this.init_buttons = function()
   {
     for (var cmd in this.buttons) {
-      if (typeof cmd != 'string')
+      if (typeof cmd !== 'string')
         continue;
 
       for (var i=0; i< this.buttons[cmd].length; i++) {
@@ -4553,7 +4580,7 @@
         obj.src = button[state];
       }
       // set class name according to button state
-      else if (obj && typeof(button[state])!='undefined') {
+      else if (obj && button[state] !== undefined) {
         button.status = state;
         obj.className = button[state];
       }
@@ -4748,7 +4775,7 @@
     var k, n, i, msg, m = this.messages;
 
     // Hide message by object, don't use for 'loading'!
-    if (typeof(obj) == 'object') {
+    if (typeof obj === 'object') {
       $(obj)[fade?'fadeOut':'hide']();
       msg = $(obj).data('key');
       if (this.messages[msg])
@@ -4894,7 +4921,7 @@
   this.set_quota = function(content)
   {
     if (content && this.gui_objects.quotadisplay) {
-      if (typeof(content) == 'object' && content.type == 'image')
+      if (typeof content === 'object' && content.type == 'image')
         this.percent_indicator(this.gui_objects.quotadisplay, content);
       else
         $(this.gui_objects.quotadisplay).html(content);
@@ -5122,18 +5149,18 @@
   // compose a valid url with the given parameters
   this.url = function(action, query)
   {
-    var querystring = typeof(query) == 'string' ? '&' + query : '';
-    
-    if (typeof action != 'string')
+    var querystring = typeof query === 'string' ? '&' + query : '';
+
+    if (typeof action !== 'string')
       query = action;
-    else if (!query || typeof(query) != 'object')
+    else if (!query || typeof query !== 'object')
       query = {};
-    
+
     if (action)
       query._action = action;
     else
       query._action = this.env.action;
-    
+
     var base = this.env.comm_path;
 
     // overwrite task name
@@ -5141,14 +5168,14 @@
       query._action = RegExp.$2;
       base = base.replace(/\_task=[a-z]+/, '_task='+RegExp.$1);
     }
-    
+
     // remove undefined values
     var param = {};
     for (var k in query) {
-      if (typeof(query[k]) != 'undefined' && query[k] !== null)
+      if (query[k] !== undefined && query[k] !== null)
         param[k] = query[k];
     }
-    
+
     return base + '&' + $.param(param) + querystring;
   };
 
@@ -5176,7 +5203,7 @@
     // trigger plugin hook
     var result = this.triggerEvent('request'+action, query);
 
-    if (typeof result != 'undefined') {
+    if (result !== undefined) {
       // abort if one the handlers returned false
       if (result === false)
         return false;
@@ -5200,7 +5227,7 @@
   {
     var url = this.url(action);
 
-    if (postdata && typeof(postdata) == 'object') {
+    if (postdata && typeof postdata === 'object') {
       postdata._remote = 1;
       postdata._unlock = (lock ? lock : 0);
     }
@@ -5209,7 +5236,7 @@
 
     // trigger plugin hook
     var result = this.triggerEvent('request'+action, postdata);
-    if (typeof result != 'undefined') {
+    if (result !== undefined) {
       // abort if one the handlers returned false
       if (result === false)
         return false;
@@ -5243,9 +5270,9 @@
       this.set_env(response.env);
 
     // we have labels to add
-    if (typeof response.texts == 'object') {
+    if (typeof response.texts === 'object') {
       for (var name in response.texts)
-        if (typeof response.texts[name] == 'string')
+        if (typeof response.texts[name] === 'string')
           this.add_label(name, response.texts[name]);
     }
 
@@ -5348,8 +5375,8 @@
   // post the given form to a hidden iframe
   this.async_upload_form = function(form, action, onload)
   {
-    var ts = new Date().getTime();
-    var frame_name = 'rcmupload'+ts;
+    var ts = new Date().getTime(),
+      frame_name = 'rcmupload'+ts;
 
     // have to do it this way for IE
     // otherwise the form will be posted to a new window
@@ -5374,6 +5401,8 @@
     form.action = this.url(action, { _uploadid:ts });
     form.setAttribute('enctype', 'multipart/form-data');
     form.submit();
+
+    return frame_name;
   };
   
   // starts interval for keep-alive/check-recent signal
@@ -5441,7 +5470,7 @@
   // gets cursor position
   this.get_caret_pos = function(obj)
   {
-    if (typeof(obj.selectionEnd)!='undefined')
+    if (obj.selectionEnd !== undefined)
       return obj.selectionEnd;
     else if (document.selection && document.selection.createRange) {
       var range = document.selection.createRange();

--
Gitblit v1.9.1