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="row">
<div class="span9 compose" id="compose"> <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="message_comp">
<div class="alert" id="send-status"></div> <div class="alert" id="send-status"></div>
<form action="/json/send_message/" method="post" class="zephyr"> <form action="/json/send_message/" method="post" class="zephyr">

View File

@@ -109,10 +109,10 @@ var people_list = [
</a> </a>
</li> </li>
{% endif %} {% 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> </ul>
<div class="hidden-phone new_message_button"> <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>
<div class="alert alert_sidebar alert-error home-error-bar" id="connection-error"> <div class="alert alert_sidebar alert-error home-error-bar" id="connection-error">

View File

@@ -8,11 +8,11 @@ var globals =
// index.html // index.html
+ ' initial_pointer email stream_list people_list have_initial_messages' + ' initial_pointer email stream_list people_list have_initial_messages'
// common.js
+ ' status_classes'
// compose.js // compose.js
+ ' show_compose hide_compose toggle_compose clear_compose_box compose_button' + ' compose'
+ ' composing_message composing_stream_message compose_stream_name'
+ ' compose_stream_name compose_subject compose_recipient compose_message'
+ ' validate_message status_classes'
// dom_access.js // dom_access.js
+ ' get_first_visible get_last_visible get_next_visible get_prev_visible' + ' get_first_visible get_last_visible get_next_visible get_prev_visible'
@@ -38,7 +38,7 @@ var globals =
// zephyr.js // zephyr.js
+ ' message_array message_dict selected_message_class' + ' 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' + ' keep_pointer_in_view move_pointer_at_page_top_and_bottom'
+ ' respond_to_message' + ' respond_to_message'
+ ' select_message select_message_by_id' + ' 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 autofocus(selector) {
$(function () { $(function () {
$(selector)[0].focus(); $(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) { if (reloading_app) {
return; return;
} }
@@ -10,81 +12,77 @@ function show_compose(tabname, focus_area) {
$('#message-type-tabs a[href="#' + tabname + '-message"]').tab('show'); $('#message-type-tabs a[href="#' + tabname + '-message"]').tab('show');
focus_area.focus(); focus_area.focus();
focus_area.select(); focus_area.select();
} };
function hide_compose() { exports.hide = function () {
$('input, textarea, button').blur(); $('input, textarea, button').blur();
$('.message_comp').slideUp(100, $('.message_comp').slideUp(100,
function() { $('#compose').css({visibility: "hidden"});}); function() { $('#compose').css({visibility: "hidden"});});
} };
function clear_compose_box() { exports.clear = function () {
$("#compose").find('input[type=text], textarea').val(''); $("#compose").find('input[type=text], textarea').val('');
} };
function compose_button(tabname) { exports.button_press = function (tabname) {
clear_compose_box(); exports.clear();
$('#sidebar a[href="#home"]').tab('show'); $('#sidebar a[href="#home"]').tab('show');
show_compose(tabname, $("#" + tabname)); exports.show(tabname, $("#" + tabname));
hotkeys.set_compose(); hotkeys.set_compose();
} };
function toggle_compose() { exports.toggle_mode = function () {
if ($("#message-type-tabs li.active").find("a[href=#stream-message]").length !== 0) { if ($("#message-type-tabs li.active").find("a[href=#stream-message]").length !== 0) {
// In stream tab, switch to personals. // In stream tab, switch to personals.
show_compose('personal', $("#huddle_recipient")); exports.show('personal', $("#huddle_recipient"));
} else { } else {
show_compose('stream', $("#stream")); exports.show('stream', $("#stream"));
} }
} };
function composing_stream_message() { exports.composing = function () {
return $("#stream-message").is(":visible"); if ($("#stream-message").is(":visible")) {
} return 'stream';
}
function composing_huddle_message() { if ($("#personal-message").is(":visible")) {
return $("#personal-message").is(":visible"); return 'huddle';
} }
function composing_message() { return false;
return composing_stream_message() || composing_huddle_message(); };
}
function compose_stream_name(newval) { exports.stream_name = function (newval) {
var oldval = $.trim($("#stream").val()); var oldval = $.trim($("#stream").val());
if (newval !== undefined) { if (newval !== undefined) {
$("#stream").val(newval); $("#stream").val(newval);
} }
return oldval; return oldval;
} };
function compose_subject(newval) { exports.subject = function (newval) {
var oldval = $.trim($("#subject").val()); var oldval = $.trim($("#subject").val());
if (newval !== undefined) { if (newval !== undefined) {
$("#subject").val(newval); $("#subject").val(newval);
} }
return oldval; return oldval;
} };
function compose_message(newval) { exports.message = function (newval) {
var oldval = $.trim($("#new_message_content").val()); var oldval = $.trim($("#new_message_content").val());
if (newval !== undefined) { if (newval !== undefined) {
$("#new_message_content").val(newval); $("#new_message_content").val(newval);
} }
return oldval; return oldval;
} };
function compose_recipient(newval) { exports.recipient = function (newval) {
var oldval = $.trim($("#huddle_recipient").val()); var oldval = $.trim($("#huddle_recipient").val());
if (newval !== undefined) { if (newval !== undefined) {
$("#huddle_recipient").val(newval); $("#huddle_recipient").val(newval);
} }
return oldval; return oldval;
} };
function compose_huddle_message(newval) {
return compose_message(newval);
}
function compose_error(error_text, bad_input) { function compose_error(error_text, bad_input) {
$('#send-status').removeClass(status_classes) $('#send-status').removeClass(status_classes)
@@ -116,7 +114,7 @@ function check_stream_for_send(stream_name) {
$('#stream-dne-name').text(stream_name); $('#stream-dne-name').text(stream_name);
$('#stream-dne').show(); $('#stream-dne').show();
submit_buttons().removeAttr('disabled'); submit_buttons().removeAttr('disabled');
hide_compose(); exports.hide();
$('#create-it').focus(); $('#create-it').focus();
} }
$("#home-error").hide(); $("#home-error").hide();
@@ -132,18 +130,18 @@ function check_stream_for_send(stream_name) {
} }
function validate_stream_message() { function validate_stream_message() {
var stream_name = compose_stream_name(); var stream_name = exports.stream_name();
if (stream_name === "") { if (stream_name === "") {
compose_error("Please specify a stream", $("#stream")); compose_error("Please specify a stream", $("#stream"));
return false; return false;
} }
if (compose_subject() === "") { if (exports.subject() === "") {
compose_error("Please specify an subject", $("#subject")); compose_error("Please specify an subject", $("#subject"));
return false; return false;
} }
if (compose_message() === "") { if (exports.message() === "") {
compose_error("You have nothing to send!", $("#new_message_content")); compose_error("You have nothing to send!", $("#new_message_content"));
return false; return false;
} }
@@ -157,7 +155,7 @@ function validate_stream_message() {
$('#stream-nosub-name').text(stream_name); $('#stream-nosub-name').text(stream_name);
$('#stream-nosub').show(); $('#stream-nosub').show();
submit_buttons().removeAttr('disabled'); submit_buttons().removeAttr('disabled');
hide_compose(); exports.hide();
$('#sub-it').focus(); $('#sub-it').focus();
return false; return false;
} }
@@ -166,12 +164,12 @@ function validate_stream_message() {
} }
function validate_huddle_message() { function validate_huddle_message() {
if (compose_recipient() === "") { if (exports.recipient() === "") {
compose_error("Please specify at least one recipient", $("#huddle_recipient")); compose_error("Please specify at least one recipient", $("#huddle_recipient"));
return false; return false;
} }
if (compose_huddle_message() === "") { if (exports.message() === "") {
compose_error("You have nothing to send!", $("#new_message_content")); compose_error("You have nothing to send!", $("#new_message_content"));
return false; return false;
} }
@@ -179,12 +177,16 @@ function validate_huddle_message() {
return true; return true;
} }
function validate_message() { exports.validate = function () {
submit_buttons().attr('disabled', 'disabled').blur(); submit_buttons().attr('disabled', 'disabled').blur();
if (composing_huddle_message()) { if (exports.composing() === 'huddle') {
return validate_huddle_message(); return validate_huddle_message();
} else { } else {
return validate_stream_message(); return validate_stream_message();
} }
} };
return exports;
}());

View File

@@ -80,17 +80,17 @@ function process_hotkey(code) {
} }
return false; return false;
case 27: // Esc: hide compose pane or un-narrow case 27: // Esc: hide compose pane or un-narrow
if (composing_message()) { if (compose.composing()) {
hide_compose(); compose.hide();
} else { } else {
narrow.show_all_messages(); narrow.show_all_messages();
} }
return process_hotkey; return process_hotkey;
case 99: // 'c': compose case 99: // 'c': compose
compose_button('stream'); compose.button_press('stream');
return process_compose_hotkey; return process_compose_hotkey;
case 67: // 'C': compose huddle case 67: // 'C': compose huddle
compose_button('personal'); compose.button_press('personal');
return process_compose_hotkey; return process_compose_hotkey;
case 114: // 'r': respond to message case 114: // 'r': respond to message
respond_to_message(); respond_to_message();
@@ -115,7 +115,7 @@ var goto_hotkeys = {
105: narrow.by_subject, // 'i' 105: narrow.by_subject, // 'i'
112: narrow.all_personals, // 'p' 112: narrow.all_personals, // 'p'
97: narrow.show_all_messages, // 'a' 97: narrow.show_all_messages, // 'a'
27: hide_compose // Esc 27: compose.hide // Esc
}; };
process_goto_hotkey = function (code) { process_goto_hotkey = function (code) {
@@ -132,7 +132,7 @@ process_goto_hotkey = function (code) {
process_key_in_input = function (code) { process_key_in_input = function (code) {
if (code === 27) { if (code === 27) {
// If the user hit the escape key, hide the compose window // If the user hit the escape key, hide the compose window
hide_compose(); compose.hide();
} }
// Otherwise, let the browser handle the key normally // Otherwise, let the browser handle the key normally
return false; return false;
@@ -140,7 +140,7 @@ process_key_in_input = function (code) {
process_compose_hotkey = function (code) { process_compose_hotkey = function (code) {
if (code === 9) { // Tab: toggles between stream and huddle compose tabs. if (code === 9) { // Tab: toggles between stream and huddle compose tabs.
toggle_compose(); compose.toggle_mode();
return process_compose_hotkey; return process_compose_hotkey;
} }
// Process the first non-tab character and everything after it // 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 // 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')); sub_from_home(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')); sub_from_home(compose.stream_name(), $('#stream-nosub'));
}); });
var throttled_scrollhandler = $.throttle(50, function(e) { var throttled_scrollhandler = $.throttle(50, function(e) {
@@ -316,7 +316,7 @@ $(function () {
}); });
$('.button-slide').click(function () { $('.button-slide').click(function () {
show_compose('stream', $("#stream")); compose.show('stream', $("#stream"));
}); });
$('#sidebar a[href="#subscriptions"]').click(fetch_subs); $('#sidebar a[href="#subscriptions"]').click(fetch_subs);

View File

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