UNPKG

883 BJavaScriptView Raw
1import { assign } from "d3-let";
2import debounce from "../../utils/debounce";
3import sel from "../../utils/sel";
4
5const base = {
6 on(model, attrName) {
7 var refresh = refreshFunction(this, model, attrName),
8 uid = model.uid;
9
10 // DOM => model binding
11 this.sel.on(`input.${uid}`, refresh).on(`change.${uid}`, refresh);
12 },
13
14 off(model) {
15 var uid = model.uid;
16 this.sel.on(`input.${uid}`, null).on(`change.${uid}`, null);
17 },
18
19 value(value) {
20 if (arguments.length) this.sel.property("value", value);
21 else return this.sel.property("value");
22 }
23};
24
25export function createValueType(proto) {
26 function ValueType(el) {
27 sel(this).el = el;
28 }
29
30 ValueType.prototype = assign({}, base, proto);
31
32 return ValueType;
33}
34
35export function refreshFunction(vType, model, attrName) {
36 return debounce(() => {
37 model.$set(attrName, vType.value());
38 });
39}