2012-10-18 21:37:07 +02:00
|
|
|
var subs = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-01-07 23:30:21 +01:00
|
|
|
var stream_info = {}; // Maps lowercase stream name to stream properties object
|
2013-05-02 03:01:46 +02:00
|
|
|
|
2013-01-04 20:42:39 +01:00
|
|
|
var next_sub_id = 0;
|
2013-01-04 18:02:01 +01:00
|
|
|
|
2013-03-02 03:10:32 +01:00
|
|
|
function add_sub(stream_name, sub) {
|
|
|
|
stream_info[stream_name.toLowerCase()] = sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_sub(stream_name) {
|
|
|
|
return stream_info[stream_name.toLowerCase()];
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.stream_info = function (new_stream_info) {
|
2013-06-27 17:05:29 +02:00
|
|
|
if (new_stream_info !== undefined) {
|
|
|
|
stream_info = new_stream_info;
|
|
|
|
} else {
|
|
|
|
return stream_info;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-22 21:08:16 +01:00
|
|
|
exports.subscribed_streams = function () {
|
2013-07-30 00:35:44 +02:00
|
|
|
return _.chain(stream_info)
|
|
|
|
.values()
|
|
|
|
.filter(function (sub) { return sub.subscribed; })
|
|
|
|
.map(function (sub) { return sub.name; })
|
|
|
|
.value();
|
2013-01-22 21:08:16 +01:00
|
|
|
};
|
|
|
|
|
2013-08-07 20:23:12 +02:00
|
|
|
function get_color() {
|
|
|
|
var streams = _.values(stream_info);
|
|
|
|
var subscribed_streams = _.where(streams, {subscribed: true});
|
|
|
|
var used_colors = _.pluck(subscribed_streams, 'color');
|
|
|
|
var color = stream_color.pick_color(used_colors);
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2013-07-31 21:18:40 +02:00
|
|
|
exports.update_all_messages_link = function () {
|
|
|
|
// Show or hide the "All messages" link, depending on whether
|
|
|
|
// the user has any subscriptions hidden from home view.
|
2013-02-20 18:07:53 +01:00
|
|
|
var all_messages = $("#global_filters [data-name='all']")[0];
|
2013-07-31 21:18:40 +02:00
|
|
|
|
|
|
|
if (_.every(stream_info, function (sub) { return sub.in_home_view; })) {
|
2013-02-20 18:07:53 +01:00
|
|
|
$(all_messages).addClass('hidden-filter');
|
|
|
|
} else {
|
|
|
|
$(all_messages).removeClass('hidden-filter');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-24 18:20:00 +01:00
|
|
|
function should_render_subscribers() {
|
2013-03-25 23:26:14 +01:00
|
|
|
return page_params.domain !== 'mit.edu';
|
2013-01-07 22:18:47 +01:00
|
|
|
}
|
|
|
|
|
2013-01-23 17:51:01 +01:00
|
|
|
function should_list_all_streams() {
|
2013-03-25 23:26:14 +01:00
|
|
|
return page_params.domain !== 'mit.edu';
|
2013-01-23 17:51:01 +01:00
|
|
|
}
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.stream_id = function (stream_name) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
2013-05-06 18:33:03 +02:00
|
|
|
if (sub === undefined) {
|
|
|
|
blueslip.error("Tried to get subs.stream_id for a stream user is not subscribed to!");
|
|
|
|
return 0;
|
|
|
|
}
|
2013-03-02 03:10:32 +01:00
|
|
|
return parseInt(sub.id, 10);
|
|
|
|
};
|
|
|
|
|
2013-05-22 15:45:31 +02:00
|
|
|
exports.canonicalized_name = function (stream_name) {
|
|
|
|
return stream_name.toString().toLowerCase();
|
|
|
|
};
|
|
|
|
|
2013-04-08 19:43:11 +02:00
|
|
|
function set_stream_property(stream_name, property, value) {
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/subscriptions/property',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
"property": property,
|
|
|
|
"stream_name": stream_name,
|
|
|
|
"value": value
|
|
|
|
},
|
|
|
|
timeout: 10*1000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-02-12 22:32:14 +01:00
|
|
|
function stream_home_view_clicked(e) {
|
|
|
|
var sub_row = $(e.target).closest('.subscription_row');
|
|
|
|
var stream = sub_row.find('.subscription_name').text();
|
2013-05-14 21:00:28 +02:00
|
|
|
subs.toggle_home(stream);
|
|
|
|
}
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-07-16 22:47:30 +02:00
|
|
|
function update_in_home_view(sub, value) {
|
|
|
|
if (sub.in_home_view === value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sub.in_home_view = value;
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-02-20 16:28:52 +01:00
|
|
|
setTimeout(function () {
|
2013-07-03 22:35:41 +02:00
|
|
|
var scroll_offset, saved_ypos;
|
|
|
|
// Save our current scroll position
|
|
|
|
if (ui.home_tab_obscured()) {
|
|
|
|
saved_ypos = window.scrollY;
|
|
|
|
} else if (home_msg_list === current_msg_list) {
|
|
|
|
scroll_offset = current_msg_list.selected_row().offset().top - viewport.scrollTop();
|
|
|
|
}
|
2013-02-20 16:28:52 +01:00
|
|
|
|
2013-07-03 22:35:41 +02:00
|
|
|
home_msg_list.clear({clear_selected_id: false});
|
2013-02-22 20:48:31 +01:00
|
|
|
|
|
|
|
// Recreate the home_msg_list with the newly filtered all_msg_list
|
2013-04-10 16:18:09 +02:00
|
|
|
add_messages(all_msg_list.all(), home_msg_list);
|
2013-02-20 16:28:52 +01:00
|
|
|
|
2013-02-23 05:19:22 +01:00
|
|
|
// Ensure we're still at the same scroll position
|
2013-07-03 22:35:41 +02:00
|
|
|
if (ui.home_tab_obscured()) {
|
|
|
|
window.scrollTo(0, saved_ypos);
|
|
|
|
} else if (home_msg_list === current_msg_list) {
|
|
|
|
// We pass use_closest to handle the case where the
|
|
|
|
// currently selected message is being hidden from the
|
|
|
|
// home view
|
|
|
|
home_msg_list.select_id(home_msg_list.selected_id(),
|
|
|
|
{use_closest: true});
|
|
|
|
if (current_msg_list.selected_id() !== -1) {
|
|
|
|
viewport.scrollTop(current_msg_list.selected_row().offset().top - scroll_offset);
|
|
|
|
}
|
|
|
|
}
|
2013-02-20 16:28:52 +01:00
|
|
|
|
|
|
|
// In case we added messages to what's visible in the home view, we need to re-scroll to make
|
|
|
|
// sure the pointer is still visible. We don't want the auto-scroll handler to move our pointer
|
|
|
|
// to the old scroll location before we have a chance to update it.
|
|
|
|
recenter_pointer_on_display = true;
|
|
|
|
suppress_scroll_pointer_update = true;
|
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
if (! home_msg_list.empty()) {
|
2013-03-04 23:44:07 +01:00
|
|
|
process_loaded_for_unread(home_msg_list.all());
|
2013-02-20 16:28:52 +01:00
|
|
|
}
|
|
|
|
}, 0);
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-07-31 21:18:40 +02:00
|
|
|
exports.update_all_messages_link();
|
2013-07-16 22:47:30 +02:00
|
|
|
stream_list.set_in_home_view(sub.name, sub.in_home_view);
|
|
|
|
|
|
|
|
var in_home_view_checkbox = $("#subscription_" + sub.id + " #sub_setting_in_home_view .sub_setting_control");
|
|
|
|
in_home_view_checkbox.attr('checked', value);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.toggle_home = function (stream_name) {
|
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
update_in_home_view(sub, ! sub.in_home_view);
|
2013-05-14 21:00:28 +02:00
|
|
|
set_stream_property(stream_name, 'in_home_view', sub.in_home_view);
|
|
|
|
};
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-07-16 22:47:30 +02:00
|
|
|
function update_stream_notifications(sub, value) {
|
|
|
|
var in_home_view_checkbox = $("#subscription_" + sub.id + " #sub_setting_notifications .sub_setting_control");
|
|
|
|
in_home_view_checkbox.attr('checked', value);
|
|
|
|
sub.notifications = value;
|
|
|
|
}
|
|
|
|
|
2013-04-09 02:14:13 +02:00
|
|
|
function stream_notifications_clicked(e) {
|
|
|
|
var sub_row = $(e.target).closest('.subscription_row');
|
|
|
|
var stream = sub_row.find('.subscription_name').text();
|
|
|
|
|
|
|
|
var sub = get_sub(stream);
|
|
|
|
sub.notifications = ! sub.notifications;
|
|
|
|
set_stream_property(stream, 'notifications', sub.notifications);
|
|
|
|
}
|
|
|
|
|
2013-08-07 19:28:06 +02:00
|
|
|
exports.set_color = function (stream_name, color) {
|
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
stream_color.update_stream_color(sub, stream_name, color, {update_historical: true});
|
2013-04-08 19:43:11 +02:00
|
|
|
set_stream_property(stream_name, 'color', color);
|
2013-05-17 21:35:17 +02:00
|
|
|
};
|
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function create_sub(stream_name, attrs) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
if (sub !== undefined) {
|
|
|
|
// We've already created this subscription, no need to continue.
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
2013-07-30 05:11:50 +02:00
|
|
|
sub = _.defaults({}, attrs, {
|
|
|
|
name: stream_name,
|
|
|
|
id: next_sub_id++,
|
|
|
|
render_subscribers: should_render_subscribers(),
|
|
|
|
subscribed: true,
|
|
|
|
in_home_view: true,
|
|
|
|
invite_only: false,
|
2013-08-07 20:23:12 +02:00
|
|
|
notifications: false
|
2013-07-30 05:11:50 +02:00
|
|
|
});
|
2013-08-07 20:23:12 +02:00
|
|
|
|
|
|
|
if (!sub.color) {
|
|
|
|
sub.color = get_color();
|
|
|
|
}
|
2013-03-02 03:10:32 +01:00
|
|
|
|
|
|
|
add_sub(stream_name, sub);
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('sub_obj_created.zulip', {sub: sub}));
|
2013-01-22 23:16:04 +01:00
|
|
|
return sub;
|
|
|
|
}
|
2013-01-04 20:42:39 +01:00
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function button_for_sub(sub) {
|
|
|
|
var id = parseInt(sub.id, 10);
|
|
|
|
return $("#subscription_" + id + " .sub_unsub_button");
|
|
|
|
}
|
2013-01-04 20:42:39 +01:00
|
|
|
|
2013-01-23 19:43:11 +01:00
|
|
|
function settings_for_sub(sub) {
|
|
|
|
var id = parseInt(sub.id, 10);
|
|
|
|
return $("#subscription_settings_" + id);
|
|
|
|
}
|
|
|
|
|
2013-06-18 22:15:41 +02:00
|
|
|
exports.show_settings_for = function (stream_name) {
|
|
|
|
settings_for_sub(get_sub(stream_name)).collapse('show');
|
|
|
|
};
|
|
|
|
|
2013-01-23 19:43:11 +01:00
|
|
|
function add_sub_to_table(sub) {
|
2013-02-16 10:45:32 +01:00
|
|
|
$('#create_stream_row').after(
|
2013-07-26 20:43:14 +02:00
|
|
|
templates.render('subscription', sub));
|
2013-01-23 19:43:11 +01:00
|
|
|
settings_for_sub(sub).collapse('show');
|
|
|
|
}
|
|
|
|
|
2013-01-23 21:34:48 +01:00
|
|
|
function format_member_list_elem(name, email) {
|
|
|
|
return name + ' <' + email + '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_to_member_list(ul, name, email) {
|
|
|
|
var member;
|
|
|
|
if (email === undefined) {
|
|
|
|
member = name;
|
|
|
|
} else {
|
|
|
|
member = format_member_list_elem(name, email);
|
|
|
|
}
|
|
|
|
$('<li>').prependTo(ul).text(member);
|
|
|
|
}
|
|
|
|
|
2013-02-05 20:30:00 +01:00
|
|
|
function mark_subscribed(stream_name, attrs) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
2013-01-04 20:42:39 +01:00
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
if (sub === undefined) {
|
2013-03-10 19:36:45 +01:00
|
|
|
// Create a new stream.
|
2013-02-05 20:30:00 +01:00
|
|
|
sub = create_sub(stream_name, attrs);
|
2013-01-23 19:43:11 +01:00
|
|
|
add_sub_to_table(sub);
|
2013-01-22 23:16:04 +01:00
|
|
|
} else if (! sub.subscribed) {
|
2013-03-10 19:36:45 +01:00
|
|
|
// Add yourself to an existing stream.
|
2013-08-07 20:23:12 +02:00
|
|
|
var color = get_color();
|
|
|
|
exports.set_color(stream_name, color);
|
2013-01-22 23:16:04 +01:00
|
|
|
sub.subscribed = true;
|
2013-04-24 22:21:14 +02:00
|
|
|
var settings = settings_for_sub(sub);
|
2013-01-22 23:16:04 +01:00
|
|
|
var button = button_for_sub(sub);
|
|
|
|
if (button.length !== 0) {
|
|
|
|
button.text("Unsubscribe").removeClass("btn-primary");
|
2013-04-24 22:21:14 +02:00
|
|
|
// Add the user to the member list if they're currently
|
|
|
|
// viewing the members of this stream
|
|
|
|
if (sub.render_subscribers && settings.hasClass('in')) {
|
|
|
|
var members = settings.find(".subscriber_list_container ul");
|
|
|
|
add_to_member_list(members, page_params.fullname, page_params.email);
|
|
|
|
}
|
2013-01-22 23:16:04 +01:00
|
|
|
} else {
|
2013-01-23 19:43:11 +01:00
|
|
|
add_sub_to_table(sub);
|
2013-01-22 23:16:04 +01:00
|
|
|
}
|
2013-01-23 22:08:34 +01:00
|
|
|
|
|
|
|
// Display the swatch and subscription settings
|
|
|
|
var sub_row = settings.closest('.subscription_row');
|
2013-01-26 00:04:18 +01:00
|
|
|
sub_row.find(".color_swatch").addClass('in');
|
|
|
|
sub_row.find(".regular_subscription_settings").collapse('show');
|
2013-01-22 23:16:04 +01:00
|
|
|
} else {
|
|
|
|
// Already subscribed
|
|
|
|
return;
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
2013-03-21 22:13:17 +01:00
|
|
|
|
2013-04-10 23:38:30 +02:00
|
|
|
if (current_msg_list.narrowed) {
|
|
|
|
current_msg_list.update_trailing_bookend();
|
|
|
|
}
|
|
|
|
|
2013-03-21 22:13:17 +01:00
|
|
|
// Update unread counts as the new stream in sidebar might
|
|
|
|
// need its unread counts re-calculated
|
|
|
|
process_loaded_for_unread(all_msg_list.all());
|
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('subscription_add_done.zulip', {sub: sub}));
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function mark_unsubscribed(stream_name) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
2013-01-22 23:16:04 +01:00
|
|
|
|
|
|
|
if (sub === undefined) {
|
|
|
|
// We don't know about this stream
|
|
|
|
return;
|
|
|
|
} else if (sub.subscribed) {
|
2013-05-06 02:54:15 +02:00
|
|
|
stream_list.remove_narrow_filter(stream_name, 'stream');
|
2013-01-22 23:16:04 +01:00
|
|
|
sub.subscribed = false;
|
|
|
|
button_for_sub(sub).text("Subscribe").addClass("btn-primary");
|
2013-01-23 19:51:47 +01:00
|
|
|
var settings = settings_for_sub(sub);
|
|
|
|
if (settings.hasClass('in')) {
|
|
|
|
settings.collapse('hide');
|
|
|
|
}
|
2013-01-23 22:08:34 +01:00
|
|
|
|
|
|
|
// Hide the swatch and subscription settings
|
|
|
|
var sub_row = settings.closest('.subscription_row');
|
2013-01-26 00:04:18 +01:00
|
|
|
sub_row.find(".color_swatch").removeClass('in');
|
2013-01-23 22:08:34 +01:00
|
|
|
if (sub.render_subscribers) {
|
|
|
|
// TODO: having a completely empty settings div messes
|
|
|
|
// with Bootstrap's collapser. We currently just ensure
|
|
|
|
// that it's not empty on the MIT realm, even though it
|
|
|
|
// looks weird
|
2013-01-26 00:04:18 +01:00
|
|
|
sub_row.find(".regular_subscription_settings").collapse('hide');
|
2013-01-23 22:08:34 +01:00
|
|
|
}
|
2013-01-22 23:16:04 +01:00
|
|
|
} else {
|
|
|
|
// Already unsubscribed
|
|
|
|
return;
|
|
|
|
}
|
2013-04-10 23:38:30 +02:00
|
|
|
|
|
|
|
if (current_msg_list.narrowed) {
|
|
|
|
current_msg_list.update_trailing_bookend();
|
|
|
|
}
|
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('subscription_remove_done.zulip', {sub: sub}));
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
|
|
|
|
2013-03-29 20:27:41 +01:00
|
|
|
$(function () {
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('subscription_add.zulip', function (e) {
|
2013-03-29 20:27:41 +01:00
|
|
|
mark_subscribed(e.subscription.name, e.subscription);
|
|
|
|
});
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('subscription_remove.zulip', function (e) {
|
2013-03-29 20:27:41 +01:00
|
|
|
mark_unsubscribed(e.subscription.name);
|
|
|
|
});
|
2013-04-02 20:47:18 +02:00
|
|
|
|
|
|
|
$(document).on('click', '.subs_set_all_users', function (e) {
|
|
|
|
$('#people_to_add :checkbox').attr('checked', true);
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
$(document).on('click', '.subs_unset_all_users', function (e) {
|
|
|
|
$('#people_to_add :checkbox').attr('checked', false);
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2013-03-29 20:27:41 +01:00
|
|
|
});
|
|
|
|
|
2012-12-01 04:37:52 +01:00
|
|
|
exports.get_color = function (stream_name) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
if (sub === undefined) {
|
2013-08-07 19:28:06 +02:00
|
|
|
return stream_color.default_color;
|
2013-01-04 18:02:01 +01:00
|
|
|
}
|
2013-03-02 03:10:32 +01:00
|
|
|
return sub.color;
|
2012-12-01 04:37:52 +01:00
|
|
|
};
|
|
|
|
|
2013-02-05 20:51:11 +01:00
|
|
|
exports.get_invite_only = function (stream_name) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
if (sub === undefined) {
|
2013-02-05 20:51:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-02 03:10:32 +01:00
|
|
|
return sub.invite_only;
|
2013-02-05 20:51:11 +01:00
|
|
|
};
|
|
|
|
|
2013-04-09 02:14:13 +02:00
|
|
|
exports.receives_notifications = function (stream_name) {
|
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
if (sub === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return sub.notifications;
|
|
|
|
};
|
|
|
|
|
2013-06-12 21:16:05 +02:00
|
|
|
function populate_subscriptions(subs, subscribed) {
|
2013-04-04 22:30:28 +02:00
|
|
|
var sub_rows = [];
|
|
|
|
subs.sort(function (a, b) {
|
2013-05-03 19:16:50 +02:00
|
|
|
return util.strcmp(a.name, b.name);
|
2013-04-04 22:30:28 +02:00
|
|
|
});
|
|
|
|
subs.forEach(function (elem) {
|
|
|
|
var stream_name = elem.name;
|
|
|
|
var sub = create_sub(stream_name, {color: elem.color, in_home_view: elem.in_home_view,
|
2013-04-09 02:14:13 +02:00
|
|
|
invite_only: elem.invite_only,
|
2013-06-12 21:16:05 +02:00
|
|
|
notifications: elem.notifications, subscribed: subscribed});
|
2013-04-04 22:30:28 +02:00
|
|
|
sub_rows.push(sub);
|
|
|
|
});
|
|
|
|
|
2013-05-06 02:54:15 +02:00
|
|
|
stream_list.sort_narrow_list();
|
2013-04-04 22:30:28 +02:00
|
|
|
return sub_rows;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.reload_subscriptions = function (opts) {
|
|
|
|
var on_success;
|
2013-07-30 05:11:50 +02:00
|
|
|
opts = _.defaults({}, opts, {clear_first: false, custom_callbacks: false});
|
2013-04-04 22:30:28 +02:00
|
|
|
|
|
|
|
if (! opts.custom_callbacks) {
|
|
|
|
on_success = function (data) {
|
|
|
|
if (data) {
|
2013-06-12 21:16:05 +02:00
|
|
|
populate_subscriptions(data.subscriptions, true);
|
2013-04-04 22:30:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.clear_first) {
|
2013-04-14 22:58:33 +02:00
|
|
|
stream_info = {};
|
2013-05-06 02:54:15 +02:00
|
|
|
stream_list.remove_all_narrow_filters();
|
2013-04-04 22:30:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/subscriptions/list',
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*1000,
|
|
|
|
success: on_success
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-01-10 20:30:15 +01:00
|
|
|
exports.setup_page = function () {
|
2013-01-16 21:26:55 +01:00
|
|
|
util.make_loading_indicator($('#subs_page_loading_indicator'));
|
2013-01-23 17:41:04 +01:00
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
function populate_and_fill(stream_data, subscription_data) {
|
|
|
|
var all_streams = [];
|
|
|
|
var our_subs = [];
|
2013-06-12 21:16:05 +02:00
|
|
|
var sub_rows = [];
|
2013-01-23 17:41:04 +01:00
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
/* arguments are [ "success", statusText, jqXHR ] */
|
|
|
|
if (stream_data.length > 2 && stream_data[2]) {
|
|
|
|
var stream_response = JSON.parse(stream_data[2].responseText);
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(stream_response.streams, function (stream) {
|
2013-06-24 21:15:33 +02:00
|
|
|
all_streams.push(stream.name);
|
|
|
|
});
|
2013-04-04 22:30:28 +02:00
|
|
|
}
|
|
|
|
if (subscription_data.length > 2 && subscription_data[2]) {
|
|
|
|
var subs_response = JSON.parse(subscription_data[2].responseText);
|
|
|
|
our_subs = subs_response.subscriptions;
|
2013-01-23 17:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-06-17 20:42:35 +02:00
|
|
|
// All streams won't contain invite-only streams,
|
|
|
|
// or anything at all if should_list_all_streams() is false
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(our_subs, function (stream) {
|
|
|
|
if (_.indexOf(all_streams, stream.name) === -1) {
|
2013-06-17 20:42:35 +02:00
|
|
|
all_streams.push(stream.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-12 21:16:05 +02:00
|
|
|
populate_subscriptions(our_subs, true);
|
2013-01-23 17:41:04 +01:00
|
|
|
|
|
|
|
all_streams.forEach(function (stream) {
|
2013-06-12 21:16:05 +02:00
|
|
|
var sub = exports.get(stream);
|
|
|
|
if (!sub) {
|
|
|
|
sub = create_sub(stream, {subscribed: false});
|
2013-01-23 17:41:04 +01:00
|
|
|
}
|
|
|
|
sub_rows.push(sub);
|
|
|
|
});
|
|
|
|
|
2013-06-12 21:16:05 +02:00
|
|
|
sub_rows.sort(function (streama, streamb) {
|
|
|
|
if (streama.subscribed && !streamb.subscribed) {
|
|
|
|
return -1;
|
|
|
|
} else if (streamb.subscribed && !streama.subscribed) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return util.strcmp(streama.name, streamb.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-26 20:43:14 +02:00
|
|
|
$('#subscriptions_table').empty();
|
|
|
|
var rendered = templates.render('subscription_table_body', {subscriptions: sub_rows});
|
|
|
|
$('#subscriptions_table').append(rendered);
|
2013-01-31 21:13:28 +01:00
|
|
|
|
2013-01-23 17:41:04 +01:00
|
|
|
util.destroy_loading_indicator($('#subs_page_loading_indicator'));
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('subs_page_loaded.zulip'));
|
2013-01-23 17:41:04 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
function failed_listing(xhr, error) {
|
|
|
|
util.destroy_loading_indicator($('#subs_page_loading_indicator'));
|
|
|
|
ui.report_error("Error listing streams or subscriptions", xhr, $("#subscriptions-status"));
|
|
|
|
}
|
|
|
|
|
|
|
|
var requests = [];
|
2013-01-23 17:51:01 +01:00
|
|
|
if (should_list_all_streams()) {
|
|
|
|
// This query must go first to prevent a race when we are not
|
|
|
|
// listing all streams
|
2013-04-04 22:30:28 +02:00
|
|
|
var req = $.ajax({
|
2013-01-23 17:51:01 +01:00
|
|
|
type: 'POST',
|
|
|
|
url: '/json/get_public_streams',
|
|
|
|
dataType: 'json',
|
2013-04-04 22:30:28 +02:00
|
|
|
timeout: 10*1000
|
2013-01-23 17:51:01 +01:00
|
|
|
});
|
2013-04-04 22:30:28 +02:00
|
|
|
requests.push(req);
|
2013-01-23 17:51:01 +01:00
|
|
|
} else {
|
2013-04-04 22:30:28 +02:00
|
|
|
// Handing an object to $.when() means that it counts as a 'success' with the
|
|
|
|
// object delivered directly to the callback
|
|
|
|
requests.push({streams: []});
|
2013-01-31 16:34:29 +01:00
|
|
|
$('#create_stream_button').val("Subscribe");
|
2013-01-23 17:51:01 +01:00
|
|
|
}
|
2013-01-23 17:41:04 +01:00
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
requests.push(exports.reload_subscriptions({custom_callbacks: true}));
|
|
|
|
|
|
|
|
// Trigger finished callback when:
|
|
|
|
// * Both AJAX requests are finished, if we sent themm both
|
|
|
|
// * Just one AJAX is finished if should_list_all_streams() is false
|
|
|
|
$.when.apply(this, requests).then(populate_and_fill, failed_listing);
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-03 22:24:17 +02:00
|
|
|
|
2013-06-12 21:13:19 +02:00
|
|
|
exports.get = function (stream_name) {
|
|
|
|
return get_sub(stream_name);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.in_home_view = function (stream_name) {
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
2013-06-12 21:13:19 +02:00
|
|
|
return sub !== undefined && sub.in_home_view;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.is_subscribed = function (stream_name) {
|
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
return sub !== undefined && sub.subscribed;
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-09 21:01:41 +02:00
|
|
|
|
2013-07-16 22:21:41 +02:00
|
|
|
exports.update_subscription_properties = function (stream_name, property, value) {
|
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
switch(property) {
|
|
|
|
case 'color':
|
2013-08-07 19:28:06 +02:00
|
|
|
stream_color.update_stream_color(sub, stream_name, value, {update_historical: true});
|
2013-07-16 22:21:41 +02:00
|
|
|
break;
|
|
|
|
case 'in_home_view':
|
2013-07-16 22:47:30 +02:00
|
|
|
update_in_home_view(sub, value);
|
2013-07-16 22:21:41 +02:00
|
|
|
break;
|
|
|
|
case 'notifications':
|
2013-07-16 22:47:30 +02:00
|
|
|
update_stream_notifications(sub, value);
|
2013-07-16 22:21:41 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
blueslip.warn("Unexpected subscription property type", {property: property,
|
|
|
|
value: value});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function ajaxSubscribe(stream) {
|
2013-01-31 21:09:34 +01:00
|
|
|
// Subscribe yourself to a single stream.
|
|
|
|
var true_stream_name;
|
|
|
|
|
2013-04-12 21:36:15 +02:00
|
|
|
return $.ajax({
|
2012-11-07 23:25:04 +01:00
|
|
|
type: "POST",
|
|
|
|
url: "/json/subscriptions/add",
|
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
2013-06-24 21:32:56 +02:00
|
|
|
data: {"subscriptions": JSON.stringify([{"name": stream}]) },
|
2012-11-07 23:25:04 +01:00
|
|
|
success: function (resp, statusText, xhr, form) {
|
2013-01-31 21:09:34 +01:00
|
|
|
$("#create_stream_name").val("");
|
|
|
|
|
|
|
|
var res = $.parseJSON(xhr.responseText);
|
|
|
|
if (!$.isEmptyObject(res.already_subscribed)) {
|
|
|
|
// Display the canonical stream capitalization.
|
2013-03-25 23:26:14 +01:00
|
|
|
true_stream_name = res.already_subscribed[page_params.email][0];
|
2013-01-31 21:09:34 +01:00
|
|
|
ui.report_success("Already subscribed to " + true_stream_name,
|
|
|
|
$("#subscriptions-status"));
|
2012-11-07 23:25:04 +01:00
|
|
|
}
|
2013-03-29 20:49:05 +01:00
|
|
|
// The rest of the work is done via the subscribe event we will get
|
2012-11-07 23:25:04 +01:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2012-11-16 16:45:39 +01:00
|
|
|
ui.report_error("Error adding subscription", xhr, $("#subscriptions-status"));
|
2013-01-23 19:10:51 +01:00
|
|
|
$("#create_stream_name").focus();
|
2012-11-07 23:25:04 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-10-09 21:01:41 +02:00
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function ajaxUnsubscribe(stream) {
|
2012-11-16 20:11:08 +01:00
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "/json/subscriptions/remove",
|
2012-10-03 22:24:17 +02:00
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
2012-11-16 20:11:08 +01:00
|
|
|
data: {"subscriptions": JSON.stringify([stream]) },
|
2012-10-03 22:24:17 +02:00
|
|
|
success: function (resp, statusText, xhr, form) {
|
2012-11-16 20:11:08 +01:00
|
|
|
var name, res = $.parseJSON(xhr.responseText);
|
2013-02-06 00:58:03 +01:00
|
|
|
$("#subscriptions-status").hide();
|
2013-03-29 20:49:05 +01:00
|
|
|
// The rest of the work is done via the unsubscribe event we will get
|
2012-10-03 22:24:17 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2012-11-16 16:45:39 +01:00
|
|
|
ui.report_error("Error removing subscription", xhr, $("#subscriptions-status"));
|
2013-01-23 19:10:51 +01:00
|
|
|
$("#create_stream_name").focus();
|
2012-10-03 22:24:17 +02:00
|
|
|
}
|
|
|
|
});
|
2013-01-10 22:15:55 +01:00
|
|
|
}
|
2012-11-16 20:11:08 +01:00
|
|
|
|
2013-01-30 23:12:23 +01:00
|
|
|
function ajaxSubscribeForCreation(stream, principals, invite_only) {
|
2013-01-31 21:09:34 +01:00
|
|
|
// Subscribe yourself and possible other people to a new stream.
|
2013-04-12 21:36:15 +02:00
|
|
|
return $.ajax({
|
2013-01-31 21:09:34 +01:00
|
|
|
type: "POST",
|
|
|
|
url: "/json/subscriptions/add",
|
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
2013-06-24 21:32:56 +02:00
|
|
|
data: {"subscriptions": JSON.stringify([{"name": stream}]),
|
2013-01-30 23:12:23 +01:00
|
|
|
"principals": JSON.stringify(principals),
|
|
|
|
"invite_only": JSON.stringify(invite_only)
|
2013-01-31 21:09:34 +01:00
|
|
|
},
|
|
|
|
success: function (data) {
|
|
|
|
$("#create_stream_name").val("");
|
2013-02-06 00:58:03 +01:00
|
|
|
$("#subscriptions-status").hide();
|
2013-01-31 21:09:34 +01:00
|
|
|
$('#stream-creation').modal("hide");
|
2013-03-29 20:49:05 +01:00
|
|
|
// The rest of the work is done via the subscribe event we will get
|
2013-01-31 21:09:34 +01:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
ui.report_error("Error creating stream", xhr, $("#subscriptions-status"));
|
|
|
|
$('#stream-creation').modal("hide");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-02-02 00:07:50 +01:00
|
|
|
function people_cmp(person1, person2) {
|
2013-01-31 21:09:34 +01:00
|
|
|
// Compares objects of the form used in people_list.
|
2013-05-03 19:16:50 +02:00
|
|
|
var name_cmp = util.strcmp(person1.full_name, person2.full_name);
|
2013-02-02 00:07:50 +01:00
|
|
|
if (name_cmp < 0) {
|
2013-01-31 21:09:34 +01:00
|
|
|
return -1;
|
2013-02-02 00:07:50 +01:00
|
|
|
} else if (name_cmp > 0) {
|
2013-01-31 21:09:34 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2013-05-03 19:16:50 +02:00
|
|
|
return util.strcmp(person1.email, person2.email);
|
2013-01-31 21:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function show_new_stream_modal() {
|
2013-08-06 22:45:45 +02:00
|
|
|
var people_minus_you_and_internal_users = [];
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(page_params.people_list, function (person) {
|
2013-03-25 23:26:14 +01:00
|
|
|
if (person.email !== page_params.email &&
|
2013-07-24 20:56:42 +02:00
|
|
|
(page_params.domain === "zulip.com" ||
|
|
|
|
person.email.split('@')[1] !== "zulip.com"
|
2013-03-07 21:26:28 +01:00
|
|
|
)
|
|
|
|
) {
|
2013-08-06 22:45:45 +02:00
|
|
|
people_minus_you_and_internal_users.push({"email": person.email,
|
2013-03-07 21:26:28 +01:00
|
|
|
"full_name": person.full_name});
|
2013-01-31 21:09:34 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-02-16 10:45:32 +01:00
|
|
|
$('#people_to_add').html(templates.render('new_stream_users', {
|
2013-08-06 22:45:45 +02:00
|
|
|
users: people_minus_you_and_internal_users.sort(people_cmp)
|
2013-01-31 21:09:34 +01:00
|
|
|
}));
|
|
|
|
$('#stream-creation').modal("show");
|
|
|
|
}
|
|
|
|
|
2012-11-16 20:11:08 +01:00
|
|
|
$(function () {
|
|
|
|
var i;
|
2013-01-04 18:02:01 +01:00
|
|
|
// Populate stream_info with data handed over to client-side template.
|
2013-06-12 21:16:05 +02:00
|
|
|
populate_subscriptions(page_params.stream_list, true);
|
|
|
|
populate_subscriptions(page_params.unsubbed_info, false);
|
2012-10-03 22:24:17 +02:00
|
|
|
|
2013-07-26 20:43:14 +02:00
|
|
|
$("#subscriptions_table").on("submit", "#add_new_subscription", function (e) {
|
2012-10-31 18:36:08 +01:00
|
|
|
e.preventDefault();
|
2013-01-31 21:13:28 +01:00
|
|
|
|
|
|
|
if (!should_list_all_streams()) {
|
|
|
|
ajaxSubscribe($("#create_stream_name").val());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-31 21:09:34 +01:00
|
|
|
var stream = $.trim($("#create_stream_name").val());
|
2013-03-11 23:13:37 +01:00
|
|
|
var stream_status = compose.check_stream_existence(stream);
|
2013-01-31 21:09:34 +01:00
|
|
|
if (stream_status === "does-not-exist") {
|
|
|
|
$("#stream_name").text(stream);
|
|
|
|
show_new_stream_modal();
|
|
|
|
} else {
|
|
|
|
ajaxSubscribe(stream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#stream_creation_form").on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var stream = $.trim($("#create_stream_name").val());
|
2013-07-30 00:35:44 +02:00
|
|
|
var principals = _.map(
|
|
|
|
$("#stream_creation_form input:checkbox[name=user]:checked"),
|
|
|
|
function (elem) {
|
|
|
|
return $(elem).val();
|
|
|
|
}
|
|
|
|
);
|
2013-01-31 21:09:34 +01:00
|
|
|
// You are always subscribed to streams you create.
|
2013-03-25 23:26:14 +01:00
|
|
|
principals.push(page_params.email);
|
2013-03-11 20:30:37 +01:00
|
|
|
ajaxSubscribeForCreation(stream,
|
|
|
|
principals,
|
|
|
|
$('#stream_creation_form input[name=privacy]:checked').val() === "invite-only"
|
|
|
|
);
|
2012-10-03 22:24:17 +02:00
|
|
|
});
|
2013-01-07 22:09:33 +01:00
|
|
|
|
2013-01-10 22:15:55 +01:00
|
|
|
$("#subscriptions_table").on("click", ".sub_unsub_button", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
var sub_row = $(e.target).closest('.subscription_row');
|
|
|
|
var stream_name = sub_row.find('.subscription_name').text();
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
2013-01-10 22:15:55 +01:00
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
if (sub.subscribed) {
|
|
|
|
ajaxUnsubscribe(stream_name);
|
2013-01-10 22:15:55 +01:00
|
|
|
} else {
|
2013-01-22 23:16:04 +01:00
|
|
|
ajaxSubscribe(stream_name);
|
2013-01-10 22:15:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-01-16 20:16:28 +01:00
|
|
|
$("#subscriptions_table").on("show", ".subscription_settings", function (e) {
|
2013-03-08 23:44:02 +01:00
|
|
|
var subrow = $(e.target).closest('.subscription_row');
|
|
|
|
var colorpicker = subrow.find('.colorpicker');
|
2013-07-10 18:33:31 +02:00
|
|
|
|
|
|
|
var color = exports.get_color(subrow.find('.subscription_name').text());
|
2013-08-07 19:28:06 +02:00
|
|
|
stream_color.set_colorpicker_color(colorpicker, color);
|
2013-03-08 23:44:02 +01:00
|
|
|
|
|
|
|
// To figure out the worst case for an expanded row's height, we do some math:
|
|
|
|
// .subscriber_list_container max-height,
|
|
|
|
// .subscriber_list_settings,
|
|
|
|
// .regular_subscription_settings
|
|
|
|
// .subscription_header line-height,
|
|
|
|
// .subscription_header padding
|
|
|
|
var expanded_row_size = 200 + 30 + 100 + 30 + 5;
|
|
|
|
var cover = subrow.position().top + expanded_row_size -
|
|
|
|
viewport.height() + $("#top_navbar").height() - viewport.scrollTop();
|
|
|
|
if (cover > 0) {
|
|
|
|
$('html, body').animate({
|
|
|
|
scrollTop: viewport.scrollTop() + cover + 5
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-16 20:16:28 +01:00
|
|
|
});
|
|
|
|
|
2013-04-08 20:53:12 +02:00
|
|
|
$("#subscriptions_table").on("click", ".sub_setting_checkbox", function (e) {
|
|
|
|
var control = $(e.target).closest('.sub_setting_checkbox').find('.sub_setting_control');
|
|
|
|
// A hack. Don't change the state of the checkbox if we
|
|
|
|
// clicked on the checkbox itself.
|
|
|
|
if (control[0] !== e.target) {
|
|
|
|
control.prop("checked", ! control.prop("checked"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
$("#subscriptions_table").on("click", "#sub_setting_in_home_view", stream_home_view_clicked);
|
2013-04-09 02:14:13 +02:00
|
|
|
$("#subscriptions_table").on("click", "#sub_setting_notifications", stream_notifications_clicked);
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-06-18 22:14:48 +02:00
|
|
|
// From here down is only stuff that happens when we're rendering
|
|
|
|
// the subscriber settings
|
2013-01-24 18:20:00 +01:00
|
|
|
if (! should_render_subscribers()) {
|
2013-01-16 20:16:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-10 19:33:56 +01:00
|
|
|
$("#subscriptions_table").on("submit", ".subscriber_list_add form", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var sub_row = $(e.target).closest('.subscription_row');
|
|
|
|
var stream = sub_row.find('.subscription_name').text();
|
|
|
|
var text_box = sub_row.find('input[name="principal"]');
|
2013-01-10 20:10:06 +01:00
|
|
|
var principal = $.trim(text_box.val());
|
2013-01-10 19:33:56 +01:00
|
|
|
// TODO: clean up this error handling
|
|
|
|
var error_elem = sub_row.find('.subscriber_list_container .alert-error');
|
|
|
|
var warning_elem = sub_row.find('.subscriber_list_container .alert-warning');
|
|
|
|
var list = sub_row.find('.subscriber_list_container ul');
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "/json/subscriptions/add",
|
|
|
|
dataType: 'json',
|
2013-06-24 21:32:56 +02:00
|
|
|
data: {"subscriptions": JSON.stringify([{"name": stream}]),
|
2013-01-31 21:09:34 +01:00
|
|
|
"principals": JSON.stringify([principal])},
|
2013-01-10 19:33:56 +01:00
|
|
|
success: function (data) {
|
|
|
|
text_box.val('');
|
2013-01-31 21:09:34 +01:00
|
|
|
|
|
|
|
if (data.subscribed.hasOwnProperty(principal)) {
|
2013-01-10 19:33:56 +01:00
|
|
|
error_elem.addClass("hide");
|
|
|
|
warning_elem.addClass("hide");
|
2013-03-25 23:26:14 +01:00
|
|
|
if (principal === page_params.email) {
|
2013-01-29 20:36:01 +01:00
|
|
|
// mark_subscribed adds the user to the member list
|
|
|
|
mark_subscribed(stream);
|
|
|
|
} else {
|
|
|
|
add_to_member_list(list, people_dict[principal].full_name, principal);
|
|
|
|
}
|
2013-01-10 19:33:56 +01:00
|
|
|
} else {
|
|
|
|
error_elem.addClass("hide");
|
|
|
|
warning_elem.removeClass("hide").text("User already subscribed");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
warning_elem.addClass("hide");
|
|
|
|
error_elem.removeClass("hide").text("Could not add user to this stream");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2013-01-07 22:09:33 +01:00
|
|
|
|
2013-01-26 00:04:18 +01:00
|
|
|
$("#subscriptions_table").on("show", ".regular_subscription_settings", function (e) {
|
|
|
|
// We want 'show' events that originate from
|
|
|
|
// 'regular_subscription_settings' divs not to trigger the
|
|
|
|
// handler for the entire subscription_settings div
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2013-01-07 22:09:33 +01:00
|
|
|
$("#subscriptions_table").on("show", ".subscription_settings", function (e) {
|
|
|
|
var sub_row = $(e.target).closest('.subscription_row');
|
|
|
|
var stream = sub_row.find('.subscription_name').text();
|
2013-01-29 22:37:24 +01:00
|
|
|
var warning_elem = sub_row.find('.subscriber_list_container .alert-warning');
|
2013-01-10 19:33:56 +01:00
|
|
|
var error_elem = sub_row.find('.subscriber_list_container .alert-error');
|
2013-01-07 22:09:33 +01:00
|
|
|
var list = sub_row.find('.subscriber_list_container ul');
|
2013-01-16 22:02:49 +01:00
|
|
|
var indicator_elem = sub_row.find('.subscriber_list_loading_indicator');
|
2013-01-10 20:22:25 +01:00
|
|
|
|
2013-01-29 22:37:24 +01:00
|
|
|
warning_elem.addClass('hide');
|
2013-01-07 22:09:33 +01:00
|
|
|
error_elem.addClass('hide');
|
|
|
|
list.empty();
|
|
|
|
|
2013-01-16 22:02:49 +01:00
|
|
|
util.make_loading_indicator(indicator_elem);
|
|
|
|
|
2013-01-07 22:09:33 +01:00
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "/json/get_subscribers",
|
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
|
|
|
data: {stream: stream},
|
|
|
|
success: function (data) {
|
2013-01-16 22:02:49 +01:00
|
|
|
util.destroy_loading_indicator(indicator_elem);
|
2013-07-29 23:35:36 +02:00
|
|
|
var subscribers = _.map(data.subscribers, function (elem) {
|
2013-01-15 23:28:58 +01:00
|
|
|
var person = people_dict[elem];
|
|
|
|
if (person === undefined) {
|
|
|
|
return elem;
|
|
|
|
}
|
2013-01-23 21:34:48 +01:00
|
|
|
return format_member_list_elem(people_dict[elem].full_name, elem);
|
2013-01-14 22:19:13 +01:00
|
|
|
});
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(subscribers.sort().reverse(), function (elem) {
|
2013-02-07 17:59:27 +01:00
|
|
|
// add_to_member_list *prepends* the element,
|
|
|
|
// so we need to sort in reverse order for it to
|
|
|
|
// appear in alphabetical order.
|
2013-01-23 21:34:48 +01:00
|
|
|
add_to_member_list(list, elem);
|
2013-01-07 22:09:33 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2013-01-16 22:02:49 +01:00
|
|
|
util.destroy_loading_indicator(indicator_elem);
|
2013-01-07 22:09:33 +01:00
|
|
|
error_elem.removeClass("hide").text("Could not fetch subscriber list");
|
|
|
|
}
|
|
|
|
});
|
2013-01-10 19:57:34 +01:00
|
|
|
|
|
|
|
sub_row.find('input[name="principal"]').typeahead({
|
|
|
|
source: typeahead_helper.private_message_typeahead_list,
|
|
|
|
items: 4,
|
|
|
|
highlighter: function (item) {
|
|
|
|
var query = this.query;
|
|
|
|
return typeahead_helper.highlight_with_escaping(query, item);
|
|
|
|
},
|
2013-01-10 20:10:06 +01:00
|
|
|
matcher: function (item) {
|
|
|
|
var query = $.trim(this.query);
|
|
|
|
if (query === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Case-insensitive.
|
|
|
|
return (item.toLowerCase().indexOf(query.toLowerCase()) !== -1);
|
|
|
|
},
|
2013-01-10 19:57:34 +01:00
|
|
|
updater: function (item) {
|
|
|
|
return typeahead_helper.private_message_mapped[item].email;
|
|
|
|
}
|
|
|
|
});
|
2013-01-07 22:09:33 +01:00
|
|
|
});
|
2013-06-25 17:49:48 +02:00
|
|
|
|
|
|
|
// Change the down arrow to an up arrow on expansion, and back to a down
|
|
|
|
// arrow on collapse.
|
|
|
|
// FIXME: If there's a way, it may be better to do this in pure CSS.
|
|
|
|
$("#subscriptions_table").on("show", ".subscription_settings", function (e) {
|
|
|
|
var sub_arrow = $(e.target).closest('.subscription_row').find('.sub_arrow i');
|
|
|
|
sub_arrow.removeClass('icon-vector-chevron-down');
|
|
|
|
sub_arrow.addClass('icon-vector-chevron-up');
|
|
|
|
});
|
|
|
|
$("#subscriptions_table").on("hide", ".subscription_settings", function (e) {
|
|
|
|
var sub_arrow = $(e.target).closest('.subscription_row').find('.sub_arrow i');
|
|
|
|
sub_arrow.removeClass('icon-vector-chevron-up');
|
|
|
|
sub_arrow.addClass('icon-vector-chevron-down');
|
|
|
|
});
|
2012-10-03 22:24:17 +02:00
|
|
|
});
|
2012-10-18 21:37:07 +02:00
|
|
|
|
2013-03-29 19:55:28 +01:00
|
|
|
function focus_on_narrowed_stream() {
|
2013-06-13 19:44:36 +02:00
|
|
|
var stream_name = narrow.stream();
|
|
|
|
if (stream_name === undefined) {
|
2013-03-29 19:55:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var sub = get_sub(stream_name);
|
|
|
|
if (sub !== undefined) {
|
|
|
|
// This stream is in the list, so focus on it.
|
|
|
|
$('html, body').animate({
|
|
|
|
scrollTop: settings_for_sub(sub).offset().top
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// This stream doesn't exist, so prep for creating it.
|
|
|
|
$("#create_stream_name").val(stream_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.show_and_focus_on_narrow = function () {
|
|
|
|
$("#gear-menu a[href='#subscriptions']").one('shown',
|
|
|
|
focus_on_narrowed_stream);
|
|
|
|
ui.change_tab_to("#subscriptions");
|
|
|
|
};
|
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|