rails_admin_next 1.0.0
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.
- checksums.yaml +7 -0
- data/Gemfile +46 -0
- data/LICENSE.md +21 -0
- data/README.md +317 -0
- data/Rakefile +25 -0
- data/app/assets/javascripts/rails_admin/custom/ui.js +1 -0
- data/app/assets/stylesheets/rails_admin/framework.css +2375 -0
- data/app/assets/stylesheets/rails_admin/skin.css +472 -0
- data/app/assets/stylesheets/rails_admin/tokens.css +281 -0
- data/app/assets/stylesheets/rails_admin.css +47 -0
- data/app/controllers/rails_admin_next/application_controller.rb +98 -0
- data/app/controllers/rails_admin_next/main_controller.rb +185 -0
- data/app/helpers/rails_admin_next/application_helper.rb +254 -0
- data/app/helpers/rails_admin_next/form_builder.rb +173 -0
- data/app/helpers/rails_admin_next/main_helper.rb +63 -0
- data/app/views/layouts/rails_admin_next/_head.html.erb +29 -0
- data/app/views/layouts/rails_admin_next/_navigation.html.erb +18 -0
- data/app/views/layouts/rails_admin_next/_secondary_navigation.html.erb +27 -0
- data/app/views/layouts/rails_admin_next/_sidebar_navigation.html.erb +6 -0
- data/app/views/layouts/rails_admin_next/application.html.erb +42 -0
- data/app/views/layouts/rails_admin_next/content.html.erb +22 -0
- data/app/views/layouts/rails_admin_next/modal.js.erb +7 -0
- data/app/views/rails_admin_next/main/_dashboard_history.html.erb +46 -0
- data/app/views/rails_admin_next/main/_delete_notice.html.erb +36 -0
- data/app/views/rails_admin_next/main/_form_action_text.html.erb +2 -0
- data/app/views/rails_admin_next/main/_form_boolean.html.erb +15 -0
- data/app/views/rails_admin_next/main/_form_colorpicker.html.erb +6 -0
- data/app/views/rails_admin_next/main/_form_datetime.html.erb +14 -0
- data/app/views/rails_admin_next/main/_form_enumeration.html.erb +26 -0
- data/app/views/rails_admin_next/main/_form_field.html.erb +2 -0
- data/app/views/rails_admin_next/main/_form_file_upload.html.erb +18 -0
- data/app/views/rails_admin_next/main/_form_filtering_multiselect.html.erb +20 -0
- data/app/views/rails_admin_next/main/_form_filtering_select.html.erb +22 -0
- data/app/views/rails_admin_next/main/_form_multiple_file_upload.html.erb +21 -0
- data/app/views/rails_admin_next/main/_form_nested_many.html.erb +20 -0
- data/app/views/rails_admin_next/main/_form_nested_one.html.erb +19 -0
- data/app/views/rails_admin_next/main/_form_polymorphic_association.html.erb +24 -0
- data/app/views/rails_admin_next/main/_form_text.html.erb +2 -0
- data/app/views/rails_admin_next/main/_pagination.html.erb +54 -0
- data/app/views/rails_admin_next/main/_submit_buttons.html.erb +26 -0
- data/app/views/rails_admin_next/main/bulk_delete.html.erb +19 -0
- data/app/views/rails_admin_next/main/dashboard.html.erb +64 -0
- data/app/views/rails_admin_next/main/delete.html.erb +21 -0
- data/app/views/rails_admin_next/main/edit.html.erb +3 -0
- data/app/views/rails_admin_next/main/export.html.erb +149 -0
- data/app/views/rails_admin_next/main/history.html.erb +80 -0
- data/app/views/rails_admin_next/main/index.html.erb +186 -0
- data/app/views/rails_admin_next/main/new.html.erb +3 -0
- data/app/views/rails_admin_next/main/show.html.erb +30 -0
- data/config/brakeman.ignore +145 -0
- data/config/importmap.rails_admin.rb +29 -0
- data/config/initializers/active_record_extensions.rb +21 -0
- data/config/locales/rails_admin_next.en.yml +158 -0
- data/config/routes.rb +14 -0
- data/lib/generators/rails_admin_next/install_generator.rb +39 -0
- data/lib/generators/rails_admin_next/templates/initializer.erb +40 -0
- data/lib/generators/rails_admin_next/utils.rb +21 -0
- data/lib/rails_admin_next/abstract_model.rb +266 -0
- data/lib/rails_admin_next/adapters/active_record/association.rb +130 -0
- data/lib/rails_admin_next/adapters/active_record/object_extension.rb +13 -0
- data/lib/rails_admin_next/adapters/active_record/property.rb +58 -0
- data/lib/rails_admin_next/adapters/active_record.rb +371 -0
- data/lib/rails_admin_next/config/actions/base.rb +160 -0
- data/lib/rails_admin_next/config/actions/bulk_delete.rb +67 -0
- data/lib/rails_admin_next/config/actions/dashboard.rb +62 -0
- data/lib/rails_admin_next/config/actions/delete.rb +58 -0
- data/lib/rails_admin_next/config/actions/edit.rb +56 -0
- data/lib/rails_admin_next/config/actions/export.rb +47 -0
- data/lib/rails_admin_next/config/actions/history_index.rb +40 -0
- data/lib/rails_admin_next/config/actions/history_show.rb +40 -0
- data/lib/rails_admin_next/config/actions/index.rb +108 -0
- data/lib/rails_admin_next/config/actions/new.rb +69 -0
- data/lib/rails_admin_next/config/actions/show.rb +36 -0
- data/lib/rails_admin_next/config/actions/show_in_app.rb +37 -0
- data/lib/rails_admin_next/config/actions.rb +122 -0
- data/lib/rails_admin_next/config/configurable.rb +107 -0
- data/lib/rails_admin_next/config/const_load_suppressor.rb +78 -0
- data/lib/rails_admin_next/config/fields/association.rb +169 -0
- data/lib/rails_admin_next/config/fields/base.rb +391 -0
- data/lib/rails_admin_next/config/fields/collection_association.rb +90 -0
- data/lib/rails_admin_next/config/fields/factories/action_text.rb +14 -0
- data/lib/rails_admin_next/config/fields/factories/active_storage.rb +34 -0
- data/lib/rails_admin_next/config/fields/factories/association.rb +32 -0
- data/lib/rails_admin_next/config/fields/factories/encrypted_password.rb +28 -0
- data/lib/rails_admin_next/config/fields/factories/enum.rb +23 -0
- data/lib/rails_admin_next/config/fields/factories/password.rb +19 -0
- data/lib/rails_admin_next/config/fields/group.rb +77 -0
- data/lib/rails_admin_next/config/fields/singular_association.rb +62 -0
- data/lib/rails_admin_next/config/fields/types/action_text.rb +20 -0
- data/lib/rails_admin_next/config/fields/types/active_record_enum.rb +62 -0
- data/lib/rails_admin_next/config/fields/types/active_storage.rb +70 -0
- data/lib/rails_admin_next/config/fields/types/all.rb +32 -0
- data/lib/rails_admin_next/config/fields/types/belongs_to_association.rb +48 -0
- data/lib/rails_admin_next/config/fields/types/boolean.rb +70 -0
- data/lib/rails_admin_next/config/fields/types/citext.rb +15 -0
- data/lib/rails_admin_next/config/fields/types/color.rb +43 -0
- data/lib/rails_admin_next/config/fields/types/date.rb +38 -0
- data/lib/rails_admin_next/config/fields/types/datetime.rb +79 -0
- data/lib/rails_admin_next/config/fields/types/decimal.rb +23 -0
- data/lib/rails_admin_next/config/fields/types/enum.rb +52 -0
- data/lib/rails_admin_next/config/fields/types/file_upload.rb +87 -0
- data/lib/rails_admin_next/config/fields/types/float.rb +23 -0
- data/lib/rails_admin_next/config/fields/types/has_and_belongs_to_many_association.rb +16 -0
- data/lib/rails_admin_next/config/fields/types/has_many_association.rb +16 -0
- data/lib/rails_admin_next/config/fields/types/has_one_association.rb +35 -0
- data/lib/rails_admin_next/config/fields/types/hidden.rb +31 -0
- data/lib/rails_admin_next/config/fields/types/inet.rb +15 -0
- data/lib/rails_admin_next/config/fields/types/integer.rb +20 -0
- data/lib/rails_admin_next/config/fields/types/json.rb +37 -0
- data/lib/rails_admin_next/config/fields/types/multiple_active_storage.rb +82 -0
- data/lib/rails_admin_next/config/fields/types/multiple_file_upload.rb +134 -0
- data/lib/rails_admin_next/config/fields/types/numeric.rb +24 -0
- data/lib/rails_admin_next/config/fields/types/password.rb +45 -0
- data/lib/rails_admin_next/config/fields/types/polymorphic_association.rb +118 -0
- data/lib/rails_admin_next/config/fields/types/serialized.rb +28 -0
- data/lib/rails_admin_next/config/fields/types/string.rb +50 -0
- data/lib/rails_admin_next/config/fields/types/string_like.rb +25 -0
- data/lib/rails_admin_next/config/fields/types/text.rb +28 -0
- data/lib/rails_admin_next/config/fields/types/time.rb +39 -0
- data/lib/rails_admin_next/config/fields/types/timestamp.rb +16 -0
- data/lib/rails_admin_next/config/fields/types/uuid.rb +15 -0
- data/lib/rails_admin_next/config/fields/types.rb +29 -0
- data/lib/rails_admin_next/config/fields.rb +88 -0
- data/lib/rails_admin_next/config/groupable.rb +20 -0
- data/lib/rails_admin_next/config/has_description.rb +14 -0
- data/lib/rails_admin_next/config/has_fields.rb +144 -0
- data/lib/rails_admin_next/config/has_groups.rb +27 -0
- data/lib/rails_admin_next/config/hideable.rb +30 -0
- data/lib/rails_admin_next/config/inspectable.rb +39 -0
- data/lib/rails_admin_next/config/lazy_model.rb +74 -0
- data/lib/rails_admin_next/config/model.rb +126 -0
- data/lib/rails_admin_next/config/proxyable/proxy.rb +42 -0
- data/lib/rails_admin_next/config/proxyable.rb +26 -0
- data/lib/rails_admin_next/config/sections/base.rb +36 -0
- data/lib/rails_admin_next/config/sections/create.rb +13 -0
- data/lib/rails_admin_next/config/sections/edit.rb +13 -0
- data/lib/rails_admin_next/config/sections/export.rb +13 -0
- data/lib/rails_admin_next/config/sections/list.rb +63 -0
- data/lib/rails_admin_next/config/sections/modal.rb +12 -0
- data/lib/rails_admin_next/config/sections/nested.rb +12 -0
- data/lib/rails_admin_next/config/sections/show.rb +12 -0
- data/lib/rails_admin_next/config/sections/update.rb +12 -0
- data/lib/rails_admin_next/config/sections.rb +39 -0
- data/lib/rails_admin_next/config.rb +436 -0
- data/lib/rails_admin_next/engine.rb +91 -0
- data/lib/rails_admin_next/extension.rb +35 -0
- data/lib/rails_admin_next/extensions/cancancan/authorization_adapter.rb +90 -0
- data/lib/rails_admin_next/extensions/cancancan.rb +5 -0
- data/lib/rails_admin_next/extensions/controller_extension.rb +8 -0
- data/lib/rails_admin_next/extensions/paper_trail/auditing_adapter.rb +192 -0
- data/lib/rails_admin_next/extensions/paper_trail.rb +5 -0
- data/lib/rails_admin_next/extensions/pundit/authorization_adapter.rb +72 -0
- data/lib/rails_admin_next/extensions/pundit.rb +5 -0
- data/lib/rails_admin_next/extensions/url_for_extension.rb +15 -0
- data/lib/rails_admin_next/icons.rb +143 -0
- data/lib/rails_admin_next/support/composite_keys_serializer.rb +15 -0
- data/lib/rails_admin_next/support/csv_converter.rb +100 -0
- data/lib/rails_admin_next/support/hash_helper.rb +31 -0
- data/lib/rails_admin_next/support/paginated_collection.rb +79 -0
- data/lib/rails_admin_next/version.rb +17 -0
- data/lib/rails_admin_next.rb +72 -0
- data/lib/tasks/rails_admin.rake +40 -0
- data/src/rails_admin/application.js +15 -0
- data/src/rails_admin/base.js +12 -0
- data/src/rails_admin/controllers/alert_controller.js +28 -0
- data/src/rails_admin/controllers/collapse_controller.js +16 -0
- data/src/rails_admin/controllers/dropdown_controller.js +110 -0
- data/src/rails_admin/controllers/feedback_controller.js +73 -0
- data/src/rails_admin/controllers/filter_box_controller.js +346 -0
- data/src/rails_admin/controllers/filtering_multiselect_controller.js +386 -0
- data/src/rails_admin/controllers/filtering_select_controller.js +509 -0
- data/src/rails_admin/controllers/index.js +31 -0
- data/src/rails_admin/controllers/modal_controller.js +142 -0
- data/src/rails_admin/controllers/nested_form_controller.js +117 -0
- data/src/rails_admin/controllers/polymorphic_controller.js +26 -0
- data/src/rails_admin/controllers/remote_form_controller.js +173 -0
- data/src/rails_admin/controllers/sidescroll_controller.js +23 -0
- data/src/rails_admin/dom-ready.js +20 -0
- data/src/rails_admin/dom.js +19 -0
- data/src/rails_admin/i18n.js +20 -0
- data/src/rails_admin/icons.js +64 -0
- data/src/rails_admin/interactions.js +221 -0
- data/src/rails_admin/select_query.js +65 -0
- metadata +346 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { Controller } from "@hotwired/stimulus";
|
|
2
|
+
import I18n from "rails_admin/i18n";
|
|
3
|
+
import { icon } from "rails_admin/icons";
|
|
4
|
+
import { el } from "rails_admin/dom";
|
|
5
|
+
import {
|
|
6
|
+
createScopedQuery,
|
|
7
|
+
toQueryString,
|
|
8
|
+
escapeRegex,
|
|
9
|
+
fetchJson,
|
|
10
|
+
} from "rails_admin/select_query";
|
|
11
|
+
|
|
12
|
+
const SEARCH_DELAY = 400;
|
|
13
|
+
|
|
14
|
+
// Dual-list widget over a hidden <select multiple> (the real form field). The
|
|
15
|
+
// left "collection" lists candidates, the right "selection" lists chosen
|
|
16
|
+
// records; add/remove/choose-all/clear-all move <option>s between them and
|
|
17
|
+
// keep the hidden select (the submitted value) in sync. Sortable (up/down)
|
|
18
|
+
// reorders the selection. The filter box searches a local cache or a remote
|
|
19
|
+
// source.
|
|
20
|
+
//
|
|
21
|
+
// The rendered DOM keeps the class/title hooks the inline-add flow
|
|
22
|
+
// (remote-form.js), the specs, and the CSS depend on (.ra-multiselect,
|
|
23
|
+
// .ra-multiselect-collection/-selection/-search/-center, the add/remove links).
|
|
24
|
+
//
|
|
25
|
+
// Wired by data-controller="filtering-multiselect" on the <select>, with
|
|
26
|
+
// data-filtering-multiselect-options-value = { xhr, remote_source, scopeBy,
|
|
27
|
+
// sortable, removable, cacheAll, regional }.
|
|
28
|
+
export default class extends Controller {
|
|
29
|
+
static values = { options: { type: Object, default: {} } };
|
|
30
|
+
|
|
31
|
+
connect() {
|
|
32
|
+
this.select = this.element;
|
|
33
|
+
this.cache = {};
|
|
34
|
+
this.searchTimer = null;
|
|
35
|
+
this.requestId = 0;
|
|
36
|
+
this.#build();
|
|
37
|
+
this.#buildCache();
|
|
38
|
+
this.#bind();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
disconnect() {
|
|
42
|
+
clearTimeout(this.searchTimer);
|
|
43
|
+
this.wrapper?.remove();
|
|
44
|
+
this.select.style.display = "";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get #regional() {
|
|
48
|
+
return this.optionsValue.regional || {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Defaults: removable on, sortable off, unless the options say otherwise (the
|
|
52
|
+
// enum js_data omits both keys).
|
|
53
|
+
get #removable() {
|
|
54
|
+
return this.optionsValue.removable !== false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get #sortable() {
|
|
58
|
+
return this.optionsValue.sortable === true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#build() {
|
|
62
|
+
const id = this.select.id;
|
|
63
|
+
this.element.parentNode
|
|
64
|
+
.querySelectorAll(`.ra-multiselect[data-input-for="${CSS.escape(id)}"]`)
|
|
65
|
+
.forEach((node) => node.remove());
|
|
66
|
+
|
|
67
|
+
this.wrapper = el("div", { class: "ra-multiselect", "data-input-for": id });
|
|
68
|
+
this.select.after(this.wrapper);
|
|
69
|
+
|
|
70
|
+
this.search = el("input", {
|
|
71
|
+
type: "search",
|
|
72
|
+
class: "form-control ra-multiselect-search",
|
|
73
|
+
placeholder: this.#regional.search || "",
|
|
74
|
+
"aria-label": this.#regional.search || "Search",
|
|
75
|
+
});
|
|
76
|
+
this.wrapper.appendChild(
|
|
77
|
+
el("div", { class: "ra-multiselect-header" }, this.search)
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const left = el("div", {
|
|
81
|
+
class: "ra-multiselect-column ra-multiselect-left",
|
|
82
|
+
});
|
|
83
|
+
const center = el("div", {
|
|
84
|
+
class: "ra-multiselect-column ra-multiselect-center",
|
|
85
|
+
});
|
|
86
|
+
const right = el("div", {
|
|
87
|
+
class: "ra-multiselect-column ra-multiselect-right",
|
|
88
|
+
});
|
|
89
|
+
this.wrapper.append(left, center, right);
|
|
90
|
+
|
|
91
|
+
this.collection = el("select", {
|
|
92
|
+
class: "form-control ra-multiselect-collection",
|
|
93
|
+
multiple: "multiple",
|
|
94
|
+
"aria-label": this.#regional.search || "Available",
|
|
95
|
+
});
|
|
96
|
+
this.addAll = el(
|
|
97
|
+
"a",
|
|
98
|
+
{ href: "#", class: "ra-multiselect-item-add-all" },
|
|
99
|
+
icon("move_right"),
|
|
100
|
+
this.#regional.chooseAll || ""
|
|
101
|
+
);
|
|
102
|
+
left.append(el("div", { class: "wrapper" }, this.collection), this.addAll);
|
|
103
|
+
|
|
104
|
+
this.add = el(
|
|
105
|
+
"a",
|
|
106
|
+
{
|
|
107
|
+
href: "#",
|
|
108
|
+
class: "ra-multiselect-item-add",
|
|
109
|
+
title: this.#regional.add,
|
|
110
|
+
role: "button",
|
|
111
|
+
"aria-label": this.#regional.add,
|
|
112
|
+
},
|
|
113
|
+
icon("move_right")
|
|
114
|
+
);
|
|
115
|
+
center.appendChild(this.add);
|
|
116
|
+
if (this.#removable) {
|
|
117
|
+
this.remove = el(
|
|
118
|
+
"a",
|
|
119
|
+
{
|
|
120
|
+
href: "#",
|
|
121
|
+
class: "ra-multiselect-item-remove",
|
|
122
|
+
title: this.#regional.remove,
|
|
123
|
+
role: "button",
|
|
124
|
+
"aria-label": this.#regional.remove,
|
|
125
|
+
},
|
|
126
|
+
icon("move_left")
|
|
127
|
+
);
|
|
128
|
+
center.appendChild(this.remove);
|
|
129
|
+
}
|
|
130
|
+
if (this.#sortable) {
|
|
131
|
+
this.up = el(
|
|
132
|
+
"a",
|
|
133
|
+
{
|
|
134
|
+
href: "#",
|
|
135
|
+
class: "ra-multiselect-item-up",
|
|
136
|
+
title: this.#regional.up,
|
|
137
|
+
role: "button",
|
|
138
|
+
"aria-label": this.#regional.up,
|
|
139
|
+
},
|
|
140
|
+
icon("move_up")
|
|
141
|
+
);
|
|
142
|
+
this.down = el(
|
|
143
|
+
"a",
|
|
144
|
+
{
|
|
145
|
+
href: "#",
|
|
146
|
+
class: "ra-multiselect-item-down",
|
|
147
|
+
title: this.#regional.down,
|
|
148
|
+
role: "button",
|
|
149
|
+
"aria-label": this.#regional.down,
|
|
150
|
+
},
|
|
151
|
+
icon("move_down")
|
|
152
|
+
);
|
|
153
|
+
center.append(this.up, this.down);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this.selection = el("select", {
|
|
157
|
+
class: "form-control ra-multiselect-selection",
|
|
158
|
+
multiple: "multiple",
|
|
159
|
+
"aria-label": "Selected",
|
|
160
|
+
});
|
|
161
|
+
right.appendChild(el("div", { class: "wrapper" }, this.selection));
|
|
162
|
+
if (this.#removable) {
|
|
163
|
+
this.removeAll = el(
|
|
164
|
+
"a",
|
|
165
|
+
{ href: "#", class: "ra-multiselect-item-remove-all" },
|
|
166
|
+
icon("move_left"),
|
|
167
|
+
this.#regional.clearAll || ""
|
|
168
|
+
);
|
|
169
|
+
right.appendChild(this.removeAll);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.status = el("div", {
|
|
173
|
+
class: "ra-multiselect-status visually-hidden",
|
|
174
|
+
role: "status",
|
|
175
|
+
"aria-live": "polite",
|
|
176
|
+
});
|
|
177
|
+
this.wrapper.appendChild(this.status);
|
|
178
|
+
|
|
179
|
+
this.select.style.display = "none";
|
|
180
|
+
|
|
181
|
+
if (this.optionsValue.xhr) {
|
|
182
|
+
this.collection.appendChild(
|
|
183
|
+
this.#placeholder(I18n.t("too_many_objects"))
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#placeholder(text) {
|
|
189
|
+
return el("option", { disabled: "disabled" }, text);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Cache every option (preserving insertion order) and split the live options
|
|
193
|
+
// into the selection (chosen) and collection (candidate) lists.
|
|
194
|
+
#buildCache() {
|
|
195
|
+
Array.from(this.select.options).forEach((option) => {
|
|
196
|
+
this.cache[`o_${option.value}`] = {
|
|
197
|
+
id: option.value,
|
|
198
|
+
value: option.text,
|
|
199
|
+
};
|
|
200
|
+
const clone = option.cloneNode(true);
|
|
201
|
+
clone.selected = false;
|
|
202
|
+
clone.title = option.text;
|
|
203
|
+
(option.selected ? this.selection : this.collection).appendChild(clone);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
#bind() {
|
|
208
|
+
this.addAll.addEventListener("click", (e) => {
|
|
209
|
+
e.preventDefault();
|
|
210
|
+
this.#select(
|
|
211
|
+
Array.from(this.collection.querySelectorAll("option:not(:disabled)"))
|
|
212
|
+
);
|
|
213
|
+
this.#announceCounts();
|
|
214
|
+
});
|
|
215
|
+
this.add.addEventListener("click", (e) => {
|
|
216
|
+
e.preventDefault();
|
|
217
|
+
this.#select(Array.from(this.collection.selectedOptions));
|
|
218
|
+
this.#announceCounts();
|
|
219
|
+
});
|
|
220
|
+
if (this.#removable) {
|
|
221
|
+
this.removeAll.addEventListener("click", (e) => {
|
|
222
|
+
e.preventDefault();
|
|
223
|
+
this.#deselect(Array.from(this.selection.options));
|
|
224
|
+
this.#announceCounts();
|
|
225
|
+
});
|
|
226
|
+
this.remove.addEventListener("click", (e) => {
|
|
227
|
+
e.preventDefault();
|
|
228
|
+
this.#deselect(Array.from(this.selection.selectedOptions));
|
|
229
|
+
this.#announceCounts();
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
if (this.#sortable) {
|
|
233
|
+
this.up.addEventListener("click", (e) => {
|
|
234
|
+
e.preventDefault();
|
|
235
|
+
this.#move("up", Array.from(this.selection.selectedOptions));
|
|
236
|
+
});
|
|
237
|
+
this.down.addEventListener("click", (e) => {
|
|
238
|
+
e.preventDefault();
|
|
239
|
+
this.#move("down", Array.from(this.selection.selectedOptions));
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
this.search.addEventListener("keyup", () => this.#scheduleFilter());
|
|
243
|
+
this.search.addEventListener("click", () => this.#scheduleFilter());
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
#scheduleFilter() {
|
|
247
|
+
clearTimeout(this.searchTimer);
|
|
248
|
+
this.searchTimer = setTimeout(
|
|
249
|
+
() => this.#queryFilter(this.search.value),
|
|
250
|
+
SEARCH_DELAY
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#queryFilter(value) {
|
|
255
|
+
this.#query(value, (matches) => {
|
|
256
|
+
const fresh = matches.filter((match) => !this.#isSelected(match.id));
|
|
257
|
+
if (fresh.length > 0) {
|
|
258
|
+
this.collection.replaceChildren(
|
|
259
|
+
...fresh.map((match) => {
|
|
260
|
+
const option = el("option", {
|
|
261
|
+
value: match.id,
|
|
262
|
+
title: match.label,
|
|
263
|
+
});
|
|
264
|
+
option.text = match.label;
|
|
265
|
+
return option;
|
|
266
|
+
})
|
|
267
|
+
);
|
|
268
|
+
} else {
|
|
269
|
+
this.collection.replaceChildren(
|
|
270
|
+
this.#placeholder(I18n.t("no_objects"))
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
#query(query, success) {
|
|
277
|
+
if (query === "") {
|
|
278
|
+
if (this.optionsValue.xhr) {
|
|
279
|
+
this.collection.replaceChildren(
|
|
280
|
+
this.#placeholder(I18n.t("too_many_objects"))
|
|
281
|
+
);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
success(
|
|
285
|
+
Object.values(this.cache).map((o) => ({ id: o.id, label: o.value }))
|
|
286
|
+
);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (this.optionsValue.xhr) {
|
|
291
|
+
const id = ++this.requestId;
|
|
292
|
+
this.controller?.abort();
|
|
293
|
+
this.controller = new AbortController();
|
|
294
|
+
const params = createScopedQuery(this.optionsValue.scopeBy, query);
|
|
295
|
+
const url = `${this.optionsValue.remote_source}&${toQueryString(params)}`;
|
|
296
|
+
fetchJson(url, { signal: this.controller.signal })
|
|
297
|
+
.then((data) => {
|
|
298
|
+
if (id !== this.requestId) return;
|
|
299
|
+
success(data);
|
|
300
|
+
})
|
|
301
|
+
.catch((error) => {
|
|
302
|
+
if (error.name === "AbortError" || id !== this.requestId) return;
|
|
303
|
+
this.collection.replaceChildren(
|
|
304
|
+
this.#placeholder(I18n.t("no_objects"))
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const matcher = new RegExp(`${escapeRegex(query)}.*`, "i");
|
|
311
|
+
success(
|
|
312
|
+
Object.values(this.cache)
|
|
313
|
+
.filter((o) => matcher.test(o.value))
|
|
314
|
+
.map((o) => ({ id: o.id, label: o.value }))
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Move options into the selection list and mark them selected on the hidden
|
|
319
|
+
// <select> (appending an <option> if it isn't already there).
|
|
320
|
+
#select(options) {
|
|
321
|
+
options.forEach((option) => {
|
|
322
|
+
const existing = Array.from(this.select.options).find(
|
|
323
|
+
(o) => o.value === option.value
|
|
324
|
+
);
|
|
325
|
+
if (existing) {
|
|
326
|
+
existing.selected = true;
|
|
327
|
+
} else {
|
|
328
|
+
const created = el("option", { value: option.value });
|
|
329
|
+
created.selected = true;
|
|
330
|
+
this.select.appendChild(created);
|
|
331
|
+
}
|
|
332
|
+
this.selection.appendChild(option);
|
|
333
|
+
option.selected = false;
|
|
334
|
+
});
|
|
335
|
+
this.select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
#deselect(options) {
|
|
339
|
+
options.forEach((option) => {
|
|
340
|
+
const existing = Array.from(this.select.options).find(
|
|
341
|
+
(o) => o.value === option.value
|
|
342
|
+
);
|
|
343
|
+
if (existing) existing.selected = false;
|
|
344
|
+
this.collection.appendChild(option);
|
|
345
|
+
option.selected = false;
|
|
346
|
+
});
|
|
347
|
+
this.select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
#move(direction, options) {
|
|
351
|
+
const ordered = direction === "up" ? options : options.reverse();
|
|
352
|
+
ordered.forEach((option) => {
|
|
353
|
+
const sibling =
|
|
354
|
+
direction === "up"
|
|
355
|
+
? option.previousElementSibling
|
|
356
|
+
: option.nextElementSibling;
|
|
357
|
+
if (!sibling) return;
|
|
358
|
+
const hidden = this.#hiddenOption(option.value);
|
|
359
|
+
const hiddenSibling = this.#hiddenOption(sibling.value);
|
|
360
|
+
if (direction === "up") {
|
|
361
|
+
sibling.before(option);
|
|
362
|
+
if (hidden && hiddenSibling) hiddenSibling.before(hidden);
|
|
363
|
+
} else {
|
|
364
|
+
sibling.after(option);
|
|
365
|
+
if (hidden && hiddenSibling) hiddenSibling.after(hidden);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
this.#announce(`${this.selection.selectedOptions.length} moved`);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
#hiddenOption(value) {
|
|
372
|
+
return Array.from(this.select.options).find((o) => o.value === value);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
#isSelected(value) {
|
|
376
|
+
return Array.from(this.selection.options).some((o) => o.value === value);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
#announceCounts() {
|
|
380
|
+
this.#announce(`${this.selection.options.length} selected`);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
#announce(message) {
|
|
384
|
+
if (this.status) this.status.textContent = message;
|
|
385
|
+
}
|
|
386
|
+
}
|