PageSourceSearch

https://dcgary.org/themes/contrib/bootstrap/js/misc/message.js?tjxjwj

js dcgary.org collected 2026-09-24 09:37:35 UTC 4,615 bytes, 181 lines download raw bytes

1/**
2 * @file
3 * message.js
4 */
5(function ($, Drupal) {
6
7  /**
8   * Retrieves the classes for a specific message type.
9   *
10   * @param {String} type
11   *   The type of message.
12   *
13   * @return {String}
14   *   The classes to add, space separated.
15   */
16  Drupal.Message.getMessageTypeClass = function (type) {
17    var classes = this.getMessageTypeClasses();
18    return 'alert alert-' + (classes[type] || 'success');
19  };
20
21  /**
22   * Helper function to map Drupal types to Bootstrap classes.
23   *
24   * @return {Object<String, String>}
25   *   A map of classes, keyed by message type.
26   */
27  Drupal.Message.getMessageTypeClasses = function () {
28    return {
29      status: 'success',
30      error: 'danger',
31      warning: 'warning',
32      info: 'info',
33    };
34  };
35
36  /**
37   * Retrieves a label for a specific message type.
38   *
39   * @param {String} type
40   *   The type of message.
41   *
42   * @return {String}
43   *   The message type label.
44   */
45  Drupal.Message.getMessageTypeLabel = function (type) {
46    var labels = this.getMessageTypeLabels();
47    return labels[type];
48  };
49
50  /**
51   * @inheritDoc
52   */
53  Drupal.Message.getMessageTypeLabels = function () {
54    return {
55      status: Drupal.t('Status message'),
56      error: Drupal.t('Error message'),
57      warning: Drupal.t('Warning message'),
58      info: Drupal.t('Informative message'),
59    };
60  };
61
62  /**
63   * Retrieves the aria-role for a specific message type.
64   *
65   * @param {String} type
66   *   The type of message.
67   *
68   * @return {String}
69   *   The message type role.
70   */
71  Drupal.Message.getMessageTypeRole = function (type) {
72    var labels = this.getMessageTypeRoles();
73    return labels[type];
74  };
75
76  /**
77   * Map of the message type aria-role values.
78   *
79   * @return {Object<String, String>}
80   *   A map of roles, keyed by message type.
81   */
82  Drupal.Message.getMessageTypeRoles = function () {
83    return {
84      status: 'status',
85      error: 'alert',
86      warning: 'alert',
87      info: 'status',
88    };
89  };
90
91  /**
92   * @inheritDoc
93   */
94  Drupal.theme.message = function (message, options) {
95    options = options || {};
96    var wrapper = Drupal.theme('messageWrapper', options.id || (new Date()).getTime(), options.type || 'status');
97
98    if (options.dismissible === void 0 || !!options.dismissible) {
99      wrapper.classList.add('alert-dismissible');
100      wrapper.appendChild(Drupal.theme('messageClose'));
101    }
102
103    wrapper.appendChild(Drupal.theme('messageContents', message && message.text));
104
105    return wrapper;
106  };
107
108  /**
109   * Themes the message container.
110   *
111   * @param {String} id
112   *   The message identifier.
113   * @param {String} type
114   *   The type of message.
115   *
116   * @return {HTMLElement}
117   *   A constructed HTMLElement.
118   */
119  Drupal.theme.messageWrapper = function (id, type) {
120    var wrapper = document.createElement('div');
121    var label = Drupal.Message.getMessageTypeLabel(type);
122    wrapper.setAttribute('class', Drupal.Message.getMessageTypeClass(type));
123    wrapper.setAttribute('role', Drupal.Message.getMessageTypeRole(type));
124    wrapper.setAttribute('aria-label', label);
125    wrapper.setAttribute('data-drupal-message-id', id);
126    wrapper.setAttribute('data-drupal-message-type', type);
127    if (label) {
128      wrapper.appendChild(Drupal.theme('messageLabel', label));
129    }
130    return wrapper;
131  };
132
133  /**
134   * Themes the message close button.
135   *
136   * @return {HTMLElement}
137   *   A constructed HTMLElement.
138   */
139  Drupal.theme.messageClose = function () {
140    var element = document.createElement('button');
141    element.setAttribute('class', 'close');
142    element.setAttribute('type', 'button');
143    element.setAttribute('role', 'button');
144    element.setAttribute('data-dismiss', 'alert');
145    element.setAttribute('aria-label', Drupal.t('Close'));
146    element.innerHTML = '<span aria-hidden="true">&times;</span>';
147    return element;
148  };
149
150  /**
151   * Themes the message container.
152   *
153   * @param {String} label
154   *   The message label.
155   *
156   * @return {HTMLElement}
157   *   A constructed HTMLElement.
158   */
159  Drupal.theme.messageLabel = function (label) {
160    var element = document.createElement('h2');
161    element.setAttribute('class', 'sr-only');
162    element.innerHTML = label;
163    return element;
164  };
165
166  /**
167   * Themes the message contents.
168   *
169   * @param {String} html
170   *   The message identifier.
171   *
172   * @return {HTMLElement}
173   *   A constructed HTMLElement.
174   */
175  Drupal.theme.messageContents = function (html) {
176    var element = document.createElement('p');
177    element.innerHTML = '' + html;
178    return element;
179  }
180
181})(window.jQuery, window.Drupal);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.