Verified Commit d3815233 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3203920 by larowlan, nod_, bnjmnm, vsujeetkumar, smustgrave, nicxvan,...

Issue #3203920 by larowlan, nod_, bnjmnm, vsujeetkumar, smustgrave, nicxvan, longwave: Replace Contextual Links BackboneJS usage with VanillaJS equivalent
parent bedda6ac
Loading
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -15835,12 +15835,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/contextual/tests/src/FunctionalJavascript/ContextualLinksTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Variable \\$unrestricted_tab_count might not be defined\\.$#',
	'identifier' => 'variable.undefined',
	'count' => 1,
	'path' => __DIR__ . '/modules/contextual/tests/src/FunctionalJavascript/EditModeTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\datetime\\\\DateTimeComputed\\:\\:setValue\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+3 −17
Original line number Diff line number Diff line
drupal.contextual-links:
  version: VERSION
  js:
    js/contextualModelView.js: {}
    # Ensure to run before contextual/drupal.context-toolbar.
    # Core.
    js/contextual.js: { weight: -2 }
    # Models.
    js/models/StateModel.js: { weight: -2 }
    # Views.
    js/views/AuralView.js: { weight: -2 }
    js/views/KeyboardView.js: { weight: -2 }
    js/views/RegionView.js: { weight: -2 }
    js/views/VisualView.js: { weight: -2 }
  css:
    component:
      css/contextual.module.css: {}
@@ -22,28 +15,21 @@ drupal.contextual-links:
    - core/drupal
    - core/drupal.ajax
    - core/drupalSettings
    # @todo Remove this in https://www.drupal.org/project/drupal/issues/3203920
    - core/internal.backbone
    - core/once
    - core/drupal.touchevents-test

drupal.contextual-toolbar:
  version: VERSION
  js:
    js/toolbar/contextualToolbarModelView.js: {}
    js/contextual.toolbar.js: {}
    # Models.
    js/toolbar/models/StateModel.js: {}
    # Views.
    js/toolbar/views/AuralView.js: {}
    js/toolbar/views/VisualView.js: {}
  css:
    component:
      css/contextual.toolbar.css: {}
  dependencies:
    - core/jquery
    - contextual/drupal.contextual-links
    - core/drupal
    # @todo Remove this in https://www.drupal.org/project/drupal/issues/3203920
    - core/internal.backbone
    - core/once
    - core/drupal.tabbingmanager
    - core/drupal.announce
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@
  left: 0;
}

.contextual.open {
  z-index: 501;
}

/**
 * Contextual region.
 */
+31 −68
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
 * Attaches behaviors for the Contextual module.
 */

