2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-07-27 14:35:58 +02:00
|
|
|
import _ from "lodash";
|
2021-07-27 14:38:39 +02:00
|
|
|
import tippy from "tippy.js";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
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-07-27 14:35:58 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-02-28 21:33:50 +01:00
|
|
|
import * as ListWidget from "./list_widget";
|
2020-09-24 04:45:12 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
export function DropdownListWidget({
|
2021-03-24 21:44:43 +01:00
|
|
|
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 = () => {},
|
|
|
|
}) {
|
2021-07-27 14:05:22 +02:00
|
|
|
// Initializing values
|
|
|
|
this.widget_name = widget_name;
|
|
|
|
this.data = data;
|
|
|
|
this.default_text = default_text;
|
|
|
|
this.render_text = render_text;
|
|
|
|
this.null_value = null_value;
|
|
|
|
this.include_current_item = include_current_item;
|
|
|
|
this.initial_value = value;
|
|
|
|
this.on_update = on_update;
|
|
|
|
|
|
|
|
this.container_id = `${widget_name}_widget`;
|
|
|
|
this.value_id = `id_${widget_name}`;
|
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
if (value === undefined) {
|
2021-07-27 14:05:22 +02:00
|
|
|
this.initial_value = null_value;
|
2021-03-24 21:44:43 +01:00
|
|
|
blueslip.warn("dropdown-list-widget: Called without a default value; using null value");
|
|
|
|
}
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
// Setting up dropdown_list_widget
|
|
|
|
this.setup();
|
|
|
|
}
|
2021-02-12 23:49:22 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
DropdownListWidget.prototype.render_default_text = function ($elem) {
|
|
|
|
$elem.text(this.default_text);
|
|
|
|
$elem.addClass("text-warning");
|
|
|
|
$elem.closest(".input-group").find(".dropdown_list_reset_button").hide();
|
2021-07-27 14:05:22 +02:00
|
|
|
};
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
DropdownListWidget.prototype.render = function (value) {
|
|
|
|
$(`#${CSS.escape(this.container_id)} #${CSS.escape(this.value_id)}`).data("value", value);
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.widget_name)}_name`);
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
if (!value || value === this.null_value) {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.render_default_text($elem);
|
2021-07-27 14:05:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-02-12 23:49:22 +01:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
// Happy path
|
|
|
|
const item = this.data.find((x) => x.value === value.toString());
|
2021-02-12 23:49:22 +01:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
if (item === undefined) {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.render_default_text($elem);
|
2021-07-27 14:05:22 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
const text = this.render_text(item.name);
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.text(text);
|
|
|
|
$elem.removeClass("text-warning");
|
|
|
|
$elem.closest(".input-group").find(".dropdown_list_reset_button").show();
|
2021-07-27 14:05:22 +02:00
|
|
|
};
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
DropdownListWidget.prototype.update = function (value) {
|
|
|
|
this.render(value);
|
|
|
|
this.on_update(value);
|
|
|
|
};
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
DropdownListWidget.prototype.register_event_handlers = function () {
|
|
|
|
$(`#${CSS.escape(this.container_id)} .dropdown-list-body`).on(
|
|
|
|
"click keypress",
|
|
|
|
".list_item",
|
|
|
|
(e) => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $setting_elem = $(e.currentTarget).closest(
|
2021-07-27 14:05:22 +02:00
|
|
|
`.${CSS.escape(this.widget_name)}_setting`,
|
|
|
|
);
|
|
|
|
if (e.type === "keypress") {
|
|
|
|
if (e.key === "Enter") {
|
2022-01-25 11:36:19 +01:00
|
|
|
$setting_elem.find(".dropdown-menu").dropdown("toggle");
|
2021-07-27 14:05:22 +02:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-28 15:24:00 +02:00
|
|
|
}
|
2021-07-27 14:05:22 +02:00
|
|
|
const value = $(e.currentTarget).attr("data-value");
|
|
|
|
this.update(value);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
$(`#${CSS.escape(this.container_id)} .dropdown_list_reset_button`).on("click", (e) => {
|
|
|
|
this.update(this.null_value);
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
2021-03-28 15:24:00 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
DropdownListWidget.prototype.setup_dropdown_widget = function (data) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_list_body = $(
|
2021-07-27 14:05:22 +02:00
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-list-body`,
|
|
|
|
).expectOne();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
2021-07-27 14:05:22 +02:00
|
|
|
const get_data = () => {
|
|
|
|
if (this.include_current_item) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
return data.filter((x) => x.value !== this.value.toString());
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($dropdown_list_body, get_data(data), {
|
2021-07-27 14:05:22 +02:00
|
|
|
name: `${CSS.escape(this.widget_name)}_list`,
|
|
|
|
modifier(item) {
|
|
|
|
return render_dropdown_list({item});
|
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $search_input,
|
2021-07-27 14:05:22 +02:00
|
|
|
predicate(item, value) {
|
|
|
|
return item.name.toLowerCase().includes(value);
|
2020-04-15 04:29:15 +02:00
|
|
|
},
|
2021-07-27 14:05:22 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $(`#${CSS.escape(this.container_id)} .dropdown-list-wrapper`),
|
2021-07-27 14:05:22 +02:00
|
|
|
});
|
|
|
|
};
|
2020-04-15 22:55:05 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
// Sets the focus to the ListWidget input once the dropdown button is clicked.
|
|
|
|
DropdownListWidget.prototype.dropdown_toggle_click_handler = function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_toggle = $(`#${CSS.escape(this.container_id)} .dropdown-toggle`);
|
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
2020-04-15 04:29:15 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$dropdown_toggle.on("click", () => {
|
|
|
|
$search_input.val("").trigger("input");
|
2021-07-27 14:05:22 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-07-27 14:09:37 +02:00
|
|
|
DropdownListWidget.prototype.dropdown_focus_events = function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
|
|
|
const $dropdown_menu = $(`.${CSS.escape(this.widget_name)}_setting .dropdown-menu`);
|
2021-07-27 14:09:37 +02:00
|
|
|
|
|
|
|
const dropdown_elements = () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_list_body = $(
|
2021-07-27 14:09:37 +02:00
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-list-body`,
|
|
|
|
).expectOne();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $dropdown_list_body.children().find("a");
|
2021-07-27 14:09:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Rest of the key handlers are supported by our
|
|
|
|
// bootstrap library.
|
2022-01-25 11:36:19 +01:00
|
|
|
$dropdown_menu.on("keydown", (e) => {
|
|
|
|
function trigger_element_focus($element) {
|
2021-07-27 14:09:37 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.trigger("focus");
|
2021-07-27 14:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (e.key) {
|
|
|
|
case "ArrowDown": {
|
|
|
|
switch (e.target) {
|
|
|
|
case dropdown_elements().last()[0]:
|
2022-01-25 11:36:19 +01:00
|
|
|
trigger_element_focus($search_input);
|
2021-07-27 14:09:37 +02:00
|
|
|
break;
|
2022-01-25 11:36:19 +01:00
|
|
|
case $search_input[0]:
|
2021-07-27 14:09:37 +02:00
|
|
|
trigger_element_focus(dropdown_elements().first());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ArrowUp": {
|
|
|
|
switch (e.target) {
|
|
|
|
case dropdown_elements().first()[0]:
|
2022-01-25 11:36:19 +01:00
|
|
|
trigger_element_focus($search_input);
|
2021-07-27 14:09:37 +02:00
|
|
|
break;
|
2022-01-25 11:36:19 +01:00
|
|
|
case $search_input[0]:
|
2021-07-27 14:09:37 +02:00
|
|
|
trigger_element_focus(dropdown_elements().last());
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Tab": {
|
|
|
|
switch (e.target) {
|
2022-01-25 11:36:19 +01:00
|
|
|
case $search_input[0]:
|
2021-07-27 14:09:37 +02:00
|
|
|
trigger_element_focus(dropdown_elements().first());
|
|
|
|
break;
|
|
|
|
case dropdown_elements().last()[0]:
|
2022-01-25 11:36:19 +01:00
|
|
|
trigger_element_focus($search_input);
|
2021-07-27 14:09:37 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
DropdownListWidget.prototype.setup = function () {
|
|
|
|
// populate the dropdown
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_list_body = $(
|
2021-07-27 14:05:22 +02:00
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-list-body`,
|
|
|
|
).expectOne();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
|
|
|
const $dropdown_toggle = $(`#${CSS.escape(this.container_id)} .dropdown-toggle`);
|
2021-07-27 14:05:22 +02:00
|
|
|
|
|
|
|
this.setup_dropdown_widget(this.data);
|
|
|
|
|
|
|
|
$(`#${CSS.escape(this.container_id)} .dropdown-search`).on("click", (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.dropdown_toggle_click_handler();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$dropdown_toggle.on("focus", (e) => {
|
2021-07-27 14:05:22 +02:00
|
|
|
// On opening a Bootstrap Dropdown, the parent element receives focus.
|
|
|
|
// Here, we want our search input to have focus instead.
|
|
|
|
e.preventDefault();
|
|
|
|
// 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.
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($dropdown_list_body.is(":visible")) {
|
|
|
|
$search_input.trigger("focus");
|
2020-04-14 10:43:30 +02:00
|
|
|
}
|
2021-07-27 14:05:22 +02:00
|
|
|
});
|
2020-04-14 10:43:30 +02:00
|
|
|
|
2021-07-27 14:09:37 +02:00
|
|
|
this.dropdown_focus_events();
|
2021-07-27 14:05:22 +02:00
|
|
|
|
|
|
|
this.render(this.initial_value);
|
|
|
|
this.register_event_handlers();
|
|
|
|
};
|
2020-04-15 04:29:15 +02:00
|
|
|
|
2021-07-27 14:05:22 +02:00
|
|
|
// Returns the updated value
|
|
|
|
DropdownListWidget.prototype.value = function () {
|
|
|
|
let val = $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.value_id)}`).data("value");
|
|
|
|
if (val === null) {
|
|
|
|
val = "";
|
|
|
|
}
|
|
|
|
return val;
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
export function MultiSelectDropdownListWidget({
|
|
|
|
widget_name,
|
|
|
|
data,
|
|
|
|
default_text,
|
|
|
|
null_value = null,
|
|
|
|
on_update = () => {},
|
|
|
|
on_close,
|
|
|
|
value,
|
|
|
|
limit,
|
|
|
|
}) {
|
|
|
|
// A widget mostly similar to `DropdownListWidget` but
|
|
|
|
// used in cases of multiple dropdown selection.
|
|
|
|
|
|
|
|
// Initializing values specific to `MultiSelectDropdownListWidget`.
|
|
|
|
this.limit = limit;
|
|
|
|
this.on_close = on_close;
|
|
|
|
|
|
|
|
// Important thing to note is that this needs to be maintained as
|
|
|
|
// a reference type and not to deep clone it/assign it to a
|
|
|
|
// different variable, so that it can be later referenced within
|
|
|
|
// `list_widget` as well. The way we manage dropdown elements are
|
|
|
|
// essentially by just modifying the values in `data_selected` variable.
|
|
|
|
this.data_selected = []; // Populate the dropdown values selected by user.
|
|
|
|
|
|
|
|
DropdownListWidget.call(this, {
|
|
|
|
widget_name,
|
|
|
|
data,
|
|
|
|
default_text,
|
|
|
|
null_value,
|
|
|
|
on_update,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (limit === undefined) {
|
|
|
|
this.limit = 2;
|
|
|
|
blueslip.warn(
|
|
|
|
"Multiselect dropdown-list-widget: Called without limit value; using 2 as the limit",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.initialize_dropdown_values();
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiSelectDropdownListWidget.prototype = Object.create(DropdownListWidget.prototype);
|
|
|
|
|
|
|
|
MultiSelectDropdownListWidget.prototype.initialize_dropdown_values = function () {
|
|
|
|
// Stop the execution if value parameter is undefined and null_value is passed.
|
|
|
|
if (!this.initial_value || this.initial_value === this.null_value) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.widget_name)}_name`);
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
// Push values from initial valued array to `data_selected`.
|
|
|
|
this.data_selected.push(...this.initial_value);
|
2022-01-25 11:36:19 +01:00
|
|
|
this.render_button_text($elem, this.limit);
|
2021-07-27 14:35:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set the button text as per the selected data.
|
2022-01-25 11:36:19 +01:00
|
|
|
MultiSelectDropdownListWidget.prototype.render_button_text = function ($elem, limit) {
|
2021-07-27 14:35:58 +02:00
|
|
|
const items_selected = this.data_selected.length;
|
|
|
|
let text = "";
|
|
|
|
|
2021-07-27 14:38:39 +02:00
|
|
|
// Destroy the tooltip once the button text reloads.
|
|
|
|
this.destroy_tooltip();
|
|
|
|
|
2021-07-27 14:35:58 +02:00
|
|
|
if (items_selected === 0) {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.render_default_text($elem);
|
2021-07-27 14:35:58 +02:00
|
|
|
return;
|
|
|
|
} else if (limit >= items_selected) {
|
|
|
|
const data_selected = this.data.filter((data) => this.data_selected.includes(data.value));
|
|
|
|
text = data_selected.map((data) => data.name).toString();
|
|
|
|
} else {
|
|
|
|
text = $t({defaultMessage: "{items_selected} selected"}, {items_selected});
|
2021-07-27 14:38:39 +02:00
|
|
|
this.render_tooltip();
|
2021-07-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.text(text);
|
|
|
|
$elem.removeClass("text-warning");
|
|
|
|
$elem.closest(".input-group").find(".dropdown_list_reset_button").show();
|
2021-07-27 14:35:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Override the DrodownListWidget `render` function.
|
|
|
|
MultiSelectDropdownListWidget.prototype.render = function (value) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(`#${CSS.escape(this.container_id)} #${CSS.escape(this.widget_name)}_name`);
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
if (!value || value === this.null_value) {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.render_default_text($elem);
|
2021-07-27 14:35:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
this.render_button_text($elem, this.limit);
|
2021-07-27 14:35:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
MultiSelectDropdownListWidget.prototype.dropdown_toggle_click_handler = function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_toggle = $(`#${CSS.escape(this.container_id)} .dropdown-toggle`);
|
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
2021-07-27 14:35:58 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$dropdown_toggle.on("click", () => {
|
2021-07-27 14:35:58 +02:00
|
|
|
this.reset_dropdown_items();
|
2022-01-25 11:36:19 +01:00
|
|
|
$search_input.val("").trigger("input");
|
2021-07-27 14:35:58 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Cases where a user presses any dropdown item but accidentally closes
|
|
|
|
// the dropdown list.
|
|
|
|
MultiSelectDropdownListWidget.prototype.reset_dropdown_items = function () {
|
|
|
|
// Clear the data selected and stop the execution once the user has
|
|
|
|
// pressed the `reset` button.
|
|
|
|
if (this.is_reset) {
|
|
|
|
this.data_selected.splice(0, this.data_selected.length);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const original_items = this.checked_items ? this.checked_items : this.initial_value;
|
|
|
|
const items_added = _.difference(this.data_selected, original_items);
|
|
|
|
|
|
|
|
// Removing the unnecessary items from dropdown.
|
|
|
|
for (const val of items_added) {
|
|
|
|
const index = this.data_selected.indexOf(val);
|
|
|
|
if (index > -1) {
|
|
|
|
this.data_selected.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Items that are removed in dropdown but should have been a part of it
|
|
|
|
const items_removed = _.difference(original_items, this.data_selected);
|
|
|
|
this.data_selected.push(...items_removed);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Override the DrodownListWidget `setup_dropdown_widget` function.
|
|
|
|
MultiSelectDropdownListWidget.prototype.setup_dropdown_widget = function (data) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_list_body = $(
|
2021-07-27 14:35:58 +02:00
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-list-body`,
|
|
|
|
).expectOne();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
2021-07-27 14:35:58 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($dropdown_list_body, data, {
|
2021-07-27 14:35:58 +02:00
|
|
|
name: `${CSS.escape(this.widget_name)}_list`,
|
|
|
|
modifier(item) {
|
|
|
|
return render_dropdown_list({item});
|
|
|
|
},
|
|
|
|
multiselect: {
|
|
|
|
selected_items: this.data_selected,
|
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $search_input,
|
2021-07-27 14:35:58 +02:00
|
|
|
predicate(item, value) {
|
|
|
|
return item.name.toLowerCase().includes(value);
|
|
|
|
},
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $(`#${CSS.escape(this.container_id)} .dropdown-list-wrapper`),
|
2021-07-27 14:35:58 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add the check mark to dropdown element passed.
|
2022-01-25 11:36:19 +01:00
|
|
|
MultiSelectDropdownListWidget.prototype.add_check_mark = function ($element) {
|
|
|
|
const value = $element.attr("data-value");
|
|
|
|
const $link_elem = $element.find("a").expectOne();
|
|
|
|
$link_elem.prepend($("<i>", {class: "fa fa-check"}));
|
|
|
|
$element.addClass("checked");
|
2021-07-27 14:35:58 +02:00
|
|
|
this.data_selected.push(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove the check mark from dropdown element.
|
2022-01-25 11:36:19 +01:00
|
|
|
MultiSelectDropdownListWidget.prototype.remove_check_mark = function ($element) {
|
|
|
|
const $icon = $element.find("i").expectOne();
|
|
|
|
const value = $element.attr("data-value");
|
2021-07-27 14:35:58 +02:00
|
|
|
const index = this.data_selected.indexOf(value);
|
|
|
|
|
|
|
|
if (index > -1) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$icon.remove();
|
|
|
|
$element.removeClass("checked");
|
2021-07-27 14:35:58 +02:00
|
|
|
this.data_selected.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-27 14:38:39 +02:00
|
|
|
// Render the tooltip once the text changes to `n` selected.
|
|
|
|
MultiSelectDropdownListWidget.prototype.render_tooltip = function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(`#${CSS.escape(this.container_id)}`);
|
2021-07-27 14:38:39 +02:00
|
|
|
const selected_items = this.data.filter((item) => this.checked_items.includes(item.value));
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
tippy($elem[0], {
|
2021-07-27 14:38:39 +02:00
|
|
|
content: selected_items.map((item) => item.name).join(", "),
|
|
|
|
placement: "top",
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MultiSelectDropdownListWidget.prototype.destroy_tooltip = function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(`#${CSS.escape(this.container_id)}`);
|
|
|
|
const tippy_instance = $elem[0]._tippy;
|
2021-07-27 14:38:39 +02:00
|
|
|
if (!tippy_instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tippy_instance.destroy();
|
|
|
|
};
|
|
|
|
|
2021-07-27 14:35:58 +02:00
|
|
|
MultiSelectDropdownListWidget.prototype.dropdown_focus_events = function () {
|
|
|
|
// Main keydown event handler which transfers the focus from one child element
|
|
|
|
// to another.
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $search_input = $(
|
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-search > input[type=text]`,
|
|
|
|
);
|
|
|
|
const $dropdown_menu = $(`.${CSS.escape(this.widget_name)}_setting .dropdown-menu`);
|
|
|
|
const $filter_button = $(`#${CSS.escape(this.container_id)} .multiselect_btn`);
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
const dropdown_elements = () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_list_body = $(
|
2021-07-27 14:35:58 +02:00
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-list-body`,
|
|
|
|
).expectOne();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $dropdown_list_body.children().find("a");
|
2021-07-27 14:35:58 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$dropdown_menu.on("keydown", (e) => {
|
|
|
|
function trigger_element_focus($element) {
|
2021-07-27 14:35:58 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.trigger("focus");
|
2021-07-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (e.key) {
|
|
|
|
case "ArrowDown": {
|
|
|
|
switch (e.target) {
|
|
|
|
case dropdown_elements().last()[0]:
|
2022-01-25 11:36:19 +01:00
|
|
|
trigger_element_focus($filter_button);
|
2021-07-27 14:35:58 +02:00
|
|
|
break;
|
|
|
|
case $(`#${CSS.escape(this.container_id)} .multiselect_btn`)[0]:
|
2022-01-25 11:36:19 +01:00
|
|
|
trigger_element_focus($search_input);
|
2021-07-27 14:35:58 +02:00
|
|
|
break;
|
2022-01-25 11:36:19 +01:00
|
|
|
case $search_input[0]:
|
2021-07-27 14:35:58 +02:00
|
|
|
trigger_element_focus(dropdown_elements().first());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ArrowUp": {
|
|
|
|
switch (e.target) {
|
|
|
|
case dropdown_elements().first()[0]:
|
2022-01-25 11:36:19 +01:00
|
|
|
trigger_element_focus($search_input);
|
2021-07-27 14:35:58 +02:00
|
|
|
break;
|
2022-01-25 11:36:19 +01:00
|
|
|
case $search_input[0]:
|
|
|
|
trigger_element_focus($filter_button);
|
2021-07-27 14:35:58 +02:00
|
|
|
break;
|
|
|
|
case $(`#${CSS.escape(this.container_id)} .multiselect_btn`)[0]:
|
|
|
|
trigger_element_focus(dropdown_elements().last());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "Tab": {
|
|
|
|
switch (e.target) {
|
2022-01-25 11:36:19 +01:00
|
|
|
case $search_input[0]:
|
2021-07-27 14:35:58 +02:00
|
|
|
trigger_element_focus(dropdown_elements().first());
|
|
|
|
break;
|
2022-01-25 11:36:19 +01:00
|
|
|
case $filter_button[0]:
|
|
|
|
trigger_element_focus($search_input);
|
2021-07-27 14:35:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Override the `register_event_handlers` function.
|
|
|
|
MultiSelectDropdownListWidget.prototype.register_event_handlers = function () {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $dropdown_list_body = $(
|
2021-07-27 14:35:58 +02:00
|
|
|
`#${CSS.escape(this.container_id)} .dropdown-list-body`,
|
|
|
|
).expectOne();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$dropdown_list_body.on("click keypress", ".list_item", (e) => {
|
2021-07-27 14:35:58 +02:00
|
|
|
if (e.type === "keypress" && e.key !== "Enter") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $element = $(e.target.closest("li"));
|
|
|
|
if ($element.hasClass("checked")) {
|
|
|
|
this.remove_check_mark($element);
|
2021-07-27 14:35:58 +02:00
|
|
|
} else {
|
2022-01-25 11:36:19 +01:00
|
|
|
this.add_check_mark($element);
|
2021-07-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(`#${CSS.escape(this.container_id)} .dropdown_list_reset_button`).on("click", (e) => {
|
|
|
|
// Default back the values.
|
|
|
|
this.is_reset = true;
|
|
|
|
this.checked_items = undefined;
|
|
|
|
|
|
|
|
this.update(this.null_value);
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(`#${CSS.escape(this.container_id)} .multiselect_btn`).on("click", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// Set the value to `false` to end the scope of the
|
|
|
|
// `reset` button.
|
|
|
|
this.is_reset = false;
|
|
|
|
// We deep clone the values of `data_selected` to a new
|
|
|
|
// variable. This is so because arrays are reference types
|
|
|
|
// and modifying the parent array can change the values
|
|
|
|
// within the child array. Here, `checked_items` copies over the
|
|
|
|
// value and not just the reference.
|
|
|
|
this.checked_items = _.cloneDeep(this.data_selected);
|
|
|
|
this.update(this.data_selected);
|
|
|
|
|
|
|
|
// Cases when the user wants to pass a successful event after
|
|
|
|
// the dropdown is closed.
|
|
|
|
if (this.on_close) {
|
|
|
|
e.stopPropagation();
|
2022-01-25 11:36:19 +01:00
|
|
|
const $setting_elem = $(e.currentTarget).closest(
|
2021-07-27 14:35:58 +02:00
|
|
|
`.${CSS.escape(this.widget_name)}_setting`,
|
|
|
|
);
|
2022-01-25 11:36:19 +01:00
|
|
|
$setting_elem.find(".dropdown-menu").dropdown("toggle");
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
this.on_close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns array of values selected by user.
|
|
|
|
MultiSelectDropdownListWidget.prototype.value = function () {
|
|
|
|
let val = this.checked_items;
|
|
|
|
// Cases taken care of -
|
|
|
|
// - User never pressed the filter button -> We return the initial value.
|
|
|
|
// - User pressed the `reset` button -> We return an empty array.
|
|
|
|
if (val === undefined) {
|
|
|
|
val = this.is_reset ? [] : this.initial_value;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
};
|