base.coffee 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class app.views.BasePage extends app.View
  2. constructor: (@el, @entry) -> super
  3. deactivate: ->
  4. if super
  5. @highlightNodes = []
  6. render: (content, fromCache = false) ->
  7. @highlightNodes = []
  8. @previousTiming = null
  9. @addClass "_#{@entry.doc.type}" unless @constructor.className
  10. @html content
  11. @prepare?() unless fromCache
  12. @activate()
  13. @delay @afterRender if @afterRender
  14. if @highlightNodes.length > 0
  15. $.requestAnimationFrame => $.requestAnimationFrame(@paintCode)
  16. return
  17. highlightCode: (el, language) ->
  18. return unless language
  19. language = "language-#{language}"
  20. if $.isCollection(el)
  21. for e in el
  22. e.classList.add(language)
  23. @highlightNodes.push(e)
  24. else if el
  25. el.classList.add(language)
  26. @highlightNodes.push(el)
  27. return
  28. paintCode: (timing) =>
  29. if @previousTiming
  30. if Math.round(1000 / (timing - @previousTiming)) > 50 # fps
  31. @nodesPerFrame = Math.round(Math.min(@nodesPerFrame * 1.25, 50))
  32. else
  33. @nodesPerFrame = Math.round(Math.max(@nodesPerFrame * .8, 10))
  34. else
  35. @nodesPerFrame = 10
  36. for el in @highlightNodes.splice(0, @nodesPerFrame)
  37. $.remove(clipEl) if clipEl = el.lastElementChild
  38. Prism.highlightElement(el)
  39. $.append(el, clipEl) if clipEl
  40. $.requestAnimationFrame(@paintCode) if @highlightNodes.length > 0
  41. @previousTiming = timing
  42. return