UNPKG

1.2 kBJavaScriptView Raw
1import { isArray } from "d3-let";
2import properties from "../utils/htmlprops";
3
4//
5// d3-attr-<attr> directive
6// ==============================
7//
8// Create a one-way binding between a model and a HTML element attribute
9//
10export default {
11 create(expression) {
12 if (!this.arg)
13 return this.logWarn(
14 "Cannot bind to empty attribute. Specify :<attr-name>"
15 );
16 return expression;
17 },
18
19 refresh(model, value) {
20 if (this.arg === "class") return this.refreshClass(value);
21 if (isArray(value))
22 return this.logWarn(`Cannot apply array to attribute ${this.arg}`);
23 var prop = properties.get(this.arg);
24 if (prop) this.sel.property(prop, value || false);
25 else this.sel.attr(this.arg, value || null);
26 },
27
28 refreshClass(value) {
29 var sel = this.sel;
30
31 if (!isArray(value)) value = [value];
32
33 if (this.oldValue)
34 this.oldValue.forEach(entry => {
35 if (entry) sel.classed(entry, false);
36 });
37
38 this.oldValue = value.map(entry => {
39 var exist = true;
40 if (isArray(entry)) {
41 exist = entry[1];
42 entry = entry[0];
43 }
44 if (entry) sel.classed(entry, exist);
45 return entry;
46 });
47 }
48};