big_pipe.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings) {
  8. function mapTextContentToAjaxResponse(content) {
  9. if (content === '') {
  10. return false;
  11. }
  12. try {
  13. return JSON.parse(content);
  14. } catch (e) {
  15. return false;
  16. }
  17. }
  18. function bigPipeProcessPlaceholderReplacement(index, placeholderReplacement) {
  19. var placeholderId = placeholderReplacement.getAttribute('data-big-pipe-replacement-for-placeholder-with-id');
  20. var content = this.textContent.trim();
  21. if (typeof drupalSettings.bigPipePlaceholderIds[placeholderId] !== 'undefined') {
  22. var response = mapTextContentToAjaxResponse(content);
  23. if (response === false) {
  24. $(this).removeOnce('big-pipe');
  25. } else {
  26. var ajaxObject = Drupal.ajax({
  27. url: '',
  28. base: false,
  29. element: false,
  30. progress: false
  31. });
  32. ajaxObject.success(response, 'success');
  33. }
  34. }
  35. }
  36. var interval = drupalSettings.bigPipeInterval || 50;
  37. var timeoutID = void 0;
  38. function bigPipeProcessDocument(context) {
  39. if (!context.querySelector('script[data-big-pipe-event="start"]')) {
  40. return false;
  41. }
  42. $(context).find('script[data-big-pipe-replacement-for-placeholder-with-id]').once('big-pipe').each(bigPipeProcessPlaceholderReplacement);
  43. if (context.querySelector('script[data-big-pipe-event="stop"]')) {
  44. if (timeoutID) {
  45. clearTimeout(timeoutID);
  46. }
  47. return true;
  48. }
  49. return false;
  50. }
  51. function bigPipeProcess() {
  52. timeoutID = setTimeout(function () {
  53. if (!bigPipeProcessDocument(document)) {
  54. bigPipeProcess();
  55. }
  56. }, interval);
  57. }
  58. bigPipeProcess();
  59. $(window).on('load', function () {
  60. if (timeoutID) {
  61. clearTimeout(timeoutID);
  62. }
  63. bigPipeProcessDocument(document);
  64. });
  65. })(jQuery, Drupal, drupalSettings);