Thomas Bruederli
2014-07-09 007f1b443888a17b5156ef871e5548d28a3a0130
program/js/app.js
@@ -142,7 +142,7 @@
    this.task = this.env.task;
    // check browser
    if (!bw.dom || !bw.xmlhttp_test() || (bw.mz && bw.vendver < 1.9)) {
    if (this.env.server_error != 409 && (!bw.dom || !bw.xmlhttp_test() || (bw.mz && bw.vendver < 1.9))) {
      this.goto_url('error', '_code=0x199');
      return;
    }
@@ -564,7 +564,7 @@
  // execute a specific command on the web client
  this.command = function(command, props, obj, event)
  {
    var ret, uid, cid, url, flag;
    var ret, uid, cid, url, flag, aborted = false;
    if (obj && obj.blur)
      obj.blur();
@@ -593,6 +593,7 @@
      // remove copy from local storage if compose screen is left intentionally
      this.remove_compose_data(this.env.compose_id);
      this.compose_skip_unsavedcheck = true;
    }
    // process external commands
@@ -650,11 +651,17 @@
          var form = this.gui_objects.messageform,
            win = this.open_window('');
          this.save_compose_form_local();
          $("input[name='_action']", form).val('compose');
          form.action = this.url('mail/compose', { _id: this.env.compose_id, _extwin: 1 });
          form.target = win.name;
          form.submit();
          if (win) {
            this.save_compose_form_local();
            this.compose_skip_unsavedcheck = true;
            $("input[name='_action']", form).val('compose');
            form.action = this.url('mail/compose', { _id: this.env.compose_id, _extwin: 1 });
            form.target = win.name;
            form.submit();
          }
          else {
            // this.display_message(this.get_label('windowopenerror'), 'error');
          }
        }
        else {
          this.open_window(this.env.permaurl, true);
@@ -1049,7 +1056,11 @@
        // Reset the auto-save timer
        clearTimeout(this.save_timer);
        this.upload_file(props || this.gui_objects.uploadform, 'upload');
        if (!(flag = this.upload_file(props || this.gui_objects.uploadform, 'upload'))) {
          if (flag !== false)
            alert(this.get_label('selectimportfile'));
          aborted = true;
        }
        break;
      case 'insert-sig':
@@ -1171,9 +1182,17 @@
        break;
      case 'import-messages':
        var form = props || this.gui_objects.importform;
        $('input[name="_unlock"]', form).val(this.set_busy(true, 'importwait'));
        this.upload_file(form, 'import');
        var form = props || this.gui_objects.importform,
          importlock = this.set_busy(true, 'importwait');
        $('input[name="_unlock"]', form).val(importlock);
        if (!(flag = this.upload_file(form, 'import'))) {
          this.set_busy(false, null, importlock);
          if (flag !== false)
            alert(this.get_label('selectimportfile'));
          aborted = true;
        }
        break;
      case 'import':
@@ -1181,6 +1200,7 @@
          var file = document.getElementById('rcmimportfile');
          if (file && !file.value) {
            alert(this.get_label('selectimportfile'));
            aborted = true;
            break;
          }
          this.gui_objects.importform.submit();
@@ -1232,9 +1252,9 @@
        break;
    }
    if (this.triggerEvent('after'+command, props) === false)
    if (!aborted && this.triggerEvent('after'+command, props) === false)
      ret = false;
    this.triggerEvent('actionafter', {props:props, action:command});
    this.triggerEvent('actionafter', { props:props, action:command, aborted:aborted });
    return ret === false ? false : obj ? false : true;
  };
@@ -1307,7 +1327,7 @@
    var url = this.get_task_url(task);
    if (task == 'mail')
      url += '&_mbox=INBOX';
    else if (task == 'logout')
    else if (task == 'logout' && !this.env.server_error)
      this.clear_compose_data();
    this.redirect(url);
