Skip to content

Commit

Permalink
Improved folders sorting by name - use Intl.Collator if supported
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed May 28, 2014
1 parent 20ef295 commit 2441264
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions program/include/rcmail_output_html.php
Expand Up @@ -68,6 +68,7 @@ public function __construct($task = null, $framed = false)
$this->set_env('task', $task);
$this->set_env('x_frame_options', $this->config->get('x_frame_options', 'sameorigin'));
$this->set_env('standard_windows', (bool) $this->config->get('standard_windows'));
$this->set_env('locale', $_SESSION['language']);

// add cookie info
$this->set_env('cookie_domain', ini_get('session.cookie_domain'));
Expand Down
43 changes: 26 additions & 17 deletions program/js/app.js
Expand Up @@ -5898,7 +5898,8 @@ function rcube_webmail()
if (!this.gui_objects.subscriptionlist)
return false;

var row, n, i, tmp, tmp_name, rowid, folders = [], list = [], slist = [],
var row, n, i, tmp, tmp_name, rowid, collator,
folders = [], list = [], slist = [],
tbody = this.gui_objects.subscriptionlist.tBodies[0],
refrow = $('tr', tbody).get(1),
id = 'rcmrow'+((new Date).getTime());
Expand All @@ -5925,24 +5926,32 @@ function rcube_webmail()
// add to folder/row-ID map
this.env.subscriptionrows[id] = [name, display_name, false];

// 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) {
if (v.length < 4) {
var n = v[0];
n = n.replace(replace_from, replace_to);
v.push(n);
}
folders.push(v);
});
// copy folders data to an array for sorting
$.each(this.env.subscriptionrows, function(k, v) { folders.push(v); });

try {
// use collator if supported (FF29, IE11, Opera15, Chrome24)
collator = new Intl.Collator(this.env.locale.replace('_', '-'));
}
catch (e) {};

// sort folders
folders.sort(function(a, b) {
var len = a.length - 1; n1 = a[len], n2 = b[len];
return n1 < n2 ? -1 : 1;
var i, f1, f2,
path1 = a[0].split(ref.env.delimiter),
path2 = b[0].split(ref.env.delimiter);

for (i=0; i<path1.length; i++) {
f1 = path1[i];
f2 = path2[i];

if (f1 !== f2) {
if (collator)
return collator.compare(f1, f2);
else
return f1 < f2 ? -1 : 1;
}
}
});

for (n in folders) {
Expand Down

0 comments on commit 2441264

Please sign in to comment.