Skip to content

Commit

Permalink
Support setting options after bringing in the odometer file. Support …
Browse files Browse the repository at this point in the history
…auto init on IE8. Make digit formatting safe from infinite loops. Add contributing instructions and license.
  • Loading branch information
zackbloom committed Oct 10, 2013
1 parent e49cefe commit f8e5271
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
8 changes: 8 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Copyright (c) 2013 HubSpot, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ On older browsers, it will automatically fallback to a simpler animation which w
Advanced
--------

If you need to, you can set options by creating a `odometerOptions` object before bringing in the odometer source.
If you need to, you can set options by creating a `odometerOptions` object:

```javascript
window.odometerOptions = {
Expand All @@ -49,3 +49,31 @@ od.update(555)
// or
el.innerHTML = 555
```

Browser Support
---------------

Odometer is intended to support IE8+, FF4+, Chrome and Safari.

Contributing
------------

Odometer is built using Grunt. To setup the build environment you first
must have Node.js installed. Then:

```bash
# In the project directory
npm install
```

To build the project:
```bash
grunt
```

To have it watch for changes and build automatically:
```bash
grunt watch
```

We welcome pull requests!
36 changes: 30 additions & 6 deletions odometer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ createFromHTML = (html) ->
el.children[0]

now = ->
performance?.now() ? +new Date
window.performance?.now() ? +new Date

class Odometer
constructor: (@options) ->
Expand Down Expand Up @@ -96,6 +96,7 @@ class Odometer
, 0

true
, false

resetFormat: ->
@format = @options.format.split('').reverse().join('')
Expand Down Expand Up @@ -164,11 +165,16 @@ class Odometer
@inside.insertBefore digit, @inside.children[0]

addDigit: (value) ->
resetted = false
while true
if not @format.length
if resetted
throw new Error "Bad odometer format without digits"

@resetFormat()
resetted = true

char = @format.substring(0, 1)
char = @format[0]
@format = @format.substring(1)

break if char is 'd'
Expand Down Expand Up @@ -211,7 +217,7 @@ class Odometer
cur += dist
@render Math.round cur

if window.requestAnimationFrame
if window.requestAnimationFrame?
requestAnimationFrame tick
else
setTimeout tick, COUNT_MS_PER_FRAME
Expand Down Expand Up @@ -277,14 +283,32 @@ class Odometer

Odometer.options = window.odometerOptions ? {}

setTimeout ->
# We do this in a seperate pass to allow people to set
# window.odometerOptions after bringing the file in.
if window.odometerOptions
for k, v of window.odometerOptions
Odometer.options[k] ?= v
, 0

Odometer.init = ->
elements = document.querySelectorAll (Odometer.options.selector or '.odometer')

for el in elements
el.odometer = new Odometer {el, value: el.innerText}

document.addEventListener 'DOMContentLoaded', ->
if Odometer.options.auto isnt false
Odometer.init()
if document.documentElement?.doScroll? and document.createEventObject?
# IE < 9
_old = document.onreadystatechange
document.onreadystatechange = ->
if document.readyState is 'complete' and Odometer.options.auto isnt false
Odometer.init()

_old?.apply this, arguments
else
document.addEventListener 'DOMContentLoaded', ->
if Odometer.options.auto isnt false
Odometer.init()
, false

window.Odometer = Odometer

0 comments on commit f8e5271

Please sign in to comment.