Skip to content

Commit 792cca2

Browse files
brianteemanwilsonge
authored andcommitted
auto-dismiss (#128)
1 parent 8989142 commit 792cca2

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/js/alert/alert.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
get dismiss() { return this.getAttribute('dismiss'); }
1515

16+
get autodismiss() { return this.getAttribute('auto-dismiss'); }
17+
1618
get acknowledge() { return this.getAttribute('acknowledge'); }
1719

1820
get href() { return this.getAttribute('href'); }
@@ -35,6 +37,10 @@
3537
this.appendCloseButton();
3638
}
3739

40+
if (this.hasAttribute('auto-dismiss')) {
41+
this.autoDismiss();
42+
}
43+
3844
this.dispatchCustomEvent('joomla.alert.show');
3945
}
4046

@@ -70,6 +76,9 @@
7076
this.removeCloseButton();
7177
}
7278
break;
79+
case 'auto-dismiss':
80+
this.autoDismiss();
81+
break;
7382
case 'href':
7483
if (!newValue || newValue === '') {
7584
this.removeCloseButton();
@@ -83,11 +92,15 @@
8392
}
8493

8594
/* Method to close the alert */
86-
close() {
95+
close(element = null) {
8796
this.dispatchCustomEvent('joomla.alert.close');
8897
this.addEventListener('transitionend', () => {
8998
this.dispatchCustomEvent('joomla.alert.closed');
90-
this.parentNode.removeChild(this);
99+
if (element) {
100+
element.remove();
101+
} else {
102+
this.remove();
103+
}
91104
}, false);
92105
this.classList.remove('joomla-alert--show');
93106
}
@@ -148,25 +161,27 @@
148161
});
149162
}
150163
}
164+
}
151165

152-
if (this.hasAttribute('auto-dismiss')) {
153-
setTimeout(() => {
154-
self.dispatchCustomEvent('joomla.alert.buttonClicked');
155-
if (self.hasAttribute('data-callback')) {
156-
window[self.getAttribute('data-callback')]();
157-
} else {
158-
self.close();
159-
}
160-
}, self.getAttribute('auto-dismiss') ? parseInt(self.getAttribute('auto-dismiss'), 10) : 3000);
161-
}
166+
/* Method to auto-dismiss */
167+
autoDismiss() {
168+
const self = this;
169+
setTimeout(() => {
170+
self.dispatchCustomEvent('joomla.alert.buttonClicked');
171+
if (self.hasAttribute('data-callback')) {
172+
window[self.getAttribute('data-callback')]();
173+
} else {
174+
self.close(self);
175+
}
176+
}, parseInt(self.getAttribute('auto-dismiss'), 10) ? self.getAttribute('auto-dismiss') : 3000);
162177
}
163178

164179
/* Method to remove the close button */
165180
removeCloseButton() {
166181
const button = this.querySelector('button');
167182
if (button) {
168183
button.removeEventListener('click', this);
169-
button.parentNode.removeChild(button);
184+
button.remove();
170185
}
171186
}
172187

0 commit comments

Comments
 (0)