Module pattern for compose.js

(imported from commit 8ea9d949b793285e8722bebcef30991d06e5673e)
This commit is contained in:
Keegan McAllister
2012-10-18 14:29:16 -04:00
parent c4bf81a312
commit e7661f7b2f
8 changed files with 85 additions and 81 deletions

View File

@@ -51,7 +51,7 @@
<div class="row">
<div class="span9 compose" id="compose">
<div class="close composebox-close" onclick="hide_compose()">&times;</div>
<div class="close composebox-close" onclick="compose.hide()">&times;</div>
<div class="message_comp">
<div class="alert" id="send-status"></div>
<form action="/json/send_message/" method="post" class="zephyr">

View File

@@ -109,10 +109,10 @@ var people_list = [
</a>
</li>
{% endif %}
<button type="button" class="visible-phone btn btn-small phone_new_message_button" onclick="compose_button('stream');">New Message</button>
<button type="button" class="visible-phone btn btn-small phone_new_message_button" onclick="compose.button_press('stream');">New Message</button>
</ul>
<div class="hidden-phone new_message_button">
<button type="button" class="btn btn-large btn-block" id="new_message_button" onclick="compose_button('stream');">New Message</button>
<button type="button" class="btn btn-large btn-block" id="new_message_button" onclick="compose.button_press('stream');">New Message</button>
</div>
<div class="alert alert_sidebar alert-error home-error-bar" id="connection-error">

View File

@@ -8,11 +8,11 @@ var globals =
// index.html
+ ' initial_pointer email stream_list people_list have_initial_messages'
// common.js
+ ' status_classes'
// compose.js
+ ' show_compose hide_compose toggle_compose clear_compose_box compose_button'
+ ' composing_message composing_stream_message compose_stream_name'
+ ' compose_stream_name compose_subject compose_recipient compose_message'
+ ' validate_message status_classes'
+ ' compose'
// dom_access.js
+ ' get_first_visible get_last_visible get_next_visible get_prev_visible'
@@ -38,7 +38,7 @@ var globals =
// zephyr.js
+ ' message_array message_dict selected_message_class'
+ ' status_classes clear_table add_to_table subject_dict'
+ ' clear_table add_to_table subject_dict'
+ ' keep_pointer_in_view move_pointer_at_page_top_and_bottom'
+ ' respond_to_message'
+ ' select_message select_message_by_id'

View File

@@ -1,3 +1,5 @@
var status_classes = 'alert-error alert-success alert-info';
function autofocus(selector) {
$(function () {
$(selector)[0].focus();

View File

@@ -1,6 +1,8 @@
var status_classes = 'alert-error alert-success alert-info';
var compose = (function () {
function show_compose(tabname, focus_area) {
var exports = {};
exports.show = function (tabname, focus_area) {
if (reloading_app) {
return;
}
@@ -10,81 +12,77 @@ function show_compose(tabname, focus_area) {
$('#message-type-tabs a[href="#' + tabname + '-message"]').tab('show');
focus_area.focus();
focus_area.select();
}
};
function hide_compose() {
exports.hide = function () {
$('input, textarea, button').blur();
$('.message_comp').slideUp(100,
function() { $('#compose').css({visibility: "hidden"});});
}
};
function clear_compose_box() {
exports.clear = function () {
$("#compose").find('input[type=text], textarea').val('');
}
};
function compose_button(tabname) {
clear_compose_box();
exports.button_press = function (tabname) {
exports.clear();
$('#sidebar a[href="#home"]').tab('show');
show_compose(tabname, $("#" + tabname));
exports.show(tabname, $("#" + tabname));
hotkeys.set_compose();
}
};
function toggle_compose() {
exports.toggle_mode = function () {
if ($("#message-type-tabs li.active").find("a[href=#stream-message]").length !== 0) {
// In stream tab, switch to personals.
show_compose('personal', $("#huddle_recipient"));
exports.show('personal', $("#huddle_recipient"));
} else {
show_compose('stream', $("#stream"));
exports.show('stream', $("#stream"));
}
}
};
function composing_stream_message() {
return $("#stream-message").is(":visible");
}
exports.composing = function () {
if ($("#stream-message").is(":visible")) {
return 'stream';
}
function composing_huddle_message() {
return $("#personal-message").is(":visible");
}
if ($("#personal-message").is(":visible")) {
return 'huddle';
}
function composing_message() {
return composing_stream_message() || composing_huddle_message();
}
return false;
};
function compose_stream_name(newval) {
exports.stream_name = function (newval) {
var oldval = $.trim($("#stream").val());
if (newval !== undefined) {
$("#stream").val(newval);
}
return oldval;
}
};
function compose_subject(newval) {
exports.subject = function (newval) {
var oldval = $.trim($("#subject").val());
if (newval !== undefined) {
$("#subject").val(newval);
}
return oldval;
}
};
function compose_message(newval) {
exports.message = function (newval) {
var oldval = $.trim($("#new_message_content").val());
if (newval !== undefined) {
$("#new_message_content").val(newval);
}
return oldval;
}
};
function compose_recipient(newval) {
exports.recipient = function (newval) {
var oldval = $.trim($("#huddle_recipient").val());
if (newval !== undefined) {
$("#huddle_recipient").val(newval);
}
return oldval;
}
function compose_huddle_message(newval) {
return compose_message(newval);
}
};
function compose_error(error_text, bad_input) {
$('#send-status').removeClass(status_classes)
@@ -116,7 +114,7 @@ function check_stream_for_send(stream_name) {
$('#stream-dne-name').text(stream_name);
$('#stream-dne').show();
submit_buttons().removeAttr('disabled');
hide_compose();
exports.hide();
$('#create-it').focus();
}
$("#home-error").hide();
@@ -132,18 +130,18 @@ function check_stream_for_send(stream_name) {
}
function validate_stream_message() {
var stream_name = compose_stream_name();
var stream_name = exports.stream_name();
if (stream_name === "") {
compose_error("Please specify a stream", $("#stream"));
return false;
}
if (compose_subject() === "") {
if (exports.subject() === "") {
compose_error("Please specify an subject", $("#subject"));
return false;
}
if (compose_message() === "") {
if (exports.message() === "") {
compose_error("You have nothing to send!", $("#new_message_content"));
return false;
}
@@ -157,7 +155,7 @@ function validate_stream_message() {
$('#stream-nosub-name').text(stream_name);
$('#stream-nosub').show();
submit_buttons().removeAttr('disabled');
hide_compose();
exports.hide();
$('#sub-it').focus();
return false;
}
@@ -166,12 +164,12 @@ function validate_stream_message() {
}
function validate_huddle_message() {
if (compose_recipient() === "") {
if (exports.recipient() === "") {
compose_error("Please specify at least one recipient", $("#huddle_recipient"));
return false;
}
if (compose_huddle_message() === "") {
if (exports.message() === "") {
compose_error("You have nothing to send!", $("#new_message_content"));
return false;
}
@@ -179,12 +177,16 @@ function validate_huddle_message() {
return true;
}
function validate_message() {
exports.validate = function () {
submit_buttons().attr('disabled', 'disabled').blur();
if (composing_huddle_message()) {
if (exports.composing() === 'huddle') {
return validate_huddle_message();
} else {
return validate_stream_message();
}
}
};
return exports;
}());

View File

@@ -80,17 +80,17 @@ function process_hotkey(code) {
}
return false;
case 27: // Esc: hide compose pane or un-narrow
if (composing_message()) {
hide_compose();
if (compose.composing()) {
compose.hide();
} else {
narrow.show_all_messages();
}
return process_hotkey;
case 99: // 'c': compose
compose_button('stream');
compose.button_press('stream');
return process_compose_hotkey;
case 67: // 'C': compose huddle
compose_button('personal');
compose.button_press('personal');
return process_compose_hotkey;
case 114: // 'r': respond to message
respond_to_message();
@@ -115,7 +115,7 @@ var goto_hotkeys = {
105: narrow.by_subject, // 'i'
112: narrow.all_personals, // 'p'
97: narrow.show_all_messages, // 'a'
27: hide_compose // Esc
27: compose.hide // Esc
};
process_goto_hotkey = function (code) {
@@ -132,7 +132,7 @@ process_goto_hotkey = function (code) {
process_key_in_input = function (code) {
if (code === 27) {
// If the user hit the escape key, hide the compose window
hide_compose();
compose.hide();
}
// Otherwise, let the browser handle the key normally
return false;
@@ -140,7 +140,7 @@ process_key_in_input = function (code) {
process_compose_hotkey = function (code) {
if (code === 9) { // Tab: toggles between stream and huddle compose tabs.
toggle_compose();
compose.toggle_mode();
return process_compose_hotkey;
}
// Process the first non-tab character and everything after it

View File

@@ -255,12 +255,12 @@ $(function () {
// Prepare the click handler for subbing to a new stream to which
// you have composed a message.
$('#create-it').click(function () {
sub_from_home(compose_stream_name(), $('#stream-dne'));
sub_from_home(compose.stream_name(), $('#stream-dne'));
});
// Prepare the click handler for subbing to an existing stream.
$('#sub-it').click(function () {
sub_from_home(compose_stream_name(), $('#stream-nosub'));
sub_from_home(compose.stream_name(), $('#stream-nosub'));
});
var throttled_scrollhandler = $.throttle(50, function(e) {
@@ -316,7 +316,7 @@ $(function () {
});
$('.button-slide').click(function () {
show_compose('stream', $("#stream"));
compose.show('stream', $("#stream"));
});
$('#sidebar a[href="#subscriptions"]').click(fetch_subs);

View File

@@ -24,11 +24,11 @@ $(function () {
var options = {
dataType: 'json', // This seems to be ignored. We still get back an xhr.
beforeSubmit: validate_message,
beforeSubmit: compose.validate,
success: function (resp, statusText, xhr, form) {
form.find('textarea').val('');
send_status.hide();
hide_compose();
compose.hide();
buttons.removeAttr('disabled');
if (get_updates_params.reload_pending) {
reload_app();
@@ -166,7 +166,7 @@ function get_huddle_recipient_names(message) {
function respond_to_message(reply_type) {
var message, tabname;
message = message_dict[selected_message_id];
clear_compose_box();
compose.clear();
if (message.type === "stream") {
$("#stream").val(message.display_recipient);
$("#subject").val(message.subject);
@@ -189,7 +189,7 @@ function respond_to_message(reply_type) {
// Huddle messages use the personals compose box
tabname = "personal";
}
show_compose(tabname, $("#new_message_content"));
compose.show(tabname, $("#new_message_content"));
}
// Called by mouseover etc.
@@ -527,7 +527,7 @@ function do_reload_app() {
function reload_app() {
// If we can, reload the page immediately
if (! composing_message()) {
if (! compose.composing()) {
do_reload_app();
}
@@ -539,15 +539,15 @@ function reload_app() {
function reload_app_preserving_compose(send_after_reload) {
var url = "#reload:send_after_reload=" + Number(send_after_reload);
if (composing_stream_message()) {
if (compose.composing() === 'stream') {
url += "+msg_type=stream";
url += "+stream=" + encodeURIComponent(compose_stream_name());
url += "+subject=" + encodeURIComponent(compose_subject());
url += "+stream=" + encodeURIComponent(compose.stream_name());
url += "+subject=" + encodeURIComponent(compose.subject());
} else {
url += "+msg_type=huddle";
url += "+recipient=" + encodeURIComponent(compose_recipient());
url += "+recipient=" + encodeURIComponent(compose.recipient());
}
url += "+msg="+ encodeURIComponent(compose_message());
url += "+msg="+ encodeURIComponent(compose.message());
window.location.replace(url);
do_reload_app();
@@ -575,18 +575,18 @@ $(function () {
var send_now = parseInt(vars.send_after_reload, 10);
if (vars.msg_type === "stream") {
if (! send_now) {
show_compose("stream", $("#new_message_content"));
compose.show("stream", $("#new_message_content"));
}
compose_stream_name(vars.stream);
compose_subject(vars.subject);
compose.stream_name(vars.stream);
compose.subject(vars.subject);
} else {
if (! send_now) {
show_compose("huddle", $("#new_message_content"));
compose.show("huddle", $("#new_message_content"));
}
show_compose("huddle", $("#new_message_content"));
compose_recipient(vars.recipient);
compose.show("huddle", $("#new_message_content"));
compose.recipient(vars.recipient);
}
compose_message(vars.msg);
compose.message(vars.msg);
if (send_now) {
$("#compose form").ajaxSubmit();