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