This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmca.js
125 lines (116 loc) · 2.46 KB
/
mca.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
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
/*globals m, document, Alt, _ */
var alt = new Alt();
const actions = alt.generateActions("addItem", "checkItem", "saveDescr");
class TodoStore {
constructor() {
this.list = [];
this.descr = "";
this.bindActions(actions);
}
onAddItem(descr) {
this.descr = "";
this.list.push({
descr,
done: false
});
}
onCheckItem(data) {
this.list[data.index].done = data.done;
}
onSaveDescr(descr) {
this.descr = descr;
}
}
let todoStore = alt.createStore(TodoStore);
class DataContainer {
constructor(...args) {
this._args = args;
let stores = this.getStores();
if (_.isPlainObject(stores)) {
_.each(stores, (store, key) => {
this[key] = store.getState();
store.listen(state => {
_.merge(this[key], state);
this.render();
});
});
} else {
if (!_.isArray(stores)) {
stores = [stores];
}
_.each(stores, store => {
_.merge(this, store.getState());
store.listen(state => {
_.merge(this, state);
this.render();
});
});
}
}
render() {
m.render(this._containerEl, this.content(this, ...this._args));
}
content() {
return "";
}
static view(ctrl, ...args) {
if (args[0][0] === ctrl) {
// see: https://github.com/lhorie/mithril.js/issues/765
args.shift();
}
if (!_.isEqual(args, ctrl._args)) {
ctrl._args = args;
ctrl.render();
}
return m("div.data-container", {
config: (el, isOld) => {
if (isOld) return;
ctrl._containerEl = el;
ctrl.render();
}
});
}
}
let addListener = (name, fn) => (element, isInit, context) => {
if (isInit) return;
element[name] = fn;
context.onunload = () => element[name] = null;
};
class Todo extends DataContainer {
getStores () {
return todoStore;
}
content(todo) {
return [
m("input", {
config: addListener("onchange", ev => actions.saveDescr(ev.target.value)),
value: todo.descr
}),
m("button", {
config: addListener("onclick", () => actions.addItem(todo.descr))
}, "Add"),
m("table", todo.list.map((task, index) => m("tr", [
m("td",
m("input[type=checkbox]", {
config: addListener("onclick", ev => actions.checkItem({
index,
done: ev.target.checked
})),
checked: task.done
})
),
m("td", {
style: {
textDecoration: task.done ? "line-through" : "none"
}
}, task.descr)
]))),
m("hr"),
m("pre", JSON.stringify(JSON.parse(alt.takeSnapshot()), null, 2))
];
}
}
m.mount(document.body, {
controller: Todo,
view: Todo.view
});