summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2013-10-30 19:51:55 +0100
committerAleksander Machniak <machniak@kolabsys.com>2013-10-30 19:51:55 +0100
commit6a91448aee5d036b35c621bbdaff250dc84e15f3 (patch)
tree4cc4c149e05b163d8a2a209471d07b13344e5801
parent8d9dcca2b6fc1117e170195aa4104980e5bfa238 (diff)
Improve performance and code readability by using String's startsWith() method, other code improvements
-rw-r--r--plugins/archive/archive.js2
-rw-r--r--program/js/app.js24
-rw-r--r--program/js/common.js8
-rw-r--r--program/js/editor.js2
-rw-r--r--program/js/list.js7
5 files changed, 26 insertions, 17 deletions
diff --git a/plugins/archive/archive.js b/plugins/archive/archive.js
index 17a1ecdb4..2e5267425 100644
--- a/plugins/archive/archive.js
+++ b/plugins/archive/archive.js
@@ -27,7 +27,7 @@ function rcmail_is_archive()
// 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;
}
diff --git a/program/js/app.js b/program/js/app.js
index 55dea3620..1f75e219c 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -1474,7 +1474,7 @@ function rcube_webmail()
// 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 @@ function rcube_webmail()
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 @@ function rcube_webmail()
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 @@ function rcube_webmail()
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 @@ function rcube_webmail()
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 @@ function rcube_webmail()
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 @@ function rcube_webmail()
// 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 @@ function rcube_webmail()
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 @@ function rcube_webmail()
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];
}
diff --git a/program/js/common.js b/program/js/common.js
index afaf91639..02934f734 100644
--- a/program/js/common.js
+++ b/program/js/common.js
@@ -592,6 +592,14 @@ Date.prototype.getStdTimezoneOffset = function()
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;
diff --git a/program/js/editor.js b/program/js/editor.js
index 6d7b9538a..b349c9c35 100644
--- a/program/js/editor.js
+++ b/program/js/editor.js
@@ -161,7 +161,7 @@ function rcmail_editor_images()
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]);
}
}
diff --git a/program/js/list.js b/program/js/list.js
index 46d9d2757..b2d4f8a63 100644
--- a/program/js/list.js
+++ b/program/js/list.js
@@ -31,6 +31,7 @@ function rcube_list_widget(list, p)
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: function()
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 @@ get_first_row: function()
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 @@ get_last_row: function()
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;
}