codecombat/app/views/play/level/dialogue_animator.coffee

48 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
module.exports = class DialogueAnimator
jqueryElement: null
childrenToAdd: null
charsToAdd: null
childAnimator: null
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
constructor: (html, @jqueryElement) ->
d = $('<div></div>').html(html)
@childrenToAdd = _.map(d[0].childNodes, (e) -> return e)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
tick: ->
if not @charsToAdd and not @childAnimator
@addNextElement()
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
if @charsToAdd
@addSingleChar()
return
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
if @childAnimator
@childAnimator.tick()
if @childAnimator.done()
@childAnimator = null
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
addNextElement: ->
return unless @childrenToAdd.length
nextElem = @childrenToAdd[0]
@childrenToAdd = @childrenToAdd[1..]
if nextElem.nodeName is '#text'
@charsToAdd = _.string.chars(nextElem.nodeValue)
else
value = nextElem.innerHTML
newElem = $(nextElem).html('')
@jqueryElement.append(newElem)
if value
@childAnimator = new DialogueAnimator(value, newElem)
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
addSingleChar: ->
@jqueryElement.html(@jqueryElement.html() + @charsToAdd[0])
@charsToAdd = @charsToAdd[1..]
if @charsToAdd.length is 0
@charsToAdd = null
2014-06-30 22:16:26 -04:00
2014-01-03 13:32:13 -05:00
done: ->
return false if @childrenToAdd.length > 0
return false if @charsToAdd
return false if @childAnimator
return true