filtered-select-multiple-widget 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +177 -0
- package/package.json +49 -0
- package/src/FilteredSelectMultiple.js +448 -0
- package/src/index.js +7 -0
- package/src/themes.js +88 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) Thomas Breitner.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Filtered Select Multiple Widget
|
|
2
|
+
|
|
3
|
+
A dependency-free, JavaScript implementation of Django's dual-pane “FilteredSelectMultiple” widget. It transforms a native `<select multiple>` element into a picker with move buttons and client-side filtering.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
## Why?
|
|
8
|
+
|
|
9
|
+
I had been missing the beautiful and, above all, practical Django admin widget `filter-horizontal` outside of the Django Admin interface for quite some time.
|
|
10
|
+
|
|
11
|
+
<details>
|
|
12
|
+
<summary>And how does it look in the Django admin?</summary>
|
|
13
|
+
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
References:
|
|
17
|
+
|
|
18
|
+
- [Django admin docs `filter_horizontal`](https://docs.djangoproject.com/en/5.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.filter_horizontal)
|
|
19
|
+
- [Django admin `FilteredSelectMultiple`](https://github.com/django/django/blob/main/django/contrib/admin/widgets.py#L23C1-L23C52)
|
|
20
|
+
- [Django admin `SelectBox`](https://github.com/django/django/blob/main/django/contrib/admin/static/admin/js/SelectBox.js)
|
|
21
|
+
|
|
22
|
+
The Django widget is a thousand times better than mine. But I hope to gradually improve my implementation.
|
|
23
|
+
|
|
24
|
+
</details>
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- Zero dependencies and ES module friendly.
|
|
29
|
+
- Works with any native `<select multiple>` element.
|
|
30
|
+
- Optional client-side filtering of the available choices.
|
|
31
|
+
- Keyboard friendly and form-ready (synced back to the original select element).
|
|
32
|
+
- Integrates with Bootstrap 5, DaisyUI, and Tailwind CSS (see live demo with theme switcher).
|
|
33
|
+
- Automatically detects pane labels from associated `<label>` elements.
|
|
34
|
+
|
|
35
|
+
## Demo
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npm ci
|
|
39
|
+
npm run demo
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The demo includes a theme switcher to showcase integration with Bootstrap 5, DaisyUI, and Tailwind CSS.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install git+https://github.com/tombreit/filtered-select-multiple-widget.git
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```html
|
|
53
|
+
<select id="permissions" name="permissions" multiple>
|
|
54
|
+
<option value="read">Read</option>
|
|
55
|
+
<option value="write" selected>Write</option>
|
|
56
|
+
<option value="admin">Admin</option>
|
|
57
|
+
</select>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
import { FilteredSelectMultiple } from "filtered-select-multiple-widget";
|
|
62
|
+
|
|
63
|
+
const select = document.querySelector("#permissions");
|
|
64
|
+
const widget = new FilteredSelectMultiple(select);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Options
|
|
68
|
+
|
|
69
|
+
| Option | Type | Default | Description |
|
|
70
|
+
| ------ | ---- | ------- | ----------- |
|
|
71
|
+
| `showFilter` | `boolean` | `true` | Toggle the search box above the available list. |
|
|
72
|
+
| `filterMatchMode` | `'contains' \| 'startsWith'` | `'contains'` | How filtering behaves. |
|
|
73
|
+
| `size` | `number` | `select.size \|\| clamp(optionCount)` | Number of visible rows for each list. |
|
|
74
|
+
| `preserveSelectionOrder` | `boolean` | `false` | Keep the order items were added in the chosen list rather than the original option order. |
|
|
75
|
+
| `text` | `object` | see defaults | Override UI copy (`availableLabel`, `chosenLabel`, `filterPlaceholder`, `availableFilterPlaceholder`, `chosenFilterPlaceholder`, `addAll`, `addSelected`, `removeSelected`, `removeAll`). If `availableLabel` and `chosenLabel` are not provided, the widget will attempt to auto-detect them from an associated `<label>` element. |
|
|
76
|
+
| `theme` | `Theme` | `defaultTheme` | CSS theme configuration for styling framework integration. |
|
|
77
|
+
|
|
78
|
+
### Theming
|
|
79
|
+
|
|
80
|
+
The widget supports some UI frameworks through configurable themes:
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
import {
|
|
84
|
+
FilteredSelectMultiple,
|
|
85
|
+
bootstrap5Theme,
|
|
86
|
+
daisyUITheme,
|
|
87
|
+
tailwindTheme
|
|
88
|
+
} from "filtered-select-multiple-widget";
|
|
89
|
+
|
|
90
|
+
// Bootstrap 5
|
|
91
|
+
new FilteredSelectMultiple(select, { theme: bootstrap5Theme });
|
|
92
|
+
|
|
93
|
+
// DaisyUI 5
|
|
94
|
+
new FilteredSelectMultiple(select, { theme: daisyUITheme });
|
|
95
|
+
|
|
96
|
+
// Tailwind CSS 4
|
|
97
|
+
new FilteredSelectMultiple(select, { theme: tailwindTheme });
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
#### Custom Themes
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
import { Theme } from "filtered-select-multiple-widget";
|
|
104
|
+
|
|
105
|
+
const customTheme = new Theme({
|
|
106
|
+
container: "my-widget-container",
|
|
107
|
+
button: "my-button-class",
|
|
108
|
+
buttonAddSelected: "my-button-class my-primary-button",
|
|
109
|
+
filter: "my-input-class",
|
|
110
|
+
select: "my-select-class",
|
|
111
|
+
// ... customize any element
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
new FilteredSelectMultiple(select, { theme: customTheme });
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
#### Available Theme Elements
|
|
118
|
+
|
|
119
|
+
- `container` - Main widget wrapper
|
|
120
|
+
- `column`, `availableColumn`, `chosenColumn` - Pane containers
|
|
121
|
+
- `label` - Pane labels
|
|
122
|
+
- `filter` - Search input fields
|
|
123
|
+
- `select` - Multi-select elements
|
|
124
|
+
- `controls` - Button container
|
|
125
|
+
- `button`, `buttonAddAll`, `buttonAddSelected`, `buttonRemoveSelected`, `buttonRemoveAll` - Control buttons
|
|
126
|
+
- `buttonDisabled` - Disabled button modifier
|
|
127
|
+
- `button_svg` - Button SVG icon
|
|
128
|
+
|
|
129
|
+
### Destroying the widget
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
widget.destroy();
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The original `<select>` is restored and remains in sync with user selections.
|
|
136
|
+
|
|
137
|
+
## Third-Party Assets
|
|
138
|
+
|
|
139
|
+
<details>
|
|
140
|
+
<summary>Heroicons</summary>
|
|
141
|
+
|
|
142
|
+
This project uses icons from [Heroicons](https://heroicons.com/), which is licensed under the MIT License.
|
|
143
|
+
|
|
144
|
+
Copyright (c) Tailwind Labs, Inc.
|
|
145
|
+
|
|
146
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
147
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
148
|
+
in the Software without restriction, including without limitation the rights
|
|
149
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
150
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
151
|
+
furnished to do so, subject to the following conditions:
|
|
152
|
+
|
|
153
|
+
The above copyright notice and this permission notice shall be included in all
|
|
154
|
+
copies or substantial portions of the Software.
|
|
155
|
+
|
|
156
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
157
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
158
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
159
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
160
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
161
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
162
|
+
SOFTWARE.
|
|
163
|
+
|
|
164
|
+
</details>
|
|
165
|
+
|
|
166
|
+
<details>
|
|
167
|
+
<summary>Django project</summary>
|
|
168
|
+
|
|
169
|
+
Thank you for being here and for all the inspiration.
|
|
170
|
+
|
|
171
|
+
BSD 3-Clause license.
|
|
172
|
+
|
|
173
|
+
</details>
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "filtered-select-multiple-widget",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "ES module implementation of Django's FilteredSelectMultiple widget.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Thomas Breitner",
|
|
8
|
+
"email": "mail@thms.de",
|
|
9
|
+
"url": "https://thms.de"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./src/index.js",
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=14"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://tombreit.github.io/filtered-select-multiple-widget/",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/tombreit/filtered-select-multiple-widget.git"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/tombreit/filtered-select-multiple-widget/issues"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"widget",
|
|
31
|
+
"multiselect"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./src/index.js",
|
|
36
|
+
"default": "./src/index.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"http-server": "^14.1.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"test": "node --check src/index.js && node --check src/FilteredSelectMultiple.js && node --check src/themes.js",
|
|
44
|
+
"pre-demo": "ln -sf ../src docs/src",
|
|
45
|
+
"demo": "http-server docs/ -p 8000 -o",
|
|
46
|
+
"pre-ghpages": "rm -rf docs/src",
|
|
47
|
+
"ghpages": "cp -r src docs/src"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { defaultTheme } from "./themes.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Transform a native <select multiple> element into a dual-list transfer widget.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* import { FilteredSelectMultiple } from "filtered-select-multiple-widget";
|
|
8
|
+
* const widget = new FilteredSelectMultiple(selectElement, options);
|
|
9
|
+
*/
|
|
10
|
+
export class FilteredSelectMultiple {
|
|
11
|
+
constructor(selectElement, options = {}) {
|
|
12
|
+
// Validate
|
|
13
|
+
if (!(selectElement instanceof HTMLSelectElement) || !selectElement.multiple) {
|
|
14
|
+
throw new Error("FilteredSelectMultiple requires a <select multiple> element");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.selectElement = selectElement;
|
|
18
|
+
this.doc = selectElement.ownerDocument;
|
|
19
|
+
|
|
20
|
+
// Parse options
|
|
21
|
+
const optionCount = selectElement.options.length;
|
|
22
|
+
this.showFilter = options.showFilter ?? true;
|
|
23
|
+
this.filterMatchMode = options.filterMatchMode ?? "contains";
|
|
24
|
+
this.size = options.size ?? (selectElement.size || Math.min(Math.max(optionCount, 4), 12));
|
|
25
|
+
this.preserveSelectionOrder = options.preserveSelectionOrder ?? false;
|
|
26
|
+
this.theme = { ...defaultTheme, ...options.theme };
|
|
27
|
+
|
|
28
|
+
// Parse text options
|
|
29
|
+
const textDefaults = {
|
|
30
|
+
availableLabel: "Available",
|
|
31
|
+
chosenLabel: "Chosen",
|
|
32
|
+
filterPlaceholder: "Filter",
|
|
33
|
+
addAll: "Add all",
|
|
34
|
+
addSelected: "Add selected",
|
|
35
|
+
removeSelected: "Remove selected",
|
|
36
|
+
removeAll: "Remove all",
|
|
37
|
+
};
|
|
38
|
+
this.text = { ...textDefaults, ...(options.text || {}) };
|
|
39
|
+
|
|
40
|
+
// Auto-detect labels from <label> element if defaults are used
|
|
41
|
+
if (this.text.availableLabel === textDefaults.availableLabel &&
|
|
42
|
+
this.text.chosenLabel === textDefaults.chosenLabel) {
|
|
43
|
+
const label = this.doc.querySelector(`label[for="${selectElement.id}"]`);
|
|
44
|
+
if (label) {
|
|
45
|
+
const baseLabel = label.textContent.trim();
|
|
46
|
+
this.text.availableLabel = `Available ${baseLabel}`;
|
|
47
|
+
this.text.chosenLabel = `Chosen ${baseLabel}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Set filter placeholder defaults
|
|
52
|
+
const fallbackFilter = options.text?.filterPlaceholder ?? textDefaults.filterPlaceholder;
|
|
53
|
+
this.text.availableFilterPlaceholder = this.text.availableFilterPlaceholder ?? fallbackFilter;
|
|
54
|
+
this.text.chosenFilterPlaceholder = this.text.chosenFilterPlaceholder ?? fallbackFilter;
|
|
55
|
+
|
|
56
|
+
// State
|
|
57
|
+
this.options = new Map(); // key -> {index, value, label, disabled, dataset, title, original}
|
|
58
|
+
this.available = [];
|
|
59
|
+
this.chosen = [];
|
|
60
|
+
|
|
61
|
+
// UI elements
|
|
62
|
+
this.container = null;
|
|
63
|
+
this.availableSelect = null;
|
|
64
|
+
this.chosenSelect = null;
|
|
65
|
+
this.availableFilter = null;
|
|
66
|
+
this.chosenFilter = null;
|
|
67
|
+
this.buttons = {};
|
|
68
|
+
|
|
69
|
+
this._init();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
_init() {
|
|
73
|
+
// Build state from original select
|
|
74
|
+
this._buildState();
|
|
75
|
+
|
|
76
|
+
// Hide original select
|
|
77
|
+
this.selectElement.dataset.filteredSelectMultiple = "true";
|
|
78
|
+
this.previousDisplay = this.selectElement.style.display;
|
|
79
|
+
this.selectElement.style.display = "none";
|
|
80
|
+
|
|
81
|
+
// Create placeholder comment for restoring position
|
|
82
|
+
this.placeholder = this.doc.createComment("filtered-select-multiple");
|
|
83
|
+
this.selectElement.parentNode.insertBefore(this.placeholder, this.selectElement);
|
|
84
|
+
|
|
85
|
+
// Build and render UI
|
|
86
|
+
this._buildUI();
|
|
87
|
+
this._attachEvents();
|
|
88
|
+
this._render();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_buildState() {
|
|
92
|
+
const options = Array.from(this.selectElement.options);
|
|
93
|
+
const orderKeys = [];
|
|
94
|
+
|
|
95
|
+
options.forEach((option, index) => {
|
|
96
|
+
const key = `fsm-${index}`;
|
|
97
|
+
this.options.set(key, {
|
|
98
|
+
key,
|
|
99
|
+
index,
|
|
100
|
+
value: option.value,
|
|
101
|
+
label: option.text,
|
|
102
|
+
disabled: option.disabled,
|
|
103
|
+
dataset: { ...option.dataset },
|
|
104
|
+
title: option.title,
|
|
105
|
+
original: option,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
orderKeys.push(key);
|
|
109
|
+
if (option.selected) {
|
|
110
|
+
this.chosen.push(key);
|
|
111
|
+
} else {
|
|
112
|
+
this.available.push(key);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Sort by original order
|
|
117
|
+
this._sortByIndex(this.available);
|
|
118
|
+
if (!this.preserveSelectionOrder) {
|
|
119
|
+
this._sortByIndex(this.chosen);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_sortByIndex(keys) {
|
|
124
|
+
keys.sort((a, b) => {
|
|
125
|
+
const aIndex = this.options.get(a)?.index ?? Number.MAX_SAFE_INTEGER;
|
|
126
|
+
const bIndex = this.options.get(b)?.index ?? Number.MAX_SAFE_INTEGER;
|
|
127
|
+
return aIndex - bIndex;
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
_buildUI() {
|
|
132
|
+
// Main container
|
|
133
|
+
this.container = this.doc.createElement("div");
|
|
134
|
+
this.container.className = this.theme.container;
|
|
135
|
+
|
|
136
|
+
// Available pane
|
|
137
|
+
const availablePane = this._createPane({
|
|
138
|
+
type: "available",
|
|
139
|
+
label: this.text.availableLabel,
|
|
140
|
+
filterPlaceholder: this.text.availableFilterPlaceholder,
|
|
141
|
+
});
|
|
142
|
+
this.availableSelect = availablePane.select;
|
|
143
|
+
this.availableFilter = availablePane.filter;
|
|
144
|
+
|
|
145
|
+
// Controls (buttons)
|
|
146
|
+
const controls = this._createControls();
|
|
147
|
+
|
|
148
|
+
// Chosen pane
|
|
149
|
+
const chosenPane = this._createPane({
|
|
150
|
+
type: "chosen",
|
|
151
|
+
label: this.text.chosenLabel,
|
|
152
|
+
filterPlaceholder: this.text.chosenFilterPlaceholder,
|
|
153
|
+
});
|
|
154
|
+
this.chosenSelect = chosenPane.select;
|
|
155
|
+
this.chosenFilter = chosenPane.filter;
|
|
156
|
+
|
|
157
|
+
// Assemble
|
|
158
|
+
this.container.append(availablePane.column, controls, chosenPane.column);
|
|
159
|
+
this.placeholder.parentNode.insertBefore(this.container, this.placeholder);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
_createPane({ type, label, filterPlaceholder }) {
|
|
163
|
+
const column = this.doc.createElement("div");
|
|
164
|
+
const isAvailable = type === "available";
|
|
165
|
+
column.className = this.theme.column + " " + (isAvailable ? this.theme.availableColumn : this.theme.chosenColumn);
|
|
166
|
+
|
|
167
|
+
// Label
|
|
168
|
+
const labelEl = this.doc.createElement("div");
|
|
169
|
+
labelEl.className = this.theme.label;
|
|
170
|
+
labelEl.textContent = label;
|
|
171
|
+
column.appendChild(labelEl);
|
|
172
|
+
|
|
173
|
+
// Filter input (optional)
|
|
174
|
+
let filter = null;
|
|
175
|
+
if (this.showFilter) {
|
|
176
|
+
filter = this.doc.createElement("input");
|
|
177
|
+
filter.type = "search";
|
|
178
|
+
filter.className = this.theme.filter;
|
|
179
|
+
filter.placeholder = filterPlaceholder;
|
|
180
|
+
filter.dataset.paneType = type;
|
|
181
|
+
column.appendChild(filter);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Select element
|
|
185
|
+
const select = this.doc.createElement("select");
|
|
186
|
+
select.multiple = true;
|
|
187
|
+
select.size = this.size;
|
|
188
|
+
select.className = this.theme.select;
|
|
189
|
+
select.dataset.paneType = type;
|
|
190
|
+
column.appendChild(select);
|
|
191
|
+
|
|
192
|
+
return { column, select, filter };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
_createControls() {
|
|
196
|
+
const column = this.doc.createElement("div");
|
|
197
|
+
column.className = this.theme.controls;
|
|
198
|
+
|
|
199
|
+
const icons = {
|
|
200
|
+
addAll: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="height: 1.2em;">
|
|
201
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="m5.25 4.5 7.5 7.5-7.5 7.5m6-15 7.5 7.5-7.5 7.5" />
|
|
202
|
+
</svg>`,
|
|
203
|
+
addSelected: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="height: 1.2em;">
|
|
204
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="m8.25 4.5 7.5 7.5-7.5 7.5" />
|
|
205
|
+
</svg>`,
|
|
206
|
+
removeSelected: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="height: 1.2em;">
|
|
207
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 19.5 8.25 12l7.5-7.5" />
|
|
208
|
+
</svg>`,
|
|
209
|
+
removeAll: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="height: 1.2em;">
|
|
210
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="m18.75 4.5-7.5 7.5 7.5 7.5m-6-15L5.25 12l7.5 7.5" />
|
|
211
|
+
</svg>`,
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const buttons = [
|
|
215
|
+
{ action: "addAll", label: this.text.addAll, icon: icons.addAll },
|
|
216
|
+
{ action: "addSelected", label: this.text.addSelected, icon: icons.addSelected },
|
|
217
|
+
{ action: "removeSelected", label: this.text.removeSelected, icon: icons.removeSelected },
|
|
218
|
+
{ action: "removeAll", label: this.text.removeAll, icon: icons.removeAll },
|
|
219
|
+
];
|
|
220
|
+
|
|
221
|
+
buttons.forEach(({ action, label, icon }) => {
|
|
222
|
+
const button = this.doc.createElement("button");
|
|
223
|
+
button.type = "button";
|
|
224
|
+
const specificClass = this.theme[`button_${action}`] || "";
|
|
225
|
+
button.className = (this.theme.button + " " + specificClass).trim();
|
|
226
|
+
|
|
227
|
+
const isRemove = action.startsWith("remove");
|
|
228
|
+
const svgClass = this.theme.button_svg ? ` class="${this.theme.button_svg}"` : '';
|
|
229
|
+
const modifiedIcon = icon.replace('<svg', `<svg${svgClass}`);
|
|
230
|
+
button.innerHTML = isRemove ? `${modifiedIcon} ${label}` : `${label} ${modifiedIcon}`;
|
|
231
|
+
button.dataset.action = action;
|
|
232
|
+
|
|
233
|
+
this.buttons[action] = button;
|
|
234
|
+
column.appendChild(button);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return column;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
_attachEvents() {
|
|
241
|
+
// Filter input events
|
|
242
|
+
if (this.availableFilter) {
|
|
243
|
+
this.availableFilter.addEventListener("input", () => this._renderPane("available"));
|
|
244
|
+
}
|
|
245
|
+
if (this.chosenFilter) {
|
|
246
|
+
this.chosenFilter.addEventListener("input", () => this._renderPane("chosen"));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Selection change events
|
|
250
|
+
this.availableSelect.addEventListener("change", () => this._updateButtons());
|
|
251
|
+
this.chosenSelect.addEventListener("change", () => this._updateButtons());
|
|
252
|
+
|
|
253
|
+
// Double-click to move
|
|
254
|
+
this.availableSelect.addEventListener("dblclick", () => {
|
|
255
|
+
if (!this.availableSelect.disabled) {
|
|
256
|
+
this._moveSelected("available", "chosen");
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
this.chosenSelect.addEventListener("dblclick", () => {
|
|
260
|
+
if (!this.chosenSelect.disabled) {
|
|
261
|
+
this._moveSelected("chosen", "available");
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
// Button clicks
|
|
266
|
+
Object.entries(this.buttons).forEach(([action, button]) => {
|
|
267
|
+
button.addEventListener("click", () => {
|
|
268
|
+
switch (action) {
|
|
269
|
+
case "addAll":
|
|
270
|
+
this._moveAll("available", "chosen");
|
|
271
|
+
break;
|
|
272
|
+
case "addSelected":
|
|
273
|
+
this._moveSelected("available", "chosen");
|
|
274
|
+
break;
|
|
275
|
+
case "removeSelected":
|
|
276
|
+
this._moveSelected("chosen", "available");
|
|
277
|
+
break;
|
|
278
|
+
case "removeAll":
|
|
279
|
+
this._moveAll("chosen", "available");
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
_render() {
|
|
287
|
+
this._renderPane("available");
|
|
288
|
+
this._renderPane("chosen");
|
|
289
|
+
this._updateButtons();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
_renderPane(type) {
|
|
293
|
+
const select = type === "available" ? this.availableSelect : this.chosenSelect;
|
|
294
|
+
const filter = type === "available" ? this.availableFilter : this.chosenFilter;
|
|
295
|
+
const keys = type === "available" ? this.available : this.chosen;
|
|
296
|
+
|
|
297
|
+
// Get filter term
|
|
298
|
+
const term = filter ? filter.value.trim().toLowerCase() : "";
|
|
299
|
+
|
|
300
|
+
// Preserve selection
|
|
301
|
+
const previousSelection = new Set(
|
|
302
|
+
Array.from(select.selectedOptions, opt => opt.dataset.key)
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
// Clear and rebuild
|
|
306
|
+
select.innerHTML = "";
|
|
307
|
+
|
|
308
|
+
keys.forEach(key => {
|
|
309
|
+
const meta = this.options.get(key);
|
|
310
|
+
if (!meta) return;
|
|
311
|
+
|
|
312
|
+
// Apply filter
|
|
313
|
+
if (term && !this._passesFilter(meta.label, term)) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const option = this.doc.createElement("option");
|
|
318
|
+
option.value = meta.value;
|
|
319
|
+
option.textContent = meta.label;
|
|
320
|
+
option.dataset.key = key;
|
|
321
|
+
option.disabled = meta.disabled;
|
|
322
|
+
|
|
323
|
+
if (meta.title) {
|
|
324
|
+
option.title = meta.title;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
Object.entries(meta.dataset).forEach(([attr, value]) => {
|
|
328
|
+
option.dataset[attr] = value;
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
if (previousSelection.has(key)) {
|
|
332
|
+
option.selected = true;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
select.appendChild(option);
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
_passesFilter(label, term) {
|
|
340
|
+
if (!term) return true;
|
|
341
|
+
|
|
342
|
+
const haystack = label.toLowerCase();
|
|
343
|
+
|
|
344
|
+
if (this.filterMatchMode === "startsWith") {
|
|
345
|
+
return haystack.startsWith(term);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// "contains" mode - all tokens must match
|
|
349
|
+
const tokens = term.split(/\s+/).filter(Boolean);
|
|
350
|
+
return tokens.every(token => haystack.includes(token));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
_getSelectedKeys(select) {
|
|
354
|
+
return Array.from(select.selectedOptions, opt => opt.dataset.key);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
_moveSelected(fromType, toType) {
|
|
358
|
+
const fromSelect = fromType === "available" ? this.availableSelect : this.chosenSelect;
|
|
359
|
+
const keys = this._getSelectedKeys(fromSelect);
|
|
360
|
+
if (keys.length > 0) {
|
|
361
|
+
this._transfer(keys, fromType, toType);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
_moveAll(fromType, toType) {
|
|
366
|
+
const keys = fromType === "available" ? [...this.available] : [...this.chosen];
|
|
367
|
+
if (keys.length > 0) {
|
|
368
|
+
this._transfer(keys, fromType, toType);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
_transfer(keys, fromType, toType) {
|
|
373
|
+
const from = fromType === "available" ? this.available : this.chosen;
|
|
374
|
+
const to = toType === "available" ? this.available : this.chosen;
|
|
375
|
+
|
|
376
|
+
const keySet = new Set(keys);
|
|
377
|
+
|
|
378
|
+
// Remove from source
|
|
379
|
+
const remaining = from.filter(key => !keySet.has(key));
|
|
380
|
+
from.splice(0, from.length, ...remaining);
|
|
381
|
+
|
|
382
|
+
// Add to target
|
|
383
|
+
to.push(...keys);
|
|
384
|
+
|
|
385
|
+
// Sort
|
|
386
|
+
this._sortByIndex(from);
|
|
387
|
+
if (!this.preserveSelectionOrder || toType === "available") {
|
|
388
|
+
this._sortByIndex(to);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Update and notify
|
|
392
|
+
this._render();
|
|
393
|
+
this._syncToOriginal();
|
|
394
|
+
this._dispatchChange();
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
_syncToOriginal() {
|
|
398
|
+
const chosenSet = new Set(this.chosen);
|
|
399
|
+
this.options.forEach(meta => {
|
|
400
|
+
meta.original.selected = chosenSet.has(meta.key);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
_dispatchChange() {
|
|
405
|
+
const event = new Event("change", { bubbles: true });
|
|
406
|
+
this.selectElement.dispatchEvent(event);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
_updateButtons() {
|
|
410
|
+
const hasAvailableSelection = this.availableSelect.selectedOptions.length > 0;
|
|
411
|
+
const hasChosenSelection = this.chosenSelect.selectedOptions.length > 0;
|
|
412
|
+
const hasAvailableItems = this.available.length > 0;
|
|
413
|
+
const hasChosenItems = this.chosen.length > 0;
|
|
414
|
+
|
|
415
|
+
this._setButtonState("addSelected", !hasAvailableSelection);
|
|
416
|
+
this._setButtonState("removeSelected", !hasChosenSelection);
|
|
417
|
+
this._setButtonState("addAll", !hasAvailableItems);
|
|
418
|
+
this._setButtonState("removeAll", !hasChosenItems);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
_setButtonState(action, disabled) {
|
|
422
|
+
const button = this.buttons[action];
|
|
423
|
+
if (!button) return;
|
|
424
|
+
|
|
425
|
+
button.disabled = disabled;
|
|
426
|
+
|
|
427
|
+
// Rebuild className from scratch to avoid classList issues with spaces
|
|
428
|
+
const baseClasses = this.theme.button.split(/\s+/).filter(Boolean);
|
|
429
|
+
const actionClasses = (this.theme[`button_${action}`] || '').split(/\s+/).filter(Boolean);
|
|
430
|
+
const disabledClasses = (this.theme.buttonDisabled || '').split(/\s+/).filter(Boolean);
|
|
431
|
+
|
|
432
|
+
let finalClasses = [...baseClasses, ...actionClasses];
|
|
433
|
+
if (disabled) {
|
|
434
|
+
finalClasses.push(...disabledClasses);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
button.className = [...new Set(finalClasses)].join(' ');
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** Revert the widget and show the original select element */
|
|
441
|
+
destroy() {
|
|
442
|
+
this.container.remove();
|
|
443
|
+
this.selectElement.style.display = this.previousDisplay;
|
|
444
|
+
this.selectElement.removeAttribute("data-filtered-select-multiple");
|
|
445
|
+
this.placeholder.parentNode.insertBefore(this.selectElement, this.placeholder);
|
|
446
|
+
this.placeholder.remove();
|
|
447
|
+
}
|
|
448
|
+
}
|
package/src/index.js
ADDED
package/src/themes.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Theme definitions for FilteredSelectMultiple widget
|
|
3
|
+
* Simple objects mapping element types to CSS class strings
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Default/minimal theme (original classes)
|
|
8
|
+
*/
|
|
9
|
+
export const defaultTheme = {
|
|
10
|
+
container: "filtered-select-multiple",
|
|
11
|
+
column: "fsm-column",
|
|
12
|
+
availableColumn: "fsm-available",
|
|
13
|
+
chosenColumn: "fsm-chosen",
|
|
14
|
+
label: "fsm-label",
|
|
15
|
+
filter: "fsm-filter",
|
|
16
|
+
select: "fsm-select",
|
|
17
|
+
controls: "fsm-controls",
|
|
18
|
+
button: "fsm-button",
|
|
19
|
+
button_addAll: "fsm-add-all",
|
|
20
|
+
button_addSelected: "fsm-add-selected",
|
|
21
|
+
button_removeSelected: "fsm-remove-selected",
|
|
22
|
+
button_removeAll: "fsm-remove-all",
|
|
23
|
+
buttonDisabled: "fsm-button-disabled",
|
|
24
|
+
button_svg: "fsm-button-svg",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Bootstrap 5 theme preset
|
|
29
|
+
*/
|
|
30
|
+
export const bootstrap5Theme = {
|
|
31
|
+
container: "d-flex gap-3",
|
|
32
|
+
column: "d-flex flex-column",
|
|
33
|
+
availableColumn: "d-flex flex-column",
|
|
34
|
+
chosenColumn: "d-flex flex-column",
|
|
35
|
+
label: "form-label fw-semibold mb-2",
|
|
36
|
+
filter: "form-control form-control-sm mb-2",
|
|
37
|
+
select: "form-select flex-grow-1",
|
|
38
|
+
controls: "d-flex flex-column justify-content-center gap-2 px-3",
|
|
39
|
+
button: "btn btn-sm btn-primary",
|
|
40
|
+
button_addAll: "",
|
|
41
|
+
button_addSelected: "",
|
|
42
|
+
button_removeSelected: "",
|
|
43
|
+
button_removeAll: "",
|
|
44
|
+
buttonDisabled: "disabled",
|
|
45
|
+
button_svg: "d-inline-block",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* DaisyUI 5 theme preset
|
|
50
|
+
*/
|
|
51
|
+
export const daisyUITheme = {
|
|
52
|
+
container: "flex gap-4",
|
|
53
|
+
column: "flex flex-col",
|
|
54
|
+
availableColumn: "flex flex-col",
|
|
55
|
+
chosenColumn: "flex flex-col",
|
|
56
|
+
label: "label label-text font-semibold mb-2",
|
|
57
|
+
filter: "input input-bordered input-sm mb-2 w-full",
|
|
58
|
+
select: "select select-bordered w-full flex-grow",
|
|
59
|
+
controls: "flex flex-col justify-center gap-2 px-4",
|
|
60
|
+
button: "btn btn-sm",
|
|
61
|
+
button_addAll: "btn-secondary",
|
|
62
|
+
button_addSelected: "btn-primary",
|
|
63
|
+
button_removeSelected: "btn-primary",
|
|
64
|
+
button_removeAll: "btn-secondary",
|
|
65
|
+
buttonDisabled: "btn-disabled",
|
|
66
|
+
button_svg: "",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Tailwind CSS 4 theme preset (minimal utility classes)
|
|
71
|
+
*/
|
|
72
|
+
export const tailwindTheme = {
|
|
73
|
+
container: "flex gap-4",
|
|
74
|
+
column: "flex flex-col",
|
|
75
|
+
availableColumn: "flex flex-col",
|
|
76
|
+
chosenColumn: "flex flex-col",
|
|
77
|
+
label: "font-semibold text-sm mb-2",
|
|
78
|
+
filter: "border border-slate-300 rounded px-3 py-2 text-sm mb-2 focus:outline-none focus:ring-2 focus:ring-blue-500",
|
|
79
|
+
select: "border border-slate-300 rounded px-2 py-1 min-w-[200px] flex-grow",
|
|
80
|
+
controls: "flex flex-col justify-center gap-2 px-4",
|
|
81
|
+
button: "inline-flex items-center gap-2 px-3 py-2 text-sm border rounded hover:bg-slate-50 transition-colors",
|
|
82
|
+
button_addAll: "border-slate-300",
|
|
83
|
+
button_addSelected: "bg-blue-600 text-white border-blue-600 hover:bg-blue-700",
|
|
84
|
+
button_removeSelected: "bg-blue-600 text-white border-blue-600 hover:bg-blue-700",
|
|
85
|
+
button_removeAll: "border-slate-300",
|
|
86
|
+
buttonDisabled: "opacity-50 cursor-not-allowed",
|
|
87
|
+
button_svg: "",
|
|
88
|
+
};
|