Aleksander Machniak
2013-12-08 fd0c123e44391aa01a85c5aeebbd0cea418f0808
program/js/app.js
@@ -34,7 +34,7 @@
  // webmail client settings
  this.dblclick_time = 500;
  this.message_time = 4000;
  this.message_time = 5000;
  this.identifier_expr = new RegExp('[^0-9a-z\-_]', 'gi');
  // environment defaults
@@ -578,9 +578,12 @@
    }
    // check input before leaving compose step
    if (this.task == 'mail' && this.env.action == 'compose' && $.inArray(command, this.env.compose_commands)<0) {
    if (this.task == 'mail' && this.env.action == 'compose' && $.inArray(command, this.env.compose_commands) < 0 && !this.env.server_error) {
      if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
        return false;
      // remove copy from local storage if compose screen is left intentionally
      this.remove_compose_data(this.env.compose_id);
    }
    // process external commands
@@ -615,10 +618,10 @@
        break;
      // commands to switch task
      case 'logout':
      case 'mail':
      case 'addressbook':
      case 'settings':
      case 'logout':
        this.switch_task(command);
        break;
@@ -638,6 +641,7 @@
          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;
@@ -1292,8 +1296,10 @@
      return;
    var url = this.get_task_url(task);
    if (task=='mail')
    if (task == 'mail')
      url += '&_mbox=INBOX';
    else if (task == 'logout')
      this.clear_compose_data();
    this.redirect(url);
  };
@@ -1700,7 +1706,7 @@
    url += (url.match(/\?/) ? '&' : '?') + '_extwin=1';
    if (this.env.standard_windows)
      extwin = window.open(url, wname);
      var extwin = window.open(url, wname);
    else {
      var win = this.is_framed() ? parent.window : window,
        page = $(win),
@@ -1720,8 +1726,11 @@
      extwin.document.write('<html><body>' + this.get_label('loading') + '</body></html>');
    }
    // allow plugins to grab the window reference (#1489413)
    this.triggerEvent('openwindow', { url:url, handle:extwin });
    // focus window, delayed to bring to front
    window.setTimeout(function() { extwin.focus(); }, 10);
    window.setTimeout(function() { extwin && extwin.focus(); }, 10);
    return extwin;
  };
@@ -3117,6 +3126,60 @@
      }
    }
    // check for locally stored compose data
    if (window.localStorage) {
      var index = this.local_storage_get_item('compose.index', []);
      for (var key, i = 0; i < index.length; i++) {
        key = index[i], formdata = this.local_storage_get_item('compose.' + key, null, true);
        if (!formdata) {
          continue;
        }
        // restore saved copy of current compose_id
        if (formdata.changed && key == this.env.compose_id) {
          this.restore_compose_form(key, html_mode);
          break;
        }
        // skip records from 'other' drafts
        if (this.env.draft_id && formdata.draft_id && formdata.draft_id != this.env.draft_id) {
          continue;
        }
        // show dialog asking to restore the message
        if (formdata.changed && formdata.session != this.env.session_id) {
          this.show_popup_dialog(
            this.get_label('restoresavedcomposedata')
              .replace('$date', new Date(formdata.changed).toLocaleString())
              .replace('$subject', formdata._subject)
              .replace(/\n/g, '<br/>'),
            this.get_label('restoremessage'),
            [{
              text: this.get_label('restore'),
              click: function(){
                ref.restore_compose_form(key, html_mode);
                ref.remove_compose_data(key);  // remove old copy
                ref.save_compose_form_local();  // save under current compose_id
                $(this).dialog('close');
              }
            },
            {
              text: this.get_label('delete'),
              click: function(){
                ref.remove_compose_data(key);
                $(this).dialog('close');
              }
            },
            {
              text: this.get_label('ignore'),
              click: function(){
                $(this).dialog('close');
              }
            }]
          );
          break;
        }
      }
    }
    if (input_to.val() == '')
      input_to.focus();
    else if (input_subject.val() == '')
@@ -3137,7 +3200,7 @@
  {
    this.env.recipients_delimiter = this.env.recipients_separator + ' ';
    obj[bw.ie || bw.safari || bw.chrome ? 'keydown' : 'keypress'](function(e) { return ref.ksearch_keydown(e, this, props); })
    obj.keydown(function(e) { return ref.ksearch_keydown(e, this, props); })
      .attr('autocomplete', 'off');
  };
