Security update for Core, with self-updated composer
[yaffs-website] / web / core / misc / ajax.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7 function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
8
9 (function ($, window, Drupal, drupalSettings) {
10   Drupal.behaviors.AJAX = {
11     attach: function attach(context, settings) {
12       function loadAjaxBehavior(base) {
13         var element_settings = settings.ajax[base];
14         if (typeof element_settings.selector === 'undefined') {
15           element_settings.selector = '#' + base;
16         }
17         $(element_settings.selector).once('drupal-ajax').each(function () {
18           element_settings.element = this;
19           element_settings.base = base;
20           Drupal.ajax(element_settings);
21         });
22       }
23
24       for (var base in settings.ajax) {
25         if (settings.ajax.hasOwnProperty(base)) {
26           loadAjaxBehavior(base);
27         }
28       }
29
30       $('.use-ajax').once('ajax').each(function () {
31         var element_settings = {};
32
33         element_settings.progress = { type: 'throbber' };
34
35         var href = $(this).attr('href');
36         if (href) {
37           element_settings.url = href;
38           element_settings.event = 'click';
39         }
40         element_settings.dialogType = $(this).data('dialog-type');
41         element_settings.dialogRenderer = $(this).data('dialog-renderer');
42         element_settings.dialog = $(this).data('dialog-options');
43         element_settings.base = $(this).attr('id');
44         element_settings.element = this;
45         Drupal.ajax(element_settings);
46       });
47
48       $('.use-ajax-submit').once('ajax').each(function () {
49         var element_settings = {};
50
51         element_settings.url = $(this.form).attr('action');
52
53         element_settings.setClick = true;
54
55         element_settings.event = 'click';
56
57         element_settings.progress = { type: 'throbber' };
58         element_settings.base = $(this).attr('id');
59         element_settings.element = this;
60
61         Drupal.ajax(element_settings);
62       });
63     },
64     detach: function detach(context, settings, trigger) {
65       if (trigger === 'unload') {
66         Drupal.ajax.expired().forEach(function (instance) {
67           Drupal.ajax.instances[instance.instanceIndex] = null;
68         });
69       }
70     }
71   };
72
73   Drupal.AjaxError = function (xmlhttp, uri, customMessage) {
74     var statusCode = void 0;
75     var statusText = void 0;
76     var pathText = void 0;
77     var responseText = void 0;
78     var readyStateText = void 0;
79     if (xmlhttp.status) {
80       statusCode = '\n' + Drupal.t('An AJAX HTTP error occurred.') + '\n' + Drupal.t('HTTP Result Code: !status', { '!status': xmlhttp.status });
81     } else {
82       statusCode = '\n' + Drupal.t('An AJAX HTTP request terminated abnormally.');
83     }
84     statusCode += '\n' + Drupal.t('Debugging information follows.');
85     pathText = '\n' + Drupal.t('Path: !uri', { '!uri': uri });
86     statusText = '';
87
88     try {
89       statusText = '\n' + Drupal.t('StatusText: !statusText', { '!statusText': $.trim(xmlhttp.statusText) });
90     } catch (e) {}
91
92     responseText = '';
93
94     try {
95       responseText = '\n' + Drupal.t('ResponseText: !responseText', { '!responseText': $.trim(xmlhttp.responseText) });
96     } catch (e) {}
97
98     responseText = responseText.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '');
99     responseText = responseText.replace(/[\n]+\s+/g, '\n');
100
101     readyStateText = xmlhttp.status === 0 ? '\n' + Drupal.t('ReadyState: !readyState', { '!readyState': xmlhttp.readyState }) : '';
102
103     customMessage = customMessage ? '\n' + Drupal.t('CustomMessage: !customMessage', { '!customMessage': customMessage }) : '';
104
105     this.message = statusCode + pathText + statusText + customMessage + responseText + readyStateText;
106
107     this.name = 'AjaxError';
108   };
109
110   Drupal.AjaxError.prototype = new Error();
111   Drupal.AjaxError.prototype.constructor = Drupal.AjaxError;
112
113   Drupal.ajax = function (settings) {
114     if (arguments.length !== 1) {
115       throw new Error('Drupal.ajax() function must be called with one configuration object only');
116     }
117
118     var base = settings.base || false;
119     var element = settings.element || false;
120     delete settings.base;
121     delete settings.element;
122
123     if (!settings.progress && !element) {
124       settings.progress = false;
125     }
126
127     var ajax = new Drupal.Ajax(base, element, settings);
128     ajax.instanceIndex = Drupal.ajax.instances.length;
129     Drupal.ajax.instances.push(ajax);
130
131     return ajax;
132   };
133
134   Drupal.ajax.instances = [];
135
136   Drupal.ajax.expired = function () {
137     return Drupal.ajax.instances.filter(function (instance) {
138       return instance && instance.element !== false && !document.body.contains(instance.element);
139     });
140   };
141
142   Drupal.Ajax = function (base, element, element_settings) {
143     var defaults = {
144       event: element ? 'mousedown' : null,
145       keypress: true,
146       selector: base ? '#' + base : null,
147       effect: 'none',
148       speed: 'none',
149       method: 'replaceWith',
150       progress: {
151         type: 'throbber',
152         message: Drupal.t('Please wait...')
153       },
154       submit: {
155         js: true
156       }
157     };
158
159     $.extend(this, defaults, element_settings);
160
161     this.commands = new Drupal.AjaxCommands();
162
163     this.instanceIndex = false;
164
165     if (this.wrapper) {
166       this.wrapper = '#' + this.wrapper;
167     }
168
169     this.element = element;
170
171     this.element_settings = element_settings;
172
173     if (this.element && this.element.form) {
174       this.$form = $(this.element.form);
175     }
176
177     if (!this.url) {
178       var $element = $(this.element);
179       if ($element.is('a')) {
180         this.url = $element.attr('href');
181       } else if (this.element && element.form) {
182         this.url = this.$form.attr('action');
183       }
184     }
185
186     var originalUrl = this.url;
187
188     this.url = this.url.replace(/\/nojs(\/|$|\?|#)/g, '/ajax$1');
189
190     if (drupalSettings.ajaxTrustedUrl[originalUrl]) {
191       drupalSettings.ajaxTrustedUrl[this.url] = true;
192     }
193
194     var ajax = this;
195
196     ajax.options = {
197       url: ajax.url,
198       data: ajax.submit,
199       beforeSerialize: function beforeSerialize(element_settings, options) {
200         return ajax.beforeSerialize(element_settings, options);
201       },
202       beforeSubmit: function beforeSubmit(form_values, element_settings, options) {
203         ajax.ajaxing = true;
204         return ajax.beforeSubmit(form_values, element_settings, options);
205       },
206       beforeSend: function beforeSend(xmlhttprequest, options) {
207         ajax.ajaxing = true;
208         return ajax.beforeSend(xmlhttprequest, options);
209       },
210       success: function success(response, status, xmlhttprequest) {
211         if (typeof response === 'string') {
212           response = $.parseJSON(response);
213         }
214
215         if (response !== null && !drupalSettings.ajaxTrustedUrl[ajax.url]) {
216           if (xmlhttprequest.getResponseHeader('X-Drupal-Ajax-Token') !== '1') {
217             var customMessage = Drupal.t('The response failed verification so will not be processed.');
218             return ajax.error(xmlhttprequest, ajax.url, customMessage);
219           }
220         }
221
222         return ajax.success(response, status);
223       },
224       complete: function complete(xmlhttprequest, status) {
225         ajax.ajaxing = false;
226         if (status === 'error' || status === 'parsererror') {
227           return ajax.error(xmlhttprequest, ajax.url);
228         }
229       },
230
231       dataType: 'json',
232       type: 'POST'
233     };
234
235     if (element_settings.dialog) {
236       ajax.options.data.dialogOptions = element_settings.dialog;
237     }
238
239     if (ajax.options.url.indexOf('?') === -1) {
240       ajax.options.url += '?';
241     } else {
242       ajax.options.url += '&';
243     }
244
245     var wrapper = 'drupal_' + (element_settings.dialogType || 'ajax');
246     if (element_settings.dialogRenderer) {
247       wrapper += '.' + element_settings.dialogRenderer;
248     }
249     ajax.options.url += Drupal.ajax.WRAPPER_FORMAT + '=' + wrapper;
250
251     $(ajax.element).on(element_settings.event, function (event) {
252       if (!drupalSettings.ajaxTrustedUrl[ajax.url] && !Drupal.url.isLocal(ajax.url)) {
253         throw new Error(Drupal.t('The callback URL is not local and not trusted: !url', { '!url': ajax.url }));
254       }
255       return ajax.eventResponse(this, event);
256     });
257
258     if (element_settings.keypress) {
259       $(ajax.element).on('keypress', function (event) {
260         return ajax.keypressResponse(this, event);
261       });
262     }
263
264     if (element_settings.prevent) {
265       $(ajax.element).on(element_settings.prevent, false);
266     }
267   };
268
269   Drupal.ajax.WRAPPER_FORMAT = '_wrapper_format';
270
271   Drupal.Ajax.AJAX_REQUEST_PARAMETER = '_drupal_ajax';
272
273   Drupal.Ajax.prototype.execute = function () {
274     if (this.ajaxing) {
275       return;
276     }
277
278     try {
279       this.beforeSerialize(this.element, this.options);
280
281       return $.ajax(this.options);
282     } catch (e) {
283       this.ajaxing = false;
284       window.alert('An error occurred while attempting to process ' + this.options.url + ': ' + e.message);
285
286       return $.Deferred().reject();
287     }
288   };
289
290   Drupal.Ajax.prototype.keypressResponse = function (element, event) {
291     var ajax = this;
292
293     if (event.which === 13 || event.which === 32 && element.type !== 'text' && element.type !== 'textarea' && element.type !== 'tel' && element.type !== 'number') {
294       event.preventDefault();
295       event.stopPropagation();
296       $(element).trigger(ajax.element_settings.event);
297     }
298   };
299
300   Drupal.Ajax.prototype.eventResponse = function (element, event) {
301     event.preventDefault();
302     event.stopPropagation();
303
304     var ajax = this;
305
306     if (ajax.ajaxing) {
307       return;
308     }
309
310     try {
311       if (ajax.$form) {
312         if (ajax.setClick) {
313           element.form.clk = element;
314         }
315
316         ajax.$form.ajaxSubmit(ajax.options);
317       } else {
318         ajax.beforeSerialize(ajax.element, ajax.options);
319         $.ajax(ajax.options);
320       }
321     } catch (e) {
322       ajax.ajaxing = false;
323       window.alert('An error occurred while attempting to process ' + ajax.options.url + ': ' + e.message);
324     }
325   };
326
327   Drupal.Ajax.prototype.beforeSerialize = function (element, options) {
328     if (this.$form) {
329       var settings = this.settings || drupalSettings;
330       Drupal.detachBehaviors(this.$form.get(0), settings, 'serialize');
331     }
332
333     options.data[Drupal.Ajax.AJAX_REQUEST_PARAMETER] = 1;
334
335     var pageState = drupalSettings.ajaxPageState;
336     options.data['ajax_page_state[theme]'] = pageState.theme;
337     options.data['ajax_page_state[theme_token]'] = pageState.theme_token;
338     options.data['ajax_page_state[libraries]'] = pageState.libraries;
339   };
340
341   Drupal.Ajax.prototype.beforeSubmit = function (form_values, element, options) {};
342
343   Drupal.Ajax.prototype.beforeSend = function (xmlhttprequest, options) {
344     if (this.$form) {
345       options.extraData = options.extraData || {};
346
347       options.extraData.ajax_iframe_upload = '1';
348
349       var v = $.fieldValue(this.element);
350       if (v !== null) {
351         options.extraData[this.element.name] = v;
352       }
353     }
354
355     $(this.element).prop('disabled', true);
356
357     if (!this.progress || !this.progress.type) {
358       return;
359     }
360
361     var progressIndicatorMethod = 'setProgressIndicator' + this.progress.type.slice(0, 1).toUpperCase() + this.progress.type.slice(1).toLowerCase();
362     if (progressIndicatorMethod in this && typeof this[progressIndicatorMethod] === 'function') {
363       this[progressIndicatorMethod].call(this);
364     }
365   };
366
367   Drupal.Ajax.prototype.setProgressIndicatorBar = function () {
368     var progressBar = new Drupal.ProgressBar('ajax-progress-' + this.element.id, $.noop, this.progress.method, $.noop);
369     if (this.progress.message) {
370       progressBar.setProgress(-1, this.progress.message);
371     }
372     if (this.progress.url) {
373       progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500);
374     }
375     this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar');
376     this.progress.object = progressBar;
377     $(this.element).after(this.progress.element);
378   };
379
380   Drupal.Ajax.prototype.setProgressIndicatorThrobber = function () {
381     this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber">&nbsp;</div></div>');
382     if (this.progress.message) {
383       this.progress.element.find('.throbber').after('<div class="message">' + this.progress.message + '</div>');
384     }
385     $(this.element).after(this.progress.element);
386   };
387
388   Drupal.Ajax.prototype.setProgressIndicatorFullscreen = function () {
389     this.progress.element = $('<div class="ajax-progress ajax-progress-fullscreen">&nbsp;</div>');
390     $('body').after(this.progress.element);
391   };
392
393   Drupal.Ajax.prototype.success = function (response, status) {
394     if (this.progress.element) {
395       $(this.progress.element).remove();
396     }
397     if (this.progress.object) {
398       this.progress.object.stopMonitoring();
399     }
400     $(this.element).prop('disabled', false);
401
402     var elementParents = $(this.element).parents('[data-drupal-selector]').addBack().toArray();
403
404     var focusChanged = false;
405     for (var i in response) {
406       if (response.hasOwnProperty(i) && response[i].command && this.commands[response[i].command]) {
407         this.commands[response[i].command](this, response[i], status);
408         if (response[i].command === 'invoke' && response[i].method === 'focus') {
409           focusChanged = true;
410         }
411       }
412     }
413
414     if (!focusChanged && this.element && !$(this.element).data('disable-refocus')) {
415       var target = false;
416
417       for (var n = elementParents.length - 1; !target && n > 0; n--) {
418         target = document.querySelector('[data-drupal-selector="' + elementParents[n].getAttribute('data-drupal-selector') + '"]');
419       }
420
421       if (target) {
422         $(target).trigger('focus');
423       }
424     }
425
426     if (this.$form) {
427       var settings = this.settings || drupalSettings;
428       Drupal.attachBehaviors(this.$form.get(0), settings);
429     }
430
431     this.settings = null;
432   };
433
434   Drupal.Ajax.prototype.getEffect = function (response) {
435     var type = response.effect || this.effect;
436     var speed = response.speed || this.speed;
437
438     var effect = {};
439     if (type === 'none') {
440       effect.showEffect = 'show';
441       effect.hideEffect = 'hide';
442       effect.showSpeed = '';
443     } else if (type === 'fade') {
444       effect.showEffect = 'fadeIn';
445       effect.hideEffect = 'fadeOut';
446       effect.showSpeed = speed;
447     } else {
448       effect.showEffect = type + 'Toggle';
449       effect.hideEffect = type + 'Toggle';
450       effect.showSpeed = speed;
451     }
452
453     return effect;
454   };
455
456   Drupal.Ajax.prototype.error = function (xmlhttprequest, uri, customMessage) {
457     if (this.progress.element) {
458       $(this.progress.element).remove();
459     }
460     if (this.progress.object) {
461       this.progress.object.stopMonitoring();
462     }
463
464     $(this.wrapper).show();
465
466     $(this.element).prop('disabled', false);
467
468     if (this.$form) {
469       var settings = this.settings || drupalSettings;
470       Drupal.attachBehaviors(this.$form.get(0), settings);
471     }
472     throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage);
473   };
474
475   Drupal.AjaxCommands = function () {};
476   Drupal.AjaxCommands.prototype = {
477     insert: function insert(ajax, response, status) {
478       var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
479       var method = response.method || ajax.method;
480       var effect = ajax.getEffect(response);
481       var settings = void 0;
482
483       var $new_content_wrapped = $('<div></div>').html(response.data);
484       var $new_content = $new_content_wrapped.contents();
485
486       if ($new_content.length !== 1 || $new_content.get(0).nodeType !== 1) {
487         $new_content = $new_content_wrapped;
488       }
489
490       switch (method) {
491         case 'html':
492         case 'replaceWith':
493         case 'replaceAll':
494         case 'empty':
495         case 'remove':
496           settings = response.settings || ajax.settings || drupalSettings;
497           Drupal.detachBehaviors($wrapper.get(0), settings);
498       }
499
500       $wrapper[method]($new_content);
501
502       if (effect.showEffect !== 'show') {
503         $new_content.hide();
504       }
505
506       if ($new_content.find('.ajax-new-content').length > 0) {
507         $new_content.find('.ajax-new-content').hide();
508         $new_content.show();
509         $new_content.find('.ajax-new-content')[effect.showEffect](effect.showSpeed);
510       } else if (effect.showEffect !== 'show') {
511         $new_content[effect.showEffect](effect.showSpeed);
512       }
513
514       if ($new_content.parents('html').length > 0) {
515         settings = response.settings || ajax.settings || drupalSettings;
516         Drupal.attachBehaviors($new_content.get(0), settings);
517       }
518     },
519     remove: function remove(ajax, response, status) {
520       var settings = response.settings || ajax.settings || drupalSettings;
521       $(response.selector).each(function () {
522         Drupal.detachBehaviors(this, settings);
523       }).remove();
524     },
525     changed: function changed(ajax, response, status) {
526       var $element = $(response.selector);
527       if (!$element.hasClass('ajax-changed')) {
528         $element.addClass('ajax-changed');
529         if (response.asterisk) {
530           $element.find(response.asterisk).append(' <abbr class="ajax-changed" title="' + Drupal.t('Changed') + '">*</abbr> ');
531         }
532       }
533     },
534     alert: function alert(ajax, response, status) {
535       window.alert(response.text, response.title);
536     },
537     redirect: function redirect(ajax, response, status) {
538       window.location = response.url;
539     },
540     css: function css(ajax, response, status) {
541       $(response.selector).css(response.argument);
542     },
543     settings: function settings(ajax, response, status) {
544       var ajaxSettings = drupalSettings.ajax;
545
546       if (ajaxSettings) {
547         Drupal.ajax.expired().forEach(function (instance) {
548
549           if (instance.selector) {
550             var selector = instance.selector.replace('#', '');
551             if (selector in ajaxSettings) {
552               delete ajaxSettings[selector];
553             }
554           }
555         });
556       }
557
558       if (response.merge) {
559         $.extend(true, drupalSettings, response.settings);
560       } else {
561         ajax.settings = response.settings;
562       }
563     },
564     data: function data(ajax, response, status) {
565       $(response.selector).data(response.name, response.value);
566     },
567     invoke: function invoke(ajax, response, status) {
568       var $element = $(response.selector);
569       $element[response.method].apply($element, _toConsumableArray(response.args));
570     },
571     restripe: function restripe(ajax, response, status) {
572       $(response.selector).find('> tbody > tr:visible, > tr:visible').removeClass('odd even').filter(':even').addClass('odd').end().filter(':odd').addClass('even');
573     },
574     update_build_id: function update_build_id(ajax, response, status) {
575       $('input[name="form_build_id"][value="' + response.old + '"]').val(response.new);
576     },
577     add_css: function add_css(ajax, response, status) {
578       $('head').prepend(response.data);
579
580       var match = void 0;
581       var importMatch = /^@import url\("(.*)"\);$/igm;
582       if (document.styleSheets[0].addImport && importMatch.test(response.data)) {
583         importMatch.lastIndex = 0;
584         do {
585           match = importMatch.exec(response.data);
586           document.styleSheets[0].addImport(match[1]);
587         } while (match);
588       }
589     }
590   };
591 })(jQuery, window, Drupal, drupalSettings);