Module pattern for subs.js

(imported from commit adfe466ea892fbfe5f13a5f346e4fd00f194c42a)
This commit is contained in:
Keegan McAllister
2012-10-18 15:37:07 -04:00
parent 2a2c0187c5
commit 95f8eea14b
5 changed files with 56 additions and 47 deletions

View File

@@ -27,8 +27,7 @@ var globals =
+ ' loading_spinner templates' + ' loading_spinner templates'
// subs.js // subs.js
+ ' fetch_subs sub_from_home subscribed_to stream_list_hash' + ' subs'
+ ' add_to_stream_list'
// ui.js // ui.js
+ ' register_onclick hide_email show_email' + ' register_onclick hide_email show_email'

View File

@@ -146,7 +146,7 @@ function validate_stream_message() {
return false; return false;
} }
if (!subscribed_to(stream_name)) { if (!subs.have(stream_name)) {
if (!check_stream_for_send(stream_name)) { if (!check_stream_for_send(stream_name)) {
return false; return false;
} }

View File

@@ -1,4 +1,37 @@
function fetch_subs() { var subs = (function () {
var exports = {};
var stream_list_hash = [];
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) {
if (!exports.have(stream_name)) {
stream_list.push(stream_name);
stream_list_hash[stream_name.toLowerCase()] = true;
}
}
function remove_from_stream_list(stream_name) {
delete stream_list_hash[stream_name.toLowerCase()];
var removal_index = case_insensitive_subscription_index(stream_name);
if (removal_index !== -1) {
stream_list.splice(removal_index, 1);
}
}
exports.fetch = function () {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: 'json/subscriptions/list', url: 'json/subscriptions/list',
@@ -17,9 +50,9 @@ function fetch_subs() {
report_error("Error listing subscriptions", xhr, $("#subscriptions-status")); report_error("Error listing subscriptions", xhr, $("#subscriptions-status"));
} }
}); });
} };
function sub_from_home(stream, prompt_button) { exports.add = function (stream, prompt_button) {
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: '/json/subscriptions/add', url: '/json/subscriptions/add',
@@ -35,43 +68,21 @@ function sub_from_home(stream, prompt_button) {
report_error("Unable to subscribe", xhr, $("#home-error")); report_error("Unable to subscribe", xhr, $("#home-error"));
} }
}); });
} };
stream_list_hash = []; exports.have = function (stream_name) {
function subscribed_to(stream_name) {
return (stream_list_hash[stream_name.toLowerCase()] === true); return (stream_list_hash[stream_name.toLowerCase()] === true);
} };
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) {
if (!subscribed_to(stream_name)) {
stream_list.push(stream_name);
stream_list_hash[stream_name.toLowerCase()] = true;
}
}
function remove_from_stream_list(stream_name) {
delete stream_list_hash[stream_name.toLowerCase()];
var removal_index = case_insensitive_subscription_index(stream_name);
if (removal_index !== -1) {
stream_list.splice(removal_index, 1);
}
}
// FIXME: It would be nice to move the UI setup into ui.js.
$(function () { $(function () {
var i;
// Populate stream_list_hash with data handed over to client-side template.
for (i = 0; i < stream_list.length; i++) {
stream_list_hash[stream_list[i].toLowerCase()] = true;
}
// FIXME: It would be nice to move the UI setup into ui.js.
$("#current_subscriptions").ajaxForm({ $("#current_subscriptions").ajaxForm({
dataType: 'json', // This seems to be ignored. We still get back an xhr. dataType: 'json', // This seems to be ignored. We still get back an xhr.
success: function (resp, statusText, xhr, form) { success: function (resp, statusText, xhr, form) {
@@ -104,3 +115,7 @@ $(function () {
} }
}); });
}); });
return exports;
}());

View File

@@ -255,12 +255,12 @@ $(function () {
// Prepare the click handler for subbing to a new stream to which // Prepare the click handler for subbing to a new stream to which
// you have composed a message. // you have composed a message.
$('#create-it').click(function () { $('#create-it').click(function () {
sub_from_home(compose.stream_name(), $('#stream-dne')); subs.add(compose.stream_name(), $('#stream-dne'));
}); });
// Prepare the click handler for subbing to an existing stream. // Prepare the click handler for subbing to an existing stream.
$('#sub-it').click(function () { $('#sub-it').click(function () {
sub_from_home(compose.stream_name(), $('#stream-nosub')); subs.add(compose.stream_name(), $('#stream-nosub'));
}); });
var throttled_scrollhandler = $.throttle(50, function(e) { var throttled_scrollhandler = $.throttle(50, function(e) {
@@ -319,7 +319,7 @@ $(function () {
compose.show('stream', $("#stream")); compose.show('stream', $("#stream"));
}); });
$('#sidebar a[href="#subscriptions"]').click(fetch_subs); $('#sidebar a[href="#subscriptions"]').click(subs.fetch);
var settings_status = $('#settings-status'); var settings_status = $('#settings-status');
$("#settings-change-box form").ajaxForm({ $("#settings-change-box form").ajaxForm({

View File

@@ -62,11 +62,6 @@ $(function () {
send_status.hide(); send_status.hide();
$("#compose form").ajaxForm(options); $("#compose form").ajaxForm(options);
// Populate stream_list_hash with data handed over to client-side template.
for (i = 0; i < stream_list.length; i++) {
stream_list_hash[stream_list[i].toLowerCase()] = true;
}
$.each(people_list, function (idx, person) { $.each(people_list, function (idx, person) {
people_hash[person.email] = 1; people_hash[person.email] = 1;
}); });