@@ -1887,18 +1907,18 @@
    tree += '<span id="msgicn'+uid+'" class="'+css_class+'">&nbsp;</span>';
    row.className = row_class;
    // build subject link
    if (!bw.ie && cols.subject) {
    // build subject link
    if (cols.subject) {
      var action = flags.mbox == this.env.drafts_mailbox ? 'compose' : 'show';
      var uid_param = flags.mbox == this.env.drafts_mailbox ? '_draft_uid' : '_uid';
      cols.subject = '<a href="./?_task=mail&_action='+action+'&_mbox='+urlencode(flags.mbox)+'&'+uid_param+'='+uid+'"'+
        ' onclick="return rcube_event.cancel(event)" onmouseover="rcube_webmail.long_subject_title(this,'+(message.depth+1)+')">'+cols.subject+'</a>';
        ' onclick="return rcube_event.cancel(event)" onmouseover="rcube_webmail.long_subject_title(this,'+(message.depth+1)+')"><span>'+cols.subject+'</span></a>';
    }
    // add each submitted col
    for (n in this.env.coltypes) {
      c = this.env.coltypes[n];
      col = { className: String(c).toLowerCase() };
      col = {className: String(c).toLowerCase(), events:{}};
      if (c == 'flag') {
        css_class = (flags.flagged ? 'flagged' : 'unflagged');
@@ -1926,11 +1946,8 @@
      else if (c == 'threads')
        html = expando;
      else if (c == 'subject') {
        if (bw.ie) {
          col.onmouseover = function() { rcube_webmail.long_subject_title_ex(this, message.depth+1); };
          if (bw.ie8)
            tree = '<span></span>' + tree; // #1487821
        }
        if (bw.ie)
          col.events.mouseover = function() { rcube_webmail.long_subject_title_ex(this); };
        html = tree + cols[c];
      }
      else if (c == 'priority') {
@@ -2049,17 +2066,35 @@
        this.location_href(this.env.comm_path+url, target, true);
      // mark as read and change mbox unread counter
      if (preview && this.message_list && this.message_list.rows[id] && this.message_list.rows[id].unread && this.env.preview_pane_mark_read >= 0) {
      if (preview && this.message_list && this.message_list.rows[id] && this.message_list.rows[id].unread && this.env.preview_pane_mark_read > 0) {
        this.preview_read_timer = setTimeout(function() {
          ref.set_message(id, 'unread', false);
          if (ref.env.unread_counts[ref.env.mailbox]) {
            ref.env.unread_counts[ref.env.mailbox] -= 1;
            ref.set_unread_count(ref.env.mailbox, ref.env.unread_counts[ref.env.mailbox], ref.env.mailbox == 'INBOX');
          }
          if (ref.env.preview_pane_mark_read > 0)
            ref.http_post('mark', {_uid: id, _flag: 'read', _quiet: 1});
          ref.set_unread_message(id, ref.env.mailbox);
          ref.http_post('mark', {_uid: id, _flag: 'read', _quiet: 1});
        }, this.env.preview_pane_mark_read * 1000);
      }
    }
  };
  // update message status and unread counter after marking a message as read
  this.set_unread_message = function(id, folder)
  {
    var self = this;
    // find window with messages list
    if (!self.message_list)
      self = self.opener();
    if (!self && window.parent)
      self = parent.rcmail;
    if (!self || !self.message_list)
      return;
    self.set_message(id, 'unread', false);
    if (self.env.unread_counts[folder] > 0) {
      self.env.unread_counts[folder] -= 1;
      self.set_unread_count(folder, self.env.unread_counts[folder], folder == 'INBOX');
    }
  };
@@ -2136,7 +2171,7 @@
    var lock = this.set_busy(true, 'checkingmail'),
      params = this.check_recent_params();
    this.http_request('check-recent', params, lock);
    this.http_post('check-recent', params, lock);
  };
  // list messages of a specific mailbox using filter
@@ -3094,7 +3129,12 @@
    // close compose step in opener
    if (opener_rc && opener_rc.env.action == 'compose') {
      setTimeout(function(){ opener.history.back(); }, 100);
      setTimeout(function(){
        if (opener.history.length > 1)
          opener.history.back();
        else
          opener_rc.redirect(opener_rc.get_task_url('mail'));
      }, 100);
      this.env.opened_extwin = true;
    }
@@ -3223,6 +3263,7 @@
    form._draft.value = draft ? '1' : '';
    form.action = this.add_url(form.action, '_unlock', msgid);
    form.action = this.add_url(form.action, '_lang', lang);
    form.action = this.add_url(form.action, '_framed', 1);
    // register timer to notify about connection timeout
    this.submit_timer = setTimeout(function(){
@@ -3369,17 +3410,8 @@
          $(tinyMCE.get(props.id).getBody()).css('font-family', rcmail.env.default_font);
        }, 500);
    }
    else {
      var thisMCE = tinyMCE.get(props.id), existingHtml;
      if (existingHtml = thisMCE.getContent()) {
        if (!confirm(this.get_label('editorwarning'))) {
          return false;
        }
        this.html2plain(existingHtml, props.id);
      }
    else if (this.html2plain(tinyMCE.get(props.id).getContent(), props.id))
      tinyMCE.execCommand('mceRemoveControl', false, props.id);
    }
    return true;
  };
