3 * Some basic behaviors and utility functions for Views UI.
6 (function($, Drupal, drupalSettings) {
13 * Improve the user experience of the views edit interface.
15 * @type {Drupal~behavior}
17 * @prop {Drupal~behaviorAttach} attach
18 * Attaches toggling of SQL rewrite warning on the corresponding checkbox.
20 Drupal.behaviors.viewsUiEditView = {
22 // Only show the SQL rewrite warning when the user has chosen the
23 // corresponding checkbox.
24 $('[data-drupal-selector="edit-query-options-disable-sql-rewrite"]').on(
27 $('.sql-rewrite-warning').toggleClass('js-hide');
34 * In the add view wizard, use the view name to prepopulate form fields such
35 * as page title and menu link.
37 * @type {Drupal~behavior}
39 * @prop {Drupal~behaviorAttach} attach
40 * Attaches behavior for prepopulating page title and menu links, based on
43 Drupal.behaviors.viewsUiAddView = {
45 const $context = $(context);
46 // Set up regular expressions to allow only numbers, letters, and dashes.
47 const exclude = new RegExp('[^a-z0-9\\-]+', 'g');
51 // The page title, block title, and menu link fields can all be
52 // prepopulated with the view name - no regular expression needed.
53 const $fields = $context.find(
54 '[id^="edit-page-title"], [id^="edit-block-title"], [id^="edit-page-link-properties-title"]',
57 if (!this.fieldsFiller) {
58 this.fieldsFiller = new Drupal.viewsUi.FormFieldFiller($fields);
60 // After an AJAX response, this.fieldsFiller will still have event
61 // handlers bound to the old version of the form fields (which don't
62 // exist anymore). The event handlers need to be unbound and then
63 // rebound to the new markup. Note that jQuery.live is difficult to
64 // make work in this case because the IDs of the form fields change
65 // on every AJAX response.
66 this.fieldsFiller.rebind($fields);
70 // Prepopulate the path field with a URLified version of the view name.
71 const $pathField = $context.find('[id^="edit-page-path"]');
72 if ($pathField.length) {
73 if (!this.pathFiller) {
74 this.pathFiller = new Drupal.viewsUi.FormFieldFiller(
80 this.pathFiller.rebind($pathField);
84 // Populate the RSS feed field with a URLified version of the view name,
85 // and an .xml suffix (to make it unique).
86 const $feedField = $context.find(
87 '[id^="edit-page-feed-properties-path"]',
89 if ($feedField.length) {
90 if (!this.feedFiller) {
92 this.feedFiller = new Drupal.viewsUi.FormFieldFiller(
99 this.feedFiller.rebind($feedField);
106 * Constructor for the {@link Drupal.viewsUi.FormFieldFiller} object.
108 * Prepopulates a form field based on the view name.
112 * @param {jQuery} $target
113 * A jQuery object representing the form field or fields to prepopulate.
114 * @param {bool} [exclude=false]
115 * A regular expression representing characters to exclude from
117 * @param {string} [replace='']
118 * A string to use as the replacement value for disallowed characters.
119 * @param {string} [suffix='']
120 * A suffix to append at the end of the target field content.
122 Drupal.viewsUi.FormFieldFiller = function($target, exclude, replace, suffix) {
127 this.source = $('#edit-label');
133 this.target = $target;
139 this.exclude = exclude || false;
145 this.replace = replace || '';
151 this.suffix = suffix || '';
153 // Create bound versions of this instance's object methods to use as event
154 // handlers. This will let us easily unbind those specific handlers later
155 // on. NOTE: jQuery.proxy will not work for this because it assumes we want
156 // only one bound version of an object method, whereas we need one version
157 // per object instance.
161 * Populate the target form field with the altered source field value.
164 * The result of the _populate call, which should be undefined.
166 this.populate = function() {
167 return self._populate.call(self);
171 * Stop prepopulating the form fields.
174 * The result of the _unbind call, which should be undefined.
176 this.unbind = function() {
177 return self._unbind.call(self);
181 // Object constructor; no return value.
185 Drupal.viewsUi.FormFieldFiller.prototype,
186 /** @lends Drupal.viewsUi.FormFieldFiller# */ {
188 * Bind the form-filling behavior.
192 // Populate the form field when the source changes.
193 this.source.on('keyup.viewsUi change.viewsUi', this.populate);
194 // Quit populating the field as soon as it gets focus.
195 this.target.on('focus.viewsUi', this.unbind);
199 * Get the source form field value as altered by the passed-in parameters.
202 * The source form field value.
204 getTransliterated() {
205 let from = this.source.val();
207 from = from.toLowerCase().replace(this.exclude, this.replace);
213 * Populate the target form field with the altered source field value.
216 const transliterated = this.getTransliterated();
217 const suffix = this.suffix;
218 this.target.each(function(i) {
219 // Ensure that the maxlength is not exceeded by prepopulating the field.
220 const maxlength = $(this).attr('maxlength') - suffix.length;
221 $(this).val(transliterated.substr(0, maxlength) + suffix);
226 * Stop prepopulating the form fields.
229 this.source.off('keyup.viewsUi change.viewsUi', this.populate);
230 this.target.off('focus.viewsUi', this.unbind);
234 * Bind event handlers to new form fields, after they're replaced via Ajax.
236 * @param {jQuery} $fields
237 * Fields to rebind functionality to.
240 this.target = $fields;
247 * Adds functionality for the add item form.
249 * @type {Drupal~behavior}
251 * @prop {Drupal~behaviorAttach} attach
252 * Attaches the functionality in {@link Drupal.viewsUi.AddItemForm} to the
255 Drupal.behaviors.addItemForm = {
257 const $context = $(context);
258 let $form = $context;
259 // The add handler form may have an id of views-ui-add-handler-form--n.
260 if (!$context.is('form[id^="views-ui-add-handler-form"]')) {
261 $form = $context.find('form[id^="views-ui-add-handler-form"]');
263 if ($form.once('views-ui-add-handler-form').length) {
264 // If we we have an unprocessed views-ui-add-handler-form, let's
266 new Drupal.viewsUi.AddItemForm($form);
272 * Constructs a new AddItemForm.
276 * @param {jQuery} $form
277 * The form element used.
279 Drupal.viewsUi.AddItemForm = function($form) {
286 .find('.views-filterable-options :checkbox')
287 .on('click', $.proxy(this.handleCheck, this));
290 * Find the wrapper of the displayed text.
292 this.$selected_div = this.$form.find('.views-selected-options').parent();
293 this.$selected_div.hide();
299 this.checkedItems = [];
303 * Handles a checkbox check.
305 * @param {jQuery.Event} event
306 * The event triggered.
308 Drupal.viewsUi.AddItemForm.prototype.handleCheck = function(event) {
309 const $target = $(event.target);
310 const label = $.trim(
316 // Add/remove the checked item to the list.
317 if ($target.is(':checked')) {
318 this.$selected_div.show().css('display', 'block');
319 this.checkedItems.push(label);
321 const position = $.inArray(label, this.checkedItems);
322 // Delete the item from the list and make sure that the list doesn't have
323 // undefined items left.
324 for (let i = 0; i < this.checkedItems.length; i++) {
325 if (i === position) {
326 this.checkedItems.splice(i, 1);
331 // Hide it again if none item is selected.
332 if (this.checkedItems.length === 0) {
333 this.$selected_div.hide();
336 this.refreshCheckedItems();
340 * Refresh the display of the checked items.
342 Drupal.viewsUi.AddItemForm.prototype.refreshCheckedItems = function() {
343 // Perhaps we should precache the text div, too.
345 .find('.views-selected-options')
346 .html(this.checkedItems.join(', '))
347 .trigger('dialogContentResize');
351 * The input field items that add displays must be rendered as `<input>`
352 * elements. The following behavior detaches the `<input>` elements from the
353 * DOM, wraps them in an unordered list, then appends them to the list of
356 * @type {Drupal~behavior}
358 * @prop {Drupal~behaviorAttach} attach
359 * Fixes the input elements needed.
361 Drupal.behaviors.viewsUiRenderAddViewButton = {
363 // Build the add display menu and pull the display input buttons into it.
364 const $menu = $(context)
365 .find('#views-display-menu-tabs')
366 .once('views-ui-render-add-view-button');
371 const $addDisplayDropdown = $(
372 `<li class="add"><a href="#"><span class="icon add"></span>${Drupal.t(
374 )}</a><ul class="action-list" style="display:none;"></ul></li>`,
376 const $displayButtons = $menu.nextAll('input.add-display').detach();
378 .appendTo($addDisplayDropdown.find('.action-list'))
386 // Remove the 'Add ' prefix from the button labels since they're being
387 // placed in an 'Add' dropdown. @todo This assumes English, but so does
388 // $addDisplayDropdown above. Add support for translation.
389 $displayButtons.each(function() {
390 const label = $(this).val();
391 if (label.substr(0, 4) === 'Add ') {
392 $(this).val(label.substr(4));
395 $addDisplayDropdown.appendTo($menu);
397 // Add the click handler for the add display button.
398 $menu.find('li.add > a').on('click', function(event) {
399 event.preventDefault();
400 const $trigger = $(this);
401 Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger);
403 // Add a mouseleave handler to close the dropdown when the user mouses
404 // away from the item. We use mouseleave instead of mouseout because
405 // the user is going to trigger mouseout when she moves from the trigger
406 // link to the sub menu items.
407 // We use the live binder because the open class on this item will be
408 // toggled on and off and we want the handler to take effect in the cases
409 // that the class is present, but not when it isn't.
410 $('li.add', $menu).on('mouseleave', function(event) {
411 const $this = $(this);
412 const $trigger = $this.children('a[href="#"]');
413 if ($this.children('.action-list').is(':visible')) {
414 Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu($trigger);
421 * Toggle menu visibility.
423 * @param {jQuery} $trigger
424 * The element where the toggle was triggered.
427 * @note [@jessebeach] I feel like the following should be a more generic
428 * function and not written specifically for this UI, but I'm not sure
431 Drupal.behaviors.viewsUiRenderAddViewButton.toggleMenu = function($trigger) {
432 $trigger.parent().toggleClass('open');
433 $trigger.next().slideToggle('fast');
437 * Add search options to the views ui.
439 * @type {Drupal~behavior}
441 * @prop {Drupal~behaviorAttach} attach
442 * Attaches {@link Drupal.viewsUi.OptionsSearch} to the views ui filter
445 Drupal.behaviors.viewsUiSearchOptions = {
447 const $context = $(context);
448 let $form = $context;
449 // The add handler form may have an id of views-ui-add-handler-form--n.
450 if (!$context.is('form[id^="views-ui-add-handler-form"]')) {
451 $form = $context.find('form[id^="views-ui-add-handler-form"]');
453 // Make sure we don't add more than one event handler to the same form.
454 if ($form.once('views-ui-filter-options').length) {
455 new Drupal.viewsUi.OptionsSearch($form);
461 * Constructor for the viewsUi.OptionsSearch object.
463 * The OptionsSearch object filters the available options on a form according
464 * to the user's search term. Typing in "taxonomy" will show only those
465 * options containing "taxonomy" in their label.
469 * @param {jQuery} $form
472 Drupal.viewsUi.OptionsSearch = function($form) {
479 // Click on the title checks the box.
480 this.$form.on('click', 'td.title', event => {
481 const $target = $(event.currentTarget);
488 const searchBoxSelector =
489 '[data-drupal-selector="edit-override-controls-options-search"]';
490 const controlGroupSelector =
491 '[data-drupal-selector="edit-override-controls-group"]';
494 `${searchBoxSelector},${controlGroupSelector}`,
495 $.proxy(this.handleFilter, this),
498 this.$searchBox = this.$form.find(searchBoxSelector);
499 this.$controlGroup = this.$form.find(controlGroupSelector);
502 * Get a list of option labels and their corresponding divs and maintain it
503 * in memory, so we have as little overhead as possible at keyup time.
505 this.options = this.getOptions(this.$form.find('.filterable-option'));
507 // Trap the ENTER key in the search box so that it doesn't submit the form.
508 this.$searchBox.on('keypress', event => {
509 if (event.which === 13) {
510 event.preventDefault();
516 Drupal.viewsUi.OptionsSearch.prototype,
517 /** @lends Drupal.viewsUi.OptionsSearch# */ {
519 * Assemble a list of all the filterable options on the form.
521 * @param {jQuery} $allOptions
522 * A jQuery object representing the rows of filterable options to be
523 * shown and hidden depending on the user's search terms.
526 * An array of all the filterable options.
528 getOptions($allOptions) {
533 const length = $allOptions.length;
534 for (let i = 0; i < length; i++) {
535 $option = $($allOptions[i]);
536 $title = $option.find('.title');
537 $description = $option.find('.description');
539 // Search on the lowercase version of the title text + description.
540 searchText: `${$title
542 .toLowerCase()} ${$description.text().toLowerCase()}`,
543 // Maintain a reference to the jQuery object for each row, so we don't
544 // have to create a new object inside the performance-sensitive keyup
553 * Filter handler for the search box and type select that hides or shows the relevant
556 * @param {jQuery.Event} event
557 * The formUpdated event.
559 handleFilter(event) {
560 // Determine the user's search query. The search text has been converted
562 const search = this.$searchBox.val().toLowerCase();
563 const words = search.split(' ');
564 // Get selected Group
565 const group = this.$controlGroup.val();
567 // Search through the search texts in the form for matching text.
568 this.options.forEach(option => {
569 function hasWord(word) {
570 return option.searchText.indexOf(word) !== -1;
574 // Each word in the search string has to match the item in order for the
577 found = words.every(hasWord);
579 if (found && group !== 'all') {
580 found = option.$div.hasClass(group);
583 option.$div.toggle(found);
586 // Adapt dialog to content size.
587 $(event.target).trigger('dialogContentResize');
593 * Preview functionality in the views edit form.
595 * @type {Drupal~behavior}
597 * @prop {Drupal~behaviorAttach} attach
598 * Attaches the preview functionality to the view edit form.
600 Drupal.behaviors.viewsUiPreview = {
602 // Only act on the edit view form.
603 const $contextualFiltersBucket = $(context).find(
604 '.views-display-column .views-ui-display-tab-bucket.argument',
606 if ($contextualFiltersBucket.length === 0) {
610 // If the display has no contextual filters, hide the form where you
611 // enter the contextual filters for the live preview. If it has contextual
612 // filters, show the form.
613 const $contextualFilters = $contextualFiltersBucket.find(
614 '.views-display-setting a',
616 if ($contextualFilters.length) {
626 // Executes an initial preview.
628 $('#edit-displays-live-preview')
629 .once('edit-displays-live-preview')
633 .once('edit-displays-live-preview')
640 * Rearranges the filters.
642 * @type {Drupal~behavior}
644 * @prop {Drupal~behaviorAttach} attach
645 * Attach handlers to make it possible to rearrange the filters in the form
647 * @see Drupal.viewsUi.RearrangeFilterHandler
649 Drupal.behaviors.viewsUiRearrangeFilter = {
651 // Only act on the rearrange filter form.
653 typeof Drupal.tableDrag === 'undefined' ||
654 typeof Drupal.tableDrag['views-rearrange-filters'] === 'undefined'
658 const $context = $(context);
659 const $table = $context
660 .find('#views-rearrange-filters')
661 .once('views-rearrange-filters');
662 const $operator = $context
663 .find('.js-form-item-filter-groups-operator')
664 .once('views-rearrange-filters');
666 new Drupal.viewsUi.RearrangeFilterHandler($table, $operator);
672 * Improve the UI of the rearrange filters dialog box.
676 * @param {jQuery} $table
677 * The table in the filter form.
678 * @param {jQuery} $operator
679 * The filter groups operator element.
681 Drupal.viewsUi.RearrangeFilterHandler = function($table, $operator) {
683 * Keep a reference to the `<table>` being altered and to the div containing
684 * the filter groups operator dropdown (if it exists).
692 this.operator = $operator;
698 this.hasGroupOperator = this.operator.length > 0;
701 * Keep a reference to all draggable rows within the table.
705 this.draggableRows = $table.find('.draggable');
708 * Keep a reference to the buttons for adding and removing filter groups.
712 this.addGroupButton = $('input#views-add-group');
717 this.removeGroupButtons = $table.find('input.views-remove-group');
719 // Add links that duplicate the functionality of the (hidden) add and remove
721 this.insertAddRemoveFilterGroupLinks();
723 // When there is a filter groups operator dropdown on the page, create
724 // duplicates of the dropdown between each pair of filter groups.
725 if (this.hasGroupOperator) {
729 this.dropdowns = this.duplicateGroupsOperator();
730 this.syncGroupsOperators();
733 // Add methods to the tableDrag instance to account for operator cells
734 // (which span multiple rows), the operator labels next to each filter
735 // (e.g., "And" or "Or"), the filter groups, and other special aspects of
736 // this tableDrag instance.
737 this.modifyTableDrag();
739 // Initialize the operator labels (e.g., "And" or "Or") that are displayed
740 // next to the filters in each group, and bind a handler so that they change
741 // based on the values of the operator dropdown within that group.
742 this.redrawOperatorLabels();
744 .find('.views-group-title select')
745 .once('views-rearrange-filter-handler')
747 'change.views-rearrange-filter-handler',
748 $.proxy(this, 'redrawOperatorLabels'),
751 // Bind handlers so that when a "Remove" link is clicked, we:
752 // - Update the rowspans of cells containing an operator dropdown (since
753 // they need to change to reflect the number of rows in each group).
754 // - Redraw the operator labels next to the filters in the group (since the
755 // filter that is currently displayed last in each group is not supposed
756 // to have a label display next to it).
758 .find('a.views-groups-remove-link')
759 .once('views-rearrange-filter-handler')
761 'click.views-rearrange-filter-handler',
762 $.proxy(this, 'updateRowspans'),
765 'click.views-rearrange-filter-handler',
766 $.proxy(this, 'redrawOperatorLabels'),
771 Drupal.viewsUi.RearrangeFilterHandler.prototype,
772 /** @lends Drupal.viewsUi.RearrangeFilterHandler# */ {
774 * Insert links that allow filter groups to be added and removed.
776 insertAddRemoveFilterGroupLinks() {
777 // Insert a link for adding a new group at the top of the page, and make
778 // it match the action link styling used in a typical page.html.twig.
779 // Since Drupal does not provide a theme function for this markup this is
780 // the best we can do.
782 `<ul class="action-links"><li><a id="views-add-group-link" href="#">${this.addGroupButton.val()}</a></li></ul>`,
784 .prependTo(this.table.parent())
785 // When the link is clicked, dynamically click the hidden form button
786 // for adding a new filter group.
787 .once('views-rearrange-filter-handler')
788 .find('#views-add-group-link')
790 'click.views-rearrange-filter-handler',
791 $.proxy(this, 'clickAddGroupButton'),
794 // Find each (visually hidden) button for removing a filter group and
795 // insert a link next to it.
796 const length = this.removeGroupButtons.length;
798 for (i = 0; i < length; i++) {
799 const $removeGroupButton = $(this.removeGroupButtons[i]);
800 const buttonId = $removeGroupButton.attr('id');
802 `<a href="#" class="views-remove-group-link">${Drupal.t(
806 .insertBefore($removeGroupButton)
807 // When the link is clicked, dynamically click the corresponding form
809 .once('views-rearrange-filter-handler')
811 'click.views-rearrange-filter-handler',
813 $.proxy(this, 'clickRemoveGroupButton'),
819 * Dynamically click the button that adds a new filter group.
821 * @param {jQuery.Event} event
822 * The event triggered.
824 clickAddGroupButton(event) {
825 this.addGroupButton.trigger('mousedown');
826 event.preventDefault();
830 * Dynamically click a button for removing a filter group.
832 * @param {jQuery.Event} event
833 * Event being triggered, with event.data.buttonId set to the ID of the
834 * form button that should be clicked.
836 clickRemoveGroupButton(event) {
837 this.table.find(`#${event.data.buttonId}`).trigger('mousedown');
838 event.preventDefault();
842 * Move the groups operator so that it's between the first two groups, and
843 * duplicate it between any subsequent groups.
846 * An operator element.
848 duplicateGroupsOperator() {
852 const titleRows = $('tr.views-group-title').once(
853 'duplicateGroupsOperator',
856 if (!titleRows.length) {
857 return this.operator;
860 // Get rid of the explanatory text around the operator; its placement is
861 // explanatory enough.
864 .add('div.description')
865 .addClass('visually-hidden');
866 this.operator.find('select').addClass('form-select');
868 // Keep a list of the operator dropdowns, so we can sync their behavior
870 const dropdowns = this.operator;
872 // Move the operator to a new row just above the second group.
873 titleRow = $('tr#views-group-title-2');
875 '<tr class="filter-group-operator-row"><td colspan="5"></td></tr>',
877 newRow.find('td').append(this.operator);
878 newRow.insertBefore(titleRow);
879 const length = titleRows.length;
880 // Starting with the third group, copy the operator to a new row above the
882 for (let i = 2; i < length; i++) {
883 titleRow = $(titleRows[i]);
884 // Make a copy of the operator dropdown and put it in a new table row.
885 const fakeOperator = this.operator.clone();
886 fakeOperator.attr('id', '');
888 '<tr class="filter-group-operator-row"><td colspan="5"></td></tr>',
890 newRow.find('td').append(fakeOperator);
891 newRow.insertBefore(titleRow);
892 dropdowns.add(fakeOperator);
899 * Make the duplicated groups operators change in sync with each other.
901 syncGroupsOperators() {
902 if (this.dropdowns.length < 2) {
903 // We only have one dropdown (or none at all), so there's nothing to
908 this.dropdowns.on('change', $.proxy(this, 'operatorChangeHandler'));
912 * Click handler for the operators that appear between filter groups.
914 * Forces all operator dropdowns to have the same value.
916 * @param {jQuery.Event} event
917 * The event triggered.
919 operatorChangeHandler(event) {
920 const $target = $(event.target);
921 const operators = this.dropdowns.find('select').not($target);
923 // Change the other operators to match this new value.
924 operators.val($target.val());
931 const tableDrag = Drupal.tableDrag['views-rearrange-filters'];
932 const filterHandler = this;
935 * Override the row.onSwap method from tabledrag.js.
937 * When a row is dragged to another place in the table, several things
939 * - The row needs to be moved so that it's within one of the filter
941 * - The operator cells that span multiple rows need their rowspan
942 * attributes updated to reflect the number of rows in each group.
943 * - The operator labels that are displayed next to each filter need to
944 * be redrawn, to account for the row's new location.
946 tableDrag.row.prototype.onSwap = function() {
947 if (filterHandler.hasGroupOperator) {
948 // Make sure the row that just got moved (this.group) is inside one
949 // of the filter groups (i.e. below an empty marker row or a
950 // draggable). If it isn't, move it down one.
951 const thisRow = $(this.group);
952 const previousRow = thisRow.prev('tr');
954 previousRow.length &&
955 !previousRow.hasClass('group-message') &&
956 !previousRow.hasClass('draggable')
958 // Move the dragged row down one.
959 const next = thisRow.next();
961 this.swap('after', next);
964 filterHandler.updateRowspans();
966 // Redraw the operator labels that are displayed next to each filter, to
967 // account for the row's new location.
968 filterHandler.redrawOperatorLabels();
972 * Override the onDrop method from tabledrag.js.
974 tableDrag.onDrop = function() {
975 // If the tabledrag change marker (i.e., the "*") has been inserted
976 // inside a row after the operator label (i.e., "And" or "Or")
977 // rearrange the items so the operator label continues to appear last.
978 const changeMarker = $(this.oldRowElement).find('.tabledrag-changed');
979 if (changeMarker.length) {
980 // Search for occurrences of the operator label before the change
981 // marker, and reverse them.
982 const operatorLabel = changeMarker.prevAll('.views-operator-label');
983 if (operatorLabel.length) {
984 operatorLabel.insertAfter(changeMarker);
988 // Make sure the "group" dropdown is properly updated when rows are
989 // dragged into an empty filter group. This is borrowed heavily from
990 // the block.js implementation of tableDrag.onDrop().
991 const groupRow = $(this.rowObject.element)
992 .prevAll('tr.group-message')
994 const groupName = groupRow.className.replace(
995 /([^ ]+[ ]+)*group-([^ ]+)-message([ ]+[^ ]+)*/,
998 const groupField = $(
999 'select.views-group-select',
1000 this.rowObject.element,
1003 $(this.rowObject.element)
1005 .is('.group-message') &&
1006 !groupField.is(`.views-group-select-${groupName}`)
1008 const oldGroupName = groupField
1011 /([^ ]+[ ]+)*views-group-select-([^ ]+)([ ]+[^ ]+)*/,
1015 .removeClass(`views-group-select-${oldGroupName}`)
1016 .addClass(`views-group-select-${groupName}`);
1017 groupField.val(groupName);
1023 * Redraw the operator labels that are displayed next to each filter.
1025 redrawOperatorLabels() {
1026 for (let i = 0; i < this.draggableRows.length; i++) {
1027 // Within the row, the operator labels are displayed inside the first
1028 // table cell (next to the filter name).
1029 const $draggableRow = $(this.draggableRows[i]);
1030 const $firstCell = $draggableRow.find('td').eq(0);
1031 if ($firstCell.length) {
1032 // The value of the operator label ("And" or "Or") is taken from the
1033 // first operator dropdown we encounter, going backwards from the
1034 // current row. This dropdown is the one associated with the current
1035 // row's filter group.
1036 const operatorValue = $draggableRow
1037 .prevAll('.views-group-title')
1038 .find('option:selected')
1040 const operatorLabel = `<span class="views-operator-label">${operatorValue}</span>`;
1041 // If the next visible row after this one is a draggable filter row,
1042 // display the operator label next to the current row. (Checking for
1043 // visibility is necessary here since the "Remove" links hide the
1044 // removed row but don't actually remove it from the document).
1045 const $nextRow = $draggableRow.nextAll(':visible').eq(0);
1046 const $existingOperatorLabel = $firstCell.find(
1047 '.views-operator-label',
1049 if ($nextRow.hasClass('draggable')) {
1050 // If an operator label was already there, replace it with the new
1052 if ($existingOperatorLabel.length) {
1053 $existingOperatorLabel.replaceWith(operatorLabel);
1055 // Otherwise, append the operator label to the end of the table
1058 $firstCell.append(operatorLabel);
1061 // If the next row doesn't contain a filter, then this is the last row
1062 // in the group. We don't want to display the operator there (since
1063 // operators should only display between two related filters, e.g.
1064 // "filter1 AND filter2 AND filter3"). So we remove any existing label
1065 // that this row has.
1067 $existingOperatorLabel.remove();
1074 * Update the rowspan attribute of each cell containing an operator
1079 let $currentEmptyRow;
1082 const rows = $(this.table).find('tr');
1083 const length = rows.length;
1084 for (let i = 0; i < length; i++) {
1086 if ($row.hasClass('views-group-title')) {
1087 // This row is a title row.
1088 // Keep a reference to the cell containing the dropdown operator.
1089 $operatorCell = $row.find('td.group-operator');
1090 // Assume this filter group is empty, until we find otherwise.
1092 $currentEmptyRow = $row.next('tr');
1094 .removeClass('group-populated')
1095 .addClass('group-empty');
1096 // The cell with the dropdown operator should span the title row and
1097 // the "this group is empty" row.
1098 $operatorCell.attr('rowspan', 2);
1099 } else if ($row.hasClass('draggable') && $row.is(':visible')) {
1100 // We've found a visible filter row, so we now know the group isn't
1104 .removeClass('group-empty')
1105 .addClass('group-populated');
1106 // The operator cell should span all draggable rows, plus the title.
1107 $operatorCell.attr('rowspan', draggableCount + 1);
1115 * Add a select all checkbox, which checks each checkbox at once.
1117 * @type {Drupal~behavior}
1119 * @prop {Drupal~behaviorAttach} attach
1120 * Attaches select all functionality to the views filter form.
1122 Drupal.behaviors.viewsFilterConfigSelectAll = {
1124 const $context = $(context);
1126 const $selectAll = $context
1127 .find('.js-form-item-options-value-all')
1128 .once('filterConfigSelectAll');
1129 const $selectAllCheckbox = $selectAll.find('input[type=checkbox]');
1130 const $checkboxes = $selectAll
1131 .closest('.form-checkboxes')
1133 '.js-form-type-checkbox:not(.js-form-item-options-value-all) input[type="checkbox"]',
1136 if ($selectAll.length) {
1137 // Show the select all checkbox.
1139 $selectAllCheckbox.on('click', function() {
1140 // Update all checkbox beside the select all checkbox.
1141 $checkboxes.prop('checked', $(this).is(':checked'));
1144 // Uncheck the select all checkbox if any of the others are unchecked.
1145 $checkboxes.on('click', function() {
1146 if ($(this).is('checked') === false) {
1147 $selectAllCheckbox.prop('checked', false);
1155 * Remove icon class from elements that are themed as buttons or dropbuttons.
1157 * @type {Drupal~behavior}
1159 * @prop {Drupal~behaviorAttach} attach
1160 * Removes the icon class from certain views elements.
1162 Drupal.behaviors.viewsRemoveIconClass = {
1165 .find('.dropbutton')
1166 .once('dropbutton-icon')
1168 .removeClass('icon');
1173 * Change "Expose filter" buttons into checkboxes.
1175 * @type {Drupal~behavior}
1177 * @prop {Drupal~behaviorAttach} attach
1178 * Changes buttons into checkboxes via {@link Drupal.viewsUi.Checkboxifier}.
1180 Drupal.behaviors.viewsUiCheckboxify = {
1181 attach(context, settings) {
1183 '[data-drupal-selector="edit-options-expose-button-button"], [data-drupal-selector="edit-options-group-button-button"]',
1184 ).once('views-ui-checkboxify');
1185 const length = $buttons.length;
1187 for (i = 0; i < length; i++) {
1188 new Drupal.viewsUi.Checkboxifier($buttons[i]);
1194 * Change the default widget to select the default group according to the
1195 * selected widget for the exposed group.
1197 * @type {Drupal~behavior}
1199 * @prop {Drupal~behaviorAttach} attach
1200 * Changes the default widget based on user input.
1202 Drupal.behaviors.viewsUiChangeDefaultWidget = {
1204 const $context = $(context);
1206 function changeDefaultWidget(event) {
1207 if ($(event.target).prop('checked')) {
1209 .find('input.default-radios')
1213 .find('td.any-default-radios-row')
1217 .find('input.default-checkboxes')
1222 .find('input.default-checkboxes')
1226 .find('td.any-default-radios-row')
1230 .find('input.default-radios')
1236 // Update on widget change.
1238 .find('input[name="options[group_info][multiple]"]')
1239 .on('change', changeDefaultWidget)
1240 // Update the first time the form is rendered.
1246 * Attaches expose filter button to a checkbox that triggers its click event.
1250 * @param {HTMLElement} button
1251 * The DOM object representing the button to be checkboxified.
1253 Drupal.viewsUi.Checkboxifier = function(button) {
1254 this.$button = $(button);
1255 this.$parent = this.$button.parent('div.views-expose, div.views-grouped');
1256 this.$input = this.$parent.find('input:checkbox, input:radio');
1257 // Hide the button and its description.
1258 this.$button.hide();
1259 this.$parent.find('.exposed-description, .grouped-description').hide();
1261 this.$input.on('click', $.proxy(this, 'clickHandler'));
1265 * When the checkbox is checked or unchecked, simulate a button press.
1267 * @param {jQuery.Event} e
1268 * The event triggered.
1270 Drupal.viewsUi.Checkboxifier.prototype.clickHandler = function(e) {
1271 this.$button.trigger('click').trigger('submit');
1275 * Change the Apply button text based upon the override select state.
1277 * @type {Drupal~behavior}
1279 * @prop {Drupal~behaviorAttach} attach
1280 * Attaches behavior to change the Apply button according to the current
1283 Drupal.behaviors.viewsUiOverrideSelect = {
1286 .find('[data-drupal-selector="edit-override-dropdown"]')
1287 .once('views-ui-override-button-text')
1290 const $context = $(context);
1291 const $submit = $context.find('[id^=edit-submit]');
1292 const oldValue = $submit.val();
1295 .once('views-ui-override-button-text')
1296 .on('mouseup', function() {
1297 $(this).val(oldValue);
1302 .on('change', function() {
1303 const $this = $(this);
1304 if ($this.val() === 'default') {
1305 $submit.val(Drupal.t('Apply (all displays)'));
1306 } else if ($this.val() === 'default_revert') {
1307 $submit.val(Drupal.t('Revert to default'));
1309 $submit.val(Drupal.t('Apply (this display)'));
1311 const $dialog = $context.closest('.ui-dialog-content');
1312 $dialog.trigger('dialogButtonsChange');
1320 * Functionality for the remove link in the views UI.
1322 * @type {Drupal~behavior}
1324 * @prop {Drupal~behaviorAttach} attach
1325 * Attaches behavior for the remove view and remove display links.
1327 Drupal.behaviors.viewsUiHandlerRemoveLink = {
1329 const $context = $(context);
1330 // Handle handler deletion by looking for the hidden checkbox and hiding
1333 .find('a.views-remove-link')
1335 .on('click', function(event) {
1338 .replace('views-remove-link-', '');
1339 $context.find(`#views-row-${id}`).hide();
1340 $context.find(`#views-removed-${id}`).prop('checked', true);
1341 event.preventDefault();
1344 // Handle display deletion by looking for the hidden checkbox and hiding
1347 .find('a.display-remove-link')
1349 .on('click', function(event) {
1352 .replace('display-remove-link-', '');
1353 $context.find(`#display-row-${id}`).hide();
1354 $context.find(`#display-removed-${id}`).prop('checked', true);
1355 event.preventDefault();
1359 })(jQuery, Drupal, drupalSettings);