@@ -3554,12 +3617,28 @@
    this.env.draft_id = id;
    $("input[name='_draft_saveid']").val(id);
    this.remove_compose_data(this.env.compose_id);
  };
  this.auto_save_start = function()
  {
    if (this.env.draft_autosave)
      this.save_timer = setTimeout(function(){ 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) {
      // track typing activity and only save on changes
      this.compose_type_activity = this.compose_type_activity_last = 0;
      $(document).bind('keypress', function(e){ ref.compose_type_activity++; });
      this.local_save_timer = setInterval(function(){
        if (ref.compose_type_activity > ref.compose_type_activity_last) {
          ref.save_compose_form_local();
          ref.compose_type_activity_last = ref.compose_type_activity;
        }
      }, 5000);
    }
    // Unlock interface now that saving is complete
    this.busy = false;
@@ -3588,6 +3667,115 @@
    return str;
  };
  // store the contents of the compose form to localstorage
  this.save_compose_form_local = function()
  {
    var formdata = { session:this.env.session_id, changed:new Date().getTime() },
      ed, empty = true;
    // get fresh content from editor
    if (window.tinyMCE && (ed = tinyMCE.get(this.env.composebody))) {
      tinyMCE.triggerSave();
    }
    if (this.env.draft_id) {
      formdata.draft_id = this.env.draft_id;
    }
    $('input, select, textarea', this.gui_objects.messageform).each(function(i, elem) {
      switch (elem.tagName.toLowerCase()) {
        case 'input':
          if (elem.type == 'button' || elem.type == 'submit' || (elem.type == 'hidden' && elem.name != '_is_html')) {
            break;
          }
          formdata[elem.name] = elem.type != 'checkbox' || elem.checked ? $(elem).val() : '';
          if (formdata[elem.name] != '' && elem.type != 'hidden')
            empty = false;
          break;
        case 'select':
          formdata[elem.name] = $('option:checked', elem).val();
          break;
        default:
          formdata[elem.name] = $(elem).val();
          if (formdata[elem.name] != '')
            empty = false;
      }
    });
    if (window.localStorage && !empty) {
      var index = this.local_storage_get_item('compose.index', []),
        key = this.env.compose_id;
        if ($.inArray(key, index) < 0) {
          index.push(key);
        }
        this.local_storage_set_item('compose.' + key, formdata, true);
        this.local_storage_set_item('compose.index', index);
    }
  };
  // write stored compose data back to form
  this.restore_compose_form = function(key, html_mode)
  {
    var ed, formdata = this.local_storage_get_item('compose.' + key, true);
    if (formdata && typeof formdata == 'object') {
      $.each(formdata, function(k, value) {
        if (k[0] == '_') {
          var elem = $("*[name='"+k+"']");
          if (elem[0] && elem[0].type == 'checkbox') {
            elem.prop('checked', value != '');
          }
          else {
            elem.val(value);
          }
        }
      });
      // initialize HTML editor
      if (formdata._is_html == '1') {
        if (!html_mode) {
          tinyMCE.execCommand('mceAddControl', false, this.env.composebody);
          this.triggerEvent('aftertoggle-editor', { mode:'html' });
        }
      }
      else if (html_mode) {
        tinyMCE.execCommand('mceRemoveControl', false, this.env.composebody);
        this.triggerEvent('aftertoggle-editor', { mode:'plain' });
      }
    }
  };
  // remove stored compose data from localStorage
  this.remove_compose_data = function(key)
  {
    if (window.localStorage) {
      var index = this.local_storage_get_item('compose.index', []);
      if ($.inArray(key, index) >= 0) {
        this.local_storage_remove_item('compose.' + key);
        this.local_storage_set_item('compose.index', $.grep(index, function(val,i) { return val != key; }));
      }
    }
  };
  // clear all stored compose data of this user
  this.clear_compose_data = function()
  {
    if (window.localStorage) {
      var index = this.local_storage_get_item('compose.index', []);
      for (var i=0; i < index.length; i++) {
        this.local_storage_remove_item('compose.' + index[i]);
      }
      this.local_storage_remove_item('compose.index');
    }
  }
  this.change_identity = function(obj, show_sig)
  {
@@ -4021,7 +4209,7 @@
      case 38:  // arrow up
      case 40:  // arrow down
        if (!this.ksearch_visible())
          break;
          return;
        var dir = key==38 ? 1 : 0;
@@ -4056,8 +4244,7 @@
      case 37:  // left
      case 39:  // right
        if (mod != SHIFT_KEY)
          return;
        return;
    }
    // start timer
@@ -4120,8 +4307,10 @@
    if (this.ksearch_input.setSelectionRange)
      this.ksearch_input.setSelectionRange(cpos, cpos);
    if (trigger)
    if (trigger) {
      this.triggerEvent('autocomplete_insert', { field:this.ksearch_input, insert:insert });
      this.compose_type_activity++;
    }
  };
  this.replace_group_recipients = function(id, recipients)
@@ -4130,6 +4319,7 @@
      this.group2expand[id].input.value = this.group2expand[id].input.value.replace(this.group2expand[id].name, recipients);
      this.triggerEvent('autocomplete_insert', { field:this.group2expand[id].input, insert:recipients });
      this.group2expand[id] = null;
      this.compose_type_activity++;
    }
  };
