Verified Commit 99af3458 authored by Dave Long's avatar Dave Long
Browse files

Issue #3390549 by finnsky, nod_, smustgrave, larowlan, catch, bnjmnm: Get rid...

Issue #3390549 by finnsky, nod_, smustgrave, larowlan, catch, bnjmnm: Get rid of jQuery in dialog events
parent a244fdde
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -549,6 +549,7 @@ drupal.debounce:
drupal.dialog:
  version: VERSION
  js:
    misc/dialog/dialog-deprecation.js: {}
    misc/dialog/dialog.js: {}
    misc/dialog/dialog.position.js: {}
    misc/dialog/dialog.jquery-ui.js: {}
@@ -582,6 +583,7 @@ drupal.dialog:
    - core/drupal.debounce
    - core/drupal.displace
    - core/tabbable.jquery.shim
    - core/once
    - core/drupal.jquery.position

drupal.dialog.ajax:
+40 −0
Original line number Diff line number Diff line
/**
 * @file
 * Maintains and deprecates Dialog jQuery events.
 */

(function ($, Drupal, once) {
  if (once('drupal-dialog-deprecation-listener', 'html').length) {
    const eventSpecial = {
      handle($event) {
        const $element = $($event.target);
        const event = $event.originalEvent;
        const dialog = event.dialog;
        const dialogArguments = [$event, dialog, $element, event?.settings];
        $event.handleObj.handler.apply(this, dialogArguments);
      },
    };

    $.event.special['dialog:beforecreate'] = eventSpecial;
    $.event.special['dialog:aftercreate'] = eventSpecial;
    $.event.special['dialog:beforeclose'] = eventSpecial;
    $.event.special['dialog:afterclose'] = eventSpecial;

    const listenDialogEvent = (event) => {
      const windowEvents = $._data(window, 'events');
      const isWindowHasDialogListener = windowEvents[event.type];
      if (isWindowHasDialogListener) {
        Drupal.deprecationError({
          message: `jQuery event ${event.type} is deprecated in 10.3.0 and is removed from Drupal:12.0.0. See https://www.drupal.org/node/3422670`,
        });
      }
    };

    [
      'dialog:beforecreate',
      'dialog:aftercreate',
      'dialog:beforeclose',
      'dialog:afterclose',
    ].forEach((e) => window.addEventListener(e, listenDialogEvent));
  }
})(jQuery, Drupal, once);
+5 −2
Original line number Diff line number Diff line
@@ -270,7 +270,9 @@
   * @param {object} [settings]
   *   Dialog settings.
   */
  $(window).on('dialog:aftercreate', (e, dialog, $element, settings) => {
  window.addEventListener('dialog:aftercreate', (event) => {
    const $element = $(event.target);
    const dialog = event.dialog;
    $element.on('click.dialog', '.dialog-cancel', (e) => {
      dialog.close('cancel');
      e.preventDefault();
@@ -288,7 +290,8 @@
   * @param {jQuery} $element
   *   jQuery collection of the dialog element.
   */
  $(window).on('dialog:beforeclose', (e, dialog, $element) => {
  window.addEventListener('dialog:beforeclose', (e) => {
    const $element = $(e.target);
    $element.off('.dialog');
  });

+22 −6
Original line number Diff line number Diff line
@@ -5,6 +5,14 @@
 * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#the-dialog-element
 */

class DrupalDialogEvent extends Event {
  constructor(type, dialog, settings = null) {
    super(`dialog:${type}`, { bubbles: true });
    this.dialog = dialog;
    this.settings = settings;
  }
}

(function ($, Drupal, drupalSettings, bodyScrollLock) {
  /**
   * Default dialog options.
@@ -61,7 +69,10 @@
   */
  Drupal.dialog = function (element, options) {
    let undef;

    const $element = $(element);
    const domElement = $element.get(0);

    const dialog = {
      open: false,
      returnValue: undef,
@@ -70,28 +81,33 @@
    function openDialog(settings) {
      settings = $.extend({}, drupalSettings.dialog, options, settings);
      // Trigger a global event to allow scripts to bind events to the dialog.
      $(window).trigger('dialog:beforecreate', [dialog, $element, settings]);
      $element.dialog(settings);
      const event = new DrupalDialogEvent('beforecreate', dialog, settings);
      domElement.dispatchEvent(event);
      $element.dialog(event.settings);
      dialog.open = true;

      // Locks the body scroll only when it opens in modal.
      if (settings.modal) {
        // Locks the body when the dialog opens.
        bodyScrollLock.lock($element.get(0));
        bodyScrollLock.lock(domElement);
      }

      $(window).trigger('dialog:aftercreate', [dialog, $element, settings]);
      domElement.dispatchEvent(
        new DrupalDialogEvent('aftercreate', dialog, settings),
      );
    }

    function closeDialog(value) {
      $(window).trigger('dialog:beforeclose', [dialog, $element]);
      domElement.dispatchEvent(new DrupalDialogEvent('beforeclose', dialog));

      // Unlocks the body when the dialog closes.
      bodyScrollLock.clearBodyLocks();

      $element.dialog('close');
      dialog.returnValue = value;
      dialog.open = false;
      $(window).trigger('dialog:afterclose', [dialog, $element]);

      domElement.dispatchEvent(new DrupalDialogEvent('afterclose', dialog));
    }

    dialog.show = () => {
+25 −23
Original line number Diff line number Diff line
@@ -113,10 +113,12 @@
      .trigger('dialogContentResize');
  }

  $(window).on({
    'dialog:aftercreate': function (event, dialog, $element, settings) {
  window.addEventListener('dialog:aftercreate', (e) => {
    const autoResize = debounce(resetSize, 20);
    const $element = $(e.target);
    const { settings } = e;
    const eventData = { settings, $element };

    if (settings.autoResize === true || settings.autoResize === 'true') {
      const uiDialog = $element
        .dialog('option', { resizable: false, draggable: false })
@@ -131,10 +133,10 @@
        autoResize,
      );
    }
    },
    'dialog:beforeclose': function (event, dialog, $element) {
  });

  window.addEventListener('dialog:beforeclose', () => {
    $(window).off('.dialogResize');
    $(document).off('.dialogResize');
    },
  });
})(jQuery, Drupal, drupalSettings, Drupal.debounce, Drupal.displace);
Loading