Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / js / entity_browser.entity_reference.js
1 /**
2  * @file entity_browser.entity_reference.js
3  *
4  * Defines the behavior of the entity reference widget that utilizes entity
5  * browser.
6  */
7
8 (function ($, Drupal) {
9
10   'use strict';
11
12   /**
13    * Registers behaviours related to entity reference field widget.
14    */
15   Drupal.behaviors.entityBrowserEntityReference = {
16     attach: function (context) {
17       $(context).find('.field--widget-entity-browser-entity-reference').each(function () {
18         $(this).find('.entities-list.sortable').sortable({
19           stop: Drupal.entityBrowserEntityReference.entitiesReordered
20         });
21       });
22       // The AJAX callback will give us a flag when we need to re-open the
23       // browser, most likely due to a "Replace" button being clicked.
24       if (typeof drupalSettings.entity_browser_reopen_browser !== 'undefined' &&  drupalSettings.entity_browser_reopen_browser) {
25         var data_drupal_selector = '[data-drupal-selector^="edit-' + drupalSettings.entity_browser_reopen_browser.replace(/_/g, '-') + '-entity-browser-entity-browser-' + '"]';
26         var $launch_browser_element = $(context).find(data_drupal_selector);
27         if (!drupalSettings.entity_browser.iframe[$launch_browser_element.attr('data-uuid')].auto_open) {
28           $launch_browser_element.click();
29         }
30         // In case this is inside a fieldset closed by default, open it so the
31         // user doesn't need to guess the browser is open but hidden there.
32         var $fieldset_summary = $launch_browser_element.closest('details').find('summary');
33         if ($fieldset_summary.length && $fieldset_summary.attr('aria-expanded') === 'false') {
34           $fieldset_summary.click();
35         }
36       }
37     }
38   };
39
40   Drupal.entityBrowserEntityReference = {};
41
42   /**
43    * Reacts on sorting of the entities.
44    *
45    * @param {object} event
46    *   Event object.
47    * @param {object} ui
48    *   Object with detailed information about the sort event.
49    */
50   Drupal.entityBrowserEntityReference.entitiesReordered = function (event, ui) {
51     var items = $(this).find('.item-container');
52     var ids = [];
53     for (var i = 0; i < items.length; i++) {
54       ids[i] = $(items[i]).attr('data-entity-id');
55     }
56
57     $(this).parent().parent().find('input[type*=hidden][name*="[target_id]"]').val(ids.join(' '));
58   };
59
60 }(jQuery, Drupal));