salix/front/core/components/snackbar/snackbar.js

123 lines
2.9 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;
2017-05-17 19:23:47 +00:00
this.snackbar = $element[0].firstChild;
this.$snackbar = angular.element(this.snackbar);
2018-06-19 06:12:36 +00:00
}
/**
* It creates a new snackbar notification
* @param {Object} data Snackbar data
* @return {Object} Created snackbar shape
*/
createShape(data) {
let shape = document.createElement('div');
shape.className = 'shape';
let button = document.createElement('button');
let buttonText = data.actionText || this.$translate.instant('Hide');
buttonText = document.createTextNode(buttonText);
button.appendChild(buttonText);
button.addEventListener('click', () => {
this.onButtonClick(shape);
});
shape.appendChild(button);
let shapeText = document.createElement('div');
shapeText.setAttribute('class', 'text');
shape.appendChild(shapeText);
let text = document.createTextNode(data.message);
shapeText.appendChild(text);
if (data.shapeType)
shape.classList.add(data.shapeType);
let parent = this.snackbar.querySelectorAll('.shape')[0];
2019-12-18 11:49:20 +00:00
if (parent)
2018-06-19 06:12:36 +00:00
this.snackbar.insertBefore(shape, parent);
2019-12-18 11:49:20 +00:00
else
2018-06-19 06:12:36 +00:00
this.snackbar.appendChild(shape);
2019-12-18 11:49:20 +00:00
2018-06-19 06:12:36 +00:00
return shape;
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) {
this.actionHandler = data.actionHandler;
2018-06-19 06:12:36 +00:00
let shape = this.createShape(data);
2018-06-19 06:12:36 +00:00
setTimeout(() =>
this.hide(shape), data.timeout || 3000);
2018-06-19 06:12:36 +00:00
setTimeout(() =>
shape.classList.add('shown'), 30);
}
2018-03-07 14:05:09 +00:00
/**
* Shows an error.
*
* @param {Object} data The message data
*/
showError(data) {
2018-06-19 06:12:36 +00:00
data.shapeType = 'error';
this.show(data);
}
2018-03-07 14:05:09 +00:00
/**
2018-06-19 06:12:36 +00:00
* Shows a success.
*
* @param {Object} data The message data
*/
2018-06-19 06:12:36 +00:00
showSuccess(data) {
data.shapeType = 'success';
this.show(data);
}
2018-03-07 14:05:09 +00:00
2018-06-19 06:12:36 +00:00
/**
* Hides the snackbar.
* @param {Object} shape Snackbar element
*/
hide(shape) {
setTimeout(() => shape.classList.remove('shown'), 30);
setTimeout(() => shape.remove(), 250);
}
2018-03-07 14:05:09 +00:00
onSnackbarClick(event) {
this.event = event;
}
2018-03-07 14:05:09 +00:00
2018-06-19 06:12:36 +00:00
onButtonClick(shape) {
2019-12-18 11:49:20 +00:00
if (this.actionHandler)
this.actionHandler();
2019-12-18 11:49:20 +00:00
else
2018-06-19 06:12:36 +00:00
this.hide(shape);
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
});