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

164 lines
4.2 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;
2020-02-10 06:24:12 +00:00
let shape;
const lastShape = this.lastShape;
const lastShapeData = lastShape && lastShape.data;
const isEqual = lastShape && (lastShapeData.shapeType == data.shapeType && lastShapeData.message == data.message);
if (this.lastShape && isEqual) {
shape = this.lastShape.element;
const shapeText = shape.querySelector('.text');
let chip = shapeText.querySelector('vn-chip');
if (chip) {
const text = chip.querySelector('span');
const number = parseInt(text.innerHTML);
text.innerHTML = number + 1;
} else {
chip = document.createElement('vn-chip');
chip.setAttribute('class', 'warning small');
let parent = document.createElement('div');
let span = document.createElement('span');
2020-02-20 10:23:21 +00:00
let text = document.createTextNode(1);
2020-02-10 06:24:12 +00:00
span.append(text);
parent.append(span);
chip.append(parent);
shapeText.appendChild(chip);
}
if (this.hideTimeout)
clearTimeout(this.hideTimeout);
} else {
shape = this.createShape(data);
setTimeout(() =>
shape.classList.add('shown'), 30);
}
this.hideTimeout = setTimeout(() => {
this.hide(shape);
this.lastShape = null;
2020-02-20 10:23:21 +00:00
}, data.timeout || 9000);
2020-02-10 06:24:12 +00:00
this.lastShape = {
data: data,
element: shape
};
}
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
});