@@ -3526,10 +3558,7 @@
    // submit delete request
    if (key && confirm(this.get_label('deleteresponseconfirm'))) {
      this.http_post('settings/delete-response', { _key: key }, false);
      return true;
    }
    return false;
  };
  this.stop_spellchecking = function()
@@ -3618,16 +3647,29 @@
      this.env.draft_id = id;
      $("input[name='_draft_saveid']").val(id);
      // reset history of hidden iframe used for saving draft (#1489643)
      // but don't do this on timer-triggered draft-autosaving (#1489789)
      if (window.frames['savetarget'] && window.frames['savetarget'].history && !this.draft_autosave_submit) {
        window.frames['savetarget'].history.back();
      }
      this.draft_autosave_submit = false;
    }
    // always remove local copy upon saving as draft
    this.remove_compose_data(this.env.compose_id);
    this.compose_skip_unsavedcheck = false;
  };
  this.auto_save_start = function()
  {
    if (this.env.draft_autosave)
      this.save_timer = setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
    if (this.env.draft_autosave) {
      this.draft_autosave_submit = false;
      this.save_timer = setTimeout(function(){
          ref.draft_autosave_submit = true;  // set auto-saved flag (#1489789)
          ref.command("savedraft");
      }, this.env.draft_autosave * 1000);
    }
    // save compose form content to local storage every 5 seconds
    if (!this.local_save_timer && window.localStorage) {
@@ -3641,6 +3683,21 @@
          ref.compose_type_activity_last = ref.compose_type_activity;
        }
      }, 5000);
      $(window).unload(function() {
        // remove copy from local storage if compose screen is left after warning
        if (!ref.env.server_error)
          ref.remove_compose_data(ref.env.compose_id);
      });
    }
    // check for unsaved changes before leaving the compose page
    if (!window.onbeforeunload) {
      window.onbeforeunload = function() {
        if (!ref.compose_skip_unsavedcheck && ref.cmp_hash != ref.compose_field_hash()) {
          return ref.get_label('notsentwarning');
        }
      };
    }
    // Unlock interface now that saving is complete
