forked from observablehq/inputs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.js
45 lines (41 loc) · 1.41 KB
/
bind.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
import {disposal} from "./disposal.js";
import {bubbles} from "./event.js";
export function bind(target, source, invalidation = disposal(target)) {
const sourceEvent = eventof(source);
const onsource = () => set(target, source);
const ontarget = () => (set(source, target), source.dispatchEvent(new Event(sourceEvent, bubbles)));
onsource();
target.addEventListener(eventof(target), ontarget);
source.addEventListener(sourceEvent, onsource);
invalidation.then(() => source.removeEventListener(sourceEvent, onsource));
return target;
}
function get(input) {
switch (input.type) {
case "range":
case "number": return input.valueAsNumber;
case "date": return input.valueAsDate;
case "checkbox": return input.checked;
case "file": return input.multiple ? input.files : input.files[0];
default: return input.value;
}
}
function set(target, source) {
const value = get(source);
switch (target.type) {
case "range":
case "number": target.valueAsNumber = value; break;
case "date": target.valueAsDate = value; break;
case "checkbox": target.checked = value; break;
case "file": target.multiple ? (target.files = value) : (target.files = [value]); break;
default: target.value = value; break;
}
}
function eventof(input) {
switch (input.type) {
case "button":
case "submit": return "click";
case "file": return "change";
default: return "input";
}
}