2012-10-18 20:29:16 +02:00
|
|
|
var compose = (function () {
|
2012-10-03 22:53:15 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
var exports = {};
|
2012-10-19 19:51:19 +02:00
|
|
|
var is_composing_message = false;
|
2012-10-18 20:29:16 +02:00
|
|
|
|
2012-10-18 20:16:56 +02:00
|
|
|
exports.start = function (msg_type, opts) {
|
|
|
|
opts = $.extend({ 'message_type': msg_type,
|
|
|
|
'stream': '',
|
|
|
|
'subject': '',
|
|
|
|
'huddle_recipient': '',
|
|
|
|
'message': '' },
|
|
|
|
opts);
|
|
|
|
|
|
|
|
$("#stream").val(opts.stream);
|
|
|
|
$("#subject").val(opts.subject);
|
|
|
|
$("#huddle_recipient").val(opts.huddle_recipient);
|
|
|
|
$("#new_message_content").val(opts.message);
|
|
|
|
|
|
|
|
$('#sidebar a[href="#home"]').tab('show');
|
|
|
|
|
2012-10-19 22:09:13 +02:00
|
|
|
if (msg_type !== 'stream') {
|
|
|
|
// TODO: Just to make sure that compose.composing() output is
|
|
|
|
// consistent. We really should just standardize our
|
|
|
|
// terminology
|
2012-10-19 19:51:19 +02:00
|
|
|
msg_type = "huddle";
|
2012-10-19 22:09:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var focus_area;
|
|
|
|
if (opts.stream && ! opts.subject) {
|
|
|
|
focus_area = 'subject';
|
|
|
|
} else if (opts.stream || opts.huddle_recipient) {
|
|
|
|
focus_area = 'new_message_content';
|
|
|
|
}
|
2012-10-19 19:51:19 +02:00
|
|
|
|
2012-10-19 22:09:13 +02:00
|
|
|
if (msg_type === 'stream') {
|
2012-10-19 22:17:29 +02:00
|
|
|
exports.show('stream', $("#" + (focus_area || 'stream')));
|
2012-10-19 22:09:13 +02:00
|
|
|
} else {
|
2012-10-19 23:04:28 +02:00
|
|
|
exports.show('personal', $("#" + (focus_area || 'huddle_recipient')));
|
2012-10-18 20:16:56 +02:00
|
|
|
}
|
2012-10-19 22:09:13 +02:00
|
|
|
|
2012-10-18 20:16:56 +02:00
|
|
|
hotkeys.set_compose();
|
2012-10-19 19:51:19 +02:00
|
|
|
is_composing_message = msg_type;
|
2012-10-18 20:25:36 +02:00
|
|
|
$(document).trigger($.Event('compose_started.zephyr', opts));
|
2012-10-18 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.cancel = function () {
|
|
|
|
compose.hide();
|
2012-10-19 19:51:19 +02:00
|
|
|
is_composing_message = false;
|
2012-10-18 20:25:36 +02:00
|
|
|
$(document).trigger($.Event('compose_canceled.zephyr'));
|
2012-10-18 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
2012-10-29 18:06:53 +01:00
|
|
|
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.
|
|
|
|
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
|
2012-10-29 21:02:46 +01:00
|
|
|
reload.initiate({immediate: true, send_after_reload: true});
|
2012-10-29 18:06:53 +01:00
|
|
|
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('×')
|
|
|
|
.click(function () { send_status.stop(true).fadeOut(500); }))
|
|
|
|
.stop(true).fadeTo(0,1);
|
|
|
|
|
|
|
|
buttons.removeAttr('disabled');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
send_status.hide();
|
|
|
|
});
|
|
|
|
|
2012-10-18 20:16:56 +02:00
|
|
|
exports.finish = function () {
|
2012-10-29 22:37:34 +01:00
|
|
|
if (! compose.validate()) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-29 18:06:53 +01:00
|
|
|
$("#compose form").ajaxSubmit(send_options);
|
2012-10-19 19:51:19 +02:00
|
|
|
is_composing_message = false;
|
2012-10-18 20:25:36 +02:00
|
|
|
$(document).trigger($.Event('compose_finished.zephyr'));
|
2012-10-29 22:37:34 +01:00
|
|
|
return true;
|
2012-10-18 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
2012-10-29 18:06:53 +01:00
|
|
|
$(function () {
|
|
|
|
$("#compose form").on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
compose.finish();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.show = function (tabname, focus_area) {
|
2012-10-29 21:02:46 +01:00
|
|
|
if (reload.is_in_progress()) {
|
2012-10-17 20:43:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-10-10 22:57:02 +02:00
|
|
|
$("#send-status").removeClass(status_classes).hide();
|
2012-10-09 23:56:16 +02:00
|
|
|
$('#compose').css({visibility: "visible"});
|
2012-10-24 04:07:51 +02:00
|
|
|
$("#new_message_content").trigger("autosize");
|
2012-10-10 00:06:04 +02:00
|
|
|
$('.message_comp').slideDown(100);
|
2012-10-09 23:46:41 +02:00
|
|
|
$('#message-type-tabs a[href="#' + tabname + '-message"]').tab('show');
|
2012-10-04 00:04:58 +02:00
|
|
|
focus_area.focus();
|
|
|
|
focus_area.select();
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-04 00:04:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide = function () {
|
2012-10-04 00:04:58 +02:00
|
|
|
$('input, textarea, button').blur();
|
2012-10-10 00:06:04 +02:00
|
|
|
$('.message_comp').slideUp(100,
|
2012-10-09 23:56:16 +02:00
|
|
|
function() { $('#compose').css({visibility: "hidden"});});
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-04 00:04:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.clear = function () {
|
2012-10-09 23:56:16 +02:00
|
|
|
$("#compose").find('input[type=text], textarea').val('');
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 21:44:07 +02:00
|
|
|
|
2012-10-20 16:14:47 +02:00
|
|
|
exports.set_message_type = function (tabname) {
|
|
|
|
is_composing_message = tabname;
|
|
|
|
$("#send-status").removeClass(status_classes).hide();
|
|
|
|
if (tabname === "stream") {
|
|
|
|
$('#personal-message').hide();
|
|
|
|
$('#stream-message').show();
|
|
|
|
$('#new_message_type').val('stream');
|
|
|
|
$("#stream").focus();
|
|
|
|
} else {
|
|
|
|
$('#personal-message').show();
|
|
|
|
$('#stream-message').hide();
|
|
|
|
$('#new_message_type').val('personal');
|
|
|
|
$("#huddle_recipient").focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.toggle_mode = function () {
|
2012-10-10 23:29:02 +02:00
|
|
|
if ($("#message-type-tabs li.active").find("a[href=#stream-message]").length !== 0) {
|
2012-10-10 23:37:14 +02:00
|
|
|
// In stream tab, switch to personals.
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.show('personal', $("#huddle_recipient"));
|
2012-10-03 20:49:58 +02:00
|
|
|
} else {
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.show('stream', $("#stream"));
|
2012-10-03 20:49:58 +02:00
|
|
|
}
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.composing = function () {
|
2012-10-19 19:51:19 +02:00
|
|
|
return is_composing_message;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-11 20:30:34 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.stream_name = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#stream").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#stream").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.subject = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#subject").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#subject").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-19 19:56:35 +02:00
|
|
|
exports.message_content = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#new_message_content").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#new_message_content").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.recipient = function (newval) {
|
2012-10-16 21:12:52 +02:00
|
|
|
var oldval = $.trim($("#huddle_recipient").val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
$("#huddle_recipient").val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
2012-10-03 20:49:58 +02:00
|
|
|
|
|
|
|
function compose_error(error_text, bad_input) {
|
|
|
|
$('#send-status').removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.text(error_text)
|
|
|
|
.stop(true).fadeTo(0, 1);
|
2012-10-09 23:56:16 +02:00
|
|
|
$('#compose').find('input[type="submit"]').removeAttr('disabled');
|
2012-10-03 20:49:58 +02:00
|
|
|
bad_input.focus().select();
|
|
|
|
}
|
2012-10-03 22:39:05 +02:00
|
|
|
|
2012-10-03 23:04:36 +02:00
|
|
|
function submit_buttons() {
|
2012-10-09 23:56:16 +02:00
|
|
|
return $('#compose').find('input[type="submit"]');
|
2012-10-03 23:04:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
// *Synchronously* check if a stream exists.
|
2012-10-03 22:53:15 +02:00
|
|
|
// If not, displays an error and returns false.
|
2012-10-10 23:37:14 +02:00
|
|
|
function check_stream_for_send(stream_name) {
|
2012-10-03 22:53:15 +02:00
|
|
|
var okay = true;
|
|
|
|
$.ajax({
|
2012-10-16 22:10:48 +02:00
|
|
|
type: "POST",
|
2012-10-17 20:05:17 +02:00
|
|
|
url: "/json/subscriptions/exists",
|
|
|
|
data: {'stream': stream_name},
|
2012-10-03 22:53:15 +02:00
|
|
|
async: false,
|
|
|
|
success: function (data) {
|
2012-10-18 19:40:53 +02:00
|
|
|
if (!data.exists) {
|
2012-10-10 23:37:14 +02:00
|
|
|
// The stream doesn't exist
|
2012-10-03 22:53:15 +02:00
|
|
|
okay = false;
|
2012-10-04 00:04:58 +02:00
|
|
|
$('#send-status').removeClass(status_classes).show();
|
2012-10-10 23:33:38 +02:00
|
|
|
$('#stream-dne-name').text(stream_name);
|
|
|
|
$('#stream-dne').show();
|
2012-10-03 23:04:36 +02:00
|
|
|
submit_buttons().removeAttr('disabled');
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide();
|
2012-10-09 20:06:56 +02:00
|
|
|
$('#create-it').focus();
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
$("#home-error").hide();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
okay = false;
|
|
|
|
report_error("Error checking subscription", xhr, $("#home-error"));
|
2012-10-10 23:33:38 +02:00
|
|
|
$("#stream").focus();
|
2012-10-03 23:04:36 +02:00
|
|
|
submit_buttons().removeAttr('disabled');
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return okay;
|
|
|
|
}
|
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
function validate_stream_message() {
|
2012-10-18 20:29:16 +02:00
|
|
|
var stream_name = exports.stream_name();
|
2012-10-10 23:28:31 +02:00
|
|
|
if (stream_name === "") {
|
2012-10-10 23:37:14 +02:00
|
|
|
compose_error("Please specify a stream", $("#stream"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.subject() === "") {
|
2012-10-30 21:47:43 +01:00
|
|
|
compose_error("Please specify a subject", $("#subject"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 19:56:35 +02:00
|
|
|
if (exports.message_content() === "") {
|
2012-10-09 23:43:53 +02:00
|
|
|
compose_error("You have nothing to send!", $("#new_message_content"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-18 21:37:07 +02:00
|
|
|
if (!subs.have(stream_name)) {
|
2012-10-18 19:26:39 +02:00
|
|
|
if (!check_stream_for_send(stream_name)) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-10-10 23:37:14 +02:00
|
|
|
// You're not subbed to the stream
|
2012-10-03 23:13:38 +02:00
|
|
|
$('#send-status').removeClass(status_classes).show();
|
2012-10-10 23:33:38 +02:00
|
|
|
$('#stream-nosub-name').text(stream_name);
|
|
|
|
$('#stream-nosub').show();
|
2012-10-03 23:13:38 +02:00
|
|
|
submit_buttons().removeAttr('disabled');
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide();
|
2012-10-09 20:06:56 +02:00
|
|
|
$('#sub-it').focus();
|
2012-10-03 23:13:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:39:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate_huddle_message() {
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.recipient() === "") {
|
2012-10-09 15:36:24 +02:00
|
|
|
compose_error("Please specify at least one recipient", $("#huddle_recipient"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-19 19:56:35 +02:00
|
|
|
if (exports.message_content() === "") {
|
2012-10-09 23:43:53 +02:00
|
|
|
compose_error("You have nothing to send!", $("#new_message_content"));
|
2012-10-03 22:39:05 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-03 22:39:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.validate = function () {
|
2012-10-03 23:13:38 +02:00
|
|
|
submit_buttons().attr('disabled', 'disabled').blur();
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.composing() === 'huddle') {
|
2012-10-03 23:13:38 +02:00
|
|
|
return validate_huddle_message();
|
|
|
|
} else {
|
2012-10-10 23:37:14 +02:00
|
|
|
return validate_stream_message();
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
2012-10-18 20:29:16 +02:00
|
|
|
};
|
|
|
|
|
2012-10-24 04:02:39 +02:00
|
|
|
$(function () {
|
|
|
|
$("#new_message_content").autosize();
|
|
|
|
});
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|