Sith/core/static/webpack/utils/select2.ts

282 lines
8.0 KiB
TypeScript
Raw Normal View History

2024-08-01 16:43:27 +00:00
/**
* Builders to use Select2 in our templates.
*
* This comes with two flavours : local data or remote data.
*
* # Local data source
*
* To use local data source, you must define an array
* in your JS code, having the fields `id` and `text`.
*
* ```js
* const data = [
* {id: 1, text: "foo"},
* {id: 2, text: "bar"},
* ];
* document.addEventListener("DOMContentLoaded", () => sithSelect2({
* element: document.getElementById("select2-input"),
2024-10-08 15:14:22 +00:00
* dataSource: localDataSource(data)
2024-08-01 16:43:27 +00:00
* }));
* ```
*
* You can also define a callback that return ids to exclude :
*
* ```js
* const data = [
* {id: 1, text: "foo"},
* {id: 2, text: "bar"},
* {id: 3, text: "to exclude"},
* ];
* document.addEventListener("DOMContentLoaded", () => sithSelect2({
* element: document.getElementById("select2-input"),
2024-10-08 15:14:22 +00:00
* dataSource: localDataSource(data, {
2024-08-01 16:43:27 +00:00
* excluded: () => data.filter((i) => i.text === "to exclude").map((i) => parseInt(i))
* })
* }));
* ```
*
* # Remote data source
*
* Select2 with remote data sources are similar to those with local
2024-10-08 15:14:22 +00:00
* data, but with some more parameters, like `resultConverter`,
2024-08-01 16:43:27 +00:00
* which takes a callback that must return a `Select2Object`.
*
* ```js
2024-10-12 22:26:43 +00:00
* import { makeUrl } from "#core:utils/api";
* import {userSearchUsers } from "#openapi"
2024-08-01 16:43:27 +00:00
* document.addEventListener("DOMContentLoaded", () => sithSelect2({
* element: document.getElementById("select2-input"),
2024-10-12 22:26:43 +00:00
* dataSource: remoteDataSource(await makeUrl(userSearchUsers), {
2024-08-01 16:43:27 +00:00
* excluded: () => [1, 2], // exclude users 1 and 2 from the search
2024-10-12 22:26:43 +00:00
* resultConverter: (user: AjaxResponse) => {id: user.id, text: (user.firstName as UserType)}
2024-08-01 16:43:27 +00:00
* })
* }));
* ```
*
* # Overrides
*
* Dealing with a select2 may be complex.
* That's why, when defining a select,
* you may add an override parameter,
* in which you can declare any parameter defined in the
* Select2 documentation.
*
* ```js
2024-10-12 22:26:43 +00:00
* import { makeUrl } from "#core:utils/api";
* import {userSearchUsers } from "#openapi"
2024-08-01 16:43:27 +00:00
* document.addEventListener("DOMContentLoaded", () => sithSelect2({
* element: document.getElementById("select2-input"),
2024-10-12 22:26:43 +00:00
* dataSource: remoteDataSource(await makeUrl(userSearchUsers), {
* resultConverter: (user: AjaxResponse) => {id: user.id, text: (user.firstName as UserType)}
2024-08-01 16:43:27 +00:00
* overrides: {
* delay: 500
* }
* })
* }));
* ```
*
* # Caveats with exclude
*
* With local data source, select2 evaluates the data only once.
* Thus, modify the exclude after the initialisation is a no-op.
*
* With remote data source, the exclude list will be evaluated
* after each api response.
* It makes it possible to bind the data returned by the callback
* to some reactive data, thus making the exclude list dynamic.
*
* # Images
*
* Sometimes, you would like to display an image besides
* the text on the select items.
2024-10-08 15:14:22 +00:00
* In this case, fill the `pictureGetter` option :
2024-08-01 16:43:27 +00:00
*
* ```js
2024-10-12 22:26:43 +00:00
* import { makeUrl } from "#core:utils/api";
* import {userSearchUsers } from "#openapi"
2024-08-01 16:43:27 +00:00
* document.addEventListener("DOMContentLoaded", () => sithSelect2({
* element: document.getElementById("select2-input"),
2024-10-12 22:26:43 +00:00
* dataSource: remoteDataSource(await makeUrl(userSearchUsers), {
* resultConverter: (user: AjaxResponse) => {id: user.id, text: (user.firstName as UserType)}
2024-08-01 16:43:27 +00:00
* })
2024-10-08 15:14:22 +00:00
* pictureGetter: (user) => user.profilePict,
2024-08-01 16:43:27 +00:00
* }));
* ```
*
* # Binding with alpine
*
* You can declare your select2 component in an Alpine data.
*
* ```html
* <body>
* <div x-data="select2_test">
* <select x-ref="search" x-ref="select"></select>
2024-10-08 15:14:22 +00:00
* <p x-text="currentSelection.id"></p>
* <p x-text="currentSelection.text"></p>
2024-08-01 16:43:27 +00:00
* </div>
* </body>
*
* <script>
* document.addEventListener("alpine:init", () => {
* Alpine.data("select2_test", () => ({
* selector: undefined,
2024-10-08 15:14:22 +00:00
* currentSelect: {id: "", text: ""},
2024-08-01 16:43:27 +00:00
*
* init() {
* this.selector = sithSelect2({
* element: $(this.$refs.select),
2024-10-08 15:14:22 +00:00
* dataSource: localDataSource(
2024-08-01 16:43:27 +00:00
* [{id: 1, text: "foo"}, {id: 2, text: "bar"}]
* ),
* });
* this.selector.on("select2:select", (event) => {
* // select2 => Alpine signals here
2024-10-08 15:14:22 +00:00
* this.currentSelect = this.selector.select2("data")
2024-08-01 16:43:27 +00:00
* });
2024-10-08 15:14:22 +00:00
* this.$watch("currentSelected" (value) => {
2024-08-01 16:43:27 +00:00
* // Alpine => select2 signals here
* });
* },
* }));
* })
* </script>
*/
import type {
AjaxOptions,
DataFormat,
GroupedDataFormat,
LoadingData,
Options,
} from "select2";
import "select2/dist/css/select2.css";
2024-08-01 16:43:27 +00:00
export interface Select2Object {
id: number;
text: string;
}
// biome-ignore lint/suspicious/noExplicitAny: You have to do it at some point
export type RemoteResult = any;
export type AjaxResponse = AjaxOptions<DataFormat | GroupedDataFormat, RemoteResult>;
interface DataSource {
ajax?: AjaxResponse | undefined;
data?: RemoteResult | DataFormat[] | GroupedDataFormat[] | undefined;
}
interface Select2Options {
element: Element;
/** the data source, built with `localDataSource` or `remoteDataSource` */
dataSource: DataSource;
excluded?: number[];
/** A callback to get the picture field from the API response */
pictureGetter?: (element: LoadingData | DataFormat | GroupedDataFormat) => string;
/** Any other select2 parameter to apply on the config */
overrides?: Options;
}
2024-08-01 16:43:27 +00:00
/**
2024-10-12 22:26:43 +00:00
* Create a new select2 with sith presets
2024-08-01 16:43:27 +00:00
*/
export function sithSelect2(options: Select2Options) {
const elem = $(options.element as HTMLInputElement);
2024-10-08 11:54:44 +00:00
return elem.select2({
theme: elem[0].multiple ? "classic" : "default",
minimumInputLength: 2,
2024-10-08 15:14:22 +00:00
templateResult: selectItemBuilder(options.pictureGetter),
...options.dataSource,
...(options.overrides ?? {}),
2024-10-08 11:54:44 +00:00
});
2024-08-01 16:43:27 +00:00
}
interface LocalSourceOptions {
excluded: () => number[];
}
2024-08-01 16:43:27 +00:00
/**
* Build a data source for a Select2 from a local array
*/
export function localDataSource(
2024-10-12 22:26:43 +00:00
source: Select2Object[] /** Array containing the data */,
options: LocalSourceOptions,
): DataSource {
2024-10-08 11:54:44 +00:00
if (options.excluded) {
const ids = options.excluded();
return { data: source.filter((i) => !ids.includes(i.id)) };
}
return { data: source };
2024-08-01 16:43:27 +00:00
}
interface RemoteSourceOptions {
/** A callback to the ids to exclude from the search */
excluded?: () => number[];
/** A converter for a value coming from the remote api */
resultConverter?: ((obj: RemoteResult) => DataFormat | GroupedDataFormat) | undefined;
/** Any other select2 parameter to apply on the config */
overrides?: AjaxOptions;
}
2024-08-01 16:43:27 +00:00
/**
* Build a data source for a Select2 from a remote url
*/
export function remoteDataSource(
2024-10-12 22:26:43 +00:00
source: string /** url of the endpoint */,
options: RemoteSourceOptions,
): DataSource {
$.ajaxSetup({
traditional: true,
});
const params: AjaxOptions = {
2024-10-08 11:54:44 +00:00
url: source,
dataType: "json",
cache: true,
delay: 250,
data: function (params) {
return {
search: params.term,
exclude: [
...(this.val() || []).map((i: string) => Number.parseInt(i)),
2024-10-08 11:54:44 +00:00
...(options.excluded ? options.excluded() : []),
],
};
},
};
2024-10-08 15:14:22 +00:00
if (options.resultConverter) {
2024-10-08 11:54:44 +00:00
params.processResults = (data) => ({
2024-10-08 15:14:22 +00:00
results: data.results.map(options.resultConverter),
2024-10-08 11:54:44 +00:00
});
}
if (options.overrides) {
Object.assign(params, options.overrides);
}
return { ajax: params };
2024-08-01 16:43:27 +00:00
}
export function itemFormatter(user: { loading: boolean; text: string }) {
2024-10-08 11:54:44 +00:00
if (user.loading) {
return user.text;
}
2024-08-01 16:43:27 +00:00
}
/**
* Build a function to display the results
*/
export function selectItemBuilder(pictureGetter?: (item: RemoteResult) => string) {
return (item: RemoteResult) => {
2024-10-08 15:14:22 +00:00
const picture = typeof pictureGetter === "function" ? pictureGetter(item) : null;
const imgHtml = picture
2024-10-08 11:54:44 +00:00
? `<img
2024-10-08 15:14:22 +00:00
src="${pictureGetter(item)}"
2024-09-10 20:55:39 +00:00
alt="${item.text}"
onerror="this.src = '/static/core/img/unknown.jpg'"
/>`
2024-10-08 11:54:44 +00:00
: "";
2024-08-01 16:43:27 +00:00
2024-10-08 11:54:44 +00:00
return $(`<div class="select-item">
2024-10-08 15:14:22 +00:00
${imgHtml}
2024-08-01 16:43:27 +00:00
<span class="select-item-text">${item.text}</span>
2024-10-07 23:33:21 +00:00
</div>`);
2024-10-08 11:54:44 +00:00
};
2024-08-01 16:43:27 +00:00
}