jquery.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS206: Consider reworking classes to avoid initClass
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  9. */
  10. //= require views/pages/base
  11. app.views.JqueryPage = class JqueryPage extends app.views.BasePage {
  12. static initClass() {
  13. this.demoClassName = "_jquery-demo";
  14. }
  15. afterRender() {
  16. // Prevent jQuery Mobile's demo iframes from scrolling the page
  17. for (var iframe of Array.from(this.findAllByTag("iframe"))) {
  18. iframe.style.display = "none";
  19. this.onIframeLoaded = this.onIframeLoaded.bind(this);
  20. $.on(iframe, "load", this.onIframeLoaded);
  21. }
  22. return this.runExamples();
  23. }
  24. onIframeLoaded(event) {
  25. event.target.style.display = "";
  26. $.off(event.target, "load", this.onIframeLoaded);
  27. }
  28. runExamples() {
  29. for (var el of Array.from(this.findAllByClass("entry-example"))) {
  30. try {
  31. this.runExample(el);
  32. } catch (error) {}
  33. }
  34. }
  35. runExample(el) {
  36. let iframe;
  37. const source = el.getElementsByClassName("syntaxhighlighter")[0];
  38. if (!source || source.innerHTML.indexOf("!doctype") === -1) {
  39. return;
  40. }
  41. if (
  42. !(iframe = el.getElementsByClassName(this.constructor.demoClassName)[0])
  43. ) {
  44. iframe = document.createElement("iframe");
  45. iframe.className = this.constructor.demoClassName;
  46. iframe.width = "100%";
  47. iframe.height = 200;
  48. el.appendChild(iframe);
  49. }
  50. const doc = iframe.contentDocument;
  51. doc.write(this.fixIframeSource(source.textContent));
  52. doc.close();
  53. }
  54. fixIframeSource(source) {
  55. source = source.replace(
  56. '"/resources/',
  57. '"https://api.jquery.com/resources/',
  58. ); // attr(), keydown()
  59. source = source.replace(
  60. "</head>",
  61. `\
  62. <style>
  63. html, body { border: 0; margin: 0; padding: 0; }
  64. body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; }
  65. </style>
  66. <script>
  67. $.ajaxPrefilter(function(opt, opt2, xhr) {
  68. if (opt.url.indexOf('http') !== 0) {
  69. xhr.abort();
  70. document.body.innerHTML = "<p><strong>This demo cannot run inside DevDocs.</strong></p>";
  71. }
  72. });
  73. </script>
  74. </head>\
  75. `,
  76. );
  77. return source.replace(/<script>/gi, '<script nonce="devdocs">');
  78. }
  79. };
  80. app.views.JqueryPage.initClass();