2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 21:33:50 +01:00
|
|
|
import render_dropdown_list from "../templates/settings/dropdown_list.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 21:33:50 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
2020-09-24 04:45:12 +02:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
export const DropdownListWidget = function ({
|
|
|
|
widget_name,
|
|
|
|
data,
|
|
|
|
default_text,
|
|
|
|
render_text = (item_name) => item_name,
|
|
|
|
null_value = null,
|
2021-03-28 15:24:00 +02:00
|
|
|
include_current_item = true,
|
2021-03-24 21:44:43 +01:00
|
|
|
value,
|
|
|
|
on_update = () => {},
|
|
|
|
}) {
|
|
|
|
const container_id = `${widget_name}_widget`;
|
|
|
|
const value_id = `id_${widget_name}`;
|
|
|
|
if (value === undefined) {
|
|
|
|
value = null_value;
|
|
|
|
blueslip.warn("dropdown-list-widget: Called without a default value; using null value");
|
|
|
|
}
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-02-12 23:49:22 +01:00
|
|
|
const render_default_text = (elem) => {
|
2021-03-24 21:44:43 +01:00
|
|
|
elem.text(default_text);
|
2021-02-12 23:49:22 +01:00
|
|
|
elem.addClass("text-warning");
|
|
|
|
elem.closest(".input-group").find(".dropdown_list_reset_button:enabled").hide();
|
|
|
|
};
|
|
|
|
|
2020-04-14 10:43:30 +02:00
|
|
|
const render = (value) => {
|
2021-03-24 21:44:43 +01:00
|
|
|
$(`#${CSS.escape(container_id)} #${CSS.escape(value_id)}`).data("value", value);
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
const elem = $(`#${CSS.escape(container_id)} #${CSS.escape(widget_name)}_name`);
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
if (!value || value === null_value) {
|
2021-02-12 23:49:22 +01:00
|
|
|
render_default_text(elem);
|
2020-04-14 10:43:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Happy path
|
2021-03-24 21:44:43 +01:00
|
|
|
const item = data.find((x) => x.value === value.toString());
|
2021-02-12 23:49:22 +01:00
|
|
|
|
|
|
|
if (item === undefined) {
|
|
|
|
render_default_text(elem);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
const text = render_text(item.name);
|
2020-04-14 10:43:30 +02:00
|
|
|
elem.text(text);
|
2020-07-15 01:29:15 +02:00
|
|
|
elem.removeClass("text-warning");
|
2020-10-30 00:26:54 +01:00
|
|
|
elem.closest(".input-group").find(".dropdown_list_reset_button:enabled").show();
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const update = (value) => {
|
|
|
|
render(value);
|
2021-03-24 21:44:43 +01:00
|
|
|
on_update(value);
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const register_event_handlers = () => {
|
2021-03-24 21:44:43 +01:00
|
|
|
$(`#${CSS.escape(container_id)} .dropdown-list-body`).on(
|
2020-12-09 23:43:53 +01:00
|
|
|
"click keypress",
|
|
|
|
".list_item",
|
|
|
|
function (e) {
|
2021-03-24 21:44:43 +01:00
|
|
|
const setting_elem = $(this).closest(`.${CSS.escape(widget_name)}_setting`);
|
2020-12-09 23:43:53 +01:00
|
|
|
if (e.type === "keypress") {
|
|
|
|
if (e.which === 13) {
|
|
|
|
setting_elem.find(".dropdown-menu").dropdown("toggle");
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-14 10:43:30 +02:00
|
|
|
}
|
2020-12-09 23:43:53 +01:00
|
|
|
const value = $(this).attr("data-value");
|
|
|
|
update(value);
|
|
|
|
},
|
|
|
|
);
|
2021-03-24 21:44:43 +01:00
|
|
|
$(`#${CSS.escape(container_id)} .dropdown_list_reset_button`).on("click", (e) => {
|
|
|
|
update(null_value);
|
2020-05-19 07:01:53 +02:00
|
|
|
e.preventDefault();
|
2020-04-14 10:43:30 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-04-15 04:29:15 +02:00
|
|
|
const setup = () => {
|
|
|
|
// populate the dropdown
|
2021-02-03 23:23:32 +01:00
|
|
|
const dropdown_list_body = $(
|
2021-03-24 21:44:43 +01:00
|
|
|
`#${CSS.escape(container_id)} .dropdown-list-body`,
|
2021-02-03 23:23:32 +01:00
|
|
|
).expectOne();
|
2021-03-24 21:44:43 +01:00
|
|
|
const search_input = $(`#${CSS.escape(container_id)} .dropdown-search > input[type=text]`);
|
|
|
|
const dropdown_toggle = $(`#${CSS.escape(container_id)} .dropdown-toggle`);
|
2021-03-28 15:24:00 +02:00
|
|
|
const get_data = (data) => {
|
|
|
|
if (include_current_item) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
return data.filter((x) => x.value !== value.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
ListWidget.create(dropdown_list_body, get_data(data), {
|
2021-03-24 21:44:43 +01:00
|
|
|
name: `${CSS.escape(widget_name)}_list`,
|
2020-07-20 22:18:43 +02:00
|
|
|
modifier(item) {
|
|
|
|
return render_dropdown_list({item});
|
2020-04-15 04:29:15 +02:00
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: search_input,
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate(item, value) {
|
2020-04-15 04:29:15 +02:00
|
|
|
return item.name.toLowerCase().includes(value);
|
|
|
|
},
|
|
|
|
},
|
2021-03-24 21:44:43 +01:00
|
|
|
simplebar_container: $(`#${CSS.escape(container_id)} .dropdown-list-wrapper`),
|
2020-04-15 04:29:15 +02:00
|
|
|
});
|
2021-03-24 21:44:43 +01:00
|
|
|
$(`#${CSS.escape(container_id)} .dropdown-search`).on("click", (e) => {
|
2020-04-15 04:29:15 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
dropdown_toggle.on("click", () => {
|
2020-04-15 04:29:15 +02:00
|
|
|
search_input.val("").trigger("input");
|
|
|
|
});
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
dropdown_toggle.on("focus", (e) => {
|
2020-08-11 01:47:44 +02:00
|
|
|
// On opening a Bootstrap Dropdown, the parent element receives focus.
|
2020-04-15 22:55:05 +02:00
|
|
|
// Here, we want our search input to have focus instead.
|
|
|
|
e.preventDefault();
|
2021-03-30 19:24:52 +02:00
|
|
|
// This function gets called twice when focusing the
|
|
|
|
// dropdown, and only in the second call is the input
|
|
|
|
// field visible in the DOM; so the following visibility
|
|
|
|
// check ensures we wait for the second call to focus.
|
|
|
|
if (dropdown_list_body.is(":visible")) {
|
|
|
|
search_input.trigger("focus");
|
|
|
|
}
|
2020-04-15 22:55:05 +02:00
|
|
|
});
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
search_input.on("keydown", (e) => {
|
2020-04-15 22:55:05 +02:00
|
|
|
if (!/(38|40|27)/.test(e.keyCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
2021-03-11 05:43:45 +01:00
|
|
|
const custom_event = new $.Event("keydown.dropdown.data-api", {
|
2020-04-15 22:55:05 +02:00
|
|
|
keyCode: e.keyCode,
|
|
|
|
which: e.keyCode,
|
|
|
|
});
|
|
|
|
dropdown_toggle.trigger(custom_event);
|
|
|
|
});
|
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
render(value);
|
2020-04-15 04:29:15 +02:00
|
|
|
register_event_handlers();
|
|
|
|
};
|
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
const get_value = () => {
|
|
|
|
let val = $(`#${CSS.escape(container_id)} #${CSS.escape(value_id)}`).data("value");
|
2020-04-14 10:43:30 +02:00
|
|
|
if (val === null) {
|
2020-07-15 01:29:15 +02:00
|
|
|
val = "";
|
2020-04-14 10:43:30 +02:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
};
|
|
|
|
|
2020-04-15 04:29:15 +02:00
|
|
|
// Run setup() automatically on initialization.
|
|
|
|
setup();
|
|
|
|
|
2020-04-14 10:43:30 +02:00
|
|
|
return {
|
|
|
|
render,
|
2021-03-24 21:44:43 +01:00
|
|
|
value: get_value,
|
2020-04-30 16:27:42 +02:00
|
|
|
update,
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
|
|
|
};
|