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;
|
2013-02-13 21:30:17 +01:00
|
|
|
var faded_to;
|
2013-04-05 16:44:46 +02:00
|
|
|
var message_snapshot;
|
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.
|
2013-02-20 18:26:50 +01:00
|
|
|
if (current_msg_list.selected_id() === -1) {
|
2012-12-05 19:27:31 +01:00
|
|
|
// If there's no selected message, there's no need to
|
|
|
|
// scroll the compose box to avoid it.
|
|
|
|
return;
|
|
|
|
}
|
2013-02-14 23:48:37 +01:00
|
|
|
var selected_row = current_msg_list.selected_row();
|
|
|
|
var cover = selected_row.offset().top + selected_row.height()
|
2012-11-30 03:49:18 +01:00
|
|
|
- $("#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-30 21:49:56 +01:00
|
|
|
// Disable the notifications bar if it overlaps with the composebox
|
|
|
|
notifications_bar.maybe_disable();
|
2012-11-26 19:10:05 +01:00
|
|
|
}
|
|
|
|
|
2013-05-02 17:12:58 +02:00
|
|
|
function update_lock_icon_for_stream(stream_name) {
|
|
|
|
var icon = $("#compose-lock-icon");
|
|
|
|
if (subs.get_invite_only(stream_name)) {
|
|
|
|
icon.show();
|
|
|
|
} else {
|
|
|
|
icon.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2013-05-02 17:12:58 +02:00
|
|
|
update_lock_icon_for_stream(stream_name);
|
2013-02-19 20:13:50 +01:00
|
|
|
$("#stream-message .message_header_stream")
|
|
|
|
.css('background-color', color)
|
|
|
|
.removeClass(subs.color_classes)
|
|
|
|
.addClass(subs.get_color_class(color));
|
2013-01-15 22:19:38 +01:00
|
|
|
};
|
|
|
|
|
2013-03-12 21:46:54 +01:00
|
|
|
function messages_to_fade() {
|
2013-03-06 23:47:32 +01:00
|
|
|
var all_elts = rows.get_table(current_msg_list.table_name).find(".recipient_row, .messagebox");
|
|
|
|
var i, elts_to_fade = [];
|
|
|
|
var different_recipient = false;
|
|
|
|
// Note: The below algorithm relies on the fact that all_elts is
|
|
|
|
// sorted as it would be displayed in the message view
|
|
|
|
for (i = 0; i < all_elts.length; i++) {
|
|
|
|
var elt = $(all_elts[i]);
|
|
|
|
if (elt.hasClass("recipient_row")) {
|
2013-03-12 21:46:54 +01:00
|
|
|
if (!util.same_recipient(faded_to, current_msg_list.get(rows.id(elt)))) {
|
2013-03-06 23:47:32 +01:00
|
|
|
elts_to_fade.push(elt);
|
|
|
|
different_recipient = true;
|
|
|
|
} else {
|
|
|
|
different_recipient = false;
|
|
|
|
}
|
|
|
|
} else if (different_recipient) {
|
|
|
|
elts_to_fade.push(elt);
|
2013-02-13 21:30:17 +01:00
|
|
|
}
|
2013-03-06 23:47:32 +01:00
|
|
|
}
|
|
|
|
return elts_to_fade;
|
2013-02-13 21:30:17 +01:00
|
|
|
}
|
|
|
|
|
2013-03-06 23:47:49 +01:00
|
|
|
exports.unfade_messages = function (clear_state) {
|
2013-02-13 21:30:17 +01:00
|
|
|
if (faded_to === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fade_class = narrow.active() ? "message_reply_fade_narrowed" : "message_reply_fade";
|
2013-03-06 23:47:32 +01:00
|
|
|
rows.get_table(current_msg_list.table_name).find(".recipient_row, .messagebox").removeClass(fade_class);
|
2013-03-06 23:47:49 +01:00
|
|
|
if (clear_state === true) {
|
|
|
|
faded_to = undefined;
|
|
|
|
}
|
2013-02-13 21:30:17 +01:00
|
|
|
ui.enable_floating_recipient_bar();
|
|
|
|
};
|
|
|
|
|
2013-03-04 16:59:09 +01:00
|
|
|
exports.update_faded_messages = function () {
|
|
|
|
if (faded_to === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-12 21:46:54 +01:00
|
|
|
var i, fade_class, elts_to_fade;
|
|
|
|
|
|
|
|
fade_class = narrow.active() ? "message_reply_fade_narrowed" : "message_reply_fade";
|
|
|
|
elts_to_fade = messages_to_fade();
|
|
|
|
|
|
|
|
for (i = 0; i < elts_to_fade.length; i++) {
|
|
|
|
$(elts_to_fade[i]).addClass(fade_class);
|
|
|
|
}
|
|
|
|
ui.disable_floating_recipient_bar();
|
2013-03-04 16:59:09 +01:00
|
|
|
};
|
|
|
|
|
2013-03-12 20:53:54 +01:00
|
|
|
exports.update_recipient_on_narrow = function() {
|
|
|
|
if (!compose.composing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (compose.message_content() !== "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var compose_opts = {};
|
|
|
|
narrow.set_compose_defaults(compose_opts);
|
|
|
|
if (compose_opts.stream) {
|
|
|
|
compose.start("stream");
|
|
|
|
} else {
|
|
|
|
compose.start("private");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-12 21:46:54 +01:00
|
|
|
function do_fade(reply_message, fade_type) {
|
|
|
|
compose.unfade_messages();
|
|
|
|
|
|
|
|
// Construct faded_to as a mocked up element which has all the
|
|
|
|
// fields of a message used by util.same_recipient()
|
|
|
|
faded_to = {
|
|
|
|
type: fade_type
|
|
|
|
};
|
|
|
|
if (fade_type === "stream") {
|
2013-04-18 17:13:43 +02:00
|
|
|
faded_to.stream = reply_message.stream;
|
2013-03-12 21:46:54 +01:00
|
|
|
faded_to.subject = reply_message.subject;
|
|
|
|
} else {
|
|
|
|
faded_to.reply_to = reply_message.reply_to;
|
|
|
|
}
|
|
|
|
exports.update_faded_messages();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-27 19:24:56 +01:00
|
|
|
var default_opts = {
|
|
|
|
message_type: msg_type,
|
|
|
|
stream: '',
|
|
|
|
subject: '',
|
|
|
|
private_message_recipient: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
// Set default parameters based on the current narrowed view.
|
|
|
|
narrow.set_compose_defaults(default_opts);
|
|
|
|
|
|
|
|
opts = $.extend(default_opts, opts);
|
2012-10-18 20:16:56 +02:00
|
|
|
|
2013-03-12 20:27:38 +01:00
|
|
|
if (!(compose.composing() === msg_type &&
|
|
|
|
((msg_type === "stream" &&
|
2013-03-13 16:21:51 +01:00
|
|
|
opts.stream === compose.stream_name() &&
|
2013-03-12 20:27:38 +01:00
|
|
|
opts.subject === compose.subject()) ||
|
|
|
|
(msg_type === "private" &&
|
|
|
|
opts.private_message_recipient === compose.recipient())))) {
|
|
|
|
// Clear the compose box if the existing message is to a different recipient
|
|
|
|
compose.clear();
|
|
|
|
}
|
|
|
|
|
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
|
2013-04-17 19:43:02 +02:00
|
|
|
if (opts.content !== undefined) {
|
|
|
|
compose.message_content(opts.content);
|
2012-12-05 17:29:56 +01:00
|
|
|
}
|
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
|
|
|
|
2013-04-05 16:44:46 +02:00
|
|
|
if (message_snapshot !== undefined) {
|
2013-04-24 23:44:25 +02:00
|
|
|
$('#restore-draft').show();
|
2013-04-05 16:44:46 +02:00
|
|
|
}
|
|
|
|
|
2013-02-13 21:30:17 +01:00
|
|
|
if (opts.replying_to_message !== undefined) {
|
2013-03-12 21:46:54 +01:00
|
|
|
do_fade(opts.replying_to_message, msg_type);
|
2013-02-13 21:30:17 +01: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
|
|
|
};
|
|
|
|
|
2013-03-27 18:49:28 +01:00
|
|
|
function abort_xhr () {
|
|
|
|
$("#compose-send-button").removeAttr("disabled");
|
|
|
|
var xhr = $("#compose").data("filedrop_xhr");
|
|
|
|
if (xhr !== undefined) {
|
|
|
|
xhr.abort();
|
|
|
|
$("#compose").removeData("filedrop_xhr");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 20:16:56 +02:00
|
|
|
exports.cancel = function () {
|
|
|
|
compose.hide();
|
2013-03-27 18:49:28 +01:00
|
|
|
abort_xhr();
|
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
|
|
|
};
|
|
|
|
|
2013-04-17 19:34:20 +02:00
|
|
|
function create_message_object() {
|
|
|
|
var message = {client: 'website',
|
|
|
|
type: compose.composing(),
|
|
|
|
subject: compose.subject(),
|
|
|
|
stream: compose.stream_name(),
|
|
|
|
private_message_recipient: compose.recipient(),
|
|
|
|
content: compose.message_content()};
|
|
|
|
|
|
|
|
if (message.type === "private") {
|
|
|
|
// TODO: this should be collapsed with the code in composebox_typeahead.js
|
|
|
|
message.to = compose.recipient().split(/\s*[,;]\s*/);
|
2013-04-18 21:36:55 +02:00
|
|
|
message.reply_to = compose.recipient();
|
2013-04-17 19:34:20 +02:00
|
|
|
} else {
|
|
|
|
message.to = compose.stream_name();
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2013-04-05 16:44:46 +02:00
|
|
|
exports.snapshot_message = function(message) {
|
|
|
|
if (!exports.composing() || (exports.message_content() === "")) {
|
|
|
|
// If you aren't in the middle of composing the body of a
|
|
|
|
// message, don't try to snapshot.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message !== undefined) {
|
|
|
|
message_snapshot = $.extend({}, message);
|
|
|
|
} else {
|
|
|
|
// Save what we can.
|
|
|
|
message_snapshot = create_message_object();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function clear_message_snapshot() {
|
2013-04-24 23:44:25 +02:00
|
|
|
$("#restore-draft").hide();
|
2013-04-05 16:44:46 +02:00
|
|
|
message_snapshot = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.restore_message = function() {
|
|
|
|
if (!message_snapshot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var snapshot_copy = $.extend({}, message_snapshot);
|
2013-04-18 21:36:55 +02:00
|
|
|
if ((snapshot_copy.type === "stream" &&
|
|
|
|
snapshot_copy.stream.length > 0 &&
|
|
|
|
snapshot_copy.subject.length > 0) ||
|
|
|
|
(snapshot_copy.type === "private" &&
|
|
|
|
snapshot_copy.reply_to.length > 0)) {
|
|
|
|
snapshot_copy = $.extend({replying_to_message: snapshot_copy},
|
|
|
|
snapshot_copy);
|
|
|
|
}
|
2013-04-05 16:44:46 +02:00
|
|
|
clear_message_snapshot();
|
|
|
|
exports.unfade_messages(true);
|
|
|
|
compose.start(snapshot_copy.type, snapshot_copy);
|
|
|
|
};
|
|
|
|
|
2013-02-01 17:04:23 +01:00
|
|
|
function compose_error(error_text, bad_input) {
|
|
|
|
$('#send-status').removeClass(status_classes)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.stop(true).fadeTo(0, 1);
|
2013-02-20 17:04:00 +01:00
|
|
|
$('#error-msg').html(error_text);
|
2013-02-01 17:04:23 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2013-03-06 17:40:51 +01:00
|
|
|
$("#sending-indicator").hide();
|
2013-02-01 17:04:23 +01:00
|
|
|
bad_input.focus().select();
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
|
2013-04-17 19:34:20 +02:00
|
|
|
var request = create_message_object();
|
2013-04-05 16:44:46 +02:00
|
|
|
exports.snapshot_message(request);
|
2012-11-07 20:26:24 +01:00
|
|
|
|
2013-04-17 19:34:20 +02:00
|
|
|
if (tutorial.is_running()) {
|
2013-04-23 23:13:19 +02:00
|
|
|
// We want the un-json-encoded version of the request.to, so
|
|
|
|
// we need to get it before the JSON stringify below messes it
|
|
|
|
// up.
|
2013-04-17 19:34:20 +02:00
|
|
|
tutorial.message_was_sent(request);
|
2012-11-14 23:21:46 +01:00
|
|
|
}
|
2012-11-07 20:26:24 +01:00
|
|
|
|
2013-04-17 19:34:20 +02:00
|
|
|
if (request.type === "private") {
|
|
|
|
request.to = JSON.stringify(request.to);
|
|
|
|
} else {
|
|
|
|
request.to = JSON.stringify([request.to]);
|
2013-02-13 22:15:13 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2013-04-05 16:44:46 +02:00
|
|
|
clear_message_snapshot();
|
2012-10-31 21:17:07 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2013-03-06 17:40:51 +01:00
|
|
|
$("#sending-indicator").hide();
|
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-02-01 17:04:23 +01:00
|
|
|
compose_error(response, $('#new_message_content'));
|
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 () {
|
2013-04-05 16:44:46 +02:00
|
|
|
exports.snapshot_message();
|
2013-03-06 17:11:20 +01:00
|
|
|
$('.message_comp').find('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"});});
|
2013-01-30 21:49:56 +01:00
|
|
|
notifications_bar.enable();
|
2013-03-06 23:47:49 +01:00
|
|
|
exports.unfade_messages(true);
|
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-02-17 23:44:02 +01:00
|
|
|
ui.change_tab_to('#home');
|
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-02-06 00:33:45 +01:00
|
|
|
show('stream', $("#stream"));
|
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
|
|
|
|
2013-01-29 00:28:56 +01:00
|
|
|
function get_or_set(fieldname, keep_outside_whitespace) {
|
2012-10-31 22:12:45 +01:00
|
|
|
// 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);
|
2013-01-29 00:28:56 +01:00
|
|
|
var oldval = elem.val();
|
2012-10-31 22:12:45 +01:00
|
|
|
if (newval !== undefined) {
|
|
|
|
elem.val(newval);
|
|
|
|
}
|
2013-01-29 00:28:56 +01:00
|
|
|
return keep_outside_whitespace ? oldval : $.trim(oldval);
|
2012-10-31 22:12:45 +01:00
|
|
|
};
|
|
|
|
}
|
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');
|
2013-01-29 00:28:56 +01:00
|
|
|
exports.message_content = get_or_set('new_message_content', true);
|
2012-11-08 00:38:21 +01:00
|
|
|
exports.recipient = get_or_set('private_message_recipient');
|
2012-10-03 20:49:58 +02:00
|
|
|
|
2012-10-10 23:37:14 +02:00
|
|
|
// *Synchronously* check if a stream exists.
|
2013-01-31 21:03:45 +01:00
|
|
|
exports.check_stream_existence = function (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) {
|
2013-03-21 19:58:30 +01:00
|
|
|
if (data.subscribed) {
|
2012-11-15 19:13:33 +01:00
|
|
|
result = "subscribed";
|
|
|
|
} else {
|
|
|
|
result = "not-subscribed";
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
2013-03-21 19:58:30 +01:00
|
|
|
if (xhr.status === 404) {
|
|
|
|
result = "does-not-exist";
|
|
|
|
} else {
|
|
|
|
result = "error";
|
|
|
|
}
|
2012-10-03 22:53:15 +02:00
|
|
|
}
|
|
|
|
});
|
2013-03-11 23:13:37 +01:00
|
|
|
return result;
|
2013-01-31 21:03:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Checks if a stream exists. If not, displays an error and returns
|
|
|
|
// false.
|
|
|
|
function check_stream_for_send(stream_name) {
|
2013-03-11 23:13:37 +01:00
|
|
|
var result = exports.check_stream_existence(stream_name);
|
2013-01-31 21:03:45 +01:00
|
|
|
|
|
|
|
if (result === "error") {
|
2013-03-11 23:09:22 +01:00
|
|
|
compose_error("Error checking subscription", $("#stream"));
|
2013-01-31 21:03:45 +01:00
|
|
|
$("#compose-send-button").removeAttr('disabled');
|
2013-03-06 17:40:51 +01:00
|
|
|
$("#sending-indicator").hide();
|
2013-01-31 21:03:45 +01: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
|
|
|
}
|
|
|
|
|
2013-02-01 17:04:23 +01:00
|
|
|
var response;
|
|
|
|
|
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":
|
2013-02-20 17:04:00 +01:00
|
|
|
response = "<p>The stream <b>" +
|
|
|
|
Handlebars.Utils.escapeExpression(stream_name) + "</b> does not exist.</p>" +
|
|
|
|
"<p>Manage your subscriptions <a href='#subscriptions'>on your Streams page</a>.</p>";
|
2013-02-01 17:04:23 +01:00
|
|
|
compose_error(response, $('#stream'));
|
|
|
|
return false;
|
2012-11-15 19:13:33 +01:00
|
|
|
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-02-20 17:04:00 +01:00
|
|
|
response = "<p>You're not subscribed to the stream <b>" +
|
|
|
|
Handlebars.Utils.escapeExpression(stream_name) + "</b>.</p>" +
|
|
|
|
"<p>Manage your subscriptions <a href='#subscriptions'>on your Streams page</a>.</p>";
|
2013-02-01 17:04:23 +01:00
|
|
|
compose_error(response, $('#stream'));
|
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();
|
2013-03-06 17:40:51 +01:00
|
|
|
$("#sending-indicator").show();
|
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();
|
2013-03-14 22:12:25 +01:00
|
|
|
$("#compose").filedrop({
|
|
|
|
url: "json/upload_file",
|
2013-04-24 00:40:47 +02:00
|
|
|
fallback_id: "file_input",
|
2013-03-14 22:12:25 +01:00
|
|
|
paramname: "file",
|
|
|
|
maxfilesize: 25,
|
|
|
|
data: {
|
|
|
|
// the token isn't automatically included in filedrop's post
|
|
|
|
csrfmiddlewaretoken: csrf_token
|
|
|
|
},
|
2013-04-19 19:02:38 +02:00
|
|
|
raw_droppable: ['text/uri-list', 'text/plain'],
|
2013-03-14 22:12:25 +01:00
|
|
|
drop: function (i, file, len) {
|
|
|
|
$("#compose-send-button").attr("disabled", "");
|
|
|
|
$("#send-status").addClass("alert-info")
|
|
|
|
.show();
|
2013-03-27 18:49:28 +01:00
|
|
|
$(".send-status-close").one('click', abort_xhr);
|
2013-03-27 19:11:08 +01:00
|
|
|
$("#error-msg").html(
|
|
|
|
$("<p>").text("Uploading…")
|
|
|
|
.after('<div class="progress progress-striped active">' +
|
|
|
|
'<div class="bar" id="upload-bar" style="width: 00%;"></div>' +
|
|
|
|
'</div>'));
|
|
|
|
},
|
|
|
|
progressUpdated: function (i, file, progress) {
|
|
|
|
$("#upload-bar").width(progress + "%");
|
2013-03-14 22:12:25 +01:00
|
|
|
},
|
2013-03-27 18:45:24 +01:00
|
|
|
error: function (err, file) {
|
|
|
|
var msg;
|
|
|
|
$("#send-status").addClass("alert-error")
|
|
|
|
.removeClass("alert-info");
|
|
|
|
$("#compose-send-button").removeAttr("disabled");
|
|
|
|
switch(err) {
|
|
|
|
case 'BrowserNotSupported':
|
|
|
|
msg = "File upload is not yet available for your browser.";
|
|
|
|
break;
|
|
|
|
case 'TooManyFiles':
|
|
|
|
msg = "Unable to upload that many files at once.";
|
|
|
|
break;
|
|
|
|
case 'FileTooLarge':
|
|
|
|
// sanitizatio not needed as the file name is not potentially parsed as HTML, etc.
|
|
|
|
msg = "\"" + file.name + "\" was too large; the maximum file size is 25MiB.";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
msg = "An unknown error occured.";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$("#error-msg").text(msg);
|
|
|
|
},
|
2013-03-14 22:12:25 +01:00
|
|
|
uploadFinished: function (i, file, response, time) {
|
2013-04-05 22:19:42 +02:00
|
|
|
if (response.uri === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-05 22:20:00 +02:00
|
|
|
var textbox = $("#new_message_content"),
|
|
|
|
split_uri = response.uri.split("/"),
|
|
|
|
filename = split_uri[split_uri.length - 1];
|
2013-04-19 19:03:48 +02:00
|
|
|
if (i === -1) {
|
|
|
|
// This is a paste, so there's no filename. Show the image directly
|
|
|
|
textbox.val(textbox.val() + "[pasted image](" + response.uri + ") ");
|
|
|
|
} else {
|
|
|
|
// This is a dropped file, so make the filename a link to the image
|
|
|
|
textbox.val(textbox.val() + "[" + filename + "](" + response.uri + ")" + " ");
|
|
|
|
}
|
2013-03-14 22:12:25 +01:00
|
|
|
$("#new_message_content").trigger("autosize");
|
|
|
|
$("#compose-send-button").removeAttr("disabled");
|
|
|
|
$("#send-status").removeClass("alert-info")
|
|
|
|
.hide();
|
2013-04-19 19:02:38 +02:00
|
|
|
},
|
|
|
|
rawDrop: function (contents) {
|
|
|
|
var textbox = $("#new_message_content");
|
|
|
|
textbox.val(textbox.val() + contents);
|
2013-03-14 22:12:25 +01:00
|
|
|
}
|
|
|
|
});
|
2012-10-24 04:02:39 +02:00
|
|
|
});
|
|
|
|
|
2012-10-18 20:29:16 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|