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
|
|
|
|
|
|
|
//returns true if the edit task should end.
|
2013-05-15 00:22:16 +02:00
|
|
|
exports.save = function (row) {
|
|
|
|
var msg_list = current_msg_list;
|
|
|
|
var message = current_msg_list.get(rows.id(row));
|
2013-08-20 17:00:55 +02:00
|
|
|
var changed = false;
|
2013-08-16 23:45:13 +02:00
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
var request = {message_id: message.id};
|
2013-08-20 17:00:55 +02:00
|
|
|
if (message.type === "stream") {
|
|
|
|
var new_topic = row.find(".message_edit_topic").val();
|
|
|
|
if (new_topic !== message.subject && new_topic.trim() !== "") {
|
|
|
|
request.subject = new_topic;
|
2013-09-03 22:07:59 +02:00
|
|
|
|
2013-09-13 18:12:29 +02:00
|
|
|
if (feature_flags.propagate_topic_edits) {
|
|
|
|
var selected_topic_propagation = row.find("select.message_edit_topic_propagate").val();
|
|
|
|
request.propagate_mode = selected_topic_propagation;
|
2013-09-03 22:07:59 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
var new_content = row.find(".message_edit_content").val();
|
2013-05-15 00:22:16 +02:00
|
|
|
if (new_content !== message.raw_content) {
|
|
|
|
request.content = new_content;
|
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-05-22 18:15:59 +02:00
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/update_message',
|
|
|
|
data: request,
|
|
|
|
dataType: 'json',
|
|
|
|
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) {
|
|
|
|
var message = util.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.
|
|
|
|
};
|
|
|
|
|
2013-08-02 19:25:37 +02:00
|
|
|
function handle_edit_keydown(e) {
|
|
|
|
var row, code = e.keyCode || e.which;
|
|
|
|
|
|
|
|
if (e.target.id === "message_edit_content" && code === 13 &&
|
|
|
|
(e.metaKey || e.ctrlKey)) {
|
|
|
|
row = $(".message_edit_content").filter(":focus").closest(".message_row");
|
2013-08-16 23:45:13 +02:00
|
|
|
if (message_edit.save(row) === true) {
|
|
|
|
message_edit.end(row);
|
|
|
|
}
|
|
|
|
} else if (e.target.id === "message_edit_topic" && code === 13) {
|
|
|
|
//hitting enter in topic field isn't so great.
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2013-08-02 19:25:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2013-05-15 00:22:16 +02:00
|
|
|
content: raw_content}));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-08-03 00:44:03 +02:00
|
|
|
form.keydown(handle_edit_keydown);
|
2013-08-02 19:25:37 +02:00
|
|
|
|
2013-05-15 00:22:16 +02:00
|
|
|
currently_editing_messages[message.id] = edit_obj;
|
2013-07-17 16:24:54 +02:00
|
|
|
if (message.subject === compose.empty_subject_placeholder()) {
|
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
|
|
|
|
|
|
|
if (feature_flags.propagate_topic_edits) {
|
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
|
|
|
|
|
|
|
composebox_typeahead.initialize_compose_typeahead("#message_edit_content");
|
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;
|
|
|
|
$.ajax({
|
|
|
|
type: 'POST',
|
|
|
|
url: '/json/fetch_raw_message',
|
|
|
|
data: {message_id: message.id},
|
|
|
|
dataType: 'json',
|
|
|
|
success: function (data) {
|
|
|
|
if (current_msg_list === msg_list) {
|
|
|
|
message.raw_content = data.raw_content;
|
|
|
|
edit_message(row, data.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);
|
|
|
|
form.keydown(handle_edit_keydown);
|
|
|
|
form.find(".message_edit_topic").focus();
|
|
|
|
};
|
|
|
|
|
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));
|
2013-05-28 22:13:49 +02:00
|
|
|
if (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) {
|
2013-05-15 00:22:16 +02:00
|
|
|
if (currently_editing_messages[id] !== undefined){
|
|
|
|
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;
|
|
|
|
}());
|