jquery.coffee 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. for el in @findAllByClass 'syntaxhighlighter'
  11. language = if el.classList.contains('javascript') then 'javascript' else 'markup'
  12. @highlightCode el, language
  13. return
  14. onIframeLoaded: (event) =>
  15. event.target.style.display = ''
  16. $.off event.target, 'load', @onIframeLoaded
  17. return
  18. runExamples: ->
  19. for el in @findAllByClass 'entry-example'
  20. try @runExample el catch
  21. return
  22. runExample: (el) ->
  23. source = el.getElementsByClassName('syntaxhighlighter')[0]
  24. return unless source and source.innerHTML.indexOf('!doctype') isnt -1
  25. unless iframe = el.getElementsByClassName(@constructor.demoClassName)[0]
  26. iframe = document.createElement 'iframe'
  27. iframe.className = @constructor.demoClassName
  28. iframe.width = '100%'
  29. iframe.height = 200
  30. el.appendChild(iframe)
  31. doc = iframe.contentDocument
  32. doc.write @fixIframeSource(source.textContent)
  33. doc.close()
  34. return
  35. fixIframeSource: (source) ->
  36. source = source.replace '"/resources/', '"http://api.jquery.com/resources/' # attr(), keydown()
  37. source.replace '</head>', """
  38. <style>
  39. html, body { border: 0; margin: 0; padding: 0; }
  40. body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
  41. </style>
  42. <script>
  43. $.ajaxPrefilter(function(opt, opt2, xhr) {
  44. if (opt.url.indexOf('http') !== 0) {
  45. xhr.abort();
  46. document.body.innerHTML = "<p><strong>This demo cannot run inside DevDocs.</strong></p>";
  47. }
  48. });
  49. </script>
  50. </head>
  51. """