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-11-26 19:10:05 +01:00
|
|
|
function show(tabname, focus_area) {
|
|
|
|
if (tabname === "stream") {
|
|
|
|
$('#private-message').hide();
|
|
|
|
$('#stream-message').show();
|
|
|
|
$("#stream_toggle").addClass("active");
|
|
|
|
$("#private_message_toggle").removeClass("active");
|
|
|
|
} else {
|
|
|
|
$('#private-message').show();
|
|
|
|
$('#stream-message').hide();
|
|
|
|
$("#stream_toggle").removeClass("active");
|
|
|
|
$("#private_message_toggle").addClass("active");
|
|
|
|
}
|
|
|
|
$("#send-status").removeClass(status_classes).hide();
|
|
|
|
$('#compose').css({visibility: "visible"});
|
|
|
|
$("#new_message_content").trigger("autosize");
|
2012-11-30 03:49:18 +01:00
|
|
|
$('.message_comp').slideDown(100, function () {
|
|
|
|
// If the compose box is obscuring the currently selected message,
|
|
|
|
// scroll up until the message is no longer occluded.
|
2012-12-05 19:27:31 +01:00
|
|
|
if (selected_message_id === -1) {
|
|
|
|
// If there's no selected message, there's no need to
|
|
|
|
// scroll the compose box to avoid it.
|
|
|
|
return;
|
|
|
|
}
|
2012-11-30 03:49:18 +01:00
|
|
|
var cover = selected_message.offset().top + selected_message.height()
|
|
|
|
- $("#compose").offset().top;
|
|
|
|
if (cover > 0) {
|
|
|
|
disable_pointer_movement = true;
|
|
|
|
// We use $('html, body') because you can't animate window.scrollTop
|
|
|
|
// on Chrome (http://bugs.jquery.com/ticket/10419).
|
|
|
|
$('html, body').animate({
|
|
|
|
scrollTop: viewport.scrollTop() + cover + 5
|
|
|
|
}, {
|
|
|
|
complete: function () {
|
|
|
|
// The complete callback is actually called before the
|
|
|
|
// scrolling has completed, so we try to let scrolling
|
|
|
|
// finish before allowing pointer movements again or the
|
|
|
|
// pointer may still move.
|
|
|
|
setTimeout(function () {
|
|
|
|
disable_pointer_movement = false;
|
|
|
|
}, 50);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2012-11-26 19:10:05 +01:00
|
|
|
focus_area.focus();
|
|
|
|
focus_area.select();
|
|
|
|
}
|
|
|
|
|
2013-01-15 22:19:38 +01:00
|
|
|
// In an attempt to decrease mixing, make the composebox's
|
|
|
|
// stream bar look like what you're replying to.
|
|
|
|
// (In particular, if there's a color associated with it,
|
|
|
|
// have that color be reflected here too.)
|
|
|
|
exports.decorate_stream_bar = function (stream_name) {
|
|
|
|
var color = subs.get_color(stream_name);
|
|
|
|
$("#stream-message .message_header_stream").css("background-color", color);
|
|
|
|
};
|
|
|
|
|
2012-10-18 20:16:56 +02:00
|
|
|
exports.start = function (msg_type, opts) {
|
2012-11-26 19:30:11 +01:00
|
|
|
if (reload.is_in_progress()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-18 21:28:28 +01:00
|
|
|
compose.clear();
|
|
|
|
|
2013-01-16 01:39:50 +01:00
|
|
|
var default_stream = '';
|
|
|
|
if (msg_type === 'stream' && lurk_stream !== undefined)
|
|
|
|
default_stream = lurk_stream;
|
2012-10-31 22:08:44 +01:00
|
|
|
opts = $.extend({ message_type: msg_type,
|
2013-01-16 01:39:50 +01:00
|
|
|
stream: default_stream,
|
2012-10-31 22:08:44 +01:00
|
|
|
subject: '',
|
2012-12-05 17:29:56 +01:00
|
|
|
private_message_recipient: ''
|
2012-10-31 22:08:44 +01:00
|
|
|
}, opts);
|
2012-10-18 20:16:56 +02:00
|
|
|
|
2012-11-16 21:00:38 +01:00
|
|
|
compose.stream_name(opts.stream);
|
|
|
|
compose.subject(opts.subject);
|
|
|
|
compose.recipient(opts.private_message_recipient);
|
2012-12-05 17:29:56 +01:00
|
|
|
// If the user opens the compose box, types some text, and then clicks on a
|
|
|
|
// different stream/subject, we want to keep the text in the compose box
|
|
|
|
if (opts.message !== undefined) {
|
|
|
|
compose.message_content(opts.message);
|
|
|
|
}
|
2012-10-18 20:16:56 +02:00
|
|
|
|
2012-12-19 21:19:29 +01:00
|
|
|
ui.change_tab_to("#home");
|
2012-10-18 20:16:56 +02:00
|
|
|
|
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-11-26 19:10:05 +01:00
|
|
|
show('stream', $("#" + (focus_area || 'stream')));
|
2012-10-19 22:09:13 +02:00
|
|
|
} else {
|
2012-11-26 19:10:05 +01:00
|
|
|
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-19 19:51:19 +02:00
|
|
|
is_composing_message = msg_type;
|
2013-01-15 22:19:38 +01:00
|
|
|
exports.decorate_stream_bar(opts.stream);
|
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) {
|
2012-12-11 21:44:04 +01:00
|
|
|
if (error_type !== 'timeout' && reload.is_pending()) {
|
2012-10-29 18:06:53 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2013-01-03 00:14:48 +01:00
|
|
|
show(is_composing_message, $("#new_message_content"));
|
2012-10-29 18:06:53 +01:00
|
|
|
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.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-11-29 20:35:24 +01:00
|
|
|
// Set the mode of a compose already in progress.
|
|
|
|
// Does not clear the input fields.
|
|
|
|
exports.set_mode = function (mode) {
|
2013-01-11 22:14:45 +01:00
|
|
|
if (!is_composing_message) {
|
|
|
|
exports.start(mode);
|
|
|
|
}
|
2012-11-29 20:35:24 +01:00
|
|
|
if (mode === 'private') {
|
2012-11-26 19:10:05 +01:00
|
|
|
show('private', $("#private_message_recipient"));
|
2012-11-26 19:04:22 +01:00
|
|
|
is_composing_message = "private";
|
2012-10-03 20:49:58 +02:00
|
|
|
} else {
|
2013-01-16 01:39:50 +01:00
|
|
|
show('stream', $(lurk_stream !== undefined ? "#stream" : "#subject"));
|
2012-11-26 19:04:22 +01:00
|
|
|
is_composing_message = "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";
|
2013-01-03 00:14:48 +01:00
|
|
|
$('#send-status').removeClass(status_classes);
|
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-11-16 16:45:39 +01:00
|
|
|
ui.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":
|
2013-01-16 20:03:33 +01:00
|
|
|
if (lurk_stream.toLowerCase() === stream_name.toLowerCase()) {
|
|
|
|
// Just subscribe them.
|
|
|
|
subs.subscribe_for_send(stream_name);
|
|
|
|
// When the subscription request completes,
|
|
|
|
// subscribe_for_send will send our message (like when
|
|
|
|
// the user clicks on subscribe-and-send).
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-03 00:14:48 +01:00
|
|
|
$('#send-status').removeClass(status_classes);
|
2012-11-15 19:13:33 +01:00
|
|
|
$('#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;
|
|
|
|
|
|
|
|
}());
|