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) {
|
2012-10-31 22:08:44 +01:00
|
|
|
opts = $.extend({ message_type: msg_type,
|
|
|
|
stream: '',
|
|
|
|
subject: '',
|
2012-11-08 00:38:21 +01:00
|
|
|
private_message_recipient: '',
|
2012-10-31 22:08:44 +01:00
|
|
|
message: ''
|
|
|
|
}, opts);
|
2012-10-18 20:16:56 +02:00
|
|
|
|
|
|
|
$("#stream").val(opts.stream);
|
|
|
|
$("#subject").val(opts.subject);
|
2012-11-08 00:38:21 +01:00
|
|
|
$("#private_message_recipient").val(opts.private_message_recipient);
|
2012-10-18 20:16:56 +02:00
|
|
|
$("#new_message_content").val(opts.message);
|
|
|
|
|
|
|
|
$('#sidebar a[href="#home"]').tab('show');
|
|
|
|
|
2012-10-19 22:09:13 +02:00
|
|
|
var focus_area;
|
|
|
|
if (opts.stream && ! opts.subject) {
|
|
|
|
focus_area = 'subject';
|
2012-11-08 00:38:21 +01:00
|
|
|
} else if (opts.stream || opts.private_message_recipient) {
|
2012-10-19 22:09:13 +02:00
|
|
|
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-11-08 00:38:21 +01:00
|
|
|
exports.show('private', $("#" + (focus_area || 'private_message_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;
|
|
|
|
|
2012-11-07 20:26:24 +01:00
|
|
|
function send_message() {
|
2012-10-29 18:06:53 +01:00
|
|
|
var send_status = $('#send-status');
|
|
|
|
|
2012-11-07 20:26:24 +01:00
|
|
|
// TODO: this should be collapsed with the code in composebox_typeahead.js
|
|
|
|
var recipients = compose.recipient().split(/\s*[,;]\s*/);
|
|
|
|
|
|
|
|
var request = {client: 'website',
|
|
|
|
type: compose.composing(),
|
|
|
|
subject: compose.subject(),
|
|
|
|
content: compose.message_content()};
|
2012-11-14 23:21:46 +01:00
|
|
|
if (request.type === "private") {
|
|
|
|
request.to = JSON.stringify(recipients);
|
|
|
|
} else {
|
|
|
|
request.to = compose.stream_name();
|
|
|
|
}
|
2012-11-07 20:26:24 +01:00
|
|
|
|
|
|
|
$.ajax({
|
2012-10-29 18:06:53 +01:00
|
|
|
dataType: 'json', // This seems to be ignored. We still get back an xhr.
|
2012-11-07 20:26:24 +01:00
|
|
|
url: '/json/send_message',
|
|
|
|
type: 'POST',
|
|
|
|
data: request,
|
|
|
|
success: function (resp, statusText, xhr) {
|
|
|
|
compose.clear();
|
2012-10-29 18:06:53 +01:00
|
|
|
send_status.hide();
|
2012-11-07 19:59:35 +01:00
|
|
|
is_composing_message = false;
|
2012-10-29 18:06:53 +01:00
|
|
|
compose.hide();
|
2012-10-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2012-10-29 18:06:53 +01:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
|
2012-10-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2012-10-29 18:06:53 +01:00
|
|
|
}
|
2012-11-07 20:26:24 +01:00
|
|
|
});
|
2012-10-29 18:06:53 +01:00
|
|
|
|
|
|
|
send_status.hide();
|
2012-11-07 20:26:24 +01:00
|
|
|
}
|
2012-10-29 18:06:53 +01:00
|
|
|
|
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-11-07 20:26:24 +01:00
|
|
|
send_message();
|
2012-11-07 19:59:35 +01:00
|
|
|
// TODO: Do we want to fire the event even if the send failed due
|
|
|
|
// to a server-side error?
|
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-11-05 21:01:49 +01: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") {
|
2012-11-08 00:38:21 +01:00
|
|
|
$('#private-message').hide();
|
2012-10-20 16:14:47 +02:00
|
|
|
$('#stream-message').show();
|
|
|
|
$('#new_message_type').val('stream');
|
|
|
|
$("#stream").focus();
|
|
|
|
} else {
|
2012-11-08 00:38:21 +01:00
|
|
|
$('#private-message').show();
|
2012-10-20 16:14:47 +02:00
|
|
|
$('#stream-message').hide();
|
2012-11-08 00:38:21 +01:00
|
|
|
$('#new_message_type').val('private');
|
|
|
|
$("#private_message_recipient").focus();
|
2012-10-20 16:14:47 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.toggle_mode = function () {
|
2012-11-05 21:01:49 +01:00
|
|
|
if ($("#message-type-tabs li.active").find("a[href=#stream-message]").length !== 0) {
|
2012-11-08 00:38:21 +01:00
|
|
|
// In stream tab, switch to private
|
|
|
|
exports.show('private', $("#private_message_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-31 22:12:45 +01:00
|
|
|
function get_or_set(fieldname) {
|
|
|
|
// We can't hoist the assignment of 'elem' out of this lambda,
|
|
|
|
// because the DOM element might not exist yet when get_or_set
|
|
|
|
// is called.
|
|
|
|
return function (newval) {
|
|
|
|
var elem = $('#'+fieldname);
|
|
|
|
var oldval = $.trim(elem.val());
|
|
|
|
if (newval !== undefined) {
|
|
|
|
elem.val(newval);
|
|
|
|
}
|
|
|
|
return oldval;
|
|
|
|
};
|
|
|
|
}
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-31 22:12:45 +01:00
|
|
|
exports.stream_name = get_or_set('stream');
|
|
|
|
exports.subject = get_or_set('subject');
|
|
|
|
exports.message_content = get_or_set('new_message_content');
|
2012-11-08 00:38:21 +01:00
|
|
|
exports.recipient = get_or_set('private_message_recipient');
|
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-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2012-10-03 20:49:58 +02:00
|
|
|
bad_input.focus().select();
|
|
|
|
}
|
2012-10-03 22:39:05 +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-11-15 19:13:33 +01:00
|
|
|
var result = "error";
|
2012-10-03 22:53:15 +02:00
|
|
|
$.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-11-15 19:13:33 +01:00
|
|
|
result = "does-not-exist";
|
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-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2012-10-18 20:29:16 +02:00
|
|
|
exports.hide();
|
2012-10-09 20:06:56 +02:00
|
|
|
$('#create-it').focus();
|
2012-11-15 19:13:33 +01:00
|
|
|
} else if (data.subscribed) {
|
|
|
|
result = "subscribed";
|
|
|
|
} else {
|
|
|
|
result = "not-subscribed";
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
$("#home-error").hide();
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2012-11-15 19:13:33 +01:00
|
|
|
result = "error";
|
2012-10-03 22:53:15 +02:00
|
|
|
report_error("Error checking subscription", xhr, $("#home-error"));
|
2012-10-10 23:33:38 +02:00
|
|
|
$("#stream").focus();
|
2012-10-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
});
|
2012-11-15 19:13:33 +01:00
|
|
|
return result;
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
|
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-18 21:37:07 +02:00
|
|
|
if (!subs.have(stream_name)) {
|
2012-11-15 19:13:33 +01:00
|
|
|
switch(check_stream_for_send(stream_name)) {
|
|
|
|
case "does-not-exist":
|
|
|
|
case "error":
|
|
|
|
return false;
|
|
|
|
case "subscribed":
|
|
|
|
// You're actually subscribed to the stream, but this
|
|
|
|
// browser window doesn't know it.
|
|
|
|
return true;
|
|
|
|
case "not-subscribed":
|
|
|
|
$('#send-status').removeClass(status_classes).show();
|
|
|
|
$('#stream-nosub-name').text(stream_name);
|
|
|
|
$('#stream-nosub').show();
|
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
|
|
|
exports.hide();
|
|
|
|
$('#sub-it').focus();
|
2012-10-18 19:26:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-03 23:13:38 +02:00
|
|
|
}
|
|
|
|
|
2012-10-03 22:39:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
function validate_private_message() {
|
2012-10-18 20:29:16 +02:00
|
|
|
if (exports.recipient() === "") {
|
2012-11-08 00:38:21 +01:00
|
|
|
compose_error("Please specify at least one recipient", $("#private_message_recipient"));
|
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-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").attr('disabled', 'disabled').blur();
|
2012-10-03 23:13:38 +02:00
|
|
|
|
2012-10-31 22:18:48 +01:00
|
|
|
if (exports.message_content() === "") {
|
|
|
|
compose_error("You have nothing to send!", $("#new_message_content"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-08 00:38:21 +01:00
|
|
|
if (exports.composing() === 'private') {
|
|
|
|
return validate_private_message();
|
2012-10-03 23:13:38 +02:00
|
|
|
} 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;
|
|
|
|
|
|
|
|
}());
|