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
|
2012-12-01 04:37:52 +01:00
|
|
|
// We fetch the stream colors asynchronous while the message feed is
|
|
|
|
// getting constructed, so we may need to go back and color streams
|
|
|
|
// that have already been rendered.
|
|
|
|
var initial_color_fetch = true;
|
2012-10-18 21:37:07 +02:00
|
|
|
|
2013-01-04 21:05:18 +01:00
|
|
|
var default_color = "#c2c2c2";
|
2013-03-10 19:36:45 +01:00
|
|
|
// Auto-assigned colors should be from the default palette so it's easy to undo
|
|
|
|
// changes, so if that pallete changes, change these colors.
|
|
|
|
var stream_assignment_colors = ["#76ce90", "#fae589", "#a6c7e5", "#e79ab5",
|
|
|
|
"#bfd56f", "#f4ae55", "#b0a5fd", "#addfe5",
|
|
|
|
"#f5ce6e", "#c2726a", "#94c849", "#bd86e5",
|
|
|
|
"#ee7e4a", "#a6dcbf", "#95a5fd", "#53a063",
|
|
|
|
"#9987e1", "#e4523d", "#c2c2c2", "#4f8de4",
|
|
|
|
"#c6a8ad", "#e7cc4d", "#c8bebf", "#a47462"];
|
|
|
|
|
2013-05-02 03:01:46 +02:00
|
|
|
// Clone stream_assignement_colors
|
|
|
|
var available_colors = stream_assignment_colors.slice(0);
|
|
|
|
|
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-02-16 09:43:27 +01:00
|
|
|
// Classes which could be returned by get_color_class.
|
2013-02-19 20:13:50 +01:00
|
|
|
exports.color_classes = 'dark_background';
|
2013-02-16 09:43:27 +01:00
|
|
|
|
2013-03-10 19:36:45 +01:00
|
|
|
function pick_color() {
|
|
|
|
if (available_colors.length === 0) {
|
|
|
|
// We've used all the palette colors, so start re-using them.
|
2013-05-02 03:01:46 +02:00
|
|
|
return stream_assignment_colors[exports.subscribed_streams().length
|
|
|
|
% stream_assignment_colors.length];
|
2013-03-10 19:36:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return available_colors[0];
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:01:46 +02:00
|
|
|
function mark_color_used(color) {
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < available_colors.length; ++i) {
|
|
|
|
if (available_colors[i] === color) {
|
|
|
|
available_colors.splice(i, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 21:08:16 +01:00
|
|
|
exports.subscribed_streams = function () {
|
|
|
|
// TODO: Object.keys() compatibility
|
2013-01-22 23:16:04 +01:00
|
|
|
var list = [];
|
|
|
|
$.each(Object.keys(stream_info), function (idx, key) {
|
|
|
|
var sub = stream_info[key];
|
|
|
|
if (sub.subscribed) {
|
|
|
|
list.push(sub.name);
|
|
|
|
}
|
|
|
|
});
|
2013-01-22 21:08:16 +01:00
|
|
|
list.sort();
|
|
|
|
return list;
|
|
|
|
};
|
|
|
|
|
2013-02-20 18:07:53 +01:00
|
|
|
exports.maybe_toggle_all_messages = function () {
|
|
|
|
var show_all_messages = false;
|
|
|
|
$.each(stream_info, function (idx, stream) {
|
|
|
|
if (!stream.in_home_view) {
|
|
|
|
show_all_messages = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var all_messages = $("#global_filters [data-name='all']")[0];
|
|
|
|
if (!show_all_messages) {
|
|
|
|
$(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
|
|
|
}
|
|
|
|
|
2012-12-01 04:37:52 +01:00
|
|
|
function update_table_stream_color(table, stream_name, color) {
|
2013-02-16 09:43:27 +01:00
|
|
|
var color_class = exports.get_color_class(color);
|
|
|
|
function fixup(elem) {
|
|
|
|
elem.css("background-color", color)
|
2013-02-19 20:13:50 +01:00
|
|
|
.removeClass(exports.color_classes)
|
2013-02-16 09:43:27 +01:00
|
|
|
.addClass(color_class);
|
|
|
|
}
|
|
|
|
|
2012-12-01 04:37:52 +01:00
|
|
|
$.each(table.find(".stream_label"), function () {
|
|
|
|
if ($(this).text() === stream_name) {
|
|
|
|
var parent_label = $(this).parent("td");
|
2013-02-16 09:43:27 +01:00
|
|
|
fixup(parent_label);
|
|
|
|
fixup(parent_label.prev("td"));
|
2012-12-01 04:37:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-03-02 03:10:32 +01:00
|
|
|
exports.stream_id = function(stream_name) {
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
function update_stream_sidebar_swatch_color(stream_name, color) {
|
|
|
|
var id = exports.stream_id(stream_name);
|
|
|
|
$("#stream_sidebar_swatch_" + id).css('background-color', color);
|
|
|
|
}
|
|
|
|
|
2012-12-01 04:37:52 +01:00
|
|
|
function update_historical_message_color(stream_name, color) {
|
|
|
|
update_table_stream_color($(".focused_table"), stream_name, color);
|
|
|
|
if ($(".focused_table").attr("id") !== "#zhome") {
|
|
|
|
update_table_stream_color($("#zhome"), stream_name, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 23:10:59 +01:00
|
|
|
function update_stream_color(stream_name, color, opts) {
|
|
|
|
opts = $.extend({}, {update_historical: false}, opts);
|
2013-03-02 03:10:32 +01:00
|
|
|
var sub = get_sub(stream_name);
|
2013-01-22 23:10:59 +01:00
|
|
|
sub.color = color;
|
|
|
|
var id = parseInt(sub.id, 10);
|
|
|
|
$("#subscription_" + id + " .color_swatch").css('background-color', color);
|
|
|
|
if (opts.update_historical) {
|
|
|
|
update_historical_message_color(stream_name, color);
|
|
|
|
}
|
2013-03-02 03:10:32 +01:00
|
|
|
update_stream_sidebar_swatch_color(stream_name, color);
|
2013-01-22 23:10:59 +01:00
|
|
|
}
|
|
|
|
|
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-05-14 21:00:28 +02:00
|
|
|
exports.toggle_home = function (stream_name) {
|
|
|
|
var sub = get_sub(stream_name);
|
2013-04-08 20:53:12 +02:00
|
|
|
sub.in_home_view = ! sub.in_home_view;
|
2013-02-12 22:32:14 +01:00
|
|
|
|
2013-02-20 16:28:52 +01:00
|
|
|
setTimeout(function () {
|
2013-02-22 20:48:31 +01:00
|
|
|
home_msg_list.clear({clear_selected_id: false});
|
2013-02-20 16:28:52 +01:00
|
|
|
|
2013-02-22 20:48:31 +01:00
|
|
|
// Remember the scroll position as the adding or removing this
|
2013-02-20 16:28:52 +01:00
|
|
|
// number of rows might cause the page to scroll in unexpected ways
|
2013-02-23 05:19:22 +01:00
|
|
|
var saved_ypos = window.scrollY;
|
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
|
|
|
|
window.scrollTo(0, saved_ypos);
|
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-02-20 18:07:53 +01:00
|
|
|
exports.maybe_toggle_all_messages();
|
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-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-03-10 19:36:45 +01:00
|
|
|
function set_color(stream_name, color) {
|
|
|
|
update_stream_color(stream_name, color, {update_historical: true});
|
2013-04-08 19:43:11 +02:00
|
|
|
set_stream_property(stream_name, 'color', color);
|
2013-03-10 19:36:45 +01:00
|
|
|
}
|
|
|
|
|
2012-12-12 07:43:58 +01:00
|
|
|
var colorpicker_options = {
|
|
|
|
clickoutFiresChange: true,
|
|
|
|
showPalette: true,
|
|
|
|
palette: [
|
|
|
|
['a47462', 'c2726a', 'e4523d', 'e7664d', 'ee7e4a', 'f4ae55'],
|
|
|
|
['76ce90', '53a063', '94c849', 'bfd56f', 'fae589', 'f5ce6e'],
|
|
|
|
['a6dcbf', 'addfe5', 'a6c7e5', '4f8de4', '95a5fd', 'b0a5fd'],
|
|
|
|
['c2c2c2', 'c8bebf', 'c6a8ad', 'e79ab5', 'bd86e5', '9987e1']
|
|
|
|
],
|
|
|
|
change: function (color) {
|
|
|
|
// TODO: Kind of a hack.
|
2013-01-04 21:05:18 +01:00
|
|
|
var sub_row = $(this).closest('.subscription_row');
|
|
|
|
var stream_name = sub_row.find('.subscription_name').text();
|
2012-12-12 07:43:58 +01:00
|
|
|
var hex_color = color.toHexString();
|
2013-03-10 19:36:45 +01:00
|
|
|
set_color(stream_name, hex_color);
|
2012-12-12 07:43:58 +01: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-05-02 03:22:44 +02:00
|
|
|
sub = $.extend({}, {name: stream_name, id: next_sub_id++,
|
2013-03-02 03:10:32 +01:00
|
|
|
render_subscribers: should_render_subscribers(),
|
2013-04-09 02:14:13 +02:00
|
|
|
subscribed: true, in_home_view: true, invite_only: false,
|
|
|
|
notifications: false}, attrs);
|
2013-05-02 03:22:44 +02:00
|
|
|
if (sub.color === undefined) {
|
|
|
|
sub.color = pick_color();
|
|
|
|
}
|
2013-05-02 03:01:46 +02:00
|
|
|
mark_color_used(sub.color);
|
2013-03-02 03:10:32 +01:00
|
|
|
|
|
|
|
add_sub(stream_name, sub);
|
2013-05-06 02:50:05 +02:00
|
|
|
$(document).trigger($.Event('sub_obj_created.zephyr', {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);
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_sub_to_table(sub) {
|
2013-02-16 10:45:32 +01:00
|
|
|
$('#create_stream_row').after(
|
|
|
|
templates.render('subscription', {subscriptions: [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-01-22 23:16:04 +01:00
|
|
|
sub.subscribed = true;
|
2013-03-10 19:36:45 +01:00
|
|
|
set_color(stream_name, pick_color());
|
2013-05-02 03:01:46 +02:00
|
|
|
mark_color_used(sub.color);
|
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-01-22 23:16:04 +01:00
|
|
|
typeahead_helper.update_autocomplete();
|
2013-05-06 02:50:05 +02:00
|
|
|
|
|
|
|
$(document).trigger($.Event('subscription_add_done.zephyr', {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-01-22 23:16:04 +01:00
|
|
|
typeahead_helper.update_autocomplete();
|
2013-05-06 02:50:05 +02:00
|
|
|
|
|
|
|
$(document).trigger($.Event('subscription_remove_done.zephyr', {sub: sub}));
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
|
|
|
|
2013-03-29 20:27:41 +01:00
|
|
|
$(function () {
|
|
|
|
$(document).on('subscription_add.zephyr', function (e) {
|
|
|
|
mark_subscribed(e.subscription.name, e.subscription);
|
|
|
|
});
|
|
|
|
$(document).on('subscription_remove.zephyr', function (e) {
|
|
|
|
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-01-04 18:02:01 +01:00
|
|
|
return default_color;
|
|
|
|
}
|
2013-03-02 03:10:32 +01:00
|
|
|
return sub.color;
|
2012-12-01 04:37:52 +01:00
|
|
|
};
|
|
|
|
|
2013-02-16 09:43:27 +01:00
|
|
|
var lightness_threshold;
|
|
|
|
$(function () {
|
|
|
|
// sRGB color component for dark label text.
|
|
|
|
// 0x33 to match the color #333333 set by Bootstrap.
|
|
|
|
var label_color = 0x33;
|
|
|
|
var lightness = colorspace.luminance_to_lightness(
|
|
|
|
colorspace.sRGB_to_linear(label_color));
|
|
|
|
|
|
|
|
// Compute midpoint lightness between that and white (100).
|
|
|
|
lightness_threshold = (lightness + 100) / 2;
|
|
|
|
});
|
|
|
|
|
|
|
|
// From a background color (in format "#fff" or "#ffffff")
|
|
|
|
// pick a CSS class (or empty string) to determine the
|
|
|
|
// text label color etc.
|
|
|
|
//
|
|
|
|
// It would be better to work with an actual data structure
|
|
|
|
// rather than a hex string, but we have to deal with values
|
|
|
|
// already saved on the server, etc.
|
|
|
|
//
|
|
|
|
// This gets called on every message, so cache the results.
|
|
|
|
exports.get_color_class = util.memoize(function (color) {
|
|
|
|
var match, i, lightness, channel = [0, 0, 0], mult = 1;
|
|
|
|
|
|
|
|
match = /^#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})$/.exec(color);
|
|
|
|
if (!match) {
|
|
|
|
// 3-digit shorthand; Spectrum gives this e.g. for pure black.
|
|
|
|
// Multiply each digit by 16+1.
|
|
|
|
mult = 17;
|
|
|
|
|
|
|
|
match = /^#([\da-fA-F])([\da-fA-F])([\da-fA-F])$/.exec(color);
|
|
|
|
if (!match) {
|
|
|
|
// Can't understand color.
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSS colors are specified in the sRGB color space.
|
|
|
|
// Convert to linear intensity values.
|
|
|
|
for (i=0; i<3; i++) {
|
|
|
|
channel[i] = colorspace.sRGB_to_linear(mult * parseInt(match[i+1], 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute perceived lightness as CIE L*.
|
|
|
|
lightness = colorspace.luminance_to_lightness(
|
|
|
|
colorspace.rgb_luminance(channel));
|
|
|
|
|
|
|
|
// Determine if we're past the midpoint between the
|
|
|
|
// dark and light label lightness.
|
|
|
|
return (lightness < lightness_threshold) ? 'dark_background' : '';
|
|
|
|
});
|
|
|
|
|
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-04-04 22:30:28 +02:00
|
|
|
function populate_subscriptions(subs) {
|
|
|
|
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,
|
|
|
|
notifications: elem.notifications, subscribed: true});
|
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;
|
|
|
|
opts = $.extend({}, {clear_first: false, custom_callbacks: false}, opts);
|
|
|
|
|
|
|
|
if (! opts.custom_callbacks) {
|
|
|
|
on_success = function (data) {
|
|
|
|
if (data) {
|
|
|
|
populate_subscriptions(data.subscriptions);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
all_streams = stream_response.streams;
|
|
|
|
}
|
|
|
|
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-04-04 22:30:28 +02:00
|
|
|
var sub_rows = populate_subscriptions(our_subs);
|
2013-01-23 17:41:04 +01:00
|
|
|
|
2013-01-25 18:02:31 +01:00
|
|
|
all_streams.sort();
|
2013-01-23 17:41:04 +01:00
|
|
|
all_streams.forEach(function (stream) {
|
|
|
|
if (exports.have(stream)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var sub = create_sub(stream, {subscribed: false});
|
|
|
|
sub_rows.push(sub);
|
|
|
|
});
|
|
|
|
|
2013-01-23 19:10:51 +01:00
|
|
|
$('#subscriptions_table tr:gt(0)').remove();
|
2013-02-16 10:45:32 +01:00
|
|
|
$('#subscriptions_table').append(
|
|
|
|
templates.render('subscription', {subscriptions: sub_rows}));
|
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-01-23 19:10:51 +01:00
|
|
|
$('#create_stream_name').focus().select();
|
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
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
exports.have = function (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 && sub.subscribed) {
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
return false;
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-09 21:01:41 +02:00
|
|
|
|
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.
|
2012-11-16 19:43:27 +01:00
|
|
|
data: {"subscriptions": JSON.stringify([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.
|
|
|
|
data: {"subscriptions": JSON.stringify([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-03-05 21:04:02 +01:00
|
|
|
// The tutorial bot needs this function to add you to the
|
|
|
|
// tutorial-yourname stream.
|
|
|
|
exports.tutorial_subscribe_or_add_me_to = function (stream_name) {
|
2013-03-11 23:13:37 +01:00
|
|
|
var stream_status = compose.check_stream_existence(stream_name);
|
2013-03-05 21:04:02 +01:00
|
|
|
if (stream_status === 'does-not-exist') {
|
2013-04-12 21:36:15 +02:00
|
|
|
return ajaxSubscribeForCreation(stream_name, [page_params.email], false);
|
2013-03-05 21:04:02 +01:00
|
|
|
} else {
|
2013-04-12 21:36:15 +02:00
|
|
|
return ajaxSubscribe(stream_name);
|
2013-03-05 21:04:02 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.tutorial_unsubscribe_me_from = function (stream_name) {
|
|
|
|
ajaxUnsubscribe(stream_name);
|
|
|
|
};
|
|
|
|
|
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-03-07 21:26:28 +01:00
|
|
|
var people_minus_you_and_maybe_humbuggers = [];
|
2013-03-25 23:26:14 +01:00
|
|
|
$.each(page_params.people_list, function (idx, person) {
|
|
|
|
if (person.email !== page_params.email &&
|
|
|
|
(page_params.domain === "humbughq.com" ||
|
2013-03-07 21:26:28 +01:00
|
|
|
person.email.split('@')[1] !== "humbughq.com"
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
people_minus_you_and_maybe_humbuggers.push({"email": person.email,
|
|
|
|
"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-03-07 21:26:28 +01:00
|
|
|
users: people_minus_you_and_maybe_humbuggers.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-05-02 02:51:35 +02:00
|
|
|
populate_subscriptions(page_params.stream_list);
|
2012-10-03 22:24:17 +02:00
|
|
|
|
2012-10-31 18:36:08 +01:00
|
|
|
$("#add_new_subscription").on("submit", function (e) {
|
|
|
|
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());
|
|
|
|
var principals = [];
|
|
|
|
$("#stream_creation_form input:checkbox[name=user]:checked").each(function () {
|
|
|
|
principals.push($(this).val());
|
|
|
|
});
|
|
|
|
// 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-01-16 20:16:28 +01:00
|
|
|
colorpicker.spectrum(colorpicker_options);
|
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-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
|
|
|
// From here down is only stuff that happens when we're rendering
|
|
|
|
// the subscriber settings
|
|
|
|
|
|
|
|
$("#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',
|
|
|
|
data: {"subscriptions": JSON.stringify([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-01-14 22:19:13 +01: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-02-07 17:59:27 +01:00
|
|
|
$.each(subscribers.sort().reverse(), function (idx, elem) {
|
|
|
|
// 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
|
|
|
});
|
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() {
|
|
|
|
var operators = narrow.operators();
|
|
|
|
if (!operators) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var stream_name = operators[0][1];
|
|
|
|
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;
|
|
|
|
|
|
|
|
}());
|