salix/client/core/src/components/snackbar/snackbar.js

105 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../../module';
import Component from '../../lib/component';
import './style.scss';
2017-05-17 19:23:47 +00:00
/**
* A simple component to show non-obstructive notifications to the user.
*/
export default class Controller extends Component {
constructor($element, $translate) {
super($element);
this.$translate = $translate;
this.shown = false;
2017-05-17 19:23:47 +00:00
this.snackbar = $element[0].firstChild;
this.$snackbar = angular.element(this.snackbar);
this.button = $element[0].querySelector('button');
this.textNode = this.snackbar.querySelector('.text');
2017-05-17 19:23:47 +00:00
}
2018-03-07 14:05:09 +00:00
2017-05-17 19:23:47 +00:00
/**
* Shows a notification.
*
* @param {Object} data The message data
*/
show(data) {
if (this.shown) {
this.hide();
this.onTransitionEnd();
}
2018-05-31 12:40:42 +00:00
this.clearTimeouts();
this.shown = true;
this.textNode.textContent = data.message;
this.actionHandler = data.actionHandler;
this.button.textContent =
data.actionText || this.$translate.instant('Hide');
2018-03-07 14:05:09 +00:00
this.timeoutId = setTimeout(() =>
this.hide(), data.timeout || 6000);
2018-03-07 14:05:09 +00:00
this.transitionTimeout = setTimeout(() =>
this.$snackbar.addClass('shown'), 30);
}
2018-03-07 14:05:09 +00:00
/**
* Shows an error.
*
* @param {Object} data The message data
*/
showError(data) {
this.show(data);
this.$snackbar.addClass('error');
}
2018-03-07 14:05:09 +00:00
/**
* Hides the snackbar.
*/
hide() {
if (!this.shown) return;
clearTimeout(this.timeoutId);
this.shown = false;
this.hideTimeout = setTimeout(() => this.onTransitionEnd(), 250);
this.transitionTimeout =
setTimeout(() => this.$snackbar.removeClass('shown'), 30);
}
2018-03-07 14:05:09 +00:00
onTransitionEnd() {
this.$snackbar.removeClass('error');
this.textNode.textContent = '';
this.button.textContent = '';
this.actionHandler = null;
}
2018-03-07 14:05:09 +00:00
onSnackbarClick(event) {
this.event = event;
}
2018-03-07 14:05:09 +00:00
onButtonClick() {
if (this.actionHandler)
this.actionHandler();
else
this.hide();
}
2018-03-07 14:05:09 +00:00
clearTimeouts() {
clearTimeout(this.timeoutId);
clearTimeout(this.hideTimeout);
clearTimeout(this.transitionTimeout);
this.timeoutId = null;
this.hideTimeout = null;
this.transitionTimeout = null;
}
2018-03-07 14:05:09 +00:00
$onDestroy() {
this.clearTimeouts();
2017-05-17 19:23:47 +00:00
}
}
Controller.$inject = ['$element', '$translate'];
2017-05-17 19:23:47 +00:00
2018-02-10 15:18:01 +00:00
ngModule.component('vnSnackbar', {
2017-05-31 08:28:39 +00:00
template: require('./snackbar.html'),
2017-05-17 19:23:47 +00:00
controller: Controller
});