@el.offsetHeight
addClass @el, 'odometer-animating'
, 0
@value = newValue
renderDigit: ->
createFromHTML DIGIT_HTML
insertDigit: (digit, before) ->
if before?
@inside.insertBefore digit, before
else if not @inside.children.length
@inside.appendChild digit
else
@inside.insertBefore digit, @inside.children[0]
addSpacer: (chr, before, extraClasses) ->
spacer = createFromHTML FORMAT_MARK_HTML
spacer.innerHTML = chr
addClass(spacer, extraClasses) if extraClasses
@insertDigit spacer, before
addDigit: (value, repeating=true) ->
if value is '-'
return @addSpacer value, null, 'odometer-negation-mark'
if value is '.'
return @addSpacer (@format.radix ? '.'), null, 'odometer-radix-mark'
if repeating
resetted = false
while true
if not @format.repeating.length
if resetted
throw new Error "Bad odometer format without digits"
@resetFormat()
resetted = true
chr = @format.repeating[@format.repeating.length - 1]
@format.repeating = @format.repeating.substring(0, @format.repeating.length - 1)
break if chr is 'd'
@addSpacer chr
digit = @renderDigit()
digit.querySelector('.odometer-value').innerHTML = value
@digits.push digit
@insertDigit digit
animate: (newValue) ->
if not TRANSITION_SUPPORT or @options.animation is 'count'
@animateCount newValue
else
@animateSlide newValue
animateCount: (newValue) ->
return unless diff = +newValue - @value
start = last = now()
cur = @value
do tick = =>
if (now() - start) > @options.duration
@value = newValue
@render()
trigger @el, 'odometerdone'
return
delta = now() - last
if delta > COUNT_MS_PER_FRAME
last = now()
fraction = delta / @options.duration
dist = diff * fraction
cur += dist
@render Math.round cur
if requestAnimationFrame?
requestAnimationFrame tick
else
setTimeout tick, COUNT_MS_PER_FRAME
getDigitCount: (values...) ->
for value, i in values
values[i] = Math.abs(value)
max = Math.max values...
Math.ceil(Math.log(max + 1) / Math.log(10))
getFractionalDigitCount: (values...) ->