govuk_publishing_components 58.1.1 → 59.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.
Files changed (165) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/select-with-search/cross-icon.svg +6 -0
  3. data/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-search-tracker.js +4 -0
  4. data/app/assets/javascripts/govuk_publishing_components/components/select-with-search.js +57 -0
  5. data/app/assets/stylesheets/govuk_publishing_components/_all_components.scss +2 -0
  6. data/app/assets/stylesheets/govuk_publishing_components/components/_action-link.scss +14 -136
  7. data/app/assets/stylesheets/govuk_publishing_components/components/_heading.scss +0 -5
  8. data/app/assets/stylesheets/govuk_publishing_components/components/_layout-header.scss +6 -48
  9. data/app/assets/stylesheets/govuk_publishing_components/components/_select-with-search.scss +168 -0
  10. data/app/assets/stylesheets/govuk_publishing_components/components/_select.scss +6 -0
  11. data/app/assets/stylesheets/govuk_publishing_components/components/_tag.scss +14 -0
  12. data/app/assets/stylesheets/govuk_publishing_components/components/helpers/_markdown-typography.scss +1 -1
  13. data/app/views/govuk_publishing_components/components/_action_link.html.erb +4 -37
  14. data/app/views/govuk_publishing_components/components/_layout_header.html.erb +0 -2
  15. data/app/views/govuk_publishing_components/components/_select.html.erb +22 -23
  16. data/app/views/govuk_publishing_components/components/_select_with_search.html.erb +14 -0
  17. data/app/views/govuk_publishing_components/components/_share_links.html.erb +17 -9
  18. data/app/views/govuk_publishing_components/components/_tag.html.erb +20 -0
  19. data/app/views/govuk_publishing_components/components/docs/action_link.yml +0 -62
  20. data/app/views/govuk_publishing_components/components/docs/layout_header.yml +13 -0
  21. data/app/views/govuk_publishing_components/components/docs/select.yml +11 -0
  22. data/app/views/govuk_publishing_components/components/docs/select_with_search.yml +196 -0
  23. data/app/views/govuk_publishing_components/components/docs/share_links.yml +10 -0
  24. data/app/views/govuk_publishing_components/components/docs/signup_link.yml +0 -7
  25. data/app/views/govuk_publishing_components/components/docs/single_page_notification_button.yml +1 -7
  26. data/app/views/govuk_publishing_components/components/docs/subscription_links.yml +1 -7
  27. data/app/views/govuk_publishing_components/components/docs/tag.yml +57 -0
  28. data/app/views/govuk_publishing_components/components/layout_header/_header_logo.html.erb +16 -5
  29. data/lib/govuk_publishing_components/presenters/heading_helper.rb +1 -2
  30. data/lib/govuk_publishing_components/presenters/select_helper.rb +8 -5
  31. data/lib/govuk_publishing_components/presenters/select_with_search_helper.rb +92 -0
  32. data/lib/govuk_publishing_components/presenters/single_page_notification_button_helper.rb +1 -1
  33. data/lib/govuk_publishing_components/version.rb +1 -1
  34. data/lib/govuk_publishing_components.rb +1 -0
  35. data/node_modules/choices.js/LICENSE +21 -0
  36. data/node_modules/choices.js/README.md +1360 -0
  37. data/node_modules/choices.js/package.json +173 -0
  38. data/node_modules/choices.js/public/assets/scripts/choices.js +5230 -0
  39. data/node_modules/choices.js/public/assets/scripts/choices.min.js +2 -0
  40. data/node_modules/choices.js/public/assets/scripts/choices.mjs +5222 -0
  41. data/node_modules/choices.js/public/assets/scripts/choices.search-basic.js +4748 -0
  42. data/node_modules/choices.js/public/assets/scripts/choices.search-basic.min.js +2 -0
  43. data/node_modules/choices.js/public/assets/scripts/choices.search-basic.mjs +4740 -0
  44. data/node_modules/choices.js/public/assets/scripts/choices.search-kmp.js +3631 -0
  45. data/node_modules/choices.js/public/assets/scripts/choices.search-kmp.min.js +2 -0
  46. data/node_modules/choices.js/public/assets/scripts/choices.search-kmp.mjs +3623 -0
  47. data/node_modules/choices.js/public/assets/scripts/choices.search-prefix.js +3590 -0
  48. data/node_modules/choices.js/public/assets/scripts/choices.search-prefix.min.js +2 -0
  49. data/node_modules/choices.js/public/assets/scripts/choices.search-prefix.mjs +3582 -0
  50. data/node_modules/choices.js/public/assets/styles/base.css +180 -0
  51. data/node_modules/choices.js/public/assets/styles/base.css.map +1 -0
  52. data/node_modules/choices.js/public/assets/styles/base.min.css +1 -0
  53. data/node_modules/choices.js/public/assets/styles/choices.css +338 -0
  54. data/node_modules/choices.js/public/assets/styles/choices.css.map +1 -0
  55. data/node_modules/choices.js/public/assets/styles/choices.min.css +1 -0
  56. data/node_modules/choices.js/public/types/src/index.d.ts +6 -0
  57. data/node_modules/choices.js/public/types/src/scripts/actions/choices.d.ts +30 -0
  58. data/node_modules/choices.js/public/types/src/scripts/actions/groups.d.ts +8 -0
  59. data/node_modules/choices.js/public/types/src/scripts/actions/items.d.ts +17 -0
  60. data/node_modules/choices.js/public/types/src/scripts/choices.d.ts +210 -0
  61. data/node_modules/choices.js/public/types/src/scripts/components/container.d.ts +36 -0
  62. data/node_modules/choices.js/public/types/src/scripts/components/dropdown.d.ts +21 -0
  63. data/node_modules/choices.js/public/types/src/scripts/components/index.d.ts +7 -0
  64. data/node_modules/choices.js/public/types/src/scripts/components/input.d.ts +37 -0
  65. data/node_modules/choices.js/public/types/src/scripts/components/list.d.ts +14 -0
  66. data/node_modules/choices.js/public/types/src/scripts/components/wrapped-element.d.ts +21 -0
  67. data/node_modules/choices.js/public/types/src/scripts/components/wrapped-input.d.ts +3 -0
  68. data/node_modules/choices.js/public/types/src/scripts/components/wrapped-select.d.ts +20 -0
  69. data/node_modules/choices.js/public/types/src/scripts/constants.d.ts +1 -0
  70. data/node_modules/choices.js/public/types/src/scripts/defaults.d.ts +4 -0
  71. data/node_modules/choices.js/public/types/src/scripts/interfaces/action-type.d.ts +13 -0
  72. data/node_modules/choices.js/public/types/src/scripts/interfaces/build-flags.d.ts +11 -0
  73. data/node_modules/choices.js/public/types/src/scripts/interfaces/choice-full.d.ts +23 -0
  74. data/node_modules/choices.js/public/types/src/scripts/interfaces/class-names.d.ts +61 -0
  75. data/node_modules/choices.js/public/types/src/scripts/interfaces/event-choice.d.ts +7 -0
  76. data/node_modules/choices.js/public/types/src/scripts/interfaces/event-type.d.ts +14 -0
  77. data/node_modules/choices.js/public/types/src/scripts/interfaces/group-full.d.ts +10 -0
  78. data/node_modules/choices.js/public/types/src/scripts/interfaces/index.d.ts +14 -0
  79. data/node_modules/choices.js/public/types/src/scripts/interfaces/input-choice.d.ts +15 -0
  80. data/node_modules/choices.js/public/types/src/scripts/interfaces/input-group.d.ts +10 -0
  81. data/node_modules/choices.js/public/types/src/scripts/interfaces/item.d.ts +17 -0
  82. data/node_modules/choices.js/public/types/src/scripts/interfaces/keycode-map.d.ts +13 -0
  83. data/node_modules/choices.js/public/types/src/scripts/interfaces/options.d.ts +566 -0
  84. data/node_modules/choices.js/public/types/src/scripts/interfaces/passed-element-type.d.ts +7 -0
  85. data/node_modules/choices.js/public/types/src/scripts/interfaces/passed-element.d.ts +95 -0
  86. data/node_modules/choices.js/public/types/src/scripts/interfaces/position-options-type.d.ts +1 -0
  87. data/node_modules/choices.js/public/types/src/scripts/interfaces/search.d.ts +11 -0
  88. data/node_modules/choices.js/public/types/src/scripts/interfaces/state.d.ts +10 -0
  89. data/node_modules/choices.js/public/types/src/scripts/interfaces/store.d.ts +64 -0
  90. data/node_modules/choices.js/public/types/src/scripts/interfaces/string-pre-escaped.d.ts +3 -0
  91. data/node_modules/choices.js/public/types/src/scripts/interfaces/string-untrusted.d.ts +4 -0
  92. data/node_modules/choices.js/public/types/src/scripts/interfaces/templates.d.ts +29 -0
  93. data/node_modules/choices.js/public/types/src/scripts/interfaces/types.d.ts +18 -0
  94. data/node_modules/choices.js/public/types/src/scripts/lib/choice-input.d.ts +9 -0
  95. data/node_modules/choices.js/public/types/src/scripts/lib/html-guard-statements.d.ts +4 -0
  96. data/node_modules/choices.js/public/types/src/scripts/lib/utils.d.ts +31 -0
  97. data/node_modules/choices.js/public/types/src/scripts/reducers/choices.d.ts +8 -0
  98. data/node_modules/choices.js/public/types/src/scripts/reducers/groups.d.ts +8 -0
  99. data/node_modules/choices.js/public/types/src/scripts/reducers/items.d.ts +9 -0
  100. data/node_modules/choices.js/public/types/src/scripts/search/fuse.d.ts +14 -0
  101. data/node_modules/choices.js/public/types/src/scripts/search/index.d.ts +3 -0
  102. data/node_modules/choices.js/public/types/src/scripts/search/kmp.d.ts +11 -0
  103. data/node_modules/choices.js/public/types/src/scripts/search/prefix-filter.d.ts +11 -0
  104. data/node_modules/choices.js/public/types/src/scripts/store/store.d.ts +59 -0
  105. data/node_modules/choices.js/public/types/src/scripts/templates.d.ts +8 -0
  106. data/node_modules/choices.js/src/entry.js +3 -0
  107. data/node_modules/choices.js/src/icons/cross-inverse.svg +1 -0
  108. data/node_modules/choices.js/src/icons/cross.svg +1 -0
  109. data/node_modules/choices.js/src/index.ts +8 -0
  110. data/node_modules/choices.js/src/scripts/actions/choices.ts +59 -0
  111. data/node_modules/choices.js/src/scripts/actions/groups.ts +14 -0
  112. data/node_modules/choices.js/src/scripts/actions/items.ts +34 -0
  113. data/node_modules/choices.js/src/scripts/choices.ts +2364 -0
  114. data/node_modules/choices.js/src/scripts/components/container.ts +157 -0
  115. data/node_modules/choices.js/src/scripts/components/dropdown.ts +50 -0
  116. data/node_modules/choices.js/src/scripts/components/index.ts +8 -0
  117. data/node_modules/choices.js/src/scripts/components/input.ts +146 -0
  118. data/node_modules/choices.js/src/scripts/components/list.ts +89 -0
  119. data/node_modules/choices.js/src/scripts/components/wrapped-element.ts +89 -0
  120. data/node_modules/choices.js/src/scripts/components/wrapped-input.ts +3 -0
  121. data/node_modules/choices.js/src/scripts/components/wrapped-select.ts +115 -0
  122. data/node_modules/choices.js/src/scripts/constants.ts +1 -0
  123. data/node_modules/choices.js/src/scripts/defaults.ts +93 -0
  124. data/node_modules/choices.js/src/scripts/interfaces/action-type.ts +15 -0
  125. data/node_modules/choices.js/src/scripts/interfaces/build-flags.ts +17 -0
  126. data/node_modules/choices.js/src/scripts/interfaces/choice-full.ts +30 -0
  127. data/node_modules/choices.js/src/scripts/interfaces/class-names.ts +61 -0
  128. data/node_modules/choices.js/src/scripts/interfaces/event-choice.ts +9 -0
  129. data/node_modules/choices.js/src/scripts/interfaces/event-type.ts +16 -0
  130. data/node_modules/choices.js/src/scripts/interfaces/group-full.ts +12 -0
  131. data/node_modules/choices.js/src/scripts/interfaces/index.ts +14 -0
  132. data/node_modules/choices.js/src/scripts/interfaces/input-choice.ts +17 -0
  133. data/node_modules/choices.js/src/scripts/interfaces/input-group.ts +11 -0
  134. data/node_modules/choices.js/src/scripts/interfaces/item.ts +17 -0
  135. data/node_modules/choices.js/src/scripts/interfaces/keycode-map.ts +13 -0
  136. data/node_modules/choices.js/src/scripts/interfaces/options.ts +619 -0
  137. data/node_modules/choices.js/src/scripts/interfaces/passed-element-type.ts +9 -0
  138. data/node_modules/choices.js/src/scripts/interfaces/passed-element.ts +96 -0
  139. data/node_modules/choices.js/src/scripts/interfaces/position-options-type.ts +1 -0
  140. data/node_modules/choices.js/src/scripts/interfaces/search.ts +12 -0
  141. data/node_modules/choices.js/src/scripts/interfaces/state.ts +12 -0
  142. data/node_modules/choices.js/src/scripts/interfaces/store.ts +84 -0
  143. data/node_modules/choices.js/src/scripts/interfaces/string-pre-escaped.ts +3 -0
  144. data/node_modules/choices.js/src/scripts/interfaces/string-untrusted.ts +5 -0
  145. data/node_modules/choices.js/src/scripts/interfaces/templates.ts +66 -0
  146. data/node_modules/choices.js/src/scripts/interfaces/types.ts +21 -0
  147. data/node_modules/choices.js/src/scripts/lib/choice-input.ts +88 -0
  148. data/node_modules/choices.js/src/scripts/lib/html-guard-statements.ts +7 -0
  149. data/node_modules/choices.js/src/scripts/lib/utils.ts +230 -0
  150. data/node_modules/choices.js/src/scripts/reducers/choices.ts +86 -0
  151. data/node_modules/choices.js/src/scripts/reducers/groups.ts +32 -0
  152. data/node_modules/choices.js/src/scripts/reducers/items.ts +86 -0
  153. data/node_modules/choices.js/src/scripts/search/fuse.ts +59 -0
  154. data/node_modules/choices.js/src/scripts/search/index.ts +17 -0
  155. data/node_modules/choices.js/src/scripts/search/kmp.ts +87 -0
  156. data/node_modules/choices.js/src/scripts/search/prefix-filter.ts +42 -0
  157. data/node_modules/choices.js/src/scripts/store/store.ts +184 -0
  158. data/node_modules/choices.js/src/scripts/templates.ts +409 -0
  159. data/node_modules/choices.js/src/styles/base.scss +189 -0
  160. data/node_modules/choices.js/src/styles/choices.scss +414 -0
  161. data/node_modules/choices.js/src/tsconfig.json +22 -0
  162. metadata +137 -4
  163. data/app/assets/images/govuk_publishing_components/action-link-arrow--dark.svg +0 -5
  164. data/app/assets/images/govuk_publishing_components/action-link-arrow--simple-light.svg +0 -4
  165. data/app/assets/images/govuk_publishing_components/action-link-arrow.svg +0 -5
