Skip to content

Commit 0245415

Browse files
committed
code cleanup
1 parent 512d22e commit 0245415

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

app/coffeescripts/time_log/angular-code.coffee

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class @TasksController
1414
{year, month, date} = $routeParams
1515
@currentDate = new Date(year, month, date)
1616
@$xhr "get", "/api/tasks/#{year}/#{parseInt(month) + 1}/#{date}", (code, @tasks)=>
17+
setInterval (=> @$apply(->)), 1000
1718

1819
TaskViewHelper:
1920
isCompleted:(task)-> !! task.completedAt
@@ -32,15 +33,13 @@ class @TasksController
3233
startTimer:->
3334
task = {tag:null, title:"Start", completedAt:@timeStamp(), duration:0, createdAt:@timeStamp()}
3435
@createTask task
35-
@time = 0
36-
setInterval @tick, 1000
3736

38-
tick:=>
39-
@time += 1000
37+
hasStarted:->
38+
(task for task in (@tasks or []) when (task.title is "Start")).length > 0
39+
40+
timeSinceStart:->
41+
4042

41-
hasStarted:(tasks = [])->
42-
(task for task in tasks when (task.title is "Start")).length > 0
43-
4443
createTask:(task)->
4544
@$xhr "post", "/api/tasks", task, (code, data)=>
4645
@tasks.push data
@@ -68,20 +67,30 @@ class @TasksController
6867
loadData:->
6968
@$location.path "/tasks/#{@currentDate.getFullYear()}/#{@currentDate.getMonth()}/#{@currentDate.getDate()}"
7069

71-
totalDurations:->
70+
totalDurations:(condition)->
71+
condition ||= (->true)
7272
duration = 0
7373
for task in (@tasks or [])
74-
if task.duration
74+
if task.duration and condition(task)
7575
duration += task.duration
7676
duration
7777

78+
totalUntagged:->
79+
totalDurations ((task)-> task.tag is null or task.tag.length < 1)
80+
81+
totalTagged:(tag)->
82+
totalDurations ((task)-> task.tag is tag)
83+
7884
lastCompletedAt:->
7985
maxCompletedAt = 0
80-
for task in @tasks
86+
for task in (@tasks or [])
8187
if task.completedAt and task.completedAt > maxCompletedAt
8288
maxCompletedAt = task.completedAt
8389
maxCompletedAt
8490

91+
timeSinceStart:->
92+
Math.floor((@timeStamp() - @lastCompletedAt()) / 1000)
93+
8594
TasksController.$inject = ['$xhr', '$routeParams', '$location']
8695

8796
class @TaskController
@@ -100,8 +109,8 @@ class @TaskController
100109
@createTask @task
101110

102111
finish:->
112+
@task.duration = Math.floor((@timeStamp() - @lastCompletedAt()) / 1000)
103113
@task.completedAt = @timeStamp()
104-
@task.duration = Math.floor((@lastCompletedAt() - @timeStamp()) / 1000)
105114
@update()
106115

107116
unFinish:->

lib/server.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ class DemoApp < Sinatra::Base
4646

4747
get "/api/tasks/:year/:month/:date" do |year, month, date|
4848
content_type :json
49-
#puts @db.members
5049
tasks_for_today = @db.members.select do |task|
5150
# NOTE: JavaScript stores dates in milliseconds.
5251
time = Time.at task['createdAt'].to_i/1000
53-
puts time.year, time.month, time.day
5452
if time.year == year.to_i && time.month == month.to_i && time.day == date.to_i
5553
true
5654
else

public/inside.html

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
</nav>
1313
<div><h1>{{currentDate.toLocaleDateString()}}</h1></div>
1414
<div class="tasks">
15-
<p class="message-blank" ng:show="isToday() && !(hasStarted(tasks))">
15+
<p class="message-blank" ng:show="isToday() && !(hasStarted())">
1616
<button class="start-tracking" type="Submit" ng:click="startTimer()">Start</button> tracking my time
1717
</p>
1818

19-
<p class="message-blank" ng:show="!(isToday()) && !(hasStarted(tasks))">
19+
<p class="message-blank" ng:show="!(isToday()) && !(hasStarted())">
2020
No tasks were tracked on this day
2121
</p>
2222

@@ -29,13 +29,19 @@
2929
</li>
3030
</ul>
3131

32-
<div class="clocks" ng:show="isToday() && hasStarted(tasks)">
32+
<div class="clocks" ng:show="hasStarted()">
3333
<div class="total-clock">
3434
<time class="total-duration">{{totalDurations() | formatSecondsAsTime}}</time>
3535
<div class="total-time">Total</div>
3636
</div>
3737
</div>
3838

39+
<div class="elapsed-clock" ng:show="isToday() && hasStarted()">
40+
<div class="elapsed">
41+
<time class="elapsed-duration">{{ timeSinceStart() | formatSecondsAsTime }}</time> has elapsed
42+
</div>
43+
</div>
44+
3945
<ul id="tasks-to-complete" ng:repeat="task in tasks.$filter(TaskViewHelper.isNotCompleted).$orderBy('createdAt', false)">
4046
<li class="task" ng:show="!(task.deleted) && !(task.editing)" ng:controller="TaskController">
4147
<input class="is-done" type="checkbox" ng:click="finish()" />
@@ -61,7 +67,7 @@
6167
</div>
6268
<form ng:submit="addTask()" id="newTaskForm" ng:show="isToday()">
6369
<div class="type"></div>
64-
<!-- validation placement is off -->
70+
<!-- validation placement is off because of css -->
6571
<input id="new-task" type="text" placeholder="Type a new task..." ng:model="newTaskValue" required/>
6672
</form>
6773
</div>

0 commit comments

Comments
 (0)