message.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. (function (Drupal) {
  10. Drupal.Message = function () {
  11. function _class() {
  12. var messageWrapper = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  13. _classCallCheck(this, _class);
  14. if (!messageWrapper) {
  15. this.messageWrapper = Drupal.Message.defaultWrapper();
  16. } else {
  17. this.messageWrapper = messageWrapper;
  18. }
  19. }
  20. _createClass(_class, [{
  21. key: 'add',
  22. value: function add(message) {
  23. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  24. if (!options.hasOwnProperty('type')) {
  25. options.type = 'status';
  26. }
  27. if (typeof message !== 'string') {
  28. throw new Error('Message must be a string.');
  29. }
  30. Drupal.Message.announce(message, options);
  31. options.id = options.id ? String(options.id) : options.type + '-' + Math.random().toFixed(15).replace('0.', '');
  32. if (!Drupal.Message.getMessageTypeLabels().hasOwnProperty(options.type)) {
  33. var type = options.type;
  34. throw new Error('The message type, ' + type + ', is not present in Drupal.Message.getMessageTypeLabels().');
  35. }
  36. this.messageWrapper.appendChild(Drupal.theme('message', { text: message }, options));
  37. return options.id;
  38. }
  39. }, {
  40. key: 'select',
  41. value: function select(id) {
  42. return this.messageWrapper.querySelector('[data-drupal-message-id^="' + id + '"]');
  43. }
  44. }, {
  45. key: 'remove',
  46. value: function remove(id) {
  47. return this.messageWrapper.removeChild(this.select(id));
  48. }
  49. }, {
  50. key: 'clear',
  51. value: function clear() {
  52. var _this = this;
  53. Array.prototype.forEach.call(this.messageWrapper.querySelectorAll('[data-drupal-message-id]'), function (message) {
  54. _this.messageWrapper.removeChild(message);
  55. });
  56. }
  57. }], [{
  58. key: 'defaultWrapper',
  59. value: function defaultWrapper() {
  60. var wrapper = document.querySelector('[data-drupal-messages]');
  61. if (!wrapper) {
  62. wrapper = document.querySelector('[data-drupal-messages-fallback]');
  63. wrapper.removeAttribute('data-drupal-messages-fallback');
  64. wrapper.setAttribute('data-drupal-messages', '');
  65. wrapper.classList.remove('hidden');
  66. }
  67. return wrapper.innerHTML === '' ? Drupal.Message.messageInternalWrapper(wrapper) : wrapper.firstElementChild;
  68. }
  69. }, {
  70. key: 'getMessageTypeLabels',
  71. value: function getMessageTypeLabels() {
  72. return {
  73. status: Drupal.t('Status message'),
  74. error: Drupal.t('Error message'),
  75. warning: Drupal.t('Warning message')
  76. };
  77. }
  78. }, {
  79. key: 'announce',
  80. value: function announce(message, options) {
  81. if (!options.priority && (options.type === 'warning' || options.type === 'error')) {
  82. options.priority = 'assertive';
  83. }
  84. if (options.announce !== '') {
  85. Drupal.announce(options.announce || message, options.priority);
  86. }
  87. }
  88. }, {
  89. key: 'messageInternalWrapper',
  90. value: function messageInternalWrapper(messageWrapper) {
  91. var innerWrapper = document.createElement('div');
  92. innerWrapper.setAttribute('class', 'messages__wrapper');
  93. messageWrapper.insertAdjacentElement('afterbegin', innerWrapper);
  94. return innerWrapper;
  95. }
  96. }]);
  97. return _class;
  98. }();
  99. Drupal.theme.message = function (_ref, _ref2) {
  100. var text = _ref.text;
  101. var type = _ref2.type,
  102. id = _ref2.id;
  103. var messagesTypes = Drupal.Message.getMessageTypeLabels();
  104. var messageWrapper = document.createElement('div');
  105. messageWrapper.setAttribute('class', 'messages messages--' + type);
  106. messageWrapper.setAttribute('role', type === 'error' || type === 'warning' ? 'alert' : 'status');
  107. messageWrapper.setAttribute('data-drupal-message-id', id);
  108. messageWrapper.setAttribute('data-drupal-message-type', type);
  109. messageWrapper.setAttribute('aria-label', messagesTypes[type]);
  110. messageWrapper.innerHTML = '' + text;
  111. return messageWrapper;
  112. };
  113. })(Drupal);