Move "page parameters" (email, enter_sends, etc) into a single object

(imported from commit 842b2371bf6364982f1358f1cd2d91118c7fb2bf)
This commit is contained in:
Keegan McAllister 2013-03-25 18:26:14 -04:00
parent 38ebefe68f
commit 6d9aca5f1a
15 changed files with 88 additions and 86 deletions

View File

@ -52,33 +52,36 @@
{% endif %}
<script type="text/javascript">
{% autoescape off %}
var page_params = {
{# Not escaped, because it's guaranteed by the model to be an integer. #}
var initial_pointer = {{ user_profile.pointer }};
{# Not escaped, because it's guaranteed by the model to be an integer. #}
initial_pointer: {{ user_profile.pointer }},
var poll_timeout = {{ poll_timeout }};
poll_timeout: {{ poll_timeout }},
var fullname = "{{ user_profile.full_name|escapejs }}";
var email = "{{ user_profile.user.email|escapejs }}";
var domain = "{{ user_profile.realm.domain|escapejs }}";
var have_initial_messages = {{ have_initial_messages|escapejs }};
var desktop_notifications_enabled = {{ desktop_notifications_enabled|escapejs }};
var enter_sends = {{ enter_sends|escapejs }};
var debug_mode = {% if debug %} true {% else %} false {% endif %};
var needs_tutorial = {{ needs_tutorial|escapejs }};
fullname: "{{ user_profile.full_name|escapejs }}",
email: "{{ user_profile.user.email|escapejs }}",
domain: "{{ user_profile.realm.domain|escapejs }}",
have_initial_messages: {{ have_initial_messages|escapejs }},
desktop_notifications_enabled: {{ desktop_notifications_enabled|escapejs }},
enter_sends: {{ enter_sends|escapejs }},
debug_mode: {% if debug %} true {% else %} false {% endif %},
needs_tutorial: {{ needs_tutorial|escapejs }},
{# We use JSONEncoderForHTML to generate "streams". #}
var stream_list = {{ streams }};
var people_list = [
{% for person in people %}
{ 'email': "{{ person.email|escapejs }}",
'full_name': "{{ person.full_name|escapejs }}" },
{% endfor %}
];
{# We use JSONEncoderForHTML to generate "streams". #}
stream_list: {{ streams }},
people_list: [
{% for person in people %}
{ 'email': "{{ person.email|escapejs }}",
'full_name': "{{ person.full_name|escapejs }}" },
{% endfor %}
]
};
{% endautoescape %}
</script>
{% endblock %}
{% block content %}

View File

@ -6,9 +6,7 @@ var globals =
' $ jQuery Spinner Handlebars XDate'
// index.html
+ ' initial_pointer email stream_list people_list have_initial_messages'
+ ' fullname desktop_notifications_enabled enter_sends domain poll_timeout'
+ ' debug_mode needs_tutorial'
+ ' page_params'
// common.js
+ ' status_classes'

View File

@ -90,7 +90,7 @@ function focus_ping() {
age = now - presence.website.timestamp;
}
if (email !== this_email) {
if (page_params.email !== this_email) {
var status = 'idle';
if (presence.website !== undefined
&& presence.website.status === ACTIVE && age >= 0) {

View File

@ -86,7 +86,7 @@ BlueslipError.prototype = Error.prototype;
return func.blueslip_wrapper;
}
var new_func = function blueslip_wrapper() {
if (debug_mode) {
if (page_params.debug_mode) {
return func.apply(this, arguments);
} else {
try {
@ -230,13 +230,13 @@ exports.info = function blueslip_info (msg) {
exports.warn = function blueslip_warn (msg) {
console.warn(msg);
if (debug_mode) {
if (page_params.debug_mode) {
console.trace();
}
};
exports.error = function blueslip_error (msg) {
if (debug_mode) {
if (page_params.debug_mode) {
throw new BlueslipError(msg);
} else {
console.error(msg);
@ -245,7 +245,7 @@ exports.error = function blueslip_error (msg) {
};
exports.fatal = function blueslip_fatal (msg) {
if (! debug_mode) {
if (! page_params.debug_mode) {
report_error(msg, Error().stack, {show_ui_msg: true});
}

View File

@ -95,8 +95,8 @@ function handle_keydown(e) {
// want to change focus right away in the private_message_recipient box since it
// takes the typeaheads a little time to open after the user finishes typing, which
// can lead to the focus moving without the autocomplete having a chance to happen.
if ((domain === "humbughq.com" && nextFocus === "compose-send-button") ||
(domain !== "humbughq.com" && nextFocus)) {
if ((page_params.domain === "humbughq.com" && nextFocus === "compose-send-button") ||
(page_params.domain !== "humbughq.com" && nextFocus)) {
ui.focus_on(nextFocus);
nextFocus = false;
}
@ -106,7 +106,7 @@ function handle_keydown(e) {
// (Unless shift is being held down, which we *do* want to insert a linebreak)
if (e.target.id === "new_message_content"
&& code === 13 && !e.shiftKey
&& enter_sends) {
&& page_params.enter_sends) {
e.preventDefault();
if ($("#compose-send-button").attr('disabled') !== "disabled") {
$("#compose-send-button").attr('disabled', 'disabled');
@ -158,8 +158,8 @@ exports.initialize = function () {
$("#enter_sends").click(function () {
var send_button = $("#compose-send-button");
enter_sends = $("#enter_sends").is(":checked");
if (enter_sends) {
page_params.enter_sends = $("#enter_sends").is(":checked");
if (page_params.enter_sends) {
send_button.fadeOut();
} else {
send_button.fadeIn();
@ -168,11 +168,11 @@ exports.initialize = function () {
dataType: 'json',
url: '/json/change_enter_sends',
type: 'POST',
data: {'enter_sends': enter_sends}
data: {'enter_sends': page_params.enter_sends}
});
});
$("#enter_sends").prop('checked', enter_sends);
if (enter_sends) $("#compose-send-button").hide();
$("#enter_sends").prop('checked', page_params.enter_sends);
if (page_params.enter_sends) $("#compose-send-button").hide();
// limit number of items so the list doesn't fall off the screen
$( "#stream" ).typeahead({

View File

@ -57,7 +57,7 @@ function parse_narrow(hash) {
if (current_msg_list.selected_id() !== -1) {
new_selection = current_msg_list.selected_id();
} else {
new_selection = initial_pointer;
new_selection = page_params.initial_pointer;
}
narrow.activate(operators, {
then_select_id: new_selection,

View File

@ -278,7 +278,7 @@ MessageList.prototype = {
message.dom_id = table_name + message.id;
if (message.sender_email === email) {
if (message.sender_email === page_params.email) {
message.stamp = ui.get_gravatar_stamp();
}

View File

@ -16,11 +16,11 @@ function browser_desktop_notifications_on () {
}
exports.initialize = function () {
names = fullname.toLowerCase().split(" ");
names.push(email.split("@")[0].toLowerCase());
names = page_params.fullname.toLowerCase().split(" ");
names.push(page_params.email.split("@")[0].toLowerCase());
names.push("all");
names.push("everyone");
names.push("<strong>" + fullname.toLowerCase() + "</strong>");
names.push("<strong>" + page_params.fullname.toLowerCase() + "</strong>");
$(window).focus(function () {
window_has_focus = true;
@ -43,7 +43,7 @@ exports.initialize = function () {
}
$(document).click(function () {
if (!desktop_notifications_enabled || asked_permission_already) {
if (!page_params.desktop_notifications_enabled || asked_permission_already) {
return;
}
if (window.webkitNotifications.checkPermission() !== 0) { // 0 is PERMISSION_ALLOWED
@ -59,7 +59,7 @@ exports.update_title_count = function () {
var new_message_count = unread_in_current_view();
document.title = (new_message_count ? ("(" + new_message_count + ") ") : "")
+ domain + " - Humbug";
+ page_params.domain + " - Humbug";
// IE doesn't support PNG favicons, *shrug*
if (! $.browser.msie) {
@ -163,7 +163,7 @@ exports.speaking_at_me = function (message) {
var found_match = false, indexof, after_name, after_atname;
var punctuation = /[\.,-\/#!$%\^&\*;:{}=\-_`~()\+\?\[\]\s<>]/;
if (domain === "mit.edu") {
if (page_params.domain === "mit.edu") {
return false;
}
@ -199,10 +199,10 @@ exports.received_messages = function (messages) {
}
$.each(messages, function (index, message) {
if (message.sender_email !== email && narrow.message_in_home(message)) {
if (message.sender_email !== page_params.email && narrow.message_in_home(message)) {
title_needs_update = true;
if (desktop_notifications_enabled &&
if (page_params.desktop_notifications_enabled &&
browser_desktop_notifications_on() &&
(message.type === "private" ||
exports.speaking_at_me(message))) {

View File

@ -57,10 +57,10 @@ exports.update_typeahead = function () {
var streams = $.map(subs.subscribed_streams(), function(elt,idx) {
return {action: 'stream', query: elt};
});
var people = $.map(people_list, function(elt,idx) {
var people = $.map(page_params.people_list, function(elt,idx) {
return {action: 'private_message', query: elt};
});
var senders = $.map(people_list, function(elt,idx) {
var senders = $.map(page_params.people_list, function(elt,idx) {
return {action: 'sender', query: elt};
});
var options = streams.concat(people).concat(senders);

View File

@ -5,7 +5,7 @@ var csrf_token;
$(function () {
// Display loading indicator. This disappears after the first
// get_updates completes.
if (have_initial_messages) {
if (page_params.have_initial_messages) {
util.make_loading_indicator($('#page_loading_indicator'), 'Loading...');
} else {
util.show_first_run_message();

View File

@ -80,11 +80,11 @@ exports.maybe_toggle_all_messages = function () {
};
function should_render_subscribers() {
return domain !== 'mit.edu';
return page_params.domain !== 'mit.edu';
}
function should_list_all_streams() {
return domain !== 'mit.edu';
return page_params.domain !== 'mit.edu';
}
function update_table_stream_color(table, stream_name, color) {
@ -299,7 +299,7 @@ function mark_subscribed(stream_name, attrs) {
var settings = settings_for_sub(sub);
if (sub.render_subscribers && settings.hasClass('in')) {
var members = settings.find(".subscriber_list_container ul");
add_to_member_list(members, fullname, email);
add_to_member_list(members, page_params.fullname, page_params.email);
}
// Display the swatch and subscription settings
@ -525,12 +525,12 @@ function ajaxSubscribe(stream) {
var res = $.parseJSON(xhr.responseText);
if (!$.isEmptyObject(res.already_subscribed)) {
// Display the canonical stream capitalization.
true_stream_name = res.already_subscribed[email][0];
true_stream_name = res.already_subscribed[page_params.email][0];
ui.report_success("Already subscribed to " + true_stream_name,
$("#subscriptions-status"));
} else {
// Display the canonical stream capitalization.
true_stream_name = res.subscribed[email][0];
true_stream_name = res.subscribed[page_params.email][0];
}
mark_subscribed(true_stream_name);
},
@ -592,7 +592,7 @@ function ajaxSubscribeForCreation(stream, principals, invite_only) {
exports.tutorial_subscribe_or_add_me_to = function (stream_name) {
var stream_status = compose.check_stream_existence(stream_name);
if (stream_status === 'does-not-exist') {
ajaxSubscribeForCreation(stream_name, [email], false);
ajaxSubscribeForCreation(stream_name, [page_params.email], false);
} else {
ajaxSubscribe(stream_name);
}
@ -615,9 +615,9 @@ function people_cmp(person1, person2) {
function show_new_stream_modal() {
var people_minus_you_and_maybe_humbuggers = [];
$.each(people_list, function (idx, person) {
if (person.email !== email &&
(domain === "humbughq.com" ||
$.each(page_params.people_list, function (idx, person) {
if (person.email !== page_params.email &&
(page_params.domain === "humbughq.com" ||
person.email.split('@')[1] !== "humbughq.com"
)
) {
@ -635,8 +635,8 @@ function show_new_stream_modal() {
$(function () {
var i;
// Populate stream_info with data handed over to client-side template.
for (i = 0; i < stream_list.length; i++) {
create_sub(stream_list[i].name, stream_list[i]);
for (i = 0; i < page_params.stream_list.length; i++) {
create_sub(page_params.stream_list[i].name, page_params.stream_list[i]);
}
$("#add_new_subscription").on("submit", function (e) {
@ -665,7 +665,7 @@ $(function () {
principals.push($(this).val());
});
// You are always subscribed to streams you create.
principals.push(email);
principals.push(page_params.email);
ajaxSubscribeForCreation(stream,
principals,
$('#stream_creation_form input[name=privacy]:checked').val() === "invite-only"
@ -723,7 +723,7 @@ $(function () {
if (data.subscribed.hasOwnProperty(principal)) {
error_elem.addClass("hide");
warning_elem.addClass("hide");
if (principal === email) {
if (principal === page_params.email) {
// mark_subscribed adds the user to the member list
mark_subscribed(stream);
} else {

View File

@ -120,12 +120,12 @@ function wait_for_message(time_to_wait_sec, condition) {
var script = [];
function make_script() {
my_tutorial_stream = 'tutorial-' + email.split('@')[0];
my_tutorial_stream = 'tutorial-' + page_params.email.split('@')[0];
my_tutorial_stream = my_tutorial_stream.substring(0, 30);
// Try to guess at one of your main streams.
// This is problematic because it might end up being 'commits' or something.
var main_stream_name = domain.split('.')[0];
var main_stream_name = page_params.domain.split('.')[0];
var my_streams = subs.subscribed_streams();
if (my_streams.length <= 2) {
@ -145,7 +145,7 @@ function make_script() {
script = [
go(sleep, 1000), // The first message seems to sometimes get eaten in Chrome otherwise.
go2(stream_message, "tutorial", "Hello, " + fullname + "!"),
go2(stream_message, "tutorial", "Hello, " + page_params.fullname + "!"),
go(sleep, 2000),
go2(stream_message, "tutorial", "Welcome to Humbug!"),
go(sleep, 2000),
@ -291,7 +291,7 @@ exports.is_running = function () {
exports.initialize = function () {
make_script();
// Global variable populated by the server code
if (needs_tutorial) {
if (page_params.needs_tutorial) {
exports.start();
}
};

View File

@ -74,7 +74,7 @@ exports.update_all_recipients = function (recipients) {
exports.update_your_recipients = function (recipients) {
$.each(recipients, function (idx, recipient_data) {
if (recipient_data.email !== email) {
if (recipient_data.email !== page_params.email) {
add_to_known_recipients(recipient_data, true);
}
});

View File

@ -262,7 +262,7 @@ $(function () {
$("img.gravatar-profile").bind('load', resizehandler);
// We don't have a stream list at MIT.
if (domain === "mit.edu") {
if (page_params.domain === "mit.edu") {
$("#stream_filters").remove();
$("#stream_filters_sep").remove();
}
@ -727,7 +727,7 @@ $(function () {
update_gravatars();
if (result.enable_desktop_notifications !== undefined) {
desktop_notifications_enabled = result.enable_desktop_notifications;
page_params.desktop_notifications_enabled = result.enable_desktop_notifications;
}
settings_status.removeClass(status_classes)
@ -789,7 +789,7 @@ $(function () {
resizehandler();
hack_for_floating_recipient_bar();
typeahead_helper.update_all_recipients(people_list);
typeahead_helper.update_all_recipients(page_params.people_list);
composebox_typeahead.initialize();
search.initialize();
notifications.initialize();
@ -984,7 +984,7 @@ exports.add_narrow_filter = function(name, type, uri) {
* This will not be as much of an issue once we do prioritization of streams
* in the list.
*/
if (domain === "mit.edu" && type === "stream") {
if (page_params.domain === "mit.edu" && type === "stream") {
return false;
}
@ -1057,8 +1057,8 @@ exports.set_presence_list = function (users, presence_info) {
$('#user_presences').append(entry);
}
if (domain !== "mit.edu") {
add_entry(fullname, email, 'active');
if (page_params.domain !== "mit.edu") {
add_entry(page_params.fullname, page_params.email, 'active');
}
$.each(users, function (idx, email) {

View File

@ -34,12 +34,12 @@ var server_furthest_read = -1;
var pointer_update_in_flight = false;
function add_person(person) {
people_list.push(person);
page_params.people_list.push(person);
people_dict[person.email] = person;
}
$(function () {
$.each(people_list, function (idx, person) {
$.each(page_params.people_list, function (idx, person) {
people_dict[person.email] = person;
});
// The special account feedback@humbughq.com is used for in-app
@ -113,7 +113,7 @@ function get_private_message_recipient(message, attr) {
var recipient, i;
var other_recipients = $.grep(message.display_recipient,
function (element, index) {
return element.email !== email;
return element.email !== page_params.email;
});
if (other_recipients.length === 0) {
// private message with oneself
@ -214,7 +214,7 @@ function message_unread(message) {
var sent_by_human = ['website', 'iphone', 'android']
.indexOf(message.client.toLowerCase()) !== -1;
if (message.sender_email === email && sent_by_human) {
if (message.sender_email === page_params.email && sent_by_human) {
return false;
}
@ -389,8 +389,8 @@ function send_pointer_update() {
}
$(function () {
furthest_read = initial_pointer;
server_furthest_read = initial_pointer;
furthest_read = page_params.initial_pointer;
server_furthest_read = page_params.initial_pointer;
// We only send pointer updates when the user has been idle for a
// short while to avoid hammering the server
@ -449,7 +449,7 @@ function add_message_metadata(message, dummy) {
involved_people = message.display_recipient;
if (message.sender_email === email) {
if (message.sender_email === page_params.email) {
typeahead_helper.update_your_recipients(involved_people);
} else {
typeahead_helper.update_all_recipients(involved_people);
@ -596,7 +596,7 @@ function get_updates(options) {
url: '/json/get_updates',
data: get_updates_params,
dataType: 'json',
timeout: poll_timeout,
timeout: page_params.poll_timeout,
success: function (data) {
if (! data) {
// The server occasionally returns no data during a
@ -769,7 +769,8 @@ $(function () {
// We fall back to the closest selected id, as the user may have removed
// a stream from the home before already
if (home_msg_list.selected_id() === -1) {
home_msg_list.select_id(initial_pointer, {then_scroll: true, use_closest: true});
home_msg_list.select_id(page_params.initial_pointer,
{then_scroll: true, use_closest: true});
}
// catch the user up
@ -801,9 +802,9 @@ $(function () {
}});
}
if (have_initial_messages) {
if (page_params.have_initial_messages) {
load_old_messages({
anchor: initial_pointer,
anchor: page_params.initial_pointer,
num_before: 200,
num_after: 200,
msg_list: home_msg_list,
@ -839,7 +840,7 @@ function load_more_messages(msg_list) {
ui.show_loading_more_messages_indicator();
load_more_enabled = false;
if (msg_list.first() === undefined) {
oldest_message_id = initial_pointer;
oldest_message_id = page_params.initial_pointer;
} else {
oldest_message_id = msg_list.first().id;
}
@ -941,7 +942,7 @@ function fast_forward_pointer(btn) {
$.ajax({
type: 'POST',
url: '/json/get_profile',
data: {email: email},
data: {email: page_params.email},
dataType: 'json',
success: function (data) {
mark_all_as_read(function () {