2016-10-27 01:02:41 +02:00
|
|
|
var topic_list = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2016-11-10 15:35:14 +01:00
|
|
|
// We can only ever have one active widget.
|
|
|
|
var active_widget;
|
2016-10-27 01:36:20 +02:00
|
|
|
|
2016-11-10 16:15:12 +01:00
|
|
|
// We know whether we're zoomed or not.
|
|
|
|
var zoomed = false;
|
|
|
|
|
2016-10-27 01:53:47 +02:00
|
|
|
exports.remove_expanded_topics = function () {
|
2017-03-05 17:28:40 +01:00
|
|
|
stream_popover.hide_topic_popover();
|
2016-10-27 01:53:47 +02:00
|
|
|
|
2016-11-10 15:35:14 +01:00
|
|
|
if (active_widget) {
|
|
|
|
active_widget.remove();
|
2017-08-11 00:30:23 +02:00
|
|
|
active_widget = undefined;
|
2016-11-10 15:35:14 +01:00
|
|
|
}
|
|
|
|
};
|
2016-10-27 01:36:20 +02:00
|
|
|
|
2017-08-11 00:30:23 +02:00
|
|
|
exports.close = function () {
|
|
|
|
zoomed = false;
|
|
|
|
exports.remove_expanded_topics();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.zoom_out = function () {
|
|
|
|
zoomed = false;
|
|
|
|
exports.rebuild(active_widget.get_parent(), active_widget.get_stream_id());
|
|
|
|
};
|
|
|
|
|
2016-11-12 01:01:20 +01:00
|
|
|
function update_unread_count(unread_count_elem, count) {
|
|
|
|
// unread_count_elem is a jquery element...we expect DOM
|
|
|
|
// to look like this:
|
2016-11-12 01:14:34 +01:00
|
|
|
// <div class="topic-unread-count {{#if is_zero}}zero_count{{/if}}">
|
2016-11-12 01:01:20 +01:00
|
|
|
// <div class="value">{{unread}}</div>
|
|
|
|
// </div>
|
|
|
|
var value_span = unread_count_elem.find('.value');
|
|
|
|
|
|
|
|
if (value_span.length === 0) {
|
|
|
|
blueslip.error('malformed dom for unread count');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-27 01:02:41 +02:00
|
|
|
if (count === 0) {
|
2016-11-12 01:01:20 +01:00
|
|
|
unread_count_elem.hide();
|
2016-10-27 01:02:41 +02:00
|
|
|
value_span.text('');
|
|
|
|
} else {
|
2016-11-12 01:01:20 +01:00
|
|
|
unread_count_elem.removeClass("zero_count");
|
|
|
|
unread_count_elem.show();
|
2016-10-27 01:02:41 +02:00
|
|
|
value_span.text(count);
|
|
|
|
}
|
2016-10-29 19:39:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
exports.set_count = function (stream_id, topic, count) {
|
|
|
|
if (active_widget && active_widget.is_for_stream(stream_id)) {
|
2016-11-10 15:35:14 +01:00
|
|
|
active_widget.set_count(topic, count);
|
2016-10-27 01:36:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-25 16:51:16 +02:00
|
|
|
exports.widget = function (parent_elem, my_stream_id) {
|
2016-10-29 20:19:25 +02:00
|
|
|
var self = {};
|
2016-10-27 02:02:40 +02:00
|
|
|
|
2017-09-25 16:38:39 +02:00
|
|
|
self.build_list = function () {
|
2017-09-22 20:37:05 +02:00
|
|
|
self.topic_items = new Dict({fold_case: true});
|
2017-05-13 19:26:54 +02:00
|
|
|
|
2017-09-22 21:51:00 +02:00
|
|
|
var max_topics = 5;
|
2017-07-24 18:22:37 +02:00
|
|
|
var topic_names = topic_data.get_recent_names(my_stream_id);
|
2017-09-22 20:37:05 +02:00
|
|
|
var my_stream_name = stream_data.get_sub_by_id(my_stream_id).name;
|
2016-10-27 02:02:40 +02:00
|
|
|
|
2016-11-05 19:34:47 +01:00
|
|
|
var ul = $('<ul class="topic-list">');
|
2017-05-14 18:06:57 +02:00
|
|
|
ul.attr('data-stream', my_stream_name);
|
2016-10-29 20:19:25 +02:00
|
|
|
|
2017-07-24 15:15:28 +02:00
|
|
|
_.each(topic_names, function (topic_name, idx) {
|
2017-07-31 14:11:18 +02:00
|
|
|
var num_unread = unread.num_unread_for_topic(my_stream_id, topic_name);
|
2016-10-29 20:19:25 +02:00
|
|
|
|
2017-08-23 08:38:56 +02:00
|
|
|
if (!zoomed) {
|
2016-11-10 20:46:01 +01:00
|
|
|
// Show the most recent topics, as well as any with unread messages
|
2017-08-23 08:38:56 +02:00
|
|
|
var show_topic = (idx < max_topics) || (num_unread > 0) ||
|
2017-09-25 16:38:39 +02:00
|
|
|
(self.active_topic === topic_name.toLowerCase());
|
2016-11-10 20:46:01 +01:00
|
|
|
|
|
|
|
if (!show_topic) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-29 20:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var topic_info = {
|
|
|
|
topic_name: topic_name,
|
|
|
|
unread: num_unread,
|
|
|
|
is_zero: num_unread === 0,
|
2017-05-14 18:06:57 +02:00
|
|
|
is_muted: muting.is_topic_muted(my_stream_name, topic_name),
|
|
|
|
url: narrow.by_stream_subject_uri(my_stream_name, topic_name),
|
2016-10-29 20:19:25 +02:00
|
|
|
};
|
2016-10-29 20:42:13 +02:00
|
|
|
var li = $(templates.render('topic_list_item', topic_info));
|
|
|
|
self.topic_items.set(topic_name, li);
|
2016-10-29 20:19:25 +02:00
|
|
|
ul.append(li);
|
|
|
|
});
|
|
|
|
|
2017-09-21 20:44:31 +02:00
|
|
|
var show_more = self.build_more_topics_section();
|
|
|
|
ul.append(show_more);
|
2016-10-27 02:02:40 +02:00
|
|
|
|
2016-10-29 20:19:25 +02:00
|
|
|
return ul;
|
2017-09-22 20:37:05 +02:00
|
|
|
};
|
2016-10-27 02:02:40 +02:00
|
|
|
|
2017-09-21 20:44:31 +02:00
|
|
|
self.build_more_topics_section = function () {
|
|
|
|
var show_more_html = templates.render('more_topics');
|
|
|
|
return $(show_more_html);
|
|
|
|
};
|
|
|
|
|
2016-11-10 20:05:14 +01:00
|
|
|
self.get_parent = function () {
|
|
|
|
return parent_elem;
|
|
|
|
};
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
self.is_for_stream = function (stream_id) {
|
|
|
|
return stream_id === my_stream_id;
|
2016-11-10 15:35:14 +01:00
|
|
|
};
|
|
|
|
|
2017-05-14 18:06:57 +02:00
|
|
|
self.get_stream_id = function () {
|
|
|
|
return my_stream_id;
|
2016-11-10 20:19:22 +01:00
|
|
|
};
|
|
|
|
|
2016-10-29 20:19:25 +02:00
|
|
|
self.get_dom = function () {
|
|
|
|
return self.dom;
|
|
|
|
};
|
|
|
|
|
2016-11-10 15:35:14 +01:00
|
|
|
self.remove = function () {
|
|
|
|
self.dom.remove();
|
|
|
|
};
|
|
|
|
|
2017-09-22 03:26:10 +02:00
|
|
|
self.num_items = function () {
|
|
|
|
return self.topic_items.num_items();
|
|
|
|
};
|
|
|
|
|
2016-10-29 21:01:07 +02:00
|
|
|
self.set_count = function (topic, count) {
|
|
|
|
if (!self.topic_items.has(topic)) {
|
2016-11-16 19:36:23 +01:00
|
|
|
// This can happen for truncated topic lists. No need
|
|
|
|
// to warn about it.
|
2016-10-29 21:01:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var topic_li = self.topic_items.get(topic);
|
2016-11-12 01:14:34 +01:00
|
|
|
var unread_count_elem = topic_li.find('.topic-unread-count').expectOne();
|
2016-10-29 21:01:07 +02:00
|
|
|
|
2016-11-12 01:01:20 +01:00
|
|
|
update_unread_count(unread_count_elem, count);
|
2016-10-29 21:01:07 +02:00
|
|
|
};
|
|
|
|
|
2017-09-25 16:38:39 +02:00
|
|
|
self.activate_topic = function () {
|
|
|
|
var li = self.topic_items.get(self.active_topic);
|
2016-10-29 20:42:13 +02:00
|
|
|
if (li) {
|
|
|
|
li.addClass('active-sub-filter');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-22 00:12:55 +02:00
|
|
|
self.show_spinner = function () {
|
|
|
|
// The spinner will go away once we get results and redraw
|
|
|
|
// the whole list.
|
|
|
|
var spinner = self.dom.find('.searching-for-more-topics');
|
|
|
|
spinner.show();
|
|
|
|
};
|
|
|
|
|
2017-09-22 03:26:10 +02:00
|
|
|
self.show_no_more_topics = function () {
|
|
|
|
var elem = self.dom.find('.no-more-topics-found');
|
|
|
|
elem.show();
|
|
|
|
self.no_more_topics = true;
|
|
|
|
};
|
|
|
|
|
2017-09-25 19:22:54 +02:00
|
|
|
self.show_help_link = function () {
|
|
|
|
var elem = self.dom.find('.sidebar-topics-help');
|
|
|
|
elem.show();
|
|
|
|
};
|
|
|
|
|
2017-09-22 03:26:10 +02:00
|
|
|
self.build = function (active_topic, no_more_topics) {
|
|
|
|
self.no_more_topics = false; // for now
|
|
|
|
|
2017-09-25 16:38:39 +02:00
|
|
|
if (active_topic) {
|
|
|
|
active_topic = active_topic.toLowerCase();
|
|
|
|
}
|
|
|
|
self.active_topic = active_topic;
|
|
|
|
|
|
|
|
self.dom = self.build_list();
|
2016-10-29 21:01:07 +02:00
|
|
|
|
2017-09-22 22:54:10 +02:00
|
|
|
parent_elem.append(self.dom);
|
2016-11-10 20:05:14 +01:00
|
|
|
|
2017-09-22 03:26:10 +02:00
|
|
|
// We often rebuild an entire topic list, and the
|
|
|
|
// caller will pass us in no_more_topics as true
|
|
|
|
// if we were showing "No more topics found" from
|
|
|
|
// the initial zooming.
|
|
|
|
if (no_more_topics) {
|
|
|
|
self.show_no_more_topics();
|
|
|
|
}
|
|
|
|
|
2017-09-25 19:22:54 +02:00
|
|
|
if (zoomed) {
|
|
|
|
self.show_help_link();
|
|
|
|
}
|
|
|
|
|
2017-09-22 22:54:10 +02:00
|
|
|
if (active_topic) {
|
2017-09-25 16:38:39 +02:00
|
|
|
self.activate_topic();
|
2017-09-22 22:54:10 +02:00
|
|
|
}
|
|
|
|
};
|
2016-11-10 20:05:14 +01:00
|
|
|
|
2016-10-29 20:19:25 +02:00
|
|
|
return self;
|
2016-10-27 02:02:40 +02:00
|
|
|
};
|
|
|
|
|
2017-08-11 00:29:35 +02:00
|
|
|
exports.active_stream_id = function () {
|
|
|
|
if (!active_widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return active_widget.get_stream_id();
|
|
|
|
};
|
|
|
|
|
2017-09-22 03:26:10 +02:00
|
|
|
exports.need_to_show_no_more_topics = function (stream_id) {
|
|
|
|
// This function is important, and the use case here is kind of
|
|
|
|
// subtle. We do complete redraws of the topic list when new
|
|
|
|
// messages come in, and we don't want to overwrite the
|
|
|
|
// "no more topics" error message.
|
|
|
|
if (!zoomed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_id !== active_widget.get_stream_id()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return active_widget.no_more_topics;
|
|
|
|
};
|
|
|
|
|
2017-05-14 18:06:57 +02:00
|
|
|
exports.rebuild = function (stream_li, stream_id) {
|
2017-04-25 15:25:31 +02:00
|
|
|
var active_topic = narrow_state.topic();
|
2017-09-22 03:26:10 +02:00
|
|
|
var no_more_topics = exports.need_to_show_no_more_topics(stream_id);
|
|
|
|
|
2016-10-27 02:33:24 +02:00
|
|
|
exports.remove_expanded_topics();
|
2017-09-25 16:51:16 +02:00
|
|
|
active_widget = exports.widget(stream_li, stream_id);
|
2017-09-22 03:26:10 +02:00
|
|
|
active_widget.build(active_topic, no_more_topics);
|
2016-10-27 02:33:24 +02:00
|
|
|
};
|
|
|
|
|
2016-11-10 16:15:12 +01:00
|
|
|
// For zooming, we only do topic-list stuff here...let stream_list
|
|
|
|
// handle hiding/showing the non-narrowed streams
|
|
|
|
exports.zoom_in = function () {
|
|
|
|
zoomed = true;
|
2016-11-10 20:46:01 +01:00
|
|
|
|
|
|
|
if (!active_widget) {
|
|
|
|
blueslip.error('Cannot find widget for topic history zooming.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-08 20:29:15 +02:00
|
|
|
var stream_id = active_widget.get_stream_id();
|
2017-09-22 03:26:10 +02:00
|
|
|
var before_count = active_widget.num_items();
|
2017-08-08 20:29:15 +02:00
|
|
|
|
|
|
|
function on_success() {
|
2017-09-22 17:34:50 +02:00
|
|
|
if ((!active_widget) || (stream_id !== active_widget.get_stream_id())) {
|
|
|
|
blueslip.warn('User re-narrowed before topic history was returned.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!zoomed) {
|
|
|
|
blueslip.warn('User zoomed out before topic history was returned.');
|
|
|
|
// Note that we could attempt to re-draw the zoomed out topic list
|
|
|
|
// here, given that we have more history, but that might be more
|
|
|
|
// confusing than helpful to a user who is likely trying to browse
|
|
|
|
// other streams.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-08 20:29:15 +02:00
|
|
|
exports.rebuild(active_widget.get_parent(), stream_id);
|
2017-09-22 03:26:10 +02:00
|
|
|
|
|
|
|
var after_count = active_widget.num_items();
|
|
|
|
|
|
|
|
if (after_count === before_count) {
|
|
|
|
active_widget.show_no_more_topics();
|
|
|
|
}
|
|
|
|
|
2017-08-08 20:29:15 +02:00
|
|
|
$('#stream-filters-container').perfectScrollbar('update');
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:49:08 +02:00
|
|
|
$('#stream-filters-container').scrollTop(0);
|
|
|
|
$('#stream-filters-container').perfectScrollbar('update');
|
2017-09-22 00:12:55 +02:00
|
|
|
active_widget.show_spinner();
|
2017-09-21 19:03:23 +02:00
|
|
|
topic_data.get_server_history(stream_id, on_success);
|
2016-11-10 16:15:12 +01:00
|
|
|
};
|
|
|
|
|
2016-10-27 03:47:05 +02:00
|
|
|
exports.set_click_handlers = function (callbacks) {
|
|
|
|
$('#stream_filters').on('click', '.show-more-topics', function (e) {
|
2017-08-11 01:33:35 +02:00
|
|
|
callbacks.zoom_in({
|
|
|
|
stream_id: active_widget.get_stream_id(),
|
|
|
|
});
|
2016-10-27 03:47:05 +02:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.show-all-streams').on('click', function (e) {
|
2017-08-10 12:04:01 +02:00
|
|
|
callbacks.zoom_out({
|
|
|
|
stream_li: active_widget.get_parent(),
|
|
|
|
});
|
2016-10-27 03:47:05 +02:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2016-11-05 20:13:36 +01:00
|
|
|
$('#stream_filters').on('click', '.topic-box', function (e) {
|
2016-10-27 03:47:05 +02:00
|
|
|
if (e.metaKey || e.ctrlKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In a more componentized world, we would delegate some
|
|
|
|
// of this stuff back up to our parents.
|
2017-05-27 15:40:54 +02:00
|
|
|
if (overlays.is_active()) {
|
2017-03-18 21:35:35 +01:00
|
|
|
ui_util.change_tab_to('#home');
|
2016-10-27 03:47:05 +02:00
|
|
|
}
|
|
|
|
|
2017-08-17 01:58:14 +02:00
|
|
|
var stream_id = $(e.target).parents('.narrow-filter').attr('data-stream-id');
|
|
|
|
var sub = stream_data.get_sub_by_id(stream_id);
|
2017-08-17 01:43:37 +02:00
|
|
|
var topic = $(e.target).parents('li').attr('data-topic-name');
|
2016-10-27 03:47:05 +02:00
|
|
|
|
2017-08-17 01:58:14 +02:00
|
|
|
narrow.activate([{operator: 'stream', operand: sub.name},
|
2016-10-27 03:47:05 +02:00
|
|
|
{operator: 'topic', operand: topic}],
|
|
|
|
{select_first_unread: true, trigger: 'sidebar'});
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-27 01:36:20 +02:00
|
|
|
|
2016-10-27 01:02:41 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = topic_list;
|
|
|
|
}
|