@@ -3946,7 +4003,7 @@
  this.upload_file = function(form, action)
  {
    if (!form)
      return false;
      return;
    // count files and size on capable browser
    var size = 0, numfiles = 0;
@@ -3967,7 +4024,7 @@
    if (numfiles) {
      if (this.env.max_filesize && this.env.filesizeerror && size > this.env.max_filesize) {
        this.display_message(this.env.filesizeerror, 'error');
        return;
        return false;
      }
      var frame_name = this.async_upload_form(form, action || 'upload', function(e) {
@@ -4001,11 +4058,11 @@
      if (this.env.upload_progress_time) {
        this.upload_progress_start('upload', ts);
      }
    }
    // set reference to the form object
    this.gui_objects.attachmentform = form;
    return true;
      // set reference to the form object
      this.gui_objects.attachmentform = form;
      return true;
    }
  };
  // add file name to attachment list
@@ -4027,7 +4084,7 @@
    li.attr('id', name)
      .addClass(att.classname)
      .html(att.html)
      .on('mouseover', function() { rcube_webmail.long_subject_title_ex(this, 0); });
      .on('mouseover', function() { rcube_webmail.long_subject_title_ex(this); });
    // replace indicator's li
    if (upload_id && (indicator = document.getElementById(upload_id))) {
@@ -4047,8 +4104,10 @@
  this.remove_from_attachment_list = function(name)
  {
    delete this.env.attachments[name];
    $('#'+name).remove();
    if (this.env.attachments) {
      delete this.env.attachments[name];
      $('#'+name).remove();
    }
  };
  this.remove_attachment = function(name)
@@ -4176,23 +4235,25 @@
  this.sent_successfully = function(type, msg, folders)
  {
    this.display_message(msg, type);
    this.compose_skip_unsavedcheck = true;
    if (this.env.extwin) {
      var rc = this.opener();
      this.lock_form(this.gui_objects.messageform);
      var rc = this.opener();
      if (rc) {
        rc.display_message(msg, type);
        // refresh the folder where sent message was saved or replied message comes from
        if (folders && rc.env.task == 'mail' && rc.env.action == '' && $.inArray(rc.env.mailbox, folders) >= 0) {
          // @TODO: try with 'checkmail' here when #1485186 is fixed. See also #1489249.
          rc.command('list');
          rc.command('checkmail');
        }
      }
      setTimeout(function(){ window.close() }, 1000);
      setTimeout(function() { window.close(); }, 1000);
    }
    else {
      // before redirect we need to wait some time for Chrome (#1486177)
      setTimeout(function(){ ref.list_mailbox(); }, 500);
      setTimeout(function() { ref.list_mailbox(); }, 500);
    }
  };
@@ -4309,9 +4370,7 @@
    this.ksearch_input.value = pre + insert + end;
    // set caret to insert pos
    cpos = p+insert.length;
    if (this.ksearch_input.setSelectionRange)
      this.ksearch_input.setSelectionRange(cpos, cpos);
    this.set_caret_pos(this.ksearch_input, p + insert.length);
    if (trigger) {
      this.triggerEvent('autocomplete_insert', { field:this.ksearch_input, insert:insert });
@@ -5570,10 +5629,8 @@
      id = this.env.iid ? this.env.iid : selection[0];
    // submit request with appended token
    if (confirm(this.get_label('deleteidentityconfirm')))
      this.goto_url('delete-identity', { _iid: id, _token: this.env.request_token }, true);
    return true;
    if (id && confirm(this.get_label('deleteidentityconfirm')))
      this.http_post('settings/delete-identity', { _iid: id }, true);
  };
  this.update_identity_row = function(id, name, add)
@@ -5617,6 +5674,23 @@
        frame.location.href = this.env.blankpage;
      }
    }
    this.enable_command('delete', false);
  };
  this.remove_identity = function(id)
  {
    var frame, list = this.identity_list,
      rid = this.html_identifier(id);
    if (list && id) {
      list.remove_row(rid);
      if (this.env.contentframe && (frame = this.get_frame_window(this.env.contentframe))) {
        frame.location.href = this.env.blankpage;
      }
    }
    this.enable_command('delete', false);
  };
@@ -6099,7 +6173,10 @@
      // disable/enable input buttons
      if (button.type == 'input') {
        button.status = state;
        obj.disabled = !state;
        obj.disabled = state == 'pas';
      }
      else if (button.type == 'uibutton') {
        $(obj).button('option', 'disabled', state == 'pas');
      }
    }
  };
@@ -6576,8 +6653,9 @@
    // fetch headers only once
    if (!this.gui_objects.all_headers_box.innerHTML) {
      var lock = this.display_message(this.get_label('loading'), 'loading');
      this.http_post('headers', {_uid: this.env.uid}, lock);
      this.http_post('headers', {_uid: this.env.uid, _mbox: this.env.mailbox},
        this.display_message(this.get_label('loading'), 'loading')
      );
    }
  };
