3 * Handles AJAX submission and response in Views UI.
6 (function ($, Drupal, drupalSettings) {
11 * Ajax command for highlighting elements.
13 * @param {Drupal.Ajax} [ajax]
15 * @param {object} response
17 * @param {string} response.selector
18 * The selector in question.
19 * @param {number} [status]
20 * The HTTP status code.
22 Drupal.AjaxCommands.prototype.viewsHighlight = function (ajax, response, status) {
23 $('.hilited').removeClass('hilited');
24 $(response.selector).addClass('hilited');
28 * Ajax command to set the form submit action in the views modal edit form.
30 * @param {Drupal.Ajax} [ajax]
32 * @param {object} response
33 * The Ajax response. Contains .url
34 * @param {string} [status]
35 * The XHR status code?
37 Drupal.AjaxCommands.prototype.viewsSetForm = function (ajax, response, status) {
38 var $form = $('.js-views-ui-dialog form');
39 // Identify the button that was clicked so that .ajaxSubmit() can use it.
40 // We need to do this for both .click() and .mousedown() since JavaScript
41 // code might trigger either behavior.
42 var $submit_buttons = $form.find('input[type=submit].js-form-submit, button.js-form-submit').once('views-ajax-submit');
43 $submit_buttons.on('click mousedown', function () {
46 $form.once('views-ajax-submit').each(function () {
48 var element_settings = {
51 base: $form.attr('id'),
54 var ajaxForm = Drupal.ajax(element_settings);
55 ajaxForm.$form = $form;
60 * Ajax command to show certain buttons in the views edit form.
62 * @param {Drupal.Ajax} [ajax]
64 * @param {object} response
66 * @param {bool} response.changed
67 * Whether the state changed for the buttons or not.
68 * @param {number} [status]
69 * The HTTP status code.
71 Drupal.AjaxCommands.prototype.viewsShowButtons = function (ajax, response, status) {
72 $('div.views-edit-view div.form-actions').removeClass('js-hide');
73 if (response.changed) {
74 $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
79 * Ajax command for triggering preview.
81 * @param {Drupal.Ajax} [ajax]
83 * @param {object} [response]
85 * @param {number} [status]
86 * The HTTP status code.
88 Drupal.AjaxCommands.prototype.viewsTriggerPreview = function (ajax, response, status) {
89 if ($('input#edit-displays-live-preview').is(':checked')) {
90 $('#preview-submit').trigger('click');
95 * Ajax command to replace the title of a page.
97 * @param {Drupal.Ajax} [ajax]
99 * @param {object} response
101 * @param {string} response.siteName
103 * @param {string} response.title
104 * The new page title.
105 * @param {number} [status]
106 * The HTTP status code.
108 Drupal.AjaxCommands.prototype.viewsReplaceTitle = function (ajax, response, status) {
110 // For the <title> element, make a best-effort attempt to replace the page
111 // title and leave the site name alone. If the theme doesn't use the site
112 // name in the <title> element, this will fail.
113 var oldTitle = doc.title;
114 // Escape the site name, in case it has special characters in it, so we can
115 // use it in our regex.
116 var escapedSiteName = response.siteName.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
117 var re = new RegExp('.+ (.) ' + escapedSiteName);
118 doc.title = oldTitle.replace(re, response.title + ' $1 ' + response.siteName);
120 $('h1.page-title').text(response.title);
124 * Get rid of irritating tabledrag messages.
127 * An array of messages. Always empty array, to get rid of the messages.
129 Drupal.theme.tableDragChangedWarning = function () {
134 * Trigger preview when the "live preview" checkbox is checked.
136 * @type {Drupal~behavior}
138 * @prop {Drupal~behaviorAttach} attach
139 * Attaches behavior to trigger live preview if the live preview option is
142 Drupal.behaviors.livePreview = {
143 attach: function (context) {
144 $('input#edit-displays-live-preview', context).once('views-ajax').on('click', function () {
145 if ($(this).is(':checked')) {
146 $('#preview-submit').trigger('click');
153 * Sync preview display.
155 * @type {Drupal~behavior}
157 * @prop {Drupal~behaviorAttach} attach
158 * Attaches behavior to sync the preview display when needed.
160 Drupal.behaviors.syncPreviewDisplay = {
161 attach: function (context) {
162 $('#views-tabset a').once('views-ajax').on('click', function () {
163 var href = $(this).attr('href');
164 // Cut of #views-tabset.
165 var display_id = href.substr(11);
166 // Set the form element.
167 $('#views-live-preview #preview-display-id').val(display_id);
173 * Ajax behaviors for the views_ui module.
175 * @type {Drupal~behavior}
177 * @prop {Drupal~behaviorAttach} attach
178 * Attaches ajax behaviors to the elements with the classes in question.
180 Drupal.behaviors.viewsAjax = {
181 collapseReplaced: false,
182 attach: function (context, settings) {
183 var base_element_settings = {
185 progress: {type: 'fullscreen'}
187 // Bind AJAX behaviors to all items showing the class.
188 $('a.views-ajax-link', context).once('views-ajax').each(function () {
189 var element_settings = base_element_settings;
190 element_settings.base = $(this).attr('id');
191 element_settings.element = this;
192 // Set the URL to go to the anchor.
193 if ($(this).attr('href')) {
194 element_settings.url = $(this).attr('href');
196 Drupal.ajax(element_settings);
199 $('div#views-live-preview a')
200 .once('views-ajax').each(function () {
201 // We don't bind to links without a URL.
202 if (!$(this).attr('href')) {
206 var element_settings = base_element_settings;
207 // Set the URL to go to the anchor.
208 element_settings.url = $(this).attr('href');
209 if (Drupal.Views.getPath(element_settings.url).substring(0, 21) !== 'admin/structure/views') {
213 element_settings.wrapper = 'views-preview-wrapper';
214 element_settings.method = 'replaceWith';
215 element_settings.base = $(this).attr('id');
216 element_settings.element = this;
217 Drupal.ajax(element_settings);
220 // Within a live preview, make exposed widget form buttons re-trigger the
222 // @todo Revisit this after fixing Views UI to display a Preview outside
223 // of the main Edit form.
224 $('div#views-live-preview input[type=submit]')
225 .once('views-ajax').each(function (event) {
226 $(this).on('click', function () {
227 this.form.clk = this;
230 var element_settings = base_element_settings;
231 // Set the URL to go to the anchor.
232 element_settings.url = $(this.form).attr('action');
233 if (Drupal.Views.getPath(element_settings.url).substring(0, 21) !== 'admin/structure/views') {
237 element_settings.wrapper = 'views-preview-wrapper';
238 element_settings.method = 'replaceWith';
239 element_settings.event = 'click';
240 element_settings.base = $(this).attr('id');
241 element_settings.element = this;
243 Drupal.ajax(element_settings);
249 })(jQuery, Drupal, drupalSettings);