Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
@@ -12,4 +12,6 @@
12 12 //
13 13 //= require jquery
14 14 //= require jquery_ujs
  15 +//= require dispatcher
  16 +//= require stopwatch
15 17 //= require_tree .
26 app/assets/javascripts/helpers.js.coffee
... ... @@ -1,8 +1,14 @@
1   -{ floor } = Math
  1 +{ floor, abs } = Math
2 2
3 3 Array::remove = (element) ->
4 4 @[i..i] = [] if (i = @indexOf(element)) > -1
5   -
  5 +
  6 +Array::sum = () ->
  7 + if this.length > 0
  8 + this.reduce (x, y) -> x + y
  9 + else
  10 + 0
  11 +
6 12 Array::last = () ->
7 13 @[@length - 1] if @length
8 14
@@ -13,17 +19,19 @@ Number::pad = (d) ->
13 19 result
14 20
15 21 Number::toTimer = (verbose = false) ->
16   - hours = floor @/3600000
17   - minutes = floor(@/60000) % 60
18   - seconds = floor(@/1000) % 60
19   - ms = @ % 1000
  22 + neg = if @ < 0 then '-' else ''
  23 + val = abs @
  24 + hours = floor val/3600000
  25 + minutes = floor(val/60000) % 60
  26 + seconds = floor(val/1000) % 60
  27 + ms = val % 1000
20 28
21 29 if verbose || hours
22   - "#{hours}:#{minutes.pad 2}:#{seconds.pad 2}.#{ms.pad 3}"
  30 + "#{neg}#{hours}:#{minutes.pad 2}:#{seconds.pad 2}.#{ms.pad 3}"
23 31 else if minutes
24   - "#{minutes}:#{seconds.pad 2}.#{ms.pad 3}"
  32 + "#{neg}#{minutes}:#{seconds.pad 2}.#{ms.pad 3}"
25 33 else
26   - "#{seconds}.#{ms.pad 3}"
  34 + "#{neg}#{seconds}.#{ms.pad 3}"
27 35
28 36 String::count = (substr) ->
29 37 num = pos = 0
40 app/assets/javascripts/runs.js.coffee
... ... @@ -1,3 +1,43 @@
1 1 # Place all the behaviors and hooks related to the matching controller here.
2 2 # All this logic will automatically be available in application.js.
3 3 # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
  4 +
  5 +class Run extends Stopwatch
  6 + goals: []
  7 + segments: []
  8 + differentials: []
  9 + goal: 0
  10 + milestone: 0
  11 +
  12 + constructor: (@goals) ->
  13 + super()
  14 +
  15 + start: (time) =>
  16 + @goal = 0
  17 + @segments = []
  18 + @differentials = []
  19 + @milestone = @goals[@goal]
  20 + super(time)
  21 +
  22 + split: () =>
  23 + time = super()
  24 + @segments[@goal] = time
  25 + @differentials[@goal] = time - @goals[@goal]
  26 +
  27 + @goal++
  28 + if @goal == @goals.length
  29 + @stop @splits.last()
  30 + else
  31 + @milestone += @goals[@goal]
  32 +
  33 + target: () =>
  34 + @goals.sum()
  35 +
  36 + previous: () =>
  37 + @differentials.last()
  38 +
  39 + difference: () =>
  40 + @differentials.sum()
  41 +
  42 + progress: () =>
  43 + @elapsed() - @milestone
20 app/assets/javascripts/stopwatch.js.coffee
@@ -16,25 +16,27 @@ class @Stopwatch extends Dispatcher
16 16 return 0 unless @running
17 17 @current() - (@splits.last() || @started)
18 18
19   - start: () =>
  19 + start: (time) =>
20 20 return if @running
21 21 @reset()
22 22 @running = true
23   - @started = @current()
24   - @trigger 'start'
  23 + @started = time || @current()
  24 + @trigger 'start', @started
25 25
26   - stop: () =>
27   - @stopped = @split()
  26 + stop: (time) =>
  27 + return unless @running
  28 + @split() unless time
  29 + @stopped = time || @splits.last()
28 30 @running = false
29   - @trigger 'stop'
  31 + @trigger 'stop', @stopped
30 32
31 33 split: () =>
32 34 return unless @running
33 35 split = @current()
34   - last = @splits.last() || @started
  36 + difference = split - (@splits.last() || @started)
35 37 @splits.push(split)
36   - @trigger 'split', split - last
37   - split
  38 + @trigger 'split', difference
  39 + difference
38 40
39 41 reset: () =>
40 42 @trigger 'stop' if @running

0 comments on commit 2894a64

Please sign in to comment.
Something went wrong with that request. Please try again.