Chris@18
|
1 /**
|
Chris@18
|
2 * DO NOT EDIT THIS FILE.
|
Chris@18
|
3 * See the following change record for more information,
|
Chris@18
|
4 * https://www.drupal.org/node/2815083
|
Chris@18
|
5 * @preserve
|
Chris@18
|
6 **/
|
Chris@18
|
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; }; }();
|
Chris@18
|
8
|
Chris@18
|
9 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
Chris@18
|
10
|
Chris@18
|
11 (function (Drupal) {
|
Chris@18
|
12 Drupal.Message = function () {
|
Chris@18
|
13 function _class() {
|
Chris@18
|
14 var messageWrapper = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
Chris@18
|
15
|
Chris@18
|
16 _classCallCheck(this, _class);
|
Chris@18
|
17
|
Chris@18
|
18 this.messageWrapper = messageWrapper;
|
Chris@18
|
19 }
|
Chris@18
|
20
|
Chris@18
|
21 _createClass(_class, [{
|
Chris@18
|
22 key: 'add',
|
Chris@18
|
23 value: function add(message) {
|
Chris@18
|
24 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
Chris@18
|
25
|
Chris@18
|
26 if (!this.messageWrapper) {
|
Chris@18
|
27 this.messageWrapper = Drupal.Message.defaultWrapper();
|
Chris@18
|
28 }
|
Chris@18
|
29 if (!options.hasOwnProperty('type')) {
|
Chris@18
|
30 options.type = 'status';
|
Chris@18
|
31 }
|
Chris@18
|
32
|
Chris@18
|
33 if (typeof message !== 'string') {
|
Chris@18
|
34 throw new Error('Message must be a string.');
|
Chris@18
|
35 }
|
Chris@18
|
36
|
Chris@18
|
37 Drupal.Message.announce(message, options);
|
Chris@18
|
38
|
Chris@18
|
39 options.id = options.id ? String(options.id) : options.type + '-' + Math.random().toFixed(15).replace('0.', '');
|
Chris@18
|
40
|
Chris@18
|
41 if (!Drupal.Message.getMessageTypeLabels().hasOwnProperty(options.type)) {
|
Chris@18
|
42 throw new Error('The message type, ' + options.type + ', is not present in Drupal.Message.getMessageTypeLabels().');
|
Chris@18
|
43 }
|
Chris@18
|
44
|
Chris@18
|
45 this.messageWrapper.appendChild(Drupal.theme('message', { text: message }, options));
|
Chris@18
|
46
|
Chris@18
|
47 return options.id;
|
Chris@18
|
48 }
|
Chris@18
|
49 }, {
|
Chris@18
|
50 key: 'select',
|
Chris@18
|
51 value: function select(id) {
|
Chris@18
|
52 return this.messageWrapper.querySelector('[data-drupal-message-id^="' + id + '"]');
|
Chris@18
|
53 }
|
Chris@18
|
54 }, {
|
Chris@18
|
55 key: 'remove',
|
Chris@18
|
56 value: function remove(id) {
|
Chris@18
|
57 return this.messageWrapper.removeChild(this.select(id));
|
Chris@18
|
58 }
|
Chris@18
|
59 }, {
|
Chris@18
|
60 key: 'clear',
|
Chris@18
|
61 value: function clear() {
|
Chris@18
|
62 var _this = this;
|
Chris@18
|
63
|
Chris@18
|
64 Array.prototype.forEach.call(this.messageWrapper.querySelectorAll('[data-drupal-message-id]'), function (message) {
|
Chris@18
|
65 _this.messageWrapper.removeChild(message);
|
Chris@18
|
66 });
|
Chris@18
|
67 }
|
Chris@18
|
68 }], [{
|
Chris@18
|
69 key: 'defaultWrapper',
|
Chris@18
|
70 value: function defaultWrapper() {
|
Chris@18
|
71 var wrapper = document.querySelector('[data-drupal-messages]');
|
Chris@18
|
72 if (!wrapper) {
|
Chris@18
|
73 wrapper = document.querySelector('[data-drupal-messages-fallback]');
|
Chris@18
|
74 wrapper.removeAttribute('data-drupal-messages-fallback');
|
Chris@18
|
75 wrapper.setAttribute('data-drupal-messages', '');
|
Chris@18
|
76 wrapper.removeAttribute('class');
|
Chris@18
|
77 }
|
Chris@18
|
78 return wrapper.innerHTML === '' ? Drupal.Message.messageInternalWrapper(wrapper) : wrapper.firstElementChild;
|
Chris@18
|
79 }
|
Chris@18
|
80 }, {
|
Chris@18
|
81 key: 'getMessageTypeLabels',
|
Chris@18
|
82 value: function getMessageTypeLabels() {
|
Chris@18
|
83 return {
|
Chris@18
|
84 status: Drupal.t('Status message'),
|
Chris@18
|
85 error: Drupal.t('Error message'),
|
Chris@18
|
86 warning: Drupal.t('Warning message')
|
Chris@18
|
87 };
|
Chris@18
|
88 }
|
Chris@18
|
89 }, {
|
Chris@18
|
90 key: 'announce',
|
Chris@18
|
91 value: function announce(message, options) {
|
Chris@18
|
92 if (!options.priority && (options.type === 'warning' || options.type === 'error')) {
|
Chris@18
|
93 options.priority = 'assertive';
|
Chris@18
|
94 }
|
Chris@18
|
95
|
Chris@18
|
96 if (options.announce !== '') {
|
Chris@18
|
97 Drupal.announce(options.announce || message, options.priority);
|
Chris@18
|
98 }
|
Chris@18
|
99 }
|
Chris@18
|
100 }, {
|
Chris@18
|
101 key: 'messageInternalWrapper',
|
Chris@18
|
102 value: function messageInternalWrapper(messageWrapper) {
|
Chris@18
|
103 var innerWrapper = document.createElement('div');
|
Chris@18
|
104 innerWrapper.setAttribute('class', 'messages__wrapper');
|
Chris@18
|
105 messageWrapper.insertAdjacentElement('afterbegin', innerWrapper);
|
Chris@18
|
106 return innerWrapper;
|
Chris@18
|
107 }
|
Chris@18
|
108 }]);
|
Chris@18
|
109
|
Chris@18
|
110 return _class;
|
Chris@18
|
111 }();
|
Chris@18
|
112
|
Chris@18
|
113 Drupal.theme.message = function (_ref, _ref2) {
|
Chris@18
|
114 var text = _ref.text;
|
Chris@18
|
115 var type = _ref2.type,
|
Chris@18
|
116 id = _ref2.id;
|
Chris@18
|
117
|
Chris@18
|
118 var messagesTypes = Drupal.Message.getMessageTypeLabels();
|
Chris@18
|
119 var messageWrapper = document.createElement('div');
|
Chris@18
|
120
|
Chris@18
|
121 messageWrapper.setAttribute('class', 'messages messages--' + type);
|
Chris@18
|
122 messageWrapper.setAttribute('role', type === 'error' || type === 'warning' ? 'alert' : 'status');
|
Chris@18
|
123 messageWrapper.setAttribute('data-drupal-message-id', id);
|
Chris@18
|
124 messageWrapper.setAttribute('data-drupal-message-type', type);
|
Chris@18
|
125
|
Chris@18
|
126 messageWrapper.setAttribute('aria-label', messagesTypes[type]);
|
Chris@18
|
127
|
Chris@18
|
128 messageWrapper.innerHTML = '' + text;
|
Chris@18
|
129
|
Chris@18
|
130 return messageWrapper;
|
Chris@18
|
131 };
|
Chris@18
|
132 })(Drupal); |