2012-10-18 21:37:07 +02:00
|
|
|
var subs = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2012-10-31 23:16:51 +01:00
|
|
|
var stream_set = {};
|
2012-10-18 21:37:07 +02:00
|
|
|
|
|
|
|
function case_insensitive_subscription_index(stream_name) {
|
|
|
|
var i;
|
|
|
|
var name = stream_name.toLowerCase();
|
|
|
|
|
|
|
|
for (i = 1; i < stream_list.length; i++) {
|
|
|
|
if (name === stream_list[i].toLowerCase()) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_to_stream_list(stream_name) {
|
2012-11-07 23:25:04 +01:00
|
|
|
var stream_sub_row;
|
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
if (!exports.have(stream_name)) {
|
|
|
|
stream_list.push(stream_name);
|
2012-10-31 23:16:51 +01:00
|
|
|
stream_set[stream_name.toLowerCase()] = true;
|
2012-11-07 23:25:04 +01:00
|
|
|
|
|
|
|
stream_sub_row = $('#subscriptions_table').find('button[value="' + stream_name + '"]');
|
|
|
|
if (stream_sub_row.length) {
|
|
|
|
stream_sub_row.text("Unsubscribe")
|
2012-11-09 22:40:34 +01:00
|
|
|
.removeClass("btn-primary")
|
2012-11-16 20:11:08 +01:00
|
|
|
.unbind("click")
|
|
|
|
.removeAttr("onclick")
|
|
|
|
.click(function (event) {exports.unsubscribe(stream_name);});
|
2012-11-07 23:25:04 +01:00
|
|
|
} else {
|
|
|
|
$('#subscriptions_table').prepend(templates.subscription({subscription: stream_name}));
|
|
|
|
}
|
2012-10-18 21:37:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_from_stream_list(stream_name) {
|
2012-10-31 23:16:51 +01:00
|
|
|
delete stream_set[stream_name.toLowerCase()];
|
2012-10-18 21:37:07 +02:00
|
|
|
var removal_index = case_insensitive_subscription_index(stream_name);
|
|
|
|
if (removal_index !== -1) {
|
|
|
|
stream_list.splice(removal_index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = function () {
|
2012-10-03 22:24:17 +02:00
|
|
|
$.ajax({
|
2012-10-16 22:50:46 +02:00
|
|
|
type: 'POST',
|
2012-10-31 23:30:32 +01:00
|
|
|
url: '/json/subscriptions/list',
|
2012-10-03 22:24:17 +02:00
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*1000,
|
|
|
|
success: function (data) {
|
|
|
|
$('#subscriptions_table tr').remove();
|
|
|
|
if (data) {
|
|
|
|
$.each(data.subscriptions, function (index, name) {
|
|
|
|
$('#subscriptions_table').append(templates.subscription({subscription: name}));
|
|
|
|
});
|
|
|
|
}
|
2012-10-31 23:10:18 +01:00
|
|
|
$('#streams').focus().select();
|
2012-10-03 22:24:17 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
report_error("Error listing subscriptions", xhr, $("#subscriptions-status"));
|
|
|
|
}
|
|
|
|
});
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-03 22:24:17 +02:00
|
|
|
|
2012-10-31 23:15:27 +01:00
|
|
|
exports.subscribe_for_send = function (stream, prompt_button) {
|
2012-10-03 22:24:17 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/subscriptions/add',
|
2012-11-16 19:43:27 +01:00
|
|
|
data: {"subscriptions": JSON.stringify([stream]) },
|
2012-10-03 22:24:17 +02:00
|
|
|
dataType: 'json',
|
|
|
|
timeout: 10*60*1000, // 10 minutes in ms
|
2012-10-09 21:01:41 +02:00
|
|
|
success: function (response) {
|
2012-10-31 18:36:08 +01:00
|
|
|
add_to_stream_list(stream);
|
|
|
|
compose.finish();
|
|
|
|
prompt_button.stop(true).fadeOut(500);
|
2012-10-03 22:24:17 +02:00
|
|
|
},
|
|
|
|
error: function (xhr, error_type, exn) {
|
2012-10-31 18:36:08 +01:00
|
|
|
report_error("Unable to subscribe", xhr, $("#home-error"));
|
2012-10-03 22:24:17 +02:00
|
|
|
}
|
|
|
|
});
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-09 21:01:41 +02:00
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
exports.have = function (stream_name) {
|
2012-10-31 23:16:51 +01:00
|
|
|
return (stream_set[stream_name.toLowerCase()] === true);
|
2012-10-18 21:37:07 +02:00
|
|
|
};
|
2012-10-09 21:01:41 +02:00
|
|
|
|
2012-11-09 22:56:05 +01:00
|
|
|
function ajaxSubscribe(stream) {
|
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) {
|
2012-11-09 22:56:05 +01:00
|
|
|
if ($("#streams").val() === stream) {
|
2012-11-07 23:25:04 +01:00
|
|
|
$("#streams").val("");
|
|
|
|
}
|
|
|
|
var name, res = $.parseJSON(xhr.responseText);
|
|
|
|
if (res.subscribed.length === 0) {
|
|
|
|
name = res.already_subscribed[0];
|
|
|
|
report_success("Already subscribed to " + name, $("#subscriptions-status"));
|
|
|
|
} else {
|
|
|
|
name = res.subscribed[0];
|
|
|
|
report_success("Successfully added subscription to " + name,
|
|
|
|
$("#subscriptions-status"));
|
|
|
|
}
|
|
|
|
add_to_stream_list(name);
|
|
|
|
$("#streams").focus();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
report_error("Error adding subscription", xhr, $("#subscriptions-status"));
|
|
|
|
$("#streams").focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-10-09 21:01:41 +02:00
|
|
|
|
2012-11-16 20:11:08 +01:00
|
|
|
exports.unsubscribe = function (stream) {
|
|
|
|
$.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);
|
|
|
|
if (res.removed.length === 0) {
|
|
|
|
name = res.not_subscribed[0];
|
|
|
|
report_success("Already not subscribed to " + name,
|
|
|
|
$("#subscriptions-status"));
|
|
|
|
} else {
|
|
|
|
name = res.removed[0];
|
|
|
|
report_success("Successfully removed subscription to " + name,
|
|
|
|
$("#subscriptions-status"));
|
|
|
|
}
|
2012-11-09 22:40:34 +01:00
|
|
|
$('#subscriptions_table').find('button[value="' + name + '"]').text("Subscribe")
|
|
|
|
.addClass("btn-primary")
|
2012-11-16 20:11:08 +01:00
|
|
|
.unbind("click")
|
|
|
|
.removeAttr("onclick")
|
2012-11-07 23:25:04 +01:00
|
|
|
.click(function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
ajaxSubscribe(name);
|
|
|
|
});
|
2012-10-10 23:26:01 +02:00
|
|
|
remove_from_stream_list(name);
|
2012-11-19 01:09:40 +01:00
|
|
|
typeahead_helper.update_autocomplete();
|
2012-11-16 20:11:08 +01:00
|
|
|
$("#streams").focus();
|
2012-10-03 22:24:17 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
report_error("Error removing subscription", xhr, $("#subscriptions-status"));
|
2012-11-16 20:11:08 +01:00
|
|
|
$("#streams").focus();
|
2012-10-03 22:24:17 +02:00
|
|
|
}
|
|
|
|
});
|
2012-11-16 20:11:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
var i;
|
|
|
|
// Populate stream_set with data handed over to client-side template.
|
|
|
|
for (i = 0; i < stream_list.length; i++) {
|
|
|
|
stream_set[stream_list[i].toLowerCase()] = true;
|
|
|
|
}
|
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();
|
2012-11-07 23:25:04 +01:00
|
|
|
ajaxSubscribe($("#streams").val());
|
2012-10-03 22:24:17 +02:00
|
|
|
});
|
|
|
|
});
|
2012-10-18 21:37:07 +02:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|