bootstrap.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /* global FORM_KEY */
  6. define([
  7. 'jquery',
  8. 'mage/apply/main',
  9. 'mage/backend/notification',
  10. 'Magento_Ui/js/lib/knockout/bootstrap',
  11. 'mage/mage',
  12. 'mage/translate'
  13. ], function ($, mage, notification) {
  14. 'use strict';
  15. var bootstrap;
  16. $.ajaxSetup({
  17. /*
  18. * @type {string}
  19. */
  20. type: 'POST',
  21. /**
  22. * Ajax before send callback.
  23. *
  24. * @param {Object} jqXHR - The jQuery XMLHttpRequest object returned by $.ajax()
  25. * @param {Object} settings
  26. */
  27. beforeSend: function (jqXHR, settings) {
  28. var formKey = typeof FORM_KEY !== 'undefined' ? FORM_KEY : null;
  29. if (!settings.url.match(new RegExp('[?&]isAjax=true',''))) {
  30. settings.url = settings.url.match(
  31. new RegExp('\\?', 'g')) ?
  32. settings.url + '&isAjax=true' :
  33. settings.url + '?isAjax=true';
  34. }
  35. if (!settings.data) {
  36. settings.data = {
  37. 'form_key': formKey
  38. };
  39. } else if ($.type(settings.data) === 'string' &&
  40. settings.data.indexOf('form_key=') === -1) {
  41. settings.data += '&' + $.param({
  42. 'form_key': formKey
  43. });
  44. } else if ($.isPlainObject(settings.data) && !settings.data['form_key']) {
  45. settings.data['form_key'] = formKey;
  46. }
  47. },
  48. /**
  49. * Ajax complete callback.
  50. *
  51. * @param {Object} jqXHR - The jQuery XMLHttpRequest object returned by $.ajax()
  52. */
  53. complete: function (jqXHR) {
  54. var jsonObject;
  55. if (jqXHR.readyState === 4) {
  56. try {
  57. jsonObject = $.parseJSON(jqXHR.responseText);
  58. if (jsonObject.ajaxExpired && jsonObject.ajaxRedirect) { //eslint-disable-line max-depth
  59. window.location.replace(jsonObject.ajaxRedirect);
  60. }
  61. } catch (e) {}
  62. }
  63. },
  64. /**
  65. * Error callback.
  66. */
  67. error: function () {
  68. $('body').notification('clear')
  69. .notification('add', {
  70. error: true,
  71. message: $.mage.__(
  72. 'A technical problem with the server created an error. ' +
  73. 'Try again to continue what you were doing. If the problem persists, try again later.'
  74. ),
  75. /**
  76. * @param {String} message
  77. */
  78. insertMethod: function (message) {
  79. var $wrapper = $('<div/>').html(message);
  80. $('.page-main-actions').after($wrapper);
  81. }
  82. });
  83. }
  84. });
  85. /**
  86. * Bootstrap application.
  87. */
  88. bootstrap = function () {
  89. /**
  90. * Init all components defined via data-mage-init attribute
  91. * and subscribe init action on contentUpdated event
  92. */
  93. mage.apply();
  94. /*
  95. * Initialization of notification widget
  96. */
  97. notification({}, $('body'));
  98. };
  99. $(bootstrap);
  100. });