2020-04-14 10:43:30 +02:00
|
|
|
const DropdownListWidget = function (opts) {
|
2020-05-19 00:16:34 +02:00
|
|
|
const init = () => {
|
|
|
|
// Run basic sanity checks on opts, and set up sane defaults.
|
|
|
|
opts = Object.assign({
|
|
|
|
null_value: null,
|
|
|
|
render_text: (item_name) => item_name,
|
|
|
|
on_update: () => {},
|
|
|
|
}, opts);
|
|
|
|
opts.container_id = `${opts.widget_name}_widget`;
|
|
|
|
opts.value_id = `id_${opts.widget_name}`;
|
|
|
|
if (opts.value === undefined) {
|
|
|
|
opts.value = opts.null_value;
|
|
|
|
blueslip.warn('dropdown-list-widget: Called without a default value; using null value');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
init();
|
2020-04-14 10:43:30 +02:00
|
|
|
|
|
|
|
const render_dropdown_list = require("../templates/settings/dropdown_list.hbs");
|
|
|
|
|
|
|
|
const render = (value) => {
|
|
|
|
$(`#${opts.container_id} #${opts.value_id}`).data("value", value);
|
|
|
|
|
2020-05-18 23:08:30 +02:00
|
|
|
const elem = $(`#${opts.container_id} #${opts.widget_name}_name`);
|
2020-04-14 10:43:30 +02:00
|
|
|
|
|
|
|
if (!value || value === opts.null_value) {
|
|
|
|
elem.text(opts.default_text);
|
|
|
|
elem.addClass("text-warning");
|
2020-05-19 07:01:53 +02:00
|
|
|
elem.closest('.input-group').find('.dropdown_list_reset_button:not([disabled])').hide();
|
2020-04-14 10:43:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Happy path
|
|
|
|
const item = opts.data.find(x => x.value === value.toString());
|
|
|
|
const text = opts.render_text(item.name);
|
|
|
|
elem.text(text);
|
|
|
|
elem.removeClass('text-warning');
|
2020-05-19 07:01:53 +02:00
|
|
|
elem.closest('.input-group').find('.dropdown_list_reset_button:not([disabled])').show();
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const update = (value) => {
|
|
|
|
render(value);
|
2020-04-30 13:09:48 +02:00
|
|
|
opts.on_update(value);
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const register_event_handlers = () => {
|
|
|
|
$(`#${opts.container_id} .dropdown-list-body`).on("click keypress", ".list_item", function (e) {
|
2020-05-18 23:08:30 +02:00
|
|
|
const setting_elem = $(this).closest(`.${opts.widget_name}_setting`);
|
2020-04-14 10:43:30 +02:00
|
|
|
if (e.type === "keypress") {
|
|
|
|
if (e.which === 13) {
|
|
|
|
setting_elem.find(".dropdown-menu").dropdown("toggle");
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const value = $(this).attr('data-value');
|
|
|
|
update(value);
|
|
|
|
});
|
2020-05-19 07:01:53 +02:00
|
|
|
$(`#${opts.container_id} .dropdown_list_reset_button`).click(function (e) {
|
2020-04-14 10:43:30 +02:00
|
|
|
update(opts.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
|
|
|
|
const dropdown_list_body = $(`#${opts.container_id} .dropdown-list-body`).expectOne();
|
|
|
|
const search_input = $(`#${opts.container_id} .dropdown-search > input[type=text]`);
|
2020-04-15 22:55:05 +02:00
|
|
|
const dropdown_toggle = $(`#${opts.container_id} .dropdown-toggle`);
|
2020-04-15 04:29:15 +02:00
|
|
|
|
|
|
|
list_render.create(dropdown_list_body, opts.data, {
|
2020-05-18 23:08:30 +02:00
|
|
|
name: `${opts.widget_name}_list`,
|
2020-04-15 04:29:15 +02:00
|
|
|
modifier: function (item) {
|
|
|
|
return render_dropdown_list({ item: item });
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: search_input,
|
|
|
|
predicate: function (item, value) {
|
|
|
|
return item.name.toLowerCase().includes(value);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
$(`#${opts.container_id} .dropdown-search`).click(function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2020-04-15 22:55:05 +02:00
|
|
|
dropdown_toggle.click(function () {
|
2020-04-15 04:29:15 +02:00
|
|
|
search_input.val("").trigger("input");
|
|
|
|
});
|
|
|
|
|
2020-04-15 22:55:05 +02:00
|
|
|
dropdown_toggle.focus(function (e) {
|
|
|
|
// On opening a Bootstrap Dropdown, the parent element recieves focus.
|
|
|
|
// Here, we want our search input to have focus instead.
|
|
|
|
e.preventDefault();
|
|
|
|
search_input.focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
search_input.keydown(function (e) {
|
|
|
|
if (!/(38|40|27)/.test(e.keyCode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
const custom_event = jQuery.Event("keydown.dropdown.data-api", {
|
|
|
|
keyCode: e.keyCode,
|
|
|
|
which: e.keyCode,
|
|
|
|
});
|
|
|
|
dropdown_toggle.trigger(custom_event);
|
|
|
|
});
|
|
|
|
|
2020-04-30 13:09:48 +02:00
|
|
|
render(opts.value);
|
2020-04-15 04:29:15 +02:00
|
|
|
register_event_handlers();
|
|
|
|
};
|
|
|
|
|
2020-04-14 10:43:30 +02:00
|
|
|
const value = () => {
|
|
|
|
let val = $(`#${opts.container_id} #${opts.value_id}`).data('value');
|
|
|
|
if (val === null) {
|
|
|
|
val = '';
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
value,
|
2020-04-30 16:27:42 +02:00
|
|
|
update,
|
2020-04-14 10:43:30 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-12 20:44:05 +02:00
|
|
|
window.dropdown_list_widget = DropdownListWidget;
|