Extract stream_data.sort_for_stream_settings().

We move some data code from subs.js to stream_data.js.

It's not clear we have been using the optimal sort for
dealing with locales, but this change preserves the
current behavior.  The only subtle change here is that
we look up subs using a Dict now instead of a plain
JS object.
This commit is contained in:
Steve Howell 2018-07-29 13:26:45 +00:00 committed by Tim Abbott
parent 5641c77c94
commit 064d0f3c89
4 changed files with 35 additions and 15 deletions

View File

@ -733,3 +733,10 @@ run_test('invite_streams', () => {
expected_list.push('Inviter');
assert.deepEqual(stream_data.invite_streams(), expected_list);
});
run_test('edge_cases', () => {
var bad_stream_ids = [555555, 99999];
// just make sure we don't explode
stream_data.sort_for_stream_settings(bad_stream_ids);
});

View File

@ -1,7 +1,7 @@
global.stub_out_jquery();
set_global('ui', {});
set_global('stream_data', {});
zrequire('stream_data');
global.patch_builtin('window', {
location: {
hash: "#streams/1/announce",
@ -55,6 +55,10 @@ run_test('filter_table', () => {
description: 'programming lang',
};
_.each(sub_row_data, function (sub) {
stream_data.add_sub(sub.name, sub);
});
var sub_stubs = [];
_.each(sub_row_data, function (data) {
var sub_row = ".stream-row-" + data.elem;
@ -76,10 +80,6 @@ run_test('filter_table', () => {
});
};
stream_data.get_sub_by_id = function (id) {
return sub_row_data[id];
};
$.stub_selector("#subscriptions_table .stream-row", sub_stubs);
var sub_table = $('#subscriptions_table .streams-list');

View File

@ -494,6 +494,27 @@ exports.get_streams_for_settings_page = function () {
return all_subs;
};
exports.sort_for_stream_settings = function (stream_ids) {
// TODO: We may want to simply use util.strcmp here,
// which uses Intl.Collator() when possible.
function name(stream_id) {
var sub = stream_data.get_sub_by_id(stream_id);
if (!sub) {
return '';
}
return sub.name.toLocaleLowerCase();
}
function by_stream_name(id_a, id_b) {
var stream_a_name = name(id_a);
var stream_b_name = name(id_b);
return String.prototype.localeCompare.call(stream_a_name, stream_b_name);
}
stream_ids.sort(by_stream_name);
};
exports.get_streams_for_admin = function () {
// Sort and combine all our streams.
function by_name(a,b) {

View File

@ -358,16 +358,9 @@ exports.filter_table = function (query) {
var stream_name_match_stream_ids = [];
var stream_description_match_stream_ids = [];
var other_stream_ids = [];
var stream_id_to_stream_name = {};
var widgets = {};
var streams_list_scrolltop = $(".streams-list").scrollTop();
function sort_by_stream_name(a, b) {
var stream_a_name = stream_id_to_stream_name[a].toLocaleLowerCase();
var stream_b_name = stream_id_to_stream_name[b].toLocaleLowerCase();
return String.prototype.localeCompare.call(stream_a_name, stream_b_name);
}
_.each($("#subscriptions_table .stream-row"), function (row) {
var sub = stream_data.get_sub_by_id($(row).attr("data-stream-id"));
sub.data_temp_view = $(row).attr("data-temp-view");
@ -383,7 +376,6 @@ exports.filter_table = function (query) {
other_stream_ids.push(sub.stream_id);
}
stream_id_to_stream_name[sub.stream_id] = sub.name;
widgets[sub.stream_id] = $(row).detach();
$(row).find('.sub-info-box [class$="-bar"] [class$="-count"]').tooltip({
@ -393,8 +385,8 @@ exports.filter_table = function (query) {
ui.update_scrollbar($("#subscription_overlay .streams-list"));
stream_name_match_stream_ids.sort(sort_by_stream_name);
stream_description_match_stream_ids.sort(sort_by_stream_name);
stream_data.sort_for_stream_settings(stream_name_match_stream_ids);
stream_data.sort_for_stream_settings(stream_description_match_stream_ids);
var all_stream_ids = [].concat(
stream_name_match_stream_ids,