jquery.coffee 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #= require views/pages/base
  2. class app.views.JqueryPage extends app.views.BasePage
  3. @demoClassName: '_jquery-demo'
  4. prepare: ->
  5. for el in @findAllByClass 'syntaxhighlighter'
  6. language = if el.classList.contains('javascript') then 'javascript' else 'markup'
  7. @highlightCode el, language
  8. return
  9. afterRender: ->
  10. # Prevent jQuery Mobile's demo iframes from scrolling the page
  11. for iframe in @findAllByTag 'iframe'
  12. iframe.style.display = 'none'
  13. $.on iframe, 'load', @onIframeLoaded
  14. @runExamples()
  15. onIframeLoaded: (event) =>
  16. event.target.style.display = ''
  17. $.off event.target, 'load', @onIframeLoaded
  18. return
  19. runExamples: ->
  20. for el in @findAllByClass 'entry-example'
  21. try @runExample el catch
  22. return
  23. runExample: (el) ->
  24. source = el.getElementsByClassName('syntaxhighlighter')[0]
  25. return unless source and source.innerHTML.indexOf('!doctype') isnt -1
  26. unless iframe = el.getElementsByClassName(@constructor.demoClassName)[0]
  27. iframe = document.createElement 'iframe'
  28. iframe.className = @constructor.demoClassName
  29. iframe.width = '100%'
  30. iframe.height = 200
  31. el.appendChild(iframe)
  32. doc = iframe.contentDocument
  33. doc.write @fixIframeSource(source.textContent)
  34. doc.close()
  35. return
  36. fixIframeSource: (source) ->
  37. source = source.replace '"/resources/', '"https://api.jquery.com/resources/' # attr(), keydown()
  38. source.replace '</head>', """
  39. <style>
  40. html, body { border: 0; margin: 0; padding: 0; }
  41. body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
  42. </style>
  43. <script>
  44. $.ajaxPrefilter(function(opt, opt2, xhr) {
  45. if (opt.url.indexOf('http') !== 0) {
  46. xhr.abort();
  47. document.body.innerHTML = "<p><strong>This demo cannot run inside DevDocs.</strong></p>";
  48. }
  49. });
  50. </script>
  51. </head>
  52. """