2 * @file entity_browser.view.js
4 * Defines the behavior of the entity browser's view widget.
7 (function ($, Drupal, drupalSettings) {
12 * Registers behaviours related to view widget.
14 Drupal.behaviors.entityBrowserView = {
15 attach: function (context) {
16 // Add the AJAX to exposed forms.
17 // We do this as the selector in core/modules/views/js/ajax_view.js
18 // assumes that the form the exposed filters reside in has a
19 // views-related ID, which ours does not.
20 var views_instance = Drupal.views.instances[Object.keys(Drupal.views.instances)[0]];
22 // Initialize the exposed form AJAX.
23 views_instance.$exposed_form = $('div#views-exposed-form-' + views_instance.settings.view_name.replace(/_/g, '-') + '-' + views_instance.settings.view_display_id.replace(/_/g, '-'));
24 views_instance.$exposed_form.once('exposed-form').each(jQuery.proxy(views_instance.attachExposedFormAjax, views_instance));
26 // The form values form_id, form_token, and form_build_id will break
27 // the exposed form. Remove them by splicing the end of form_values.
28 if (views_instance.exposedFormAjax && views_instance.exposedFormAjax.length > 0) {
29 var ajax = views_instance.exposedFormAjax[0];
30 ajax.options.beforeSubmit = function (form_values, element_settings, options) {
31 form_values = form_values.splice(form_values.length - 3, 3);
33 return ajax.beforeSubmit(form_values, element_settings, options);
37 // Handle Enter key press in the views exposed form text fields: ensure
38 // that the correct button is used for the views exposed form submit.
39 // The default browser behavior for the Enter key press is to click the
40 // first found button. But there can be other buttons in the form, for
41 // example, ones added by the Tabs widget selector plugin.
42 views_instance.$exposed_form.once('submit-by-enter-key').find('input[type="text"]').each(function () {
43 $(this).on('keypress', function (event) {
44 if (event.keyCode == 13) {
45 event.preventDefault();
46 views_instance.$exposed_form.find('input[type="submit"]').first().click();
51 // If "auto_select" functionality is enabled, then selection column is
52 // hidden and click on row will actually add element into selection
53 // display over javascript event. Currently only multistep display
54 // supports that functionality.
55 if (drupalSettings.entity_browser_widget.auto_select) {
56 var selection_cells = views_instance.$view.find('.views-field-entity-browser-select');
58 // Register on cell parents (rows) click event.
59 selection_cells.parent()
60 .once('register-row-click')
61 .click(function (event) {
62 event.preventDefault();
66 // Ensure to use input (checkbox) field from entity browser
67 // column dedicated for selection checkbox.
68 var $input = $row.find('.views-field-entity-browser-select input.form-checkbox');
70 // Get selection display element and trigger adding of entity
73 .find('.entities-list')
74 .trigger('add-entities', [[$input.val()]]);
77 // Hide selection cells (selection column) with checkboxes.
78 selection_cells.hide();
84 }(jQuery, Drupal, drupalSettings));