UNPKG

1.12 kBJavaScriptView Raw
1import types from "./types/index";
2
3//
4// d3-value directive
5// ===================
6//
7// Two-way data binding for HTML elements supporting the value property
8export default {
9 create(expression) {
10 var type = this.sel.attr("type"),
11 tag = this.el.tagName.toLowerCase(),
12 ValueType = types[type] || types[tag];
13
14 if (!ValueType) {
15 this.logWarn(`Cannot apply d3-value directive to ${tag}`);
16 return;
17 }
18 this.tag = new ValueType(this.el);
19 return expression;
20 },
21
22 mount(model) {
23 var expr = this.expression;
24 // TODO: relax this constraint
25 if (expr.parsed.type !== expr.codes.IDENTIFIER) {
26 this.logWarn(
27 `support identifiers only, got "${expr.parsed.type}": ${
28 this.expression
29 }`
30 );
31 return;
32 }
33 var attrName = this.expression.expr;
34 //
35 // Create the model reactive attribute
36 model.$set(attrName, this.tag.value());
37 // register dom event
38 this.tag.on(model, attrName);
39 return model;
40 },
41
42 refresh(model, value) {
43 this.tag.value(value);
44 },
45
46 destroy(model) {
47 this.tag.off(model);
48 }
49};