@@ -6670,14 +6748,22 @@
  this.element_position = function(element, obj)
  {
    var obj = $(obj), win = $(window),
      width = obj.width(),
      height = obj.height(),
      width = obj.outerWidth(),
      height = obj.outerHeight(),
      menu_pos = obj.data('menu-pos'),
      win_height = win.height(),
      elem_height = $(element).height(),
      elem_width = $(element).width(),
      pos = obj.offset(),
      top = pos.top,
      left = pos.left + width;
    if (menu_pos == 'bottom') {
      top += height;
      left -= width;
    }
    else
      left -= 5;
    if (top + elem_height > win_height) {
      top -= elem_height - height;
@@ -6698,6 +6784,16 @@
  this.html2plain = function(htmlText, id)
  {
    // warn the user (if converted content is not empty)
    if (!htmlText || !(htmlText.replace(/<[^>]+>|&nbsp;|\s/g, '')).length) {
      // without setTimeout() here, textarea is filled with initial (onload) content
      setTimeout(function() { $('#'+id).val(''); }, 50);
      return true;
    }
    if (!confirm(this.get_label('editorwarning')))
      return false;
    var rcmail = this,
      url = '?_task=utils&_action=html2text',
      lock = this.set_busy(true, 'converting');
@@ -6708,6 +6804,8 @@
      error: function(o, status, err) { rcmail.http_error(o, status, err, lock); },
      success: function(data) { rcmail.set_busy(false, null, lock); $('#'+id).val(data); rcmail.log(data); }
    });
    return true;
  };
  this.plain2html = function(plain, id)
@@ -6754,7 +6852,7 @@
        param[k] = query[k];
    }
    return base + '&' + $.param(param) + querystring;
    return base + (base.indexOf('?') > -1 ? '&' : '?') + $.param(param) + querystring;
  };
  this.redirect = function(url, lock)
@@ -6778,7 +6876,7 @@
  this.goto_url = function(action, query, lock)
  {
    this.redirect(this.url(action, query));
    this.redirect(this.url(action, query), lock);
  };
  this.location_href = function(url, target, frame)
@@ -7025,7 +7123,7 @@
    else if (status == 'timeout')
      this.display_message(this.get_label('requesttimedout'), 'error');
    else if (request.status == 0 && status != 'abort')
      this.display_message(this.get_label('servererror') + ' (No connection)', 'error');
      this.display_message(this.get_label('connerror'), 'error');
    // redirect to url specified in location header if not empty
    var location_url = request.getResponseHeader("Location");
@@ -7053,6 +7151,7 @@
    // save message in local storage and do not redirect
    if (this.env.action == 'compose') {
      this.save_compose_form_local();
      this.compose_skip_unsavedcheck = true;
    }
    else if (redirect_url) {
      window.setTimeout(function(){ ref.redirect(redirect_url, true); }, 2000);
@@ -7283,7 +7382,7 @@
    this.env.lastrefresh = new Date();
    // plugins should bind to 'requestrefresh' event to add own params
    this.http_request('refresh', params, lock);
    this.http_post('refresh', params, lock);
  };
  // returns check-recent request parameters
@@ -7472,20 +7571,28 @@
    try {
      window.navigator.registerProtocolHandler('mailto', this.mailto_handler_uri(), name);
    }
    catch(e) {};
    catch(e) {
      this.display_message(String(e), 'error');
    };
  };
  this.check_protocol_handler = function(name, elem)
  {
    var nav = window.navigator;
    if (!nav
      || (typeof nav.registerProtocolHandler != 'function')
      || ((typeof nav.isProtocolHandlerRegistered == 'function')
        && nav.isProtocolHandlerRegistered('mailto', this.mailto_handler_uri()) == 'registered')
    )
      $(elem).addClass('disabled');
    else
      $(elem).click(function() { rcmail.register_protocol_handler(name); return false; });
    if (!nav || (typeof nav.registerProtocolHandler != 'function')) {
      $(elem).addClass('disabled').click(function(){ return false; });
    }
    else {
      var status = null;
      if (typeof nav.isProtocolHandlerRegistered == 'function') {
        status = nav.isProtocolHandlerRegistered('mailto', this.mailto_handler_uri());
        if (status)
          $(elem).parent().find('.mailtoprotohandler-status').html(status);
      }
      else {
        $(elem).click(function() { rcmail.register_protocol_handler(name); return false; });
      }
    }
  };
  // Checks browser capabilities eg. PDF support, TIF support
@@ -7630,7 +7737,7 @@
  }
};
rcube_webmail.long_subject_title_ex = function(elem, indent)
rcube_webmail.long_subject_title_ex = function(elem)
{
  if (!elem.title) {
    var $elem = $(elem),
@@ -7642,7 +7749,7 @@
      w = tmp.width();
    tmp.remove();
    if (w + indent * 15 > $elem.width())
    if (w + $('span.branch', $elem).width() * 15 > $elem.width())
      elem.title = txt;
  }
};