diff --git a/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js b/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js index 938d5de3..b4c6cee4 100644 --- a/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js +++ b/modules/panopoly/panopoly_magic/js/panopoly-magic-preview.js @@ -21,9 +21,16 @@ 27 // esc ]; - const $form = $('form#layout-builder-update-block'); + const form_selector = 'form#layout-builder-update-block'; + // Finds matching children or the current context itself. + // @see https://stackoverflow.com/questions/2828019/looking-for-jquery-find-method-that-includes-the-current-node + const $form = $(context).find(form_selector).addBack(form_selector); const $preview = $form.find('.panopoly-magic-live-preview'); + if ($form.length === 0 || $preview.length === 0) { + return; + } + // Used to only update preview after changes stop. let timer; @@ -62,7 +69,6 @@ CKEDITOR.on('instanceCreated', function(e) { let editor = e.editor, element = e.element; - console.log('something done instanciated'); editor.on('change', triggerSubmit); }); } @@ -70,7 +76,36 @@ // Checkboxes and radios. $form.find('[type="checkbox"], [type="radio"]').on('change', function () { triggerSubmit(); - }) + }); + +// $(window).once('panopoly-magic-preview').on('dialog:beforeclose', function (event, dialog, $element) { +// console.log('dialog.beforeclose'); +// console.log(event); +// console.log(event); +// console.log(dialog); +// console.log($element); +// if (Drupal.offCanvas.isOffCanvas($element) && $(event.currentTarget).hasClass('ui-dialog-titlebar-close')) { +// console.log('is off canvas'); +// // Rather than refer to $form, this sets this event handler only once, and then looks up if there is a cancel +// // button available on the right form. This way we don't have to worry about attaching and detaching this +// // event handler. +// $(form_selector).find('[data-drupal-selector="edit-actions-cancel"]').trigger('mousedown'); +// event.preventDefault(); +// return false; +// } +// }); + + // Trigger "Cancel" button when off canvas tray closed. + $('.ui-dialog-off-canvas .ui-dialog-titlebar-close').once('panopoly-magic-preview').mousedown(function (e) { + console.log('clicked it'); + // Rather than refer to $form, this sets this event handler only once, and then looks up if there is a cancel + // button available on the right form. This way we don't have to worry about attaching and detaching this + // event handler. + $(form_selector).find('[data-drupal-selector="edit-actions-cancel"]').trigger('mousedown'); + e.preventDefault(); + return false; + }); + } }