Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions notebook/static/tree/js/notebooklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ define([
return item_in(extension(filepath), extensionslist);
};

function name_sorter(ascending) {
function name_sorter(ascending, natural) {
return (function(a, b) {
if (type_order[a['type']] < type_order[b['type']]) {
return -1;
}
if (type_order[a['type']] > type_order[b['type']]) {
return 1;
}
if (natural) {
const res = natural_sort(a['name'], b['name']);
return (ascending) ? res : res * (-1);
}
if (a['name'].toLowerCase() < b['name'].toLowerCase()) {
return (ascending) ? -1 : 1;
}
Expand All @@ -54,6 +58,28 @@ define([
});
}

function natural_sort(a, b) {
/**
* Performs a natural (alphanumeric) sort on a and b without their
* respective extensions. If equal, sorts on their extensions.
**/
const a_sub = a.substring(0, a.lastIndexOf('.')) || a;
const b_sub = b.substring(0, b.lastIndexOf('.')) || b;
let res = a_sub.localeCompare(b_sub, undefined, {numeric: true, sensitivity: 'base'});
if (res === 0) {
let a_ext = a.substring(a.lastIndexOf('.'));
let b_ext = b.substring(b.lastIndexOf('.'));
if (a_ext === a ){
a_ext = ''
}
if (b_ext === b){
b_ext = ''
}
res = a_ext.localeCompare(b_ext, undefined, {numeric:true, sensitivity:'base'});
}
return res;
}

function modified_sorter(ascending) {
var order = ascending ? 1 : 0;
return (function(a, b) {
Expand Down Expand Up @@ -128,10 +154,10 @@ define([
function(e, d) { that.sessions_loaded(d); });
}
this.selected = [];
this.sort_function = name_sorter(1);
// 0 => descending, 1 => ascending
this.sort_direction = 1; // 0 => descending, 1 => ascending
this.natural_sorting = 1; // 0 => standard sorting, 1 => natural sorting
this.sort_function = name_sorter(this.sort_direction, this.natural_sorting);
this.sort_id = 'sort-name';
this.sort_direction = 1;
this._max_upload_size_mb = 25;
this.EDIT_MIMETYPES = [
'application/javascript',
Expand Down Expand Up @@ -261,14 +287,15 @@ define([
$("#" + sort_on + " i").addClass("fa-arrow-down");
that.sort_direction = 1;
}
that.sort_id = sort_on;
});
}
};

NotebookList.prototype.sort_list = function(id, order) {
if (sort_functions.hasOwnProperty(id)) {
this.sort_function = sort_functions[id](order);
this.sort_function = sort_functions[id](order, this.natural_sorting); // second argument is only used for name_sorter
// set global sort_id after sort_function as defined by id parameter has been set
this.sort_id = id;
this.draw_notebook_list(this.model_list, this.error_msg);
} else {
console.error("No such sort id: '" + id + "'")
Expand Down