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