jquery.js 1.8 KB

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