d28e1761d03bca22f4d8b1b4039eb099536f858c
[yaffs-website] / web / modules / contrib / entity_browser / js / entity_browser.common.js
1 /**
2  * @file entity_browser.common.js
3  *
4  * Common helper functions used by various parts of entity browser.
5  */
6
7 (function ($, Drupal, drupalSettings) {
8
9   'use strict';
10
11   Drupal.entityBrowser = {};
12
13   /**
14    * Command to refresh an entity_browser_entity_reference field widget.
15    *
16    * @param {Drupal.Ajax} [ajax]
17    *   The ajax object.
18    * @param {object} response
19    *   Object holding the server response.
20    * @param {string} response.details_id
21    *   The ID for the details element.
22    * @param {number} [status]
23    *   The HTTP status code.
24    */
25   Drupal.AjaxCommands.prototype.entity_browser_value_updated = function (ajax, response, status) {
26     $('#' + response.details_id)
27       .find('input[type="hidden"][name$="[target_id]"]')
28       .trigger('entity_browser_value_updated');
29   };
30
31   /**
32    * Reacts on "entities selected" event.
33    *
34    * @param {object} event
35    *   Event object.
36    * @param {string} uuid
37    *   Entity browser UUID.
38    * @param {array} entities
39    *   Array of selected entities.
40    */
41   Drupal.entityBrowser.selectionCompleted = function (event, uuid, entities) {
42     var selected_entities = $.map(entities, function (item) {
43       return item[2] + ':' + item[0];
44     });
45     // @todo Use uuid here. But for this to work we need to move eb uuid
46     // generation from display to eb directly. When we do this, we can change
47     // \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::formElement
48     // also.
49     // Checking if cardinality is set - assume unlimited.
50     var cardinality = isNaN(parseInt(drupalSettings['entity_browser'][uuid]['cardinality'])) ? -1 : parseInt(drupalSettings['entity_browser'][uuid]['cardinality']);
51
52     // Get field widget selection mode.
53     var selection_mode = drupalSettings['entity_browser'][uuid]['selection_mode'];
54
55     // Update value form element with new entity IDs.
56     var selector = drupalSettings['entity_browser'][uuid]['selector'] ? $(drupalSettings['entity_browser'][uuid]['selector']) : $(this).parent().parent().find('input[type*=hidden]');
57     var entity_ids = selector.val();
58     var existing_entities = (entity_ids.length !== 0) ? entity_ids.split(' ') : [];
59
60     entity_ids = Drupal.entityBrowser.updateEntityIds(
61       existing_entities,
62       selected_entities,
63       selection_mode,
64       cardinality
65     );
66
67     selector.val(entity_ids);
68     selector.trigger('entity_browser_value_updated');
69   };
70
71   /**
72    * Updates the list of selected entities.
73    *
74    * It uses existing selection and selected entities in entity browser. Also
75    * considers cardinality and used selection mode.
76    *
77    * Note: Selection modes are defined in EntityBrowserElement class and same
78    * options should be used here to determine what action will be performed.
79    * Default action is append ('selection_append').
80    *
81    * @param {Array} existing_entities
82    *   List of existing entity IDs.
83    * @param {Array} selected_entities
84    *   The entities that are selected and entity browser.
85    * @param {string} selection_mode
86    *   Selection mode defined by entity browser field widget.
87    * @param {int} cardinality
88    *   The maximal amount of items the field can store.
89    *
90    * @return {string}
91    *   List of entities as a string, separated by space.
92    */
93   Drupal.entityBrowser.updateEntityIds = function (existing_entities, selected_entities, selection_mode, cardinality) {
94     var combined_entities;
95
96     if (selection_mode === 'selection_edit') {
97       // Propagate new selected entities.
98       combined_entities = selected_entities;
99     }
100     else if (selection_mode === 'selection_prepend') {
101       // Prepend selected entities to existing list of entities.
102       combined_entities = selected_entities.concat(existing_entities);
103     }
104     else {
105       // Append selected entities to existing list of entities.
106       combined_entities = existing_entities.concat(selected_entities);
107     }
108
109     // Having more elements than cardinality should never happen, because
110     // server side authentication should prevent it, but we handle it here
111     // anyway.
112     if (cardinality > 0 && combined_entities.length > cardinality) {
113       combined_entities = combined_entities.slice(0, cardinality);
114     }
115
116     return combined_entities.join(' ');
117   };
118
119   /**
120    * Reacts on "entities selected" event.
121    *
122    * @param {object} element
123    *   Element to bind on.
124    * @param {array} callbacks
125    *   List of callbacks.
126    * @param {string} event_name
127    *   Name of event to bind to.
128    */
129   Drupal.entityBrowser.registerJsCallbacks = function (element, callbacks, event_name) {
130     // JS callbacks are registred as strings. We need to split their names and
131     // find actual functions.
132     for (var i = 0; i < callbacks.length; i++) {
133       var callback = callbacks[i].split('.');
134       var fn = window;
135
136       for (var j = 0; j < callback.length; j++) {
137         fn = fn[callback[j]];
138       }
139
140       if (typeof fn === 'function') {
141         $(element).bind(event_name, fn);
142       }
143     }
144   };
145
146 }(jQuery, Drupal, drupalSettings));