2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../../module';
|
2018-02-20 09:00:19 +00:00
|
|
|
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.
|
|
|
|
*/
|
2018-02-20 09:00:19 +00:00
|
|
|
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;
|
2018-02-20 09:00:19 +00:00
|
|
|
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];
|
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
this.snackbar.insertBefore(shape, parent);
|
|
|
|
} else {
|
|
|
|
this.snackbar.appendChild(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-02-20 09:00:19 +00:00
|
|
|
this.actionHandler = data.actionHandler;
|
|
|
|
|
2018-06-19 06:12:36 +00:00
|
|
|
let shape = this.createShape(data);
|
2018-02-20 09:00:19 +00:00
|
|
|
|
2018-06-19 06:12:36 +00:00
|
|
|
setTimeout(() =>
|
2018-08-08 06:53:19 +00:00
|
|
|
this.hide(shape), data.timeout || 3000);
|
2018-02-20 09:00:19 +00:00
|
|
|
|
2018-06-19 06:12:36 +00:00
|
|
|
setTimeout(() =>
|
|
|
|
shape.classList.add('shown'), 30);
|
2018-02-20 09:00:19 +00:00
|
|
|
}
|
2018-03-07 14:05:09 +00:00
|
|
|
|
2018-02-20 09:00:19 +00:00
|
|
|
/**
|
|
|
|
* Shows an error.
|
|
|
|
*
|
|
|
|
* @param {Object} data The message data
|
|
|
|
*/
|
|
|
|
showError(data) {
|
2018-06-19 06:12:36 +00:00
|
|
|
data.shapeType = 'error';
|
|
|
|
|
2018-02-20 09:00:19 +00:00
|
|
|
this.show(data);
|
|
|
|
}
|
2018-03-07 14:05:09 +00:00
|
|
|
|
2018-02-20 09:00:19 +00:00
|
|
|
/**
|
2018-06-19 06:12:36 +00:00
|
|
|
* Shows a success.
|
|
|
|
*
|
|
|
|
* @param {Object} data The message data
|
2018-02-20 09:00:19 +00:00
|
|
|
*/
|
2018-06-19 06:12:36 +00:00
|
|
|
showSuccess(data) {
|
|
|
|
data.shapeType = 'success';
|
|
|
|
|
|
|
|
this.show(data);
|
2018-02-20 09:00:19 +00:00
|
|
|
}
|
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-02-20 09:00:19 +00:00
|
|
|
}
|
2018-03-07 14:05:09 +00:00
|
|
|
|
2018-02-20 09:00:19 +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) {
|
|
|
|
if (this.actionHandler) {
|
2018-02-20 09:00:19 +00:00
|
|
|
this.actionHandler();
|
2018-06-19 06:12:36 +00:00
|
|
|
} else {
|
|
|
|
this.hide(shape);
|
|
|
|
}
|
2017-05-17 19:23:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 09:00:19 +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
|
|
|
|
});
|