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-01-04 20:42:39 +01:00
|
|
|
var next_sub_id = 0;
|
2013-01-04 18:02:01 +01:00
|
|
|
|
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-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-01-07 22:18:47 +01:00
|
|
|
return domain !== 'mit.edu';
|
|
|
|
}
|
|
|
|
|
2013-01-23 17:51:01 +01:00
|
|
|
function should_list_all_streams() {
|
|
|
|
return domain !== 'mit.edu';
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
var sub = stream_info[stream_name.toLowerCase()];
|
|
|
|
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-02-12 22:32:14 +01:00
|
|
|
function stream_home_view_clicked(e) {
|
|
|
|
var in_home_view, cb;
|
|
|
|
if (e.target.type === "checkbox") {
|
|
|
|
in_home_view = e.target.checked;
|
|
|
|
} else {
|
|
|
|
cb = $(e.target).closest('.sub_setting_show_in_home').children('.sub_setting_show_in_home_cb')[0];
|
|
|
|
in_home_view = !cb.checked;
|
|
|
|
$(cb).prop("checked", in_home_view);
|
|
|
|
}
|
|
|
|
|
|
|
|
var sub_row = $(e.target).closest('.subscription_row');
|
|
|
|
var stream = sub_row.find('.subscription_name').text();
|
|
|
|
|
|
|
|
if (in_home_view === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sub = stream_info[stream.toLowerCase()];
|
|
|
|
sub.in_home_view = in_home_view;
|
|
|
|
|
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
|
|
|
|
add_messages(all_msg_list.all(), home_msg_list, {append_to_table: true, update_unread_counts: false});
|
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;
|
|
|
|
|
|
|
|
// If we added any messages that were unread but before the currently selected message pointer
|
|
|
|
// we need to re-process them to update the unread count
|
2013-02-22 20:48:31 +01:00
|
|
|
if (! home_msg_list.empty()) {
|
|
|
|
process_unread_counts(message_range(home_msg_list.first().id, home_msg_list.selected_id()), true);
|
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-02-12 22:32:14 +01:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/subscriptions/property',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
"property": "in_home_view",
|
|
|
|
"stream_name": stream,
|
|
|
|
"in_home_view": in_home_view
|
|
|
|
},
|
|
|
|
timeout: 10*1000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-01-22 23:10:59 +01:00
|
|
|
|
|
|
|
update_stream_color(stream_name, hex_color, {update_historical: true});
|
2012-12-12 07:43:58 +01:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/subscriptions/property',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
"property": "stream_colors",
|
|
|
|
"stream_name": stream_name,
|
|
|
|
"color": hex_color
|
|
|
|
},
|
|
|
|
timeout: 10*1000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function create_sub(stream_name, attrs) {
|
|
|
|
var sub = $.extend({}, {name: stream_name, color: default_color, id: next_sub_id++,
|
2013-01-24 18:20:00 +01:00
|
|
|
render_subscribers: should_render_subscribers(),
|
2013-02-05 20:30:00 +01:00
|
|
|
subscribed: true, in_home_view: true, invite_only: false}, attrs);
|
2013-01-22 23:16:04 +01:00
|
|
|
stream_info[stream_name.toLowerCase()] = sub;
|
2013-02-06 19:48:26 +01:00
|
|
|
if (sub.subscribed) {
|
2013-02-08 17:58:08 +01:00
|
|
|
// This will do nothing on MIT
|
2013-02-06 19:48:26 +01:00
|
|
|
ui.add_narrow_filter(stream_name, "stream", "#narrow/stream/" + encodeURIComponent(stream_name));
|
|
|
|
}
|
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) {
|
|
|
|
$('#create_stream_row').after(templates.subscription({subscriptions: [sub]}));
|
|
|
|
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-01-04 20:42:39 +01:00
|
|
|
var lstream_name = stream_name.toLowerCase();
|
2013-01-22 23:16:04 +01:00
|
|
|
var sub = stream_info[lstream_name];
|
2013-01-04 20:42:39 +01:00
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
if (sub === undefined) {
|
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) {
|
|
|
|
sub.subscribed = true;
|
2013-02-08 17:58:08 +01:00
|
|
|
// This will do nothing on MIT
|
2013-02-06 19:48:26 +01:00
|
|
|
ui.add_narrow_filter(stream_name, "stream", "#narrow/stream/" + encodeURIComponent(stream_name));
|
2013-01-22 23:16:04 +01:00
|
|
|
var button = button_for_sub(sub);
|
|
|
|
if (button.length !== 0) {
|
|
|
|
button.text("Unsubscribe").removeClass("btn-primary");
|
|
|
|
} 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
|
|
|
|
|
|
|
// Add the user to the member list if they're currently
|
|
|
|
// viewing the members of this stream
|
2013-01-23 21:35:32 +01:00
|
|
|
var settings = settings_for_sub(sub);
|
2013-01-23 22:08:34 +01:00
|
|
|
if (sub.render_subscribers && settings.hasClass('in')) {
|
2013-01-23 21:35:32 +01:00
|
|
|
var members = settings.find(".subscriber_list_container ul");
|
|
|
|
add_to_member_list(members, fullname, email);
|
|
|
|
}
|
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-01-22 23:16:04 +01:00
|
|
|
typeahead_helper.update_autocomplete();
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
function mark_unsubscribed(stream_name) {
|
2013-01-04 20:42:39 +01:00
|
|
|
var lstream_name = stream_name.toLowerCase();
|
2013-01-22 23:16:04 +01:00
|
|
|
var sub = stream_info[lstream_name];
|
|
|
|
|
2013-02-06 19:48:26 +01:00
|
|
|
ui.remove_narrow_filter(stream_name, 'stream');
|
|
|
|
|
2013-01-22 23:16:04 +01:00
|
|
|
if (sub === undefined) {
|
|
|
|
// We don't know about this stream
|
|
|
|
return;
|
|
|
|
} else if (sub.subscribed) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
typeahead_helper.update_autocomplete();
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
|
|
|
|
2012-12-01 04:37:52 +01:00
|
|
|
exports.get_color = function (stream_name) {
|
2013-01-07 23:30:21 +01:00
|
|
|
var lstream_name = stream_name.toLowerCase();
|
|
|
|
if (stream_info[lstream_name] === undefined) {
|
2013-01-04 18:02:01 +01:00
|
|
|
return default_color;
|
|
|
|
}
|
2013-01-07 23:30:21 +01:00
|
|
|
return stream_info[lstream_name].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) {
|
|
|
|
var lstream_name = stream_name.toLowerCase();
|
|
|
|
if (stream_info[lstream_name] === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return stream_info[lstream_name].invite_only;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
var our_subs;
|
|
|
|
var all_streams;
|
|
|
|
|
|
|
|
function maybe_populate_subscriptions() {
|
|
|
|
// We only execute if both asynchronous queries have returned
|
|
|
|
if (our_subs === undefined || all_streams === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sub_rows = [];
|
2013-01-25 18:02:31 +01:00
|
|
|
our_subs.sort(function (a, b) {
|
2013-01-29 21:40:16 +01:00
|
|
|
return a.name.localeCompare(b.name);
|
2013-01-25 18:02:31 +01:00
|
|
|
});
|
2013-01-23 17:41:04 +01:00
|
|
|
our_subs.forEach(function (elem) {
|
2013-01-29 21:40:16 +01:00
|
|
|
var stream_name = elem.name;
|
2013-02-05 20:30:00 +01:00
|
|
|
var sub = create_sub(stream_name, {color: elem.color, in_home_view: elem.in_home_view,
|
|
|
|
invite_only: elem.invite_only, subscribed: true});
|
2013-01-23 17:41:04 +01:00
|
|
|
stream_info[stream_name.toLowerCase()] = sub;
|
|
|
|
sub_rows.push(sub);
|
|
|
|
});
|
|
|
|
|
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});
|
|
|
|
stream_info[stream.toLowerCase()] = sub;
|
|
|
|
sub_rows.push(sub);
|
|
|
|
});
|
|
|
|
|
2013-01-23 19:10:51 +01:00
|
|
|
$('#subscriptions_table tr:gt(0)').remove();
|
2013-01-23 17:41:04 +01:00
|
|
|
$('#subscriptions_table').append(templates.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-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
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/get_public_streams',
|
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*1000,
|
|
|
|
success: function (data) {
|
|
|
|
if (data) {
|
|
|
|
all_streams = data.streams;
|
|
|
|
maybe_populate_subscriptions();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
util.destroy_loading_indicator($('#subs_page_loading_indicator'));
|
|
|
|
ui.report_error("Error listing subscriptions", xhr, $("#subscriptions-status"));
|
2013-01-23 17:41:04 +01:00
|
|
|
}
|
2013-01-23 17:51:01 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
all_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
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
2013-01-23 17:51:01 +01:00
|
|
|
url: '/json/subscriptions/list',
|
2013-01-23 17:41:04 +01:00
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*1000,
|
|
|
|
success: function (data) {
|
2012-10-03 22:24:17 +02:00
|
|
|
if (data) {
|
2013-01-23 17:51:01 +01:00
|
|
|
our_subs = data.subscriptions;
|
2013-01-23 17:41:04 +01:00
|
|
|
maybe_populate_subscriptions();
|
2012-10-03 22:24:17 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2013-01-16 21:26:55 +01:00
|
|
|
util.destroy_loading_indicator($('#subs_page_loading_indicator'));
|
2012-11-16 16:45:39 +01:00
|
|
|
ui.report_error("Error listing subscriptions", xhr, $("#subscriptions-status"));
|
2012-10-03 22:24:17 +02:00
|
|
|
}
|
|
|
|
});
|
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-01-22 23:16:04 +01:00
|
|
|
var sub = stream_info[stream_name.toLowerCase()];
|
|
|
|
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;
|
|
|
|
|
2012-11-07 23:25:04 +01:00
|
|
|
$.ajax({
|
|
|
|
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.
|
|
|
|
true_stream_name = res.already_subscribed[email][0];
|
|
|
|
ui.report_success("Already subscribed to " + true_stream_name,
|
|
|
|
$("#subscriptions-status"));
|
2012-11-07 23:25:04 +01:00
|
|
|
} else {
|
2013-01-31 21:09:34 +01:00
|
|
|
// Display the canonical stream capitalization.
|
|
|
|
true_stream_name = res.subscribed[email][0];
|
2012-11-07 23:25:04 +01:00
|
|
|
}
|
2013-01-31 21:09:34 +01:00
|
|
|
mark_subscribed(true_stream_name);
|
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();
|
2012-11-16 20:11:08 +01:00
|
|
|
if (res.removed.length === 0) {
|
|
|
|
name = res.not_subscribed[0];
|
|
|
|
} else {
|
|
|
|
name = res.removed[0];
|
|
|
|
}
|
2013-01-22 23:16:04 +01:00
|
|
|
mark_unsubscribed(name);
|
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.
|
|
|
|
$.ajax({
|
|
|
|
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-02-05 20:30:00 +01:00
|
|
|
mark_subscribed(stream, {invite_only: invite_only});
|
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-02-02 00:07:50 +01:00
|
|
|
var name_cmp = person1.full_name.localeCompare(person2.full_name);
|
|
|
|
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-02-02 00:07:50 +01:00
|
|
|
return person1.email.localeCompare(person2.email);
|
2013-01-31 21:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function show_new_stream_modal() {
|
|
|
|
var people_minus_you = [];
|
|
|
|
$.each(people_list, function (idx, person) {
|
|
|
|
if (person.email !== email) {
|
|
|
|
people_minus_you.push({"email": person.email, "full_name": person.full_name});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#people_to_add').html(templates.new_stream_users({
|
2013-02-02 00:07:50 +01:00
|
|
|
users: people_minus_you.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.
|
2012-11-16 20:11:08 +01:00
|
|
|
for (i = 0; i < stream_list.length; i++) {
|
2013-01-30 21:02:41 +01:00
|
|
|
stream_info[stream_list[i].name.toLowerCase()] = create_sub(stream_list[i].name, stream_list[i]);
|
2012-11-16 20:11:08 +01:00
|
|
|
}
|
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());
|
|
|
|
var stream_status = compose.check_stream_existence(stream)[0];
|
|
|
|
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.
|
|
|
|
principals.push(email);
|
2013-01-30 23:12:23 +01:00
|
|
|
ajaxSubscribeForCreation(stream, principals, $('#invite-only').is(':checked'));
|
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();
|
|
|
|
var sub = stream_info[stream_name.toLowerCase()];
|
|
|
|
|
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) {
|
|
|
|
var colorpicker = $(e.target).closest('.subscription_row').find('.colorpicker');
|
|
|
|
colorpicker.spectrum(colorpicker_options);
|
|
|
|
});
|
|
|
|
|
2013-02-12 22:32:14 +01:00
|
|
|
$("#subscriptions_table").on("click", ".sub_setting_show_in_home", stream_home_view_clicked);
|
|
|
|
|
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-01-29 20:36:01 +01:00
|
|
|
if (principal === email) {
|
|
|
|
// 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-01-31 21:09:34 +01:00
|
|
|
exports.set_all_users = function (e, val) {
|
|
|
|
$('#people_to_add :checkbox').attr('checked', val);
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|