-
Notifications
You must be signed in to change notification settings - Fork 0
/
Element.svelte
263 lines (224 loc) · 7.71 KB
/
Element.svelte
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<script context="module">
import deepEqual from '@wareset-utilites/deep-equal';
import _unique from '@wareset-utilites/unique';
import store from '@wareset/store';
const not_equal = (a, b) => !deepEqual(a, b, 0);
const unique = _unique.bind([null, undefined]);
// const unique = _unique.bind([null, undefined]);
import {
classes_normalize,
styles_normalize,
attributes_normalize,
events_factory,
actions,
is_void,
set_if_isset,
data_option,
svelte_set,
rand_ID
} from 'svelte-plugin-element/util';
let ID = 0;
</script>
<script>
import { onMount, tick } from 'svelte';
import { get_current_component } from 'svelte/internal';
import { select_options, select_multiple_value } from 'svelte/internal';
if (ID >= Number.MAX_SAFE_INTEGER - 1) ID = 0;
const __ID__ = `element-${++ID}`;
const component = get_current_component();
const events = events_factory(component);
const CACHE = {};
export let tag = 'div';
$: tag = tag.toLowerCase();
let __TAG__ = tag;
let __CLASS__ = '';
export { __CLASS__ as class };
let __STYLE__ = '';
export { __STYLE__ as style };
let __USE__ = [];
export { __USE__ as use };
let __ON__ = [];
export { __ON__ as on };
let __ATTRIBUTES__ = {};
export { __ATTRIBUTES__ as attributes };
export let element = undefined;
export let id = undefined;
export let type = 'text';
$: type = type.toLowerCase();
export let value = id || __ID__;
export let group = [];
export let checked = null;
export let indeterminate = null;
$: if (element) element.__value = value;
const element$ = store(element);
const value$ = store(value);
const group$ = store(group);
const checked$ = store(checked);
const indeterminate$ = store(indeterminate);
let _value, _group, _checked, _indeterminate, _element;
// FORMFIELDS
function onInput(e) {
switch (tag) {
case 'select':
(group.length = 0), group.push(...select_multiple_value(element));
group$.set((_group = unique(group)));
break;
case 'input':
if (type === 'checkbox' || type === 'radio') {
checked = !!element.checked;
}
default:
if ('value' in element) value = element.value || '';
}
// console.log([value, checked, indeterminate], [...group]);
}
const change_element = v => not_equal($element$, v) && element$.set(v);
$: change_element(element);
const change_value = v => not_equal($value$, v) && value$.set(v);
$: change_value(value);
const change_group = v => not_equal($group$, v) && group$.set(v);
$: change_group(group);
const change_checked = v => not_equal($checked$, v) && checked$.set(v);
$: change_checked(checked);
const change_indeterminate = v =>
not_equal($indeterminate$, v) && indeterminate$.set(v);
$: change_indeterminate(indeterminate);
onMount(() => {
const needUpdate$ = store(false, [
value$,
group$,
checked$,
indeterminate$,
element$
]);
let is_init = false;
let update_symbol = {};
function update() {
if (not_equal(value, value$.$)) return (value = value$.$);
if (not_equal(group, group$.$)) return (group = group$.$);
if (not_equal(checked, checked$.$)) return (checked = checked$.$);
if (not_equal(indeterminate, indeterminate$.$)) {
return (indeterminate = indeterminate$.$);
}
(_value = value), (_group = group);
(_checked = checked), (_indeterminate = indeterminate);
return update_symbol;
}
function need_update(v, [value, group, checked, indeterminate, element]) {
// console.log(value, group, checked, indeterminate, is_init);
if (not_equal(_element, element)) (_element = element), (is_init = false);
if (!element) return;
if (typeof indeterminate !== 'boolean') {
indeterminate$.set((_indeterminate = !!indeterminate));
return;
}
if (typeof checked !== 'boolean' && checked !== null) {
checked$.set((_checked = !!checked));
return;
}
if (!Array.isArray(group)) {
group$.set((_group = unique([group])));
return;
}
if (is_void(value)) {
value$.set((_value = ''));
return;
}
const index = group.indexOf(value);
if (!is_init) {
if (typeof checked === 'boolean' && !checked !== !~index) {
checked ? group.push(value) : group.splice(index, 1);
group$.set((_group = unique(group)));
return;
}
if (indeterminate && (!is_void(checked) || ~index)) {
if (!is_void(checked)) checked$.set((_checked = null));
else group.splice(index, 1), group$.set((_group = unique(group)));
return;
}
if (checked && (indeterminate || !~index)) {
if (indeterminate) indeterminate$.set((_indeterminate = false));
else group.push(value), group$.set((_group = unique(group)));
return;
}
if (~index && (indeterminate || !checked)) {
if (indeterminate) indeterminate$.set((_indeterminate = false));
else checked$.set((_checked = true));
return;
}
(_value = value), (_group = group);
(_checked = checked), (_indeterminate = indeterminate);
} else {
if (not_equal(_value, value)) {
const old_index = group.indexOf(_value);
_value = value;
if (~old_index && checked) {
group.splice(old_index, 1, value);
group$.set((_group = unique(group)));
return;
}
}
if (not_equal(_group, group)) {
_group = group;
if (element && element.options) select_options(element, group);
if (!checked !== !~index) {
checked$.set((_checked = !!~index));
return;
}
}
if (not_equal(_checked, checked)) {
_checked = checked;
// console.log(1111, group);
if (!checked !== !~index) {
let i = index;
if (type === 'radio') (group.length = 0), (i = 0);
checked ? group.push(value) : group.splice(i, 1);
group$.set((_group = unique(group)));
// console.log(_group);
return;
}
}
if (not_equal(_indeterminate, indeterminate)) {
_indeterminate = indeterminate;
if (indeterminate)
!is_void(checked) && checked$.set((_checked = null));
if (!indeterminate)
is_void(checked) && checked$.set((_checked = false));
return;
}
}
if (update() === update_symbol) is_init = true;
else needUpdate$.setSure(true);
}
const unsubscribes = [needUpdate$.subscribe(need_update)];
return () => unsubscribes.forEach(v => v());
});
$: if (element) set_if_isset(element, 'type', type);
$: if ((element && tag === 'input') || tag === 'option' || tag === 'textarea')
set_if_isset(element, 'value', value);
$: if (element && (type === 'radio' || type === 'checkbox'))
set_if_isset(element, 'checked', checked);
$: if (element && type === 'checkbox')
set_if_isset(element, 'indeterminate', indeterminate);
</script>
<svelte:options immutable={true} />
{#if __TAG__ && __TAG__ === tag}
<sveltepluginelement
{id}
class={classes_normalize.bind([
element,
CACHE
])(__CLASS__, __ATTRIBUTES__.class) || null}
style={styles_normalize.bind([
element,
CACHE
])(__STYLE__, __ATTRIBUTES__.style) || null}
bind:this={element}
use:events={__ON__}
use:actions={[__USE__, __ATTRIBUTES__.use]}
{...attributes_normalize.bind([element, CACHE, component])(__ATTRIBUTES__)}
on:input={onInput}
>
<slot />
</sveltepluginelement>
{:else if (__TAG__ = tag)}{''}{/if}