90856f212ad427458cf67d251c361eadd9f04dbd
[yaffs-website] / web / core / modules / views_ui / js / ajax.es6.js
1 /**
2  * @file
3  * Handles AJAX submission and response in Views UI.
4  */
5
6 (function($, Drupal, drupalSettings) {
7   /**
8    * Ajax command for highlighting elements.
9    *
10    * @param {Drupal.Ajax} [ajax]
11    *   An Ajax object.
12    * @param {object} response
13    *   The Ajax response.
14    * @param {string} response.selector
15    *   The selector in question.
16    * @param {number} [status]
17    *   The HTTP status code.
18    */
19   Drupal.AjaxCommands.prototype.viewsHighlight = function(
20     ajax,
21     response,
22     status,
23   ) {
24     $('.hilited').removeClass('hilited');
25     $(response.selector).addClass('hilited');
26   };
27
28   /**
29    * Ajax command to set the form submit action in the views modal edit form.
30    *
31    * @param {Drupal.Ajax} [ajax]
32    *   An Ajax object.
33    * @param {object} response
34    *   The Ajax response. Contains .url
35    * @param {string} [status]
36    *   The XHR status code?
37    */
38   Drupal.AjaxCommands.prototype.viewsSetForm = function(
39     ajax,
40     response,
41     status,
42   ) {
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() {
51       this.form.clk = this;
52     });
53     $form.once('views-ajax-submit').each(function() {
54       const $form = $(this);
55       const elementSettings = {
56         url: response.url,
57         event: 'submit',
58         base: $form.attr('id'),
59         element: this,
60       };
61       const ajaxForm = Drupal.ajax(elementSettings);
62       ajaxForm.$form = $form;
63     });
64   };
65
66   /**
67    * Ajax command to show certain buttons in the views edit form.
68    *
69    * @param {Drupal.Ajax} [ajax]
70    *   An Ajax object.
71    * @param {object} response
72    *   The Ajax 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.
77    */
78   Drupal.AjaxCommands.prototype.viewsShowButtons = function(
79     ajax,
80     response,
81     status,
82   ) {
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');
86     }
87   };
88
89   /**
90    * Ajax command for triggering preview.
91    *
92    * @param {Drupal.Ajax} [ajax]
93    *   An Ajax object.
94    * @param {object} [response]
95    *   The Ajax response.
96    * @param {number} [status]
97    *   The HTTP status code.
98    */
99   Drupal.AjaxCommands.prototype.viewsTriggerPreview = function(
100     ajax,
101     response,
102     status,
103   ) {
104     if ($('input#edit-displays-live-preview').is(':checked')) {
105       $('#preview-submit').trigger('click');
106     }
107   };
108
109   /**
110    * Ajax command to replace the title of a page.
111    *
112    * @param {Drupal.Ajax} [ajax]
113    *   An Ajax object.
114    * @param {object} response
115    *   The Ajax response.
116    * @param {string} response.siteName
117    *   The site name.
118    * @param {string} response.title
119    *   The new page title.
120    * @param {number} [status]
121    *   The HTTP status code.
122    */
123   Drupal.AjaxCommands.prototype.viewsReplaceTitle = function(
124     ajax,
125     response,
126     status,
127   ) {
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,
137       '\\$&',
138     );
139     const re = new RegExp(`.+ (.) ${escapedSiteName}`);
140     doc.title = oldTitle.replace(
141       re,
142       `${response.title} $1 ${response.siteName}`,
143     );
144
145     $('h1.page-title').text(response.title);
146   };
147
148   /**
149    * Get rid of irritating tabledrag messages.
150    *
151    * @return {Array}
152    *   An array of messages. Always empty array, to get rid of the messages.
153    */
154   Drupal.theme.tableDragChangedWarning = function() {
155     return [];
156   };
157
158   /**
159    * Trigger preview when the "live preview" checkbox is checked.
160    *
161    * @type {Drupal~behavior}
162    *
163    * @prop {Drupal~behaviorAttach} attach
164    *   Attaches behavior to trigger live preview if the live preview option is
165    *   checked.
166    */
167   Drupal.behaviors.livePreview = {
168     attach(context) {
169       $('input#edit-displays-live-preview', context)
170         .once('views-ajax')
171         .on('click', function() {
172           if ($(this).is(':checked')) {
173             $('#preview-submit').trigger('click');
174           }
175         });
176     },
177   };
178
179   /**
180    * Sync preview display.
181    *
182    * @type {Drupal~behavior}
183    *
184    * @prop {Drupal~behaviorAttach} attach
185    *   Attaches behavior to sync the preview display when needed.
186    */
187   Drupal.behaviors.syncPreviewDisplay = {
188     attach(context) {
189       $('#views-tabset a')
190         .once('views-ajax')
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);
197         });
198     },
199   };
200
201   /**
202    * Ajax behaviors for the views_ui module.
203    *
204    * @type {Drupal~behavior}
205    *
206    * @prop {Drupal~behaviorAttach} attach
207    *   Attaches ajax behaviors to the elements with the classes in question.
208    */
209   Drupal.behaviors.viewsAjax = {
210     collapseReplaced: false,
211     attach(context, settings) {
212       const baseElementSettings = {
213         event: 'click',
214         progress: { type: 'fullscreen' },
215       };
216       // Bind AJAX behaviors to all items showing the class.
217       $('a.views-ajax-link', context)
218         .once('views-ajax')
219         .each(function() {
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');
226           }
227           Drupal.ajax(elementSettings);
228         });
229
230       $('div#views-live-preview a')
231         .once('views-ajax')
232         .each(function() {
233           // We don't bind to links without a URL.
234           if (!$(this).attr('href')) {
235             return true;
236           }
237
238           const elementSettings = baseElementSettings;
239           // Set the URL to go to the anchor.
240           elementSettings.url = $(this).attr('href');
241           if (
242             Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
243             'admin/structure/views'
244           ) {
245             return true;
246           }
247
248           elementSettings.wrapper = 'views-preview-wrapper';
249           elementSettings.method = 'replaceWith';
250           elementSettings.base = $(this).attr('id');
251           elementSettings.element = this;
252           Drupal.ajax(elementSettings);
253         });
254
255       // Within a live preview, make exposed widget form buttons re-trigger the
256       // Preview button.
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]')
260         .once('views-ajax')
261         .each(function(event) {
262           $(this).on('click', function() {
263             this.form.clk = this;
264             return true;
265           });
266           const elementSettings = baseElementSettings;
267           // Set the URL to go to the anchor.
268           elementSettings.url = $(this.form).attr('action');
269           if (
270             Drupal.Views.getPath(elementSettings.url).substring(0, 21) !==
271             'admin/structure/views'
272           ) {
273             return true;
274           }
275
276           elementSettings.wrapper = 'views-preview-wrapper';
277           elementSettings.method = 'replaceWith';
278           elementSettings.event = 'click';
279           elementSettings.base = $(this).attr('id');
280           elementSettings.element = this;
281
282           Drupal.ajax(elementSettings);
283         });
284     },
285   };
286 })(jQuery, Drupal, drupalSettings);