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-04-05 16:44:46 +02:00
|
|
|
var message_snapshot;
|
2013-07-25 21:16:22 +02:00
|
|
|
var empty_subject_placeholder = "(no topic)";
|
2012-10-18 20:29:16 +02:00
|
|
|
|
2013-05-08 16:54:01 +02:00
|
|
|
// This function resets an input type="file". Pass in the
|
|
|
|
// jquery object.
|
|
|
|
function clear_out_file_list(jq_file_list) {
|
|
|
|
var clone_for_ie_sake = jq_file_list.clone(true);
|
|
|
|
jq_file_list.replaceWith(clone_for_ie_sake);
|
|
|
|
|
|
|
|
// Hack explanation:
|
|
|
|
// IE won't let you do this (untested, but so says StackOverflow):
|
|
|
|
// $("#file_input").val("");
|
|
|
|
}
|
|
|
|
|
2013-07-12 23:26:18 +02:00
|
|
|
// Show the compose box.
|
|
|
|
function show_box(tabname, focus_area) {
|
2012-11-26 19:10:05 +01:00
|
|
|
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");
|
2013-05-28 16:55:56 +02:00
|
|
|
$(".new_message_textarea").css("min-height", "3em");
|
|
|
|
|
2013-07-09 23:08:33 +02:00
|
|
|
if (focus_area !== undefined) {
|
|
|
|
focus_area.focus().select();
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:55:56 +02:00
|
|
|
// If the compose box is obscuring the currently selected message,
|
|
|
|
// scroll up until the message is no longer occluded.
|
|
|
|
if (current_msg_list.selected_id() === -1) {
|
|
|
|
// If there's no selected message, there's no need to
|
|
|
|
// scroll the compose box to avoid it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var selected_row = current_msg_list.selected_row();
|
|
|
|
var cover = selected_row.offset().top + selected_row.height()
|
|
|
|
- $("#compose").offset().top;
|
|
|
|
if (cover > 0) {
|
|
|
|
viewport.user_initiated_animate_scroll(cover+5);
|
|
|
|
}
|
|
|
|
|
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-07-12 23:26:18 +02:00
|
|
|
function clear_box() {
|
|
|
|
exports.snapshot_message();
|
|
|
|
$("#compose").find('input[type=text], textarea').val('');
|
|
|
|
$("#new_message_content").trigger('autosize');
|
|
|
|
$("#send-status").hide(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hide_box() {
|
|
|
|
$('.message_comp').find('input, textarea, button').blur();
|
|
|
|
$('#stream-message').hide();
|
|
|
|
$('#private-message').hide();
|
|
|
|
$(".new_message_textarea").css("min-height", "");
|
|
|
|
notifications_bar.enable();
|
2013-08-12 17:40:55 +02:00
|
|
|
compose_fade.clear_compose();
|
2013-07-12 23:26:18 +02:00
|
|
|
$('.message_comp').hide();
|
|
|
|
$("#compose_controls").show();
|
|
|
|
}
|
|
|
|
|
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)
|
2013-08-07 19:28:06 +02:00
|
|
|
.removeClass(stream_color.color_classes)
|
|
|
|
.addClass(stream_color.get_color_class(color));
|
2013-01-15 22:19:38 +01:00
|
|
|
};
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.update_recipient_on_narrow = function () {
|
2013-03-12 20:53:54 +01:00
|
|
|
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-06-17 19:31:38 +02:00
|
|
|
function update_fade () {
|
2013-08-01 17:47:48 +02:00
|
|
|
if (!is_composing_message) {
|
|
|
|
return;
|
|
|
|
}
|
2013-03-12 21:46:54 +01:00
|
|
|
|
2013-08-11 23:54:50 +02:00
|
|
|
// Legacy strangeness: is_composing_message can be false, "stream", or "private"
|
|
|
|
var msg_type = is_composing_message;
|
|
|
|
compose_fade.set_focused_recipient(msg_type);
|
2013-08-11 21:21:47 +02:00
|
|
|
compose_fade.update_faded_messages();
|
2013-03-12 21:46:54 +01:00
|
|
|
}
|
|
|
|
|
2013-06-17 19:31:38 +02:00
|
|
|
$(function () {
|
|
|
|
$('#stream,#subject,#private_message_recipient').bind({
|
|
|
|
keyup: update_fade,
|
|
|
|
change: update_fade
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-06-12 23:57:43 +02:00
|
|
|
$("#compose_close").show();
|
2013-06-14 22:02:45 +02:00
|
|
|
$("#compose_controls").hide();
|
|
|
|
$('.message_comp').show();
|
2013-06-12 23:57:43 +02:00
|
|
|
|
2013-02-27 19:24:56 +01:00
|
|
|
var default_opts = {
|
|
|
|
message_type: msg_type,
|
|
|
|
stream: '',
|
|
|
|
subject: '',
|
2013-05-20 23:35:41 +02:00
|
|
|
private_message_recipient: '',
|
|
|
|
trigger: 'unknown'
|
2013-02-27 19:24:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set default parameters based on the current narrowed view.
|
|
|
|
narrow.set_compose_defaults(default_opts);
|
|
|
|
|
2013-07-30 05:11:50 +02:00
|
|
|
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
|
2013-07-12 23:26:18 +02:00
|
|
|
clear_box();
|
2013-03-12 20:27:38 +01:00
|
|
|
}
|
|
|
|
|
2012-11-16 21:00:38 +01:00
|
|
|
compose.stream_name(opts.stream);
|
|
|
|
compose.subject(opts.subject);
|
2013-07-26 00:54:25 +02:00
|
|
|
|
|
|
|
// Set the recipients with a space after each comma, so it looks nice.
|
|
|
|
compose.recipient(opts.private_message_recipient.replace(/,\s*/g, ", "));
|
|
|
|
|
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;
|
2013-07-09 23:08:33 +02:00
|
|
|
if (msg_type === 'stream' && opts.stream && ! opts.subject) {
|
2012-10-19 22:09:13 +02:00
|
|
|
focus_area = 'subject';
|
2013-07-09 23:08:33 +02:00
|
|
|
} else if ((msg_type === 'stream' && opts.stream)
|
2013-07-16 00:45:38 +02:00
|
|
|
|| (msg_type === 'private' && 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
|
|
|
|
2013-06-13 23:48:55 +02:00
|
|
|
is_composing_message = msg_type;
|
|
|
|
|
2012-10-19 22:09:13 +02:00
|
|
|
if (msg_type === 'stream') {
|
2013-07-12 23:26:18 +02:00
|
|
|
show_box('stream', $("#" + (focus_area || 'stream')));
|
2012-10-19 22:09:13 +02:00
|
|
|
} else {
|
2013-07-12 23:26:18 +02:00
|
|
|
show_box('private', $("#" + (focus_area || 'private_message_recipient')));
|
2012-10-18 20:16:56 +02:00
|
|
|
}
|
2012-10-19 22:09:13 +02:00
|
|
|
|
2013-08-12 17:40:55 +02:00
|
|
|
compose_fade.start_compose(msg_type);
|
2013-02-13 21:30:17 +01:00
|
|
|
|
2013-01-15 22:19:38 +01:00
|
|
|
exports.decorate_stream_bar(opts.stream);
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('compose_started.zulip', 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 () {
|
2013-06-12 23:57:43 +02:00
|
|
|
$("#compose_close").hide();
|
2013-07-12 23:26:18 +02:00
|
|
|
clear_box();
|
|
|
|
hide_box();
|
2013-03-27 18:49:28 +01:00
|
|
|
abort_xhr();
|
2012-10-19 19:51:19 +02:00
|
|
|
is_composing_message = false;
|
2013-05-03 22:16:52 +02:00
|
|
|
if (message_snapshot !== undefined) {
|
|
|
|
$('#restore-draft').show();
|
|
|
|
}
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('compose_canceled.zulip'));
|
2013-05-16 23:16:15 +02:00
|
|
|
respond_to_cursor = false;
|
2012-10-18 20:16:56 +02:00
|
|
|
};
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.empty_subject_placeholder = function () {
|
2013-05-20 22:52:03 +02:00
|
|
|
return empty_subject_placeholder;
|
|
|
|
};
|
|
|
|
|
2013-04-17 19:34:20 +02:00
|
|
|
function create_message_object() {
|
2013-05-20 22:52:03 +02:00
|
|
|
// Subjects are optional, and we provide a placeholder if one isn't given.
|
|
|
|
var subject = compose.subject();
|
|
|
|
if (subject === "") {
|
|
|
|
subject = compose.empty_subject_placeholder();
|
|
|
|
}
|
2013-04-17 19:34:20 +02:00
|
|
|
var message = {client: 'website',
|
|
|
|
type: compose.composing(),
|
2013-05-20 22:52:03 +02:00
|
|
|
subject: subject,
|
2013-04-17 19:34:20 +02:00
|
|
|
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-07-05 17:43:56 +02:00
|
|
|
exports.snapshot_message = function (message) {
|
2013-04-05 16:44:46 +02:00
|
|
|
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) {
|
2013-07-30 05:11:50 +02:00
|
|
|
message_snapshot = _.extend({}, message);
|
2013-04-05 16:44:46 +02:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.restore_message = function () {
|
2013-04-05 16:44:46 +02:00
|
|
|
if (!message_snapshot) {
|
|
|
|
return;
|
|
|
|
}
|
2013-07-30 05:11:50 +02:00
|
|
|
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)) {
|
2013-07-30 19:10:50 +02:00
|
|
|
snapshot_copy = _.extend({replying_to_message: snapshot_copy},
|
|
|
|
snapshot_copy);
|
2013-04-18 21:36:55 +02:00
|
|
|
}
|
2013-04-05 16:44:46 +02:00
|
|
|
clear_message_snapshot();
|
2013-08-12 17:40:55 +02:00
|
|
|
compose_fade.clear_compose();
|
2013-04-05 16:44:46 +02:00
|
|
|
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 (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) {
|
2013-07-12 23:26:18 +02:00
|
|
|
clear_box();
|
2012-10-29 18:06:53 +01:00
|
|
|
send_status.hide();
|
2012-11-07 19:59:35 +01:00
|
|
|
is_composing_message = false;
|
2013-07-12 23:26:18 +02:00
|
|
|
hide_box();
|
2013-05-08 15:29:22 +02:00
|
|
|
if (request.type === "private") {
|
|
|
|
onboarding.mark_checklist_step("sent_private_message");
|
|
|
|
} else {
|
|
|
|
onboarding.mark_checklist_step("sent_stream_message");
|
|
|
|
}
|
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();
|
2013-05-16 23:16:15 +02:00
|
|
|
send_status.hide();
|
2013-07-26 19:13:02 +02:00
|
|
|
if (respond_to_cursor && (feature_flags.always_open_compose ||
|
2013-06-11 17:30:55 +02:00
|
|
|
narrow.narrowed_by_reply())) {
|
2013-05-16 23:16:15 +02:00
|
|
|
respond_to_message({trigger: 'autorespond'});
|
|
|
|
}
|
2013-05-16 23:16:15 +02:00
|
|
|
else {
|
|
|
|
respond_to_cursor = false;
|
2013-07-26 19:13:02 +02:00
|
|
|
|
|
|
|
if (feature_flags.always_open_compose) {
|
2013-07-30 05:11:50 +02:00
|
|
|
var new_msg = _.extend({replying_to_message: request},
|
|
|
|
request);
|
2013-07-25 22:45:55 +02:00
|
|
|
new_msg.content = "";
|
|
|
|
compose.start(new_msg.type, new_msg);
|
|
|
|
}
|
2013-06-07 18:37:18 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
2013-07-11 23:42:44 +02:00
|
|
|
var response = util.xhr_error_message("Error sending message", xhr);
|
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
|
|
|
|
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?
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).trigger($.Event('compose_finished.zulip'));
|
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.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
|
|
|
}
|
|
|
|
|
2013-02-01 17:04:23 +01:00
|
|
|
var response;
|
|
|
|
|
2013-06-12 21:13:19 +02:00
|
|
|
if (!subs.is_subscribed(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-05-03 22:16:52 +02:00
|
|
|
|
2013-06-13 23:48:55 +02:00
|
|
|
$("#new_message_content").focus(function (e) {
|
2013-06-11 19:33:17 +02:00
|
|
|
// If we click in the composebox, start up a new message
|
2013-05-03 22:16:52 +02:00
|
|
|
if (!compose.composing()) {
|
2013-05-24 22:00:23 +02:00
|
|
|
if (narrow.narrowed_to_pms()) {
|
2013-05-26 04:56:55 +02:00
|
|
|
compose.start('private');
|
2013-05-24 22:00:23 +02:00
|
|
|
} else {
|
2013-05-26 04:56:55 +02:00
|
|
|
compose.start('stream');
|
2013-05-24 22:00:23 +02:00
|
|
|
}
|
2013-05-03 22:16:52 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|
2013-05-07 23:19:24 +02:00
|
|
|
case 'REQUEST ENTITY TOO LARGE':
|
|
|
|
msg = "Sorry, the file was too large.";
|
|
|
|
break;
|
2013-03-27 18:45:24 +01:00
|
|
|
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-05-07 21:22:25 +02:00
|
|
|
// Urgh, yet another hack to make sure we're "composing"
|
|
|
|
// when text gets added into the composebox.
|
|
|
|
if (!compose.composing()) {
|
2013-07-12 23:16:07 +02:00
|
|
|
compose.start('stream');
|
2013-05-07 21:22:25 +02:00
|
|
|
}
|
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-05-08 16:54:01 +02:00
|
|
|
|
|
|
|
// In order to upload the same file twice in a row, we need to clear out
|
|
|
|
// the #file_input element, so that the next time we use the file dialog,
|
|
|
|
// an actual change event is fired. This is extracted to a function
|
|
|
|
// to abstract away some IE hacks.
|
|
|
|
clear_out_file_list($("#file_input"));
|
2013-04-19 19:02:38 +02:00
|
|
|
},
|
|
|
|
rawDrop: function (contents) {
|
|
|
|
var textbox = $("#new_message_content");
|
2013-05-07 21:22:25 +02:00
|
|
|
if (!compose.composing()) {
|
2013-07-12 23:16:07 +02:00
|
|
|
compose.start('stream');
|
2013-05-07 21:22:25 +02:00
|
|
|
}
|
2013-04-19 19:02:38 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
}());
|