-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.js
128 lines (104 loc) · 3.06 KB
/
application.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
126
127
128
var App = window.App = Ember.Application.create();
var proxyToContent = function(method) {
return function() {
this.get('content')[method]();
};
};
App.PomodoroController = Ember.Controller.extend({
start: proxyToContent('start'),
stop: proxyToContent('stop'),
reset: proxyToContent('reset'),
timeLeftMin: Ember.computed(function() {
return Math.floor(this.get('content.timeLeft') / 60 / 1000);
}).property('content.timeLeft'),
timeLeftSec: Ember.computed(function() {
return Math.floor(this.get('content.timeLeft') / 1000) % 60
}).property('content.timeLeft'),
});
Ember.TEMPLATES['application'] = Ember.Handlebars.compile([
'<header>',
'<div id="title">',
'<h1>Pomodoro</h1>',
'</div>',
'</header>',
'<div id="content">',
'{{outlet}}',
'</div>'
].join(''));
Ember.TEMPLATES['pomodoro'] = Ember.Handlebars.compile([
'<div id="timer">',
'<div id="time-left">{{timeLeftMin}}</div>',
'<div id="time-left-sec">.{{timeLeftSec}}</div>',
'</div>',
'<span>',
'<button {{action start}}>Start</button>',
'<button {{action stop}}>Stop</button>',
'<button {{action reset}}>Reset</button>',
'</span>'
].join(''))
App.PomodoroView = Ember.View.extend({
tagName: 'div',
});
App.Timer = Ember.Object.extend({
timerId: null,
startTime: null,
currentTime: null,
passedTime: 0,
max: 25 * 60 * 1000,
start: function() {
if (this.get('timerId')) {
return
}
var now = Date.now();
if (Ember.isNone(this.get('currentTime'))) {
this.set('startTime', now);
this.set('currentTime', now);
} else {
var diff = now - this.get('currentTime');
this.set('startTime', this.get('startTime') + diff);
this.set('currentTime', now);
}
var timerId = setInterval(function() {
this.set('currentTime', Date.now());
}.bind(this), 20);
this.set('timerId', timerId);
},
stop: function() {
var timerId = this.get('timerId');
if (!timerId) {
return
}
clearInterval(timerId);
this.set('timerId', null);
this.set('currentTime', Date.now());
},
reset: function() {
this.stop();
this.set('startTime', null);
this.set('currentTime', null);
},
currentTimeDidChanged: Ember.observer(function() {
if (this.get('max') - this.get('passedTime') < 1000) {
this.stop();
// TODO Do some action to notify compete pomodoro.
}
}, 'currentTime'),
passedTime: Ember.computed(function() {
return this.get('currentTime') - this.get('startTime');
}).property('startTime', 'currentTime'),
timeLeft: Ember.computed(function() {
return this.get('max') - (this.get('currentTime') - this.get('startTime'));
}).property('max', 'currentTime', 'startTime')
});
App.currentTimer = App.Timer.create();
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.currentTimer;
},
setupController: function(controller, model) {
this.controllerFor('pomodoro').set('content', model);
},
renderTemplate: function() {
this.render('pomodoro');
}
});