Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ludicast committed Dec 10, 2011
1 parent 512d22e commit 0245415
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
31 changes: 20 additions & 11 deletions app/coffeescripts/time_log/angular-code.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class @TasksController
{year, month, date} = $routeParams
@currentDate = new Date(year, month, date)
@$xhr "get", "/api/tasks/#{year}/#{parseInt(month) + 1}/#{date}", (code, @tasks)=>
setInterval (=> @$apply(->)), 1000

TaskViewHelper:
isCompleted:(task)-> !! task.completedAt
Expand All @@ -32,15 +33,13 @@ class @TasksController
startTimer:->
task = {tag:null, title:"Start", completedAt:@timeStamp(), duration:0, createdAt:@timeStamp()}
@createTask task
@time = 0
setInterval @tick, 1000

tick:=>
@time += 1000
hasStarted:->
(task for task in (@tasks or []) when (task.title is "Start")).length > 0

timeSinceStart:->


hasStarted:(tasks = [])->
(task for task in tasks when (task.title is "Start")).length > 0

createTask:(task)->
@$xhr "post", "/api/tasks", task, (code, data)=>
@tasks.push data
Expand Down Expand Up @@ -68,20 +67,30 @@ class @TasksController
loadData:->
@$location.path "/tasks/#{@currentDate.getFullYear()}/#{@currentDate.getMonth()}/#{@currentDate.getDate()}"

totalDurations:->
totalDurations:(condition)->
condition ||= (->true)
duration = 0
for task in (@tasks or [])
if task.duration
if task.duration and condition(task)
duration += task.duration
duration

totalUntagged:->
totalDurations ((task)-> task.tag is null or task.tag.length < 1)

totalTagged:(tag)->
totalDurations ((task)-> task.tag is tag)

lastCompletedAt:->
maxCompletedAt = 0
for task in @tasks
for task in (@tasks or [])
if task.completedAt and task.completedAt > maxCompletedAt
maxCompletedAt = task.completedAt
maxCompletedAt

timeSinceStart:->
Math.floor((@timeStamp() - @lastCompletedAt()) / 1000)

TasksController.$inject = ['$xhr', '$routeParams', '$location']

class @TaskController
Expand All @@ -100,8 +109,8 @@ class @TaskController
@createTask @task

finish:->
@task.duration = Math.floor((@timeStamp() - @lastCompletedAt()) / 1000)
@task.completedAt = @timeStamp()
@task.duration = Math.floor((@lastCompletedAt() - @timeStamp()) / 1000)
@update()

unFinish:->
Expand Down
2 changes: 0 additions & 2 deletions lib/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ class DemoApp < Sinatra::Base

get "/api/tasks/:year/:month/:date" do |year, month, date|
content_type :json
#puts @db.members
tasks_for_today = @db.members.select do |task|
# NOTE: JavaScript stores dates in milliseconds.
time = Time.at task['createdAt'].to_i/1000
puts time.year, time.month, time.day
if time.year == year.to_i && time.month == month.to_i && time.day == date.to_i
true
else
Expand Down
14 changes: 10 additions & 4 deletions public/inside.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
</nav>
<div><h1>{{currentDate.toLocaleDateString()}}</h1></div>
<div class="tasks">
<p class="message-blank" ng:show="isToday() && !(hasStarted(tasks))">
<p class="message-blank" ng:show="isToday() && !(hasStarted())">
<button class="start-tracking" type="Submit" ng:click="startTimer()">Start</button> tracking my time
</p>

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

Expand All @@ -29,13 +29,19 @@
</li>
</ul>

<div class="clocks" ng:show="isToday() && hasStarted(tasks)">
<div class="clocks" ng:show="hasStarted()">
<div class="total-clock">
<time class="total-duration">{{totalDurations() | formatSecondsAsTime}}</time>
<div class="total-time">Total</div>
</div>
</div>

<div class="elapsed-clock" ng:show="isToday() && hasStarted()">
<div class="elapsed">
<time class="elapsed-duration">{{ timeSinceStart() | formatSecondsAsTime }}</time> has elapsed
</div>
</div>

<ul id="tasks-to-complete" ng:repeat="task in tasks.$filter(TaskViewHelper.isNotCompleted).$orderBy('createdAt', false)">
<li class="task" ng:show="!(task.deleted) && !(task.editing)" ng:controller="TaskController">
<input class="is-done" type="checkbox" ng:click="finish()" />
Expand All @@ -61,7 +67,7 @@
</div>
<form ng:submit="addTask()" id="newTaskForm" ng:show="isToday()">
<div class="type"></div>
<!-- validation placement is off -->
<!-- validation placement is off because of css -->
<input id="new-task" type="text" placeholder="Type a new task..." ng:model="newTaskValue" required/>
</form>
</div>
Expand Down

0 comments on commit 0245415

Please sign in to comment.