Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Run javascript class to track progress toward/differentials for s…

…egment goals

- add support for negative values in timer display
- allow stopwatch to accept passed time values for start/stop calls
  • Loading branch information...
commit 2894a648cdd0677114f80bdd2e8b13c67a479a85 1 parent b08bd90
Andrew Horner authored
2  app/assets/javascripts/application.js
View
@@ -12,4 +12,6 @@
//
//= require jquery
//= require jquery_ujs
+//= require dispatcher
+//= require stopwatch
//= require_tree .
26 app/assets/javascripts/helpers.js.coffee
View
@@ -1,8 +1,14 @@
-{ floor } = Math
+{ floor, abs } = Math
Array::remove = (element) ->
@[i..i] = [] if (i = @indexOf(element)) > -1
-
+
+Array::sum = () ->
+ if this.length > 0
+ this.reduce (x, y) -> x + y
+ else
+ 0
+
Array::last = () ->
@[@length - 1] if @length
@@ -13,17 +19,19 @@ Number::pad = (d) ->
result
Number::toTimer = (verbose = false) ->
- hours = floor @/3600000
- minutes = floor(@/60000) % 60
- seconds = floor(@/1000) % 60
- ms = @ % 1000
+ neg = if @ < 0 then '-' else ''
+ val = abs @
+ hours = floor val/3600000
+ minutes = floor(val/60000) % 60
+ seconds = floor(val/1000) % 60
+ ms = val % 1000
if verbose || hours
- "#{hours}:#{minutes.pad 2}:#{seconds.pad 2}.#{ms.pad 3}"
+ "#{neg}#{hours}:#{minutes.pad 2}:#{seconds.pad 2}.#{ms.pad 3}"
else if minutes
- "#{minutes}:#{seconds.pad 2}.#{ms.pad 3}"
+ "#{neg}#{minutes}:#{seconds.pad 2}.#{ms.pad 3}"
else
- "#{seconds}.#{ms.pad 3}"
+ "#{neg}#{seconds}.#{ms.pad 3}"
String::count = (substr) ->
num = pos = 0
40 app/assets/javascripts/runs.js.coffee
View
@@ -1,3 +1,43 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
+
+class Run extends Stopwatch
+ goals: []
+ segments: []
+ differentials: []
+ goal: 0
+ milestone: 0
+
+ constructor: (@goals) ->
+ super()
+
+ start: (time) =>
+ @goal = 0
+ @segments = []
+ @differentials = []
+ @milestone = @goals[@goal]
+ super(time)
+
+ split: () =>
+ time = super()
+ @segments[@goal] = time
+ @differentials[@goal] = time - @goals[@goal]
+
+ @goal++
+ if @goal == @goals.length
+ @stop @splits.last()
+ else
+ @milestone += @goals[@goal]
+
+ target: () =>
+ @goals.sum()
+
+ previous: () =>
+ @differentials.last()
+
+ difference: () =>
+ @differentials.sum()
+
+ progress: () =>
+ @elapsed() - @milestone
20 app/assets/javascripts/stopwatch.js.coffee
View
@@ -16,25 +16,27 @@ class @Stopwatch extends Dispatcher
return 0 unless @running
@current() - (@splits.last() || @started)
- start: () =>
+ start: (time) =>
return if @running
@reset()
@running = true
- @started = @current()
- @trigger 'start'
+ @started = time || @current()
+ @trigger 'start', @started
- stop: () =>
- @stopped = @split()
+ stop: (time) =>
+ return unless @running
+ @split() unless time
+ @stopped = time || @splits.last()
@running = false
- @trigger 'stop'
+ @trigger 'stop', @stopped
split: () =>
return unless @running
split = @current()
- last = @splits.last() || @started
+ difference = split - (@splits.last() || @started)
@splits.push(split)
- @trigger 'split', split - last
- split
+ @trigger 'split', difference
+ difference
reset: () =>
@trigger 'stop' if @running
Please sign in to comment.
Something went wrong with that request. Please try again.