From f0b4206f961ea9e645f9da100e29bb5db47a7d15 Mon Sep 17 00:00:00 2001
From: Justin Toupin <justin@atendesigngroup.com>
Date: Mon, 10 Jan 2022 08:58:09 -0700
Subject: [PATCH] JS fixes: 1) Prevent dialog buttons from being assigned
 twice. 2) Prevent dialog height check interval from being initialized more
 than once at the same time.

---
 js/builder.es6.js | 14 +++++++++++---
 js/builder.js     | 15 ++++++++++++---
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/js/builder.es6.js b/js/builder.es6.js
index 06d12b8..778444c 100644
--- a/js/builder.es6.js
+++ b/js/builder.es6.js
@@ -486,6 +486,10 @@
   // @see https://www.drupal.org/project/layout_paragraphs/issues/3216981
   $(window).on('dialog:aftercreate', (event, dialog, $dialog) => {
     if ($dialog.attr('id').indexOf('lpb-dialog-') === 0) {
+      // If buttons have already been added to the buttonpane, do not continue.
+      if ($dialog.dialog('option', 'buttons').length > 0) {
+        return;
+      }
       const buttons = [];
       const $buttons = $dialog.find(
         '.layout-paragraphs-component-form > .form-actions input[type=submit], .layout-paragraphs-component-form > .form-actions a.button',
@@ -518,13 +522,17 @@
   // Repositions open dialogs.
   // @see https://www.drupal.org/project/layout_paragraphs/issues/3252978
   // @see https://stackoverflow.com/questions/5456298/refresh-jquery-ui-dialog-position
+  let lpDialogInterval;
   $(window).on('dialog:aftercreate', (event, dialog, $dialog) => {
     if ($dialog[0].id.indexOf('lpb-dialog-') === 0) {
+      if (lpDialogInterval) {
+        clearInterval(lpDialogInterval);
+      }
       $dialog.data('lpOriginalHeight', $dialog.outerHeight());
-      $dialog.data('lpDialogInterval', setInterval(repositionDialog.bind(null, $dialog), 500));
+      lpDialogInterval = setInterval(repositionDialog.bind(null, $dialog), 500);
     }
   });
-  $(window).on('dialog:beforeclose', (event, dialog, $dialog) => {
-    clearInterval($dialog.data('lpDialogInterval'));
+  $(window).on('dialog:beforeclose', () => {
+    clearInterval(lpDialogInterval);
   });
 })(jQuery, Drupal, Drupal.debounce, dragula);
diff --git a/js/builder.js b/js/builder.js
index c1d3867..d3a5dfb 100644
--- a/js/builder.js
+++ b/js/builder.js
@@ -371,6 +371,10 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
   };
   $(window).on('dialog:aftercreate', function (event, dialog, $dialog) {
     if ($dialog.attr('id').indexOf('lpb-dialog-') === 0) {
+      if ($dialog.dialog('option', 'buttons').length > 0) {
+        return;
+      }
+
       var buttons = [];
       var $buttons = $dialog.find('.layout-paragraphs-component-form > .form-actions input[type=submit], .layout-paragraphs-component-form > .form-actions a.button');
       $buttons.each(function (_i, el) {
@@ -396,13 +400,18 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
       }
     }
   });
+  var lpDialogInterval;
   $(window).on('dialog:aftercreate', function (event, dialog, $dialog) {
     if ($dialog[0].id.indexOf('lpb-dialog-') === 0) {
+      if (lpDialogInterval) {
+        clearInterval(lpDialogInterval);
+      }
+
       $dialog.data('lpOriginalHeight', $dialog.outerHeight());
-      $dialog.data('lpDialogInterval', setInterval(repositionDialog.bind(null, $dialog), 500));
+      lpDialogInterval = setInterval(repositionDialog.bind(null, $dialog), 500);
     }
   });
-  $(window).on('dialog:beforeclose', function (event, dialog, $dialog) {
-    clearInterval($dialog.data('lpDialogInterval'));
+  $(window).on('dialog:beforeclose', function () {
+    clearInterval(lpDialogInterval);
   });
 })(jQuery, Drupal, Drupal.debounce, dragula);
\ No newline at end of file
-- 
GitLab