(function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) {
(function ($, Drupal, drupalSettings, JSON, storage) {
  const options = $.extend(
    drupalSettings.contextual,
    // Merge strings on top of drupalSettings so that they are not mutable.
@@ -14,18 +14,15 @@
      },
    },
  );

  // Clear the cached contextual links whenever the current user's set of
  // permissions changes.
  const cachedPermissionsHash = storage.getItem(
    'Drupal.contextual.permissionsHash',
  );
  const permissionsHash = drupalSettings.user.permissionsHash;
  const { permissionsHash } = drupalSettings.user;
  if (cachedPermissionsHash !== permissionsHash) {
    if (typeof permissionsHash === 'string') {
      _.chain(storage)
        .keys()
        .each((key) => {
      Object.keys(storage).forEach((key) => {
        if (key.startsWith('Drupal.contextual.')) {
          storage.removeItem(key);
        }
@@ -87,7 +84,7 @@
   */
  function initContextual($contextual, html) {
    const $region = $contextual.closest('.contextual-region');
    const contextual = Drupal.contextual;
    const { contextual } = Drupal;

    $contextual
      // Update the placeholder to contain its rendered contextual links.
@@ -107,46 +104,18 @@
      const glue = url.includes('?') ? '&' : '?';
      this.setAttribute('href', url + glue + destination);
    });

    let title = '';
    const $regionHeading = $region.find('h2');
    if ($regionHeading.length) {
      title = $regionHeading[0].textContent.trim();
    }
    // Create a model and the appropriate views.
    const model = new contextual.StateModel({
      title,
    });
    const viewOptions = $.extend({ el: $contextual, model }, options);
    contextual.views.push({
      visual: new contextual.VisualView(viewOptions),
      aural: new contextual.AuralView(viewOptions),
      keyboard: new contextual.KeyboardView(viewOptions),
    });
    contextual.regionViews.push(
      new contextual.RegionView($.extend({ el: $region, model }, options)),
    );

    // Add the model to the collection. This must happen after the views have
    // been associated with it, otherwise collection change event handlers can't
    // trigger the model change event handler in its views.
    contextual.collection.add(model);

    // Let other JavaScript react to the adding of a new contextual link.
    $(document).trigger(
      'drupalContextualLinkAdded',
      Drupal.deprecatedProperty({
        target: {
          $el: $contextual,
    options.title = title;
    const contextualModelView = new Drupal.contextual.ContextualModelView(
      $contextual,
      $region,
          model,
        },
        deprecatedProperty: 'model',
        message:
          'The model property is deprecated in drupal:9.4.0 and is removed from drupal:12.0.0. There is no replacement.',
      }),
      options,
    );

    contextual.instances.push(contextualModelView);
    // Fix visual collisions between contextual link triggers.
    adjustIfNestedAndOverlapping($contextual);
  }
@@ -192,7 +161,7 @@
          // Initialize after the current execution cycle, to make the AJAX
          // request for retrieving the uncached contextual links as soon as
          // possible, but also to ensure that other Drupal behaviors have had
          // the chance to set up an event listener on the Backbone collection
          // the chance to set up an event listener on the collection
          // Drupal.contextual.collection.
          window.setTimeout(() => {
            initContextual(
@@ -217,7 +186,7 @@
          data: { 'ids[]': uncachedIDs, 'tokens[]': uncachedTokens },
          dataType: 'json',
          success(results) {
            _.each(results, (html, contextualID) => {
            Object.entries(results).forEach(([contextualID, html]) => {
              // Store the metadata.
              storage.setItem(`Drupal.contextual.${contextualID}`, html);
              // If the rendered contextual links are empty, then the current
@@ -274,20 +243,22 @@
     *  replacement.
     */
    regionViews: [],
    instances: new Proxy([], {
      set: function set(obj, prop, value) {
        obj[prop] = value;
        window.dispatchEvent(new Event('contextual-instances-added'));
        return true;
      },
      deleteProperty(target, prop) {
        if (prop in target) {
          delete target[prop];
          window.dispatchEvent(new Event('contextual-instances-removed'));
        }
      },
    }),
    ContextualModelView: {},
  };

  /**
   * A Backbone.Collection of {@link Drupal.contextual.StateModel} instances.
   *
   * @type {Backbone.Collection}
   *
   * @deprecated in drupal:9.4.0 and is removed from drupal:12.0.0. There is no
   *  replacement.
   */
  Drupal.contextual.collection = new Backbone.Collection([], {
    model: Drupal.contextual.StateModel,
  });

  /**
   * A trigger is an interactive element often bound to a click handler.
   *
@@ -311,12 +282,4 @@
  $(document).on('drupalContextualLinkAdded', (event, data) => {
    Drupal.ajax.bindAjaxLinks(data.$el[0]);
  });
})(
  jQuery,
  Drupal,
  drupalSettings,
  _,
  Backbone,
  window.JSON,
  window.sessionStorage,
);
})(jQuery, Drupal, drupalSettings, window.JSON, window.sessionStorage);
+9 −26
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
 * Attaches behaviors for the Contextual module's edit toolbar tab.
 */

(function ($, Drupal, Backbone) {
(function ($, Drupal) {
  const strings = {
    tabbingReleased: Drupal.t(
      'Tabbing is no longer constrained by the Contextual module.',
@@ -21,33 +21,19 @@
   *   A contextual links DOM element as rendered by the server.
   */
  function initContextualToolbar(context) {
    if (!Drupal.contextual || !Drupal.contextual.collection) {
    if (!Drupal.contextual || !Drupal.contextual.instances) {
      return;
    }

    const contextualToolbar = Drupal.contextualToolbar;
    contextualToolbar.model = new contextualToolbar.StateModel(
      {
        // Checks whether localStorage indicates we should start in edit mode
        // rather than view mode.
        // @see Drupal.contextualToolbar.VisualView.persist
        isViewing:
          document.querySelector('body .contextual-region') === null ||
          localStorage.getItem('Drupal.contextualToolbar.isViewing') !==
            'false',
      },
      {
        contextualCollection: Drupal.contextual.collection,
      },
    );
    const { contextualToolbar } = Drupal;

    const viewOptions = {
      el: $('.toolbar .toolbar-bar .contextual-toolbar-tab'),
      model: contextualToolbar.model,
      strings,
    };
    new contextualToolbar.VisualView(viewOptions);
    new contextualToolbar.AuralView(viewOptions);
    contextualToolbar.model = new Drupal.contextual.ContextualToolbarModelView(
      viewOptions,
    );
  }

  /**
@@ -75,13 +61,10 @@
   */
  Drupal.contextualToolbar = {
    /**
     * The {@link Drupal.contextualToolbar.StateModel} instance.
     *
     * @type {?Drupal.contextualToolbar.StateModel}
     * The {@link Drupal.contextual.ContextualToolbarModelView} instance.
     *
     * @deprecated in drupal:9.4.0 and is removed from drupal:12.0.0. There is
     * no replacement.
     * @type {?Drupal.contextual.ContextualToolbarModelView}
     */
    model: null,
  };
})(jQuery, Drupal, Backbone);
})(jQuery, Drupal);
Loading