mirror of
https://github.com/zulip/zulip.git
synced 2025-11-08 07:52:19 +00:00
Move stream sidebar functions to their own module
(imported from commit 50771c1590eb3a0dbb151bf6a798bd14a4418857)
This commit is contained in:
@@ -282,6 +282,7 @@ PIPELINE_JS = {
|
||||
'js/util.js',
|
||||
'js/setup.js',
|
||||
'js/rows.js',
|
||||
'js/stream_list.js',
|
||||
'js/narrow.js',
|
||||
'js/reload.js',
|
||||
'js/notifications_bar.js',
|
||||
|
||||
@@ -17,7 +17,7 @@ var globals =
|
||||
// Modules, defined in their respective files.
|
||||
+ ' compose rows hotkeys narrow reload notifications_bar search subs'
|
||||
+ ' composebox_typeahead typeahead_helper notifications hashchange'
|
||||
+ ' invite ui util activity timerender MessageList blueslip'
|
||||
+ ' invite ui util activity timerender MessageList blueslip stream_list'
|
||||
|
||||
// colorspace.js
|
||||
+ ' colorspace'
|
||||
|
||||
194
zephyr/static/js/stream_list.js
Normal file
194
zephyr/static/js/stream_list.js
Normal file
@@ -0,0 +1,194 @@
|
||||
var stream_list = (function () {
|
||||
|
||||
var exports = {};
|
||||
|
||||
exports.sort_narrow_list = function () {
|
||||
var sort_recent = (subs.subscribed_streams().length > 40);
|
||||
var items = $('#stream_filters > li').get();
|
||||
var parent = $('#stream_filters');
|
||||
items.sort(function(a,b){
|
||||
var a_stream_name = $(a).attr('data-name');
|
||||
var b_stream_name = $(b).attr('data-name');
|
||||
if (sort_recent) {
|
||||
if (recent_subjects[b_stream_name] !== undefined &&
|
||||
recent_subjects[a_stream_name] === undefined) {
|
||||
return 1;
|
||||
} else if (recent_subjects[b_stream_name] === undefined &&
|
||||
recent_subjects[a_stream_name] !== undefined) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return util.strcmp(a_stream_name, b_stream_name);
|
||||
});
|
||||
|
||||
parent.empty();
|
||||
|
||||
$.each(items, function(i, li){
|
||||
var stream_name = $(li).attr('data-name');
|
||||
if (sort_recent) {
|
||||
if (recent_subjects[stream_name] === undefined) {
|
||||
$(li).addClass("inactive_stream");
|
||||
} else {
|
||||
$(li).removeClass("inactive_stream");
|
||||
}
|
||||
}
|
||||
parent.append(li);
|
||||
});
|
||||
};
|
||||
|
||||
function iterate_to_find(selector, data_name, context) {
|
||||
var retval = $();
|
||||
$(selector, context).each(function (idx, elem) {
|
||||
var jelem = $(elem);
|
||||
if (jelem.attr('data-name') === data_name) {
|
||||
retval = jelem;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return retval;
|
||||
}
|
||||
|
||||
exports.get_filter_li = function(type, name) {
|
||||
if (type === 'stream') {
|
||||
return $("#stream_sidebar_" + subs.stream_id(name));
|
||||
}
|
||||
return iterate_to_find("#" + type + "_filters > li", name);
|
||||
};
|
||||
|
||||
exports.get_subject_filter_li = function(stream, subject) {
|
||||
var stream_li = exports.get_filter_li('stream', stream);
|
||||
return iterate_to_find(".expanded_subjects li", subject, stream_li);
|
||||
};
|
||||
|
||||
exports.add_narrow_filter = function(name, type) {
|
||||
var uri = "#narrow/stream/" + hashchange.encodeHashComponent(name);
|
||||
var list_item;
|
||||
|
||||
if (exports.get_filter_li(type, name).length) {
|
||||
// already exists
|
||||
return false;
|
||||
}
|
||||
|
||||
// For some reason, even though the span is inline-block, if it is empty it
|
||||
// takes up no space and you don't see the background color. Thus, add a
|
||||
// to get the inline-block behavior we want.
|
||||
var swatch = $('<span/>').attr('id', "stream_sidebar_swatch_" + subs.stream_id(name))
|
||||
.addClass('streamlist_swatch')
|
||||
.css('background-color', subs.get_color(name)).html(" ");
|
||||
list_item = $('<li>').attr('data-name', name).html(swatch);
|
||||
if (type === 'stream') {
|
||||
list_item.attr('id', "stream_sidebar_" + subs.stream_id(name));
|
||||
}
|
||||
|
||||
list_item.append($('<a>').attr('href', uri)
|
||||
.addClass('subscription_name')
|
||||
.text(name)
|
||||
.append('<span class="count">(<span class="value"></span>)</span>'));
|
||||
if (type === "stream" && subs.have(name).invite_only) {
|
||||
list_item.append("<i class='icon-lock'/>");
|
||||
}
|
||||
$("#" + type + "_filters").append(list_item);
|
||||
};
|
||||
|
||||
exports.get_count = function (type, name) {
|
||||
return exports.get_filter_li(type, name).find('.count .value').text();
|
||||
};
|
||||
|
||||
exports.set_count = function (type, name, count) {
|
||||
var count_span = exports.get_filter_li(type, name).find('.count');
|
||||
var value_span = count_span.find('.value');
|
||||
|
||||
if (count === 0) {
|
||||
return exports.clear_count(type, name);
|
||||
}
|
||||
count_span.show();
|
||||
|
||||
value_span.text(count);
|
||||
};
|
||||
|
||||
exports.clear_count = function (type, name) {
|
||||
exports.get_filter_li(type, name).find('.count').hide()
|
||||
.find('.value').text('');
|
||||
};
|
||||
|
||||
exports.remove_narrow_filter = function (name, type) {
|
||||
exports.get_filter_li(type, name).remove();
|
||||
};
|
||||
|
||||
exports.remove_all_narrow_filters = function () {
|
||||
$("#stream_filters").children().remove();
|
||||
};
|
||||
|
||||
function rebuild_recent_subjects(stream, subject) {
|
||||
$('.expanded_subjects').remove();
|
||||
var stream_li = exports.get_filter_li('stream', stream);
|
||||
var subjects = recent_subjects[stream] || [];
|
||||
|
||||
stream_li.append(templates.render('sidebar_subject_list',
|
||||
{subjects: subjects,
|
||||
stream: stream}));
|
||||
if (subject !== undefined) {
|
||||
exports.get_subject_filter_li(stream, subject).addClass('active-subject-filter');
|
||||
}
|
||||
}
|
||||
|
||||
exports.update_streams_sidebar = function () {
|
||||
exports.sort_narrow_list();
|
||||
|
||||
if (! narrow.active()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var op_stream = narrow.filter().operands('stream');
|
||||
var op_subject = narrow.filter().operands('subject');
|
||||
var subject;
|
||||
if (op_stream.length !== 0) {
|
||||
if (op_subject.length !== 0) {
|
||||
subject = op_subject[0];
|
||||
}
|
||||
if (subs.have(op_stream[0])) {
|
||||
rebuild_recent_subjects(op_stream[0], subject);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$(function () {
|
||||
$(document).on('narrow_activated.zephyr', function (event) {
|
||||
$("ul.filters li").removeClass('active-filter active-subject-filter');
|
||||
|
||||
// TODO: handle confused filters like "in:all stream:foo"
|
||||
var op_in = event.filter.operands('in');
|
||||
if (op_in.length !== 0) {
|
||||
if (['all', 'home'].indexOf(op_in[0]) !== -1) {
|
||||
$("#global_filters li[data-name='" + op_in[0] + "']").addClass('active-filter');
|
||||
}
|
||||
}
|
||||
var op_is = event.filter.operands('is');
|
||||
if (op_is.length !== 0) {
|
||||
if (['private-message', 'starred'].indexOf(op_is[0]) !== -1) {
|
||||
$("#global_filters li[data-name='" + op_is[0] + "']").addClass('active-filter');
|
||||
}
|
||||
}
|
||||
var op_stream = event.filter.operands('stream');
|
||||
if (op_stream.length !== 0 && subs.have(op_stream[0])) {
|
||||
var stream_li = exports.get_filter_li('stream', op_stream[0]);
|
||||
var op_subject = event.filter.operands('subject');
|
||||
var subject;
|
||||
if (op_subject.length !== 0) {
|
||||
subject = op_subject[0];
|
||||
} else {
|
||||
stream_li.addClass('active-filter');
|
||||
}
|
||||
rebuild_recent_subjects(op_stream[0], subject);
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on('narrow_deactivated.zephyr', function (event) {
|
||||
$("ul.filters li").removeClass('active-filter active-subject-filter');
|
||||
$("ul.expanded_subjects").remove();
|
||||
$("#global_filters li[data-name='home']").addClass('active-filter');
|
||||
});
|
||||
});
|
||||
|
||||
return exports;
|
||||
}());
|
||||
@@ -242,7 +242,7 @@ function create_sub(stream_name, attrs) {
|
||||
add_sub(stream_name, sub);
|
||||
$(document).trigger($.Event('sub_obj_created.zephyr', {sub: sub}));
|
||||
if (sub.subscribed) {
|
||||
ui.add_narrow_filter(stream_name, "stream");
|
||||
stream_list.add_narrow_filter(stream_name, "stream");
|
||||
}
|
||||
return sub;
|
||||
}
|
||||
@@ -289,7 +289,7 @@ function mark_subscribed(stream_name, attrs) {
|
||||
sub.subscribed = true;
|
||||
set_color(stream_name, pick_color());
|
||||
mark_color_used(sub.color);
|
||||
ui.add_narrow_filter(stream_name, "stream");
|
||||
stream_list.add_narrow_filter(stream_name, "stream");
|
||||
var settings = settings_for_sub(sub);
|
||||
var button = button_for_sub(sub);
|
||||
if (button.length !== 0) {
|
||||
@@ -320,7 +320,7 @@ function mark_subscribed(stream_name, attrs) {
|
||||
// Update unread counts as the new stream in sidebar might
|
||||
// need its unread counts re-calculated
|
||||
process_loaded_for_unread(all_msg_list.all());
|
||||
ui.sort_narrow_list();
|
||||
stream_list.sort_narrow_list();
|
||||
|
||||
typeahead_helper.update_autocomplete();
|
||||
|
||||
@@ -334,7 +334,7 @@ function mark_unsubscribed(stream_name) {
|
||||
// We don't know about this stream
|
||||
return;
|
||||
} else if (sub.subscribed) {
|
||||
ui.remove_narrow_filter(stream_name, 'stream');
|
||||
stream_list.remove_narrow_filter(stream_name, 'stream');
|
||||
sub.subscribed = false;
|
||||
button_for_sub(sub).text("Subscribe").addClass("btn-primary");
|
||||
var settings = settings_for_sub(sub);
|
||||
@@ -473,7 +473,7 @@ function populate_subscriptions(subs) {
|
||||
sub_rows.push(sub);
|
||||
});
|
||||
|
||||
ui.sort_narrow_list();
|
||||
stream_list.sort_narrow_list();
|
||||
return sub_rows;
|
||||
}
|
||||
|
||||
@@ -491,7 +491,7 @@ exports.reload_subscriptions = function (opts) {
|
||||
|
||||
if (opts.clear_first) {
|
||||
stream_info = {};
|
||||
ui.remove_all_narrow_filters();
|
||||
stream_list.remove_all_narrow_filters();
|
||||
}
|
||||
|
||||
return $.ajax({
|
||||
|
||||
@@ -533,19 +533,6 @@ exports.hide_loading_more_messages_indicator = function () {
|
||||
}
|
||||
};
|
||||
|
||||
function rebuild_recent_subjects(stream, subject) {
|
||||
$('.expanded_subjects').remove();
|
||||
var stream_li = exports.get_filter_li('stream', stream);
|
||||
var subjects = recent_subjects[stream] || [];
|
||||
|
||||
stream_li.append(templates.render('sidebar_subject_list',
|
||||
{subjects: subjects,
|
||||
stream: stream}));
|
||||
if (subject !== undefined) {
|
||||
exports.get_subject_filter_li(stream, subject).addClass('active-subject-filter');
|
||||
}
|
||||
}
|
||||
|
||||
$(function () {
|
||||
// NB: This just binds to current elements, and won't bind to elements
|
||||
// created after ready() is called.
|
||||
@@ -1159,43 +1146,6 @@ $(function () {
|
||||
}
|
||||
});
|
||||
|
||||
// side-bar-related handlers
|
||||
$(document).on('narrow_activated.zephyr', function (event) {
|
||||
$("ul.filters li").removeClass('active-filter active-subject-filter');
|
||||
|
||||
// TODO: handle confused filters like "in:all stream:foo"
|
||||
var op_in = event.filter.operands('in');
|
||||
if (op_in.length !== 0) {
|
||||
if (['all', 'home'].indexOf(op_in[0]) !== -1) {
|
||||
$("#global_filters li[data-name='" + op_in[0] + "']").addClass('active-filter');
|
||||
}
|
||||
}
|
||||
var op_is = event.filter.operands('is');
|
||||
if (op_is.length !== 0) {
|
||||
if (['private-message', 'starred'].indexOf(op_is[0]) !== -1) {
|
||||
$("#global_filters li[data-name='" + op_is[0] + "']").addClass('active-filter');
|
||||
}
|
||||
}
|
||||
var op_stream = event.filter.operands('stream');
|
||||
if (op_stream.length !== 0 && subs.have(op_stream[0])) {
|
||||
var stream_li = exports.get_filter_li('stream', op_stream[0]);
|
||||
var op_subject = event.filter.operands('subject');
|
||||
var subject;
|
||||
if (op_subject.length !== 0) {
|
||||
subject = op_subject[0];
|
||||
} else {
|
||||
stream_li.addClass('active-filter');
|
||||
}
|
||||
rebuild_recent_subjects(op_stream[0], subject);
|
||||
}
|
||||
});
|
||||
|
||||
$(document).on('narrow_deactivated.zephyr', function (event) {
|
||||
$("ul.filters li").removeClass('active-filter active-subject-filter');
|
||||
$("ul.expanded_subjects").remove();
|
||||
$("#global_filters li[data-name='home']").addClass('active-filter');
|
||||
});
|
||||
|
||||
// initialize other stuff
|
||||
typeahead_helper.update_all_recipients(page_params.people_list);
|
||||
composebox_typeahead.initialize();
|
||||
@@ -1208,123 +1158,6 @@ $(function () {
|
||||
tutorial.initialize();
|
||||
});
|
||||
|
||||
exports.sort_narrow_list = function () {
|
||||
var sort_recent = (subs.subscribed_streams().length > 40);
|
||||
var items = $('#stream_filters > li').get();
|
||||
var parent = $('#stream_filters');
|
||||
items.sort(function(a,b){
|
||||
var a_stream_name = $(a).attr('data-name');
|
||||
var b_stream_name = $(b).attr('data-name');
|
||||
if (sort_recent) {
|
||||
if (recent_subjects[b_stream_name] !== undefined &&
|
||||
recent_subjects[a_stream_name] === undefined) {
|
||||
return 1;
|
||||
} else if (recent_subjects[b_stream_name] === undefined &&
|
||||
recent_subjects[a_stream_name] !== undefined) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return util.strcmp(a_stream_name, b_stream_name);
|
||||
});
|
||||
|
||||
parent.empty();
|
||||
|
||||
$.each(items, function(i, li){
|
||||
var stream_name = $(li).attr('data-name');
|
||||
if (sort_recent) {
|
||||
if (recent_subjects[stream_name] === undefined) {
|
||||
$(li).addClass("inactive_stream");
|
||||
} else {
|
||||
$(li).removeClass("inactive_stream");
|
||||
}
|
||||
}
|
||||
parent.append(li);
|
||||
});
|
||||
};
|
||||
|
||||
function iterate_to_find(selector, data_name, context) {
|
||||
var retval = $();
|
||||
$(selector, context).each(function (idx, elem) {
|
||||
var jelem = $(elem);
|
||||
if (jelem.attr('data-name') === data_name) {
|
||||
retval = jelem;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return retval;
|
||||
}
|
||||
|
||||
exports.get_filter_li = function(type, name) {
|
||||
if (type === 'stream') {
|
||||
return $("#stream_sidebar_" + subs.stream_id(name));
|
||||
}
|
||||
return iterate_to_find("#" + type + "_filters > li", name);
|
||||
};
|
||||
|
||||
exports.get_subject_filter_li = function(stream, subject) {
|
||||
var stream_li = exports.get_filter_li('stream', stream);
|
||||
return iterate_to_find(".expanded_subjects li", subject, stream_li);
|
||||
};
|
||||
|
||||
exports.add_narrow_filter = function(name, type) {
|
||||
var uri = "#narrow/stream/" + hashchange.encodeHashComponent(name);
|
||||
var list_item;
|
||||
|
||||
if (exports.get_filter_li(type, name).length) {
|
||||
// already exists
|
||||
return false;
|
||||
}
|
||||
|
||||
// For some reason, even though the span is inline-block, if it is empty it
|
||||
// takes up no space and you don't see the background color. Thus, add a
|
||||
// to get the inline-block behavior we want.
|
||||
var swatch = $('<span/>').attr('id', "stream_sidebar_swatch_" + subs.stream_id(name))
|
||||
.addClass('streamlist_swatch')
|
||||
.css('background-color', subs.get_color(name)).html(" ");
|
||||
list_item = $('<li>').attr('data-name', name).html(swatch);
|
||||
if (type === 'stream') {
|
||||
list_item.attr('id', "stream_sidebar_" + subs.stream_id(name));
|
||||
}
|
||||
|
||||
list_item.append($('<a>').attr('href', uri)
|
||||
.addClass('subscription_name')
|
||||
.text(name)
|
||||
.append('<span class="count">(<span class="value"></span>)</span>'));
|
||||
if (type === "stream" && subs.have(name).invite_only) {
|
||||
list_item.append("<i class='icon-lock'/>");
|
||||
}
|
||||
$("#" + type + "_filters").append(list_item);
|
||||
};
|
||||
|
||||
exports.get_count = function (type, name) {
|
||||
return exports.get_filter_li(type, name).find('.count .value').text();
|
||||
};
|
||||
|
||||
exports.set_count = function (type, name, count) {
|
||||
var count_span = exports.get_filter_li(type, name).find('.count');
|
||||
var value_span = count_span.find('.value');
|
||||
|
||||
if (count === 0) {
|
||||
return exports.clear_count(type, name);
|
||||
}
|
||||
count_span.show();
|
||||
|
||||
value_span.text(count);
|
||||
};
|
||||
|
||||
exports.clear_count = function (type, name) {
|
||||
exports.get_filter_li(type, name).find('.count').hide()
|
||||
.find('.value').text('');
|
||||
};
|
||||
|
||||
exports.remove_narrow_filter = function (name, type) {
|
||||
exports.get_filter_li(type, name).remove();
|
||||
};
|
||||
|
||||
exports.remove_all_narrow_filters = function () {
|
||||
$("#stream_filters").children().remove();
|
||||
};
|
||||
|
||||
var presence_descriptions = {
|
||||
active: ' is active',
|
||||
away: ' was recently active',
|
||||
@@ -1436,25 +1269,5 @@ exports.process_condensing = function (index, elem) {
|
||||
}
|
||||
};
|
||||
|
||||
exports.update_streams_sidebar = function () {
|
||||
exports.sort_narrow_list();
|
||||
|
||||
if (! narrow.active()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var op_stream = narrow.filter().operands('stream');
|
||||
var op_subject = narrow.filter().operands('subject');
|
||||
var subject;
|
||||
if (op_stream.length !== 0) {
|
||||
if (op_subject.length !== 0) {
|
||||
subject = op_subject[0];
|
||||
}
|
||||
if (subs.have(op_stream[0])) {
|
||||
rebuild_recent_subjects(op_stream[0], subject);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return exports;
|
||||
}());
|
||||
|
||||
@@ -298,7 +298,7 @@ function update_unread_counts() {
|
||||
}
|
||||
|
||||
var count = Object.keys(obj).length;
|
||||
ui.set_count("stream", index, count);
|
||||
stream_list.set_count("stream", index, count);
|
||||
if (narrow.stream_in_home(index)) {
|
||||
home_unread_messages += newer_than_pointer_count(only_in_home_view(Object.keys(obj)));
|
||||
}
|
||||
@@ -308,7 +308,7 @@ function update_unread_counts() {
|
||||
$.each(unread_counts["private"], function(index, obj) {
|
||||
pm_count += newer_than_pointer_count(only_in_home_view(Object.keys(obj)));
|
||||
});
|
||||
ui.set_count("global", "private", pm_count);
|
||||
stream_list.set_count("global", "private", pm_count);
|
||||
home_unread_messages += pm_count;
|
||||
|
||||
}
|
||||
@@ -840,7 +840,7 @@ function get_updates(options) {
|
||||
process_visible_unread_messages();
|
||||
notifications.received_messages(messages);
|
||||
compose.update_faded_messages();
|
||||
ui.update_streams_sidebar();
|
||||
stream_list.update_streams_sidebar();
|
||||
}
|
||||
|
||||
if (new_pointer !== undefined
|
||||
@@ -920,7 +920,7 @@ function load_old_messages(opts) {
|
||||
add_messages(messages, opts.msg_list);
|
||||
}
|
||||
|
||||
ui.update_streams_sidebar();
|
||||
stream_list.update_streams_sidebar();
|
||||
|
||||
if (opts.cont !== undefined) {
|
||||
opts.cont(messages);
|
||||
|
||||
Reference in New Issue
Block a user