2 * @file entity_browser.common.js
4 * Common helper functions used by various parts of entity browser.
7 (function ($, Drupal, drupalSettings) {
11 Drupal.entityBrowser = {};
14 * Command to refresh an entity_browser_entity_reference field widget.
16 * @param {Drupal.Ajax} [ajax]
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.
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');
32 * Reacts on "entities selected" event.
34 * @param {object} event
36 * @param {string} uuid
37 * Entity browser UUID.
38 * @param {array} entities
39 * Array of selected entities.
41 Drupal.entityBrowser.selectionCompleted = function (event, uuid, entities) {
42 var selected_entities = $.map(entities, function (item) {
43 return item[2] + ':' + item[0];
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
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']);
52 // Get field widget selection mode.
53 var selection_mode = drupalSettings['entity_browser'][uuid]['selection_mode'];
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(' ') : [];
60 entity_ids = Drupal.entityBrowser.updateEntityIds(
67 selector.val(entity_ids);
68 selector.trigger('entity_browser_value_updated');
72 * Updates the list of selected entities.
74 * It uses existing selection and selected entities in entity browser. Also
75 * considers cardinality and used selection mode.
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').
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.
91 * List of entities as a string, separated by space.
93 Drupal.entityBrowser.updateEntityIds = function (existing_entities, selected_entities, selection_mode, cardinality) {
94 var combined_entities;
96 if (selection_mode === 'selection_edit') {
97 // Propagate new selected entities.
98 combined_entities = selected_entities;
100 else if (selection_mode === 'selection_prepend') {
101 // Prepend selected entities to existing list of entities.
102 combined_entities = selected_entities.concat(existing_entities);
105 // Append selected entities to existing list of entities.
106 combined_entities = existing_entities.concat(selected_entities);
109 // Having more elements than cardinality should never happen, because
110 // server side authentication should prevent it, but we handle it here
112 if (cardinality > 0 && combined_entities.length > cardinality) {
113 combined_entities = combined_entities.slice(0, cardinality);
116 return combined_entities.join(' ');
120 * Reacts on "entities selected" event.
122 * @param {object} element
123 * Element to bind on.
124 * @param {array} callbacks
126 * @param {string} event_name
127 * Name of event to bind to.
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('.');
136 for (var j = 0; j < callback.length; j++) {
137 fn = fn[callback[j]];
140 if (typeof fn === 'function') {
141 $(element).bind(event_name, fn);
146 }(jQuery, Drupal, drupalSettings));