annotate core/misc/ajax.js @ 19:fa3358dc1485 tip

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