Create stream_data.js module.

This pulls a lot of data-centric functions out of subs.js.

(imported from commit 0deed7d4bf5697e893af9bc9d888c2d5da8d9fa2)
This commit is contained in:
Steve Howell
2013-08-15 15:11:07 -04:00
parent 64ac31b9af
commit 29c012dc74
21 changed files with 159 additions and 154 deletions

View File

@@ -76,7 +76,7 @@ function hide_box() {
function update_lock_icon_for_stream(stream_name) { function update_lock_icon_for_stream(stream_name) {
var icon = $("#compose-lock-icon"); var icon = $("#compose-lock-icon");
if (subs.get_invite_only(stream_name)) { if (stream_data.get_invite_only(stream_name)) {
icon.show(); icon.show();
} else { } else {
icon.hide(); icon.hide();
@@ -88,7 +88,7 @@ function update_lock_icon_for_stream(stream_name) {
// (In particular, if there's a color associated with it, // (In particular, if there's a color associated with it,
// have that color be reflected here too.) // have that color be reflected here too.)
exports.decorate_stream_bar = function (stream_name) { exports.decorate_stream_bar = function (stream_name) {
var color = subs.get_color(stream_name); var color = stream_data.get_color(stream_name);
update_lock_icon_for_stream(stream_name); update_lock_icon_for_stream(stream_name);
$("#stream-message .message_header_stream") $("#stream-message .message_header_stream")
.css('background-color', color) .css('background-color', color)
@@ -449,7 +449,7 @@ function validate_stream_message() {
var response; var response;
if (!subs.is_subscribed(stream_name)) { if (!stream_data.is_subscribed(stream_name)) {
switch(check_stream_for_send(stream_name)) { switch(check_stream_for_send(stream_name)) {
case "does-not-exist": case "does-not-exist":
response = "<p>The stream <b>" + response = "<p>The stream <b>" +

View File

@@ -211,7 +211,7 @@ exports.initialize = function () {
// limit number of items so the list doesn't fall off the screen // limit number of items so the list doesn't fall off the screen
$( "#stream" ).typeahead({ $( "#stream" ).typeahead({
source: function (query, process) { source: function (query, process) {
return subs.subscribed_streams(); return stream_data.subscribed_streams();
}, },
items: 3, items: 3,
highlighter: function (item) { highlighter: function (item) {

View File

@@ -5,7 +5,7 @@ function message_in_home(message) {
return true; return true;
} }
return subs.in_home_view(message.stream); return stream_data.in_home_view(message.stream);
} }
function Filter(operators) { function Filter(operators) {
@@ -80,7 +80,7 @@ Filter.prototype = {
return _.map(operators_mixed_case, function (operator) { return _.map(operators_mixed_case, function (operator) {
// We may want to consider allowing mixed-case operators at some point // We may want to consider allowing mixed-case operators at some point
return [Filter.canonicalize_operator(operator[0]), return [Filter.canonicalize_operator(operator[0]),
subs.canonicalized_name(operator[1])]; stream_data.canonicalized_name(operator[1])];
}); });
}, },

View File

@@ -8,8 +8,8 @@ function update_subscription_checkboxes() {
// checkboxes are saved from invocation to invocation (which is // checkboxes are saved from invocation to invocation (which is
// nice if I want to invite a bunch of people at once) // nice if I want to invite a bunch of people at once)
var streams = []; var streams = [];
_.each(subs.subscribed_streams(), function (value) { _.each(stream_data.subscribed_streams(), function (value) {
streams.push({name: value, invite_only: subs.get_invite_only(value)}); streams.push({name: value, invite_only: stream_data.get_invite_only(value)});
}); });
$('#streams_to_add').html(templates.render('invite_subscription', {streams: streams})); $('#streams_to_add').html(templates.render('invite_subscription', {streams: streams}));
} }

View File

@@ -398,9 +398,9 @@ MessageList.prototype = {
function set_template_properties(message) { function set_template_properties(message) {
if (message.is_stream) { if (message.is_stream) {
message.background_color = subs.get_color(message.stream); message.background_color = stream_data.get_color(message.stream);
message.color_class = stream_color.get_color_class(message.background_color); message.color_class = stream_color.get_color_class(message.background_color);
message.invite_only = subs.get_invite_only(message.stream); message.invite_only = stream_data.get_invite_only(message.stream);
} }
} }
@@ -755,7 +755,7 @@ MessageList.prototype = {
if (stream === undefined) { if (stream === undefined) {
return; return;
} }
var trailing_bookend_content, subscribed = subs.is_subscribed(stream); var trailing_bookend_content, subscribed = stream_data.is_subscribed(stream);
if (subscribed) { if (subscribed) {
if (this.last_message_historical) { if (this.last_message_historical) {
trailing_bookend_content = "--- Subscribed to stream " + stream + " ---"; trailing_bookend_content = "--- Subscribed to stream " + stream + " ---";

View File

@@ -448,7 +448,7 @@ function pick_empty_narrow_banner() {
// You have no private messages. // You have no private messages.
return $("#empty_narrow_all_private_message"); return $("#empty_narrow_all_private_message");
} }
} else if ((first_operator === "stream") && !subs.is_subscribed(first_operand)) { } else if ((first_operator === "stream") && !stream_data.is_subscribed(first_operand)) {
// You are narrowed to a stream to which you aren't subscribed. // You are narrowed to a stream to which you aren't subscribed.
return $("#nonsubbed_stream_narrow_message"); return $("#nonsubbed_stream_narrow_message");
} else if (first_operator === "search") { } else if (first_operator === "search") {

View File

@@ -232,7 +232,7 @@ exports.register_click_handlers = function () {
var ypos = $(elt).offset().top - viewport.scrollTop(); var ypos = $(elt).offset().top - viewport.scrollTop();
$(elt).popover({ $(elt).popover({
content: templates.render('stream_sidebar_actions', {'stream': subs.get(stream)}), content: templates.render('stream_sidebar_actions', {'stream': stream_data.get_sub(stream)}),
trigger: "manual", trigger: "manual",
fixed: true fixed: true
}); });
@@ -256,7 +256,7 @@ exports.register_click_handlers = function () {
} }
$(elt).popover("show"); $(elt).popover("show");
var popover = $('.streams_popover[data-id=' + subs.get(stream).id + ']'); var popover = $('.streams_popover[data-id=' + stream_data.get_sub(stream).id + ']');
update_spectrum(popover, function (colorpicker) { update_spectrum(popover, function (colorpicker) {
colorpicker.spectrum(stream_color.sidebar_popover_colorpicker_options); colorpicker.spectrum(stream_color.sidebar_popover_colorpicker_options);
}); });

View File

@@ -112,7 +112,7 @@ function get_stream_suggestions(operators) {
return []; return [];
} }
var streams = subs.subscribed_streams(); var streams = stream_data.subscribed_streams();
streams = _.filter(streams, function (stream) { streams = _.filter(streams, function (stream) {
return stream_matches_query(stream, query); return stream_matches_query(stream, query);
@@ -279,7 +279,7 @@ function get_topic_suggestions(query_operators) {
return []; return [];
} }
stream = subs.canonicalized_name(stream); stream = stream_data.canonicalized_name(stream);
var topics = recent_subjects.get(stream); var topics = recent_subjects.get(stream);

83
static/js/stream_data.js Normal file
View File

@@ -0,0 +1,83 @@
var stream_data = (function () {
var exports = {};
var stream_info = new Dict(); // Maps lowercase stream name to stream properties object
exports.add_sub = function (stream_name, sub) {
stream_info.set(stream_name.toLowerCase(), sub);
};
exports.get_sub = function (stream_name) {
return stream_info.get(stream_name.toLowerCase());
};
exports.set_stream_info = function (new_stream_info) {
stream_info = new_stream_info;
};
exports.get_stream_info = function () {
return stream_info;
};
// List subscribed streams.
// Internal version returns the full stream info object for each stream.
function subscribed_streams() {
return _.where(stream_info.values(), {subscribed: true});
}
exports.subscribed_streams = function () {
return _.pluck(subscribed_streams(), 'name');
};
exports.get_colors = function () {
return _.pluck(subscribed_streams(), 'color');
};
exports.all_subscribed_streams_are_in_home_view = function () {
return _.every(subscribed_streams(), function (sub) {
return sub.in_home_view; }
);
};
exports.clear_subscriptions = function () {
stream_info = new Dict();
};
exports.canonicalized_name = function (stream_name) {
return stream_name.toString().toLowerCase();
};
exports.get_color = function (stream_name) {
var sub = exports.get_sub(stream_name);
if (sub === undefined) {
return stream_color.default_color;
}
return sub.color;
};
exports.in_home_view = function (stream_name) {
var sub = exports.get_sub(stream_name);
return sub !== undefined && sub.in_home_view;
};
exports.is_subscribed = function (stream_name) {
var sub = exports.get_sub(stream_name);
return sub !== undefined && sub.subscribed;
};
exports.get_invite_only = function (stream_name) {
var sub = exports.get_sub(stream_name);
if (sub === undefined) {
return false;
}
return sub.invite_only;
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = stream_data;
}

View File

@@ -7,7 +7,7 @@ var last_mention_count = 0;
var previous_sort_order; var previous_sort_order;
exports.sort_narrow_list = function () { exports.sort_narrow_list = function () {
var streams = subs.subscribed_streams(); var streams = stream_data.subscribed_streams();
if (streams.length === 0) { if (streams.length === 0) {
return; return;
} }
@@ -37,7 +37,7 @@ exports.sort_narrow_list = function () {
var elems = []; var elems = [];
_.each(streams, function (stream) { _.each(streams, function (stream) {
var li = $(subs.get(stream).sidebar_li); var li = $(stream_data.get_sub(stream).sidebar_li);
if (sort_recent) { if (sort_recent) {
if (! recent_subjects.has(stream)) { if (! recent_subjects.has(stream)) {
li.addClass('inactive_stream'); li.addClass('inactive_stream');
@@ -94,9 +94,9 @@ function add_narrow_filter(name, type) {
var args = {name: name, var args = {name: name,
id: subs.stream_id(name), id: subs.stream_id(name),
uri: narrow.by_stream_uri(name), uri: narrow.by_stream_uri(name),
not_in_home_view: (subs.in_home_view(name) === false), not_in_home_view: (stream_data.in_home_view(name) === false),
invite_only: subs.get(name).invite_only, invite_only: stream_data.get_sub(name).invite_only,
color: subs.get_color(name)}; color: stream_data.get_color(name)};
var list_item = $(templates.render('stream_sidebar_row', args)); var list_item = $(templates.render('stream_sidebar_row', args));
$("#" + type + "_filters").append(list_item); $("#" + type + "_filters").append(list_item);
return list_item; return list_item;
@@ -189,7 +189,7 @@ exports.update_streams_sidebar = function () {
if (op_subject.length !== 0) { if (op_subject.length !== 0) {
subject = op_subject[0]; subject = op_subject[0];
} }
if (subs.is_subscribed(op_stream[0])) { if (stream_data.is_subscribed(op_stream[0])) {
rebuild_recent_subjects(op_stream[0], subject); rebuild_recent_subjects(op_stream[0], subject);
} }
} }
@@ -272,7 +272,7 @@ $(function () {
} }
} }
var op_stream = event.filter.operands('stream'); var op_stream = event.filter.operands('stream');
if (op_stream.length !== 0 && subs.is_subscribed(op_stream[0])) { if (op_stream.length !== 0 && stream_data.is_subscribed(op_stream[0])) {
var stream_li = get_filter_li('stream', op_stream[0]); var stream_li = get_filter_li('stream', op_stream[0]);
var op_subject = event.filter.operands('topic'); var op_subject = event.filter.operands('topic');
var subject; var subject;

View File

@@ -2,37 +2,10 @@ var subs = (function () {
var exports = {}; var exports = {};
var stream_info = new Dict(); // Maps lowercase stream name to stream properties object
var next_sub_id = 0; var next_sub_id = 0;
function add_sub(stream_name, sub) {
stream_info.set(stream_name.toLowerCase(), sub);
}
function get_sub(stream_name) {
return stream_info.get(stream_name.toLowerCase());
}
exports.stream_info = function (new_stream_info) {
if (new_stream_info !== undefined) {
stream_info = new_stream_info;
} else {
return stream_info;
}
};
exports.subscribed_streams = function () {
return _.chain(stream_info.values())
.where({subscribed: true})
.pluck('name')
.value();
};
function get_color() { function get_color() {
var streams = _.values(stream_info); var used_colors = stream_data.get_colors();
var subscribed_streams = _.where(streams, {subscribed: true});
var used_colors = _.pluck(subscribed_streams, 'color');
var color = stream_color.pick_color(used_colors); var color = stream_color.pick_color(used_colors);
return color; return color;
} }
@@ -42,8 +15,7 @@ exports.update_all_messages_link = function () {
// the user has any subscriptions hidden from home view. // the user has any subscriptions hidden from home view.
var all_messages = $("#global_filters [data-name='all']")[0]; var all_messages = $("#global_filters [data-name='all']")[0];
if (_.every(_.where(stream_info.values(), {subscribed: true}), if (stream_data.all_subscribed_streams_are_in_home_view()) {
function (sub) { return sub.in_home_view; })) {
$(all_messages).addClass('hidden-filter'); $(all_messages).addClass('hidden-filter');
} else { } else {
$(all_messages).removeClass('hidden-filter'); $(all_messages).removeClass('hidden-filter');
@@ -59,7 +31,7 @@ function should_list_all_streams() {
} }
exports.stream_id = function (stream_name) { exports.stream_id = function (stream_name) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub === undefined) { if (sub === undefined) {
blueslip.error("Tried to get subs.stream_id for a stream user is not subscribed to!"); blueslip.error("Tried to get subs.stream_id for a stream user is not subscribed to!");
return 0; return 0;
@@ -67,10 +39,6 @@ exports.stream_id = function (stream_name) {
return parseInt(sub.id, 10); return parseInt(sub.id, 10);
}; };
exports.canonicalized_name = function (stream_name) {
return stream_name.toString().toLowerCase();
};
function set_stream_property(stream_name, property, value) { function set_stream_property(stream_name, property, value) {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
@@ -144,7 +112,7 @@ function update_in_home_view(sub, value) {
} }
exports.toggle_home = function (stream_name) { exports.toggle_home = function (stream_name) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
update_in_home_view(sub, ! sub.in_home_view); update_in_home_view(sub, ! sub.in_home_view);
set_stream_property(stream_name, 'in_home_view', sub.in_home_view); set_stream_property(stream_name, 'in_home_view', sub.in_home_view);
}; };
@@ -159,19 +127,19 @@ function stream_notifications_clicked(e) {
var sub_row = $(e.target).closest('.subscription_row'); var sub_row = $(e.target).closest('.subscription_row');
var stream = sub_row.find('.subscription_name').text(); var stream = sub_row.find('.subscription_name').text();
var sub = get_sub(stream); var sub = stream_data.get_sub(stream);
sub.notifications = ! sub.notifications; sub.notifications = ! sub.notifications;
set_stream_property(stream, 'notifications', sub.notifications); set_stream_property(stream, 'notifications', sub.notifications);
} }
exports.set_color = function (stream_name, color) { exports.set_color = function (stream_name, color) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
stream_color.update_stream_color(sub, stream_name, color, {update_historical: true}); stream_color.update_stream_color(sub, stream_name, color, {update_historical: true});
set_stream_property(stream_name, 'color', color); set_stream_property(stream_name, 'color', color);
}; };
function create_sub(stream_name, attrs) { function create_sub(stream_name, attrs) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub !== undefined) { if (sub !== undefined) {
// We've already created this subscription, no need to continue. // We've already created this subscription, no need to continue.
return sub; return sub;
@@ -191,7 +159,7 @@ function create_sub(stream_name, attrs) {
sub.color = get_color(); sub.color = get_color();
} }
add_sub(stream_name, sub); stream_data.add_sub(stream_name, sub);
$(document).trigger($.Event('sub_obj_created.zulip', {sub: sub})); $(document).trigger($.Event('sub_obj_created.zulip', {sub: sub}));
return sub; return sub;
} }
@@ -207,7 +175,7 @@ function settings_for_sub(sub) {
} }
exports.show_settings_for = function (stream_name) { exports.show_settings_for = function (stream_name) {
settings_for_sub(get_sub(stream_name)).collapse('show'); settings_for_sub(stream_data.get_sub(stream_name)).collapse('show');
}; };
function add_sub_to_table(sub) { function add_sub_to_table(sub) {
@@ -232,7 +200,7 @@ function add_to_member_list(ul, name, email) {
} }
function mark_subscribed(stream_name, attrs) { function mark_subscribed(stream_name, attrs) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub === undefined) { if (sub === undefined) {
// Create a new stream. // Create a new stream.
@@ -278,7 +246,7 @@ function mark_subscribed(stream_name, attrs) {
} }
function mark_unsubscribed(stream_name) { function mark_unsubscribed(stream_name) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub === undefined) { if (sub === undefined) {
// We don't know about this stream // We don't know about this stream
@@ -332,24 +300,8 @@ $(function () {
}); });
}); });
exports.get_color = function (stream_name) {
var sub = get_sub(stream_name);
if (sub === undefined) {
return stream_color.default_color;
}
return sub.color;
};
exports.get_invite_only = function (stream_name) {
var sub = get_sub(stream_name);
if (sub === undefined) {
return false;
}
return sub.invite_only;
};
exports.receives_notifications = function (stream_name) { exports.receives_notifications = function (stream_name) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub === undefined) { if (sub === undefined) {
return false; return false;
} }
@@ -387,7 +339,7 @@ exports.reload_subscriptions = function (opts) {
} }
if (opts.clear_first) { if (opts.clear_first) {
stream_info = new Dict(); stream_data.clear_subscriptions();
stream_list.remove_all_narrow_filters(); stream_list.remove_all_narrow_filters();
} }
@@ -403,14 +355,14 @@ exports.reload_subscriptions = function (opts) {
exports.setup_page = function () { exports.setup_page = function () {
util.make_loading_indicator($('#subs_page_loading_indicator')); util.make_loading_indicator($('#subs_page_loading_indicator'));
function populate_and_fill(stream_data, subscription_data) { function populate_and_fill(data_for_streams, subscription_data) {
var all_streams = []; var all_streams = [];
var our_subs = []; var our_subs = [];
var sub_rows = []; var sub_rows = [];
/* arguments are [ "success", statusText, jqXHR ] */ /* arguments are [ "success", statusText, jqXHR ] */
if (stream_data.length > 2 && stream_data[2]) { if (data_for_streams.length > 2 && data_for_streams[2]) {
var stream_response = JSON.parse(stream_data[2].responseText); var stream_response = JSON.parse(data_for_streams[2].responseText);
_.each(stream_response.streams, function (stream) { _.each(stream_response.streams, function (stream) {
all_streams.push(stream.name); all_streams.push(stream.name);
}); });
@@ -431,7 +383,7 @@ exports.setup_page = function () {
populate_subscriptions(our_subs, true); populate_subscriptions(our_subs, true);
all_streams.forEach(function (stream) { all_streams.forEach(function (stream) {
var sub = exports.get(stream); var sub = stream_data.get_sub(stream);
if (!sub) { if (!sub) {
sub = create_sub(stream, {subscribed: false}); sub = create_sub(stream, {subscribed: false});
} }
@@ -490,22 +442,8 @@ exports.setup_page = function () {
$.when.apply(this, requests).then(populate_and_fill, failed_listing); $.when.apply(this, requests).then(populate_and_fill, failed_listing);
}; };
exports.get = function (stream_name) {
return get_sub(stream_name);
};
exports.in_home_view = function (stream_name) {
var sub = get_sub(stream_name);
return sub !== undefined && sub.in_home_view;
};
exports.is_subscribed = function (stream_name) {
var sub = get_sub(stream_name);
return sub !== undefined && sub.subscribed;
};
exports.update_subscription_properties = function (stream_name, property, value) { exports.update_subscription_properties = function (stream_name, property, value) {
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
switch(property) { switch(property) {
case 'color': case 'color':
stream_color.update_stream_color(sub, stream_name, value, {update_historical: true}); stream_color.update_stream_color(sub, stream_name, value, {update_historical: true});
@@ -667,7 +605,7 @@ $(function () {
e.stopPropagation(); e.stopPropagation();
var sub_row = $(e.target).closest('.subscription_row'); var sub_row = $(e.target).closest('.subscription_row');
var stream_name = sub_row.find('.subscription_name').text(); var stream_name = sub_row.find('.subscription_name').text();
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub.subscribed) { if (sub.subscribed) {
ajaxUnsubscribe(stream_name); ajaxUnsubscribe(stream_name);
@@ -680,7 +618,7 @@ $(function () {
var subrow = $(e.target).closest('.subscription_row'); var subrow = $(e.target).closest('.subscription_row');
var colorpicker = subrow.find('.colorpicker'); var colorpicker = subrow.find('.colorpicker');
var color = exports.get_color(subrow.find('.subscription_name').text()); var color = stream_data.get_color(subrow.find('.subscription_name').text());
stream_color.set_colorpicker_color(colorpicker, color); stream_color.set_colorpicker_color(colorpicker, color);
// To figure out the worst case for an expanded row's height, we do some math: // To figure out the worst case for an expanded row's height, we do some math:
@@ -847,7 +785,7 @@ function focus_on_narrowed_stream() {
if (stream_name === undefined) { if (stream_name === undefined) {
return; return;
} }
var sub = get_sub(stream_name); var sub = stream_data.get_sub(stream_name);
if (sub !== undefined) { if (sub !== undefined) {
// This stream is in the list, so focus on it. // This stream is in the list, so focus on it.
$('html, body').animate({ $('html, body').animate({

View File

@@ -20,7 +20,7 @@ function make_tab_data() {
// Root breadcrumb item: Either Home or All Messages // Root breadcrumb item: Either Home or All Messages
if ((filter.has_operator("stream") && if ((filter.has_operator("stream") &&
!subs.in_home_view(filter.operands("stream")[0])) || !stream_data.in_home_view(filter.operands("stream")[0])) ||
filter.has_operand("in", "all")) { filter.has_operand("in", "all")) {
tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root")); tabs.push(make_tab("All Messages", "#narrow/in/all", undefined, "root"));
} else { } else {
@@ -101,7 +101,7 @@ exports.colorize_tab_bar = function () {
} }
stream_name = stream_name.toString(); stream_name = stream_name.toString();
var stream_color = subs.get_color(stream_name); var stream_color = stream_data.get_color(stream_name);
if (!stream_tab.hasClass('active')) { if (!stream_tab.hasClass('active')) {
stream_tab.css('border-color', stream_color); stream_tab.css('border-color', stream_color);

View File

@@ -246,7 +246,7 @@ function finale() {
current_msg_list.clear(); current_msg_list.clear();
// Force a check on new events before we re-render the message list. // Force a check on new events before we re-render the message list.
force_get_updates(); force_get_updates();
subs.stream_info(real_stream_info); stream_data.set_stream_info(real_stream_info);
util.show_first_run_message(); util.show_first_run_message();
current_msg_list.rerender(); current_msg_list.rerender();
enable_event_handlers(); enable_event_handlers();
@@ -407,8 +407,8 @@ exports.start = function () {
narrow.deactivate(); narrow.deactivate();
// Set temporarly colors for the streams used in the tutorial. // Set temporarly colors for the streams used in the tutorial.
real_stream_info = subs.stream_info(); real_stream_info = stream_data.get_stream_info();
subs.stream_info(tutorial_stream_info); stream_data.set_stream_info(tutorial_stream_info);
// Add the fake messages to the feed and get started. // Add the fake messages to the feed and get started.
current_msg_list.add_and_rerender(fake_messages); current_msg_list.add_and_rerender(fake_messages);
disable_event_handlers(); disable_event_handlers();

View File

@@ -12,7 +12,7 @@ var unread_subjects = new Dict();
function unread_hashkey(message) { function unread_hashkey(message) {
var hashkey; var hashkey;
if (message.type === 'stream') { if (message.type === 'stream') {
hashkey = subs.canonicalized_name(message.stream); hashkey = stream_data.canonicalized_name(message.stream);
} else { } else {
hashkey = message.reply_to; hashkey = message.reply_to;
} }
@@ -22,7 +22,7 @@ function unread_hashkey(message) {
} }
if (message.type === 'stream') { if (message.type === 'stream') {
var canon_subject = subs.canonicalized_name(message.subject); var canon_subject = stream_data.canonicalized_name(message.subject);
if (! unread_subjects.has(hashkey)) { if (! unread_subjects.has(hashkey)) {
unread_subjects.set(hashkey, new Dict()); unread_subjects.set(hashkey, new Dict());
} }
@@ -43,14 +43,14 @@ exports.message_unread = function (message) {
}; };
exports.update_unread_subjects = function (msg, event) { exports.update_unread_subjects = function (msg, event) {
var canon_stream = subs.canonicalized_name(msg.stream); var canon_stream = stream_data.canonicalized_name(msg.stream);
var canon_subject = subs.canonicalized_name(msg.subject); var canon_subject = stream_data.canonicalized_name(msg.subject);
if (event.subject !== undefined && if (event.subject !== undefined &&
unread_subjects.has(canon_stream) && unread_subjects.has(canon_stream) &&
unread_subjects.get(canon_stream).has(canon_subject) && unread_subjects.get(canon_stream).has(canon_subject) &&
unread_subjects.get(canon_stream).get(canon_subject)[msg.id]) { unread_subjects.get(canon_stream).get(canon_subject)[msg.id]) {
var new_canon_subject = subs.canonicalized_name(event.subject); var new_canon_subject = stream_data.canonicalized_name(event.subject);
// Move the unread subject count to the new subject // Move the unread subject count to the new subject
delete unread_subjects.get(canon_stream).get(canon_subject)[msg.id]; delete unread_subjects.get(canon_stream).get(canon_subject)[msg.id];
if (unread_subjects.get(canon_stream).get(canon_subject).length === 0) { if (unread_subjects.get(canon_stream).get(canon_subject).length === 0) {
@@ -74,7 +74,7 @@ exports.process_loaded_messages = function (messages) {
unread_counts[message.type].get(hashkey)[message.id] = true; unread_counts[message.type].get(hashkey)[message.id] = true;
if (message.type === 'stream') { if (message.type === 'stream') {
var canon_subject = subs.canonicalized_name(message.subject); var canon_subject = stream_data.canonicalized_name(message.subject);
unread_subjects.get(hashkey).get(canon_subject)[message.id] = true; unread_subjects.get(hashkey).get(canon_subject)[message.id] = true;
} }
@@ -88,8 +88,8 @@ exports.process_read_message = function (message) {
var hashkey = unread_hashkey(message); var hashkey = unread_hashkey(message);
delete unread_counts[message.type].get(hashkey)[message.id]; delete unread_counts[message.type].get(hashkey)[message.id];
if (message.type === 'stream') { if (message.type === 'stream') {
var canon_stream = subs.canonicalized_name(message.stream); var canon_stream = stream_data.canonicalized_name(message.stream);
var canon_subject = subs.canonicalized_name(message.subject); var canon_subject = stream_data.canonicalized_name(message.subject);
delete unread_subjects.get(canon_stream).get(canon_subject)[message.id]; delete unread_subjects.get(canon_stream).get(canon_subject)[message.id];
} }
delete unread_mentioned[message.id]; delete unread_mentioned[message.id];
@@ -131,14 +131,14 @@ exports.get_counts = function () {
} }
unread_counts.stream.each(function (msgs, stream) { unread_counts.stream.each(function (msgs, stream) {
if (! subs.is_subscribed(stream)) { if (! stream_data.is_subscribed(stream)) {
return true; return true;
} }
var count = Object.keys(msgs).length; var count = Object.keys(msgs).length;
res.stream_count.set(stream, count); res.stream_count.set(stream, count);
if (subs.in_home_view(stream)) { if (stream_data.in_home_view(stream)) {
res.home_unread_messages += only_in_home_view(Object.keys(msgs)).length; res.home_unread_messages += only_in_home_view(Object.keys(msgs)).length;
} }

View File

@@ -476,8 +476,8 @@ function unconditionally_send_pointer_update() {
function process_message_for_recent_subjects(message, remove_message) { function process_message_for_recent_subjects(message, remove_message) {
var current_timestamp = 0; var current_timestamp = 0;
var count = 0; var count = 0;
var canon_stream = subs.canonicalized_name(message.stream); var canon_stream = stream_data.canonicalized_name(message.stream);
var canon_subject = subs.canonicalized_name(message.subject); var canon_subject = stream_data.canonicalized_name(message.subject);
if (! recent_subjects.has(canon_stream)) { if (! recent_subjects.has(canon_stream)) {
recent_subjects.set(canon_stream, []); recent_subjects.set(canon_stream, []);

View File

@@ -26,7 +26,7 @@ var globals =
+ ' invite ui util activity timerender MessageList blueslip unread stream_list' + ' invite ui util activity timerender MessageList blueslip unread stream_list'
+ ' onboarding message_edit tab_bar emoji popovers navigate message_tour' + ' onboarding message_edit tab_bar emoji popovers navigate message_tour'
+ ' avatar feature_flags search_suggestion referral stream_color Dict' + ' avatar feature_flags search_suggestion referral stream_color Dict'
+ ' Filter summary admin' + ' Filter summary admin stream_data'
// colorspace.js // colorspace.js
+ ' colorspace' + ' colorspace'

View File

@@ -5,11 +5,10 @@ var assert = require('assert');
global.util = require('js/util.js'); global.util = require('js/util.js');
global.Dict = require('js/dict.js'); global.Dict = require('js/dict.js');
global.$ = function () {}; // for subs.js to load
global.subs = require('js/subs.js');
global.page_params = { global.page_params = {
domain: 'zulip.com' domain: 'zulip.com'
}; };
global.stream_data = require('js/stream_data.js');
global.Filter = require('js/filter.js'); global.Filter = require('js/filter.js');
}()); }());

View File

@@ -5,8 +5,7 @@ var assert = require('assert');
global.util = require('js/util.js'); global.util = require('js/util.js');
global.Dict = require('js/dict.js'); global.Dict = require('js/dict.js');
global.narrow = require('js/narrow.js'); global.narrow = require('js/narrow.js');
global.$ = function () {}; // for subs.js global.stream_data = require('js/stream_data.js');
global.subs = require('js/subs.js');
global.Filter = require('js/filter.js'); global.Filter = require('js/filter.js');
}()); }());

View File

@@ -19,10 +19,6 @@ function set_up_dependencies() {
email: 'bob@zulip.com' email: 'bob@zulip.com'
}; };
global.subs = {
canonicalized_name: function (name) { return name; }
};
global.typeahead_helper = require('js/typeahead_helper.js'); global.typeahead_helper = require('js/typeahead_helper.js');
global.util = require('js/util.js'); global.util = require('js/util.js');
@@ -30,6 +26,7 @@ function set_up_dependencies() {
global.recent_subjects = new global.Dict(); global.recent_subjects = new global.Dict();
global.Filter = require('js/filter.js'); global.Filter = require('js/filter.js');
global.stream_data = require('js/stream_data.js');
return search; return search;
} }
@@ -39,7 +36,7 @@ var search = set_up_dependencies();
(function test_basic_get_suggestions() { (function test_basic_get_suggestions() {
var query = 'fred'; var query = 'fred';
global.subs.subscribed_streams = function () { global.stream_data.subscribed_streams = function () {
return []; return [];
}; };
@@ -58,7 +55,7 @@ var search = set_up_dependencies();
(function test_empty_query_suggestions() { (function test_empty_query_suggestions() {
var query = ''; var query = '';
global.subs.subscribed_streams = function () { global.stream_data.subscribed_streams = function () {
return ['devel', 'office']; return ['devel', 'office'];
}; };
@@ -95,7 +92,7 @@ var search = set_up_dependencies();
(function test_topic_suggestions() { (function test_topic_suggestions() {
var query = 'te'; var query = 'te';
global.subs.subscribed_streams = function () { global.stream_data.subscribed_streams = function () {
return ['office']; return ['office'];
}; };
@@ -132,7 +129,7 @@ var search = set_up_dependencies();
(function test_whitespace_glitch() { (function test_whitespace_glitch() {
var query = 'stream:office '; // note trailing space var query = 'stream:office '; // note trailing space
global.subs.subscribed_streams = function () { global.stream_data.subscribed_streams = function () {
return ['office']; return ['office'];
}; };
@@ -154,7 +151,7 @@ var search = set_up_dependencies();
(function test_people_suggestions() { (function test_people_suggestions() {
var query = 'te'; var query = 'te';
global.subs.subscribed_streams = function () { global.stream_data.subscribed_streams = function () {
return []; return [];
}; };

View File

@@ -10,6 +10,7 @@
global._ = require('third/underscore/underscore.js'); global._ = require('third/underscore/underscore.js');
global.util = require('js/util.js'); global.util = require('js/util.js');
global.Dict = require('js/dict.js'); global.Dict = require('js/dict.js');
var stream_data = global.stream_data = require('js/stream_data.js');
var Dict = global.Dict; var Dict = global.Dict;
var unread = require('js/unread.js'); var unread = require('js/unread.js');
var assert = require('assert'); var assert = require('assert');
@@ -23,13 +24,6 @@ global.current_msg_list = current_msg_list;
var home_msg_list = {}; var home_msg_list = {};
global.home_msg_list = home_msg_list; global.home_msg_list = home_msg_list;
var subs = {};
global.subs = subs;
subs.canonicalized_name = function (name) {
return name;
};
var zero_counts = { var zero_counts = {
private_message_count: 0, private_message_count: 0,
home_unread_messages: 0, home_unread_messages: 0,
@@ -138,10 +132,10 @@ var zero_counts = {
narrow.active = function () { narrow.active = function () {
return false; return false;
}; };
subs.is_subscribed = function () { stream_data.is_subscribed = function () {
return true; return true;
}; };
subs.in_home_view = function () { stream_data.in_home_view = function () {
return true; return true;
}; };
@@ -172,12 +166,9 @@ var zero_counts = {
narrow.active = function () { narrow.active = function () {
return false; return false;
}; };
subs.is_subscribed = function () { stream_data.is_subscribed = function () {
return true; return true;
}; };
subs.in_home_view = function () {
return false;
};
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.private_message_count, 0); assert.equal(counts.private_message_count, 0);
@@ -201,12 +192,9 @@ var zero_counts = {
narrow.active = function () { narrow.active = function () {
return false; return false;
}; };
subs.is_subscribed = function () { stream_data.is_subscribed = function () {
return true; return true;
}; };
subs.in_home_view = function () {
return false;
};
var counts = unread.get_counts(); var counts = unread.get_counts();
assert.equal(counts.mentioned_message_count, 0); assert.equal(counts.mentioned_message_count, 0);

View File

@@ -379,6 +379,7 @@ JS_SPECS = {
'js/compose.js', 'js/compose.js',
'js/stream_color.js', 'js/stream_color.js',
'js/admin.js', 'js/admin.js',
'js/stream_data.js',
'js/subs.js', 'js/subs.js',
'js/message_edit.js', 'js/message_edit.js',
'js/ui.js', 'js/ui.js',