base.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. @highlightCode() unless fromCache
  12. @activate()
  13. @delay @afterRender if @afterRender
  14. if @highlightNodes.length > 0
  15. $.requestAnimationFrame => $.requestAnimationFrame(@paintCode)
  16. return
  17. highlightCode: ->
  18. for el in @findAll('pre[data-language]')
  19. language = el.getAttribute('data-language')
  20. el.classList.add("language-#{language}")
  21. @highlightNodes.push(el)
  22. return
  23. paintCode: (timing) =>
  24. if @previousTiming
  25. if Math.round(1000 / (timing - @previousTiming)) > 50 # fps
  26. @nodesPerFrame = Math.round(Math.min(@nodesPerFrame * 1.25, 50))
  27. else
  28. @nodesPerFrame = Math.round(Math.max(@nodesPerFrame * .8, 10))
  29. else
  30. @nodesPerFrame = 10
  31. for el in @highlightNodes.splice(0, @nodesPerFrame)
  32. $.remove(clipEl) if clipEl = el.lastElementChild
  33. Prism.highlightElement(el)
  34. $.append(el, clipEl) if clipEl
  35. $.requestAnimationFrame(@paintCode) if @highlightNodes.length > 0
  36. @previousTiming = timing
  37. return