@@ -5355,9 +5545,8 @@
      target = win;
    }
    if (action && (id || action == 'add-identity')) {
      this.set_busy(true);
      this.location_href(url, target);
    if (id || action == 'add-identity') {
      this.location_href(url, target, true);
    }
    return true;
@@ -5543,7 +5732,7 @@
    if (!this.gui_objects.subscriptionlist)
      return false;
    var row, n, i, tmp, tmp_name, folders, rowid, list = [], slist = [],
    var row, n, i, tmp, tmp_name, rowid, folders = [], list = [], slist = [],
      tbody = this.gui_objects.subscriptionlist.tBodies[0],
      refrow = $('tr', tbody).get(1),
      id = 'rcmrow'+((new Date).getTime());
@@ -5558,8 +5747,7 @@
    row = $(refrow).clone(true);
    // set ID, reset css class
    row.attr('id', id);
    row.attr('class', class_name);
    row.attr({id: id, 'class': class_name});
    // set folder name
    row.find('td:first').html(display_name);
@@ -5571,10 +5759,21 @@
    // add to folder/row-ID map
    this.env.subscriptionrows[id] = [name, display_name, 0];
    // sort folders, to find a place where to insert the row
    folders = [];
    $.each(this.env.subscriptionrows, function(k,v){ folders.push(v) });
    folders.sort(function(a,b){ return a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0) });
    // sort folders (to find a place where to insert the row)
    // replace delimiter with \0 character to fix sorting
    // issue where 'Abc Abc' would be placed before 'Abc/def'
    var replace_from = RegExp(RegExp.escape(this.env.delimiter), 'g'),
      replace_to = String.fromCharCode(0);
    $.each(this.env.subscriptionrows, function(k,v) {
      var n = v[0];
      n = n.replace(replace_from, replace_to);
      v.push(n);
      folders.push(v);
    });
    folders.sort(function(a, b) {
      var len = a.length - 1; n1 = a[len], n2 = b[len];
      return n1 < n2 ? -1 : 1;
    });
    for (n in folders) {
      // protected folder
@@ -6010,7 +6209,7 @@
    this.triggerEvent('message', { message:msg, type:type, timeout:timeout, object:obj });
    if (timeout > 0)
      setTimeout(function() { ref.hide_message(id, type == 'loading'); }, timeout);
      setTimeout(function() { ref.hide_message(id, type != 'loading'); }, timeout);
    return id;
  };
@@ -6644,7 +6843,6 @@
          if ((response.action == 'list' || response.action == 'search') && this.message_list) {
            this.msglist_select(this.message_list);
            this.message_list.resize();
            this.triggerEvent('listupdate', { folder:this.env.mailbox, rowcount:this.message_list.rowcount });
          }
        }
@@ -6655,7 +6853,6 @@
            this.enable_command('search-create', this.env.source == '');
            this.enable_command('search-delete', this.env.search_id);
            this.update_group_commands();
            this.contact_list.resize();
            this.triggerEvent('listupdate', { folder:this.env.source, rowcount:this.contact_list.rowcount });
          }
        }
@@ -6707,6 +6904,20 @@
    // re-send keep-alive requests after 30 seconds
    if (action == 'keep-alive')
      setTimeout(function(){ ref.keep_alive(); ref.start_keepalive(); }, 30000);
  };
  // handler for session errors detected on the server
  this.session_error = function(redirect_url)
  {
    this.env.server_error = 401;
    // save message in local storage and do not redirect
    if (this.env.action == 'compose') {
      this.save_compose_form_local();
    }
    else if (redirect_url) {
      window.setTimeout(function(){ ref.redirect(redirect_url, true); }, 2000);
    }
  };
  // callback when an iframe finished loading
@@ -7230,7 +7441,37 @@
  this.set_cookie = function(name, value, expires)
  {
    setCookie(name, value, expires, this.env.cookie_path, this.env.cookie_domain, this.env.cookie_secure);
  }
  };
  this.get_local_storage_prefix = function()
  {
    if (!this.local_storage_prefix)
      this.local_storage_prefix = 'roundcube.' + (this.env.user_id || 'anonymous') + '.';
    return this.local_storage_prefix;
  };
  // wrapper for localStorage.getItem(key)
  this.local_storage_get_item = function(key, deflt, encrypted)
  {
    // TODO: add encryption
    var item = localStorage.getItem(this.get_local_storage_prefix() + key);
    return item !== null ? JSON.parse(item) : (deflt || null);
  };
  // wrapper for localStorage.setItem(key, data)
  this.local_storage_set_item = function(key, data, encrypted)
  {
    // TODO: add encryption
    return localStorage.setItem(this.get_local_storage_prefix() + key, JSON.stringify(data));
  };
  // wrapper for localStorage.removeItem(key)
  this.local_storage_remove_item = function(key)
  {
    return localStorage.removeItem(this.get_local_storage_prefix() + key);
  };
}  // end object rcube_webmail