@ons/design-system 70.0.6 → 70.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -17
- package/components/access-code/_macro.spec.js +145 -145
- package/components/access-code/access-code.dom.js +5 -5
- package/components/access-code/access-code.js +16 -16
- package/components/access-code/access-code.scss +22 -22
- package/components/access-code/access-code.spec.js +17 -17
- package/components/accordion/_macro.spec.js +154 -154
- package/components/accordion/accordion.dom.js +10 -10
- package/components/accordion/accordion.js +50 -50
- package/components/accordion/accordion.spec.js +104 -104
- package/components/address-input/_macro.spec.js +420 -420
- package/components/address-input/autosuggest.address.dom.js +5 -5
- package/components/address-input/autosuggest.address.error.js +77 -77
- package/components/address-input/autosuggest.address.js +357 -357
- package/components/address-input/autosuggest.address.setter.js +95 -95
- package/components/address-input/autosuggest.address.spec.js +668 -651
- package/components/address-input/example-address-input-editable.njk +1 -1
- package/components/address-input/example-address-input.njk +1 -1
- package/components/address-output/_address-output.scss +3 -3
- package/components/address-output/_macro.spec.js +84 -84
- package/components/autosuggest/_autosuggest.scss +114 -114
- package/components/autosuggest/_macro.spec.js +255 -255
- package/components/autosuggest/autosuggest.dom.js +5 -5
- package/components/autosuggest/autosuggest.helpers.js +11 -11
- package/components/autosuggest/autosuggest.helpers.spec.js +73 -73
- package/components/autosuggest/autosuggest.js +20 -20
- package/components/autosuggest/autosuggest.spec.js +542 -536
- package/components/autosuggest/autosuggest.ui.js +478 -475
- package/components/autosuggest/fuse-config.js +17 -17
- package/components/back-to-top/_back-to-top.scss +27 -27
- package/components/back-to-top/_macro.spec.js +49 -49
- package/components/back-to-top/back-to-top.dom.js +5 -5
- package/components/back-to-top/back-to-top.js +52 -52
- package/components/back-to-top/back-to-top.spec.js +106 -106
- package/components/back-to-top/example-back-to-top.njk +6 -6
- package/components/breadcrumbs/_breadcrumbs.scss +63 -65
- package/components/breadcrumbs/_macro.spec.js +91 -91
- package/components/browser-banner/_browser-banner.scss +23 -23
- package/components/browser-banner/_macro.spec.js +92 -92
- package/components/button/_button.scss +482 -483
- package/components/button/_macro.spec.js +363 -363
- package/components/button/button.dom.js +15 -15
- package/components/button/button.js +53 -53
- package/components/button/button.spec.js +248 -240
- package/components/call-to-action/_call-to-action.scss +5 -5
- package/components/call-to-action/_macro.spec.js +28 -28
- package/components/card/_card.scss +23 -23
- package/components/card/_macro.spec.js +180 -180
- package/components/char-check-limit/_macro.spec.js +48 -48
- package/components/char-check-limit/character-check.js +58 -58
- package/components/char-check-limit/character-check.spec.js +173 -173
- package/components/char-check-limit/character-limit.js +40 -40
- package/components/checkboxes/_checkbox-macro.spec.js +355 -355
- package/components/checkboxes/_checkbox.scss +180 -180
- package/components/checkboxes/_checkboxes.scss +37 -27
- package/components/checkboxes/_macro.spec.js +261 -261
- package/components/checkboxes/checkbox-with-autoselect.js +32 -32
- package/components/checkboxes/checkbox-with-fieldset.js +21 -21
- package/components/checkboxes/checkboxes-with-reveal.js +10 -10
- package/components/checkboxes/checkboxes.dom.js +27 -27
- package/components/checkboxes/checkboxes.spec.js +183 -183
- package/components/content-pagination/_content-pagination.scss +41 -41
- package/components/content-pagination/_macro.spec.js +159 -159
- package/components/cookies-banner/_cookies-banner.scss +22 -22
- package/components/cookies-banner/_macro.spec.js +177 -177
- package/components/cookies-banner/cookies-banner.dom.js +7 -7
- package/components/cookies-banner/cookies-banner.js +76 -76
- package/components/cookies-banner/cookies-banner.spec.js +72 -68
- package/components/date-input/_macro.spec.js +338 -338
- package/components/description-list/_description-list.scss +23 -23
- package/components/description-list/_macro.spec.js +144 -144
- package/components/details/_details.scss +109 -109
- package/components/details/_macro.spec.js +132 -132
- package/components/details/details.dom.js +6 -6
- package/components/details/details.js +60 -60
- package/components/details/details.spec.js +106 -106
- package/components/document-list/_macro.spec.js +444 -444
- package/components/document-list/document-list.scss +145 -149
- package/components/download-resources/_download-resources.scss +109 -108
- package/components/download-resources/download-resources.js +907 -900
- package/components/download-resources/download-resources.spec.js +461 -461
- package/components/duration/_macro.spec.js +291 -291
- package/components/error/_macro.spec.js +72 -72
- package/components/external-link/_external-link.scss +19 -19
- package/components/external-link/_macro.spec.js +68 -68
- package/components/feedback/_feedback.scss +31 -31
- package/components/feedback/_macro.spec.js +72 -72
- package/components/field/_field-group.scss +10 -10
- package/components/field/_field.scss +16 -16
- package/components/field/_macro.spec.js +80 -80
- package/components/fieldset/_fieldset.scss +27 -27
- package/components/fieldset/_macro.spec.js +161 -161
- package/components/footer/_footer.scss +45 -45
- package/components/footer/_macro.spec.js +452 -452
- package/components/header/_header.scss +202 -202
- package/components/header/_macro.spec.js +833 -829
- package/components/helpers/_grid.scss +4 -4
- package/components/hero/_hero.scss +48 -48
- package/components/hero/_macro.spec.js +59 -59
- package/components/icon/_icon.scss +44 -44
- package/components/icon/_macro.spec.js +110 -110
- package/components/image/_image.scss +11 -11
- package/components/image/_macro.spec.js +81 -81
- package/components/input/_input-type.scss +86 -89
- package/components/input/_input.scss +123 -124
- package/components/input/_macro.spec.js +604 -604
- package/components/input/character-check.dom.js +5 -5
- package/components/input/input.dom.js +5 -5
- package/components/input/input.js +10 -10
- package/components/input/input.spec.js +18 -18
- package/components/label/_label.scss +24 -24
- package/components/label/_macro.spec.js +173 -170
- package/components/language-selector/_macro.spec.js +97 -97
- package/components/language-selector/language.scss +7 -7
- package/components/list/_list.scss +84 -84
- package/components/list/_macro.njk +2 -2
- package/components/list/_macro.spec.js +583 -583
- package/components/message/_macro.njk +5 -5
- package/components/message/_macro.spec.js +74 -74
- package/components/message/_message.scss +39 -39
- package/components/message-list/_macro.spec.js +86 -86
- package/components/message-list/_message-list.scss +16 -16
- package/components/modal/_macro.spec.js +69 -69
- package/components/modal/_modal.scss +36 -36
- package/components/modal/modal.dom.js +6 -6
- package/components/modal/modal.js +89 -89
- package/components/modal/modal.spec.js +50 -50
- package/components/mutually-exclusive/_macro.spec.js +140 -140
- package/components/mutually-exclusive/mutually-exclusive.checkboxes.spec.js +188 -188
- package/components/mutually-exclusive/mutually-exclusive.date.spec.js +211 -211
- package/components/mutually-exclusive/mutually-exclusive.dom.js +5 -5
- package/components/mutually-exclusive/mutually-exclusive.duration.spec.js +210 -210
- package/components/mutually-exclusive/mutually-exclusive.email.spec.js +90 -90
- package/components/mutually-exclusive/mutually-exclusive.js +137 -137
- package/components/mutually-exclusive/mutually-exclusive.multiple-options.checkboxes.spec.js +197 -197
- package/components/mutually-exclusive/mutually-exclusive.number.spec.js +96 -96
- package/components/mutually-exclusive/mutually-exclusive.textarea.spec.js +102 -102
- package/components/navigation/_macro.spec.js +360 -354
- package/components/navigation/_navigation.scss +122 -123
- package/components/navigation/navigation.dom.js +35 -35
- package/components/navigation/navigation.js +49 -49
- package/components/navigation/navigation.spec.js +249 -249
- package/components/pagination/_macro.spec.js +342 -335
- package/components/pagination/_pagination.scss +58 -58
- package/components/panel/_macro.spec.js +372 -372
- package/components/panel/_panel.scss +200 -199
- package/components/password/_macro.spec.js +95 -95
- package/components/password/password.dom.js +5 -5
- package/components/password/password.js +10 -10
- package/components/password/password.spec.js +26 -26
- package/components/phase-banner/_macro.spec.js +86 -86
- package/components/phase-banner/_phase-banner.scss +16 -16
- package/components/question/_macro.spec.js +235 -235
- package/components/question/_question.scss +24 -24
- package/components/quote/_macro.spec.js +52 -52
- package/components/quote/_quote.scss +24 -24
- package/components/radios/_macro.spec.js +524 -524
- package/components/radios/_radio.scss +49 -48
- package/components/radios/_radios.scss +14 -20
- package/components/radios/check-radios.js +21 -21
- package/components/radios/clear-radios.js +45 -45
- package/components/radios/radio-with-fieldset.js +22 -22
- package/components/radios/radios.dom.js +32 -32
- package/components/radios/radios.spec.js +251 -251
- package/components/related-content/_macro.spec.js +109 -109
- package/components/related-content/_related-content.scss +12 -12
- package/components/related-content/_section-macro.spec.js +20 -20
- package/components/relationships/_macro.spec.js +94 -94
- package/components/relationships/_relationships.scss +9 -9
- package/components/relationships/relationships.dom.js +5 -5
- package/components/relationships/relationships.js +18 -18
- package/components/relationships/relationships.spec.js +71 -71
- package/components/reply/_macro.spec.js +47 -47
- package/components/reply/reply-input.js +15 -15
- package/components/reply/reply.dom.js +5 -5
- package/components/reply/reply.spec.js +57 -57
- package/components/section-navigation/_macro.spec.js +210 -210
- package/components/section-navigation/_section-navigation.scss +76 -76
- package/components/select/_macro.spec.js +166 -166
- package/components/share-page/_macro.spec.js +68 -68
- package/components/skip-to-content/_macro.spec.js +54 -54
- package/components/skip-to-content/_skip.scss +30 -30
- package/components/skip-to-content/skip-to-content.dom.js +6 -6
- package/components/skip-to-content/skip-to-content.js +7 -7
- package/components/skip-to-content/skip-to-content.spec.js +21 -21
- package/components/status/_macro.spec.js +53 -53
- package/components/status/_status.scss +32 -32
- package/components/summary/_macro.spec.js +551 -535
- package/components/summary/_summary.scss +191 -195
- package/components/table/_macro.spec.js +499 -499
- package/components/table/_table.scss +204 -201
- package/components/table/scrollable-table.dom.js +5 -5
- package/components/table/scrollable-table.js +60 -60
- package/components/table/sortable-table.dom.js +5 -5
- package/components/table/sortable-table.js +135 -135
- package/components/table/table.spec.js +144 -140
- package/components/table-of-contents/_macro.spec.js +125 -125
- package/components/table-of-contents/_toc.scss +9 -9
- package/components/table-of-contents/toc.dom.js +5 -5
- package/components/table-of-contents/toc.js +30 -30
- package/components/table-of-contents/toc.spec.js +88 -88
- package/components/tabs/_macro.spec.js +92 -92
- package/components/tabs/_tabs.scss +120 -115
- package/components/tabs/tabs.dom.js +5 -5
- package/components/tabs/tabs.js +266 -266
- package/components/tabs/tabs.spec.js +268 -268
- package/components/text-indent/_macro.spec.js +33 -33
- package/components/text-indent/_text-indent.scss +3 -3
- package/components/textarea/_macro.spec.js +238 -238
- package/components/textarea/textarea.dom.js +5 -5
- package/components/textarea/textarea.spec.js +78 -74
- package/components/timeline/_macro.spec.js +83 -83
- package/components/timeline/_timeline.scss +26 -26
- package/components/timeout-modal/_macro.spec.js +47 -47
- package/components/timeout-modal/timeout-modal.dom.js +9 -9
- package/components/timeout-modal/timeout-modal.js +66 -66
- package/components/timeout-modal/timeout-modal.spec.js +157 -157
- package/components/timeout-panel/_macro.spec.js +41 -41
- package/components/timeout-panel/timeout-panel.dom.js +8 -8
- package/components/timeout-panel/timeout-panel.spec.js +118 -118
- package/components/upload/_macro.spec.js +52 -52
- package/components/upload/_upload.scss +28 -28
- package/components/video/_macro.spec.js +42 -42
- package/components/video/_video.scss +16 -16
- package/components/video/video.dom.js +5 -5
- package/components/video/video.js +32 -32
- package/components/video/video.spec.js +103 -97
- package/css/main.css +1 -1
- package/favicons/android-chrome-192x192.png +0 -0
- package/favicons/android-chrome-512x512.png +0 -0
- package/favicons/apple-touch-icon.png +0 -0
- package/favicons/favicon-16x16.png +0 -0
- package/favicons/favicon-32x32.png +0 -0
- package/favicons/favicon.ico +0 -0
- package/favicons/manifest.json +25 -25
- package/favicons/maskable_icon.png +0 -0
- package/favicons/opengraph.png +0 -0
- package/favicons/twitter.png +0 -0
- package/img/large/alison-pritchard-featured.jpg +0 -0
- package/img/large/alison-pritchard.jpg +0 -0
- package/img/large/census-monuments-lights-featured.jpg +0 -0
- package/img/large/census-monuments-lights.jpg +0 -0
- package/img/large/mother-and-daughter-in-ireland.jpg +0 -0
- package/img/large/mum-and-child-healthcare-belfast.jpg +0 -0
- package/img/large/ons-award-winners.jpg +0 -0
- package/img/large/placeholder-card.png +0 -0
- package/img/large/placeholder-news-medium.png +0 -0
- package/img/large/placeholder-news.png +0 -0
- package/img/large/placeholder-portrait.png +0 -0
- package/img/large/students.jpg +0 -0
- package/img/large/woman-in-purple-dress-shirt.jpg +0 -0
- package/img/small/alison-pritchard-featured.jpg +0 -0
- package/img/small/alison-pritchard.jpg +0 -0
- package/img/small/census-monuments-lights-featured.jpg +0 -0
- package/img/small/census-monuments-lights.jpg +0 -0
- package/img/small/mother-and-daughter-in-ireland.jpg +0 -0
- package/img/small/mum-and-child-healthcare-belfast.jpg +0 -0
- package/img/small/ons-award-winners.jpg +0 -0
- package/img/small/placeholder-card.png +0 -0
- package/img/small/placeholder-news-medium.png +0 -0
- package/img/small/placeholder-news.png +0 -0
- package/img/small/placeholder-portrait.png +0 -0
- package/img/small/students.jpg +0 -0
- package/img/small/woman-in-purple-dress-shirt.jpg +0 -0
- package/js/abortable-fetch.js +23 -23
- package/js/analytics.js +53 -53
- package/js/cookies-functions.js +135 -135
- package/js/cookies-settings.dom.js +7 -7
- package/js/cookies-settings.js +77 -77
- package/js/cookies-settings.spec.js +106 -106
- package/js/domready.js +8 -8
- package/js/fetch.js +14 -14
- package/js/inpagelink.dom.js +5 -5
- package/js/inpagelink.js +19 -19
- package/js/polyfills.js +0 -1
- package/js/print-button.js +6 -6
- package/js/timeout.js +211 -211
- package/layout/_template.njk +5 -7
- package/package.json +131 -127
- package/scripts/main.es5.js +1 -1
- package/scripts/main.js +1 -1
- package/scss/base/_forms.scss +10 -10
- package/scss/base/_global.scss +45 -44
- package/scss/base/_typography.scss +20 -20
- package/scss/helpers/_functions.scss +18 -15
- package/scss/helpers/_mixins.scss +59 -53
- package/scss/helpers/_mq.scss +62 -65
- package/scss/objects/_container.scss +20 -20
- package/scss/objects/_page.scss +33 -33
- package/scss/objects/_spacing.scss +10 -10
- package/scss/overrides/hcm.scss +237 -237
- package/scss/overrides/rtl.scss +95 -95
- package/scss/print.scss +47 -47
- package/scss/utilities/_border.scss +7 -7
- package/scss/utilities/_colors.scss +6 -6
- package/scss/utilities/_display.scss +8 -8
- package/scss/utilities/_float.scss +7 -7
- package/scss/utilities/_grid.scss +144 -144
- package/scss/utilities/_highlight.scss +4 -4
- package/scss/utilities/_margin.scss +17 -17
- package/scss/utilities/_pad.scss +15 -15
- package/scss/utilities/_typography.scss +35 -33
- package/scss/utilities/_utilities.scss +8 -8
- package/scss/utilities/_visibility.scss +25 -25
- package/scss/vars/_colors.scss +116 -116
- package/scss/vars/_forms.scss +22 -22
- package/scss/vars/_grid.scss +11 -9
- package/scss/vars/_typography.scss +54 -54
|
@@ -12,541 +12,544 @@ export const classAutosuggestHasResults = 'ons-autosuggest--has-results';
|
|
|
12
12
|
export const classAutosuggestResultsTitle = 'ons-autosuggest__results-title';
|
|
13
13
|
|
|
14
14
|
export default class AutosuggestUI {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// State
|
|
86
|
-
this.ctrlKey = false;
|
|
87
|
-
this.deleting = false;
|
|
88
|
-
this.query = '';
|
|
89
|
-
this.sanitisedQuery = '';
|
|
90
|
-
this.previousQuery = '';
|
|
91
|
-
this.results = [];
|
|
92
|
-
this.resultOptions = [];
|
|
93
|
-
this.allSelections = [];
|
|
94
|
-
this.data = [];
|
|
95
|
-
this.foundResults = 0;
|
|
96
|
-
this.numberOfResults = 0;
|
|
97
|
-
this.highlightedResultIndex = 0;
|
|
98
|
-
this.settingResult = false;
|
|
99
|
-
this.resultSelected = false;
|
|
100
|
-
this.blurring = false;
|
|
101
|
-
this.blurTimeout = null;
|
|
102
|
-
this.sanitisedQueryReplaceChars = sanitisedQueryReplaceChars || [];
|
|
103
|
-
this.sanitisedQuerySplitNumsChars = sanitisedQuerySplitNumsChars || false;
|
|
104
|
-
|
|
105
|
-
this.initialiseUI();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
get lang() {
|
|
109
|
-
return !this.language ? document.documentElement.getAttribute('lang').toLowerCase() : this.language.toLowerCase();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
initialiseUI() {
|
|
113
|
-
this.input.setAttribute('aria-autocomplete', 'list');
|
|
114
|
-
this.input.setAttribute('aria-controls', this.listbox.getAttribute('id'));
|
|
115
|
-
this.input.setAttribute('aria-describedby', this.instructions.getAttribute('id'));
|
|
116
|
-
this.input.setAttribute('aria-haspopup', true);
|
|
117
|
-
this.input.setAttribute('aria-owns', this.listbox.getAttribute('id'));
|
|
118
|
-
this.input.setAttribute('aria-expanded', false);
|
|
119
|
-
this.input.setAttribute('autocomplete', this.input.getAttribute('autocomplete') || 'off');
|
|
120
|
-
this.input.setAttribute('role', 'combobox');
|
|
121
|
-
|
|
122
|
-
this.context.classList.add('ons-autosuggest--initialised');
|
|
123
|
-
|
|
124
|
-
this.bindEventListeners();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
async fetchData() {
|
|
128
|
-
this.fetch = abortableFetch(this.autosuggestData);
|
|
129
|
-
const response = await this.fetch.send();
|
|
130
|
-
this.data = await response.json();
|
|
131
|
-
this.responseStatus = response.status;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
bindEventListeners() {
|
|
135
|
-
this.input.addEventListener('keydown', this.handleKeydown.bind(this));
|
|
136
|
-
this.input.addEventListener('keyup', this.handleKeyup.bind(this));
|
|
137
|
-
this.input.addEventListener('input', this.handleChange.bind(this));
|
|
138
|
-
this.input.addEventListener('focus', this.handleFocus.bind(this));
|
|
139
|
-
this.input.addEventListener('blur', this.handleBlur.bind(this));
|
|
140
|
-
|
|
141
|
-
this.listbox.addEventListener('mouseover', this.handleMouseover.bind(this));
|
|
142
|
-
this.listbox.addEventListener('mouseout', this.handleMouseout.bind(this));
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
handleKeydown(event) {
|
|
146
|
-
this.ctrlKey = (event.ctrlKey || event.metaKey) && event.key !== 'v';
|
|
147
|
-
|
|
148
|
-
switch (event.keyCode) {
|
|
149
|
-
case 38: {
|
|
150
|
-
event.preventDefault();
|
|
151
|
-
this.navigateResults(-1);
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
case 40: {
|
|
155
|
-
event.preventDefault();
|
|
156
|
-
this.navigateResults(1);
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
case 13: {
|
|
160
|
-
if (this.highlightedResultIndex !== null) {
|
|
161
|
-
event.preventDefault();
|
|
162
|
-
}
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
handleKeyup(event) {
|
|
169
|
-
switch (event.keyCode) {
|
|
170
|
-
case 40:
|
|
171
|
-
case 38: {
|
|
172
|
-
event.preventDefault();
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
case 13: {
|
|
176
|
-
if (this.highlightedResultIndex !== null) {
|
|
177
|
-
this.selectResult();
|
|
15
|
+
constructor({
|
|
16
|
+
context,
|
|
17
|
+
autosuggestData,
|
|
18
|
+
sanitisedQueryReplaceChars,
|
|
19
|
+
sanitisedQuerySplitNumsChars,
|
|
20
|
+
minChars,
|
|
21
|
+
resultLimit,
|
|
22
|
+
suggestOnBoot,
|
|
23
|
+
onSelect,
|
|
24
|
+
onUnsetResult,
|
|
25
|
+
suggestionFunction,
|
|
26
|
+
handleUpdate,
|
|
27
|
+
ariaYouHaveSelected,
|
|
28
|
+
ariaMinChars,
|
|
29
|
+
ariaOneResult,
|
|
30
|
+
ariaNResults,
|
|
31
|
+
ariaLimitedResults,
|
|
32
|
+
ariaGroupedResults,
|
|
33
|
+
moreResults,
|
|
34
|
+
resultsTitle,
|
|
35
|
+
noResults,
|
|
36
|
+
tooManyResults,
|
|
37
|
+
errorAPI,
|
|
38
|
+
errorAPILinkText,
|
|
39
|
+
typeMore,
|
|
40
|
+
}) {
|
|
41
|
+
// DOM Elements
|
|
42
|
+
this.context = context;
|
|
43
|
+
this.input = context.querySelector(`.${baseClass}-input`);
|
|
44
|
+
this.resultsContainer = context.querySelector(`.${baseClass}-results`);
|
|
45
|
+
this.listbox = this.resultsContainer.querySelector(`.${baseClass}-listbox`);
|
|
46
|
+
this.resultsTitleContainer = this.resultsContainer.querySelector(`.ons-autosuggest__results-title`);
|
|
47
|
+
this.instructions = context.querySelector(`.${baseClass}-instructions`);
|
|
48
|
+
this.ariaStatus = context.querySelector(`.${baseClass}-aria-status`);
|
|
49
|
+
this.form = context.closest('form');
|
|
50
|
+
this.label = document.querySelector('.ons-label');
|
|
51
|
+
|
|
52
|
+
// Settings
|
|
53
|
+
this.autosuggestData = autosuggestData || context.getAttribute('data-autosuggest-data');
|
|
54
|
+
this.ariaYouHaveSelected = ariaYouHaveSelected || context.getAttribute('data-aria-you-have-selected');
|
|
55
|
+
this.minChars = minChars || context.getAttribute('data-min-chars') || 3;
|
|
56
|
+
this.ariaMinChars = ariaMinChars || context.getAttribute('data-aria-min-chars');
|
|
57
|
+
this.ariaOneResult = ariaOneResult || context.getAttribute('data-aria-one-result');
|
|
58
|
+
this.ariaNResults = ariaNResults || context.getAttribute('data-aria-n-results');
|
|
59
|
+
this.ariaLimitedResults = ariaLimitedResults || context.getAttribute('data-aria-limited-results');
|
|
60
|
+
this.ariaGroupedResults = ariaGroupedResults || context.getAttribute('data-aria-grouped-results');
|
|
61
|
+
this.moreResults = moreResults || context.getAttribute('data-more-results');
|
|
62
|
+
this.resultsTitle = resultsTitle || context.getAttribute('data-results-title');
|
|
63
|
+
this.noResults = noResults || context.getAttribute('data-no-results');
|
|
64
|
+
this.tooManyResults = tooManyResults || context.getAttribute('data-too-many-results');
|
|
65
|
+
this.errorAPI = errorAPI || context.getAttribute('data-error-api');
|
|
66
|
+
this.errorAPILinkText = errorAPILinkText || context.getAttribute('data-error-api-link-text');
|
|
67
|
+
this.typeMore = typeMore || context.getAttribute('data-type-more');
|
|
68
|
+
this.language = context.getAttribute('data-lang');
|
|
69
|
+
this.allowMultiple = context.getAttribute('data-allow-multiple') || false;
|
|
70
|
+
this.listboxId = this.listbox.getAttribute('id');
|
|
71
|
+
this.resultLimit = resultLimit || 10;
|
|
72
|
+
this.suggestOnBoot = suggestOnBoot;
|
|
73
|
+
|
|
74
|
+
// Callbacks
|
|
75
|
+
this.onSelect = onSelect;
|
|
76
|
+
this.onUnsetResult = onUnsetResult;
|
|
77
|
+
this.handleUpdate = handleUpdate;
|
|
78
|
+
|
|
79
|
+
if (suggestionFunction) {
|
|
80
|
+
this.fetchSuggestions = suggestionFunction;
|
|
81
|
+
} else {
|
|
82
|
+
this.fetchData();
|
|
178
83
|
}
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
84
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
this.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
85
|
+
// State
|
|
86
|
+
this.ctrlKey = false;
|
|
87
|
+
this.deleting = false;
|
|
88
|
+
this.query = '';
|
|
89
|
+
this.sanitisedQuery = '';
|
|
90
|
+
this.previousQuery = '';
|
|
91
|
+
this.results = [];
|
|
92
|
+
this.resultOptions = [];
|
|
93
|
+
this.allSelections = [];
|
|
94
|
+
this.data = [];
|
|
95
|
+
this.foundResults = 0;
|
|
96
|
+
this.numberOfResults = 0;
|
|
97
|
+
this.highlightedResultIndex = 0;
|
|
98
|
+
this.settingResult = false;
|
|
99
|
+
this.resultSelected = false;
|
|
100
|
+
this.blurring = false;
|
|
101
|
+
this.blurTimeout = null;
|
|
102
|
+
this.sanitisedQueryReplaceChars = sanitisedQueryReplaceChars || [];
|
|
103
|
+
this.sanitisedQuerySplitNumsChars = sanitisedQuerySplitNumsChars || false;
|
|
104
|
+
|
|
105
|
+
this.initialiseUI();
|
|
197
106
|
}
|
|
198
|
-
}
|
|
199
107
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
this.input.value = `${this.input.value}, `;
|
|
108
|
+
get lang() {
|
|
109
|
+
return !this.language ? document.documentElement.getAttribute('lang').toLowerCase() : this.language.toLowerCase();
|
|
203
110
|
}
|
|
204
|
-
}
|
|
205
111
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
112
|
+
initialiseUI() {
|
|
113
|
+
this.input.setAttribute('aria-autocomplete', 'list');
|
|
114
|
+
this.input.setAttribute('aria-controls', this.listbox.getAttribute('id'));
|
|
115
|
+
this.input.setAttribute('aria-describedby', this.instructions.getAttribute('id'));
|
|
116
|
+
this.input.setAttribute('aria-haspopup', true);
|
|
117
|
+
this.input.setAttribute('aria-owns', this.listbox.getAttribute('id'));
|
|
118
|
+
this.input.setAttribute('aria-expanded', false);
|
|
119
|
+
this.input.setAttribute('autocomplete', this.input.getAttribute('autocomplete') || 'off');
|
|
120
|
+
this.input.setAttribute('role', 'combobox');
|
|
209
121
|
|
|
210
|
-
|
|
211
|
-
this.blurring = false;
|
|
212
|
-
if (!this.settingResult) {
|
|
213
|
-
this.clearListbox();
|
|
214
|
-
}
|
|
215
|
-
}, 300);
|
|
122
|
+
this.context.classList.add('ons-autosuggest--initialised');
|
|
216
123
|
|
|
217
|
-
|
|
218
|
-
this.input.value = this.input.value.slice(0, -2);
|
|
124
|
+
this.bindEventListeners();
|
|
219
125
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
this.
|
|
226
|
-
}, 2000);
|
|
227
|
-
} else {
|
|
228
|
-
clearTimeout(this.inputTimeout);
|
|
126
|
+
|
|
127
|
+
async fetchData() {
|
|
128
|
+
this.fetch = abortableFetch(this.autosuggestData);
|
|
129
|
+
const response = await this.fetch.send();
|
|
130
|
+
this.data = await response.json();
|
|
131
|
+
this.responseStatus = response.status;
|
|
229
132
|
}
|
|
230
|
-
}
|
|
231
133
|
|
|
232
|
-
|
|
233
|
-
|
|
134
|
+
bindEventListeners() {
|
|
135
|
+
this.input.addEventListener('keydown', this.handleKeydown.bind(this));
|
|
136
|
+
this.input.addEventListener('keyup', this.handleKeyup.bind(this));
|
|
137
|
+
this.input.addEventListener('input', this.handleChange.bind(this));
|
|
138
|
+
this.input.addEventListener('focus', this.handleFocus.bind(this));
|
|
139
|
+
this.input.addEventListener('blur', this.handleBlur.bind(this));
|
|
140
|
+
|
|
141
|
+
this.listbox.addEventListener('mouseover', this.handleMouseover.bind(this));
|
|
142
|
+
this.listbox.addEventListener('mouseout', this.handleMouseout.bind(this));
|
|
143
|
+
}
|
|
234
144
|
|
|
235
|
-
|
|
236
|
-
|
|
145
|
+
handleKeydown(event) {
|
|
146
|
+
this.ctrlKey = (event.ctrlKey || event.metaKey) && event.key !== 'v';
|
|
147
|
+
|
|
148
|
+
switch (event.keyCode) {
|
|
149
|
+
case 38: {
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
this.navigateResults(-1);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
case 40: {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
this.navigateResults(1);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
case 13: {
|
|
160
|
+
if (this.highlightedResultIndex !== null) {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
237
166
|
}
|
|
238
|
-
}
|
|
239
167
|
|
|
240
|
-
|
|
241
|
-
|
|
168
|
+
handleKeyup(event) {
|
|
169
|
+
switch (event.keyCode) {
|
|
170
|
+
case 40:
|
|
171
|
+
case 38: {
|
|
172
|
+
event.preventDefault();
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 13: {
|
|
176
|
+
if (this.highlightedResultIndex !== null) {
|
|
177
|
+
this.selectResult();
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
this.ctrlKey = false;
|
|
184
|
+
}
|
|
242
185
|
|
|
243
|
-
|
|
244
|
-
|
|
186
|
+
handleChange(groupResults = false) {
|
|
187
|
+
if ((!this.blurring && this.input.value.trim()) || groupResults === true) {
|
|
188
|
+
this.settingResult = false;
|
|
189
|
+
if (groupResults === true) {
|
|
190
|
+
this.getSuggestions(false, groupResults);
|
|
191
|
+
} else {
|
|
192
|
+
this.getSuggestions();
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
this.abortFetch();
|
|
196
|
+
this.clearListbox();
|
|
197
|
+
}
|
|
245
198
|
}
|
|
246
|
-
}
|
|
247
199
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
200
|
+
handleFocus() {
|
|
201
|
+
if (this.allowMultiple === 'true' && this.allSelections.length && this.input.value.slice(-1) !== ' ' && this.input.value !== '') {
|
|
202
|
+
this.input.value = `${this.input.value}, `;
|
|
203
|
+
}
|
|
252
204
|
}
|
|
253
205
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
206
|
+
handleBlur() {
|
|
207
|
+
clearTimeout(this.blurTimeout);
|
|
208
|
+
this.blurring = true;
|
|
209
|
+
|
|
210
|
+
this.blurTimeout = setTimeout(() => {
|
|
211
|
+
this.blurring = false;
|
|
212
|
+
if (!this.settingResult) {
|
|
213
|
+
this.clearListbox();
|
|
214
|
+
}
|
|
215
|
+
}, 300);
|
|
258
216
|
|
|
259
|
-
|
|
217
|
+
if (this.allowMultiple === 'true' && this.input.value.slice(-2) === ', ') {
|
|
218
|
+
this.input.value = this.input.value.slice(0, -2);
|
|
219
|
+
}
|
|
260
220
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
this.query = newQuery ? newQuery : this.input.value;
|
|
268
|
-
} else {
|
|
269
|
-
this.query = this.input.value;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const sanitisedQuery = sanitiseAutosuggestText(this.query, this.sanitisedQueryReplaceChars, this.sanitisedQuerySplitNumsChars);
|
|
273
|
-
|
|
274
|
-
if (sanitisedQuery !== this.sanitisedQuery || (force && !this.resultSelected)) {
|
|
275
|
-
this.sanitisedQuery = sanitisedQuery;
|
|
276
|
-
this.unsetResults();
|
|
277
|
-
this.checkCharCount();
|
|
278
|
-
if (this.sanitisedQuery.length >= this.minChars) {
|
|
279
|
-
this.fetchSuggestions(this.sanitisedQuery, this.data, groupResults)
|
|
280
|
-
.then(this.handleResults.bind(this))
|
|
281
|
-
.catch((error) => {
|
|
282
|
-
if (error.name !== 'AbortError') {
|
|
283
|
-
console.log('error:', error);
|
|
284
|
-
this.handleNoResults(500);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
221
|
+
|
|
222
|
+
checkCharCount() {
|
|
223
|
+
if (this.input.value.length > 1 && this.input.value.length < this.minChars) {
|
|
224
|
+
this.inputTimeout = setTimeout(() => {
|
|
225
|
+
this.handleNoResults(false);
|
|
226
|
+
}, 2000);
|
|
287
227
|
} else {
|
|
288
|
-
|
|
228
|
+
clearTimeout(this.inputTimeout);
|
|
289
229
|
}
|
|
290
|
-
}
|
|
291
230
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
});
|
|
300
|
-
return {
|
|
301
|
-
status: this.responseStatus,
|
|
302
|
-
results,
|
|
303
|
-
totalResults: results.length,
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
abortFetch() {
|
|
308
|
-
if (this.fetch && this.fetch.status !== 'DONE') {
|
|
309
|
-
this.fetch.abort();
|
|
231
|
+
|
|
232
|
+
handleMouseover() {
|
|
233
|
+
const focusedItem = this.resultOptions[this.highlightedResultIndex];
|
|
234
|
+
|
|
235
|
+
if (focusedItem) {
|
|
236
|
+
focusedItem.classList.remove(classAutosuggestOptionFocused);
|
|
237
|
+
}
|
|
310
238
|
}
|
|
311
|
-
}
|
|
312
239
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
this.resultOptions = [];
|
|
316
|
-
this.resultSelected = false;
|
|
240
|
+
handleMouseout() {
|
|
241
|
+
const focusedItem = this.resultOptions[this.highlightedResultIndex];
|
|
317
242
|
|
|
318
|
-
|
|
319
|
-
|
|
243
|
+
if (focusedItem) {
|
|
244
|
+
focusedItem.classList.add(classAutosuggestOptionFocused);
|
|
245
|
+
}
|
|
320
246
|
}
|
|
321
|
-
}
|
|
322
247
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
248
|
+
navigateResults(direction) {
|
|
249
|
+
let index = 0;
|
|
250
|
+
if (this.highlightedResultIndex !== null) {
|
|
251
|
+
index = this.highlightedResultIndex + direction;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (index < this.numberOfResults) {
|
|
255
|
+
if (index < 0) {
|
|
256
|
+
index = null;
|
|
257
|
+
}
|
|
328
258
|
|
|
329
|
-
|
|
330
|
-
|
|
259
|
+
this.setHighlightedResult(index);
|
|
260
|
+
}
|
|
331
261
|
}
|
|
332
|
-
}
|
|
333
262
|
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
263
|
+
getSuggestions(force, groupResults) {
|
|
264
|
+
if (!this.settingResult) {
|
|
265
|
+
if (this.allowMultiple === 'true' && this.allSelections.length) {
|
|
266
|
+
const newQuery = this.input.value.split(', ').find((item) => !this.allSelections.includes(item));
|
|
267
|
+
this.query = newQuery ? newQuery : this.input.value;
|
|
268
|
+
} else {
|
|
269
|
+
this.query = this.input.value;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const sanitisedQuery = sanitiseAutosuggestText(this.query, this.sanitisedQueryReplaceChars, this.sanitisedQuerySplitNumsChars);
|
|
273
|
+
|
|
274
|
+
if (sanitisedQuery !== this.sanitisedQuery || (force && !this.resultSelected)) {
|
|
275
|
+
this.sanitisedQuery = sanitisedQuery;
|
|
276
|
+
this.unsetResults();
|
|
277
|
+
this.checkCharCount();
|
|
278
|
+
if (this.sanitisedQuery.length >= this.minChars) {
|
|
279
|
+
this.fetchSuggestions(this.sanitisedQuery, this.data, groupResults)
|
|
280
|
+
.then(this.handleResults.bind(this))
|
|
281
|
+
.catch((error) => {
|
|
282
|
+
if (error.name !== 'AbortError') {
|
|
283
|
+
console.log('error:', error);
|
|
284
|
+
this.handleNoResults(500);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
} else {
|
|
288
|
+
this.clearListbox();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
339
292
|
}
|
|
340
293
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
if (this.results) {
|
|
347
|
-
this.resultOptions = this.results.map((result, index) => {
|
|
348
|
-
let innerHTML = this.emboldenMatch(result[this.lang], this.query);
|
|
349
|
-
|
|
350
|
-
const listElement = document.createElement('li');
|
|
351
|
-
listElement.className = classAutosuggestOption;
|
|
352
|
-
listElement.setAttribute('id', `${this.listboxId}__option--${index}`);
|
|
353
|
-
listElement.setAttribute('role', 'option');
|
|
354
|
-
if (result.category) {
|
|
355
|
-
innerHTML = innerHTML + `<span class="ons-autosuggest__category ons-u-lighter ons-u-fs-s ons-u-db">${result.category}</span>`;
|
|
356
|
-
}
|
|
357
|
-
listElement.innerHTML = innerHTML;
|
|
358
|
-
listElement.addEventListener('click', () => {
|
|
359
|
-
this.selectResult(index);
|
|
360
|
-
});
|
|
361
|
-
|
|
362
|
-
this.listbox.appendChild(listElement);
|
|
363
|
-
|
|
364
|
-
this.context.querySelector(`.${classAutosuggestResultsTitle}`).classList.remove('ons-u-d-no');
|
|
365
|
-
|
|
366
|
-
return listElement;
|
|
294
|
+
async fetchSuggestions(sanitisedQuery, data) {
|
|
295
|
+
this.abortFetch();
|
|
296
|
+
const results = await runFuse(sanitisedQuery, data, this.lang, this.resultLimit);
|
|
297
|
+
results.forEach((result) => {
|
|
298
|
+
result.sanitisedText = sanitiseAutosuggestText(result[this.lang], this.sanitisedQueryReplaceChars);
|
|
367
299
|
});
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
listElement.innerHTML = this.moreResults;
|
|
375
|
-
this.listbox.appendChild(listElement);
|
|
376
|
-
}
|
|
300
|
+
return {
|
|
301
|
+
status: this.responseStatus,
|
|
302
|
+
results,
|
|
303
|
+
totalResults: results.length,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
377
306
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
this.resultsTitleContainer.remove();
|
|
384
|
-
}
|
|
307
|
+
abortFetch() {
|
|
308
|
+
if (this.fetch && this.fetch.status !== 'DONE') {
|
|
309
|
+
this.fetch.abort();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
385
312
|
|
|
386
|
-
|
|
313
|
+
unsetResults() {
|
|
314
|
+
this.results = [];
|
|
315
|
+
this.resultOptions = [];
|
|
316
|
+
this.resultSelected = false;
|
|
387
317
|
|
|
388
|
-
|
|
318
|
+
if (this.onUnsetResult) {
|
|
319
|
+
this.onUnsetResult();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
389
322
|
|
|
390
|
-
|
|
391
|
-
this.
|
|
392
|
-
} else {
|
|
323
|
+
clearListbox(preventAriaStatusUpdate) {
|
|
324
|
+
this.listbox.innerHTML = '';
|
|
393
325
|
this.context.classList.remove(classAutosuggestHasResults);
|
|
394
|
-
this.
|
|
395
|
-
|
|
396
|
-
}
|
|
326
|
+
this.input.removeAttribute('aria-activedescendant');
|
|
327
|
+
this.input.setAttribute('aria-expanded', false);
|
|
397
328
|
|
|
398
|
-
|
|
399
|
-
|
|
329
|
+
if (!preventAriaStatusUpdate) {
|
|
330
|
+
this.setAriaStatus();
|
|
331
|
+
}
|
|
400
332
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
333
|
+
|
|
334
|
+
handleResults(result) {
|
|
335
|
+
this.resultLimit = result.limit ? result.limit : this.resultLimit;
|
|
336
|
+
this.foundResults = result.totalResults;
|
|
337
|
+
if (this.foundResults > this.resultLimit) {
|
|
338
|
+
result.results = result.results.slice(0, this.resultLimit);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
this.results = result.results;
|
|
342
|
+
this.numberOfResults = this.results ? Math.max(this.results.length, 0) : 0;
|
|
343
|
+
this.setAriaStatus();
|
|
344
|
+
if (!this.deleting || (this.numberOfResults && this.deleting)) {
|
|
345
|
+
this.listbox.innerHTML = '';
|
|
346
|
+
if (this.results) {
|
|
347
|
+
this.resultOptions = this.results.map((result, index) => {
|
|
348
|
+
let innerHTML = this.emboldenMatch(result[this.lang], this.query);
|
|
349
|
+
|
|
350
|
+
const listElement = document.createElement('li');
|
|
351
|
+
listElement.className = classAutosuggestOption;
|
|
352
|
+
listElement.setAttribute('id', `${this.listboxId}__option--${index}`);
|
|
353
|
+
listElement.setAttribute('role', 'option');
|
|
354
|
+
if (result.category) {
|
|
355
|
+
innerHTML =
|
|
356
|
+
innerHTML +
|
|
357
|
+
`<span class="ons-autosuggest__category ons-u-lighter ons-u-fs-s ons-u-db">${result.category}</span>`;
|
|
358
|
+
}
|
|
359
|
+
listElement.innerHTML = innerHTML;
|
|
360
|
+
listElement.addEventListener('click', () => {
|
|
361
|
+
this.selectResult(index);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
this.listbox.appendChild(listElement);
|
|
365
|
+
|
|
366
|
+
this.context.querySelector(`.${classAutosuggestResultsTitle}`).classList.remove('ons-u-d-no');
|
|
367
|
+
|
|
368
|
+
return listElement;
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (this.numberOfResults < this.foundResults) {
|
|
373
|
+
const listElement = document.createElement('li');
|
|
374
|
+
listElement.className = `${classAutosuggestOption} ${classAutosuggestOptionMoreResults}`;
|
|
375
|
+
listElement.setAttribute('aria-hidden', 'true');
|
|
376
|
+
listElement.innerHTML = this.moreResults;
|
|
377
|
+
this.listbox.appendChild(listElement);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (this.resultLimit === 100 && this.foundResults > this.resultLimit) {
|
|
381
|
+
let message = this.tooManyResults.replace('{n}', this.foundResults);
|
|
382
|
+
this.resultsContainer.insertBefore(this.createWarningElement(message), this.resultsContainer.firstChild);
|
|
383
|
+
this.ariaStatus.setAttribute('aria-hidden', 'true');
|
|
384
|
+
this.listbox.remove();
|
|
385
|
+
this.resultsTitleContainer.remove();
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
this.setHighlightedResult(null);
|
|
389
|
+
|
|
390
|
+
this.input.setAttribute('aria-expanded', !!this.numberOfResults);
|
|
391
|
+
|
|
392
|
+
if (!!this.numberOfResults && this.sanitisedQuery.length >= this.minChars) {
|
|
393
|
+
this.context.classList.add(classAutosuggestHasResults);
|
|
394
|
+
} else {
|
|
395
|
+
this.context.classList.remove(classAutosuggestHasResults);
|
|
396
|
+
this.clearListbox();
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (this.numberOfResults === 0 && this.noResults) {
|
|
401
|
+
this.handleNoResults(result.status);
|
|
402
|
+
}
|
|
429
403
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
this.
|
|
449
|
-
|
|
450
|
-
this.
|
|
451
|
-
|
|
404
|
+
|
|
405
|
+
handleNoResults(status) {
|
|
406
|
+
let message;
|
|
407
|
+
this.context.classList.add(classAutosuggestHasResults);
|
|
408
|
+
this.context.querySelector(`.${classAutosuggestResultsTitle}`).classList.add('ons-u-d-no');
|
|
409
|
+
this.input.setAttribute('aria-expanded', true);
|
|
410
|
+
|
|
411
|
+
if (status === 400 || status === false) {
|
|
412
|
+
message = this.typeMore;
|
|
413
|
+
this.setAriaStatus(message);
|
|
414
|
+
this.listbox.innerHTML = `<li class="${classAutosuggestOption} ${classAutosuggestOptionNoResults}">${message}</li>`;
|
|
415
|
+
} else if (status > 400 || status === '') {
|
|
416
|
+
message =
|
|
417
|
+
this.errorAPI + (this.errorAPILinkText ? ' <a href="' + window.location.href + '">' + this.errorAPILinkText + '</a>.' : '');
|
|
418
|
+
let ariaMessage = this.errorAPI + (this.errorAPILinkText ? ' ' + this.errorAPILinkText : '');
|
|
419
|
+
|
|
420
|
+
this.input.setAttribute('disabled', true);
|
|
421
|
+
this.input.value = '';
|
|
422
|
+
this.label.classList.add('ons-u-lighter');
|
|
423
|
+
|
|
424
|
+
this.resultsContainer.insertBefore(this.createWarningElement(message), this.resultsContainer.firstChild);
|
|
425
|
+
this.ariaStatus.setAttribute('aria-hidden', 'true');
|
|
426
|
+
this.setAriaStatus(ariaMessage);
|
|
427
|
+
this.listbox.remove();
|
|
428
|
+
this.resultsTitleContainer.remove();
|
|
452
429
|
} else {
|
|
453
|
-
|
|
454
|
-
|
|
430
|
+
message = this.noResults;
|
|
431
|
+
this.listbox.innerHTML = `<li class="${classAutosuggestOption} ${classAutosuggestOptionNoResults}">${message}</li>`;
|
|
455
432
|
}
|
|
456
|
-
});
|
|
457
433
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
434
|
+
|
|
435
|
+
setHighlightedResult(index) {
|
|
436
|
+
this.highlightedResultIndex = index;
|
|
437
|
+
|
|
438
|
+
if (this.highlightedResultIndex === null) {
|
|
439
|
+
this.input.removeAttribute('aria-activedescendant');
|
|
440
|
+
} else if (this.numberOfResults) {
|
|
441
|
+
this.resultOptions.forEach((option, optionIndex) => {
|
|
442
|
+
if (optionIndex === index) {
|
|
443
|
+
option.classList.add(classAutosuggestOptionFocused);
|
|
444
|
+
option.setAttribute('aria-selected', true);
|
|
445
|
+
this.input.setAttribute('aria-activedescendant', option.getAttribute('id'));
|
|
446
|
+
const groupedResult = option.querySelector('.ons-autosuggest__group');
|
|
447
|
+
const optionText = option.innerHTML.replace('<strong>', '').replace('</strong>', '');
|
|
448
|
+
if (groupedResult) {
|
|
449
|
+
let groupedAriaMsg = this.ariaGroupedResults.replace('{n}', groupedResult.innerHTML);
|
|
450
|
+
groupedAriaMsg = groupedAriaMsg.replace('{x}', optionText);
|
|
451
|
+
this.setAriaStatus(groupedAriaMsg);
|
|
452
|
+
} else {
|
|
453
|
+
this.setAriaStatus(optionText);
|
|
454
|
+
}
|
|
455
|
+
} else {
|
|
456
|
+
option.classList.remove(classAutosuggestOptionFocused);
|
|
457
|
+
option.removeAttribute('aria-selected');
|
|
458
|
+
}
|
|
459
|
+
});
|
|
475
460
|
}
|
|
476
|
-
}
|
|
477
461
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
const ariaMessage = `${this.ariaYouHaveSelected}: ${result.displayText}.`;
|
|
499
|
-
|
|
500
|
-
this.clearListbox();
|
|
501
|
-
this.setAriaStatus(ariaMessage);
|
|
462
|
+
|
|
463
|
+
setAriaStatus(content) {
|
|
464
|
+
if (!content) {
|
|
465
|
+
const queryTooShort = this.sanitisedQuery.length < this.minChars;
|
|
466
|
+
const noResults = this.numberOfResults === 0;
|
|
467
|
+
if (queryTooShort) {
|
|
468
|
+
content = this.ariaMinChars;
|
|
469
|
+
} else if (noResults) {
|
|
470
|
+
content = `${this.noResults}: "${this.query}"`;
|
|
471
|
+
} else if (this.numberOfResults === 1) {
|
|
472
|
+
content = this.ariaOneResult;
|
|
473
|
+
} else {
|
|
474
|
+
content = this.ariaNResults.replace('{n}', this.numberOfResults);
|
|
475
|
+
|
|
476
|
+
if (this.resultLimit && this.foundResults > this.resultLimit) {
|
|
477
|
+
content += ` ${this.ariaLimitedResults}`;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
this.ariaStatus.innerHTML = content;
|
|
502
482
|
}
|
|
503
|
-
}
|
|
504
483
|
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
484
|
+
selectResult(index) {
|
|
485
|
+
if (this.results.length) {
|
|
486
|
+
this.settingResult = true;
|
|
487
|
+
const result = this.results[index || this.highlightedResultIndex || 0];
|
|
488
|
+
this.resultSelected = true;
|
|
489
|
+
|
|
490
|
+
if (this.allowMultiple === 'true') {
|
|
491
|
+
let value = this.storeExistingSelections(result[this.lang]);
|
|
492
|
+
result.displayText = value;
|
|
493
|
+
} else if (result.url) {
|
|
494
|
+
result.displayText = result[this.lang];
|
|
495
|
+
window.location = result.url;
|
|
496
|
+
} else {
|
|
497
|
+
result.displayText = result[this.lang];
|
|
498
|
+
}
|
|
499
|
+
this.onSelect(result).then(() => (this.settingResult = false));
|
|
500
|
+
|
|
501
|
+
const ariaMessage = `${this.ariaYouHaveSelected}: ${result.displayText}.`;
|
|
502
|
+
|
|
503
|
+
this.clearListbox();
|
|
504
|
+
this.setAriaStatus(ariaMessage);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
510
507
|
|
|
511
|
-
|
|
512
|
-
|
|
508
|
+
createWarningElement(content) {
|
|
509
|
+
const warningContainer = document.createElement('div');
|
|
510
|
+
const warningElement = document.createElement('div');
|
|
511
|
+
const warningSpanElement = document.createElement('span');
|
|
512
|
+
const warningBodyElement = document.createElement('div');
|
|
513
513
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
warningSpanElement.innerHTML = '!';
|
|
514
|
+
warningContainer.className = 'ons-autosuggest__warning';
|
|
515
|
+
warningElement.className = 'ons-panel ons-panel--warn ons-autosuggest__panel';
|
|
517
516
|
|
|
518
|
-
|
|
519
|
-
|
|
517
|
+
warningSpanElement.className = 'ons-panel__icon';
|
|
518
|
+
warningSpanElement.setAttribute('aria-hidden', 'true');
|
|
519
|
+
warningSpanElement.innerHTML = '!';
|
|
520
520
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
warningContainer.appendChild(warningElement);
|
|
521
|
+
warningBodyElement.className = 'ons-panel__body';
|
|
522
|
+
warningBodyElement.innerHTML = content;
|
|
524
523
|
|
|
525
|
-
|
|
526
|
-
|
|
524
|
+
warningElement.appendChild(warningSpanElement);
|
|
525
|
+
warningElement.appendChild(warningBodyElement);
|
|
526
|
+
warningContainer.appendChild(warningElement);
|
|
527
527
|
|
|
528
|
-
|
|
529
|
-
this.currentSelections = this.input.value.split(', ').filter((items) => this.allSelections.includes(items));
|
|
530
|
-
this.allSelections = [];
|
|
531
|
-
if (this.currentSelections.length) {
|
|
532
|
-
this.allSelections = this.currentSelections;
|
|
528
|
+
return warningContainer;
|
|
533
529
|
}
|
|
534
|
-
this.allSelections.push(value);
|
|
535
530
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
531
|
+
storeExistingSelections(value) {
|
|
532
|
+
this.currentSelections = this.input.value.split(', ').filter((items) => this.allSelections.includes(items));
|
|
533
|
+
this.allSelections = [];
|
|
534
|
+
if (this.currentSelections.length) {
|
|
535
|
+
this.allSelections = this.currentSelections;
|
|
536
|
+
}
|
|
537
|
+
this.allSelections.push(value);
|
|
539
538
|
|
|
540
|
-
|
|
541
|
-
|
|
539
|
+
this.allSelections = this.allSelections.filter(function (value, index, array) {
|
|
540
|
+
return array.indexOf(value) == index;
|
|
541
|
+
});
|
|
542
542
|
|
|
543
|
-
|
|
544
|
-
|
|
543
|
+
return this.allSelections.join(', ');
|
|
544
|
+
}
|
|
545
545
|
|
|
546
|
-
|
|
547
|
-
|
|
546
|
+
emboldenMatch(string, query) {
|
|
547
|
+
let reg = new RegExp(this.escapeRegExp(query).split(' ').join('[\\s,]*'), 'gi');
|
|
548
548
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
549
|
+
return string.replace(reg, '<strong>$&</strong>');
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
escapeRegExp(string) {
|
|
553
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
554
|
+
}
|
|
552
555
|
}
|