Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. (() => {
  2. class JoomlaAlertElement extends HTMLElement {
  3. /* Attributes to monitor */
  4. static get observedAttributes() { return ['type', 'dismiss', 'auto-dismiss', 'acknowledge', 'href']; }
  5.  
  6. get type() { return this.getAttribute('type'); }
  7.  
  8. set type(value) { return this.setAttribute('type', value); }
  9.  
  10. get dismiss() { return this.getAttribute('dismiss'); }
  11.  
  12. get autodismiss() { return this.getAttribute('auto-dismiss'); }
  13.  
  14. get acknowledge() { return this.getAttribute('acknowledge'); }
  15.  
  16. get href() { return this.getAttribute('href'); }
  17.  
  18. /* Lifecycle, element appended to the DOM */
  19. connectedCallback() {
  20. this.setAttribute('role', 'alertdialog');
  21. this.classList.add('joomla-alert--show');
  22.  
  23. // Default to info
  24. if (!this.type || ['info', 'warning', 'danger', 'success'].indexOf(this.type) === -1) {
  25. this.setAttribute('type', 'info');
  26. }
  27. // Append button
  28. if ((this.hasAttribute('dismiss') || this.hasAttribute('acknowledge')) || ((this.hasAttribute('href') && this.getAttribute('href') !== '')
  29. && !this.querySelector('button.joomla-alert--close') && !this.querySelector('button.joomla-alert-button--close'))) {
  30. this.appendCloseButton();
  31. }
  32.  
  33. if (this.hasAttribute('auto-dismiss')) {
  34. this.autoDismiss();
  35. }
  36.  
  37. this.dispatchCustomEvent('joomla.alert.show');
  38. }
  39.  
  40. /* Lifecycle, element removed from the DOM */
  41. disconnectedCallback() {
  42. this.removeEventListener('joomla.alert.show', this);
  43. this.removeEventListener('joomla.alert.close', this);
  44. this.removeEventListener('joomla.alert.closed', this);
  45.  
  46. if (this.firstChild.tagName && this.firstChild.tagName.toLowerCase() === 'button') {
  47. this.firstChild.removeEventListener('click', this);
  48. }
  49. }
  50.  
  51. /* Respond to attribute changes */
  52. attributeChangedCallback(attr, oldValue, newValue) {
  53. switch (attr) {
  54. case 'type':
  55. if (!newValue || (newValue && ['info', 'warning', 'danger', 'success'].indexOf(newValue) === -1)) {
  56. this.type = 'info';
  57. }
  58. break;
  59. case 'dismiss':
  60. case 'acknowledge':
  61. if (!newValue || newValue === 'true') {
  62. this.appendCloseButton();
  63. } else {
  64. this.removeCloseButton();
  65. }
  66. break;
  67. case 'auto-dismiss':
  68. this.autoDismiss();
  69. break;
  70. case 'href':
  71. if (!newValue || newValue === '') {
  72. this.removeCloseButton();
  73. } else if (!this.querySelector('button.joomla-alert-button--close')) {
  74. this.appendCloseButton();
  75. }
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81.  
  82. /* Method to close the alert */
  83. close() {
  84. this.dispatchCustomEvent('joomla.alert.close');
  85. this.addEventListener('transitionend', () => {
  86. this.dispatchCustomEvent('joomla.alert.closed');
  87. }, false);
  88. this.classList.remove('joomla-alert--show');
  89. }
  90.  
  91. /* Method to dispatch events */
  92. dispatchCustomEvent(eventName) {
  93. const OriginalCustomEvent = new CustomEvent(eventName);
  94. OriginalCustomEvent.relatedTarget = this;
  95. this.dispatchEvent(OriginalCustomEvent);
  96. this.removeEventListener(eventName, this);
  97. }
  98.  
  99. /* Method to create the close button */
  100. appendCloseButton() {
  101. if (this.querySelector('button.joomla-alert--close') || this.querySelector('button.joomla-alert-button--close')) {
  102. return;
  103. }
  104.  
  105. const self = this;
  106. const closeButton = document.createElement('button');
  107.  
  108. if (this.hasAttribute('dismiss')) {
  109. closeButton.classList.add('joomla-alert--close');
  110. closeButton.innerHTML = '<span aria-hidden="true">&times;</span>';
  111. closeButton.setAttribute('aria-label', this.getText('JCLOSE', 'Close'));
  112. } else {
  113. closeButton.classList.add('joomla-alert-button--close');
  114. if (this.hasAttribute('acknowledge')) {
  115. closeButton.innerHTML = this.getText('JOK', 'ok');
  116. } else {
  117. closeButton.innerHTML = this.getText('JOPEN', 'Open');
  118. }
  119. }
  120.  
  121. if (this.firstChild) {
  122. this.insertBefore(closeButton, this.firstChild);
  123. } else {
  124. this.appendChild(closeButton);
  125. }
  126.  
  127. /* Add the required listener */
  128. if (closeButton) {
  129. if (!this.href) {
  130. closeButton.addEventListener('click', () => {
  131. self.dispatchCustomEvent('joomla.alert.buttonClicked');
  132. if (self.getAttribute('data-callback')) {
  133. window[self.getAttribute('data-callback')]();
  134. self.close();
  135. } else {
  136. self.close();
  137. }
  138. });
  139. } else {
  140. closeButton.addEventListener('click', () => {
  141. self.dispatchCustomEvent('joomla.alert.buttonClicked');
  142. window.location.href = self.href;
  143. self.close();
  144. });
  145. }
  146. }
  147. }
  148.  
  149. autoDismiss() {
  150. const self = this;
  151. setTimeout(() => {
  152. self.dispatchCustomEvent('joomla.alert.buttonClicked');
  153. if (self.hasAttribute('data-callback')) {
  154. window[self.getAttribute('data-callback')]();
  155. } else {
  156. self.close();
  157. }
  158. }, parseInt(self.getAttribute('auto-dismiss'), 10) ? self.getAttribute('auto-dismiss') : 3000);
  159. }
  160.  
  161. /* Method to remove the close button */
  162. removeCloseButton() {
  163. const button = this.querySelector('button');
  164. if (button) {
  165. button.removeEventListener('click', this);
  166. button.parentNode.removeChild(button);
  167. }
  168. }
  169.  
  170. /* Method to get the translated text */
  171. getText(str, fallback) {
  172. // TODO: Remove coupling to Joomla CMS Core JS here
  173. /* eslint-disable-next-line no-undef */
  174. return (window.Joomla && Joomla.JText && Joomla.JText._ && typeof Joomla.JText._ === 'function' && Joomla.JText._(str)) ? Joomla.JText._(str) : fallback;
  175. }
  176. }
  177.  
  178. customElements.define('joomla-alert', JoomlaAlertElement);
  179. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement