2013-05-15 00:22:16 +02:00
|
|
|
var message_edit = (function () {
|
|
|
|
var exports = {};
|
|
|
|
var currently_editing_messages = {};
|
|
|
|
|
2013-08-16 23:45:13 +02:00
|
|
|
|
2016-06-09 22:44:24 +02:00
|
|
|
// Returns true if the edit task should end.
|
2016-06-07 19:59:59 +02:00
|
|
|
exports.save = function (row, from_topic_edited_only) {
|
2013-05-15 00:22:16 +02:00
|
|
|
var msg_list = current_msg_list;
|
2014-03-13 04:25:34 +01:00
|
|
|
var message_id;
|
2014-02-26 18:37:30 +01:00
|
|
|
|
|
|
|
if (row.hasClass('recipient_row')) {
|
2014-03-13 04:25:34 +01:00
|
|
|
message_id = rows.id_for_recipient_row(row);
|
2014-02-26 18:37:30 +01:00
|
|
|
} else {
|
2014-03-13 04:25:34 +01:00
|
|
|
message_id = rows.id(row);
|
2014-02-26 18:37:30 +01:00
|
|
|
}
|
2014-03-13 04:25:34 +01:00
|
|
|
var message = current_msg_list.get(message_id);
|
2013-08-20 17:00:55 +02:00
|
|
|
var changed = false;
|
2013-08-16 23:45:13 +02:00
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
var new_content = row.find(".message_edit_content").val();
|
|
|
|
var topic_changed = false;
|
|
|
|
var new_topic;
|
2013-08-20 17:00:55 +02:00
|
|
|
if (message.type === "stream") {
|
2014-01-02 19:39:22 +01:00
|
|
|
new_topic = row.find(".message_edit_topic").val();
|
|
|
|
topic_changed = (new_topic !== message.subject && new_topic.trim() !== "");
|
|
|
|
}
|
2013-09-03 22:07:59 +02:00
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
// Editing a not-yet-acked message (because the original send attempt failed)
|
|
|
|
// just results in the in-memory message being changed
|
|
|
|
if (message.local_id !== undefined) {
|
|
|
|
// No changes
|
2014-03-13 20:06:16 +01:00
|
|
|
if (new_content === message.raw_content && !topic_changed) {
|
2014-01-02 19:39:22 +01:00
|
|
|
return true;
|
2013-08-20 17:00:55 +02:00
|
|
|
}
|
2014-01-02 19:39:22 +01:00
|
|
|
echo.edit_locally(message, new_content, topic_changed ? new_topic : undefined);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var request = {message_id: message.id};
|
|
|
|
if (topic_changed) {
|
|
|
|
request.subject = new_topic;
|
|
|
|
if (feature_flags.propagate_topic_edits) {
|
|
|
|
var selected_topic_propagation = row.find("select.message_edit_topic_propagate").val() || "change_later";
|
|
|
|
request.propagate_mode = selected_topic_propagation;
|
|
|
|
}
|
|
|
|
changed = true;
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
2013-08-20 17:00:55 +02:00
|
|
|
|
2016-06-07 19:59:59 +02:00
|
|
|
if (new_content !== message.raw_content && !from_topic_edited_only) {
|
2013-05-15 00:22:16 +02:00
|
|
|
request.content = new_content;
|
2016-06-22 22:14:35 +02:00
|
|
|
message.is_me_message = new_content.lastIndexOf('/me', 0) === 0;
|
2013-08-20 17:00:55 +02:00
|
|
|
changed = true;
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
2013-08-20 17:00:55 +02:00
|
|
|
if (!changed) {
|
2013-05-22 18:15:59 +02:00
|
|
|
// If they didn't change anything, just cancel it.
|
2013-08-16 23:45:13 +02:00
|
|
|
return true;
|
2013-05-21 16:21:13 +02:00
|
|
|
}
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-05-22 18:15:59 +02:00
|
|
|
url: '/json/update_message',
|
|
|
|
data: request,
|
|
|
|
success: function (data) {
|
|
|
|
if (msg_list === current_msg_list) {
|
2013-08-16 23:45:13 +02:00
|
|
|
return true;
|
2013-05-22 18:15:59 +02:00
|
|
|
}
|
2013-07-11 23:42:44 +02:00
|
|
|
},
|
|
|
|
error: function (xhr, error_type, xhn) {
|
2014-03-13 15:32:44 +01:00
|
|
|
var message = channel.xhr_error_message("Error saving edit", xhr);
|
2013-08-16 23:45:13 +02:00
|
|
|
row.find(".edit_error").text(message).show();
|
2013-05-22 18:15:59 +02:00
|
|
|
}
|
|
|
|
});
|
2013-05-15 00:22:16 +02:00
|
|
|
// The message will automatically get replaced when it arrives.
|
|
|
|
};
|
|
|
|
|
2016-06-07 19:59:59 +02:00
|
|
|
function handle_edit_keydown(from_topic_edited_only, e) {
|
2013-08-02 19:25:37 +02:00
|
|
|
var row, code = e.keyCode || e.which;
|
|
|
|
|
|
|
|
if (e.target.id === "message_edit_content" && code === 13 &&
|
2013-11-28 22:51:45 +01:00
|
|
|
(e.metaKey || e.ctrlKey)) {
|
2013-08-02 19:25:37 +02:00
|
|
|
row = $(".message_edit_content").filter(":focus").closest(".message_row");
|
2016-06-07 19:59:59 +02:00
|
|
|
if (message_edit.save(row, from_topic_edited_only) === true) {
|
2013-08-16 23:45:13 +02:00
|
|
|
message_edit.end(row);
|
|
|
|
}
|
|
|
|
} else if (e.target.id === "message_edit_topic" && code === 13) {
|
2016-06-09 22:44:24 +02:00
|
|
|
// Hitting enter in topic field isn't so great.
|
2013-08-16 23:45:13 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2013-08-02 19:25:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 02:25:55 +02:00
|
|
|
function timer_text(seconds_left) {
|
|
|
|
var minutes = Math.floor(seconds_left / 60);
|
|
|
|
var seconds = seconds_left % 60;
|
|
|
|
if (minutes >= 1) {
|
|
|
|
return i18n.t("__minutes__ min to edit", {'minutes': minutes.toString()});
|
|
|
|
} else if (seconds_left >= 10) {
|
|
|
|
return i18n.t("__seconds__ sec to edit", {'seconds': (seconds - seconds % 5).toString()});
|
|
|
|
}
|
|
|
|
return i18n.t("__seconds__ sec to edit", {'seconds': seconds.toString()});
|
|
|
|
}
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
function edit_message (row, raw_content) {
|
2013-07-22 20:19:35 +02:00
|
|
|
var content_top = row.find('.message_content')[0]
|
|
|
|
.getBoundingClientRect().top;
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
var message = current_msg_list.get(rows.id(row));
|
|
|
|
var edit_row = row.find(".message_edit");
|
|
|
|
var form = $(templates.render('message_edit_form',
|
|
|
|
{is_stream: message.is_stream,
|
2013-08-16 23:45:13 +02:00
|
|
|
topic: message.subject,
|
2016-07-08 02:25:55 +02:00
|
|
|
content: raw_content,
|
|
|
|
minutes_to_edit: Math.floor(page_params.realm_message_content_edit_limit_seconds / 60)}));
|
2013-05-15 00:22:16 +02:00
|
|
|
|
|
|
|
var edit_obj = {form: form, raw_content: raw_content};
|
2013-09-13 18:12:29 +02:00
|
|
|
var original_topic = message.subject;
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, edit_obj);
|
|
|
|
|
2016-06-07 19:59:59 +02:00
|
|
|
form.keydown(_.partial(handle_edit_keydown, false));
|
2013-08-02 19:25:37 +02:00
|
|
|
|
2016-07-08 02:25:55 +02:00
|
|
|
// We potentially got to this function by clicking a button that implied the
|
|
|
|
// user would be able to edit their message. Give a little bit of buffer in
|
|
|
|
// case the button has been around for a bit, e.g. we show the
|
|
|
|
// edit_content_button (hovering pencil icon) as long as the user would have
|
|
|
|
// been able to click it at the time the mouse entered the message_row. Also
|
|
|
|
// a buffer in case their computer is slow, or stalled for a second, etc
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
|
|
|
// zerver.views.messages.update_message_backend
|
|
|
|
var seconds_left_buffer = 5;
|
|
|
|
|
|
|
|
var now = new XDate();
|
|
|
|
var seconds_left = page_params.realm_message_content_edit_limit_seconds +
|
|
|
|
now.diffSeconds(message.timestamp * 1000);
|
|
|
|
var can_edit_content = (page_params.realm_message_content_edit_limit_seconds === 0) ||
|
|
|
|
(seconds_left + seconds_left_buffer > 0);
|
|
|
|
if (!can_edit_content) {
|
|
|
|
row.find('textarea.message_edit_content').attr("disabled","disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we allow editing at all, give them at least 10 seconds to do it.
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
|
|
|
// zerver.views.messages.update_message_backend
|
|
|
|
var min_seconds_to_edit = 10;
|
|
|
|
seconds_left = Math.floor(Math.max(seconds_left, min_seconds_to_edit));
|
|
|
|
|
|
|
|
// Add a visual timer if appropriate
|
|
|
|
if (can_edit_content && page_params.realm_message_content_edit_limit_seconds > 0) {
|
|
|
|
row.find('.message-edit-timer-control-group').show();
|
|
|
|
$('#message_edit_tooltip').tooltip({ animation: false, placement: 'left',
|
|
|
|
template: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner message-edit-tooltip-inner"></div></div>'});
|
|
|
|
// I believe these need to be defined outside the countdown_timer, since
|
|
|
|
// row just refers to something like the currently selected message, and
|
|
|
|
// can change out from under us
|
|
|
|
var message_content_row = row.find('textarea.message_edit_content');
|
|
|
|
var message_topic_row, message_topic_propagate_row;
|
|
|
|
if (message.is_stream) {
|
|
|
|
message_topic_row = row.find('input.message_edit_topic');
|
|
|
|
message_topic_propagate_row = row.find('select.message_edit_topic_propagate');
|
|
|
|
}
|
|
|
|
var message_save_row = row.find('button.message_edit_save');
|
|
|
|
var timer_row = row.find('.message_edit_countdown_timer');
|
|
|
|
// Do this right away, rather than waiting for the timer to do its first update,
|
|
|
|
// since otherwise there is a noticeable lag
|
|
|
|
timer_row.text(timer_text(seconds_left));
|
|
|
|
var countdown_timer = setInterval(function () {
|
|
|
|
if (--seconds_left <= 0) {
|
|
|
|
clearInterval(countdown_timer);
|
|
|
|
message_content_row.attr("disabled","disabled");
|
|
|
|
if (message.is_stream) {
|
|
|
|
message_topic_row.attr("disabled","disabled");
|
|
|
|
message_topic_propagate_row.hide();
|
|
|
|
}
|
|
|
|
message_save_row.addClass("disabled");
|
|
|
|
timer_row.text(i18n.t("Time's up!"));
|
|
|
|
} else {
|
|
|
|
timer_row.text(timer_text(seconds_left));
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
currently_editing_messages[message.id] = edit_obj;
|
2016-07-08 02:25:55 +02:00
|
|
|
if ((message.type === 'stream' && message.subject === compose.empty_subject_placeholder()) ||
|
|
|
|
!can_edit_content) {
|
2013-08-16 23:45:13 +02:00
|
|
|
edit_row.find(".message_edit_topic").focus();
|
2013-07-17 16:24:54 +02:00
|
|
|
} else {
|
|
|
|
edit_row.find(".message_edit_content").focus();
|
|
|
|
}
|
2013-07-22 20:19:35 +02:00
|
|
|
|
|
|
|
// Scroll to keep the message content in the same place
|
|
|
|
var edit_top = edit_row.find('.message_edit_content')[0]
|
|
|
|
.getBoundingClientRect().top;
|
|
|
|
|
|
|
|
var scroll_by = edit_top - content_top + 5 /* border and padding */;
|
|
|
|
edit_obj.scrolled_by = scroll_by;
|
|
|
|
viewport.scrollTop(viewport.scrollTop() + scroll_by);
|
2013-09-04 20:30:33 +02:00
|
|
|
|
2014-02-12 16:08:19 +01:00
|
|
|
if (feature_flags.propagate_topic_edits && message.local_id === undefined) {
|
2013-09-13 18:12:29 +02:00
|
|
|
var topic_input = edit_row.find(".message_edit_topic");
|
|
|
|
topic_input.keyup( function () {
|
|
|
|
var new_topic = topic_input.val();
|
|
|
|
row.find('.message_edit_topic_propagate').toggle(new_topic !== original_topic);
|
|
|
|
});
|
2013-09-04 20:30:33 +02:00
|
|
|
}
|
2013-09-20 19:15:11 +02:00
|
|
|
|
2013-09-23 20:48:19 +02:00
|
|
|
composebox_typeahead.initialize_compose_typeahead("#message_edit_content", {emoji: true});
|
2016-07-08 02:25:55 +02:00
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
function start_edit_maintaining_scroll(row, content) {
|
|
|
|
edit_message(row, content);
|
|
|
|
var row_bottom = row.height() + row.offset().top;
|
|
|
|
var composebox_top = $("#compose").offset().top;
|
|
|
|
if (row_bottom > composebox_top) {
|
|
|
|
viewport.scrollTop(viewport.scrollTop() + row_bottom - composebox_top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
exports.start = function (row) {
|
|
|
|
var message = current_msg_list.get(rows.id(row));
|
|
|
|
var msg_list = current_msg_list;
|
2013-12-18 19:55:18 +01:00
|
|
|
channel.post({
|
2013-05-15 00:22:16 +02:00
|
|
|
url: '/json/fetch_raw_message',
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2013-05-15 00:22:16 +02:00
|
|
|
data: {message_id: message.id},
|
|
|
|
success: function (data) {
|
|
|
|
if (current_msg_list === msg_list) {
|
|
|
|
message.raw_content = data.raw_content;
|
2014-01-02 19:39:22 +01:00
|
|
|
start_edit_maintaining_scroll(row, data.raw_content);
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
exports.start_local_failed_edit = function (row, message) {
|
|
|
|
start_edit_maintaining_scroll(row, message.raw_content);
|
|
|
|
};
|
|
|
|
|
2013-08-16 23:45:13 +02:00
|
|
|
exports.start_topic_edit = function (recipient_row) {
|
|
|
|
var form = $(templates.render('topic_edit_form'));
|
|
|
|
current_msg_list.show_edit_topic(recipient_row, form);
|
2016-06-07 19:59:59 +02:00
|
|
|
form.keydown(_.partial(handle_edit_keydown, true));
|
2014-03-13 04:25:34 +01:00
|
|
|
var msg_id = rows.id_for_recipient_row(recipient_row);
|
|
|
|
var message = current_msg_list.get(msg_id);
|
2013-11-21 00:28:14 +01:00
|
|
|
var topic = message.subject;
|
|
|
|
if (topic === compose.empty_subject_placeholder()) {
|
|
|
|
topic = '';
|
|
|
|
}
|
|
|
|
form.find(".message_edit_topic").val(topic).select().focus();
|
2013-08-16 23:45:13 +02:00
|
|
|
};
|
|
|
|
|
2013-06-11 18:54:07 +02:00
|
|
|
exports.is_editing = function (id) {
|
|
|
|
return currently_editing_messages[id] !== undefined;
|
|
|
|
};
|
|
|
|
|
2013-05-22 19:04:11 +02:00
|
|
|
exports.end = function (row) {
|
2013-05-15 00:22:16 +02:00
|
|
|
var message = current_msg_list.get(rows.id(row));
|
2014-02-21 16:46:26 +01:00
|
|
|
if (message !== undefined &&
|
|
|
|
currently_editing_messages[message.id] !== undefined) {
|
2013-07-22 20:19:35 +02:00
|
|
|
var scroll_by = currently_editing_messages[message.id].scrolled_by;
|
|
|
|
viewport.scrollTop(viewport.scrollTop() - scroll_by);
|
2013-05-28 22:13:49 +02:00
|
|
|
delete currently_editing_messages[message.id];
|
|
|
|
current_msg_list.hide_edit_message(row);
|
|
|
|
}
|
2013-05-15 00:22:16 +02:00
|
|
|
};
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.maybe_show_edit = function (row, id) {
|
2015-11-10 19:01:34 +01:00
|
|
|
if (currently_editing_messages[id] !== undefined) {
|
2013-05-15 00:22:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, currently_editing_messages[id]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-25 22:48:55 +02:00
|
|
|
$(document).on('narrow_deactivated.zulip', function (event) {
|
2013-07-30 00:35:44 +02:00
|
|
|
_.each(currently_editing_messages, function (elem, idx) {
|
2013-05-15 00:22:16 +02:00
|
|
|
if (current_msg_list.get(idx) !== undefined) {
|
2013-08-14 22:00:32 +02:00
|
|
|
var row = current_msg_list.get_row(idx);
|
2013-05-15 00:22:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, elem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|