921bebd682354b0d472948d7aa3591aa61892bda
[yaffs-website] / web / modules / contrib / entity_browser / js / entity_browser.view.js
1 /**
2  * @file entity_browser.view.js
3  *
4  * Defines the behavior of the entity browser's view widget.
5  */
6
7 (function ($, Drupal, drupalSettings) {
8
9   'use strict';
10
11   /**
12    * Registers behaviours related to view widget.
13    */
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]];
21       if (views_instance) {
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));
25
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);
32             ajax.ajaxing = true;
33             return ajax.beforeSubmit(form_values, element_settings, options);
34           };
35         }
36
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();
47             }
48           });
49         });
50
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');
57
58           // Register on cell parents (rows) click event.
59           selection_cells.parent()
60             .once('register-row-click')
61             .click(function (event) {
62               event.preventDefault();
63
64               var $row = $(this);
65
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');
69
70               // Get selection display element and trigger adding of entity
71               // over ajax request.
72               $row.parents('form')
73                 .find('.entities-list')
74                 .trigger('add-entities', [[$input.val()]]);
75             });
76
77           // Hide selection cells (selection column) with checkboxes.
78           selection_cells.hide();
79         }
80       }
81     }
82   };
83
84 }(jQuery, Drupal, drupalSettings));