@@ -0,0 +1,4740 @@
1
+ /*! choices.js v11.1.0 | © 2025 Josh Johnson | https://github.com/jshjohnson/Choices#readme */
2
+
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+ /* global Reflect, Promise, SuppressedError, Symbol */
18
+
19
+ var extendStatics = function (d, b) {
20
+ extendStatics = Object.setPrototypeOf || {
21
+ __proto__: []
22
+ } instanceof Array && function (d, b) {
23
+ d.__proto__ = b;
24
+ } || function (d, b) {
25
+ for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
26
+ };
27
+ return extendStatics(d, b);
28
+ };
29
+ function __extends(d, b) {
30
+ if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
31
+ extendStatics(d, b);
32
+ function __() {
33
+ this.constructor = d;
34
+ }
35
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
36
+ }
37
+ var __assign = function () {
38
+ __assign = Object.assign || function __assign(t) {
39
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
40
+ s = arguments[i];
41
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
42
+ }
43
+ return t;
44
+ };
45
+ return __assign.apply(this, arguments);
46
+ };
47
+ function __spreadArray(to, from, pack) {
48
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
49
+ if (ar || !(i in from)) {
50
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
51
+ ar[i] = from[i];
52
+ }
53
+ }
54
+ return to.concat(ar || Array.prototype.slice.call(from));
55
+ }
56
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
57
+ var e = new Error(message);
58
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
59
+ };
60
+
61
+ var ActionType = {
62
+ ADD_CHOICE: 'ADD_CHOICE',
63
+ REMOVE_CHOICE: 'REMOVE_CHOICE',
64
+ FILTER_CHOICES: 'FILTER_CHOICES',
65
+ ACTIVATE_CHOICES: 'ACTIVATE_CHOICES',
66
+ CLEAR_CHOICES: 'CLEAR_CHOICES',
67
+ ADD_GROUP: 'ADD_GROUP',
68
+ ADD_ITEM: 'ADD_ITEM',
69
+ REMOVE_ITEM: 'REMOVE_ITEM',
70
+ HIGHLIGHT_ITEM: 'HIGHLIGHT_ITEM',
71
+ };
72
+
73
+ var EventType = {
74
+ showDropdown: 'showDropdown',
75
+ hideDropdown: 'hideDropdown',
76
+ change: 'change',
77
+ choice: 'choice',
78
+ search: 'search',
79
+ addItem: 'addItem',
80
+ removeItem: 'removeItem',
81
+ highlightItem: 'highlightItem',
82
+ highlightChoice: 'highlightChoice',
83
+ unhighlightItem: 'unhighlightItem',
84
+ };
85
+
86
+ var KeyCodeMap = {
87
+ TAB_KEY: 9,
88
+ SHIFT_KEY: 16,
89
+ BACK_KEY: 46,
90
+ DELETE_KEY: 8,
91
+ ENTER_KEY: 13,
92
+ A_KEY: 65,
93
+ ESC_KEY: 27,
94
+ UP_KEY: 38,
95
+ DOWN_KEY: 40,
96
+ PAGE_UP_KEY: 33,
97
+ PAGE_DOWN_KEY: 34,
98
+ };
99
+
100
+ var ObjectsInConfig = ['fuseOptions', 'classNames'];
101
+
102
+ var PassedElementTypes = {
103
+ Text: 'text',
104
+ SelectOne: 'select-one',
105
+ SelectMultiple: 'select-multiple',
106
+ };
107
+
108
+ var addChoice = function (choice) { return ({
109
+ type: ActionType.ADD_CHOICE,
110
+ choice: choice,
111
+ }); };
112
+ var removeChoice = function (choice) { return ({
113
+ type: ActionType.REMOVE_CHOICE,
114
+ choice: choice,
115
+ }); };
116
+ var filterChoices = function (results) { return ({
117
+ type: ActionType.FILTER_CHOICES,
118
+ results: results,
119
+ }); };
120
+ var activateChoices = function (active) {
121
+ return ({
122
+ type: ActionType.ACTIVATE_CHOICES,
123
+ active: active,
124
+ });
125
+ };
126
+
127
+ var addGroup = function (group) { return ({
128
+ type: ActionType.ADD_GROUP,
129
+ group: group,
130
+ }); };
131
+
132
+ var addItem = function (item) { return ({
133
+ type: ActionType.ADD_ITEM,
134
+ item: item,
135
+ }); };
136
+ var removeItem$1 = function (item) { return ({
137
+ type: ActionType.REMOVE_ITEM,
138
+ item: item,
139
+ }); };
140
+ var highlightItem = function (item, highlighted) { return ({
141
+ type: ActionType.HIGHLIGHT_ITEM,
142
+ item: item,
143
+ highlighted: highlighted,
144
+ }); };
145
+
146
+ var getRandomNumber = function (min, max) { return Math.floor(Math.random() * (max - min) + min); };
147
+ var generateChars = function (length) {
148
+ return Array.from({ length: length }, function () { return getRandomNumber(0, 36).toString(36); }).join('');
149
+ };
150
+ var generateId = function (element, prefix) {
151
+ var id = element.id || (element.name && "".concat(element.name, "-").concat(generateChars(2))) || generateChars(4);
152
+ id = id.replace(/(:|\.|\[|\]|,)/g, '');
153
+ id = "".concat(prefix, "-").concat(id);
154
+ return id;
155
+ };
156
+ var getAdjacentEl = function (startEl, selector, direction) {
157
+ if (direction === void 0) { direction = 1; }
158
+ var prop = "".concat(direction > 0 ? 'next' : 'previous', "ElementSibling");
159
+ var sibling = startEl[prop];
160
+ while (sibling) {
161
+ if (sibling.matches(selector)) {
162
+ return sibling;
163
+ }
164
+ sibling = sibling[prop];
165
+ }
166
+ return null;
167
+ };
168
+ var isScrolledIntoView = function (element, parent, direction) {
169
+ if (direction === void 0) { direction = 1; }
170
+ var isVisible;
171
+ if (direction > 0) {
172
+ // In view from bottom
173
+ isVisible = parent.scrollTop + parent.offsetHeight >= element.offsetTop + element.offsetHeight;
174
+ }
175
+ else {
176
+ // In view from top
177
+ isVisible = element.offsetTop >= parent.scrollTop;
178
+ }
179
+ return isVisible;
180
+ };
181
+ var sanitise = function (value) {
182
+ if (typeof value !== 'string') {
183
+ if (value === null || value === undefined) {
184
+ return '';
185
+ }
186
+ if (typeof value === 'object') {
187
+ if ('raw' in value) {
188
+ return sanitise(value.raw);
189
+ }
190
+ if ('trusted' in value) {
191
+ return value.trusted;
192
+ }
193
+ }
194
+ return value;
195
+ }
196
+ return value
197
+ .replace(/&/g, '&amp;')
198
+ .replace(/>/g, '&gt;')
199
+ .replace(/</g, '&lt;')
200
+ .replace(/'/g, '&#039;')
201
+ .replace(/"/g, '&quot;');
202
+ };
203
+ var strToEl = (function () {
204
+ var tmpEl = document.createElement('div');
205
+ return function (str) {
206
+ tmpEl.innerHTML = str.trim();
207
+ var firstChild = tmpEl.children[0];
208
+ while (tmpEl.firstChild) {
209
+ tmpEl.removeChild(tmpEl.firstChild);
210
+ }
211
+ return firstChild;
212
+ };
213
+ })();
214
+ var resolveNoticeFunction = function (fn, value) {
215
+ return typeof fn === 'function' ? fn(sanitise(value), value) : fn;
216
+ };
217
+ var resolveStringFunction = function (fn) {
218
+ return typeof fn === 'function' ? fn() : fn;
219
+ };
220
+ var unwrapStringForRaw = function (s) {
221
+ if (typeof s === 'string') {
222
+ return s;
223
+ }
224
+ if (typeof s === 'object') {
225
+ if ('trusted' in s) {
226
+ return s.trusted;
227
+ }
228
+ if ('raw' in s) {
229
+ return s.raw;
230
+ }
231
+ }
232
+ return '';
233
+ };
234
+ var unwrapStringForEscaped = function (s) {
235
+ if (typeof s === 'string') {
236
+ return s;
237
+ }
238
+ if (typeof s === 'object') {
239
+ if ('escaped' in s) {
240
+ return s.escaped;
241
+ }
242
+ if ('trusted' in s) {
243
+ return s.trusted;
244
+ }
245
+ }
246
+ return '';
247
+ };
248
+ var escapeForTemplate = function (allowHTML, s) {
249
+ return allowHTML ? unwrapStringForEscaped(s) : sanitise(s);
250
+ };
251
+ var setElementHtml = function (el, allowHtml, html) {
252
+ el.innerHTML = escapeForTemplate(allowHtml, html);
253
+ };
254
+ var sortByAlpha = function (_a, _b) {
255
+ var value = _a.value, _c = _a.label, label = _c === void 0 ? value : _c;
256
+ var value2 = _b.value, _d = _b.label, label2 = _d === void 0 ? value2 : _d;
257
+ return unwrapStringForRaw(label).localeCompare(unwrapStringForRaw(label2), [], {
258
+ sensitivity: 'base',
259
+ ignorePunctuation: true,
260
+ numeric: true,
261
+ });
262
+ };
263
+ var sortByRank = function (a, b) {
264
+ return a.rank - b.rank;
265
+ };
266
+ var dispatchEvent = function (element, type, customArgs) {
267
+ if (customArgs === void 0) { customArgs = null; }
268
+ var event = new CustomEvent(type, {
269
+ detail: customArgs,
270
+ bubbles: true,
271
+ cancelable: true,
272
+ });
273
+ return element.dispatchEvent(event);
274
+ };
275
+ /**
276
+ * Returns an array of keys present on the first but missing on the second object
277
+ */
278
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
279
+ var diff = function (a, b) {
280
+ var aKeys = Object.keys(a).sort();
281
+ var bKeys = Object.keys(b).sort();
282
+ return aKeys.filter(function (i) { return bKeys.indexOf(i) < 0; });
283
+ };
284
+ var getClassNames = function (ClassNames) {
285
+ return Array.isArray(ClassNames) ? ClassNames : [ClassNames];
286
+ };
287
+ var getClassNamesSelector = function (option) {
288
+ if (option && Array.isArray(option)) {
289
+ return option
290
+ .map(function (item) {
291
+ return ".".concat(item);
292
+ })
293
+ .join('');
294
+ }
295
+ return ".".concat(option);
296
+ };
297
+ var addClassesToElement = function (element, className) {
298
+ var _a;
299
+ (_a = element.classList).add.apply(_a, getClassNames(className));
300
+ };
301
+ var removeClassesFromElement = function (element, className) {
302
+ var _a;
303
+ (_a = element.classList).remove.apply(_a, getClassNames(className));
304
+ };
305
+ var parseCustomProperties = function (customProperties) {
306
+ if (typeof customProperties !== 'undefined') {
307
+ try {
308
+ return JSON.parse(customProperties);
309
+ }
310
+ catch (e) {
311
+ return customProperties;
312
+ }
313
+ }
314
+ return {};
315
+ };
316
+ var updateClassList = function (item, add, remove) {
317
+ var itemEl = item.itemEl;
318
+ if (itemEl) {
319
+ removeClassesFromElement(itemEl, remove);
320
+ addClassesToElement(itemEl, add);
321
+ }
322
+ };
323
+
324
+ var Dropdown = /** @class */ (function () {
325
+ function Dropdown(_a) {
326
+ var element = _a.element, type = _a.type, classNames = _a.classNames;
327
+ this.element = element;
328
+ this.classNames = classNames;
329
+ this.type = type;
330
+ this.isActive = false;
331
+ }
332
+ /**
333
+ * Show dropdown to user by adding active state class
334
+ */
335
+ Dropdown.prototype.show = function () {
336
+ addClassesToElement(this.element, this.classNames.activeState);
337
+ this.element.setAttribute('aria-expanded', 'true');
338
+ this.isActive = true;
339
+ return this;
340
+ };
341
+ /**
342
+ * Hide dropdown from user
343
+ */
344
+ Dropdown.prototype.hide = function () {
345
+ removeClassesFromElement(this.element, this.classNames.activeState);
346
+ this.element.setAttribute('aria-expanded', 'false');
347
+ this.isActive = false;
348
+ return this;
349
+ };
350
+ return Dropdown;
351
+ }());
352
+
353
+ var Container = /** @class */ (function () {
354
+ function Container(_a) {
355
+ var element = _a.element, type = _a.type, classNames = _a.classNames, position = _a.position;
356
+ this.element = element;
357
+ this.classNames = classNames;
358
+ this.type = type;
359
+ this.position = position;
360
+ this.isOpen = false;
361
+ this.isFlipped = false;
362
+ this.isDisabled = false;
363
+ this.isLoading = false;
364
+ }
365
+ /**
366
+ * Determine whether container should be flipped based on passed
367
+ * dropdown position
368
+ */
369
+ Container.prototype.shouldFlip = function (dropdownPos, dropdownHeight) {
370
+ // If flip is enabled and the dropdown bottom position is
371
+ // greater than the window height flip the dropdown.
372
+ var shouldFlip = false;
373
+ if (this.position === 'auto') {
374
+ shouldFlip =
375
+ this.element.getBoundingClientRect().top - dropdownHeight >= 0 &&
376
+ !window.matchMedia("(min-height: ".concat(dropdownPos + 1, "px)")).matches;
377
+ }
378
+ else if (this.position === 'top') {
379
+ shouldFlip = true;
380
+ }
381
+ return shouldFlip;
382
+ };
383
+ Container.prototype.setActiveDescendant = function (activeDescendantID) {
384
+ this.element.setAttribute('aria-activedescendant', activeDescendantID);
385
+ };
386
+ Container.prototype.removeActiveDescendant = function () {
387
+ this.element.removeAttribute('aria-activedescendant');
388
+ };
389
+ Container.prototype.open = function (dropdownPos, dropdownHeight) {
390
+ addClassesToElement(this.element, this.classNames.openState);
391
+ this.element.setAttribute('aria-expanded', 'true');
392
+ this.isOpen = true;
393
+ if (this.shouldFlip(dropdownPos, dropdownHeight)) {
394
+ addClassesToElement(this.element, this.classNames.flippedState);
395
+ this.isFlipped = true;
396
+ }
397
+ };
398
+ Container.prototype.close = function () {
399
+ removeClassesFromElement(this.element, this.classNames.openState);
400
+ this.element.setAttribute('aria-expanded', 'false');
401
+ this.removeActiveDescendant();
402
+ this.isOpen = false;
403
+ // A dropdown flips if it does not have space within the page
404
+ if (this.isFlipped) {
405
+ removeClassesFromElement(this.element, this.classNames.flippedState);
406
+ this.isFlipped = false;
407
+ }
408
+ };
409
+ Container.prototype.addFocusState = function () {
410
+ addClassesToElement(this.element, this.classNames.focusState);
411
+ };
412
+ Container.prototype.removeFocusState = function () {
413
+ removeClassesFromElement(this.element, this.classNames.focusState);
414
+ };
415
+ Container.prototype.enable = function () {
416
+ removeClassesFromElement(this.element, this.classNames.disabledState);
417
+ this.element.removeAttribute('aria-disabled');
418
+ if (this.type === PassedElementTypes.SelectOne) {
419
+ this.element.setAttribute('tabindex', '0');
420
+ }
421
+ this.isDisabled = false;
422
+ };
423
+ Container.prototype.disable = function () {
424
+ addClassesToElement(this.element, this.classNames.disabledState);
425
+ this.element.setAttribute('aria-disabled', 'true');
426
+ if (this.type === PassedElementTypes.SelectOne) {
427
+ this.element.setAttribute('tabindex', '-1');
428
+ }
429
+ this.isDisabled = true;
430
+ };
431
+ Container.prototype.wrap = function (element) {
432
+ var el = this.element;
433
+ var parentNode = element.parentNode;
434
+ if (parentNode) {
435
+ if (element.nextSibling) {
436
+ parentNode.insertBefore(el, element.nextSibling);
437
+ }
438
+ else {
439
+ parentNode.appendChild(el);
440
+ }
441
+ }
442
+ el.appendChild(element);
443
+ };
444
+ Container.prototype.unwrap = function (element) {
445
+ var el = this.element;
446
+ var parentNode = el.parentNode;
447
+ if (parentNode) {
448
+ // Move passed element outside this element
449
+ parentNode.insertBefore(element, el);
450
+ // Remove this element
451
+ parentNode.removeChild(el);
452
+ }
453
+ };
454
+ Container.prototype.addLoadingState = function () {
455
+ addClassesToElement(this.element, this.classNames.loadingState);
456
+ this.element.setAttribute('aria-busy', 'true');
457
+ this.isLoading = true;
458
+ };
459
+ Container.prototype.removeLoadingState = function () {
460
+ removeClassesFromElement(this.element, this.classNames.loadingState);
461
+ this.element.removeAttribute('aria-busy');
462
+ this.isLoading = false;
463
+ };
464
+ return Container;
465
+ }());
466
+
467
+ var Input = /** @class */ (function () {
468
+ function Input(_a) {
469
+ var element = _a.element, type = _a.type, classNames = _a.classNames, preventPaste = _a.preventPaste;
470
+ this.element = element;
471
+ this.type = type;
472
+ this.classNames = classNames;
473
+ this.preventPaste = preventPaste;
474
+ this.isFocussed = this.element.isEqualNode(document.activeElement);
475
+ this.isDisabled = element.disabled;
476
+ this._onPaste = this._onPaste.bind(this);
477
+ this._onInput = this._onInput.bind(this);
478
+ this._onFocus = this._onFocus.bind(this);
479
+ this._onBlur = this._onBlur.bind(this);
480
+ }
481
+ Object.defineProperty(Input.prototype, "placeholder", {
482
+ set: function (placeholder) {
483
+ this.element.placeholder = placeholder;
484
+ },
485
+ enumerable: false,
486
+ configurable: true
487
+ });
488
+ Object.defineProperty(Input.prototype, "value", {
489
+ get: function () {
490
+ return this.element.value;
491
+ },
492
+ set: function (value) {
493
+ this.element.value = value;
494
+ },
495
+ enumerable: false,
496
+ configurable: true
497
+ });
498
+ Input.prototype.addEventListeners = function () {
499
+ var el = this.element;
500
+ el.addEventListener('paste', this._onPaste);
501
+ el.addEventListener('input', this._onInput, {
502
+ passive: true,
503
+ });
504
+ el.addEventListener('focus', this._onFocus, {
505
+ passive: true,
506
+ });
507
+ el.addEventListener('blur', this._onBlur, {
508
+ passive: true,
509
+ });
510
+ };
511
+ Input.prototype.removeEventListeners = function () {
512
+ var el = this.element;
513
+ el.removeEventListener('input', this._onInput);
514
+ el.removeEventListener('paste', this._onPaste);
515
+ el.removeEventListener('focus', this._onFocus);
516
+ el.removeEventListener('blur', this._onBlur);
517
+ };
518
+ Input.prototype.enable = function () {
519
+ var el = this.element;
520
+ el.removeAttribute('disabled');
521
+ this.isDisabled = false;
522
+ };
523
+ Input.prototype.disable = function () {
524
+ var el = this.element;
525
+ el.setAttribute('disabled', '');
526
+ this.isDisabled = true;
527
+ };
528
+ Input.prototype.focus = function () {
529
+ if (!this.isFocussed) {
530
+ this.element.focus();
531
+ }
532
+ };
533
+ Input.prototype.blur = function () {
534
+ if (this.isFocussed) {
535
+ this.element.blur();
536
+ }
537
+ };
538
+ Input.prototype.clear = function (setWidth) {
539
+ if (setWidth === void 0) { setWidth = true; }
540
+ this.element.value = '';
541
+ if (setWidth) {
542
+ this.setWidth();
543
+ }
544
+ return this;
545
+ };
546
+ /**
547
+ * Set the correct input width based on placeholder
548
+ * value or input value
549
+ */
550
+ Input.prototype.setWidth = function () {
551
+ // Resize input to contents or placeholder
552
+ var element = this.element;
553
+ element.style.minWidth = "".concat(element.placeholder.length + 1, "ch");
554
+ element.style.width = "".concat(element.value.length + 1, "ch");
555
+ };
556
+ Input.prototype.setActiveDescendant = function (activeDescendantID) {
557
+ this.element.setAttribute('aria-activedescendant', activeDescendantID);
558
+ };
559
+ Input.prototype.removeActiveDescendant = function () {
560
+ this.element.removeAttribute('aria-activedescendant');
561
+ };
562
+ Input.prototype._onInput = function () {
563
+ if (this.type !== PassedElementTypes.SelectOne) {
564
+ this.setWidth();
565
+ }
566
+ };
567
+ Input.prototype._onPaste = function (event) {
568
+ if (this.preventPaste) {
569
+ event.preventDefault();
570
+ }
571
+ };
572
+ Input.prototype._onFocus = function () {
573
+ this.isFocussed = true;
574
+ };
575
+ Input.prototype._onBlur = function () {
576
+ this.isFocussed = false;
577
+ };
578
+ return Input;
579
+ }());
580
+
581
+ var SCROLLING_SPEED = 4;
582
+
583
+ var List = /** @class */ (function () {
584
+ function List(_a) {
585
+ var element = _a.element;
586
+ this.element = element;
587
+ this.scrollPos = this.element.scrollTop;
588
+ this.height = this.element.offsetHeight;
589
+ }
590
+ List.prototype.prepend = function (node) {
591
+ var child = this.element.firstElementChild;
592
+ if (child) {
593
+ this.element.insertBefore(node, child);
594
+ }
595
+ else {
596
+ this.element.append(node);
597
+ }
598
+ };
599
+ List.prototype.scrollToTop = function () {
600
+ this.element.scrollTop = 0;
601
+ };
602
+ List.prototype.scrollToChildElement = function (element, direction) {
603
+ var _this = this;
604
+ if (!element) {
605
+ return;
606
+ }
607
+ var listHeight = this.element.offsetHeight;
608
+ // Scroll position of dropdown
609
+ var listScrollPosition = this.element.scrollTop + listHeight;
610
+ var elementHeight = element.offsetHeight;
611
+ // Distance from bottom of element to top of parent
612
+ var elementPos = element.offsetTop + elementHeight;
613
+ // Difference between the element and scroll position
614
+ var destination = direction > 0 ? this.element.scrollTop + elementPos - listScrollPosition : element.offsetTop;
615
+ requestAnimationFrame(function () {
616
+ _this._animateScroll(destination, direction);
617
+ });
618
+ };
619
+ List.prototype._scrollDown = function (scrollPos, strength, destination) {
620
+ var easing = (destination - scrollPos) / strength;
621
+ var distance = easing > 1 ? easing : 1;
622
+ this.element.scrollTop = scrollPos + distance;
623
+ };
624
+ List.prototype._scrollUp = function (scrollPos, strength, destination) {
625
+ var easing = (scrollPos - destination) / strength;
626
+ var distance = easing > 1 ? easing : 1;
627
+ this.element.scrollTop = scrollPos - distance;
628
+ };
629
+ List.prototype._animateScroll = function (destination, direction) {
630
+ var _this = this;
631
+ var strength = SCROLLING_SPEED;
632
+ var choiceListScrollTop = this.element.scrollTop;
633
+ var continueAnimation = false;
634
+ if (direction > 0) {
635
+ this._scrollDown(choiceListScrollTop, strength, destination);
636
+ if (choiceListScrollTop < destination) {
637
+ continueAnimation = true;
638
+ }
639
+ }
640
+ else {
641
+ this._scrollUp(choiceListScrollTop, strength, destination);
642
+ if (choiceListScrollTop > destination) {
643
+ continueAnimation = true;
644
+ }
645
+ }
646
+ if (continueAnimation) {
647
+ requestAnimationFrame(function () {
648
+ _this._animateScroll(destination, direction);
649
+ });
650
+ }
651
+ };
652
+ return List;
653
+ }());
654
+
655
+ var WrappedElement = /** @class */ (function () {
656
+ function WrappedElement(_a) {
657
+ var element = _a.element, classNames = _a.classNames;
658
+ this.element = element;
659
+ this.classNames = classNames;
660
+ this.isDisabled = false;
661
+ }
662
+ Object.defineProperty(WrappedElement.prototype, "isActive", {
663
+ get: function () {
664
+ return this.element.dataset.choice === 'active';
665
+ },
666
+ enumerable: false,
667
+ configurable: true
668
+ });
669
+ Object.defineProperty(WrappedElement.prototype, "dir", {
670
+ get: function () {
671
+ return this.element.dir;
672
+ },
673
+ enumerable: false,
674
+ configurable: true
675
+ });
676
+ Object.defineProperty(WrappedElement.prototype, "value", {
677
+ get: function () {
678
+ return this.element.value;
679
+ },
680
+ set: function (value) {
681
+ this.element.setAttribute('value', value);
682
+ this.element.value = value;
683
+ },
684
+ enumerable: false,
685
+ configurable: true
686
+ });
687
+ WrappedElement.prototype.conceal = function () {
688
+ var el = this.element;
689
+ // Hide passed input
690
+ addClassesToElement(el, this.classNames.input);
691
+ el.hidden = true;
692
+ // Remove element from tab index
693
+ el.tabIndex = -1;
694
+ // Backup original styles if any
695
+ var origStyle = el.getAttribute('style');
696
+ if (origStyle) {
697
+ el.setAttribute('data-choice-orig-style', origStyle);
698
+ }
699
+ el.setAttribute('data-choice', 'active');
700
+ };
701
+ WrappedElement.prototype.reveal = function () {
702
+ var el = this.element;
703
+ // Reinstate passed element
704
+ removeClassesFromElement(el, this.classNames.input);
705
+ el.hidden = false;
706
+ el.removeAttribute('tabindex');
707
+ // Recover original styles if any
708
+ var origStyle = el.getAttribute('data-choice-orig-style');
709
+ if (origStyle) {
710
+ el.removeAttribute('data-choice-orig-style');
711
+ el.setAttribute('style', origStyle);
712
+ }
713
+ else {
714
+ el.removeAttribute('style');
715
+ }
716
+ el.removeAttribute('data-choice');
717
+ };
718
+ WrappedElement.prototype.enable = function () {
719
+ this.element.removeAttribute('disabled');
720
+ this.element.disabled = false;
721
+ this.isDisabled = false;
722
+ };
723
+ WrappedElement.prototype.disable = function () {
724
+ this.element.setAttribute('disabled', '');
725
+ this.element.disabled = true;
726
+ this.isDisabled = true;
727
+ };
728
+ WrappedElement.prototype.triggerEvent = function (eventType, data) {
729
+ dispatchEvent(this.element, eventType, data || {});
730
+ };
731
+ return WrappedElement;
732
+ }());
733
+
734
+ var WrappedInput = /** @class */ (function (_super) {
735
+ __extends(WrappedInput, _super);
736
+ function WrappedInput() {
737
+ return _super !== null && _super.apply(this, arguments) || this;
738
+ }
739
+ return WrappedInput;
740
+ }(WrappedElement));
741
+
742
+ var coerceBool = function (arg, defaultValue) {
743
+ if (defaultValue === void 0) { defaultValue = true; }
744
+ return typeof arg === 'undefined' ? defaultValue : !!arg;
745
+ };
746
+ var stringToHtmlClass = function (input) {
747
+ if (typeof input === 'string') {
748
+ // eslint-disable-next-line no-param-reassign
749
+ input = input.split(' ').filter(function (s) { return s.length; });
750
+ }
751
+ if (Array.isArray(input) && input.length) {
752
+ return input;
753
+ }
754
+ return undefined;
755
+ };
756
+ var mapInputToChoice = function (value, allowGroup, allowRawString) {
757
+ if (allowRawString === void 0) { allowRawString = true; }
758
+ if (typeof value === 'string') {
759
+ var sanitisedValue = sanitise(value);
760
+ var userValue = allowRawString || sanitisedValue === value ? value : { escaped: sanitisedValue, raw: value };
761
+ var result_1 = mapInputToChoice({
762
+ value: value,
763
+ label: userValue,
764
+ selected: true,
765
+ }, false);
766
+ return result_1;
767
+ }
768
+ var groupOrChoice = value;
769
+ if ('choices' in groupOrChoice) {
770
+ if (!allowGroup) {
771
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup
772
+ throw new TypeError("optGroup is not allowed");
773
+ }
774
+ var group = groupOrChoice;
775
+ var choices = group.choices.map(function (e) { return mapInputToChoice(e, false); });
776
+ var result_2 = {
777
+ id: 0, // actual ID will be assigned during _addGroup
778
+ label: unwrapStringForRaw(group.label) || group.value,
779
+ active: !!choices.length,
780
+ disabled: !!group.disabled,
781
+ choices: choices,
782
+ };
783
+ return result_2;
784
+ }
785
+ var choice = groupOrChoice;
786
+ var result = {
787
+ id: 0, // actual ID will be assigned during _addChoice
788
+ group: null, // actual group will be assigned during _addGroup but before _addChoice
789
+ score: 0, // used in search
790
+ rank: 0, // used in search, stable sort order
791
+ value: choice.value,
792
+ label: choice.label || choice.value,
793
+ active: coerceBool(choice.active),
794
+ selected: coerceBool(choice.selected, false),
795
+ disabled: coerceBool(choice.disabled, false),
796
+ placeholder: coerceBool(choice.placeholder, false),
797
+ highlighted: false,
798
+ labelClass: stringToHtmlClass(choice.labelClass),
799
+ labelDescription: choice.labelDescription,
800
+ customProperties: choice.customProperties,
801
+ };
802
+ return result;
803
+ };
804
+
805
+ var isHtmlInputElement = function (e) { return e.tagName === 'INPUT'; };
806
+ var isHtmlSelectElement = function (e) { return e.tagName === 'SELECT'; };
807
+ var isHtmlOption = function (e) { return e.tagName === 'OPTION'; };
808
+ var isHtmlOptgroup = function (e) { return e.tagName === 'OPTGROUP'; };
809
+
810
+ var WrappedSelect = /** @class */ (function (_super) {
811
+ __extends(WrappedSelect, _super);
812
+ function WrappedSelect(_a) {
813
+ var element = _a.element, classNames = _a.classNames, template = _a.template, extractPlaceholder = _a.extractPlaceholder;
814
+ var _this = _super.call(this, { element: element, classNames: classNames }) || this;
815
+ _this.template = template;
816
+ _this.extractPlaceholder = extractPlaceholder;
817
+ return _this;
818
+ }
819
+ Object.defineProperty(WrappedSelect.prototype, "placeholderOption", {
820
+ get: function () {
821
+ return (this.element.querySelector('option[value=""]') ||
822
+ // Backward compatibility layer for the non-standard placeholder attribute supported in older versions.
823
+ this.element.querySelector('option[placeholder]'));
824
+ },
825
+ enumerable: false,
826
+ configurable: true
827
+ });
828
+ WrappedSelect.prototype.addOptions = function (choices) {
829
+ var _this = this;
830
+ var fragment = document.createDocumentFragment();
831
+ choices.forEach(function (obj) {
832
+ var choice = obj;
833
+ if (choice.element) {
834
+ return;
835
+ }
836
+ var option = _this.template(choice);
837
+ fragment.appendChild(option);
838
+ choice.element = option;
839
+ });
840
+ this.element.appendChild(fragment);
841
+ };
842
+ WrappedSelect.prototype.optionsAsChoices = function () {
843
+ var _this = this;
844
+ var choices = [];
845
+ this.element.querySelectorAll(':scope > option, :scope > optgroup').forEach(function (e) {
846
+ if (isHtmlOption(e)) {
847
+ choices.push(_this._optionToChoice(e));
848
+ }
849
+ else if (isHtmlOptgroup(e)) {
850
+ choices.push(_this._optgroupToChoice(e));
851
+ }
852
+ // todo: hr as empty optgroup, requires displaying empty opt-groups to be useful
853
+ });
854
+ return choices;
855
+ };
856
+ // eslint-disable-next-line class-methods-use-this
857
+ WrappedSelect.prototype._optionToChoice = function (option) {
858
+ // option.value returns the label if there is no value attribute, which can break legacy placeholder attribute support
859
+ if (!option.hasAttribute('value') && option.hasAttribute('placeholder')) {
860
+ option.setAttribute('value', '');
861
+ option.value = '';
862
+ }
863
+ return {
864
+ id: 0,
865
+ group: null,
866
+ score: 0,
867
+ rank: 0,
868
+ value: option.value,
869
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
870
+ // This attribute is text for the label indicating the meaning of the option. If the `label` attribute isn't defined, its value is that of the element text content (ie `innerText`).
871
+ label: option.label,
872
+ element: option,
873
+ active: true,
874
+ // this returns true if nothing is selected on initial load, which will break placeholder support
875
+ selected: this.extractPlaceholder ? option.selected : option.hasAttribute('selected'),
876
+ disabled: option.disabled,
877
+ highlighted: false,
878
+ placeholder: this.extractPlaceholder && (!option.value || option.hasAttribute('placeholder')),
879
+ labelClass: typeof option.dataset.labelClass !== 'undefined' ? stringToHtmlClass(option.dataset.labelClass) : undefined,
880
+ labelDescription: typeof option.dataset.labelDescription !== 'undefined' ? option.dataset.labelDescription : undefined,
881
+ customProperties: parseCustomProperties(option.dataset.customProperties),
882
+ };
883
+ };
884
+ WrappedSelect.prototype._optgroupToChoice = function (optgroup) {
885
+ var _this = this;
886
+ var options = optgroup.querySelectorAll('option');
887
+ var choices = Array.from(options).map(function (option) { return _this._optionToChoice(option); });
888
+ return {
889
+ id: 0,
890
+ label: optgroup.label || '',
891
+ element: optgroup,
892
+ active: !!choices.length,
893
+ disabled: optgroup.disabled,
894
+ choices: choices,
895
+ };
896
+ };
897
+ return WrappedSelect;
898
+ }(WrappedElement));
899
+
900
+ var DEFAULT_CLASSNAMES = {
901
+ containerOuter: ['choices'],
902
+ containerInner: ['choices__inner'],
903
+ input: ['choices__input'],
904
+ inputCloned: ['choices__input--cloned'],
905
+ list: ['choices__list'],
906
+ listItems: ['choices__list--multiple'],
907
+ listSingle: ['choices__list--single'],
908
+ listDropdown: ['choices__list--dropdown'],
909
+ item: ['choices__item'],
910
+ itemSelectable: ['choices__item--selectable'],
911
+ itemDisabled: ['choices__item--disabled'],
912
+ itemChoice: ['choices__item--choice'],
913
+ description: ['choices__description'],
914
+ placeholder: ['choices__placeholder'],
915
+ group: ['choices__group'],
916
+ groupHeading: ['choices__heading'],
917
+ button: ['choices__button'],
918
+ activeState: ['is-active'],
919
+ focusState: ['is-focused'],
920
+ openState: ['is-open'],
921
+ disabledState: ['is-disabled'],
922
+ highlightedState: ['is-highlighted'],
923
+ selectedState: ['is-selected'],
924
+ flippedState: ['is-flipped'],
925
+ loadingState: ['is-loading'],
926
+ notice: ['choices__notice'],
927
+ addChoice: ['choices__item--selectable', 'add-choice'],
928
+ noResults: ['has-no-results'],
929
+ noChoices: ['has-no-choices'],
930
+ };
931
+ var DEFAULT_CONFIG = {
932
+ items: [],
933
+ choices: [],
934
+ silent: false,
935
+ renderChoiceLimit: -1,
936
+ maxItemCount: -1,
937
+ closeDropdownOnSelect: 'auto',
938
+ singleModeForMultiSelect: false,
939
+ addChoices: false,
940
+ addItems: true,
941
+ addItemFilter: function (value) { return !!value && value !== ''; },
942
+ removeItems: true,
943
+ removeItemButton: false,
944
+ removeItemButtonAlignLeft: false,
945
+ editItems: false,
946
+ allowHTML: false,
947
+ allowHtmlUserInput: false,
948
+ duplicateItemsAllowed: true,
949
+ delimiter: ',',
950
+ paste: true,
951
+ searchEnabled: true,
952
+ searchChoices: true,
953
+ searchFloor: 1,
954
+ searchResultLimit: 4,
955
+ searchFields: ['label', 'value'],
956
+ position: 'auto',
957
+ resetScrollPosition: true,
958
+ shouldSort: true,
959
+ shouldSortItems: false,
960
+ sorter: sortByAlpha,
961
+ shadowRoot: null,
962
+ placeholder: true,
963
+ placeholderValue: null,
964
+ searchPlaceholderValue: null,
965
+ prependValue: null,
966
+ appendValue: null,
967
+ renderSelectedChoices: 'auto',
968
+ loadingText: 'Loading...',
969
+ noResultsText: 'No results found',
970
+ noChoicesText: 'No choices to choose from',
971
+ itemSelectText: 'Press to select',
972
+ uniqueItemText: 'Only unique values can be added',
973
+ customAddItemText: 'Only values matching specific conditions can be added',
974
+ addItemText: function (value) { return "Press Enter to add <b>\"".concat(value, "\"</b>"); },
975
+ removeItemIconText: function () { return "Remove item"; },
976
+ removeItemLabelText: function (value) { return "Remove item: ".concat(value); },
977
+ maxItemText: function (maxItemCount) { return "Only ".concat(maxItemCount, " values can be added"); },
978
+ valueComparer: function (value1, value2) { return value1 === value2; },
979
+ fuseOptions: {
980
+ includeScore: true,
981
+ },
982
+ labelId: '',
983
+ callbackOnInit: null,
984
+ callbackOnCreateTemplates: null,
985
+ classNames: DEFAULT_CLASSNAMES,
986
+ appendGroupInSearch: false,
987
+ };
988
+
989
+ var removeItem = function (item) {
990
+ var itemEl = item.itemEl;
991
+ if (itemEl) {
992
+ itemEl.remove();
993
+ item.itemEl = undefined;
994
+ }
995
+ };
996
+ function items(s, action, context) {
997
+ var state = s;
998
+ var update = true;
999
+ switch (action.type) {
1000
+ case ActionType.ADD_ITEM: {
1001
+ action.item.selected = true;
1002
+ var el = action.item.element;
1003
+ if (el) {
1004
+ el.selected = true;
1005
+ el.setAttribute('selected', '');
1006
+ }
1007
+ state.push(action.item);
1008
+ break;
1009
+ }
1010
+ case ActionType.REMOVE_ITEM: {
1011
+ action.item.selected = false;
1012
+ var el = action.item.element;
1013
+ if (el) {
1014
+ el.selected = false;
1015
+ el.removeAttribute('selected');
1016
+ // For a select-one, if all options are deselected, the first item is selected. To set a black value, select.value needs to be set
1017
+ var select = el.parentElement;
1018
+ if (select && isHtmlSelectElement(select) && select.type === PassedElementTypes.SelectOne) {
1019
+ select.value = '';
1020
+ }
1021
+ }
1022
+ // this is mixing concerns, but this is *so much faster*
1023
+ removeItem(action.item);
1024
+ state = state.filter(function (choice) { return choice.id !== action.item.id; });
1025
+ break;
1026
+ }
1027
+ case ActionType.REMOVE_CHOICE: {
1028
+ removeItem(action.choice);
1029
+ state = state.filter(function (item) { return item.id !== action.choice.id; });
1030
+ break;
1031
+ }
1032
+ case ActionType.HIGHLIGHT_ITEM: {
1033
+ var highlighted = action.highlighted;
1034
+ var item = state.find(function (obj) { return obj.id === action.item.id; });
1035
+ if (item && item.highlighted !== highlighted) {
1036
+ item.highlighted = highlighted;
1037
+ if (context) {
1038
+ updateClassList(item, highlighted ? context.classNames.highlightedState : context.classNames.selectedState, highlighted ? context.classNames.selectedState : context.classNames.highlightedState);
1039
+ }
1040
+ }
1041
+ break;
1042
+ }
1043
+ default: {
1044
+ update = false;
1045
+ break;
1046
+ }
1047
+ }
1048
+ return { state: state, update: update };
1049
+ }
1050
+
1051
+ function groups(s, action) {
1052
+ var state = s;
1053
+ var update = true;
1054
+ switch (action.type) {
1055
+ case ActionType.ADD_GROUP: {
1056
+ state.push(action.group);
1057
+ break;
1058
+ }
1059
+ case ActionType.CLEAR_CHOICES: {
1060
+ state = [];
1061
+ break;
1062
+ }
1063
+ default: {
1064
+ update = false;
1065
+ break;
1066
+ }
1067
+ }
1068
+ return { state: state, update: update };
1069
+ }
1070
+
1071
+ /* eslint-disable */
1072
+ function choices(s, action, context) {
1073
+ var state = s;
1074
+ var update = true;
1075
+ switch (action.type) {
1076
+ case ActionType.ADD_CHOICE: {
1077
+ state.push(action.choice);
1078
+ break;
1079
+ }
1080
+ case ActionType.REMOVE_CHOICE: {
1081
+ action.choice.choiceEl = undefined;
1082
+ if (action.choice.group) {
1083
+ action.choice.group.choices = action.choice.group.choices.filter(function (obj) { return obj.id !== action.choice.id; });
1084
+ }
1085
+ state = state.filter(function (obj) { return obj.id !== action.choice.id; });
1086
+ break;
1087
+ }
1088
+ case ActionType.ADD_ITEM:
1089
+ case ActionType.REMOVE_ITEM: {
1090
+ action.item.choiceEl = undefined;
1091
+ break;
1092
+ }
1093
+ case ActionType.FILTER_CHOICES: {
1094
+ // avoid O(n^2) algorithm complexity when searching/filtering choices
1095
+ var scoreLookup_1 = [];
1096
+ action.results.forEach(function (result) {
1097
+ scoreLookup_1[result.item.id] = result;
1098
+ });
1099
+ state.forEach(function (choice) {
1100
+ var result = scoreLookup_1[choice.id];
1101
+ if (result !== undefined) {
1102
+ choice.score = result.score;
1103
+ choice.rank = result.rank;
1104
+ choice.active = true;
1105
+ }
1106
+ else {
1107
+ choice.score = 0;
1108
+ choice.rank = 0;
1109
+ choice.active = false;
1110
+ }
1111
+ if (context && context.appendGroupInSearch) {
1112
+ choice.choiceEl = undefined;
1113
+ }
1114
+ });
1115
+ break;
1116
+ }
1117
+ case ActionType.ACTIVATE_CHOICES: {
1118
+ state.forEach(function (choice) {
1119
+ choice.active = action.active;
1120
+ if (context && context.appendGroupInSearch) {
1121
+ choice.choiceEl = undefined;
1122
+ }
1123
+ });
1124
+ break;
1125
+ }
1126
+ case ActionType.CLEAR_CHOICES: {
1127
+ state = [];
1128
+ break;
1129
+ }
1130
+ default: {
1131
+ update = false;
1132
+ break;
1133
+ }
1134
+ }
1135
+ return { state: state, update: update };
1136
+ }
1137
+
1138
+ var reducers = {
1139
+ groups: groups,
1140
+ items: items,
1141
+ choices: choices,
1142
+ };
1143
+ var Store = /** @class */ (function () {
1144
+ function Store(context) {
1145
+ this._state = this.defaultState;
1146
+ this._listeners = [];
1147
+ this._txn = 0;
1148
+ this._context = context;
1149
+ }
1150
+ Object.defineProperty(Store.prototype, "defaultState", {
1151
+ // eslint-disable-next-line class-methods-use-this
1152
+ get: function () {
1153
+ return {
1154
+ groups: [],
1155
+ items: [],
1156
+ choices: [],
1157
+ };
1158
+ },
1159
+ enumerable: false,
1160
+ configurable: true
1161
+ });
1162
+ // eslint-disable-next-line class-methods-use-this
1163
+ Store.prototype.changeSet = function (init) {
1164
+ return {
1165
+ groups: init,
1166
+ items: init,
1167
+ choices: init,
1168
+ };
1169
+ };
1170
+ Store.prototype.reset = function () {
1171
+ this._state = this.defaultState;
1172
+ var changes = this.changeSet(true);
1173
+ if (this._txn) {
1174
+ this._changeSet = changes;
1175
+ }
1176
+ else {
1177
+ this._listeners.forEach(function (l) { return l(changes); });
1178
+ }
1179
+ };
1180
+ Store.prototype.subscribe = function (onChange) {
1181
+ this._listeners.push(onChange);
1182
+ return this;
1183
+ };
1184
+ Store.prototype.dispatch = function (action) {
1185
+ var _this = this;
1186
+ var state = this._state;
1187
+ var hasChanges = false;
1188
+ var changes = this._changeSet || this.changeSet(false);
1189
+ Object.keys(reducers).forEach(function (key) {
1190
+ var stateUpdate = reducers[key](state[key], action, _this._context);
1191
+ if (stateUpdate.update) {
1192
+ hasChanges = true;
1193
+ changes[key] = true;
1194
+ state[key] = stateUpdate.state;
1195
+ }
1196
+ });
1197
+ if (hasChanges) {
1198
+ if (this._txn) {
1199
+ this._changeSet = changes;
1200
+ }
1201
+ else {
1202
+ this._listeners.forEach(function (l) { return l(changes); });
1203
+ }
1204
+ }
1205
+ };
1206
+ Store.prototype.withTxn = function (func) {
1207
+ this._txn++;
1208
+ try {
1209
+ func();
1210
+ }
1211
+ finally {
1212
+ this._txn = Math.max(0, this._txn - 1);
1213
+ if (!this._txn) {
1214
+ var changeSet_1 = this._changeSet;
1215
+ if (changeSet_1) {
1216
+ this._changeSet = undefined;
1217
+ this._listeners.forEach(function (l) { return l(changeSet_1); });
1218
+ }
1219
+ }
1220
+ }
1221
+ };
1222
+ Object.defineProperty(Store.prototype, "state", {
1223
+ /**
1224
+ * Get store object
1225
+ */
1226
+ get: function () {
1227
+ return this._state;
1228
+ },
1229
+ enumerable: false,
1230
+ configurable: true
1231
+ });
1232
+ Object.defineProperty(Store.prototype, "items", {
1233
+ /**
1234
+ * Get items from store
1235
+ */
1236
+ get: function () {
1237
+ return this.state.items;
1238
+ },
1239
+ enumerable: false,
1240
+ configurable: true
1241
+ });
1242
+ Object.defineProperty(Store.prototype, "highlightedActiveItems", {
1243
+ /**
1244
+ * Get highlighted items from store
1245
+ */
1246
+ get: function () {
1247
+ return this.items.filter(function (item) { return item.active && item.highlighted; });
1248
+ },
1249
+ enumerable: false,
1250
+ configurable: true
1251
+ });
1252
+ Object.defineProperty(Store.prototype, "choices", {
1253
+ /**
1254
+ * Get choices from store
1255
+ */
1256
+ get: function () {
1257
+ return this.state.choices;
1258
+ },
1259
+ enumerable: false,
1260
+ configurable: true
1261
+ });
1262
+ Object.defineProperty(Store.prototype, "activeChoices", {
1263
+ /**
1264
+ * Get active choices from store
1265
+ */
1266
+ get: function () {
1267
+ return this.choices.filter(function (choice) { return choice.active; });
1268
+ },
1269
+ enumerable: false,
1270
+ configurable: true
1271
+ });
1272
+ Object.defineProperty(Store.prototype, "searchableChoices", {
1273
+ /**
1274
+ * Get choices that can be searched (excluding placeholders or disabled choices)
1275
+ */
1276
+ get: function () {
1277
+ return this.choices.filter(function (choice) { return !choice.disabled && !choice.placeholder; });
1278
+ },
1279
+ enumerable: false,
1280
+ configurable: true
1281
+ });
1282
+ Object.defineProperty(Store.prototype, "groups", {
1283
+ /**
1284
+ * Get groups from store
1285
+ */
1286
+ get: function () {
1287
+ return this.state.groups;
1288
+ },
1289
+ enumerable: false,
1290
+ configurable: true
1291
+ });
1292
+ Object.defineProperty(Store.prototype, "activeGroups", {
1293
+ /**
1294
+ * Get active groups from store
1295
+ */
1296
+ get: function () {
1297
+ var _this = this;
1298
+ return this.state.groups.filter(function (group) {
1299
+ var isActive = group.active && !group.disabled;
1300
+ var hasActiveOptions = _this.state.choices.some(function (choice) { return choice.active && !choice.disabled; });
1301
+ return isActive && hasActiveOptions;
1302
+ }, []);
1303
+ },
1304
+ enumerable: false,
1305
+ configurable: true
1306
+ });
1307
+ Store.prototype.inTxn = function () {
1308
+ return this._txn > 0;
1309
+ };
1310
+ /**
1311
+ * Get single choice by it's ID
1312
+ */
1313
+ Store.prototype.getChoiceById = function (id) {
1314
+ return this.activeChoices.find(function (choice) { return choice.id === id; });
1315
+ };
1316
+ /**
1317
+ * Get group by group id
1318
+ */
1319
+ Store.prototype.getGroupById = function (id) {
1320
+ return this.groups.find(function (group) { return group.id === id; });
1321
+ };
1322
+ return Store;
1323
+ }());
1324
+
1325
+ var NoticeTypes = {
1326
+ noChoices: 'no-choices',
1327
+ noResults: 'no-results',
1328
+ addChoice: 'add-choice',
1329
+ generic: '',
1330
+ };
1331
+
1332
+ function _defineProperty(e, r, t) {
1333
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
1334
+ value: t,
1335
+ enumerable: !0,
1336
+ configurable: !0,
1337
+ writable: !0
1338
+ }) : e[r] = t, e;
1339
+ }
1340
+ function ownKeys(e, r) {
1341
+ var t = Object.keys(e);
1342
+ if (Object.getOwnPropertySymbols) {
1343
+ var o = Object.getOwnPropertySymbols(e);
1344
+ r && (o = o.filter(function (r) {
1345
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
1346
+ })), t.push.apply(t, o);
1347
+ }
1348
+ return t;
1349
+ }
1350
+ function _objectSpread2(e) {
1351
+ for (var r = 1; r < arguments.length; r++) {
1352
+ var t = null != arguments[r] ? arguments[r] : {};
1353
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
1354
+ _defineProperty(e, r, t[r]);
1355
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
1356
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
1357
+ });
1358
+ }
1359
+ return e;
1360
+ }
1361
+ function _toPrimitive(t, r) {
1362
+ if ("object" != typeof t || !t) return t;
1363
+ var e = t[Symbol.toPrimitive];
1364
+ if (void 0 !== e) {
1365
+ var i = e.call(t, r || "default");
1366
+ if ("object" != typeof i) return i;
1367
+ throw new TypeError("@@toPrimitive must return a primitive value.");
1368
+ }
1369
+ return ("string" === r ? String : Number)(t);
1370
+ }
1371
+ function _toPropertyKey(t) {
1372
+ var i = _toPrimitive(t, "string");
1373
+ return "symbol" == typeof i ? i : i + "";
1374
+ }
1375
+
1376
+ /**
1377
+ * Fuse.js v7.0.0 - Lightweight fuzzy-search (http://fusejs.io)
1378
+ *
1379
+ * Copyright (c) 2023 Kiro Risk (http://kiro.me)
1380
+ * All Rights Reserved. Apache Software License 2.0
1381
+ *
1382
+ * http://www.apache.org/licenses/LICENSE-2.0
1383
+ */
1384
+
1385
+ function isArray(value) {
1386
+ return !Array.isArray ? getTag(value) === '[object Array]' : Array.isArray(value);
1387
+ }
1388
+
1389
+ // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
1390
+ const INFINITY = 1 / 0;
1391
+ function baseToString(value) {
1392
+ // Exit early for strings to avoid a performance hit in some environments.
1393
+ if (typeof value == 'string') {
1394
+ return value;
1395
+ }
1396
+ let result = value + '';
1397
+ return result == '0' && 1 / value == -INFINITY ? '-0' : result;
1398
+ }
1399
+ function toString(value) {
1400
+ return value == null ? '' : baseToString(value);
1401
+ }
1402
+ function isString(value) {
1403
+ return typeof value === 'string';
1404
+ }
1405
+ function isNumber(value) {
1406
+ return typeof value === 'number';
1407
+ }
1408
+
1409
+ // Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
1410
+ function isBoolean(value) {
1411
+ return value === true || value === false || isObjectLike(value) && getTag(value) == '[object Boolean]';
1412
+ }
1413
+ function isObject(value) {
1414
+ return typeof value === 'object';
1415
+ }
1416
+
1417
+ // Checks if `value` is object-like.
1418
+ function isObjectLike(value) {
1419
+ return isObject(value) && value !== null;
1420
+ }
1421
+ function isDefined(value) {
1422
+ return value !== undefined && value !== null;
1423
+ }
1424
+ function isBlank(value) {
1425
+ return !value.trim().length;
1426
+ }
1427
+
1428
+ // Gets the `toStringTag` of `value`.
1429
+ // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
1430
+ function getTag(value) {
1431
+ return value == null ? value === undefined ? '[object Undefined]' : '[object Null]' : Object.prototype.toString.call(value);
1432
+ }
1433
+ const EXTENDED_SEARCH_UNAVAILABLE = 'Extended search is not available';
1434
+ const LOGICAL_SEARCH_UNAVAILABLE = 'Logical search is not available';
1435
+ const INCORRECT_INDEX_TYPE = "Incorrect 'index' type";
1436
+ const LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY = key => `Invalid value for key ${key}`;
1437
+ const PATTERN_LENGTH_TOO_LARGE = max => `Pattern length exceeds max of ${max}.`;
1438
+ const MISSING_KEY_PROPERTY = name => `Missing ${name} property in key`;
1439
+ const INVALID_KEY_WEIGHT_VALUE = key => `Property 'weight' in key '${key}' must be a positive integer`;
1440
+ const hasOwn = Object.prototype.hasOwnProperty;
1441
+ class KeyStore {
1442
+ constructor(keys) {
1443
+ this._keys = [];
1444
+ this._keyMap = {};
1445
+ let totalWeight = 0;
1446
+ keys.forEach(key => {
1447
+ let obj = createKey(key);
1448
+ this._keys.push(obj);
1449
+ this._keyMap[obj.id] = obj;
1450
+ totalWeight += obj.weight;
1451
+ });
1452
+
1453
+ // Normalize weights so that their sum is equal to 1
1454
+ this._keys.forEach(key => {
1455
+ key.weight /= totalWeight;
1456
+ });
1457
+ }
1458
+ get(keyId) {
1459
+ return this._keyMap[keyId];
1460
+ }
1461
+ keys() {
1462
+ return this._keys;
1463
+ }
1464
+ toJSON() {
1465
+ return JSON.stringify(this._keys);
1466
+ }
1467
+ }
1468
+ function createKey(key) {
1469
+ let path = null;
1470
+ let id = null;
1471
+ let src = null;
1472
+ let weight = 1;
1473
+ let getFn = null;
1474
+ if (isString(key) || isArray(key)) {
1475
+ src = key;
1476
+ path = createKeyPath(key);
1477
+ id = createKeyId(key);
1478
+ } else {
1479
+ if (!hasOwn.call(key, 'name')) {
1480
+ throw new Error(MISSING_KEY_PROPERTY('name'));
1481
+ }
1482
+ const name = key.name;
1483
+ src = name;
1484
+ if (hasOwn.call(key, 'weight')) {
1485
+ weight = key.weight;
1486
+ if (weight <= 0) {
1487
+ throw new Error(INVALID_KEY_WEIGHT_VALUE(name));
1488
+ }
1489
+ }
1490
+ path = createKeyPath(name);
1491
+ id = createKeyId(name);
1492
+ getFn = key.getFn;
1493
+ }
1494
+ return {
1495
+ path,
1496
+ id,
1497
+ weight,
1498
+ src,
1499
+ getFn
1500
+ };
1501
+ }
1502
+ function createKeyPath(key) {
1503
+ return isArray(key) ? key : key.split('.');
1504
+ }
1505
+ function createKeyId(key) {
1506
+ return isArray(key) ? key.join('.') : key;
1507
+ }
1508
+ function get(obj, path) {
1509
+ let list = [];
1510
+ let arr = false;
1511
+ const deepGet = (obj, path, index) => {
1512
+ if (!isDefined(obj)) {
1513
+ return;
1514
+ }
1515
+ if (!path[index]) {
1516
+ // If there's no path left, we've arrived at the object we care about.
1517
+ list.push(obj);
1518
+ } else {
1519
+ let key = path[index];
1520
+ const value = obj[key];
1521
+ if (!isDefined(value)) {
1522
+ return;
1523
+ }
1524
+
1525
+ // If we're at the last value in the path, and if it's a string/number/bool,
1526
+ // add it to the list
1527
+ if (index === path.length - 1 && (isString(value) || isNumber(value) || isBoolean(value))) {
1528
+ list.push(toString(value));
1529
+ } else if (isArray(value)) {
1530
+ arr = true;
1531
+ // Search each item in the array.
1532
+ for (let i = 0, len = value.length; i < len; i += 1) {
1533
+ deepGet(value[i], path, index + 1);
1534
+ }
1535
+ } else if (path.length) {
1536
+ // An object. Recurse further.
1537
+ deepGet(value, path, index + 1);
1538
+ }
1539
+ }
1540
+ };
1541
+
1542
+ // Backwards compatibility (since path used to be a string)
1543
+ deepGet(obj, isString(path) ? path.split('.') : path, 0);
1544
+ return arr ? list : list[0];
1545
+ }
1546
+ const MatchOptions = {
1547
+ // Whether the matches should be included in the result set. When `true`, each record in the result
1548
+ // set will include the indices of the matched characters.
1549
+ // These can consequently be used for highlighting purposes.
1550
+ includeMatches: false,
1551
+ // When `true`, the matching function will continue to the end of a search pattern even if
1552
+ // a perfect match has already been located in the string.
1553
+ findAllMatches: false,
1554
+ // Minimum number of characters that must be matched before a result is considered a match
1555
+ minMatchCharLength: 1
1556
+ };
1557
+ const BasicOptions = {
1558
+ // When `true`, the algorithm continues searching to the end of the input even if a perfect
1559
+ // match is found before the end of the same input.
1560
+ isCaseSensitive: false,
1561
+ // When true, the matching function will continue to the end of a search pattern even if
1562
+ includeScore: false,
1563
+ // List of properties that will be searched. This also supports nested properties.
1564
+ keys: [],
1565
+ // Whether to sort the result list, by score
1566
+ shouldSort: true,
1567
+ // Default sort function: sort by ascending score, ascending index
1568
+ sortFn: (a, b) => a.score === b.score ? a.idx < b.idx ? -1 : 1 : a.score < b.score ? -1 : 1
1569
+ };
1570
+ const FuzzyOptions = {
1571
+ // Approximately where in the text is the pattern expected to be found?
1572
+ location: 0,
1573
+ // At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
1574
+ // (of both letters and location), a threshold of '1.0' would match anything.
1575
+ threshold: 0.6,
1576
+ // Determines how close the match must be to the fuzzy location (specified above).
1577
+ // An exact letter match which is 'distance' characters away from the fuzzy location
1578
+ // would score as a complete mismatch. A distance of '0' requires the match be at
1579
+ // the exact location specified, a threshold of '1000' would require a perfect match
1580
+ // to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.
1581
+ distance: 100
1582
+ };
1583
+ const AdvancedOptions = {
1584
+ // When `true`, it enables the use of unix-like search commands
1585
+ useExtendedSearch: false,
1586
+ // The get function to use when fetching an object's properties.
1587
+ // The default will search nested paths *ie foo.bar.baz*
1588
+ getFn: get,
1589
+ // When `true`, search will ignore `location` and `distance`, so it won't matter
1590
+ // where in the string the pattern appears.
1591
+ // More info: https://fusejs.io/concepts/scoring-theory.html#fuzziness-score
1592
+ ignoreLocation: false,
1593
+ // When `true`, the calculation for the relevance score (used for sorting) will
1594
+ // ignore the field-length norm.
1595
+ // More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
1596
+ ignoreFieldNorm: false,
1597
+ // The weight to determine how much field length norm effects scoring.
1598
+ fieldNormWeight: 1
1599
+ };
1600
+ var Config = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({}, BasicOptions), MatchOptions), FuzzyOptions), AdvancedOptions);
1601
+ const SPACE = /[^ ]+/g;
1602
+
1603
+ // Field-length norm: the shorter the field, the higher the weight.
1604
+ // Set to 3 decimals to reduce index size.
1605
+ function norm(weight = 1, mantissa = 3) {
1606
+ const cache = new Map();
1607
+ const m = Math.pow(10, mantissa);
1608
+ return {
1609
+ get(value) {
1610
+ const numTokens = value.match(SPACE).length;
1611
+ if (cache.has(numTokens)) {
1612
+ return cache.get(numTokens);
1613
+ }
1614
+
1615
+ // Default function is 1/sqrt(x), weight makes that variable
1616
+ const norm = 1 / Math.pow(numTokens, 0.5 * weight);
1617
+
1618
+ // In place of `toFixed(mantissa)`, for faster computation
1619
+ const n = parseFloat(Math.round(norm * m) / m);
1620
+ cache.set(numTokens, n);
1621
+ return n;
1622
+ },
1623
+ clear() {
1624
+ cache.clear();
1625
+ }
1626
+ };
1627
+ }
1628
+ class FuseIndex {
1629
+ constructor({
1630
+ getFn = Config.getFn,
1631
+ fieldNormWeight = Config.fieldNormWeight
1632
+ } = {}) {
1633
+ this.norm = norm(fieldNormWeight, 3);
1634
+ this.getFn = getFn;
1635
+ this.isCreated = false;
1636
+ this.setIndexRecords();
1637
+ }
1638
+ setSources(docs = []) {
1639
+ this.docs = docs;
1640
+ }
1641
+ setIndexRecords(records = []) {
1642
+ this.records = records;
1643
+ }
1644
+ setKeys(keys = []) {
1645
+ this.keys = keys;
1646
+ this._keysMap = {};
1647
+ keys.forEach((key, idx) => {
1648
+ this._keysMap[key.id] = idx;
1649
+ });
1650
+ }
1651
+ create() {
1652
+ if (this.isCreated || !this.docs.length) {
1653
+ return;
1654
+ }
1655
+ this.isCreated = true;
1656
+
1657
+ // List is Array<String>
1658
+ if (isString(this.docs[0])) {
1659
+ this.docs.forEach((doc, docIndex) => {
1660
+ this._addString(doc, docIndex);
1661
+ });
1662
+ } else {
1663
+ // List is Array<Object>
1664
+ this.docs.forEach((doc, docIndex) => {
1665
+ this._addObject(doc, docIndex);
1666
+ });
1667
+ }
1668
+ this.norm.clear();
1669
+ }
1670
+ // Adds a doc to the end of the index
1671
+ add(doc) {
1672
+ const idx = this.size();
1673
+ if (isString(doc)) {
1674
+ this._addString(doc, idx);
1675
+ } else {
1676
+ this._addObject(doc, idx);
1677
+ }
1678
+ }
1679
+ // Removes the doc at the specified index of the index
1680
+ removeAt(idx) {
1681
+ this.records.splice(idx, 1);
1682
+
1683
+ // Change ref index of every subsquent doc
1684
+ for (let i = idx, len = this.size(); i < len; i += 1) {
1685
+ this.records[i].i -= 1;
1686
+ }
1687
+ }
1688
+ getValueForItemAtKeyId(item, keyId) {
1689
+ return item[this._keysMap[keyId]];
1690
+ }
1691
+ size() {
1692
+ return this.records.length;
1693
+ }
1694
+ _addString(doc, docIndex) {
1695
+ if (!isDefined(doc) || isBlank(doc)) {
1696
+ return;
1697
+ }
1698
+ let record = {
1699
+ v: doc,
1700
+ i: docIndex,
1701
+ n: this.norm.get(doc)
1702
+ };
1703
+ this.records.push(record);
1704
+ }
1705
+ _addObject(doc, docIndex) {
1706
+ let record = {
1707
+ i: docIndex,
1708
+ $: {}
1709
+ };
1710
+
1711
+ // Iterate over every key (i.e, path), and fetch the value at that key
1712
+ this.keys.forEach((key, keyIndex) => {
1713
+ let value = key.getFn ? key.getFn(doc) : this.getFn(doc, key.path);
1714
+ if (!isDefined(value)) {
1715
+ return;
1716
+ }
1717
+ if (isArray(value)) {
1718
+ let subRecords = [];
1719
+ const stack = [{
1720
+ nestedArrIndex: -1,
1721
+ value
1722
+ }];
1723
+ while (stack.length) {
1724
+ const {
1725
+ nestedArrIndex,
1726
+ value
1727
+ } = stack.pop();
1728
+ if (!isDefined(value)) {
1729
+ continue;
1730
+ }
1731
+ if (isString(value) && !isBlank(value)) {
1732
+ let subRecord = {
1733
+ v: value,
1734
+ i: nestedArrIndex,
1735
+ n: this.norm.get(value)
1736
+ };
1737
+ subRecords.push(subRecord);
1738
+ } else if (isArray(value)) {
1739
+ value.forEach((item, k) => {
1740
+ stack.push({
1741
+ nestedArrIndex: k,
1742
+ value: item
1743
+ });
1744
+ });
1745
+ } else ;
1746
+ }
1747
+ record.$[keyIndex] = subRecords;
1748
+ } else if (isString(value) && !isBlank(value)) {
1749
+ let subRecord = {
1750
+ v: value,
1751
+ n: this.norm.get(value)
1752
+ };
1753
+ record.$[keyIndex] = subRecord;
1754
+ }
1755
+ });
1756
+ this.records.push(record);
1757
+ }
1758
+ toJSON() {
1759
+ return {
1760
+ keys: this.keys,
1761
+ records: this.records
1762
+ };
1763
+ }
1764
+ }
1765
+ function createIndex(keys, docs, {
1766
+ getFn = Config.getFn,
1767
+ fieldNormWeight = Config.fieldNormWeight
1768
+ } = {}) {
1769
+ const myIndex = new FuseIndex({
1770
+ getFn,
1771
+ fieldNormWeight
1772
+ });
1773
+ myIndex.setKeys(keys.map(createKey));
1774
+ myIndex.setSources(docs);
1775
+ myIndex.create();
1776
+ return myIndex;
1777
+ }
1778
+ function parseIndex(data, {
1779
+ getFn = Config.getFn,
1780
+ fieldNormWeight = Config.fieldNormWeight
1781
+ } = {}) {
1782
+ const {
1783
+ keys,
1784
+ records
1785
+ } = data;
1786
+ const myIndex = new FuseIndex({
1787
+ getFn,
1788
+ fieldNormWeight
1789
+ });
1790
+ myIndex.setKeys(keys);
1791
+ myIndex.setIndexRecords(records);
1792
+ return myIndex;
1793
+ }
1794
+ function computeScore$1(pattern, {
1795
+ errors = 0,
1796
+ currentLocation = 0,
1797
+ expectedLocation = 0,
1798
+ distance = Config.distance,
1799
+ ignoreLocation = Config.ignoreLocation
1800
+ } = {}) {
1801
+ const accuracy = errors / pattern.length;
1802
+ if (ignoreLocation) {
1803
+ return accuracy;
1804
+ }
1805
+ const proximity = Math.abs(expectedLocation - currentLocation);
1806
+ if (!distance) {
1807
+ // Dodge divide by zero error.
1808
+ return proximity ? 1.0 : accuracy;
1809
+ }
1810
+ return accuracy + proximity / distance;
1811
+ }
1812
+ function convertMaskToIndices(matchmask = [], minMatchCharLength = Config.minMatchCharLength) {
1813
+ let indices = [];
1814
+ let start = -1;
1815
+ let end = -1;
1816
+ let i = 0;
1817
+ for (let len = matchmask.length; i < len; i += 1) {
1818
+ let match = matchmask[i];
1819
+ if (match && start === -1) {
1820
+ start = i;
1821
+ } else if (!match && start !== -1) {
1822
+ end = i - 1;
1823
+ if (end - start + 1 >= minMatchCharLength) {
1824
+ indices.push([start, end]);
1825
+ }
1826
+ start = -1;
1827
+ }
1828
+ }
1829
+
1830
+ // (i-1 - start) + 1 => i - start
1831
+ if (matchmask[i - 1] && i - start >= minMatchCharLength) {
1832
+ indices.push([start, i - 1]);
1833
+ }
1834
+ return indices;
1835
+ }
1836
+
1837
+ // Machine word size
1838
+ const MAX_BITS = 32;
1839
+ function search(text, pattern, patternAlphabet, {
1840
+ location = Config.location,
1841
+ distance = Config.distance,
1842
+ threshold = Config.threshold,
1843
+ findAllMatches = Config.findAllMatches,
1844
+ minMatchCharLength = Config.minMatchCharLength,
1845
+ includeMatches = Config.includeMatches,
1846
+ ignoreLocation = Config.ignoreLocation
1847
+ } = {}) {
1848
+ if (pattern.length > MAX_BITS) {
1849
+ throw new Error(PATTERN_LENGTH_TOO_LARGE(MAX_BITS));
1850
+ }
1851
+ const patternLen = pattern.length;
1852
+ // Set starting location at beginning text and initialize the alphabet.
1853
+ const textLen = text.length;
1854
+ // Handle the case when location > text.length
1855
+ const expectedLocation = Math.max(0, Math.min(location, textLen));
1856
+ // Highest score beyond which we give up.
1857
+ let currentThreshold = threshold;
1858
+ // Is there a nearby exact match? (speedup)
1859
+ let bestLocation = expectedLocation;
1860
+
1861
+ // Performance: only computer matches when the minMatchCharLength > 1
1862
+ // OR if `includeMatches` is true.
1863
+ const computeMatches = minMatchCharLength > 1 || includeMatches;
1864
+ // A mask of the matches, used for building the indices
1865
+ const matchMask = computeMatches ? Array(textLen) : [];
1866
+ let index;
1867
+
1868
+ // Get all exact matches, here for speed up
1869
+ while ((index = text.indexOf(pattern, bestLocation)) > -1) {
1870
+ let score = computeScore$1(pattern, {
1871
+ currentLocation: index,
1872
+ expectedLocation,
1873
+ distance,
1874
+ ignoreLocation
1875
+ });
1876
+ currentThreshold = Math.min(score, currentThreshold);
1877
+ bestLocation = index + patternLen;
1878
+ if (computeMatches) {
1879
+ let i = 0;
1880
+ while (i < patternLen) {
1881
+ matchMask[index + i] = 1;
1882
+ i += 1;
1883
+ }
1884
+ }
1885
+ }
1886
+
1887
+ // Reset the best location
1888
+ bestLocation = -1;
1889
+ let lastBitArr = [];
1890
+ let finalScore = 1;
1891
+ let binMax = patternLen + textLen;
1892
+ const mask = 1 << patternLen - 1;
1893
+ for (let i = 0; i < patternLen; i += 1) {
1894
+ // Scan for the best match; each iteration allows for one more error.
1895
+ // Run a binary search to determine how far from the match location we can stray
1896
+ // at this error level.
1897
+ let binMin = 0;
1898
+ let binMid = binMax;
1899
+ while (binMin < binMid) {
1900
+ const score = computeScore$1(pattern, {
1901
+ errors: i,
1902
+ currentLocation: expectedLocation + binMid,
1903
+ expectedLocation,
1904
+ distance,
1905
+ ignoreLocation
1906
+ });
1907
+ if (score <= currentThreshold) {
1908
+ binMin = binMid;
1909
+ } else {
1910
+ binMax = binMid;
1911
+ }
1912
+ binMid = Math.floor((binMax - binMin) / 2 + binMin);
1913
+ }
1914
+
1915
+ // Use the result from this iteration as the maximum for the next.
1916
+ binMax = binMid;
1917
+ let start = Math.max(1, expectedLocation - binMid + 1);
1918
+ let finish = findAllMatches ? textLen : Math.min(expectedLocation + binMid, textLen) + patternLen;
1919
+
1920
+ // Initialize the bit array
1921
+ let bitArr = Array(finish + 2);
1922
+ bitArr[finish + 1] = (1 << i) - 1;
1923
+ for (let j = finish; j >= start; j -= 1) {
1924
+ let currentLocation = j - 1;
1925
+ let charMatch = patternAlphabet[text.charAt(currentLocation)];
1926
+ if (computeMatches) {
1927
+ // Speed up: quick bool to int conversion (i.e, `charMatch ? 1 : 0`)
1928
+ matchMask[currentLocation] = +!!charMatch;
1929
+ }
1930
+
1931
+ // First pass: exact match
1932
+ bitArr[j] = (bitArr[j + 1] << 1 | 1) & charMatch;
1933
+
1934
+ // Subsequent passes: fuzzy match
1935
+ if (i) {
1936
+ bitArr[j] |= (lastBitArr[j + 1] | lastBitArr[j]) << 1 | 1 | lastBitArr[j + 1];
1937
+ }
1938
+ if (bitArr[j] & mask) {
1939
+ finalScore = computeScore$1(pattern, {
1940
+ errors: i,
1941
+ currentLocation,
1942
+ expectedLocation,
1943
+ distance,
1944
+ ignoreLocation
1945
+ });
1946
+
1947
+ // This match will almost certainly be better than any existing match.
1948
+ // But check anyway.
1949
+ if (finalScore <= currentThreshold) {
1950
+ // Indeed it is
1951
+ currentThreshold = finalScore;
1952
+ bestLocation = currentLocation;
1953
+
1954
+ // Already passed `loc`, downhill from here on in.
1955
+ if (bestLocation <= expectedLocation) {
1956
+ break;
1957
+ }
1958
+
1959
+ // When passing `bestLocation`, don't exceed our current distance from `expectedLocation`.
1960
+ start = Math.max(1, 2 * expectedLocation - bestLocation);
1961
+ }
1962
+ }
1963
+ }
1964
+
1965
+ // No hope for a (better) match at greater error levels.
1966
+ const score = computeScore$1(pattern, {
1967
+ errors: i + 1,
1968
+ currentLocation: expectedLocation,
1969
+ expectedLocation,
1970
+ distance,
1971
+ ignoreLocation
1972
+ });
1973
+ if (score > currentThreshold) {
1974
+ break;
1975
+ }
1976
+ lastBitArr = bitArr;
1977
+ }
1978
+ const result = {
1979
+ isMatch: bestLocation >= 0,
1980
+ // Count exact matches (those with a score of 0) to be "almost" exact
1981
+ score: Math.max(0.001, finalScore)
1982
+ };
1983
+ if (computeMatches) {
1984
+ const indices = convertMaskToIndices(matchMask, minMatchCharLength);
1985
+ if (!indices.length) {
1986
+ result.isMatch = false;
1987
+ } else if (includeMatches) {
1988
+ result.indices = indices;
1989
+ }
1990
+ }
1991
+ return result;
1992
+ }
1993
+ function createPatternAlphabet(pattern) {
1994
+ let mask = {};
1995
+ for (let i = 0, len = pattern.length; i < len; i += 1) {
1996
+ const char = pattern.charAt(i);
1997
+ mask[char] = (mask[char] || 0) | 1 << len - i - 1;
1998
+ }
1999
+ return mask;
2000
+ }
2001
+ class BitapSearch {
2002
+ constructor(pattern, {
2003
+ location = Config.location,
2004
+ threshold = Config.threshold,
2005
+ distance = Config.distance,
2006
+ includeMatches = Config.includeMatches,
2007
+ findAllMatches = Config.findAllMatches,
2008
+ minMatchCharLength = Config.minMatchCharLength,
2009
+ isCaseSensitive = Config.isCaseSensitive,
2010
+ ignoreLocation = Config.ignoreLocation
2011
+ } = {}) {
2012
+ this.options = {
2013
+ location,
2014
+ threshold,
2015
+ distance,
2016
+ includeMatches,
2017
+ findAllMatches,
2018
+ minMatchCharLength,
2019
+ isCaseSensitive,
2020
+ ignoreLocation
2021
+ };
2022
+ this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
2023
+ this.chunks = [];
2024
+ if (!this.pattern.length) {
2025
+ return;
2026
+ }
2027
+ const addChunk = (pattern, startIndex) => {
2028
+ this.chunks.push({
2029
+ pattern,
2030
+ alphabet: createPatternAlphabet(pattern),
2031
+ startIndex
2032
+ });
2033
+ };
2034
+ const len = this.pattern.length;
2035
+ if (len > MAX_BITS) {
2036
+ let i = 0;
2037
+ const remainder = len % MAX_BITS;
2038
+ const end = len - remainder;
2039
+ while (i < end) {
2040
+ addChunk(this.pattern.substr(i, MAX_BITS), i);
2041
+ i += MAX_BITS;
2042
+ }
2043
+ if (remainder) {
2044
+ const startIndex = len - MAX_BITS;
2045
+ addChunk(this.pattern.substr(startIndex), startIndex);
2046
+ }
2047
+ } else {
2048
+ addChunk(this.pattern, 0);
2049
+ }
2050
+ }
2051
+ searchIn(text) {
2052
+ const {
2053
+ isCaseSensitive,
2054
+ includeMatches
2055
+ } = this.options;
2056
+ if (!isCaseSensitive) {
2057
+ text = text.toLowerCase();
2058
+ }
2059
+
2060
+ // Exact match
2061
+ if (this.pattern === text) {
2062
+ let result = {
2063
+ isMatch: true,
2064
+ score: 0
2065
+ };
2066
+ if (includeMatches) {
2067
+ result.indices = [[0, text.length - 1]];
2068
+ }
2069
+ return result;
2070
+ }
2071
+
2072
+ // Otherwise, use Bitap algorithm
2073
+ const {
2074
+ location,
2075
+ distance,
2076
+ threshold,
2077
+ findAllMatches,
2078
+ minMatchCharLength,
2079
+ ignoreLocation
2080
+ } = this.options;
2081
+ let allIndices = [];
2082
+ let totalScore = 0;
2083
+ let hasMatches = false;
2084
+ this.chunks.forEach(({
2085
+ pattern,
2086
+ alphabet,
2087
+ startIndex
2088
+ }) => {
2089
+ const {
2090
+ isMatch,
2091
+ score,
2092
+ indices
2093
+ } = search(text, pattern, alphabet, {
2094
+ location: location + startIndex,
2095
+ distance,
2096
+ threshold,
2097
+ findAllMatches,
2098
+ minMatchCharLength,
2099
+ includeMatches,
2100
+ ignoreLocation
2101
+ });
2102
+ if (isMatch) {
2103
+ hasMatches = true;
2104
+ }
2105
+ totalScore += score;
2106
+ if (isMatch && indices) {
2107
+ allIndices = [...allIndices, ...indices];
2108
+ }
2109
+ });
2110
+ let result = {
2111
+ isMatch: hasMatches,
2112
+ score: hasMatches ? totalScore / this.chunks.length : 1
2113
+ };
2114
+ if (hasMatches && includeMatches) {
2115
+ result.indices = allIndices;
2116
+ }
2117
+ return result;
2118
+ }
2119
+ }
2120
+ const registeredSearchers = [];
2121
+ function createSearcher(pattern, options) {
2122
+ for (let i = 0, len = registeredSearchers.length; i < len; i += 1) {
2123
+ let searcherClass = registeredSearchers[i];
2124
+ if (searcherClass.condition(pattern, options)) {
2125
+ return new searcherClass(pattern, options);
2126
+ }
2127
+ }
2128
+ return new BitapSearch(pattern, options);
2129
+ }
2130
+ const LogicalOperator = {
2131
+ AND: '$and',
2132
+ OR: '$or'
2133
+ };
2134
+ const KeyType = {
2135
+ PATH: '$path',
2136
+ PATTERN: '$val'
2137
+ };
2138
+ const isExpression = query => !!(query[LogicalOperator.AND] || query[LogicalOperator.OR]);
2139
+ const isPath = query => !!query[KeyType.PATH];
2140
+ const isLeaf = query => !isArray(query) && isObject(query) && !isExpression(query);
2141
+ const convertToExplicit = query => ({
2142
+ [LogicalOperator.AND]: Object.keys(query).map(key => ({
2143
+ [key]: query[key]
2144
+ }))
2145
+ });
2146
+
2147
+ // When `auto` is `true`, the parse function will infer and initialize and add
2148
+ // the appropriate `Searcher` instance
2149
+ function parse(query, options, {
2150
+ auto = true
2151
+ } = {}) {
2152
+ const next = query => {
2153
+ let keys = Object.keys(query);
2154
+ const isQueryPath = isPath(query);
2155
+ if (!isQueryPath && keys.length > 1 && !isExpression(query)) {
2156
+ return next(convertToExplicit(query));
2157
+ }
2158
+ if (isLeaf(query)) {
2159
+ const key = isQueryPath ? query[KeyType.PATH] : keys[0];
2160
+ const pattern = isQueryPath ? query[KeyType.PATTERN] : query[key];
2161
+ if (!isString(pattern)) {
2162
+ throw new Error(LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key));
2163
+ }
2164
+ const obj = {
2165
+ keyId: createKeyId(key),
2166
+ pattern
2167
+ };
2168
+ if (auto) {
2169
+ obj.searcher = createSearcher(pattern, options);
2170
+ }
2171
+ return obj;
2172
+ }
2173
+ let node = {
2174
+ children: [],
2175
+ operator: keys[0]
2176
+ };
2177
+ keys.forEach(key => {
2178
+ const value = query[key];
2179
+ if (isArray(value)) {
2180
+ value.forEach(item => {
2181
+ node.children.push(next(item));
2182
+ });
2183
+ }
2184
+ });
2185
+ return node;
2186
+ };
2187
+ if (!isExpression(query)) {
2188
+ query = convertToExplicit(query);
2189
+ }
2190
+ return next(query);
2191
+ }
2192
+
2193
+ // Practical scoring function
2194
+ function computeScore(results, {
2195
+ ignoreFieldNorm = Config.ignoreFieldNorm
2196
+ }) {
2197
+ results.forEach(result => {
2198
+ let totalScore = 1;
2199
+ result.matches.forEach(({
2200
+ key,
2201
+ norm,
2202
+ score
2203
+ }) => {
2204
+ const weight = key ? key.weight : null;
2205
+ totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
2206
+ });
2207
+ result.score = totalScore;
2208
+ });
2209
+ }
2210
+ function transformMatches(result, data) {
2211
+ const matches = result.matches;
2212
+ data.matches = [];
2213
+ if (!isDefined(matches)) {
2214
+ return;
2215
+ }
2216
+ matches.forEach(match => {
2217
+ if (!isDefined(match.indices) || !match.indices.length) {
2218
+ return;
2219
+ }
2220
+ const {
2221
+ indices,
2222
+ value
2223
+ } = match;
2224
+ let obj = {
2225
+ indices,
2226
+ value
2227
+ };
2228
+ if (match.key) {
2229
+ obj.key = match.key.src;
2230
+ }
2231
+ if (match.idx > -1) {
2232
+ obj.refIndex = match.idx;
2233
+ }
2234
+ data.matches.push(obj);
2235
+ });
2236
+ }
2237
+ function transformScore(result, data) {
2238
+ data.score = result.score;
2239
+ }
2240
+ function format(results, docs, {
2241
+ includeMatches = Config.includeMatches,
2242
+ includeScore = Config.includeScore
2243
+ } = {}) {
2244
+ const transformers = [];
2245
+ if (includeMatches) transformers.push(transformMatches);
2246
+ if (includeScore) transformers.push(transformScore);
2247
+ return results.map(result => {
2248
+ const {
2249
+ idx
2250
+ } = result;
2251
+ const data = {
2252
+ item: docs[idx],
2253
+ refIndex: idx
2254
+ };
2255
+ if (transformers.length) {
2256
+ transformers.forEach(transformer => {
2257
+ transformer(result, data);
2258
+ });
2259
+ }
2260
+ return data;
2261
+ });
2262
+ }
2263
+ class Fuse {
2264
+ constructor(docs, options = {}, index) {
2265
+ this.options = _objectSpread2(_objectSpread2({}, Config), options);
2266
+ if (this.options.useExtendedSearch && !false) {
2267
+ throw new Error(EXTENDED_SEARCH_UNAVAILABLE);
2268
+ }
2269
+ this._keyStore = new KeyStore(this.options.keys);
2270
+ this.setCollection(docs, index);
2271
+ }
2272
+ setCollection(docs, index) {
2273
+ this._docs = docs;
2274
+ if (index && !(index instanceof FuseIndex)) {
2275
+ throw new Error(INCORRECT_INDEX_TYPE);
2276
+ }
2277
+ this._myIndex = index || createIndex(this.options.keys, this._docs, {
2278
+ getFn: this.options.getFn,
2279
+ fieldNormWeight: this.options.fieldNormWeight
2280
+ });
2281
+ }
2282
+ add(doc) {
2283
+ if (!isDefined(doc)) {
2284
+ return;
2285
+ }
2286
+ this._docs.push(doc);
2287
+ this._myIndex.add(doc);
2288
+ }
2289
+ remove(predicate = ( /* doc, idx */) => false) {
2290
+ const results = [];
2291
+ for (let i = 0, len = this._docs.length; i < len; i += 1) {
2292
+ const doc = this._docs[i];
2293
+ if (predicate(doc, i)) {
2294
+ this.removeAt(i);
2295
+ i -= 1;
2296
+ len -= 1;
2297
+ results.push(doc);
2298
+ }
2299
+ }
2300
+ return results;
2301
+ }
2302
+ removeAt(idx) {
2303
+ this._docs.splice(idx, 1);
2304
+ this._myIndex.removeAt(idx);
2305
+ }
2306
+ getIndex() {
2307
+ return this._myIndex;
2308
+ }
2309
+ search(query, {
2310
+ limit = -1
2311
+ } = {}) {
2312
+ const {
2313
+ includeMatches,
2314
+ includeScore,
2315
+ shouldSort,
2316
+ sortFn,
2317
+ ignoreFieldNorm
2318
+ } = this.options;
2319
+ let results = isString(query) ? isString(this._docs[0]) ? this._searchStringList(query) : this._searchObjectList(query) : this._searchLogical(query);
2320
+ computeScore(results, {
2321
+ ignoreFieldNorm
2322
+ });
2323
+ if (shouldSort) {
2324
+ results.sort(sortFn);
2325
+ }
2326
+ if (isNumber(limit) && limit > -1) {
2327
+ results = results.slice(0, limit);
2328
+ }
2329
+ return format(results, this._docs, {
2330
+ includeMatches,
2331
+ includeScore
2332
+ });
2333
+ }
2334
+ _searchStringList(query) {
2335
+ const searcher = createSearcher(query, this.options);
2336
+ const {
2337
+ records
2338
+ } = this._myIndex;
2339
+ const results = [];
2340
+
2341
+ // Iterate over every string in the index
2342
+ records.forEach(({
2343
+ v: text,
2344
+ i: idx,
2345
+ n: norm
2346
+ }) => {
2347
+ if (!isDefined(text)) {
2348
+ return;
2349
+ }
2350
+ const {
2351
+ isMatch,
2352
+ score,
2353
+ indices
2354
+ } = searcher.searchIn(text);
2355
+ if (isMatch) {
2356
+ results.push({
2357
+ item: text,
2358
+ idx,
2359
+ matches: [{
2360
+ score,
2361
+ value: text,
2362
+ norm,
2363
+ indices
2364
+ }]
2365
+ });
2366
+ }
2367
+ });
2368
+ return results;
2369
+ }
2370
+ _searchLogical(query) {
2371
+ {
2372
+ throw new Error(LOGICAL_SEARCH_UNAVAILABLE);
2373
+ }
2374
+ }
2375
+ _searchObjectList(query) {
2376
+ const searcher = createSearcher(query, this.options);
2377
+ const {
2378
+ keys,
2379
+ records
2380
+ } = this._myIndex;
2381
+ const results = [];
2382
+
2383
+ // List is Array<Object>
2384
+ records.forEach(({
2385
+ $: item,
2386
+ i: idx
2387
+ }) => {
2388
+ if (!isDefined(item)) {
2389
+ return;
2390
+ }
2391
+ let matches = [];
2392
+
2393
+ // Iterate over every key (i.e, path), and fetch the value at that key
2394
+ keys.forEach((key, keyIndex) => {
2395
+ matches.push(...this._findMatches({
2396
+ key,
2397
+ value: item[keyIndex],
2398
+ searcher
2399
+ }));
2400
+ });
2401
+ if (matches.length) {
2402
+ results.push({
2403
+ idx,
2404
+ item,
2405
+ matches
2406
+ });
2407
+ }
2408
+ });
2409
+ return results;
2410
+ }
2411
+ _findMatches({
2412
+ key,
2413
+ value,
2414
+ searcher
2415
+ }) {
2416
+ if (!isDefined(value)) {
2417
+ return [];
2418
+ }
2419
+ let matches = [];
2420
+ if (isArray(value)) {
2421
+ value.forEach(({
2422
+ v: text,
2423
+ i: idx,
2424
+ n: norm
2425
+ }) => {
2426
+ if (!isDefined(text)) {
2427
+ return;
2428
+ }
2429
+ const {
2430
+ isMatch,
2431
+ score,
2432
+ indices
2433
+ } = searcher.searchIn(text);
2434
+ if (isMatch) {
2435
+ matches.push({
2436
+ score,
2437
+ key,
2438
+ value: text,
2439
+ idx,
2440
+ norm,
2441
+ indices
2442
+ });
2443
+ }
2444
+ });
2445
+ } else {
2446
+ const {
2447
+ v: text,
2448
+ n: norm
2449
+ } = value;
2450
+ const {
2451
+ isMatch,
2452
+ score,
2453
+ indices
2454
+ } = searcher.searchIn(text);
2455
+ if (isMatch) {
2456
+ matches.push({
2457
+ score,
2458
+ key,
2459
+ value: text,
2460
+ norm,
2461
+ indices
2462
+ });
2463
+ }
2464
+ }
2465
+ return matches;
2466
+ }
2467
+ }
2468
+ Fuse.version = '7.0.0';
2469
+ Fuse.createIndex = createIndex;
2470
+ Fuse.parseIndex = parseIndex;
2471
+ Fuse.config = Config;
2472
+ {
2473
+ Fuse.parseQuery = parse;
2474
+ }
2475
+
2476
+ var SearchByFuse = /** @class */ (function () {
2477
+ function SearchByFuse(config) {
2478
+ this._haystack = [];
2479
+ this._fuseOptions = __assign(__assign({}, config.fuseOptions), { keys: __spreadArray([], config.searchFields, true), includeMatches: true });
2480
+ }
2481
+ SearchByFuse.prototype.index = function (data) {
2482
+ this._haystack = data;
2483
+ if (this._fuse) {
2484
+ this._fuse.setCollection(data);
2485
+ }
2486
+ };
2487
+ SearchByFuse.prototype.reset = function () {
2488
+ this._haystack = [];
2489
+ this._fuse = undefined;
2490
+ };
2491
+ SearchByFuse.prototype.isEmptyIndex = function () {
2492
+ return !this._haystack.length;
2493
+ };
2494
+ SearchByFuse.prototype.search = function (needle) {
2495
+ if (!this._fuse) {
2496
+ {
2497
+ this._fuse = new Fuse(this._haystack, this._fuseOptions);
2498
+ }
2499
+ }
2500
+ var results = this._fuse.search(needle);
2501
+ return results.map(function (value, i) {
2502
+ return {
2503
+ item: value.item,
2504
+ score: value.score || 0,
2505
+ rank: i + 1, // If value.score is used for sorting, this can create non-stable sorts!
2506
+ };
2507
+ });
2508
+ };
2509
+ return SearchByFuse;
2510
+ }());
2511
+
2512
+ function getSearcher(config) {
2513
+ {
2514
+ return new SearchByFuse(config);
2515
+ }
2516
+ }
2517
+
2518
+ /**
2519
+ * Helpers to create HTML elements used by Choices
2520
+ * Can be overridden by providing `callbackOnCreateTemplates` option.
2521
+ * `Choices.defaults.templates` allows access to the default template methods from `callbackOnCreateTemplates`
2522
+ */
2523
+ var isEmptyObject = function (obj) {
2524
+ // eslint-disable-next-line no-restricted-syntax
2525
+ for (var prop in obj) {
2526
+ if (Object.prototype.hasOwnProperty.call(obj, prop)) {
2527
+ return false;
2528
+ }
2529
+ }
2530
+ return true;
2531
+ };
2532
+ var assignCustomProperties = function (el, choice, withCustomProperties) {
2533
+ var dataset = el.dataset;
2534
+ var customProperties = choice.customProperties, labelClass = choice.labelClass, labelDescription = choice.labelDescription;
2535
+ if (labelClass) {
2536
+ dataset.labelClass = getClassNames(labelClass).join(' ');
2537
+ }
2538
+ if (labelDescription) {
2539
+ dataset.labelDescription = labelDescription;
2540
+ }
2541
+ if (withCustomProperties && customProperties) {
2542
+ if (typeof customProperties === 'string') {
2543
+ dataset.customProperties = customProperties;
2544
+ }
2545
+ else if (typeof customProperties === 'object' && !isEmptyObject(customProperties)) {
2546
+ dataset.customProperties = JSON.stringify(customProperties);
2547
+ }
2548
+ }
2549
+ };
2550
+ var addAriaLabel = function (docRoot, id, element) {
2551
+ var label = id && docRoot.querySelector("label[for='".concat(id, "']"));
2552
+ var text = label && label.innerText;
2553
+ if (text) {
2554
+ element.setAttribute('aria-label', text);
2555
+ }
2556
+ };
2557
+ var templates = {
2558
+ containerOuter: function (_a, dir, isSelectElement, isSelectOneElement, searchEnabled, passedElementType, labelId) {
2559
+ var containerOuter = _a.classNames.containerOuter;
2560
+ var div = document.createElement('div');
2561
+ addClassesToElement(div, containerOuter);
2562
+ div.dataset.type = passedElementType;
2563
+ if (dir) {
2564
+ div.dir = dir;
2565
+ }
2566
+ if (isSelectOneElement) {
2567
+ div.tabIndex = 0;
2568
+ }
2569
+ if (isSelectElement) {
2570
+ div.setAttribute('role', searchEnabled ? 'combobox' : 'listbox');
2571
+ if (searchEnabled) {
2572
+ div.setAttribute('aria-autocomplete', 'list');
2573
+ }
2574
+ else if (!labelId) {
2575
+ addAriaLabel(this._docRoot, this.passedElement.element.id, div);
2576
+ }
2577
+ div.setAttribute('aria-haspopup', 'true');
2578
+ div.setAttribute('aria-expanded', 'false');
2579
+ }
2580
+ if (labelId) {
2581
+ div.setAttribute('aria-labelledby', labelId);
2582
+ }
2583
+ return div;
2584
+ },
2585
+ containerInner: function (_a) {
2586
+ var containerInner = _a.classNames.containerInner;
2587
+ var div = document.createElement('div');
2588
+ addClassesToElement(div, containerInner);
2589
+ return div;
2590
+ },
2591
+ itemList: function (_a, isSelectOneElement) {
2592
+ var searchEnabled = _a.searchEnabled, _b = _a.classNames, list = _b.list, listSingle = _b.listSingle, listItems = _b.listItems;
2593
+ var div = document.createElement('div');
2594
+ addClassesToElement(div, list);
2595
+ addClassesToElement(div, isSelectOneElement ? listSingle : listItems);
2596
+ if (this._isSelectElement && searchEnabled) {
2597
+ div.setAttribute('role', 'listbox');
2598
+ }
2599
+ return div;
2600
+ },
2601
+ placeholder: function (_a, value) {
2602
+ var allowHTML = _a.allowHTML, placeholder = _a.classNames.placeholder;
2603
+ var div = document.createElement('div');
2604
+ addClassesToElement(div, placeholder);
2605
+ setElementHtml(div, allowHTML, value);
2606
+ return div;
2607
+ },
2608
+ item: function (_a, choice, removeItemButton) {
2609
+ var allowHTML = _a.allowHTML, removeItemButtonAlignLeft = _a.removeItemButtonAlignLeft, removeItemIconText = _a.removeItemIconText, removeItemLabelText = _a.removeItemLabelText, _b = _a.classNames, item = _b.item, button = _b.button, highlightedState = _b.highlightedState, itemSelectable = _b.itemSelectable, placeholder = _b.placeholder;
2610
+ var rawValue = unwrapStringForRaw(choice.value);
2611
+ var div = document.createElement('div');
2612
+ addClassesToElement(div, item);
2613
+ if (choice.labelClass) {
2614
+ var spanLabel = document.createElement('span');
2615
+ setElementHtml(spanLabel, allowHTML, choice.label);
2616
+ addClassesToElement(spanLabel, choice.labelClass);
2617
+ div.appendChild(spanLabel);
2618
+ }
2619
+ else {
2620
+ setElementHtml(div, allowHTML, choice.label);
2621
+ }
2622
+ div.dataset.item = '';
2623
+ div.dataset.id = choice.id;
2624
+ div.dataset.value = rawValue;
2625
+ assignCustomProperties(div, choice, true);
2626
+ if (choice.disabled || this.containerOuter.isDisabled) {
2627
+ div.setAttribute('aria-disabled', 'true');
2628
+ }
2629
+ if (this._isSelectElement) {
2630
+ div.setAttribute('aria-selected', 'true');
2631
+ div.setAttribute('role', 'option');
2632
+ }
2633
+ if (choice.placeholder) {
2634
+ addClassesToElement(div, placeholder);
2635
+ div.dataset.placeholder = '';
2636
+ }
2637
+ addClassesToElement(div, choice.highlighted ? highlightedState : itemSelectable);
2638
+ if (removeItemButton) {
2639
+ if (choice.disabled) {
2640
+ removeClassesFromElement(div, itemSelectable);
2641
+ }
2642
+ div.dataset.deletable = '';
2643
+ var removeButton = document.createElement('button');
2644
+ removeButton.type = 'button';
2645
+ addClassesToElement(removeButton, button);
2646
+ setElementHtml(removeButton, true, resolveNoticeFunction(removeItemIconText, choice.value));
2647
+ var REMOVE_ITEM_LABEL = resolveNoticeFunction(removeItemLabelText, choice.value);
2648
+ if (REMOVE_ITEM_LABEL) {
2649
+ removeButton.setAttribute('aria-label', REMOVE_ITEM_LABEL);
2650
+ }
2651
+ removeButton.dataset.button = '';
2652
+ if (removeItemButtonAlignLeft) {
2653
+ div.insertAdjacentElement('afterbegin', removeButton);
2654
+ }
2655
+ else {
2656
+ div.appendChild(removeButton);
2657
+ }
2658
+ }
2659
+ return div;
2660
+ },
2661
+ choiceList: function (_a, isSelectOneElement) {
2662
+ var list = _a.classNames.list;
2663
+ var div = document.createElement('div');
2664
+ addClassesToElement(div, list);
2665
+ if (!isSelectOneElement) {
2666
+ div.setAttribute('aria-multiselectable', 'true');
2667
+ }
2668
+ div.setAttribute('role', 'listbox');
2669
+ return div;
2670
+ },
2671
+ choiceGroup: function (_a, _b) {
2672
+ var allowHTML = _a.allowHTML, _c = _a.classNames, group = _c.group, groupHeading = _c.groupHeading, itemDisabled = _c.itemDisabled;
2673
+ var id = _b.id, label = _b.label, disabled = _b.disabled;
2674
+ var rawLabel = unwrapStringForRaw(label);
2675
+ var div = document.createElement('div');
2676
+ addClassesToElement(div, group);
2677
+ if (disabled) {
2678
+ addClassesToElement(div, itemDisabled);
2679
+ }
2680
+ div.setAttribute('role', 'group');
2681
+ div.dataset.group = '';
2682
+ div.dataset.id = id;
2683
+ div.dataset.value = rawLabel;
2684
+ if (disabled) {
2685
+ div.setAttribute('aria-disabled', 'true');
2686
+ }
2687
+ var heading = document.createElement('div');
2688
+ addClassesToElement(heading, groupHeading);
2689
+ setElementHtml(heading, allowHTML, label || '');
2690
+ div.appendChild(heading);
2691
+ return div;
2692
+ },
2693
+ choice: function (_a, choice, selectText, groupName) {
2694
+ var allowHTML = _a.allowHTML, _b = _a.classNames, item = _b.item, itemChoice = _b.itemChoice, itemSelectable = _b.itemSelectable, selectedState = _b.selectedState, itemDisabled = _b.itemDisabled, description = _b.description, placeholder = _b.placeholder;
2695
+ // eslint-disable-next-line prefer-destructuring
2696
+ var label = choice.label;
2697
+ var rawValue = unwrapStringForRaw(choice.value);
2698
+ var div = document.createElement('div');
2699
+ div.id = choice.elementId;
2700
+ addClassesToElement(div, item);
2701
+ addClassesToElement(div, itemChoice);
2702
+ if (groupName && typeof label === 'string') {
2703
+ label = escapeForTemplate(allowHTML, label);
2704
+ label += " (".concat(groupName, ")");
2705
+ label = { trusted: label };
2706
+ }
2707
+ var describedBy = div;
2708
+ if (choice.labelClass) {
2709
+ var spanLabel = document.createElement('span');
2710
+ setElementHtml(spanLabel, allowHTML, label);
2711
+ addClassesToElement(spanLabel, choice.labelClass);
2712
+ describedBy = spanLabel;
2713
+ div.appendChild(spanLabel);
2714
+ }
2715
+ else {
2716
+ setElementHtml(div, allowHTML, label);
2717
+ }
2718
+ if (choice.labelDescription) {
2719
+ var descId = "".concat(choice.elementId, "-description");
2720
+ describedBy.setAttribute('aria-describedby', descId);
2721
+ var spanDesc = document.createElement('span');
2722
+ setElementHtml(spanDesc, allowHTML, choice.labelDescription);
2723
+ spanDesc.id = descId;
2724
+ addClassesToElement(spanDesc, description);
2725
+ div.appendChild(spanDesc);
2726
+ }
2727
+ if (choice.selected) {
2728
+ addClassesToElement(div, selectedState);
2729
+ }
2730
+ if (choice.placeholder) {
2731
+ addClassesToElement(div, placeholder);
2732
+ }
2733
+ div.setAttribute('role', choice.group ? 'treeitem' : 'option');
2734
+ div.dataset.choice = '';
2735
+ div.dataset.id = choice.id;
2736
+ div.dataset.value = rawValue;
2737
+ if (selectText) {
2738
+ div.dataset.selectText = selectText;
2739
+ }
2740
+ if (choice.group) {
2741
+ div.dataset.groupId = "".concat(choice.group.id);
2742
+ }
2743
+ assignCustomProperties(div, choice, false);
2744
+ if (choice.disabled) {
2745
+ addClassesToElement(div, itemDisabled);
2746
+ div.dataset.choiceDisabled = '';
2747
+ div.setAttribute('aria-disabled', 'true');
2748
+ }
2749
+ else {
2750
+ addClassesToElement(div, itemSelectable);
2751
+ div.dataset.choiceSelectable = '';
2752
+ }
2753
+ return div;
2754
+ },
2755
+ input: function (_a, placeholderValue) {
2756
+ var _b = _a.classNames, input = _b.input, inputCloned = _b.inputCloned, labelId = _a.labelId;
2757
+ var inp = document.createElement('input');
2758
+ inp.type = 'search';
2759
+ addClassesToElement(inp, input);
2760
+ addClassesToElement(inp, inputCloned);
2761
+ inp.autocomplete = 'off';
2762
+ inp.autocapitalize = 'off';
2763
+ inp.spellcheck = false;
2764
+ inp.setAttribute('aria-autocomplete', 'list');
2765
+ if (placeholderValue) {
2766
+ inp.setAttribute('aria-label', placeholderValue);
2767
+ }
2768
+ else if (!labelId) {
2769
+ addAriaLabel(this._docRoot, this.passedElement.element.id, inp);
2770
+ }
2771
+ return inp;
2772
+ },
2773
+ dropdown: function (_a) {
2774
+ var _b = _a.classNames, list = _b.list, listDropdown = _b.listDropdown;
2775
+ var div = document.createElement('div');
2776
+ addClassesToElement(div, list);
2777
+ addClassesToElement(div, listDropdown);
2778
+ div.setAttribute('aria-expanded', 'false');
2779
+ return div;
2780
+ },
2781
+ notice: function (_a, innerHTML, type) {
2782
+ var _b = _a.classNames, item = _b.item, itemChoice = _b.itemChoice, addChoice = _b.addChoice, noResults = _b.noResults, noChoices = _b.noChoices, noticeItem = _b.notice;
2783
+ if (type === void 0) { type = NoticeTypes.generic; }
2784
+ var notice = document.createElement('div');
2785
+ setElementHtml(notice, true, innerHTML);
2786
+ addClassesToElement(notice, item);
2787
+ addClassesToElement(notice, itemChoice);
2788
+ addClassesToElement(notice, noticeItem);
2789
+ // eslint-disable-next-line default-case
2790
+ switch (type) {
2791
+ case NoticeTypes.addChoice:
2792
+ addClassesToElement(notice, addChoice);
2793
+ break;
2794
+ case NoticeTypes.noResults:
2795
+ addClassesToElement(notice, noResults);
2796
+ break;
2797
+ case NoticeTypes.noChoices:
2798
+ addClassesToElement(notice, noChoices);
2799
+ break;
2800
+ }
2801
+ if (type === NoticeTypes.addChoice) {
2802
+ notice.dataset.choiceSelectable = '';
2803
+ notice.dataset.choice = '';
2804
+ }
2805
+ return notice;
2806
+ },
2807
+ option: function (choice) {
2808
+ // HtmlOptionElement's label value does not support HTML, so the avoid double escaping unwrap the untrusted string.
2809
+ var labelValue = unwrapStringForRaw(choice.label);
2810
+ var opt = new Option(labelValue, choice.value, false, choice.selected);
2811
+ assignCustomProperties(opt, choice, true);
2812
+ opt.disabled = choice.disabled;
2813
+ if (choice.selected) {
2814
+ opt.setAttribute('selected', '');
2815
+ }
2816
+ return opt;
2817
+ },
2818
+ };
2819
+
2820
+ /** @see {@link http://browserhacks.com/#hack-acea075d0ac6954f275a70023906050c} */
2821
+ var IS_IE11 = '-ms-scroll-limit' in document.documentElement.style &&
2822
+ '-ms-ime-align' in document.documentElement.style;
2823
+ var USER_DEFAULTS = {};
2824
+ var parseDataSetId = function (element) {
2825
+ if (!element) {
2826
+ return undefined;
2827
+ }
2828
+ return element.dataset.id ? parseInt(element.dataset.id, 10) : undefined;
2829
+ };
2830
+ var selectableChoiceIdentifier = '[data-choice-selectable]';
2831
+ /**
2832
+ * Choices
2833
+ * @author Josh Johnson<josh@joshuajohnson.co.uk>
2834
+ */
2835
+ var Choices = /** @class */ (function () {
2836
+ function Choices(element, userConfig) {
2837
+ if (element === void 0) { element = '[data-choice]'; }
2838
+ if (userConfig === void 0) { userConfig = {}; }
2839
+ var _this = this;
2840
+ this.initialisedOK = undefined;
2841
+ this._hasNonChoicePlaceholder = false;
2842
+ this._lastAddedChoiceId = 0;
2843
+ this._lastAddedGroupId = 0;
2844
+ var defaults = Choices.defaults;
2845
+ this.config = __assign(__assign(__assign({}, defaults.allOptions), defaults.options), userConfig);
2846
+ ObjectsInConfig.forEach(function (key) {
2847
+ _this.config[key] = __assign(__assign(__assign({}, defaults.allOptions[key]), defaults.options[key]), userConfig[key]);
2848
+ });
2849
+ var config = this.config;
2850
+ if (!config.silent) {
2851
+ this._validateConfig();
2852
+ }
2853
+ var docRoot = config.shadowRoot || document.documentElement;
2854
+ this._docRoot = docRoot;
2855
+ var passedElement = typeof element === 'string' ? docRoot.querySelector(element) : element;
2856
+ if (!passedElement ||
2857
+ typeof passedElement !== 'object' ||
2858
+ !(isHtmlInputElement(passedElement) || isHtmlSelectElement(passedElement))) {
2859
+ if (!passedElement && typeof element === 'string') {
2860
+ throw TypeError("Selector ".concat(element, " failed to find an element"));
2861
+ }
2862
+ throw TypeError("Expected one of the following types text|select-one|select-multiple");
2863
+ }
2864
+ var elementType = passedElement.type;
2865
+ var isText = elementType === PassedElementTypes.Text;
2866
+ if (isText || config.maxItemCount !== 1) {
2867
+ config.singleModeForMultiSelect = false;
2868
+ }
2869
+ if (config.singleModeForMultiSelect) {
2870
+ elementType = PassedElementTypes.SelectMultiple;
2871
+ }
2872
+ var isSelectOne = elementType === PassedElementTypes.SelectOne;
2873
+ var isSelectMultiple = elementType === PassedElementTypes.SelectMultiple;
2874
+ var isSelect = isSelectOne || isSelectMultiple;
2875
+ this._elementType = elementType;
2876
+ this._isTextElement = isText;
2877
+ this._isSelectOneElement = isSelectOne;
2878
+ this._isSelectMultipleElement = isSelectMultiple;
2879
+ this._isSelectElement = isSelectOne || isSelectMultiple;
2880
+ this._canAddUserChoices = (isText && config.addItems) || (isSelect && config.addChoices);
2881
+ if (typeof config.renderSelectedChoices !== 'boolean') {
2882
+ config.renderSelectedChoices = config.renderSelectedChoices === 'always' || isSelectOne;
2883
+ }
2884
+ if (config.closeDropdownOnSelect === 'auto') {
2885
+ config.closeDropdownOnSelect = isText || isSelectOne || config.singleModeForMultiSelect;
2886
+ }
2887
+ else {
2888
+ config.closeDropdownOnSelect = coerceBool(config.closeDropdownOnSelect);
2889
+ }
2890
+ if (config.placeholder) {
2891
+ if (config.placeholderValue) {
2892
+ this._hasNonChoicePlaceholder = true;
2893
+ }
2894
+ else if (passedElement.dataset.placeholder) {
2895
+ this._hasNonChoicePlaceholder = true;
2896
+ config.placeholderValue = passedElement.dataset.placeholder;
2897
+ }
2898
+ }
2899
+ if (userConfig.addItemFilter && typeof userConfig.addItemFilter !== 'function') {
2900
+ var re = userConfig.addItemFilter instanceof RegExp ? userConfig.addItemFilter : new RegExp(userConfig.addItemFilter);
2901
+ config.addItemFilter = re.test.bind(re);
2902
+ }
2903
+ if (this._isTextElement) {
2904
+ this.passedElement = new WrappedInput({
2905
+ element: passedElement,
2906
+ classNames: config.classNames,
2907
+ });
2908
+ }
2909
+ else {
2910
+ var selectEl = passedElement;
2911
+ this.passedElement = new WrappedSelect({
2912
+ element: selectEl,
2913
+ classNames: config.classNames,
2914
+ template: function (data) { return _this._templates.option(data); },
2915
+ extractPlaceholder: config.placeholder && !this._hasNonChoicePlaceholder,
2916
+ });
2917
+ }
2918
+ this.initialised = false;
2919
+ this._store = new Store(config);
2920
+ this._currentValue = '';
2921
+ config.searchEnabled = (!isText && config.searchEnabled) || isSelectMultiple;
2922
+ this._canSearch = config.searchEnabled;
2923
+ this._isScrollingOnIe = false;
2924
+ this._highlightPosition = 0;
2925
+ this._wasTap = true;
2926
+ this._placeholderValue = this._generatePlaceholderValue();
2927
+ this._baseId = generateId(passedElement, 'choices-');
2928
+ /**
2929
+ * setting direction in cases where it's explicitly set on passedElement
2930
+ * or when calculated direction is different from the document
2931
+ */
2932
+ this._direction = passedElement.dir;
2933
+ if (!this._direction) {
2934
+ var elementDirection = window.getComputedStyle(passedElement).direction;
2935
+ var documentDirection = window.getComputedStyle(document.documentElement).direction;
2936
+ if (elementDirection !== documentDirection) {
2937
+ this._direction = elementDirection;
2938
+ }
2939
+ }
2940
+ this._idNames = {
2941
+ itemChoice: 'item-choice',
2942
+ };
2943
+ this._templates = defaults.templates;
2944
+ this._render = this._render.bind(this);
2945
+ this._onFocus = this._onFocus.bind(this);
2946
+ this._onBlur = this._onBlur.bind(this);
2947
+ this._onKeyUp = this._onKeyUp.bind(this);
2948
+ this._onKeyDown = this._onKeyDown.bind(this);
2949
+ this._onInput = this._onInput.bind(this);
2950
+ this._onClick = this._onClick.bind(this);
2951
+ this._onTouchMove = this._onTouchMove.bind(this);
2952
+ this._onTouchEnd = this._onTouchEnd.bind(this);
2953
+ this._onMouseDown = this._onMouseDown.bind(this);
2954
+ this._onMouseOver = this._onMouseOver.bind(this);
2955
+ this._onFormReset = this._onFormReset.bind(this);
2956
+ this._onSelectKey = this._onSelectKey.bind(this);
2957
+ this._onEnterKey = this._onEnterKey.bind(this);
2958
+ this._onEscapeKey = this._onEscapeKey.bind(this);
2959
+ this._onDirectionKey = this._onDirectionKey.bind(this);
2960
+ this._onDeleteKey = this._onDeleteKey.bind(this);
2961
+ // If element has already been initialised with Choices, fail silently
2962
+ if (this.passedElement.isActive) {
2963
+ if (!config.silent) {
2964
+ console.warn('Trying to initialise Choices on element already initialised', { element: element });
2965
+ }
2966
+ this.initialised = true;
2967
+ this.initialisedOK = false;
2968
+ return;
2969
+ }
2970
+ // Let's go
2971
+ this.init();
2972
+ // preserve the selected item list after setup for form reset
2973
+ this._initialItems = this._store.items.map(function (choice) { return choice.value; });
2974
+ }
2975
+ Object.defineProperty(Choices, "defaults", {
2976
+ get: function () {
2977
+ return Object.preventExtensions({
2978
+ get options() {
2979
+ return USER_DEFAULTS;
2980
+ },
2981
+ get allOptions() {
2982
+ return DEFAULT_CONFIG;
2983
+ },
2984
+ get templates() {
2985
+ return templates;
2986
+ },
2987
+ });
2988
+ },
2989
+ enumerable: false,
2990
+ configurable: true
2991
+ });
2992
+ Choices.prototype.init = function () {
2993
+ if (this.initialised || this.initialisedOK !== undefined) {
2994
+ return;
2995
+ }
2996
+ this._searcher = getSearcher(this.config);
2997
+ this._loadChoices();
2998
+ this._createTemplates();
2999
+ this._createElements();
3000
+ this._createStructure();
3001
+ if ((this._isTextElement && !this.config.addItems) ||
3002
+ this.passedElement.element.hasAttribute('disabled') ||
3003
+ !!this.passedElement.element.closest('fieldset:disabled')) {
3004
+ this.disable();
3005
+ }
3006
+ else {
3007
+ this.enable();
3008
+ this._addEventListeners();
3009
+ }
3010
+ // should be triggered **after** disabled state to avoid additional re-draws
3011
+ this._initStore();
3012
+ this.initialised = true;
3013
+ this.initialisedOK = true;
3014
+ var callbackOnInit = this.config.callbackOnInit;
3015
+ // Run callback if it is a function
3016
+ if (typeof callbackOnInit === 'function') {
3017
+ callbackOnInit.call(this);
3018
+ }
3019
+ };
3020
+ Choices.prototype.destroy = function () {
3021
+ if (!this.initialised) {
3022
+ return;
3023
+ }
3024
+ this._removeEventListeners();
3025
+ this.passedElement.reveal();
3026
+ this.containerOuter.unwrap(this.passedElement.element);
3027
+ this._store._listeners = []; // prevents select/input value being wiped
3028
+ this.clearStore(false);
3029
+ this._stopSearch();
3030
+ this._templates = Choices.defaults.templates;
3031
+ this.initialised = false;
3032
+ this.initialisedOK = undefined;
3033
+ };
3034
+ Choices.prototype.enable = function () {
3035
+ if (this.passedElement.isDisabled) {
3036
+ this.passedElement.enable();
3037
+ }
3038
+ if (this.containerOuter.isDisabled) {
3039
+ this._addEventListeners();
3040
+ this.input.enable();
3041
+ this.containerOuter.enable();
3042
+ }
3043
+ return this;
3044
+ };
3045
+ Choices.prototype.disable = function () {
3046
+ if (!this.passedElement.isDisabled) {
3047
+ this.passedElement.disable();
3048
+ }
3049
+ if (!this.containerOuter.isDisabled) {
3050
+ this._removeEventListeners();
3051
+ this.input.disable();
3052
+ this.containerOuter.disable();
3053
+ }
3054
+ return this;
3055
+ };
3056
+ Choices.prototype.highlightItem = function (item, runEvent) {
3057
+ if (runEvent === void 0) { runEvent = true; }
3058
+ if (!item || !item.id) {
3059
+ return this;
3060
+ }
3061
+ var choice = this._store.items.find(function (c) { return c.id === item.id; });
3062
+ if (!choice || choice.highlighted) {
3063
+ return this;
3064
+ }
3065
+ this._store.dispatch(highlightItem(choice, true));
3066
+ if (runEvent) {
3067
+ this.passedElement.triggerEvent(EventType.highlightItem, this._getChoiceForOutput(choice));
3068
+ }
3069
+ return this;
3070
+ };
3071
+ Choices.prototype.unhighlightItem = function (item, runEvent) {
3072
+ if (runEvent === void 0) { runEvent = true; }
3073
+ if (!item || !item.id) {
3074
+ return this;
3075
+ }
3076
+ var choice = this._store.items.find(function (c) { return c.id === item.id; });
3077
+ if (!choice || !choice.highlighted) {
3078
+ return this;
3079
+ }
3080
+ this._store.dispatch(highlightItem(choice, false));
3081
+ if (runEvent) {
3082
+ this.passedElement.triggerEvent(EventType.unhighlightItem, this._getChoiceForOutput(choice));
3083
+ }
3084
+ return this;
3085
+ };
3086
+ Choices.prototype.highlightAll = function () {
3087
+ var _this = this;
3088
+ this._store.withTxn(function () {
3089
+ _this._store.items.forEach(function (item) {
3090
+ if (!item.highlighted) {
3091
+ _this._store.dispatch(highlightItem(item, true));
3092
+ _this.passedElement.triggerEvent(EventType.highlightItem, _this._getChoiceForOutput(item));
3093
+ }
3094
+ });
3095
+ });
3096
+ return this;
3097
+ };
3098
+ Choices.prototype.unhighlightAll = function () {
3099
+ var _this = this;
3100
+ this._store.withTxn(function () {
3101
+ _this._store.items.forEach(function (item) {
3102
+ if (item.highlighted) {
3103
+ _this._store.dispatch(highlightItem(item, false));
3104
+ _this.passedElement.triggerEvent(EventType.highlightItem, _this._getChoiceForOutput(item));
3105
+ }
3106
+ });
3107
+ });
3108
+ return this;
3109
+ };
3110
+ Choices.prototype.removeActiveItemsByValue = function (value) {
3111
+ var _this = this;
3112
+ this._store.withTxn(function () {
3113
+ _this._store.items.filter(function (item) { return item.value === value; }).forEach(function (item) { return _this._removeItem(item); });
3114
+ });
3115
+ return this;
3116
+ };
3117
+ Choices.prototype.removeActiveItems = function (excludedId) {
3118
+ var _this = this;
3119
+ this._store.withTxn(function () {
3120
+ _this._store.items.filter(function (_a) {
3121
+ var id = _a.id;
3122
+ return id !== excludedId;
3123
+ }).forEach(function (item) { return _this._removeItem(item); });
3124
+ });
3125
+ return this;
3126
+ };
3127
+ Choices.prototype.removeHighlightedItems = function (runEvent) {
3128
+ var _this = this;
3129
+ if (runEvent === void 0) { runEvent = false; }
3130
+ this._store.withTxn(function () {
3131
+ _this._store.highlightedActiveItems.forEach(function (item) {
3132
+ _this._removeItem(item);
3133
+ // If this action was performed by the user
3134
+ // trigger the event
3135
+ if (runEvent) {
3136
+ _this._triggerChange(item.value);
3137
+ }
3138
+ });
3139
+ });
3140
+ return this;
3141
+ };
3142
+ Choices.prototype.showDropdown = function (preventInputFocus) {
3143
+ var _this = this;
3144
+ if (this.dropdown.isActive) {
3145
+ return this;
3146
+ }
3147
+ if (preventInputFocus === undefined) {
3148
+ // eslint-disable-next-line no-param-reassign
3149
+ preventInputFocus = !this._canSearch;
3150
+ }
3151
+ requestAnimationFrame(function () {
3152
+ _this.dropdown.show();
3153
+ var rect = _this.dropdown.element.getBoundingClientRect();
3154
+ _this.containerOuter.open(rect.bottom, rect.height);
3155
+ if (!preventInputFocus) {
3156
+ _this.input.focus();
3157
+ }
3158
+ _this.passedElement.triggerEvent(EventType.showDropdown);
3159
+ });
3160
+ return this;
3161
+ };
3162
+ Choices.prototype.hideDropdown = function (preventInputBlur) {
3163
+ var _this = this;
3164
+ if (!this.dropdown.isActive) {
3165
+ return this;
3166
+ }
3167
+ requestAnimationFrame(function () {
3168
+ _this.dropdown.hide();
3169
+ _this.containerOuter.close();
3170
+ if (!preventInputBlur && _this._canSearch) {
3171
+ _this.input.removeActiveDescendant();
3172
+ _this.input.blur();
3173
+ }
3174
+ _this.passedElement.triggerEvent(EventType.hideDropdown);
3175
+ });
3176
+ return this;
3177
+ };
3178
+ Choices.prototype.getValue = function (valueOnly) {
3179
+ var _this = this;
3180
+ var values = this._store.items.map(function (item) {
3181
+ return (valueOnly ? item.value : _this._getChoiceForOutput(item));
3182
+ });
3183
+ return this._isSelectOneElement || this.config.singleModeForMultiSelect ? values[0] : values;
3184
+ };
3185
+ Choices.prototype.setValue = function (items) {
3186
+ var _this = this;
3187
+ if (!this.initialisedOK) {
3188
+ this._warnChoicesInitFailed('setValue');
3189
+ return this;
3190
+ }
3191
+ this._store.withTxn(function () {
3192
+ items.forEach(function (value) {
3193
+ if (value) {
3194
+ _this._addChoice(mapInputToChoice(value, false));
3195
+ }
3196
+ });
3197
+ });
3198
+ // @todo integrate with Store
3199
+ this._searcher.reset();
3200
+ return this;
3201
+ };
3202
+ Choices.prototype.setChoiceByValue = function (value) {
3203
+ var _this = this;
3204
+ if (!this.initialisedOK) {
3205
+ this._warnChoicesInitFailed('setChoiceByValue');
3206
+ return this;
3207
+ }
3208
+ if (this._isTextElement) {
3209
+ return this;
3210
+ }
3211
+ this._store.withTxn(function () {
3212
+ // If only one value has been passed, convert to array
3213
+ var choiceValue = Array.isArray(value) ? value : [value];
3214
+ // Loop through each value and
3215
+ choiceValue.forEach(function (val) { return _this._findAndSelectChoiceByValue(val); });
3216
+ _this.unhighlightAll();
3217
+ });
3218
+ // @todo integrate with Store
3219
+ this._searcher.reset();
3220
+ return this;
3221
+ };
3222
+ /**
3223
+ * Set choices of select input via an array of objects (or function that returns array of object or promise of it),
3224
+ * a value field name and a label field name.
3225
+ * This behaves the same as passing items via the choices option but can be called after initialising Choices.
3226
+ * This can also be used to add groups of choices (see example 2); Optionally pass a true `replaceChoices` value to remove any existing choices.
3227
+ * Optionally pass a `customProperties` object to add additional data to your choices (useful when searching/filtering etc).
3228
+ *
3229
+ * **Input types affected:** select-one, select-multiple
3230
+ *
3231
+ * @example
3232
+ * ```js
3233
+ * const example = new Choices(element);
3234
+ *
3235
+ * example.setChoices([
3236
+ * {value: 'One', label: 'Label One', disabled: true},
3237
+ * {value: 'Two', label: 'Label Two', selected: true},
3238
+ * {value: 'Three', label: 'Label Three'},
3239
+ * ], 'value', 'label', false);
3240
+ * ```
3241
+ *
3242
+ * @example
3243
+ * ```js
3244
+ * const example = new Choices(element);
3245
+ *
3246
+ * example.setChoices(async () => {
3247
+ * try {
3248
+ * const items = await fetch('/items');
3249
+ * return items.json()
3250
+ * } catch(err) {
3251
+ * console.error(err)
3252
+ * }
3253
+ * });
3254
+ * ```
3255
+ *
3256
+ * @example
3257
+ * ```js
3258
+ * const example = new Choices(element);
3259
+ *
3260
+ * example.setChoices([{
3261
+ * label: 'Group one',
3262
+ * id: 1,
3263
+ * disabled: false,
3264
+ * choices: [
3265
+ * {value: 'Child One', label: 'Child One', selected: true},
3266
+ * {value: 'Child Two', label: 'Child Two', disabled: true},
3267
+ * {value: 'Child Three', label: 'Child Three'},
3268
+ * ]
3269
+ * },
3270
+ * {
3271
+ * label: 'Group two',
3272
+ * id: 2,
3273
+ * disabled: false,
3274
+ * choices: [
3275
+ * {value: 'Child Four', label: 'Child Four', disabled: true},
3276
+ * {value: 'Child Five', label: 'Child Five'},
3277
+ * {value: 'Child Six', label: 'Child Six', customProperties: {
3278
+ * description: 'Custom description about child six',
3279
+ * random: 'Another random custom property'
3280
+ * }},
3281
+ * ]
3282
+ * }], 'value', 'label', false);
3283
+ * ```
3284
+ */
3285
+ Choices.prototype.setChoices = function (choicesArrayOrFetcher, value, label, replaceChoices, clearSearchFlag, replaceItems) {
3286
+ var _this = this;
3287
+ if (choicesArrayOrFetcher === void 0) { choicesArrayOrFetcher = []; }
3288
+ if (value === void 0) { value = 'value'; }
3289
+ if (label === void 0) { label = 'label'; }
3290
+ if (replaceChoices === void 0) { replaceChoices = false; }
3291
+ if (clearSearchFlag === void 0) { clearSearchFlag = true; }
3292
+ if (replaceItems === void 0) { replaceItems = false; }
3293
+ if (!this.initialisedOK) {
3294
+ this._warnChoicesInitFailed('setChoices');
3295
+ return this;
3296
+ }
3297
+ if (!this._isSelectElement) {
3298
+ throw new TypeError("setChoices can't be used with INPUT based Choices");
3299
+ }
3300
+ if (typeof value !== 'string' || !value) {
3301
+ throw new TypeError("value parameter must be a name of 'value' field in passed objects");
3302
+ }
3303
+ if (typeof choicesArrayOrFetcher === 'function') {
3304
+ // it's a choices fetcher function
3305
+ var fetcher_1 = choicesArrayOrFetcher(this);
3306
+ if (typeof Promise === 'function' && fetcher_1 instanceof Promise) {
3307
+ // that's a promise
3308
+ // eslint-disable-next-line no-promise-executor-return
3309
+ return new Promise(function (resolve) { return requestAnimationFrame(resolve); })
3310
+ .then(function () { return _this._handleLoadingState(true); })
3311
+ .then(function () { return fetcher_1; })
3312
+ .then(function (data) {
3313
+ return _this.setChoices(data, value, label, replaceChoices, clearSearchFlag, replaceItems);
3314
+ })
3315
+ .catch(function (err) {
3316
+ if (!_this.config.silent) {
3317
+ console.error(err);
3318
+ }
3319
+ })
3320
+ .then(function () { return _this._handleLoadingState(false); })
3321
+ .then(function () { return _this; });
3322
+ }
3323
+ // function returned something else than promise, let's check if it's an array of choices
3324
+ if (!Array.isArray(fetcher_1)) {
3325
+ throw new TypeError(".setChoices first argument function must return either array of choices or Promise, got: ".concat(typeof fetcher_1));
3326
+ }
3327
+ // recursion with results, it's sync and choices were cleared already
3328
+ return this.setChoices(fetcher_1, value, label, false);
3329
+ }
3330
+ if (!Array.isArray(choicesArrayOrFetcher)) {
3331
+ throw new TypeError(".setChoices must be called either with array of choices with a function resulting into Promise of array of choices");
3332
+ }
3333
+ this.containerOuter.removeLoadingState();
3334
+ this._store.withTxn(function () {
3335
+ if (clearSearchFlag) {
3336
+ _this._isSearching = false;
3337
+ }
3338
+ // Clear choices if needed
3339
+ if (replaceChoices) {
3340
+ _this.clearChoices(true, replaceItems);
3341
+ }
3342
+ var isDefaultValue = value === 'value';
3343
+ var isDefaultLabel = label === 'label';
3344
+ choicesArrayOrFetcher.forEach(function (groupOrChoice) {
3345
+ if ('choices' in groupOrChoice) {
3346
+ var group = groupOrChoice;
3347
+ if (!isDefaultLabel) {
3348
+ group = __assign(__assign({}, group), { label: group[label] });
3349
+ }
3350
+ _this._addGroup(mapInputToChoice(group, true));
3351
+ }
3352
+ else {
3353
+ var choice = groupOrChoice;
3354
+ if (!isDefaultLabel || !isDefaultValue) {
3355
+ choice = __assign(__assign({}, choice), { value: choice[value], label: choice[label] });
3356
+ }
3357
+ var choiceFull = mapInputToChoice(choice, false);
3358
+ _this._addChoice(choiceFull);
3359
+ if (choiceFull.placeholder && !_this._hasNonChoicePlaceholder) {
3360
+ _this._placeholderValue = unwrapStringForEscaped(choiceFull.label);
3361
+ }
3362
+ }
3363
+ });
3364
+ _this.unhighlightAll();
3365
+ });
3366
+ // @todo integrate with Store
3367
+ this._searcher.reset();
3368
+ return this;
3369
+ };
3370
+ Choices.prototype.refresh = function (withEvents, selectFirstOption, deselectAll) {
3371
+ var _this = this;
3372
+ if (withEvents === void 0) { withEvents = false; }
3373
+ if (selectFirstOption === void 0) { selectFirstOption = false; }
3374
+ if (deselectAll === void 0) { deselectAll = false; }
3375
+ if (!this._isSelectElement) {
3376
+ if (!this.config.silent) {
3377
+ console.warn('refresh method can only be used on choices backed by a <select> element');
3378
+ }
3379
+ return this;
3380
+ }
3381
+ this._store.withTxn(function () {
3382
+ var choicesFromOptions = _this.passedElement.optionsAsChoices();
3383
+ // Build the list of items which require preserving
3384
+ var existingItems = {};
3385
+ if (!deselectAll) {
3386
+ _this._store.items.forEach(function (choice) {
3387
+ if (choice.id && choice.active && choice.selected) {
3388
+ existingItems[choice.value] = true;
3389
+ }
3390
+ });
3391
+ }
3392
+ _this.clearStore(false);
3393
+ var updateChoice = function (choice) {
3394
+ if (deselectAll) {
3395
+ _this._store.dispatch(removeItem$1(choice));
3396
+ }
3397
+ else if (existingItems[choice.value]) {
3398
+ choice.selected = true;
3399
+ }
3400
+ };
3401
+ choicesFromOptions.forEach(function (groupOrChoice) {
3402
+ if ('choices' in groupOrChoice) {
3403
+ groupOrChoice.choices.forEach(updateChoice);
3404
+ return;
3405
+ }
3406
+ updateChoice(groupOrChoice);
3407
+ });
3408
+ /* @todo only generate add events for the added options instead of all
3409
+ if (withEvents) {
3410
+ items.forEach((choice) => {
3411
+ if (existingItems[choice.value]) {
3412
+ this.passedElement.triggerEvent(
3413
+ EventType.removeItem,
3414
+ this._getChoiceForEvent(choice),
3415
+ );
3416
+ }
3417
+ });
3418
+ }
3419
+ */
3420
+ // load new choices & items
3421
+ _this._addPredefinedChoices(choicesFromOptions, selectFirstOption, withEvents);
3422
+ // re-do search if required
3423
+ if (_this._isSearching) {
3424
+ _this._searchChoices(_this.input.value);
3425
+ }
3426
+ });
3427
+ return this;
3428
+ };
3429
+ Choices.prototype.removeChoice = function (value) {
3430
+ var choice = this._store.choices.find(function (c) { return c.value === value; });
3431
+ if (!choice) {
3432
+ return this;
3433
+ }
3434
+ this._clearNotice();
3435
+ this._store.dispatch(removeChoice(choice));
3436
+ // @todo integrate with Store
3437
+ this._searcher.reset();
3438
+ if (choice.selected) {
3439
+ this.passedElement.triggerEvent(EventType.removeItem, this._getChoiceForOutput(choice));
3440
+ }
3441
+ return this;
3442
+ };
3443
+ Choices.prototype.clearChoices = function (clearOptions, clearItems) {
3444
+ var _this = this;
3445
+ if (clearOptions === void 0) { clearOptions = true; }
3446
+ if (clearItems === void 0) { clearItems = false; }
3447
+ if (clearOptions) {
3448
+ if (clearItems) {
3449
+ this.passedElement.element.replaceChildren('');
3450
+ }
3451
+ else {
3452
+ this.passedElement.element.querySelectorAll(':not([selected])').forEach(function (el) {
3453
+ el.remove();
3454
+ });
3455
+ }
3456
+ }
3457
+ this.itemList.element.replaceChildren('');
3458
+ this.choiceList.element.replaceChildren('');
3459
+ this._clearNotice();
3460
+ this._store.withTxn(function () {
3461
+ var items = clearItems ? [] : _this._store.items;
3462
+ _this._store.reset();
3463
+ items.forEach(function (item) {
3464
+ _this._store.dispatch(addChoice(item));
3465
+ _this._store.dispatch(addItem(item));
3466
+ });
3467
+ });
3468
+ // @todo integrate with Store
3469
+ this._searcher.reset();
3470
+ return this;
3471
+ };
3472
+ Choices.prototype.clearStore = function (clearOptions) {
3473
+ if (clearOptions === void 0) { clearOptions = true; }
3474
+ this.clearChoices(clearOptions, true);
3475
+ this._stopSearch();
3476
+ this._lastAddedChoiceId = 0;
3477
+ this._lastAddedGroupId = 0;
3478
+ return this;
3479
+ };
3480
+ Choices.prototype.clearInput = function () {
3481
+ var shouldSetInputWidth = !this._isSelectOneElement;
3482
+ this.input.clear(shouldSetInputWidth);
3483
+ this._stopSearch();
3484
+ return this;
3485
+ };
3486
+ Choices.prototype._validateConfig = function () {
3487
+ var config = this.config;
3488
+ var invalidConfigOptions = diff(config, DEFAULT_CONFIG);
3489
+ if (invalidConfigOptions.length) {
3490
+ console.warn('Unknown config option(s) passed', invalidConfigOptions.join(', '));
3491
+ }
3492
+ if (config.allowHTML && config.allowHtmlUserInput) {
3493
+ if (config.addItems) {
3494
+ console.warn('Warning: allowHTML/allowHtmlUserInput/addItems all being true is strongly not recommended and may lead to XSS attacks');
3495
+ }
3496
+ if (config.addChoices) {
3497
+ console.warn('Warning: allowHTML/allowHtmlUserInput/addChoices all being true is strongly not recommended and may lead to XSS attacks');
3498
+ }
3499
+ }
3500
+ };
3501
+ Choices.prototype._render = function (changes) {
3502
+ if (changes === void 0) { changes = { choices: true, groups: true, items: true }; }
3503
+ if (this._store.inTxn()) {
3504
+ return;
3505
+ }
3506
+ if (this._isSelectElement) {
3507
+ if (changes.choices || changes.groups) {
3508
+ this._renderChoices();
3509
+ }
3510
+ }
3511
+ if (changes.items) {
3512
+ this._renderItems();
3513
+ }
3514
+ };
3515
+ Choices.prototype._renderChoices = function () {
3516
+ var _this = this;
3517
+ if (!this._canAddItems()) {
3518
+ return; // block rendering choices if the input limit is reached.
3519
+ }
3520
+ var _a = this, config = _a.config, isSearching = _a._isSearching;
3521
+ var _b = this._store, activeGroups = _b.activeGroups, activeChoices = _b.activeChoices;
3522
+ var renderLimit = 0;
3523
+ if (isSearching && config.searchResultLimit > 0) {
3524
+ renderLimit = config.searchResultLimit;
3525
+ }
3526
+ else if (config.renderChoiceLimit > 0) {
3527
+ renderLimit = config.renderChoiceLimit;
3528
+ }
3529
+ if (this._isSelectElement) {
3530
+ var backingOptions = activeChoices.filter(function (choice) { return !choice.element; });
3531
+ if (backingOptions.length) {
3532
+ this.passedElement.addOptions(backingOptions);
3533
+ }
3534
+ }
3535
+ var fragment = document.createDocumentFragment();
3536
+ var renderableChoices = function (choices) {
3537
+ return choices.filter(function (choice) {
3538
+ return !choice.placeholder && (isSearching ? !!choice.rank : config.renderSelectedChoices || !choice.selected);
3539
+ });
3540
+ };
3541
+ var selectableChoices = false;
3542
+ var renderChoices = function (choices, withinGroup, groupLabel) {
3543
+ if (isSearching) {
3544
+ // sortByRank is used to ensure stable sorting, as scores are non-unique
3545
+ // this additionally ensures fuseOptions.sortFn is not ignored
3546
+ choices.sort(sortByRank);
3547
+ }
3548
+ else if (config.shouldSort) {
3549
+ choices.sort(config.sorter);
3550
+ }
3551
+ var choiceLimit = choices.length;
3552
+ choiceLimit = !withinGroup && renderLimit && choiceLimit > renderLimit ? renderLimit : choiceLimit;
3553
+ choiceLimit--;
3554
+ choices.every(function (choice, index) {
3555
+ // choiceEl being empty signals the contents has probably significantly changed
3556
+ var dropdownItem = choice.choiceEl || _this._templates.choice(config, choice, config.itemSelectText, groupLabel);
3557
+ choice.choiceEl = dropdownItem;
3558
+ fragment.appendChild(dropdownItem);
3559
+ if (isSearching || !choice.selected) {
3560
+ selectableChoices = true;
3561
+ }
3562
+ return index < choiceLimit;
3563
+ });
3564
+ };
3565
+ if (activeChoices.length) {
3566
+ if (config.resetScrollPosition) {
3567
+ requestAnimationFrame(function () { return _this.choiceList.scrollToTop(); });
3568
+ }
3569
+ if (!this._hasNonChoicePlaceholder && !isSearching && this._isSelectOneElement) {
3570
+ // If we have a placeholder choice along with groups
3571
+ renderChoices(activeChoices.filter(function (choice) { return choice.placeholder && !choice.group; }), false, undefined);
3572
+ }
3573
+ // If we have grouped options
3574
+ if (activeGroups.length && !isSearching) {
3575
+ if (config.shouldSort) {
3576
+ activeGroups.sort(config.sorter);
3577
+ }
3578
+ // render Choices without group first, regardless of sort, otherwise they won't be distinguishable
3579
+ // from the last group
3580
+ renderChoices(activeChoices.filter(function (choice) { return !choice.placeholder && !choice.group; }), false, undefined);
3581
+ activeGroups.forEach(function (group) {
3582
+ var groupChoices = renderableChoices(group.choices);
3583
+ if (groupChoices.length) {
3584
+ if (group.label) {
3585
+ var dropdownGroup = group.groupEl || _this._templates.choiceGroup(_this.config, group);
3586
+ group.groupEl = dropdownGroup;
3587
+ dropdownGroup.remove();
3588
+ fragment.appendChild(dropdownGroup);
3589
+ }
3590
+ renderChoices(groupChoices, true, config.appendGroupInSearch && isSearching ? group.label : undefined);
3591
+ }
3592
+ });
3593
+ }
3594
+ else {
3595
+ renderChoices(renderableChoices(activeChoices), false, undefined);
3596
+ }
3597
+ }
3598
+ if (!selectableChoices && (isSearching || !fragment.children.length || !config.renderSelectedChoices)) {
3599
+ if (!this._notice) {
3600
+ this._notice = {
3601
+ text: resolveStringFunction(isSearching ? config.noResultsText : config.noChoicesText),
3602
+ type: isSearching ? NoticeTypes.noResults : NoticeTypes.noChoices,
3603
+ };
3604
+ }
3605
+ fragment.replaceChildren('');
3606
+ }
3607
+ this._renderNotice(fragment);
3608
+ this.choiceList.element.replaceChildren(fragment);
3609
+ if (selectableChoices) {
3610
+ this._highlightChoice();
3611
+ }
3612
+ };
3613
+ Choices.prototype._renderItems = function () {
3614
+ var _this = this;
3615
+ var items = this._store.items || [];
3616
+ var itemList = this.itemList.element;
3617
+ var config = this.config;
3618
+ var fragment = document.createDocumentFragment();
3619
+ var itemFromList = function (item) {
3620
+ return itemList.querySelector("[data-item][data-id=\"".concat(item.id, "\"]"));
3621
+ };
3622
+ var addItemToFragment = function (item) {
3623
+ var el = item.itemEl;
3624
+ if (el && el.parentElement) {
3625
+ return;
3626
+ }
3627
+ el = itemFromList(item) || _this._templates.item(config, item, config.removeItemButton);
3628
+ item.itemEl = el;
3629
+ fragment.appendChild(el);
3630
+ };
3631
+ // new items
3632
+ items.forEach(addItemToFragment);
3633
+ var addedItems = !!fragment.childNodes.length;
3634
+ if (this._isSelectOneElement) {
3635
+ var existingItems = itemList.children.length;
3636
+ if (addedItems || existingItems > 1) {
3637
+ var placeholder = itemList.querySelector(getClassNamesSelector(config.classNames.placeholder));
3638
+ if (placeholder) {
3639
+ placeholder.remove();
3640
+ }
3641
+ }
3642
+ else if (!addedItems && !existingItems && this._placeholderValue) {
3643
+ addedItems = true;
3644
+ addItemToFragment(mapInputToChoice({
3645
+ selected: true,
3646
+ value: '',
3647
+ label: this._placeholderValue,
3648
+ placeholder: true,
3649
+ }, false));
3650
+ }
3651
+ }
3652
+ if (addedItems) {
3653
+ itemList.append(fragment);
3654
+ if (config.shouldSortItems && !this._isSelectOneElement) {
3655
+ items.sort(config.sorter);
3656
+ // push sorting into the DOM
3657
+ items.forEach(function (item) {
3658
+ var el = itemFromList(item);
3659
+ if (el) {
3660
+ el.remove();
3661
+ fragment.append(el);
3662
+ }
3663
+ });
3664
+ itemList.append(fragment);
3665
+ }
3666
+ }
3667
+ if (this._isTextElement) {
3668
+ // Update the value of the hidden input
3669
+ this.passedElement.value = items.map(function (_a) {
3670
+ var value = _a.value;
3671
+ return value;
3672
+ }).join(config.delimiter);
3673
+ }
3674
+ };
3675
+ Choices.prototype._displayNotice = function (text, type, openDropdown) {
3676
+ if (openDropdown === void 0) { openDropdown = true; }
3677
+ var oldNotice = this._notice;
3678
+ if (oldNotice &&
3679
+ ((oldNotice.type === type && oldNotice.text === text) ||
3680
+ (oldNotice.type === NoticeTypes.addChoice &&
3681
+ (type === NoticeTypes.noResults || type === NoticeTypes.noChoices)))) {
3682
+ if (openDropdown) {
3683
+ this.showDropdown(true);
3684
+ }
3685
+ return;
3686
+ }
3687
+ this._clearNotice();
3688
+ this._notice = text
3689
+ ? {
3690
+ text: text,
3691
+ type: type,
3692
+ }
3693
+ : undefined;
3694
+ this._renderNotice();
3695
+ if (openDropdown && text) {
3696
+ this.showDropdown(true);
3697
+ }
3698
+ };
3699
+ Choices.prototype._clearNotice = function () {
3700
+ if (!this._notice) {
3701
+ return;
3702
+ }
3703
+ var noticeElement = this.choiceList.element.querySelector(getClassNamesSelector(this.config.classNames.notice));
3704
+ if (noticeElement) {
3705
+ noticeElement.remove();
3706
+ }
3707
+ this._notice = undefined;
3708
+ };
3709
+ Choices.prototype._renderNotice = function (fragment) {
3710
+ var noticeConf = this._notice;
3711
+ if (noticeConf) {
3712
+ var notice = this._templates.notice(this.config, noticeConf.text, noticeConf.type);
3713
+ if (fragment) {
3714
+ fragment.append(notice);
3715
+ }
3716
+ else {
3717
+ this.choiceList.prepend(notice);
3718
+ }
3719
+ }
3720
+ };
3721
+ // eslint-disable-next-line class-methods-use-this
3722
+ Choices.prototype._getChoiceForOutput = function (choice, keyCode) {
3723
+ return {
3724
+ id: choice.id,
3725
+ highlighted: choice.highlighted,
3726
+ labelClass: choice.labelClass,
3727
+ labelDescription: choice.labelDescription,
3728
+ customProperties: choice.customProperties,
3729
+ disabled: choice.disabled,
3730
+ active: choice.active,
3731
+ label: choice.label,
3732
+ placeholder: choice.placeholder,
3733
+ value: choice.value,
3734
+ groupValue: choice.group ? choice.group.label : undefined,
3735
+ element: choice.element,
3736
+ keyCode: keyCode,
3737
+ };
3738
+ };
3739
+ Choices.prototype._triggerChange = function (value) {
3740
+ if (value === undefined || value === null) {
3741
+ return;
3742
+ }
3743
+ this.passedElement.triggerEvent(EventType.change, {
3744
+ value: value,
3745
+ });
3746
+ };
3747
+ Choices.prototype._handleButtonAction = function (element) {
3748
+ var _this = this;
3749
+ var items = this._store.items;
3750
+ if (!items.length || !this.config.removeItems || !this.config.removeItemButton) {
3751
+ return;
3752
+ }
3753
+ var id = element && parseDataSetId(element.parentElement);
3754
+ var itemToRemove = id && items.find(function (item) { return item.id === id; });
3755
+ if (!itemToRemove) {
3756
+ return;
3757
+ }
3758
+ this._store.withTxn(function () {
3759
+ // Remove item associated with button
3760
+ _this._removeItem(itemToRemove);
3761
+ _this._triggerChange(itemToRemove.value);
3762
+ if (_this._isSelectOneElement && !_this._hasNonChoicePlaceholder) {
3763
+ var placeholderChoice = (_this.config.shouldSort ? _this._store.choices.reverse() : _this._store.choices).find(function (choice) { return choice.placeholder; });
3764
+ if (placeholderChoice) {
3765
+ _this._addItem(placeholderChoice);
3766
+ _this.unhighlightAll();
3767
+ if (placeholderChoice.value) {
3768
+ _this._triggerChange(placeholderChoice.value);
3769
+ }
3770
+ }
3771
+ }
3772
+ });
3773
+ };
3774
+ Choices.prototype._handleItemAction = function (element, hasShiftKey) {
3775
+ var _this = this;
3776
+ if (hasShiftKey === void 0) { hasShiftKey = false; }
3777
+ var items = this._store.items;
3778
+ if (!items.length || !this.config.removeItems || this._isSelectOneElement) {
3779
+ return;
3780
+ }
3781
+ var id = parseDataSetId(element);
3782
+ if (!id) {
3783
+ return;
3784
+ }
3785
+ // We only want to select one item with a click
3786
+ // so we deselect any items that aren't the target
3787
+ // unless shift is being pressed
3788
+ items.forEach(function (item) {
3789
+ if (item.id === id && !item.highlighted) {
3790
+ _this.highlightItem(item);
3791
+ }
3792
+ else if (!hasShiftKey && item.highlighted) {
3793
+ _this.unhighlightItem(item);
3794
+ }
3795
+ });
3796
+ // Focus input as without focus, a user cannot do anything with a
3797
+ // highlighted item
3798
+ this.input.focus();
3799
+ };
3800
+ Choices.prototype._handleChoiceAction = function (element) {
3801
+ var _this = this;
3802
+ // If we are clicking on an option
3803
+ var id = parseDataSetId(element);
3804
+ var choice = id && this._store.getChoiceById(id);
3805
+ if (!choice || choice.disabled) {
3806
+ return false;
3807
+ }
3808
+ var hasActiveDropdown = this.dropdown.isActive;
3809
+ if (!choice.selected) {
3810
+ if (!this._canAddItems()) {
3811
+ return true; // causes _onEnterKey to early out
3812
+ }
3813
+ this._store.withTxn(function () {
3814
+ _this._addItem(choice, true, true);
3815
+ _this.clearInput();
3816
+ _this.unhighlightAll();
3817
+ });
3818
+ this._triggerChange(choice.value);
3819
+ }
3820
+ // We want to close the dropdown if we are dealing with a single select box
3821
+ if (hasActiveDropdown && this.config.closeDropdownOnSelect) {
3822
+ this.hideDropdown(true);
3823
+ this.containerOuter.element.focus();
3824
+ }
3825
+ return true;
3826
+ };
3827
+ Choices.prototype._handleBackspace = function (items) {
3828
+ var config = this.config;
3829
+ if (!config.removeItems || !items.length) {
3830
+ return;
3831
+ }
3832
+ var lastItem = items[items.length - 1];
3833
+ var hasHighlightedItems = items.some(function (item) { return item.highlighted; });
3834
+ // If editing the last item is allowed and there are not other selected items,
3835
+ // we can edit the item value. Otherwise if we can remove items, remove all selected items
3836
+ if (config.editItems && !hasHighlightedItems && lastItem) {
3837
+ this.input.value = lastItem.value;
3838
+ this.input.setWidth();
3839
+ this._removeItem(lastItem);
3840
+ this._triggerChange(lastItem.value);
3841
+ }
3842
+ else {
3843
+ if (!hasHighlightedItems) {
3844
+ // Highlight last item if none already highlighted
3845
+ this.highlightItem(lastItem, false);
3846
+ }
3847
+ this.removeHighlightedItems(true);
3848
+ }
3849
+ };
3850
+ Choices.prototype._loadChoices = function () {
3851
+ var _a;
3852
+ var _this = this;
3853
+ var config = this.config;
3854
+ if (this._isTextElement) {
3855
+ // Assign preset items from passed object first
3856
+ this._presetChoices = config.items.map(function (e) { return mapInputToChoice(e, false); });
3857
+ // Add any values passed from attribute
3858
+ if (this.passedElement.value) {
3859
+ var elementItems = this.passedElement.value
3860
+ .split(config.delimiter)
3861
+ .map(function (e) { return mapInputToChoice(e, false, _this.config.allowHtmlUserInput); });
3862
+ this._presetChoices = this._presetChoices.concat(elementItems);
3863
+ }
3864
+ this._presetChoices.forEach(function (choice) {
3865
+ choice.selected = true;
3866
+ });
3867
+ }
3868
+ else if (this._isSelectElement) {
3869
+ // Assign preset choices from passed object
3870
+ this._presetChoices = config.choices.map(function (e) { return mapInputToChoice(e, true); });
3871
+ // Create array of choices from option elements
3872
+ var choicesFromOptions = this.passedElement.optionsAsChoices();
3873
+ if (choicesFromOptions) {
3874
+ (_a = this._presetChoices).push.apply(_a, choicesFromOptions);
3875
+ }
3876
+ }
3877
+ };
3878
+ Choices.prototype._handleLoadingState = function (setLoading) {
3879
+ if (setLoading === void 0) { setLoading = true; }
3880
+ var el = this.itemList.element;
3881
+ if (setLoading) {
3882
+ this.disable();
3883
+ this.containerOuter.addLoadingState();
3884
+ if (this._isSelectOneElement) {
3885
+ el.replaceChildren(this._templates.placeholder(this.config, this.config.loadingText));
3886
+ }
3887
+ else {
3888
+ this.input.placeholder = this.config.loadingText;
3889
+ }
3890
+ }
3891
+ else {
3892
+ this.enable();
3893
+ this.containerOuter.removeLoadingState();
3894
+ if (this._isSelectOneElement) {
3895
+ el.replaceChildren('');
3896
+ this._render();
3897
+ }
3898
+ else {
3899
+ this.input.placeholder = this._placeholderValue || '';
3900
+ }
3901
+ }
3902
+ };
3903
+ Choices.prototype._handleSearch = function (value) {
3904
+ if (!this.input.isFocussed) {
3905
+ return;
3906
+ }
3907
+ // Check that we have a value to search and the input was an alphanumeric character
3908
+ if (value !== null && typeof value !== 'undefined' && value.length >= this.config.searchFloor) {
3909
+ var resultCount = this.config.searchChoices ? this._searchChoices(value) : 0;
3910
+ if (resultCount !== null) {
3911
+ // Trigger search event
3912
+ this.passedElement.triggerEvent(EventType.search, {
3913
+ value: value,
3914
+ resultCount: resultCount,
3915
+ });
3916
+ }
3917
+ }
3918
+ else if (this._store.choices.some(function (option) { return !option.active; })) {
3919
+ this._stopSearch();
3920
+ }
3921
+ };
3922
+ Choices.prototype._canAddItems = function () {
3923
+ var config = this.config;
3924
+ var maxItemCount = config.maxItemCount, maxItemText = config.maxItemText;
3925
+ if (!config.singleModeForMultiSelect && maxItemCount > 0 && maxItemCount <= this._store.items.length) {
3926
+ this.choiceList.element.replaceChildren('');
3927
+ this._notice = undefined;
3928
+ this._displayNotice(typeof maxItemText === 'function' ? maxItemText(maxItemCount) : maxItemText, NoticeTypes.addChoice);
3929
+ return false;
3930
+ }
3931
+ if (this._notice && this._notice.type === NoticeTypes.addChoice) {
3932
+ this._clearNotice();
3933
+ }
3934
+ return true;
3935
+ };
3936
+ Choices.prototype._canCreateItem = function (value) {
3937
+ var config = this.config;
3938
+ var canAddItem = true;
3939
+ var notice = '';
3940
+ if (canAddItem && typeof config.addItemFilter === 'function' && !config.addItemFilter(value)) {
3941
+ canAddItem = false;
3942
+ notice = resolveNoticeFunction(config.customAddItemText, value);
3943
+ }
3944
+ if (canAddItem) {
3945
+ var foundChoice = this._store.choices.find(function (choice) { return config.valueComparer(choice.value, value); });
3946
+ if (foundChoice) {
3947
+ if (this._isSelectElement) {
3948
+ // for exact matches, do not prompt to add it as a custom choice
3949
+ this._displayNotice('', NoticeTypes.addChoice);
3950
+ return false;
3951
+ }
3952
+ if (!config.duplicateItemsAllowed) {
3953
+ canAddItem = false;
3954
+ notice = resolveNoticeFunction(config.uniqueItemText, value);
3955
+ }
3956
+ }
3957
+ }
3958
+ if (canAddItem) {
3959
+ notice = resolveNoticeFunction(config.addItemText, value);
3960
+ }
3961
+ if (notice) {
3962
+ this._displayNotice(notice, NoticeTypes.addChoice);
3963
+ }
3964
+ return canAddItem;
3965
+ };
3966
+ Choices.prototype._searchChoices = function (value) {
3967
+ var newValue = value.trim().replace(/\s{2,}/, ' ');
3968
+ // signal input didn't change search
3969
+ if (!newValue.length || newValue === this._currentValue) {
3970
+ return null;
3971
+ }
3972
+ var searcher = this._searcher;
3973
+ if (searcher.isEmptyIndex()) {
3974
+ searcher.index(this._store.searchableChoices);
3975
+ }
3976
+ // If new value matches the desired length and is not the same as the current value with a space
3977
+ var results = searcher.search(newValue);
3978
+ this._currentValue = newValue;
3979
+ this._highlightPosition = 0;
3980
+ this._isSearching = true;
3981
+ var notice = this._notice;
3982
+ var noticeType = notice && notice.type;
3983
+ if (noticeType !== NoticeTypes.addChoice) {
3984
+ if (!results.length) {
3985
+ this._displayNotice(resolveStringFunction(this.config.noResultsText), NoticeTypes.noResults);
3986
+ }
3987
+ else {
3988
+ this._clearNotice();
3989
+ }
3990
+ }
3991
+ this._store.dispatch(filterChoices(results));
3992
+ return results.length;
3993
+ };
3994
+ Choices.prototype._stopSearch = function () {
3995
+ if (this._isSearching) {
3996
+ this._currentValue = '';
3997
+ this._isSearching = false;
3998
+ this._clearNotice();
3999
+ this._store.dispatch(activateChoices(true));
4000
+ this.passedElement.triggerEvent(EventType.search, {
4001
+ value: '',
4002
+ resultCount: 0,
4003
+ });
4004
+ }
4005
+ };
4006
+ Choices.prototype._addEventListeners = function () {
4007
+ var documentElement = this._docRoot;
4008
+ var outerElement = this.containerOuter.element;
4009
+ var inputElement = this.input.element;
4010
+ // capture events - can cancel event processing or propagation
4011
+ documentElement.addEventListener('touchend', this._onTouchEnd, true);
4012
+ outerElement.addEventListener('keydown', this._onKeyDown, true);
4013
+ outerElement.addEventListener('mousedown', this._onMouseDown, true);
4014
+ // passive events - doesn't call `preventDefault` or `stopPropagation`
4015
+ documentElement.addEventListener('click', this._onClick, { passive: true });
4016
+ documentElement.addEventListener('touchmove', this._onTouchMove, {
4017
+ passive: true,
4018
+ });
4019
+ this.dropdown.element.addEventListener('mouseover', this._onMouseOver, {
4020
+ passive: true,
4021
+ });
4022
+ if (this._isSelectOneElement) {
4023
+ outerElement.addEventListener('focus', this._onFocus, {
4024
+ passive: true,
4025
+ });
4026
+ outerElement.addEventListener('blur', this._onBlur, {
4027
+ passive: true,
4028
+ });
4029
+ }
4030
+ inputElement.addEventListener('keyup', this._onKeyUp, {
4031
+ passive: true,
4032
+ });
4033
+ inputElement.addEventListener('input', this._onInput, {
4034
+ passive: true,
4035
+ });
4036
+ inputElement.addEventListener('focus', this._onFocus, {
4037
+ passive: true,
4038
+ });
4039
+ inputElement.addEventListener('blur', this._onBlur, {
4040
+ passive: true,
4041
+ });
4042
+ if (inputElement.form) {
4043
+ inputElement.form.addEventListener('reset', this._onFormReset, {
4044
+ passive: true,
4045
+ });
4046
+ }
4047
+ this.input.addEventListeners();
4048
+ };
4049
+ Choices.prototype._removeEventListeners = function () {
4050
+ var documentElement = this._docRoot;
4051
+ var outerElement = this.containerOuter.element;
4052
+ var inputElement = this.input.element;
4053
+ documentElement.removeEventListener('touchend', this._onTouchEnd, true);
4054
+ outerElement.removeEventListener('keydown', this._onKeyDown, true);
4055
+ outerElement.removeEventListener('mousedown', this._onMouseDown, true);
4056
+ documentElement.removeEventListener('click', this._onClick);
4057
+ documentElement.removeEventListener('touchmove', this._onTouchMove);
4058
+ this.dropdown.element.removeEventListener('mouseover', this._onMouseOver);
4059
+ if (this._isSelectOneElement) {
4060
+ outerElement.removeEventListener('focus', this._onFocus);
4061
+ outerElement.removeEventListener('blur', this._onBlur);
4062
+ }
4063
+ inputElement.removeEventListener('keyup', this._onKeyUp);
4064
+ inputElement.removeEventListener('input', this._onInput);
4065
+ inputElement.removeEventListener('focus', this._onFocus);
4066
+ inputElement.removeEventListener('blur', this._onBlur);
4067
+ if (inputElement.form) {
4068
+ inputElement.form.removeEventListener('reset', this._onFormReset);
4069
+ }
4070
+ this.input.removeEventListeners();
4071
+ };
4072
+ Choices.prototype._onKeyDown = function (event) {
4073
+ var keyCode = event.keyCode;
4074
+ var hasActiveDropdown = this.dropdown.isActive;
4075
+ /*
4076
+ See:
4077
+ https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
4078
+ https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values
4079
+ https://en.wikipedia.org/wiki/UTF-16#Code_points_from_U+010000_to_U+10FFFF - UTF-16 surrogate pairs
4080
+ https://stackoverflow.com/a/70866532 - "Unidentified" for mobile
4081
+ http://www.unicode.org/versions/Unicode5.2.0/ch16.pdf#G19635 - U+FFFF is reserved (Section 16.7)
4082
+
4083
+ Logic: when a key event is sent, `event.key` represents its printable value _or_ one
4084
+ of a large list of special values indicating meta keys/functionality. In addition,
4085
+ key events for compose functionality contain a value of `Dead` when mid-composition.
4086
+
4087
+ I can't quite verify it, but non-English IMEs may also be able to generate key codes
4088
+ for code points in the surrogate-pair range, which could potentially be seen as having
4089
+ key.length > 1. Since `Fn` is one of the special keys, we can't distinguish by that
4090
+ alone.
4091
+
4092
+ Here, key.length === 1 means we know for sure the input was printable and not a special
4093
+ `key` value. When the length is greater than 1, it could be either a printable surrogate
4094
+ pair or a special `key` value. We can tell the difference by checking if the _character
4095
+ code_ value (not code point!) is in the "surrogate pair" range or not.
4096
+
4097
+ We don't use .codePointAt because an invalid code point would return 65535, which wouldn't
4098
+ pass the >= 0x10000 check we would otherwise use.
4099
+
4100
+ > ...The Unicode Standard sets aside 66 noncharacter code points. The last two code points
4101
+ > of each plane are noncharacters: U+FFFE and U+FFFF on the BMP...
4102
+ */
4103
+ var wasPrintableChar = event.key.length === 1 ||
4104
+ (event.key.length === 2 && event.key.charCodeAt(0) >= 0xd800) ||
4105
+ event.key === 'Unidentified';
4106
+ /*
4107
+ We do not show the dropdown if focusing out with esc or navigating through input fields.
4108
+ An activated search can still be opened with any other key.
4109
+ */
4110
+ if (!this._isTextElement &&
4111
+ !hasActiveDropdown &&
4112
+ keyCode !== KeyCodeMap.ESC_KEY &&
4113
+ keyCode !== KeyCodeMap.TAB_KEY &&
4114
+ keyCode !== KeyCodeMap.SHIFT_KEY) {
4115
+ this.showDropdown();
4116
+ if (!this.input.isFocussed && wasPrintableChar) {
4117
+ /*
4118
+ We update the input value with the pressed key as
4119
+ the input was not focussed at the time of key press
4120
+ therefore does not have the value of the key.
4121
+ */
4122
+ this.input.value += event.key;
4123
+ // browsers interpret a space as pagedown
4124
+ if (event.key === ' ') {
4125
+ event.preventDefault();
4126
+ }
4127
+ }
4128
+ }
4129
+ switch (keyCode) {
4130
+ case KeyCodeMap.A_KEY:
4131
+ return this._onSelectKey(event, this.itemList.element.hasChildNodes());
4132
+ case KeyCodeMap.ENTER_KEY:
4133
+ return this._onEnterKey(event, hasActiveDropdown);
4134
+ case KeyCodeMap.ESC_KEY:
4135
+ return this._onEscapeKey(event, hasActiveDropdown);
4136
+ case KeyCodeMap.UP_KEY:
4137
+ case KeyCodeMap.PAGE_UP_KEY:
4138
+ case KeyCodeMap.DOWN_KEY:
4139
+ case KeyCodeMap.PAGE_DOWN_KEY:
4140
+ return this._onDirectionKey(event, hasActiveDropdown);
4141
+ case KeyCodeMap.DELETE_KEY:
4142
+ case KeyCodeMap.BACK_KEY:
4143
+ return this._onDeleteKey(event, this._store.items, this.input.isFocussed);
4144
+ }
4145
+ };
4146
+ Choices.prototype._onKeyUp = function ( /* event: KeyboardEvent */) {
4147
+ this._canSearch = this.config.searchEnabled;
4148
+ };
4149
+ Choices.prototype._onInput = function ( /* event: InputEvent */) {
4150
+ var value = this.input.value;
4151
+ if (!value) {
4152
+ if (this._isTextElement) {
4153
+ this.hideDropdown(true);
4154
+ }
4155
+ else {
4156
+ this._stopSearch();
4157
+ }
4158
+ return;
4159
+ }
4160
+ if (!this._canAddItems()) {
4161
+ return;
4162
+ }
4163
+ if (this._canSearch) {
4164
+ // do the search even if the entered text can not be added
4165
+ this._handleSearch(value);
4166
+ }
4167
+ if (!this._canAddUserChoices) {
4168
+ return;
4169
+ }
4170
+ // determine if a notice needs to be displayed for why a search result can't be added
4171
+ this._canCreateItem(value);
4172
+ if (this._isSelectElement) {
4173
+ this._highlightPosition = 0; // reset to select the notice and/or exact match
4174
+ this._highlightChoice();
4175
+ }
4176
+ };
4177
+ Choices.prototype._onSelectKey = function (event, hasItems) {
4178
+ // If CTRL + A or CMD + A have been pressed and there are items to select
4179
+ if ((event.ctrlKey || event.metaKey) && hasItems) {
4180
+ this._canSearch = false;
4181
+ var shouldHightlightAll = this.config.removeItems && !this.input.value && this.input.element === document.activeElement;
4182
+ if (shouldHightlightAll) {
4183
+ this.highlightAll();
4184
+ }
4185
+ }
4186
+ };
4187
+ Choices.prototype._onEnterKey = function (event, hasActiveDropdown) {
4188
+ var _this = this;
4189
+ var value = this.input.value;
4190
+ var target = event.target;
4191
+ event.preventDefault();
4192
+ if (target && target.hasAttribute('data-button')) {
4193
+ this._handleButtonAction(target);
4194
+ return;
4195
+ }
4196
+ if (!hasActiveDropdown) {
4197
+ if (this._isSelectElement || this._notice) {
4198
+ this.showDropdown();
4199
+ }
4200
+ return;
4201
+ }
4202
+ var highlightedChoice = this.dropdown.element.querySelector(getClassNamesSelector(this.config.classNames.highlightedState));
4203
+ if (highlightedChoice && this._handleChoiceAction(highlightedChoice)) {
4204
+ return;
4205
+ }
4206
+ if (!target || !value) {
4207
+ this.hideDropdown(true);
4208
+ return;
4209
+ }
4210
+ if (!this._canAddItems()) {
4211
+ return;
4212
+ }
4213
+ var addedItem = false;
4214
+ this._store.withTxn(function () {
4215
+ addedItem = _this._findAndSelectChoiceByValue(value, true);
4216
+ if (!addedItem) {
4217
+ if (!_this._canAddUserChoices) {
4218
+ return;
4219
+ }
4220
+ if (!_this._canCreateItem(value)) {
4221
+ return;
4222
+ }
4223
+ _this._addChoice(mapInputToChoice(value, false, _this.config.allowHtmlUserInput), true, true);
4224
+ addedItem = true;
4225
+ }
4226
+ _this.clearInput();
4227
+ _this.unhighlightAll();
4228
+ });
4229
+ if (!addedItem) {
4230
+ return;
4231
+ }
4232
+ this._triggerChange(value);
4233
+ if (this.config.closeDropdownOnSelect) {
4234
+ this.hideDropdown(true);
4235
+ }
4236
+ };
4237
+ Choices.prototype._onEscapeKey = function (event, hasActiveDropdown) {
4238
+ if (hasActiveDropdown) {
4239
+ event.stopPropagation();
4240
+ this.hideDropdown(true);
4241
+ this._stopSearch();
4242
+ this.containerOuter.element.focus();
4243
+ }
4244
+ };
4245
+ Choices.prototype._onDirectionKey = function (event, hasActiveDropdown) {
4246
+ var keyCode = event.keyCode;
4247
+ // If up or down key is pressed, traverse through options
4248
+ if (hasActiveDropdown || this._isSelectOneElement) {
4249
+ this.showDropdown();
4250
+ this._canSearch = false;
4251
+ var directionInt = keyCode === KeyCodeMap.DOWN_KEY || keyCode === KeyCodeMap.PAGE_DOWN_KEY ? 1 : -1;
4252
+ var skipKey = event.metaKey || keyCode === KeyCodeMap.PAGE_DOWN_KEY || keyCode === KeyCodeMap.PAGE_UP_KEY;
4253
+ var nextEl = void 0;
4254
+ if (skipKey) {
4255
+ if (directionInt > 0) {
4256
+ nextEl = this.dropdown.element.querySelector("".concat(selectableChoiceIdentifier, ":last-of-type"));
4257
+ }
4258
+ else {
4259
+ nextEl = this.dropdown.element.querySelector(selectableChoiceIdentifier);
4260
+ }
4261
+ }
4262
+ else {
4263
+ var currentEl = this.dropdown.element.querySelector(getClassNamesSelector(this.config.classNames.highlightedState));
4264
+ if (currentEl) {
4265
+ nextEl = getAdjacentEl(currentEl, selectableChoiceIdentifier, directionInt);
4266
+ }
4267
+ else {
4268
+ nextEl = this.dropdown.element.querySelector(selectableChoiceIdentifier);
4269
+ }
4270
+ }
4271
+ if (nextEl) {
4272
+ // We prevent default to stop the cursor moving
4273
+ // when pressing the arrow
4274
+ if (!isScrolledIntoView(nextEl, this.choiceList.element, directionInt)) {
4275
+ this.choiceList.scrollToChildElement(nextEl, directionInt);
4276
+ }
4277
+ this._highlightChoice(nextEl);
4278
+ }
4279
+ // Prevent default to maintain cursor position whilst
4280
+ // traversing dropdown options
4281
+ event.preventDefault();
4282
+ }
4283
+ };
4284
+ Choices.prototype._onDeleteKey = function (event, items, hasFocusedInput) {
4285
+ // If backspace or delete key is pressed and the input has no value
4286
+ if (!this._isSelectOneElement && !event.target.value && hasFocusedInput) {
4287
+ this._handleBackspace(items);
4288
+ event.preventDefault();
4289
+ }
4290
+ };
4291
+ Choices.prototype._onTouchMove = function () {
4292
+ if (this._wasTap) {
4293
+ this._wasTap = false;
4294
+ }
4295
+ };
4296
+ Choices.prototype._onTouchEnd = function (event) {
4297
+ var target = (event || event.touches[0]).target;
4298
+ var touchWasWithinContainer = this._wasTap && this.containerOuter.element.contains(target);
4299
+ if (touchWasWithinContainer) {
4300
+ var containerWasExactTarget = target === this.containerOuter.element || target === this.containerInner.element;
4301
+ if (containerWasExactTarget) {
4302
+ if (this._isTextElement) {
4303
+ this.input.focus();
4304
+ }
4305
+ else if (this._isSelectMultipleElement) {
4306
+ this.showDropdown();
4307
+ }
4308
+ }
4309
+ // Prevents focus event firing
4310
+ event.stopPropagation();
4311
+ }
4312
+ this._wasTap = true;
4313
+ };
4314
+ /**
4315
+ * Handles mousedown event in capture mode for containetOuter.element
4316
+ */
4317
+ Choices.prototype._onMouseDown = function (event) {
4318
+ var target = event.target;
4319
+ if (!(target instanceof HTMLElement)) {
4320
+ return;
4321
+ }
4322
+ // If we have our mouse down on the scrollbar and are on IE11...
4323
+ if (IS_IE11 && this.choiceList.element.contains(target)) {
4324
+ // check if click was on a scrollbar area
4325
+ var firstChoice = this.choiceList.element.firstElementChild;
4326
+ this._isScrollingOnIe =
4327
+ this._direction === 'ltr' ? event.offsetX >= firstChoice.offsetWidth : event.offsetX < firstChoice.offsetLeft;
4328
+ }
4329
+ if (target === this.input.element) {
4330
+ return;
4331
+ }
4332
+ var item = target.closest('[data-button],[data-item],[data-choice]');
4333
+ if (item instanceof HTMLElement) {
4334
+ if ('button' in item.dataset) {
4335
+ this._handleButtonAction(item);
4336
+ }
4337
+ else if ('item' in item.dataset) {
4338
+ this._handleItemAction(item, event.shiftKey);
4339
+ }
4340
+ else if ('choice' in item.dataset) {
4341
+ this._handleChoiceAction(item);
4342
+ }
4343
+ }
4344
+ event.preventDefault();
4345
+ };
4346
+ /**
4347
+ * Handles mouseover event over this.dropdown
4348
+ * @param {MouseEvent} event
4349
+ */
4350
+ Choices.prototype._onMouseOver = function (_a) {
4351
+ var target = _a.target;
4352
+ if (target instanceof HTMLElement && 'choice' in target.dataset) {
4353
+ this._highlightChoice(target);
4354
+ }
4355
+ };
4356
+ Choices.prototype._onClick = function (_a) {
4357
+ var target = _a.target;
4358
+ var containerOuter = this.containerOuter;
4359
+ var clickWasWithinContainer = containerOuter.element.contains(target);
4360
+ if (clickWasWithinContainer) {
4361
+ if (!this.dropdown.isActive && !containerOuter.isDisabled) {
4362
+ if (this._isTextElement) {
4363
+ if (document.activeElement !== this.input.element) {
4364
+ this.input.focus();
4365
+ }
4366
+ }
4367
+ else {
4368
+ this.showDropdown();
4369
+ containerOuter.element.focus();
4370
+ }
4371
+ }
4372
+ else if (this._isSelectOneElement &&
4373
+ target !== this.input.element &&
4374
+ !this.dropdown.element.contains(target)) {
4375
+ this.hideDropdown();
4376
+ }
4377
+ }
4378
+ else {
4379
+ containerOuter.removeFocusState();
4380
+ this.hideDropdown(true);
4381
+ this.unhighlightAll();
4382
+ }
4383
+ };
4384
+ Choices.prototype._onFocus = function (_a) {
4385
+ var target = _a.target;
4386
+ var containerOuter = this.containerOuter;
4387
+ var focusWasWithinContainer = target && containerOuter.element.contains(target);
4388
+ if (!focusWasWithinContainer) {
4389
+ return;
4390
+ }
4391
+ var targetIsInput = target === this.input.element;
4392
+ if (this._isTextElement) {
4393
+ if (targetIsInput) {
4394
+ containerOuter.addFocusState();
4395
+ }
4396
+ }
4397
+ else if (this._isSelectMultipleElement) {
4398
+ if (targetIsInput) {
4399
+ this.showDropdown(true);
4400
+ // If element is a select box, the focused element is the container and the dropdown
4401
+ // isn't already open, focus and show dropdown
4402
+ containerOuter.addFocusState();
4403
+ }
4404
+ }
4405
+ else {
4406
+ containerOuter.addFocusState();
4407
+ if (targetIsInput) {
4408
+ this.showDropdown(true);
4409
+ }
4410
+ }
4411
+ };
4412
+ Choices.prototype._onBlur = function (_a) {
4413
+ var target = _a.target;
4414
+ var containerOuter = this.containerOuter;
4415
+ var blurWasWithinContainer = target && containerOuter.element.contains(target);
4416
+ if (blurWasWithinContainer && !this._isScrollingOnIe) {
4417
+ if (target === this.input.element) {
4418
+ containerOuter.removeFocusState();
4419
+ this.hideDropdown(true);
4420
+ if (this._isTextElement || this._isSelectMultipleElement) {
4421
+ this.unhighlightAll();
4422
+ }
4423
+ }
4424
+ else if (target === this.containerOuter.element) {
4425
+ // Remove the focus state when the past outerContainer was the target
4426
+ containerOuter.removeFocusState();
4427
+ // Also close the dropdown if search is disabled
4428
+ if (!this._canSearch) {
4429
+ this.hideDropdown(true);
4430
+ }
4431
+ }
4432
+ }
4433
+ else {
4434
+ // On IE11, clicking the scollbar blurs our input and thus
4435
+ // closes the dropdown. To stop this, we refocus our input
4436
+ // if we know we are on IE *and* are scrolling.
4437
+ this._isScrollingOnIe = false;
4438
+ this.input.element.focus();
4439
+ }
4440
+ };
4441
+ Choices.prototype._onFormReset = function () {
4442
+ var _this = this;
4443
+ this._store.withTxn(function () {
4444
+ _this.clearInput();
4445
+ _this.hideDropdown();
4446
+ _this.refresh(false, false, true);
4447
+ if (_this._initialItems.length) {
4448
+ _this.setChoiceByValue(_this._initialItems);
4449
+ }
4450
+ });
4451
+ };
4452
+ Choices.prototype._highlightChoice = function (el) {
4453
+ if (el === void 0) { el = null; }
4454
+ var choices = Array.from(this.dropdown.element.querySelectorAll(selectableChoiceIdentifier));
4455
+ if (!choices.length) {
4456
+ return;
4457
+ }
4458
+ var passedEl = el;
4459
+ var highlightedState = this.config.classNames.highlightedState;
4460
+ var highlightedChoices = Array.from(this.dropdown.element.querySelectorAll(getClassNamesSelector(highlightedState)));
4461
+ // Remove any highlighted choices
4462
+ highlightedChoices.forEach(function (choice) {
4463
+ removeClassesFromElement(choice, highlightedState);
4464
+ choice.setAttribute('aria-selected', 'false');
4465
+ });
4466
+ if (passedEl) {
4467
+ this._highlightPosition = choices.indexOf(passedEl);
4468
+ }
4469
+ else {
4470
+ // Highlight choice based on last known highlight location
4471
+ if (choices.length > this._highlightPosition) {
4472
+ // If we have an option to highlight
4473
+ passedEl = choices[this._highlightPosition];
4474
+ }
4475
+ else {
4476
+ // Otherwise highlight the option before
4477
+ passedEl = choices[choices.length - 1];
4478
+ }
4479
+ if (!passedEl) {
4480
+ passedEl = choices[0];
4481
+ }
4482
+ }
4483
+ addClassesToElement(passedEl, highlightedState);
4484
+ passedEl.setAttribute('aria-selected', 'true');
4485
+ this.passedElement.triggerEvent(EventType.highlightChoice, {
4486
+ el: passedEl,
4487
+ });
4488
+ if (this.dropdown.isActive) {
4489
+ // IE11 ignores aria-label and blocks virtual keyboard
4490
+ // if aria-activedescendant is set without a dropdown
4491
+ this.input.setActiveDescendant(passedEl.id);
4492
+ this.containerOuter.setActiveDescendant(passedEl.id);
4493
+ }
4494
+ };
4495
+ Choices.prototype._addItem = function (item, withEvents, userTriggered) {
4496
+ if (withEvents === void 0) { withEvents = true; }
4497
+ if (userTriggered === void 0) { userTriggered = false; }
4498
+ if (!item.id) {
4499
+ throw new TypeError('item.id must be set before _addItem is called for a choice/item');
4500
+ }
4501
+ if (this.config.singleModeForMultiSelect || this._isSelectOneElement) {
4502
+ this.removeActiveItems(item.id);
4503
+ }
4504
+ this._store.dispatch(addItem(item));
4505
+ if (withEvents) {
4506
+ this.passedElement.triggerEvent(EventType.addItem, this._getChoiceForOutput(item));
4507
+ if (userTriggered) {
4508
+ this.passedElement.triggerEvent(EventType.choice, this._getChoiceForOutput(item));
4509
+ }
4510
+ }
4511
+ };
4512
+ Choices.prototype._removeItem = function (item) {
4513
+ if (!item.id) {
4514
+ return;
4515
+ }
4516
+ this._store.dispatch(removeItem$1(item));
4517
+ var notice = this._notice;
4518
+ if (notice && notice.type === NoticeTypes.noChoices) {
4519
+ this._clearNotice();
4520
+ }
4521
+ this.passedElement.triggerEvent(EventType.removeItem, this._getChoiceForOutput(item));
4522
+ };
4523
+ Choices.prototype._addChoice = function (choice, withEvents, userTriggered) {
4524
+ if (withEvents === void 0) { withEvents = true; }
4525
+ if (userTriggered === void 0) { userTriggered = false; }
4526
+ if (choice.id) {
4527
+ throw new TypeError('Can not re-add a choice which has already been added');
4528
+ }
4529
+ var config = this.config;
4530
+ if (!config.duplicateItemsAllowed && this._store.choices.find(function (c) { return config.valueComparer(c.value, choice.value); })) {
4531
+ return;
4532
+ }
4533
+ // Generate unique id, in-place update is required so chaining _addItem works as expected
4534
+ this._lastAddedChoiceId++;
4535
+ choice.id = this._lastAddedChoiceId;
4536
+ choice.elementId = "".concat(this._baseId, "-").concat(this._idNames.itemChoice, "-").concat(choice.id);
4537
+ var prependValue = config.prependValue, appendValue = config.appendValue;
4538
+ if (prependValue) {
4539
+ choice.value = prependValue + choice.value;
4540
+ }
4541
+ if (appendValue) {
4542
+ choice.value += appendValue.toString();
4543
+ }
4544
+ if ((prependValue || appendValue) && choice.element) {
4545
+ choice.element.value = choice.value;
4546
+ }
4547
+ this._clearNotice();
4548
+ this._store.dispatch(addChoice(choice));
4549
+ if (choice.selected) {
4550
+ this._addItem(choice, withEvents, userTriggered);
4551
+ }
4552
+ };
4553
+ Choices.prototype._addGroup = function (group, withEvents) {
4554
+ var _this = this;
4555
+ if (withEvents === void 0) { withEvents = true; }
4556
+ if (group.id) {
4557
+ throw new TypeError('Can not re-add a group which has already been added');
4558
+ }
4559
+ this._store.dispatch(addGroup(group));
4560
+ if (!group.choices) {
4561
+ return;
4562
+ }
4563
+ // add unique id for the group(s), and do not store the full list of choices in this group
4564
+ this._lastAddedGroupId++;
4565
+ group.id = this._lastAddedGroupId;
4566
+ group.choices.forEach(function (item) {
4567
+ item.group = group;
4568
+ if (group.disabled) {
4569
+ item.disabled = true;
4570
+ }
4571
+ _this._addChoice(item, withEvents);
4572
+ });
4573
+ };
4574
+ Choices.prototype._createTemplates = function () {
4575
+ var _this = this;
4576
+ var callbackOnCreateTemplates = this.config.callbackOnCreateTemplates;
4577
+ var userTemplates = {};
4578
+ if (typeof callbackOnCreateTemplates === 'function') {
4579
+ userTemplates = callbackOnCreateTemplates.call(this, strToEl, escapeForTemplate, getClassNames);
4580
+ }
4581
+ var templating = {};
4582
+ Object.keys(this._templates).forEach(function (name) {
4583
+ if (name in userTemplates) {
4584
+ templating[name] = userTemplates[name].bind(_this);
4585
+ }
4586
+ else {
4587
+ templating[name] = _this._templates[name].bind(_this);
4588
+ }
4589
+ });
4590
+ this._templates = templating;
4591
+ };
4592
+ Choices.prototype._createElements = function () {
4593
+ var templating = this._templates;
4594
+ var _a = this, config = _a.config, isSelectOneElement = _a._isSelectOneElement;
4595
+ var position = config.position, classNames = config.classNames;
4596
+ var elementType = this._elementType;
4597
+ this.containerOuter = new Container({
4598
+ element: templating.containerOuter(config, this._direction, this._isSelectElement, isSelectOneElement, config.searchEnabled, elementType, config.labelId),
4599
+ classNames: classNames,
4600
+ type: elementType,
4601
+ position: position,
4602
+ });
4603
+ this.containerInner = new Container({
4604
+ element: templating.containerInner(config),
4605
+ classNames: classNames,
4606
+ type: elementType,
4607
+ position: position,
4608
+ });
4609
+ this.input = new Input({
4610
+ element: templating.input(config, this._placeholderValue),
4611
+ classNames: classNames,
4612
+ type: elementType,
4613
+ preventPaste: !config.paste,
4614
+ });
4615
+ this.choiceList = new List({
4616
+ element: templating.choiceList(config, isSelectOneElement),
4617
+ });
4618
+ this.itemList = new List({
4619
+ element: templating.itemList(config, isSelectOneElement),
4620
+ });
4621
+ this.dropdown = new Dropdown({
4622
+ element: templating.dropdown(config),
4623
+ classNames: classNames,
4624
+ type: elementType,
4625
+ });
4626
+ };
4627
+ Choices.prototype._createStructure = function () {
4628
+ var _a = this, containerInner = _a.containerInner, containerOuter = _a.containerOuter, passedElement = _a.passedElement;
4629
+ var dropdownElement = this.dropdown.element;
4630
+ // Hide original element
4631
+ passedElement.conceal();
4632
+ // Wrap input in container preserving DOM ordering
4633
+ containerInner.wrap(passedElement.element);
4634
+ // Wrapper inner container with outer container
4635
+ containerOuter.wrap(containerInner.element);
4636
+ if (this._isSelectOneElement) {
4637
+ this.input.placeholder = this.config.searchPlaceholderValue || '';
4638
+ }
4639
+ else {
4640
+ if (this._placeholderValue) {
4641
+ this.input.placeholder = this._placeholderValue;
4642
+ }
4643
+ this.input.setWidth();
4644
+ }
4645
+ containerOuter.element.appendChild(containerInner.element);
4646
+ containerOuter.element.appendChild(dropdownElement);
4647
+ containerInner.element.appendChild(this.itemList.element);
4648
+ dropdownElement.appendChild(this.choiceList.element);
4649
+ if (!this._isSelectOneElement) {
4650
+ containerInner.element.appendChild(this.input.element);
4651
+ }
4652
+ else if (this.config.searchEnabled) {
4653
+ dropdownElement.insertBefore(this.input.element, dropdownElement.firstChild);
4654
+ }
4655
+ this._highlightPosition = 0;
4656
+ this._isSearching = false;
4657
+ };
4658
+ Choices.prototype._initStore = function () {
4659
+ var _this = this;
4660
+ this._store.subscribe(this._render).withTxn(function () {
4661
+ _this._addPredefinedChoices(_this._presetChoices, _this._isSelectOneElement && !_this._hasNonChoicePlaceholder, false);
4662
+ });
4663
+ if (!this._store.choices.length || (this._isSelectOneElement && this._hasNonChoicePlaceholder)) {
4664
+ this._render();
4665
+ }
4666
+ };
4667
+ Choices.prototype._addPredefinedChoices = function (choices, selectFirstOption, withEvents) {
4668
+ var _this = this;
4669
+ if (selectFirstOption === void 0) { selectFirstOption = false; }
4670
+ if (withEvents === void 0) { withEvents = true; }
4671
+ if (selectFirstOption) {
4672
+ /**
4673
+ * If there is a selected choice already or the choice is not the first in
4674
+ * the array, add each choice normally.
4675
+ *
4676
+ * Otherwise we pre-select the first enabled choice in the array ("select-one" only)
4677
+ */
4678
+ var noSelectedChoices = choices.findIndex(function (choice) { return choice.selected; }) === -1;
4679
+ if (noSelectedChoices) {
4680
+ choices.some(function (choice) {
4681
+ if (choice.disabled || 'choices' in choice) {
4682
+ return false;
4683
+ }
4684
+ choice.selected = true;
4685
+ return true;
4686
+ });
4687
+ }
4688
+ }
4689
+ choices.forEach(function (item) {
4690
+ if ('choices' in item) {
4691
+ if (_this._isSelectElement) {
4692
+ _this._addGroup(item, withEvents);
4693
+ }
4694
+ }
4695
+ else {
4696
+ _this._addChoice(item, withEvents);
4697
+ }
4698
+ });
4699
+ };
4700
+ Choices.prototype._findAndSelectChoiceByValue = function (value, userTriggered) {
4701
+ var _this = this;
4702
+ if (userTriggered === void 0) { userTriggered = false; }
4703
+ // Check 'value' property exists and the choice isn't already selected
4704
+ var foundChoice = this._store.choices.find(function (choice) { return _this.config.valueComparer(choice.value, value); });
4705
+ if (foundChoice && !foundChoice.disabled && !foundChoice.selected) {
4706
+ this._addItem(foundChoice, true, userTriggered);
4707
+ return true;
4708
+ }
4709
+ return false;
4710
+ };
4711
+ Choices.prototype._generatePlaceholderValue = function () {
4712
+ var config = this.config;
4713
+ if (!config.placeholder) {
4714
+ return null;
4715
+ }
4716
+ if (this._hasNonChoicePlaceholder) {
4717
+ return config.placeholderValue;
4718
+ }
4719
+ if (this._isSelectElement) {
4720
+ var placeholderOption = this.passedElement.placeholderOption;
4721
+ return placeholderOption ? placeholderOption.text : null;
4722
+ }
4723
+ return null;
4724
+ };
4725
+ Choices.prototype._warnChoicesInitFailed = function (caller) {
4726
+ if (this.config.silent) {
4727
+ return;
4728
+ }
4729
+ if (!this.initialised) {
4730
+ throw new TypeError("".concat(caller, " called on a non-initialised instance of Choices"));
4731
+ }
4732
+ else if (!this.initialisedOK) {
4733
+ throw new TypeError("".concat(caller, " called for an element which has multiple instances of Choices initialised on it"));
4734
+ }
4735
+ };
4736
+ Choices.version = '11.1.0';
4737
+ return Choices;
4738
+ }());
4739
+
4740
+ export { Choices as default };