jquery.js 2.5 KB

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