forked from MithrilJS/mithril.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.html
98 lines (84 loc) · 1.96 KB
/
chain.html
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
<!doctype html>
<html>
<head>
<title>Don't break the chain</title>
</head>
<body>
<script src="mithril.js"></script>
<script>
//our app's namespace
var chain = {};
//model
chain.save = function(list) {
localStorage["chain-app.list"] = JSON.stringify(list);
};
chain.load = function() {
return JSON.parse(localStorage["chain-app.list"] || "[]");
};
chain.today = function() {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
}
chain.resetDate = function() {
return localStorage["chain-app.start-date"] = chain.today().getTime();
}
chain.startDate = function() {
return new Date(parseInt(localStorage["chain-app.start-date"] || chain.resetDate()));
}
chain.dateAt = function(index) {
var date = new Date(chain.startDate());
date.setDate(date.getDate() + index);
return date;
}
//controller
chain.controller = function() {
var list = chain.load();
this.isChecked = function(index) {
return list[index]
};
this.check = function(index, status) {
if (chain.dateAt(index).getTime() <= chain.today().getTime()) {
list[index] = status;
chain.save(list);
}
};
};
//view
chain.view = function(ctrl) {
return m("table", chain.seven(function(y) {
return m("tr", chain.seven(function(x) {
var index = chain.indexAt(x, y)
return m("td", chain.highlights(index), [
m("input[type=checkbox]", chain.checks(ctrl, index))
]);
}));
}));
};
chain.seven = function(subject) {
var output = [];
for (var i = 0; i < 7; i++) output.push(subject(i));
return output;
};
chain.checks = function(ctrl, index) {
return {
onclick: function() {
ctrl.check(index, this.checked);
},
checked: ctrl.isChecked(index)
};
};
chain.highlights = function(index) {
return {
style: {
background: chain.dateAt(index).getTime() == chain.today().getTime() ? "silver" : ""
}
};
};
chain.indexAt = function(x, y) {
return y * 7 + x;
}
//render it
m.module(document.body, chain);
</script>
</body>
</html>