Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / settings_tray / js / settings_tray.es6.js
index fce4c3ccbb3896f2c923357900d67bd947541f25..6487690c7bc99cda9143b7d6457109793048894a 100644 (file)
@@ -5,7 +5,7 @@
  * @private
  */
 
-(function ($, Drupal) {
+(($, Drupal) => {
   const blockConfigureSelector = '[data-settings-tray-edit]';
   const toggleEditSelector = '[data-drupal-settingstray="toggle"]';
   const itemsToToggleSelector = '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button';
       if ($editables.length) {
         // Use event capture to prevent clicks on links.
         document.querySelector('[data-off-canvas-main-canvas]').addEventListener('click', preventClick, true);
-
-        // When a click occurs try and find the settings-tray edit link
-        // and click it.
+        /**
+         * When a click occurs try and find the settings-tray edit link
+         * and click it.
+         */
         $editables
           .not(contextualItemsSelector)
           .on('click.settingstray', (e) => {
     setEditModeState(!isInEditMode());
   }
 
+  /**
+   * Prepares Ajax links to work with off-canvas and Settings Tray module.
+   */
+  function prepareAjaxLinks() {
+    // Find all Ajax instances that use the 'off_canvas' renderer.
+    Drupal.ajax.instances
+      /**
+       * If there is an element and the renderer is 'off_canvas' then we want
+       * to add our changes.
+       */
+      .filter(instance => instance && $(instance.element).attr('data-dialog-renderer') === 'off_canvas')
+      /**
+       * Loop through all Ajax instances that use the 'off_canvas' renderer to
+       * set active editable ID.
+       */
+      .forEach((instance) => {
+        // Check to make sure existing dialogOptions aren't overridden.
+        if (!instance.options.data.hasOwnProperty('dialogOptions')) {
+          instance.options.data.dialogOptions = {};
+        }
+        instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id');
+        instance.progress = { type: 'fullscreen' };
+      });
+  }
+
   /**
    * Reacts to contextual links being added.
    *
    * @listens event:drupalContextualLinkAdded
    */
   $(document).on('drupalContextualLinkAdded', (event, data) => {
+    /**
+     * When contextual links are add we need to set extra properties on the
+     * instances in Drupal.ajax.instances for them to work with Edit Mode.
+     */
+    prepareAjaxLinks();
 
     // When the first contextual link is added to the page set Edit Mode.
     $('body').once('settings_tray.edit_mode_init').each(() => {
       }
     });
 
-    /**
-     * Bind Ajax behaviors to all items showing the class.
-     * @todo Fix contextual links to work with use-ajax links in
-     * https://www.drupal.org/node/2764931.
-     */
-    Drupal.attachBehaviors(data.$el[0]);
     /**
      * Bind a listener to all 'Quick edit' links for blocks. Click "Edit"
      * button in toolbar to force Contextual Edit which starts Settings Tray
   Drupal.behaviors.toggleEditMode = {
     attach() {
       $(toggleEditSelector).once('settingstray').on('click.settingstray', toggleEditMode);
-      // Find all Ajax instances that use the 'off_canvas' renderer.
-      Drupal.ajax.instances
-        // If there is an element and the renderer is 'off_canvas' then we want
-        // to add our changes.
-        .filter(instance => instance && $(instance.element).attr('data-dialog-renderer') === 'off_canvas')
-        // Loop through all Ajax instances that use the 'off_canvas' renderer to
-        // set active editable ID.
-        .forEach((instance) => {
-          // Check to make sure existing dialogOptions aren't overridden.
-          if (!('dialogOptions' in instance.options.data)) {
-            instance.options.data.dialogOptions = {};
-          }
-          instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id');
-          instance.progress = { type: 'fullscreen' };
-        });
     },
   };
 
       }
     },
   });
-}(jQuery, Drupal));
+})(jQuery, Drupal);