mirror of
https://github.com/zulip/zulip.git
synced 2025-11-14 10:57:58 +00:00
upload: Centralize config object construction.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
d9f05121d9
commit
25b29b5cd4
@@ -578,10 +578,7 @@ function start_edit_with_content($row, content, edit_box_open_callback) {
|
|||||||
edit_box_open_callback();
|
edit_box_open_callback();
|
||||||
}
|
}
|
||||||
const row_id = rows.id($row);
|
const row_id = rows.id($row);
|
||||||
upload.setup_upload({
|
upload.setup_upload(upload.edit_config(row_id));
|
||||||
mode: "edit",
|
|
||||||
row: row_id,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function start($row, edit_box_open_callback) {
|
export function start($row, edit_box_open_callback) {
|
||||||
@@ -795,7 +792,7 @@ export function end_message_row_edit($row) {
|
|||||||
const row_id = rows.id($row);
|
const row_id = rows.id($row);
|
||||||
|
|
||||||
// Clean up the upload handler
|
// Clean up the upload handler
|
||||||
upload.deactivate_upload({mode: "edit", row: row_id});
|
upload.deactivate_upload(upload.edit_config(row_id));
|
||||||
|
|
||||||
const message = message_lists.current.get(row_id);
|
const message = message_lists.current.get(row_id);
|
||||||
if (message !== undefined && currently_editing_messages.has(message.id)) {
|
if (message !== undefined && currently_editing_messages.has(message.id)) {
|
||||||
|
|||||||
@@ -36,6 +36,17 @@ export function get_translated_status(file) {
|
|||||||
return "[" + status + "]()";
|
return "[" + status + "]()";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const compose_config = {
|
||||||
|
mode: "compose",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function edit_config(row) {
|
||||||
|
return {
|
||||||
|
mode: "edit",
|
||||||
|
row,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function get_item(key, config, file_id) {
|
export function get_item(key, config, file_id) {
|
||||||
if (!config) {
|
if (!config) {
|
||||||
throw new Error("Missing config");
|
throw new Error("Missing config");
|
||||||
@@ -487,9 +498,7 @@ export function deactivate_upload(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function initialize() {
|
export function initialize() {
|
||||||
compose_upload_object = setup_upload({
|
compose_upload_object = setup_upload(compose_config);
|
||||||
mode: "compose",
|
|
||||||
});
|
|
||||||
|
|
||||||
$(".app, #navbar-fixed-container").on("dragstart", (event) => {
|
$(".app, #navbar-fixed-container").on("dragstart", (event) => {
|
||||||
if (event.target.nodeName === "IMG") {
|
if (event.target.nodeName === "IMG") {
|
||||||
@@ -524,27 +533,24 @@ export function initialize() {
|
|||||||
|
|
||||||
if (compose_state.composing()) {
|
if (compose_state.composing()) {
|
||||||
// Compose box is open; drop there.
|
// Compose box is open; drop there.
|
||||||
upload_files(compose_upload_object, {mode: "compose"}, files);
|
upload_files(compose_upload_object, compose_config, files);
|
||||||
} else if ($last_drag_drop_edit_container.length !== 0) {
|
} else if ($last_drag_drop_edit_container.length !== 0) {
|
||||||
// A message edit box is open; drop there.
|
// A message edit box is open; drop there.
|
||||||
const row_id = rows.get_message_id($last_drag_drop_edit_container[0]);
|
const row_id = rows.get_message_id($last_drag_drop_edit_container[0]);
|
||||||
const $drag_drop_container = get_item("drag_drop_container", {
|
const $drag_drop_container = get_item("drag_drop_container", edit_config(row_id));
|
||||||
mode: "edit",
|
|
||||||
row: row_id,
|
|
||||||
});
|
|
||||||
if (!$drag_drop_container.closest("html").length) {
|
if (!$drag_drop_container.closest("html").length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const edit_upload_object = upload_objects_by_message_edit_row.get(row_id);
|
const edit_upload_object = upload_objects_by_message_edit_row.get(row_id);
|
||||||
|
|
||||||
upload_files(edit_upload_object, {mode: "edit", row: row_id}, files);
|
upload_files(edit_upload_object, edit_config(row_id), files);
|
||||||
} else if (message_lists.current?.selected_message()) {
|
} else if (message_lists.current?.selected_message()) {
|
||||||
// Start a reply to selected message, if viewing a message feed.
|
// Start a reply to selected message, if viewing a message feed.
|
||||||
compose_reply.respond_to_message({
|
compose_reply.respond_to_message({
|
||||||
trigger: "drag_drop_file",
|
trigger: "drag_drop_file",
|
||||||
keep_composebox_empty: true,
|
keep_composebox_empty: true,
|
||||||
});
|
});
|
||||||
upload_files(compose_upload_object, {mode: "compose"}, files);
|
upload_files(compose_upload_object, compose_config, files);
|
||||||
} else {
|
} else {
|
||||||
// Start a new message in other views.
|
// Start a new message in other views.
|
||||||
compose_actions.start({
|
compose_actions.start({
|
||||||
@@ -552,7 +558,7 @@ export function initialize() {
|
|||||||
trigger: "drag_drop_file",
|
trigger: "drag_drop_file",
|
||||||
keep_composebox_empty: true,
|
keep_composebox_empty: true,
|
||||||
});
|
});
|
||||||
upload_files(compose_upload_object, {mode: "compose"}, files);
|
upload_files(compose_upload_object, compose_config, files);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,32 +47,35 @@ test("feature_check", ({override}) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("get_item", () => {
|
test("get_item", () => {
|
||||||
assert.equal(upload.get_item("textarea", {mode: "compose"}), $("textarea#compose-textarea"));
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_message", {mode: "compose"}, "id_1"),
|
upload.get_item("textarea", upload.compose_config),
|
||||||
|
$("textarea#compose-textarea"),
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
upload.get_item("upload_banner_message", upload.compose_config, "id_1"),
|
||||||
$("#compose_banners .upload_banner.file_id_1 .upload_msg"),
|
$("#compose_banners .upload_banner.file_id_1 .upload_msg"),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_cancel_button", {mode: "compose"}, "id_2"),
|
upload.get_item("upload_banner_cancel_button", upload.compose_config, "id_2"),
|
||||||
$("#compose_banners .upload_banner.file_id_2 .upload_banner_cancel_button"),
|
$("#compose_banners .upload_banner.file_id_2 .upload_banner_cancel_button"),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_hide_button", {mode: "compose"}, "id_2"),
|
upload.get_item("upload_banner_hide_button", upload.compose_config, "id_2"),
|
||||||
$("#compose_banners .upload_banner.file_id_2 .main-view-banner-close-button"),
|
$("#compose_banners .upload_banner.file_id_2 .main-view-banner-close-button"),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("file_input_identifier", {mode: "compose"}),
|
upload.get_item("file_input_identifier", upload.compose_config),
|
||||||
"#compose .file_input",
|
"#compose .file_input",
|
||||||
);
|
);
|
||||||
assert.equal(upload.get_item("source", {mode: "compose"}), "compose-file-input");
|
assert.equal(upload.get_item("source", upload.compose_config), "compose-file-input");
|
||||||
assert.equal(upload.get_item("drag_drop_container", {mode: "compose"}), $("#compose"));
|
assert.equal(upload.get_item("drag_drop_container", upload.compose_config), $("#compose"));
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("markdown_preview_hide_button", {mode: "compose"}),
|
upload.get_item("markdown_preview_hide_button", upload.compose_config),
|
||||||
$("#compose .undo_markdown_preview"),
|
$("#compose .undo_markdown_preview"),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("textarea", {mode: "edit", row: 1}),
|
upload.get_item("textarea", upload.edit_config(1)),
|
||||||
$(`#edit_form_${CSS.escape(1)} .message_edit_content`),
|
$(`#edit_form_${CSS.escape(1)} .message_edit_content`),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -80,14 +83,14 @@ test("get_item", () => {
|
|||||||
".message_edit_save",
|
".message_edit_save",
|
||||||
$(".message_edit_save"),
|
$(".message_edit_save"),
|
||||||
);
|
);
|
||||||
assert.equal(upload.get_item("send_button", {mode: "edit", row: 2}), $(".message_edit_save"));
|
assert.equal(upload.get_item("send_button", upload.edit_config(2)), $(".message_edit_save"));
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_identifier", {mode: "edit", row: 11}, "id_3"),
|
upload.get_item("upload_banner_identifier", upload.edit_config(11), "id_3"),
|
||||||
`#edit_form_${CSS.escape(11)} .upload_banner.file_id_3`,
|
`#edit_form_${CSS.escape(11)} .upload_banner.file_id_3`,
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner", {mode: "edit", row: 75}, "id_60"),
|
upload.get_item("upload_banner", upload.edit_config(75), "id_60"),
|
||||||
$(`#edit_form_${CSS.escape(75)} .upload_banner.file_id_60`),
|
$(`#edit_form_${CSS.escape(75)} .upload_banner.file_id_60`),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -96,7 +99,7 @@ test("get_item", () => {
|
|||||||
$(".upload_banner_cancel_button"),
|
$(".upload_banner_cancel_button"),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_cancel_button", {mode: "edit", row: 2}, "id_34"),
|
upload.get_item("upload_banner_cancel_button", upload.edit_config(2), "id_34"),
|
||||||
$(`#edit_form_${CSS.escape(2)} .upload_banner.file_id_34 .upload_banner_cancel_button`),
|
$(`#edit_form_${CSS.escape(2)} .upload_banner.file_id_34 .upload_banner_cancel_button`),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -105,7 +108,7 @@ test("get_item", () => {
|
|||||||
$(".main-view-banner-close-button"),
|
$(".main-view-banner-close-button"),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_hide_button", {mode: "edit", row: 2}, "id_34"),
|
upload.get_item("upload_banner_hide_button", upload.edit_config(2), "id_34"),
|
||||||
$(`#edit_form_${CSS.escape(2)} .upload_banner.file_id_34 .main-view-banner-close-button`),
|
$(`#edit_form_${CSS.escape(2)} .upload_banner.file_id_34 .main-view-banner-close-button`),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -114,21 +117,21 @@ test("get_item", () => {
|
|||||||
$(".upload_msg"),
|
$(".upload_msg"),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("upload_banner_message", {mode: "edit", row: 22}, "id_234"),
|
upload.get_item("upload_banner_message", upload.edit_config(22), "id_234"),
|
||||||
$(`#edit_form_${CSS.escape(22)} .upload_banner.file_id_234 .upload_msg`),
|
$(`#edit_form_${CSS.escape(22)} .upload_banner.file_id_234 .upload_msg`),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("file_input_identifier", {mode: "edit", row: 123}),
|
upload.get_item("file_input_identifier", upload.edit_config(123)),
|
||||||
`#edit_form_${CSS.escape(123)} .file_input`,
|
`#edit_form_${CSS.escape(123)} .file_input`,
|
||||||
);
|
);
|
||||||
assert.equal(upload.get_item("source", {mode: "edit", row: 123}), "message-edit-file-input");
|
assert.equal(upload.get_item("source", upload.edit_config(123)), "message-edit-file-input");
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("drag_drop_container", {mode: "edit", row: 1}),
|
upload.get_item("drag_drop_container", upload.edit_config(1)),
|
||||||
$(`#message-row-1-${CSS.escape(1)} .message_edit_form`),
|
$(`#message-row-1-${CSS.escape(1)} .message_edit_form`),
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
upload.get_item("markdown_preview_hide_button", {mode: "edit", row: 65}),
|
upload.get_item("markdown_preview_hide_button", upload.edit_config(65)),
|
||||||
$(`#edit_form_${CSS.escape(65)} .undo_markdown_preview`),
|
$(`#edit_form_${CSS.escape(65)} .undo_markdown_preview`),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -143,7 +146,7 @@ test("get_item", () => {
|
|||||||
);
|
);
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => {
|
() => {
|
||||||
upload.get_item("textarea", {mode: "edit"});
|
upload.get_item("textarea", upload.edit_config());
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Error",
|
name: "Error",
|
||||||
@@ -161,7 +164,7 @@ test("get_item", () => {
|
|||||||
);
|
);
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => {
|
() => {
|
||||||
upload.get_item("invalid", {mode: "compose"});
|
upload.get_item("invalid", upload.compose_config);
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Error",
|
name: "Error",
|
||||||
@@ -170,7 +173,7 @@ test("get_item", () => {
|
|||||||
);
|
);
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => {
|
() => {
|
||||||
upload.get_item("invalid", {mode: "edit", row: 20});
|
upload.get_item("invalid", upload.edit_config(20));
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Error",
|
name: "Error",
|
||||||
@@ -193,7 +196,7 @@ test("show_error_message", ({mock_template}) => {
|
|||||||
|
|
||||||
$("#compose-send-button").prop("disabled", true);
|
$("#compose-send-button").prop("disabled", true);
|
||||||
|
|
||||||
upload.show_error_message({mode: "compose"}, "Error message");
|
upload.show_error_message(upload.compose_config, "Error message");
|
||||||
assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));
|
assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));
|
||||||
assert.ok(banner_shown);
|
assert.ok(banner_shown);
|
||||||
|
|
||||||
@@ -203,7 +206,7 @@ test("show_error_message", ({mock_template}) => {
|
|||||||
banner_shown = true;
|
banner_shown = true;
|
||||||
return "<banner-stub>";
|
return "<banner-stub>";
|
||||||
});
|
});
|
||||||
upload.show_error_message({mode: "compose"});
|
upload.show_error_message(upload.compose_config);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("upload_files", async ({mock_template, override_rewire}) => {
|
test("upload_files", async ({mock_template, override_rewire}) => {
|
||||||
@@ -237,7 +240,7 @@ test("upload_files", async ({mock_template, override_rewire}) => {
|
|||||||
hide_upload_banner_called = true;
|
hide_upload_banner_called = true;
|
||||||
assert.equal(config.mode, "compose");
|
assert.equal(config.mode, "compose");
|
||||||
});
|
});
|
||||||
const config = {mode: "compose"};
|
const config = upload.compose_config;
|
||||||
$(".message-send-controls").removeClass("disabled-message-send-controls");
|
$(".message-send-controls").removeClass("disabled-message-send-controls");
|
||||||
await upload.upload_files(uppy, config, []);
|
await upload.upload_files(uppy, config, []);
|
||||||
assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));
|
assert.ok(!$(".message-send-controls").hasClass("disabled-message-send-controls"));
|
||||||
@@ -382,7 +385,7 @@ test("uppy_config", () => {
|
|||||||
on() {},
|
on() {},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
upload.setup_upload({mode: "compose"});
|
upload.setup_upload(upload.compose_config);
|
||||||
|
|
||||||
assert.equal(uppy_stub_called, true);
|
assert.equal(uppy_stub_called, true);
|
||||||
assert.equal(uppy_set_meta_called, true);
|
assert.equal(uppy_set_meta_called, true);
|
||||||
@@ -390,7 +393,7 @@ test("uppy_config", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("file_input", ({override_rewire}) => {
|
test("file_input", ({override_rewire}) => {
|
||||||
upload.setup_upload({mode: "compose"});
|
upload.setup_upload(upload.compose_config);
|
||||||
|
|
||||||
const change_handler = $("#compose .file_input").get_on_handler("change");
|
const change_handler = $("#compose .file_input").get_on_handler("change");
|
||||||
const files = ["file1", "file2"];
|
const files = ["file1", "file2"];
|
||||||
@@ -412,7 +415,7 @@ test("file_input", ({override_rewire}) => {
|
|||||||
|
|
||||||
test("file_drop", ({override, override_rewire}) => {
|
test("file_drop", ({override, override_rewire}) => {
|
||||||
override(compose_state, "composing", () => false);
|
override(compose_state, "composing", () => false);
|
||||||
upload.setup_upload({mode: "compose"});
|
upload.setup_upload(upload.compose_config);
|
||||||
|
|
||||||
let prevent_default_counter = 0;
|
let prevent_default_counter = 0;
|
||||||
const drag_event = {
|
const drag_event = {
|
||||||
@@ -461,7 +464,7 @@ test("file_drop", ({override, override_rewire}) => {
|
|||||||
|
|
||||||
test("copy_paste", ({override, override_rewire}) => {
|
test("copy_paste", ({override, override_rewire}) => {
|
||||||
override(compose_state, "composing", () => false);
|
override(compose_state, "composing", () => false);
|
||||||
upload.setup_upload({mode: "compose"});
|
upload.setup_upload(upload.compose_config);
|
||||||
|
|
||||||
const paste_handler = $("#compose").get_on_handler("paste");
|
const paste_handler = $("#compose").get_on_handler("paste");
|
||||||
let get_as_file_called = false;
|
let get_as_file_called = false;
|
||||||
@@ -531,7 +534,7 @@ test("uppy_events", ({override_rewire, mock_template}) => {
|
|||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
upload.setup_upload({mode: "compose"});
|
upload.setup_upload(upload.compose_config);
|
||||||
assert.equal(Object.keys(callbacks).length, 5);
|
assert.equal(Object.keys(callbacks).length, 5);
|
||||||
|
|
||||||
const on_upload_success_callback = callbacks["upload-success"];
|
const on_upload_success_callback = callbacks["upload-success"];
|
||||||
@@ -756,7 +759,7 @@ test("main_file_drop_edit_mode", ({override, override_rewire}) => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
upload.setup_upload({mode: "edit", row: 40});
|
upload.setup_upload(upload.edit_config(40));
|
||||||
upload.initialize();
|
upload.initialize();
|
||||||
override(compose_state, "composing", () => false);
|
override(compose_state, "composing", () => false);
|
||||||
let prevent_default_counter = 0;
|
let prevent_default_counter = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user