Use compose.finish() instead of manually calling ajaxSubmit() in multiple places

(imported from commit 3bbe4a03171d9eace413b576ae2c7ccf99141bf1)
This commit is contained in:
Zev Benjamin 2012-10-29 13:06:53 -04:00
parent 11cd128725
commit cee3f87585
4 changed files with 53 additions and 44 deletions

View File

@ -38,7 +38,7 @@ var globals =
+ ' update_autocomplete autocomplete_needs_update'
// zephyr.js
+ ' message_array message_dict'
+ ' message_array message_dict get_updates_params'
+ ' clear_table add_to_table subject_dict'
+ ' keep_pointer_in_view move_pointer_at_page_top_and_bottom'
+ ' respond_to_message'

View File

@ -49,12 +49,61 @@ exports.cancel = function () {
$(document).trigger($.Event('compose_canceled.zephyr'));
};
var send_options;
$(function () {
var send_status = $('#send-status');
var buttons = $('#compose').find('input[type="submit"]');
send_options = {
dataType: 'json', // This seems to be ignored. We still get back an xhr.
beforeSubmit: function () { compose.validate(); },
success: function (resp, statusText, xhr, form) {
form.find('textarea').val('');
send_status.hide();
compose.hide();
buttons.removeAttr('disabled');
},
error: function (xhr, error_type) {
if (error_type !== 'timeout' && get_updates_params.reload_pending) {
// The error might be due to the server changing
do_reload_app_preserving_compose(true);
return;
}
var response = "Error sending message";
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
send_status.removeClass(status_classes)
.addClass('alert-error')
.text(response)
.append($('<span />')
.addClass('send-status-close').html('&times;')
.click(function () { send_status.stop(true).fadeOut(500); }))
.stop(true).fadeTo(0,1);
buttons.removeAttr('disabled');
}
};
send_status.hide();
});
exports.finish = function () {
$("#compose form").ajaxSubmit();
$("#compose form").ajaxSubmit(send_options);
is_composing_message = false;
$(document).trigger($.Event('compose_finished.zephyr'));
};
$(function () {
$("#compose form").on("submit", function (e) {
e.preventDefault();
compose.finish();
});
});
exports.show = function (tabname, focus_area) {
if (reloading_app) {
return;

View File

@ -61,7 +61,7 @@ exports.add_for_send = function (stream, prompt_button) {
timeout: 10*60*1000, // 10 minutes in ms
success: function (response) {
add_to_stream_list(response.data);
$("#compose form").ajaxSubmit();
compose.finish();
prompt_button.stop(true).fadeOut(500);
},
error: function (xhr, error_type, exn) {

View File

@ -17,46 +17,6 @@ var get_updates_params = {
};
$(function () {
var i;
var send_status = $('#send-status');
var buttons = $('#compose').find('input[type="submit"]');
var options = {
dataType: 'json', // This seems to be ignored. We still get back an xhr.
beforeSubmit: compose.validate,
success: function (resp, statusText, xhr, form) {
form.find('textarea').val('');
send_status.hide();
compose.hide();
buttons.removeAttr('disabled');
},
error: function (xhr, error_type) {
if (error_type !== 'timeout' && get_updates_params.reload_pending) {
// The error might be due to the server changing
do_reload_app_preserving_compose(true);
return;
}
var response = "Error sending message";
if (xhr.status.toString().charAt(0) === "4") {
// Only display the error response for 4XX, where we've crafted
// a nice response.
response += ": " + $.parseJSON(xhr.responseText).msg;
}
send_status.removeClass(status_classes)
.addClass('alert-error')
.text(response)
.append($('<span />')
.addClass('send-status-close').html('&times;')
.click(function () { send_status.stop(true).fadeOut(500); }))
.stop(true).fadeTo(0,1);
buttons.removeAttr('disabled');
}
};
send_status.hide();
$("#compose form").ajaxForm(options);
$.each(people_list, function (idx, person) {
people_hash[person.email] = 1;
});
@ -610,7 +570,7 @@ $(function () {
huddle_recipient: vars.recipient,
message: vars.msg});
if (send_now) {
$("#compose form").ajaxSubmit();
compose.finish();
}
});