3 * Handles AJAX submission and response in Views UI.
6 (function($, Drupal, drupalSettings) {
8 * Ajax command for highlighting elements.
10 * @param {Drupal.Ajax} [ajax]
12 * @param {object} response
14 * @param {string} response.selector
15 * The selector in question.
16 * @param {number} [status]
17 * The HTTP status code.
19 Drupal.AjaxCommands.prototype.viewsHighlight = function(
24 $('.hilited').removeClass('hilited');
25 $(response.selector).addClass('hilited');
29 * Ajax command to set the form submit action in the views modal edit form.
31 * @param {Drupal.Ajax} [ajax]
33 * @param {object} response
34 * The Ajax response. Contains .url
35 * @param {string} [status]
36 * The XHR status code?
38 Drupal.AjaxCommands.prototype.viewsSetForm = function(
43 const $form = $('.js-views-ui-dialog form');
44 // Identify the button that was clicked so that .ajaxSubmit() can use it.
45 // We need to do this for both .click() and .mousedown() since JavaScript
46 // code might trigger either behavior.
47 const $submitButtons = $form
48 .find('input[type=submit].js-form-submit, button.js-form-submit')
49 .once('views-ajax-submit');
50 $submitButtons.on('click mousedown', function() {
53 $form.once('views-ajax-submit').each(function() {
54 const $form = $(this);
55 const elementSettings = {
58 base: $form.attr('id'),
61 const ajaxForm = Drupal.ajax(elementSettings);
62 ajaxForm.$form = $form;
67 * Ajax command to show certain buttons in the views edit form.
69 * @param {Drupal.Ajax} [ajax]
71 * @param {object} response
73 * @param {bool} response.changed
74 * Whether the state changed for the buttons or not.
75 * @param {number} [status]
76 * The HTTP status code.
78 Drupal.AjaxCommands.prototype.viewsShowButtons = function(
83 $('div.views-edit-view div.form-actions').removeClass('js-hide');
84 if (response.changed) {
85 $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
90 * Ajax command for triggering preview.
92 * @param {Drupal.Ajax} [ajax]
94 * @param {object} [response]
96 * @param {number} [status]
97 * The HTTP status code.
99 Drupal.AjaxCommands.prototype.viewsTriggerPreview = function(
104 if ($('input#edit-displays-live-preview').is(':checked')) {
105 $('#preview-submit').trigger('click');
110 * Ajax command to replace the title of a page.
112 * @param {Drupal.Ajax} [ajax]
114 * @param {object} response
116 * @param {string} response.siteName
118 * @param {string} response.title
119 * The new page title.
120 * @param {number} [status]
121 * The HTTP status code.
123 Drupal.AjaxCommands.prototype.viewsReplaceTitle = function(
128 const doc = document;
129 // For the <title> element, make a best-effort attempt to replace the page
130 // title and leave the site name alone. If the theme doesn't use the site
131 // name in the <title> element, this will fail.
132 const oldTitle = doc.title;
133 // Escape the site name, in case it has special characters in it, so we can
134 // use it in our regex.
135 const escapedSiteName = response.siteName.replace(
136 /[-[\]{}()*+?.,\\^$|#\s]/g,
139 const re = new RegExp(`.+ (.) ${escapedSiteName}`);
140 doc.title = oldTitle.replace(
142 `${response.title} $1 ${response.siteName}`,
145 $('h1.page-title').text(response.title);
149 * Get rid of irritating tabledrag messages.
152 * An array of messages. Always empty array, to get rid of the messages.
154 Drupal.theme.tableDragChangedWarning = function() {
159 * Trigger preview when the "live preview" checkbox is checked.
161 * @type {Drupal~behavior}
163 * @prop {Drupal~behaviorAttach} attach
164 * Attaches behavior to trigger live preview if the live preview option is
167 Drupal.behaviors.livePreview = {
169 $('input#edit-displays-live-preview', context)
171 .on('click', function() {
172 if ($(this).is(':checked')) {
173 $('#preview-submit').trigger('click');
180 * Sync preview display.
182 * @type {Drupal~behavior}
184 * @prop {Drupal~behaviorAttach} attach
185 * Attaches behavior to sync the preview display when needed.
187 Drupal.behaviors.syncPreviewDisplay = {
191 .on('click', function() {
192 const href = $(this).attr('href');
193 // Cut of #views-tabset.
194 const displayId = href.substr(11);
195 // Set the form element.
196 $('#views-live-preview #preview-display-id').val(displayId);
202 * Ajax behaviors for the views_ui module.
204 * @type {Drupal~behavior}
206 * @prop {Drupal~behaviorAttach} attach
207 * Attaches ajax behaviors to the elements with the classes in question.
209 Drupal.behaviors.viewsAjax = {
210 collapseReplaced: false,
211 attach(context, settings) {
212 const baseElementSettings = {
214 progress: { type: 'fullscreen' },
216 // Bind AJAX behaviors to all items showing the class.
217 $('a.views-ajax-link', context)
220 const elementSettings = baseElementSettings;
221 elementSettings.base = $(this).attr('id');
222 elementSettings.element = this;
223 // Set the URL to go to the anchor.
224 if ($(this).attr('href')) {
225 elementSettings.url = $(this).attr('href');
227 Drupal.ajax(elementSettings);
230 $('div#views-live-preview a')
233 // We don't bind to links without a URL.
234 if (!$(this).attr('href')) {
238 const elementSettings = baseElementSettings;
239 // Set the URL to go to the anchor.
240 elementSettings.url = $(this).attr('href');
242 Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
243 'admin/structure/views'
248 elementSettings.wrapper = 'views-preview-wrapper';
249 elementSettings.method = 'replaceWith';
250 elementSettings.base = $(this).attr('id');
251 elementSettings.element = this;
252 Drupal.ajax(elementSettings);
255 // Within a live preview, make exposed widget form buttons re-trigger the
257 // @todo Revisit this after fixing Views UI to display a Preview outside
258 // of the main Edit form.
259 $('div#views-live-preview input[type=submit]')
261 .each(function(event) {
262 $(this).on('click', function() {
263 this.form.clk = this;
266 const elementSettings = baseElementSettings;
267 // Set the URL to go to the anchor.
268 elementSettings.url = $(this.form).attr('action');
270 Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
271 'admin/structure/views'
276 elementSettings.wrapper = 'views-preview-wrapper';
277 elementSettings.method = 'replaceWith';
278 elementSettings.event = 'click';
279 elementSettings.base = $(this).attr('id');
280 elementSettings.element = this;
282 Drupal.ajax(elementSettings);
286 })(jQuery, Drupal, drupalSettings);