2017-02-22 02:34:05 +01:00
|
|
|
var drafts = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var draft_model = (function () {
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
// the key that the drafts are stored under.
|
|
|
|
var KEY = "drafts";
|
|
|
|
var ls = localstorage();
|
|
|
|
ls.version = 1;
|
|
|
|
|
|
|
|
function getTimestamp() {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get() {
|
|
|
|
return ls.get(KEY) || {};
|
|
|
|
}
|
|
|
|
exports.get = get;
|
|
|
|
|
|
|
|
exports.getDraft = function (id) {
|
|
|
|
return get()[id] || false;
|
|
|
|
};
|
|
|
|
|
|
|
|
function save(drafts) {
|
|
|
|
ls.set(KEY, drafts);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.addDraft = function (draft) {
|
|
|
|
var drafts = get();
|
|
|
|
|
|
|
|
// use the base16 of the current time + a random string to reduce
|
|
|
|
// collisions to essentially zero.
|
|
|
|
var id = getTimestamp().toString(16) + "-" + Math.random().toString(16).split(/\./).pop();
|
|
|
|
|
|
|
|
draft.updatedAt = getTimestamp();
|
|
|
|
drafts[id] = draft;
|
|
|
|
save(drafts);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.editDraft = function (id, draft) {
|
|
|
|
var drafts = get();
|
|
|
|
|
|
|
|
if (drafts[id]) {
|
|
|
|
draft.updatedAt = getTimestamp();
|
|
|
|
drafts[id] = draft;
|
|
|
|
save(drafts);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.deleteDraft = function (id) {
|
|
|
|
var drafts = get();
|
|
|
|
|
|
|
|
delete drafts[id];
|
|
|
|
save(drafts);
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
exports.draft_model = draft_model;
|
|
|
|
|
2017-03-29 08:54:26 +02:00
|
|
|
exports.snapshot_message = function () {
|
2018-08-21 23:04:30 +02:00
|
|
|
if (!compose_state.composing() || compose_state.message_content().length <= 2) {
|
2017-03-29 08:54:26 +02:00
|
|
|
// If you aren't in the middle of composing the body of a
|
2018-08-21 23:04:30 +02:00
|
|
|
// message or the message is shorter than 2 characters long, don't try to snapshot.
|
2017-03-29 08:54:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save what we can.
|
|
|
|
var message = {
|
2017-04-24 20:35:26 +02:00
|
|
|
type: compose_state.get_message_type(),
|
2017-04-15 01:15:59 +02:00
|
|
|
content: compose_state.message_content(),
|
2017-03-29 08:54:26 +02:00
|
|
|
};
|
|
|
|
if (message.type === "private") {
|
|
|
|
var recipient = compose_state.recipient();
|
|
|
|
message.reply_to = recipient;
|
|
|
|
message.private_message_recipient = recipient;
|
|
|
|
} else {
|
2017-04-15 01:15:59 +02:00
|
|
|
message.stream = compose_state.stream_name();
|
2018-12-16 17:36:41 +01:00
|
|
|
message.topic = compose_state.topic();
|
2017-03-29 08:54:26 +02:00
|
|
|
}
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
2018-12-16 16:52:27 +01:00
|
|
|
exports.restore_message = function (draft) {
|
|
|
|
// This is kinda the inverse of snapshot_message, and
|
|
|
|
// we are essentially making a deep copy of the draft,
|
|
|
|
// being explicit about which fields we send to the compose
|
|
|
|
// system.
|
|
|
|
var compose_args;
|
|
|
|
|
|
|
|
if (draft.type === "stream") {
|
|
|
|
compose_args = {
|
|
|
|
type: 'stream',
|
|
|
|
stream: draft.stream,
|
2018-11-15 19:14:16 +01:00
|
|
|
topic: util.get_draft_topic(draft),
|
2018-12-16 16:52:27 +01:00
|
|
|
content: draft.content,
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
compose_args = {
|
|
|
|
type: draft.type,
|
|
|
|
private_message_recipient: draft.private_message_recipient,
|
|
|
|
content: draft.content,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return compose_args;
|
|
|
|
};
|
|
|
|
|
2017-12-11 17:04:42 +01:00
|
|
|
function draft_notify() {
|
|
|
|
$(".alert-draft").css("display", "inline-block");
|
|
|
|
$(".alert-draft").delay(1000).fadeOut(300);
|
|
|
|
}
|
|
|
|
|
2017-02-22 02:34:05 +01:00
|
|
|
exports.update_draft = function () {
|
2017-03-29 08:54:26 +02:00
|
|
|
var draft = drafts.snapshot_message();
|
2017-11-26 20:37:44 +01:00
|
|
|
var draft_id = $("#compose-textarea").data("draft-id");
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
if (draft_id !== undefined) {
|
|
|
|
if (draft !== undefined) {
|
|
|
|
draft_model.editDraft(draft_id, draft);
|
2017-12-11 17:04:42 +01:00
|
|
|
draft_notify();
|
2017-02-22 02:34:05 +01:00
|
|
|
} else {
|
|
|
|
draft_model.deleteDraft(draft_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (draft !== undefined) {
|
|
|
|
var new_draft_id = draft_model.addDraft(draft);
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").data("draft-id", new_draft_id);
|
2017-12-11 17:04:42 +01:00
|
|
|
draft_notify();
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.delete_draft_after_send = function () {
|
2017-11-26 20:37:44 +01:00
|
|
|
var draft_id = $("#compose-textarea").data("draft-id");
|
2017-02-22 02:34:05 +01:00
|
|
|
if (draft_id) {
|
|
|
|
draft_model.deleteDraft(draft_id);
|
|
|
|
}
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").removeData("draft-id");
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.restore_draft = function (draft_id) {
|
|
|
|
var draft = draft_model.getDraft(draft_id);
|
|
|
|
if (!draft) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-16 16:52:27 +01:00
|
|
|
var compose_args = exports.restore_message(draft);
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2018-12-16 16:52:27 +01:00
|
|
|
if (compose_args.type === "stream") {
|
2017-06-24 23:36:27 +02:00
|
|
|
if (draft.stream !== "") {
|
2018-05-07 01:38:14 +02:00
|
|
|
narrow.activate(
|
|
|
|
[
|
2018-12-16 16:52:27 +01:00
|
|
|
{operator: "stream", operand: compose_args.stream},
|
2018-11-15 19:14:16 +01:00
|
|
|
{operator: "topic", operand: compose_args.topic},
|
2018-05-07 01:38:14 +02:00
|
|
|
],
|
2018-12-16 16:52:27 +01:00
|
|
|
{trigger: "restore draft"}
|
|
|
|
);
|
2017-06-24 23:36:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-16 16:52:27 +01:00
|
|
|
if (compose_args.private_message_recipient !== "") {
|
|
|
|
narrow.activate(
|
|
|
|
[
|
|
|
|
{operator: "pm-with", operand: compose_args.private_message_recipient},
|
|
|
|
],
|
|
|
|
{trigger: "restore draft"}
|
|
|
|
);
|
2017-06-24 23:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:40:54 +02:00
|
|
|
overlays.close_overlay("drafts");
|
2017-02-22 02:34:05 +01:00
|
|
|
compose_fade.clear_compose();
|
2017-12-19 03:34:55 +01:00
|
|
|
compose.clear_preview_area();
|
|
|
|
|
2017-02-22 02:34:05 +01:00
|
|
|
if (draft.type === "stream" && draft.stream === "") {
|
2018-11-15 19:14:16 +01:00
|
|
|
compose_args.topic = "";
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
2018-12-16 16:52:27 +01:00
|
|
|
compose_actions.start(compose_args.type, compose_args);
|
2017-04-23 08:51:26 +02:00
|
|
|
compose_ui.autosize_textarea();
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").data("draft-id", draft_id);
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
2018-02-15 17:38:31 +01:00
|
|
|
var DRAFT_LIFETIME = 30;
|
|
|
|
|
|
|
|
function remove_old_drafts() {
|
|
|
|
var old_date = new Date().setDate(new Date().getDate() - DRAFT_LIFETIME);
|
|
|
|
var drafts = draft_model.get();
|
|
|
|
_.each(drafts, function (draft, id) {
|
|
|
|
if (draft.updatedAt < old_date) {
|
|
|
|
draft_model.deleteDraft(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Exporting for testing purpose
|
|
|
|
exports.remove_old_drafts = remove_old_drafts;
|
|
|
|
|
2018-12-16 21:10:20 +01:00
|
|
|
exports.launch = function () {
|
2017-02-22 02:34:05 +01:00
|
|
|
function format_drafts(data) {
|
2017-10-05 21:51:56 +02:00
|
|
|
var drafts = {};
|
2017-12-27 17:14:59 +01:00
|
|
|
var data_array = [];
|
2017-10-05 21:51:56 +02:00
|
|
|
_.each(data, function (draft, id) {
|
2017-12-27 17:14:59 +01:00
|
|
|
data_array.push([id, data[id]]);
|
|
|
|
});
|
2018-12-07 21:21:39 +01:00
|
|
|
var data_sorted = data_array.sort(function (draft_a, draft_b) {
|
2018-06-04 21:13:07 +02:00
|
|
|
return draft_a[1].updatedAt - draft_b[1].updatedAt;
|
2017-12-27 17:14:59 +01:00
|
|
|
});
|
|
|
|
_.each(data_sorted, function (data_element) {
|
|
|
|
var draft = data_element[1];
|
|
|
|
var id = data_element[0];
|
2017-02-22 02:34:05 +01:00
|
|
|
var formatted;
|
2017-12-25 21:43:06 +01:00
|
|
|
var time = new XDate(draft.updatedAt);
|
|
|
|
var time_stamp = timerender.render_now(time).time_str;
|
2018-03-11 21:32:14 +01:00
|
|
|
if (time_stamp === i18n.t("Today")) {
|
2017-12-25 21:43:06 +01:00
|
|
|
time_stamp = timerender.stringify_time(time);
|
|
|
|
}
|
2017-02-22 02:34:05 +01:00
|
|
|
if (draft.type === "stream") {
|
|
|
|
// In case there is no stream for the draft, we need a
|
|
|
|
// single space char for proper rendering of the stream label
|
|
|
|
var space_string = new Handlebars.SafeString(" ");
|
2018-06-06 18:19:09 +02:00
|
|
|
var stream = draft.stream.length > 0 ? draft.stream : space_string;
|
2018-12-16 17:36:41 +01:00
|
|
|
var draft_topic = util.get_draft_topic(draft);
|
|
|
|
|
|
|
|
if (draft_topic === '') {
|
|
|
|
draft_topic = compose.empty_topic_placeholder();
|
|
|
|
}
|
2017-04-10 21:09:29 +02:00
|
|
|
|
2017-02-22 02:34:05 +01:00
|
|
|
formatted = {
|
|
|
|
draft_id: id,
|
|
|
|
is_stream: true,
|
|
|
|
stream: stream,
|
|
|
|
stream_color: stream_data.get_color(draft.stream),
|
2017-04-10 21:09:29 +02:00
|
|
|
topic: draft_topic,
|
2017-02-22 02:34:05 +01:00
|
|
|
raw_content: draft.content,
|
2017-12-25 21:43:06 +01:00
|
|
|
time_stamp: time_stamp,
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
var emails = util.extract_pm_recipients(draft.private_message_recipient);
|
|
|
|
var recipients = _.map(emails, function (email) {
|
|
|
|
email = email.trim();
|
|
|
|
var person = people.get_by_email(email);
|
|
|
|
if (person !== undefined) {
|
|
|
|
return person.full_name;
|
|
|
|
}
|
|
|
|
return email;
|
|
|
|
}).join(', ');
|
|
|
|
|
|
|
|
formatted = {
|
|
|
|
draft_id: id,
|
|
|
|
is_stream: false,
|
|
|
|
recipients: recipients,
|
|
|
|
raw_content: draft.content,
|
2017-12-25 21:43:06 +01:00
|
|
|
time_stamp: time_stamp,
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
2017-10-05 21:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-05-09 18:01:43 +02:00
|
|
|
markdown.apply_markdown(formatted);
|
2017-10-05 21:51:56 +02:00
|
|
|
} catch (error) {
|
|
|
|
// In the unlikely event that there is syntax in the
|
|
|
|
// draft content which our markdown processor is
|
|
|
|
// unable to process, we delete the draft, so that the
|
|
|
|
// drafts overlay can be opened without any errors.
|
|
|
|
// We also report the exception to the server so that
|
|
|
|
// the bug can be fixed.
|
|
|
|
draft_model.deleteDraft(id);
|
|
|
|
blueslip.error("Error in rendering draft.", {
|
|
|
|
draft_content: draft.content,
|
|
|
|
}, error.stack);
|
|
|
|
return;
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
2017-10-05 21:51:56 +02:00
|
|
|
|
|
|
|
drafts[id] = formatted;
|
2017-02-22 02:34:05 +01:00
|
|
|
});
|
|
|
|
return drafts;
|
|
|
|
}
|
|
|
|
|
2018-05-30 18:06:13 +02:00
|
|
|
function populate_and_fill() {
|
2017-02-22 02:34:05 +01:00
|
|
|
$('#drafts_table').empty();
|
|
|
|
var drafts = format_drafts(draft_model.get());
|
2018-12-07 21:21:39 +01:00
|
|
|
var rendered = templates.render('draft_table_body', {
|
2018-05-06 21:43:17 +02:00
|
|
|
drafts: drafts,
|
|
|
|
draft_lifetime: DRAFT_LIFETIME,
|
2018-02-15 17:38:31 +01:00
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
$('#drafts_table').append(rendered);
|
|
|
|
if ($("#drafts_table .draft-row").length > 0) {
|
|
|
|
$('#drafts_table .no-drafts').hide();
|
|
|
|
}
|
2018-12-16 20:56:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup_event_handlers() {
|
|
|
|
$(".restore-draft").on("click", function (e) {
|
|
|
|
e.stopPropagation();
|
2017-02-22 02:34:05 +01:00
|
|
|
|
2018-12-16 20:56:35 +01:00
|
|
|
var draft_row = $(this).closest(".draft-row");
|
|
|
|
var draft_id = draft_row.data("draft-id");
|
|
|
|
exports.restore_draft(draft_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
$(".draft_controls .delete-draft").on("click", function () {
|
|
|
|
var draft_row = $(this).closest(".draft-row");
|
|
|
|
var draft_id = draft_row.data("draft-id");
|
|
|
|
|
|
|
|
exports.draft_model.deleteDraft(draft_id);
|
|
|
|
draft_row.remove();
|
|
|
|
|
|
|
|
if ($("#drafts_table .draft-row").length === 0) {
|
|
|
|
$('#drafts_table .no-drafts').show();
|
|
|
|
}
|
|
|
|
});
|
2017-02-22 02:34:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:38:31 +01:00
|
|
|
remove_old_drafts();
|
2017-02-22 02:34:05 +01:00
|
|
|
populate_and_fill();
|
2018-12-16 21:10:20 +01:00
|
|
|
exports.open_modal();
|
|
|
|
exports.set_initial_element();
|
2018-12-16 20:56:35 +01:00
|
|
|
setup_event_handlers();
|
2017-02-22 02:34:05 +01:00
|
|
|
};
|
|
|
|
|
2018-12-16 20:17:17 +01:00
|
|
|
function activate_element(elem) {
|
|
|
|
$('.draft-info-box').removeClass('active');
|
|
|
|
$(elem).expectOne().addClass('active');
|
|
|
|
elem.focus();
|
|
|
|
}
|
|
|
|
|
2017-04-14 21:12:52 +02:00
|
|
|
function drafts_initialize_focus(event_name) {
|
|
|
|
// If a draft is not focused in draft modal, then focus the last draft
|
|
|
|
// if up_arrow is clicked or the first draft if down_arrow is clicked.
|
|
|
|
if (event_name !== "up_arrow" && event_name !== "down_arrow" || $(".draft-info-box:focus")[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var draft_arrow = draft_model.get();
|
|
|
|
var draft_id_arrow = Object.getOwnPropertyNames(draft_arrow);
|
|
|
|
if (draft_id_arrow.length === 0) { // empty drafts modal
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var draft_element;
|
|
|
|
if (event_name === "up_arrow") {
|
2018-06-04 21:13:07 +02:00
|
|
|
draft_element = document.querySelectorAll('[data-draft-id="' + draft_id_arrow[draft_id_arrow.length - 1] + '"]');
|
2017-04-14 21:12:52 +02:00
|
|
|
} else if (event_name === "down_arrow") {
|
|
|
|
draft_element = document.querySelectorAll('[data-draft-id="' + draft_id_arrow[0] + '"]');
|
|
|
|
}
|
|
|
|
var focus_element = draft_element[0].children[0];
|
2018-12-16 20:17:17 +01:00
|
|
|
|
|
|
|
activate_element(focus_element);
|
2017-04-14 21:12:52 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 21:49:40 +02:00
|
|
|
function drafts_scroll(next_focus_draft_row) {
|
|
|
|
if (next_focus_draft_row[0] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (next_focus_draft_row[0].children[0] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-16 20:17:17 +01:00
|
|
|
activate_element(next_focus_draft_row[0].children[0]);
|
2017-04-14 21:49:40 +02:00
|
|
|
|
|
|
|
// If focused draft is first draft, scroll to the top.
|
|
|
|
if ($(".draft-info-box:first")[0].parentElement === next_focus_draft_row[0]) {
|
|
|
|
$(".drafts-list")[0].scrollTop = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is the last draft, scroll to the bottom.
|
|
|
|
if ($(".draft-info-box:last")[0].parentElement === next_focus_draft_row[0]) {
|
|
|
|
$(".drafts-list")[0].scrollTop = $('.drafts-list')[0].scrollHeight - $('.drafts-list').height();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is cut off from the top, scroll up halfway in draft modal.
|
|
|
|
if (next_focus_draft_row.position().top < 55) {
|
|
|
|
// 55 is the minimum distance from the top that will require extra scrolling.
|
|
|
|
$(".drafts-list")[0].scrollTop -= $(".drafts-list")[0].clientHeight / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If focused draft is cut off from the bottom, scroll down halfway in draft modal.
|
|
|
|
var dist_from_top = next_focus_draft_row.position().top;
|
|
|
|
var total_dist = dist_from_top + next_focus_draft_row[0].clientHeight;
|
|
|
|
var dist_from_bottom = $(".drafts-container")[0].clientHeight - total_dist;
|
|
|
|
if (dist_from_bottom < -4) {
|
|
|
|
//-4 is the min dist from the bottom that will require extra scrolling.
|
|
|
|
$(".drafts-list")[0].scrollTop += $(".drafts-list")[0].clientHeight / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
exports.drafts_handle_events = function (e, event_key) {
|
|
|
|
var draft_arrow = draft_model.get();
|
|
|
|
var draft_id_arrow = Object.getOwnPropertyNames(draft_arrow);
|
2017-04-14 21:12:52 +02:00
|
|
|
drafts_initialize_focus(event_key);
|
2017-04-14 08:57:41 +02:00
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
// This detects up arrow key presses when the draft overlay
|
|
|
|
// is open and scrolls through the drafts.
|
|
|
|
if (event_key === "up_arrow") {
|
|
|
|
var focus_draft_up_row = $(".draft-info-box:focus")[0].parentElement;
|
|
|
|
var prev_focus_draft_row = $(focus_draft_up_row).prev();
|
2017-04-14 21:49:40 +02:00
|
|
|
drafts_scroll(prev_focus_draft_row);
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2017-04-04 20:48:08 +02:00
|
|
|
|
2017-03-18 21:26:24 +01:00
|
|
|
// This detects down arrow key presses when the draft overlay
|
|
|
|
// is open and scrolls through the drafts.
|
|
|
|
if (event_key === "down_arrow") {
|
|
|
|
var focus_draft_down_row = $(".draft-info-box:focus")[0].parentElement;
|
|
|
|
var next_focus_draft_row = $(focus_draft_down_row).next();
|
2017-04-14 21:49:40 +02:00
|
|
|
drafts_scroll(next_focus_draft_row);
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
2017-04-04 20:48:08 +02:00
|
|
|
|
2017-04-14 08:57:41 +02:00
|
|
|
var elt = document.activeElement;
|
|
|
|
var focused_draft = $(elt.parentElement)[0].getAttribute("data-draft-id");
|
2017-03-18 21:26:24 +01:00
|
|
|
// Allows user to delete drafts with backspace
|
2018-03-07 21:27:55 +01:00
|
|
|
if (event_key === "backspace" || event_key === "delete") {
|
2017-03-18 21:26:24 +01:00
|
|
|
if (elt.parentElement.hasAttribute("data-draft-id")) {
|
|
|
|
var focus_draft_back_row = $(elt)[0].parentElement;
|
|
|
|
var backnext_focus_draft_row = $(focus_draft_back_row).next();
|
|
|
|
var backprev_focus_draft_row = $(focus_draft_back_row).prev();
|
|
|
|
var delete_id;
|
|
|
|
if (backnext_focus_draft_row[0] !== undefined) {
|
|
|
|
delete_id = backnext_focus_draft_row[0].getAttribute("data-draft-id");
|
|
|
|
} else if (backprev_focus_draft_row[0] !== undefined) {
|
|
|
|
delete_id = backprev_focus_draft_row[0].getAttribute("data-draft-id");
|
|
|
|
}
|
|
|
|
drafts.draft_model.deleteDraft(focused_draft);
|
|
|
|
document.activeElement.parentElement.remove();
|
|
|
|
var new_focus_element = document.querySelectorAll('[data-draft-id="' + delete_id + '"]');
|
|
|
|
if (new_focus_element[0] !== undefined) {
|
2018-12-16 20:17:17 +01:00
|
|
|
activate_element(new_focus_element[0].children[0]);
|
2017-03-18 21:26:24 +01:00
|
|
|
}
|
|
|
|
if ($("#drafts_table .draft-row").length === 0) {
|
|
|
|
$('#drafts_table .no-drafts').show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 08:57:41 +02:00
|
|
|
|
|
|
|
// This handles when pressing enter while looking at drafts.
|
|
|
|
// It restores draft that is focused.
|
|
|
|
if (event_key === "enter") {
|
|
|
|
if (document.activeElement.parentElement.hasAttribute("data-draft-id")) {
|
|
|
|
exports.restore_draft(focused_draft);
|
|
|
|
} else {
|
2018-06-04 21:13:07 +02:00
|
|
|
var first_draft = draft_id_arrow[draft_id_arrow.length - 1];
|
2017-04-14 08:57:41 +02:00
|
|
|
exports.restore_draft(first_draft);
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 21:26:24 +01:00
|
|
|
};
|
|
|
|
|
2018-12-16 21:10:20 +01:00
|
|
|
exports.open_modal = function () {
|
|
|
|
overlays.open_overlay({
|
|
|
|
name: 'drafts',
|
|
|
|
overlay: $('#draft_overlay'),
|
|
|
|
on_close: function () {
|
|
|
|
hashchange.exit_overlay();
|
|
|
|
},
|
2017-02-22 02:34:05 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-12-16 21:10:20 +01:00
|
|
|
exports.set_initial_element = function () {
|
|
|
|
var draft_list = drafts.draft_model.get();
|
|
|
|
var draft_id_list = Object.getOwnPropertyNames(draft_list);
|
|
|
|
if (draft_id_list.length > 0) {
|
|
|
|
var last_draft = draft_id_list[draft_id_list.length - 1];
|
|
|
|
var last_draft_element = document.querySelectorAll('[data-draft-id="' + last_draft + '"]');
|
|
|
|
var focus_element = last_draft_element[0].children[0];
|
|
|
|
activate_element(focus_element);
|
|
|
|
$(".drafts-list")[0].scrollTop = $('.drafts-list')[0].scrollHeight - $('.drafts-list').height();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-02 23:12:15 +02:00
|
|
|
exports.initialize = function () {
|
2017-02-22 02:34:05 +01:00
|
|
|
window.addEventListener("beforeunload", function () {
|
|
|
|
exports.update_draft();
|
|
|
|
});
|
|
|
|
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").focusout(exports.update_draft);
|
2018-12-16 20:17:17 +01:00
|
|
|
|
|
|
|
$('body').on('focus', '.draft-info-box', function (e) {
|
|
|
|
activate_element(e.target);
|
|
|
|
});
|
2017-06-02 23:12:15 +02:00
|
|
|
};
|
2017-02-22 02:34:05 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = drafts;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.drafts = drafts;
|