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 {
|
2020-03-23 22:57:11 +00:00
|
|
|
constructor($element, $) {
|
|
|
|
super($element, $);
|
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) {
|
2020-03-23 22:57:11 +00:00
|
|
|
let shape = Object.assign({
|
|
|
|
nMessages: 1
|
|
|
|
}, data);
|
|
|
|
|
|
|
|
let element = document.createElement('div');
|
|
|
|
element.className = 'shape';
|
|
|
|
setTimeout(() => element.classList.add('shown'), 30);
|
|
|
|
shape.element = element;
|
|
|
|
|
|
|
|
if (shape.type)
|
|
|
|
element.classList.add(shape.type);
|
2018-06-19 06:12:36 +00:00
|
|
|
|
|
|
|
let button = document.createElement('button');
|
2020-03-23 22:57:11 +00:00
|
|
|
button.addEventListener('click', () => this.onButtonClick(shape));
|
|
|
|
element.appendChild(button);
|
2018-06-19 06:12:36 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
let buttonText = shape.actionText || this.$t('Hide');
|
2018-06-19 06:12:36 +00:00
|
|
|
buttonText = document.createTextNode(buttonText);
|
|
|
|
button.appendChild(buttonText);
|
|
|
|
|
|
|
|
let shapeText = document.createElement('div');
|
|
|
|
shapeText.setAttribute('class', 'text');
|
2020-03-23 22:57:11 +00:00
|
|
|
element.appendChild(shapeText);
|
2018-06-19 06:12:36 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
let text = document.createTextNode(shape.message);
|
2018-06-19 06:12:36 +00:00
|
|
|
shapeText.appendChild(text);
|
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
let chip = document.createElement('vn-chip');
|
|
|
|
chip.className = 'warning small';
|
|
|
|
chip.style.visibility = 'hidden';
|
|
|
|
|
|
|
|
let chipWrapper = document.createElement('div');
|
|
|
|
chip.append(chipWrapper);
|
|
|
|
|
|
|
|
let span = document.createElement('span');
|
|
|
|
chipWrapper.append(span);
|
2018-06-19 06:12:36 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
let chipText = document.createTextNode(shape.nMessages);
|
|
|
|
span.append(chipText);
|
|
|
|
|
|
|
|
shapeText.appendChild(chip);
|
|
|
|
|
|
|
|
let parent = this.snackbar.querySelector('.shape');
|
2018-06-19 06:12:36 +00:00
|
|
|
|
2019-12-18 11:49:20 +00:00
|
|
|
if (parent)
|
2020-03-23 22:57:11 +00:00
|
|
|
this.snackbar.insertBefore(element, parent);
|
2019-12-18 11:49:20 +00:00
|
|
|
else
|
2020-03-23 22:57:11 +00:00
|
|
|
this.snackbar.appendChild(element);
|
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) {
|
2020-03-23 22:57:11 +00:00
|
|
|
let shape = this.lastShape;
|
2020-02-10 06:24:12 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
const isEqual = shape
|
|
|
|
&& shape.type == data.type
|
|
|
|
&& shape.message == data.message;
|
2020-02-10 06:24:12 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
if (isEqual) {
|
|
|
|
shape.nMessages++;
|
2020-02-10 06:24:12 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
const chip = shape.element.querySelector('.text vn-chip');
|
|
|
|
chip.style.visibility = 'visible';
|
2020-02-10 06:24:12 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
const span = chip.querySelector('span');
|
|
|
|
span.innerHTML = shape.nMessages;
|
|
|
|
} else
|
2020-02-10 06:24:12 +00:00
|
|
|
shape = this.createShape(data);
|
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
clearTimeout(shape.hideTimeout);
|
|
|
|
shape.hideTimeout = setTimeout(
|
|
|
|
() => this.hide(shape), shape.timeout || 3000);
|
2018-02-20 09:00:19 +00:00
|
|
|
|
2020-03-23 22:57:11 +00:00
|
|
|
this.lastShape = shape;
|
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) {
|
2020-03-23 22:57:11 +00:00
|
|
|
data.type = '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) {
|
2020-03-23 22:57:11 +00:00
|
|
|
data.type = 'success';
|
2018-06-19 06:12:36 +00:00
|
|
|
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) {
|
2020-03-23 22:57:11 +00:00
|
|
|
if (this.lastShape == shape)
|
|
|
|
this.lastShape = null;
|
|
|
|
|
|
|
|
shape.element.classList.remove('shown');
|
|
|
|
setTimeout(() => shape.element.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) {
|
2020-03-23 22:57:11 +00:00
|
|
|
if (shape.actionHandler)
|
|
|
|
shape.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:46:06 +00:00
|
|
|
ngModule.vnComponent('vnSnackbar', {
|
2017-05-31 08:28:39 +00:00
|
|
|
template: require('./snackbar.html'),
|
2017-05-17 19:23:47 +00:00
|
|
|
controller: Controller
|
|
|
|
});
|