Aleksander Machniak
2013-10-30 6a91448aee5d036b35c621bbdaff250dc84e15f3
Improve performance and code readability by using String's startsWith() method, other code improvements
5 files modified
43 ■■■■■ changed files
plugins/archive/archive.js 2 ●●● patch | view | raw | blame | history
program/js/app.js 24 ●●●● patch | view | raw | blame | history
program/js/common.js 8 ●●●●● patch | view | raw | blame | history
program/js/editor.js 2 ●●● patch | view | raw | blame | history
program/js/list.js 7 ●●●●● patch | view | raw | blame | history
plugins/archive/archive.js
@@ -27,7 +27,7 @@
  // check if current folder is an archive folder or one of its children
  if (rcmail.env.mailbox == rcmail.env.archive_folder
    || rcmail.env.mailbox.indexOf(rcmail.env.archive_folder + rcmail.env.delimiter) != 0
    || rcmail.env.mailbox.startsWith(rcmail.env.archive_folder + rcmail.env.delimiter)
  ) {
    return true;
  }
program/js/app.js
@@ -1474,7 +1474,7 @@
      // select the folder if one of its childs is currently selected
      // don't select if it's virtual (#1488346)
      if (this.env.mailbox && this.env.mailbox.indexOf(name + this.env.delimiter) == 0 && !node.virtual)
      if (this.env.mailbox && this.env.mailbox.startsWith(name + this.env.delimiter) && !node.virtual)
        this.command('list', name);
    }
    else {
@@ -1655,8 +1655,8 @@
    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/, '');
      if (cols[i].id && cols[i].id.startsWith('rcm')) {
        name = cols[i].id.slice(3);
        this.env.coltypes.push(name);
      }
@@ -3035,8 +3035,8 @@
    return (this.env.exists && (
      this.env.mailbox == this.env.trash_mailbox
      || this.env.mailbox == this.env.junk_mailbox
      || this.env.mailbox.indexOf(this.env.trash_mailbox + this.env.delimiter) == 0
      || this.env.mailbox.indexOf(this.env.junk_mailbox + this.env.delimiter) == 0
      || this.env.mailbox.startsWith(this.env.trash_mailbox + this.env.delimiter)
      || this.env.mailbox.startsWith(this.env.junk_mailbox + this.env.delimiter)
    ));
  };
@@ -4182,7 +4182,7 @@
      return;
    // ...new search value contains old one and previous search was not finished or its result was empty
    if (old_value && old_value.length && q.indexOf(old_value) == 0 && (!ac || ac.num <= 0) && this.env.contacts && !this.env.contacts.length)
    if (old_value && old_value.length && q.startsWith(old_value) && (!ac || ac.num <= 0) && this.env.contacts && !this.env.contacts.length)
      return;
    var i, lock, source, xhr, reqid = new Date().getTime(),
@@ -5467,7 +5467,7 @@
        if (this.check_droptarget(folder) &&
            !this.env.subscriptionrows[this.get_folder_row_id(this.env.mailbox)][2] &&
            (folder != this.env.mailbox.replace(reg, '')) &&
            (!folder.match(new RegExp('^'+RegExp.escape(this.env.mailbox+this.env.delimiter))))
            (!folder.startsWith(this.env.mailbox + this.env.delimiter))
        ) {
          this.env.dstfolder = folder;
          $(row).addClass('droptarget');
@@ -5593,7 +5593,7 @@
        tmp = tmp_name;
      }
      // protected folder's child
      else if (tmp && folders[n][0].indexOf(tmp) == 0)
      else if (tmp && folders[n][0].startsWith(tmp))
        slist.push(folders[n][0]);
      // other
      else {
@@ -5604,7 +5604,7 @@
    // check if subfolder of a protected folder
    for (n=0; n<slist.length; n++) {
      if (name.indexOf(slist[n]+this.env.delimiter) == 0)
      if (name.startsWith(slist[n] + this.env.delimiter))
        rowid = this.get_folder_row_id(slist[n]);
    }
@@ -5716,13 +5716,13 @@
  this.get_subfolders = function(folder)
  {
    var name, list = [],
      regex = new RegExp('^'+RegExp.escape(folder)+RegExp.escape(this.env.delimiter)),
      prefix = folder + this.env.delimiter,
      row = $('#'+this.get_folder_row_id(folder)).get(0);
    while (row = row.nextSibling) {
      if (row.id) {
        name = this.env.subscriptionrows[row.id][0];
        if (regex.test(name)) {
        if (name && name.startsWith(prefix)) {
          list.push(row.id);
        }
        else
@@ -6325,7 +6325,7 @@
          div.className.match(/collapsed/)) {
        // add children's counters
        for (var k in this.env.unread_counts)
          if (k.indexOf(mbox + this.env.delimiter) == 0)
          if (k.startsWith(mbox + this.env.delimiter))
            childcount += this.env.unread_counts[k];
      }
program/js/common.js
@@ -592,6 +592,14 @@
  return tzo;
}
// define String's startsWith() method for old browsers
if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(search, position) {
    position = position || 0;
    return this.slice(position, search.length) === search;
  };
}
// Make getElementById() case-sensitive on IE
if (bw.ie) {
  document._getElementById = document.getElementById;
program/js/editor.js
@@ -161,7 +161,7 @@
  for (i in files) {
    att = files[i];
    if (att.complete && att.mimetype.indexOf('image/') == 0) {
    if (att.complete && att.mimetype.startsWith('image/')) {
      list.push([att.name, rcmail.env.comm_path+'&_action=display-attachment&_file='+i+'&_id='+rcmail.env.compose_id]);
    }
  }
program/js/list.js
@@ -31,6 +31,7 @@
  this.list = list ? list : null;
  this.tagname = this.list ? this.list.nodeName.toLowerCase() : 'table';
  this.id_regexp = /^rcmrow([a-z0-9\-_=\+\/]+)/i;
  this.thead;
  this.tbody;
  this.fixed_header;
@@ -110,7 +111,7 @@
init_row: function(row)
{
  // make references in internal array and set event handlers
  if (row && String(row.id).match(/^rcmrow([a-z0-9\-_=\+\/]+)/i)) {
  if (row && String(row.id).match(this.id_regexp)) {
    var self = this,
      uid = RegExp.$1;
    row.uid = uid;
@@ -728,7 +729,7 @@
    var i, len, rows = this.tbody.childNodes;
    for (i=0, len=rows.length-1; i<len; i++)
      if (rows[i].id && String(rows[i].id).match(/^rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
      if (rows[i].id && String(rows[i].id).match(this.id_regexp) && this.rows[RegExp.$1] != null)
        return RegExp.$1;
  }
@@ -741,7 +742,7 @@
    var i, rows = this.tbody.childNodes;
    for (i=rows.length-1; i>=0; i--)
      if (rows[i].id && String(rows[i].id).match(/^rcmrow([a-z0-9\-_=\+\/]+)/i) && this.rows[RegExp.$1] != null)
      if (rows[i].id && String(rows[i].id).match(this.id_regexp) && this.rows[RegExp.$1] != null)
        return RegExp.$1;
  }