Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views_ui / js / ajax.js
1 /**
2  * @file
3  * Handles AJAX submission and response in Views UI.
4  */
5
6 (function ($, Drupal, drupalSettings) {
7
8   'use strict';
9
10   /**
11    * Ajax command for highlighting elements.
12    *
13    * @param {Drupal.Ajax} [ajax]
14    *   An Ajax object.
15    * @param {object} response
16    *   The Ajax response.
17    * @param {string} response.selector
18    *   The selector in question.
19    * @param {number} [status]
20    *   The HTTP status code.
21    */
22   Drupal.AjaxCommands.prototype.viewsHighlight = function (ajax, response, status) {
23     $('.hilited').removeClass('hilited');
24     $(response.selector).addClass('hilited');
25   };
26
27   /**
28    * Ajax command to set the form submit action in the views modal edit form.
29    *
30    * @param {Drupal.Ajax} [ajax]
31    *   An Ajax object.
32    * @param {object} response
33    *   The Ajax response. Contains .url
34    * @param {string} [status]
35    *   The XHR status code?
36    */
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 () {
44       this.form.clk = this;
45     });
46     $form.once('views-ajax-submit').each(function () {
47       var $form = $(this);
48       var element_settings = {
49         url: response.url,
50         event: 'submit',
51         base: $form.attr('id'),
52         element: this
53       };
54       var ajaxForm = Drupal.ajax(element_settings);
55       ajaxForm.$form = $form;
56     });
57   };
58
59   /**
60    * Ajax command to show certain buttons in the views edit form.
61    *
62    * @param {Drupal.Ajax} [ajax]
63    *   An Ajax object.
64    * @param {object} response
65    *   The Ajax 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.
70    */
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');
75     }
76   };
77
78   /**
79    * Ajax command for triggering preview.
80    *
81    * @param {Drupal.Ajax} [ajax]
82    *   An Ajax object.
83    * @param {object} [response]
84    *   The Ajax response.
85    * @param {number} [status]
86    *   The HTTP status code.
87    */
88   Drupal.AjaxCommands.prototype.viewsTriggerPreview = function (ajax, response, status) {
89     if ($('input#edit-displays-live-preview').is(':checked')) {
90       $('#preview-submit').trigger('click');
91     }
92   };
93
94   /**
95    * Ajax command to replace the title of a page.
96    *
97    * @param {Drupal.Ajax} [ajax]
98    *   An Ajax object.
99    * @param {object} response
100    *   The Ajax response.
101    * @param {string} response.siteName
102    *   The site name.
103    * @param {string} response.title
104    *   The new page title.
105    * @param {number} [status]
106    *   The HTTP status code.
107    */
108   Drupal.AjaxCommands.prototype.viewsReplaceTitle = function (ajax, response, status) {
109     var doc = document;
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);
119
120     $('h1.page-title').text(response.title);
121   };
122
123   /**
124    * Get rid of irritating tabledrag messages.
125    *
126    * @return {Array}
127    *   An array of messages. Always empty array, to get rid of the messages.
128    */
129   Drupal.theme.tableDragChangedWarning = function () {
130     return [];
131   };
132
133   /**
134    * Trigger preview when the "live preview" checkbox is checked.
135    *
136    * @type {Drupal~behavior}
137    *
138    * @prop {Drupal~behaviorAttach} attach
139    *   Attaches behavior to trigger live preview if the live preview option is
140    *   checked.
141    */
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');
147         }
148       });
149     }
150   };
151
152   /**
153    * Sync preview display.
154    *
155    * @type {Drupal~behavior}
156    *
157    * @prop {Drupal~behaviorAttach} attach
158    *   Attaches behavior to sync the preview display when needed.
159    */
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);
168       });
169     }
170   };
171
172   /**
173    * Ajax behaviors for the views_ui module.
174    *
175    * @type {Drupal~behavior}
176    *
177    * @prop {Drupal~behaviorAttach} attach
178    *   Attaches ajax behaviors to the elements with the classes in question.
179    */
180   Drupal.behaviors.viewsAjax = {
181     collapseReplaced: false,
182     attach: function (context, settings) {
183       var base_element_settings = {
184         event: 'click',
185         progress: {type: 'fullscreen'}
186       };
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');
195         }
196         Drupal.ajax(element_settings);
197       });
198
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')) {
203             return true;
204           }
205
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') {
210             return true;
211           }
212
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);
218         });
219
220       // Within a live preview, make exposed widget form buttons re-trigger the
221       // Preview button.
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;
228             return true;
229           });
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') {
234             return true;
235           }
236
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;
242
243           Drupal.ajax(element_settings);
244         });
245
246     }
247   };
248
249 })(jQuery, Drupal, drupalSettings);