-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_svelte.js
162 lines (140 loc) · 4.69 KB
/
select_svelte.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint no-console: 0 */
// Run this example by adding <%= javascript_pack_tag 'hello_svelte' %> (and
// <%= stylesheet_pack_tag 'hello_svelte' %> if you have styles in your component)
// to the head of your layout file,
// like app/views/layouts/application.html.erb.
// All it does is render <div>Hello Svelte!</div> at the bottom of the page.
import Select from '@kikonen/select_svelte/src/select.svelte';
function setupSelect() {
document.querySelectorAll('.js-svelte-select').forEach(function(input) {
let ds = input.dataset;
let fetch_options = {
fetch_url: ds.kiFetchUrl,
fetch_limit: ds.kiFetchLimit,
fetch_query_min_len: parseInt(ds.kiFetchQueryMinLen || 0, 10),
};
function fetcherRest(offset, query, fetchId) {
console.log("fetchId=" + fetchId);
let token = document.head.querySelector('meta[name="csrf-token"]').content;
return fetch(
fetch_options.fetch_url,
{ method: 'POST',
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Requested-With': 'XMLHTTPRequest',
'X-CSRF-Token': token,
}),
credentials: 'same-origin',
body: JSON.stringify({
fetch_offset: offset,
fetch_limit: fetch_options.fetch_limit,
fetch_query: query,
fetch_id: fetchId})
}).then(function(xhr) {
console.log(xhr);
return xhr.json();
});
}
function handleAction(event) {
setTimeout(function() {
let item = event.detail;
let el = document.activeElement;
console.log("WAS_ACTIVE", document.activeElement);
alert("action: " + item.action);
if (el !== document.activeElement) {
console.log("NOW_ACTIVE", document.activeElement);
el.focus();
}
});
}
function handleItemClick(event) {
setTimeout(function() {
let item = event.detail;
let el = document.activeElement;
alert("click: " + item.id + " - " + item.text);
if (el) {
el.focus();
}
});
}
function handleSelect(event) {
console.log("SELECTED", event.detail);
let selectedOption2 = input.querySelector('option[selected]');
console.log("SELECT_VIA_SELECTOR", selectedOption2);
}
function handleChange(event) {
console.log("CHANGE", event);
let check = document.querySelector('#select_brand_sync');
if (!check || !check.checked) {
return;
}
let selection = {};
let selectedOptions = input.selectedOptions;
for (let i = selectedOptions.length - 1; i >= 0; i--) {
let el = selectedOptions[i];
let styleId = el.value ? 'style_' + el.value : '';
selection[styleId] = true;
}
let target = document.querySelector('#select_style');
let targetOptions = target.options;
for (let i = targetOptions.length - 1; i >= 0; i--) {
let el = targetOptions[i];
el.selected = selection[el.value];
}
target.dispatchEvent(new Event('change'));
}
if (input.id === 'select_brand') {
input.addEventListener('change', handleChange);
}
// input.addEventListener('select-select', handleSelect);
input.addEventListener('select-action', handleAction);
input.addEventListener('select-click', handleItemClick);
let fetcher = null;
if (fetch_options.fetch_url) {
fetcher = fetcherRest;
}
let styles = {};
if (input.id === 'select_style') {
styles = {
container_class: 'border-danger',
};
}
const translations = {
clear: 'Tyhjennä',
no_results: 'Ei tuloksia',
max_limit: 'Maksimi',
selected_count: 'valittua',
selected_more: 'more',
};
const app = new Select({
target: input.parentElement,
props: {
real: input,
config: {
debugMode: false,
fetcher: fetcher,
remote: fetcher !== null,
placeholder: ds.kiPlaceholder,
typeahead: ds.kiTypeahead !== undefined ? true : false,
maxItems: parseInt(ds.kiMaxItems || 100, 0),
summaryLen: parseInt(ds.kiSummaryLen || 2, 0),
summaryWrap: ds.kiSummaryWrap !== undefined ? true : false,
noCache: ds.kiNoCache !== undefined ? true : false,
translations: translations,
styles: styles,
baseHref: ds.kiBaseHref,
},
}
});
if (input.id === 'select_brand') {
app.selectItem(3);
}
if (false && input.id === 'select_rest_1') {
app.selectItem(1000);
}
});
}
document.addEventListener('DOMContentLoaded', () => {
setupSelect();
});