2012-10-18 21:37:07 +02:00
|
|
|
var subs = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2013-01-04 20:42:39 +01:00
|
|
|
var next_sub_id = 0;
|
2013-01-04 18:02:01 +01:00
|
|
|
|
2013-08-07 20:23:12 +02:00
|
|
|
function get_color() {
|
2013-08-15 21:11:07 +02:00
|
|
|
var used_colors = stream_data.get_colors();
|
2013-08-07 20:23:12 +02:00
|
|
|
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
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
if (stream_data.all_subscribed_streams_are_in_home_view()) {
|
2013-02-20 18:07:53 +01:00
|
|
|
$(all_messages).addClass('hidden-filter');
|
|
|
|
} else {
|
|
|
|
$(all_messages).removeClass('hidden-filter');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.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-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) {
|
2013-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2013-07-16 22:47:30 +02:00
|
|
|
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-08-21 23:21:31 +02:00
|
|
|
function update_stream_name(sub, new_name) {
|
|
|
|
// Rename the stream internally.
|
|
|
|
var old_name = sub.name;
|
|
|
|
stream_data.delete_sub(old_name);
|
|
|
|
sub.name = new_name;
|
|
|
|
stream_data.add_sub(new_name, sub);
|
|
|
|
|
|
|
|
// Update the stream sidebar.
|
|
|
|
exports.reload_subscriptions({clear_first: true});
|
|
|
|
|
|
|
|
// Update the message feed.
|
|
|
|
_.each([home_msg_list, current_msg_list, all_msg_list], function (list) {
|
|
|
|
list.change_display_recipient(old_name, new_name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream);
|
2013-04-09 02:14:13 +02:00
|
|
|
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) {
|
2013-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2013-08-07 19:28:06 +02:00
|
|
|
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-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2013-03-02 03:10:32 +01:00
|
|
|
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++,
|
2013-08-15 00:50:19 +02:00
|
|
|
render_subscribers: page_params.domain !== 'mit.edu' || attrs.invite_only === true,
|
2013-07-30 05:11:50 +02:00
|
|
|
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
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
stream_data.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) {
|
2013-08-15 21:11:07 +02:00
|
|
|
settings_for_sub(stream_data.get_sub(stream_name)).collapse('show');
|
2013-06-18 22:15:41 +02:00
|
|
|
};
|
|
|
|
|
2013-08-26 20:22:22 +02:00
|
|
|
function add_email_hint(row) {
|
|
|
|
// Add a popover explaining stream e-mail addresses on hover.
|
|
|
|
var hint_id = "#email-address-hint-" + row.id;
|
|
|
|
var email_address_hint = $(hint_id);
|
|
|
|
email_address_hint.popover({"placement": "bottom",
|
|
|
|
"title": "Email integration",
|
|
|
|
"content": templates.render('email_address_hint'),
|
|
|
|
"trigger": "manual"});
|
|
|
|
$("body").on("mouseover", hint_id, function (e) {
|
|
|
|
email_address_hint.popover('show');
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
$("body").on("mouseout", hint_id, function (e) {
|
|
|
|
email_address_hint.popover('hide');
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-23 19:43:11 +01:00
|
|
|
function add_sub_to_table(sub) {
|
2013-08-12 21:13:07 +02:00
|
|
|
$('#create_stream_row').after(templates.render(
|
|
|
|
'subscription',
|
|
|
|
_.extend(sub, {'show_email_token': feature_flags.email_forwarding})));
|
2013-01-23 19:43:11 +01:00
|
|
|
settings_for_sub(sub).collapse('show');
|
2013-08-26 20:22:22 +02:00
|
|
|
add_email_hint(sub);
|
2013-01-23 19:43:11 +01:00
|
|
|
}
|
|
|
|
|
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-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.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-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.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-09 02:14:13 +02:00
|
|
|
exports.receives_notifications = function (stream_name) {
|
2013-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2013-04-09 02:14:13 +02:00
|
|
|
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-08-12 21:13:07 +02:00
|
|
|
notifications: elem.notifications, subscribed: subscribed,
|
|
|
|
email_address: elem.email_address});
|
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-08-15 21:11:07 +02:00
|
|
|
stream_data.clear_subscriptions();
|
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-08-15 21:11:07 +02:00
|
|
|
function populate_and_fill(data_for_streams, subscription_data) {
|
2013-04-04 22:30:28 +02:00
|
|
|
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 ] */
|
2013-08-15 21:11:07 +02:00
|
|
|
if (data_for_streams.length > 2 && data_for_streams[2]) {
|
|
|
|
var stream_response = JSON.parse(data_for_streams[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-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream);
|
2013-06-12 21:16:05 +02:00
|
|
|
if (!sub) {
|
|
|
|
sub = create_sub(stream, {subscribed: false});
|
2013-01-23 17:41:04 +01:00
|
|
|
}
|
2013-08-12 21:13:07 +02:00
|
|
|
if (feature_flags.email_forwarding) {
|
|
|
|
sub = _.extend(sub, {'show_email_token': feature_flags.email_forwarding});
|
|
|
|
}
|
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-08-26 20:22:22 +02:00
|
|
|
_.each(sub_rows, function (row) {
|
|
|
|
add_email_hint(row);
|
|
|
|
});
|
|
|
|
|
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-07-16 22:21:41 +02:00
|
|
|
exports.update_subscription_properties = function (stream_name, property, value) {
|
2013-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2013-07-16 22:21:41 +02:00
|
|
|
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;
|
2013-08-21 23:21:31 +02:00
|
|
|
case 'name':
|
|
|
|
update_stream_name(sub, value);
|
|
|
|
break;
|
2013-07-16 22:21:41 +02:00
|
|
|
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-08-27 19:14:18 +02:00
|
|
|
function ajaxSubscribeForCreation(stream, principals, invite_only, announce) {
|
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),
|
2013-08-27 19:14:18 +02:00
|
|
|
"invite_only": JSON.stringify(invite_only),
|
|
|
|
"announce": JSON.stringify(announce)
|
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
|
|
|
}
|
|
|
|
|
2013-08-27 19:14:18 +02:00
|
|
|
// Within the new stream modal...
|
|
|
|
function update_announce_stream_state() {
|
|
|
|
// If the stream is invite only, or everyone's added, disable
|
|
|
|
// the "Announce stream" option. Otherwise enable it.
|
|
|
|
var announce_stream_checkbox = $('#announce-new-stream input');
|
|
|
|
var disable_it = false;
|
|
|
|
var is_invite_only = $('input:radio[name=privacy]:checked').val() === 'invite-only';
|
|
|
|
|
|
|
|
if (is_invite_only) {
|
|
|
|
disable_it = true;
|
|
|
|
announce_stream_checkbox.prop('checked', false);
|
|
|
|
} else {
|
|
|
|
disable_it = $('#user-checkboxes input').length
|
|
|
|
=== $('#user-checkboxes input:checked').length;
|
|
|
|
}
|
|
|
|
|
|
|
|
announce_stream_checkbox.prop('disabled', disable_it);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}));
|
2013-08-27 19:14:18 +02:00
|
|
|
|
|
|
|
// Make the options default to the same each time:
|
|
|
|
// public, "announce stream" on.
|
|
|
|
$('#make-invite-only input:radio[value=public]').prop('checked', true);
|
|
|
|
$('#announce-new-stream input').prop('disabled', false);
|
|
|
|
$('#announce-new-stream input').prop('checked', true);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-27 19:14:18 +02:00
|
|
|
$('#stream_creation_form').on('change',
|
|
|
|
'#user-checkboxes input, #make-invite-only input',
|
|
|
|
update_announce_stream_state);
|
|
|
|
|
|
|
|
// 'Check all' and 'Uncheck all' links
|
|
|
|
$(document).on('click', '.subs_set_all_users', function (e) {
|
|
|
|
$('#people_to_add :checkbox').attr('checked', true);
|
|
|
|
e.preventDefault();
|
|
|
|
update_announce_stream_state();
|
|
|
|
});
|
|
|
|
$(document).on('click', '.subs_unset_all_users', function (e) {
|
|
|
|
$('#people_to_add :checkbox').attr('checked', false);
|
|
|
|
e.preventDefault();
|
|
|
|
update_announce_stream_state();
|
|
|
|
});
|
|
|
|
|
|
|
|
var announce_stream_docs = $("#announce-stream-docs");
|
|
|
|
announce_stream_docs.popover({"placement": "right",
|
|
|
|
"content": templates.render('announce_stream_docs'),
|
|
|
|
"trigger": "manual"});
|
|
|
|
$("body").on("mouseover", "#announce-stream-docs", function (e) {
|
|
|
|
announce_stream_docs.popover('show');
|
|
|
|
announce_stream_docs.data('popover').tip().css('z-index', 2000);
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
$("body").on("mouseout", "#announce-stream-docs", function (e) {
|
|
|
|
announce_stream_docs.popover('hide');
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2013-01-31 21:09:34 +01:00
|
|
|
$("#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,
|
2013-08-27 19:14:18 +02:00
|
|
|
$('#stream_creation_form input[name=privacy]:checked').val() === "invite-only",
|
|
|
|
$('#announce-new-stream input').prop('checked')
|
2013-03-11 20:30:37 +01:00
|
|
|
);
|
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-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.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
|
|
|
|
2013-08-15 21:11:07 +02:00
|
|
|
var color = stream_data.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-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 {
|
2013-08-07 23:56:51 +02:00
|
|
|
add_to_member_list(list, people_dict.get(principal).full_name, principal);
|
2013-01-29 20:36:01 +01:00
|
|
|
}
|
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-08-20 23:15:53 +02:00
|
|
|
if (!stream_data.get_sub(stream).render_subscribers) {
|
2013-08-15 00:50:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-08-07 23:56:51 +02:00
|
|
|
var person = people_dict.get(elem);
|
2013-01-15 23:28:58 +01:00
|
|
|
if (person === undefined) {
|
|
|
|
return elem;
|
|
|
|
}
|
2013-08-07 23:56:51 +02:00
|
|
|
return format_member_list_elem(people_dict.get(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;
|
|
|
|
}
|
2013-08-15 21:11:07 +02:00
|
|
|
var sub = stream_data.get_sub(stream_name);
|
2013-03-29 19:55:28 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
}());
|
2013-08-09 02:05:23 +02:00
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = subs;
|
|
|
|
}
|