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

163 lines
4.3 KiB
JavaScript
Raw Permalink 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, $) {
super($element, $);
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 = Object.assign({
nMessages: 1
}, data);
let element = document.createElement('div');
element.className = 'shape';
setTimeout(() => element.classList.add('shown'), 30);
shape.element = element;
if (data.additionalData && this.vnToken.token) {
this.additionalData = data.additionalData;
let supportButton = document.createElement('i');
supportButton.setAttribute('class', 'material-icons clickable');
supportButton.addEventListener('click', () => this.$.supportDialog.show());
element.appendChild(supportButton);
let buttonIcon = 'support_agent';
buttonIcon = document.createTextNode(buttonIcon);
supportButton.appendChild(buttonIcon);
}
if (shape.type)
element.classList.add(shape.type);
2018-06-19 06:12:36 +00:00
let button = document.createElement('button');
button.addEventListener('click', () => this.onButtonClick(shape));
element.appendChild(button);
2018-06-19 06:12:36 +00:00
let buttonText = '✖';
2018-06-19 06:12:36 +00:00
buttonText = document.createTextNode(buttonText);
button.appendChild(buttonText);
let shapeText = document.createElement('div');
shapeText.setAttribute('class', 'text');
element.appendChild(shapeText);
2018-06-19 06:12:36 +00:00
let text = document.createTextNode(shape.message);
2018-06-19 06:12:36 +00:00
shapeText.appendChild(text);
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
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)
this.snackbar.insertBefore(element, parent);
2019-12-18 11:49:20 +00:00
else
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) {
let shape = this.lastShape;
2020-02-10 06:24:12 +00:00
const isEqual = shape
&& shape.type == data.type
&& shape.message == data.message;
2020-02-10 06:24:12 +00:00
if (isEqual) {
shape.nMessages++;
2020-02-10 06:24:12 +00:00
const chip = shape.element.querySelector('.text vn-chip');
chip.style.visibility = 'visible';
2020-02-10 06:24:12 +00:00
const span = chip.querySelector('span');
span.innerHTML = shape.nMessages;
} else
2020-02-10 06:24:12 +00:00
shape = this.createShape(data);
clearTimeout(shape.hideTimeout);
shape.hideTimeout = setTimeout(
() => this.hide(shape), shape.timeout || 5000);
this.lastShape = shape;
}
2018-03-07 14:05:09 +00:00
/**
* Shows an error.
*
* @param {Object} data The message data
*/
showError(data) {
data.type = '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.type = 'success';
2018-06-19 06:12:36 +00:00
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) {
if (this.lastShape == shape)
this.lastShape = null;
shape.element.classList.remove('shown');
setTimeout(() => shape.element.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) {
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
});