import UserView from '../userView';
const UserViewInstance = new UserView();
export default class BaseFeedback {
feedbackStructure = {
container: 'user-feedback',
message: 'user-feedback-message',
icon: 'user-feedback-icon',
link: 'user-feedback-link',
closeButton: 'user-feedback-close'
};
isActive = false;
feedbackTimeout = false;
fadeTimeout = false;
defaultClassIcon = '';
defaultClassContainer = '';
constructor(key, configuration) {
this.feedbackType = key;
this.feedbackTime = parseInt(configuration.temporizedFor || 0, 10);
this.feedbackFadeTime = parseInt(configuration.fadeTimeout || 0, 10);
this.enabled = configuration.enabled || false;
this.canClose = configuration.canClose || false;
this.overrideProps = configuration.overrideProps || false;
this.iconClass = configuration.iconClass || '';
this.popupClass = configuration.popupClass || false;
this.container = UserViewInstance.getElement(this.feedbackStructure.container);
this.feedbackMessage = UserViewInstance.getElement(this.feedbackStructure.message);
this.feedbackIcon = UserViewInstance.getElement(this.feedbackStructure.icon);
this.feedbackLink = UserViewInstance.getElement(this.feedbackStructure.link);
this.feedbackCloseIcon = UserViewInstance.getElement(this.feedbackStructure.closeButton);
if (this.feedbackIcon && this.feedbackIcon.className) {
this.defaultClassIcon = this.feedbackIcon.className;
}
if (this.container && this.container.className) {
this.defaultClassContainer = this.container.className;
}
}
cleanFeedbackTimeout = () => {
if (this.feedbackTimeout) {
clearTimeout(this.feedbackTimeout);
this.feedbackTimeout = false;
}
};
cleanFadeTimeout = () => {
if (this.fadeTimeout) {
clearTimeout(this.fadeTimeout);
this.fadeTimeout = false;
}
};
closePopup = () => {
this.feedbackMessage.innerHTML = '';
this.feedbackIcon.className = this.defaultClassIcon;
this.container.className = this.defaultClassContainer;
if (this.feedbackLink.getAttribute('href')) {
this.feedbackLink.setAttribute('href', '');
this.feedbackLink.innerText = '';
this.feedbackLink.style.display = 'none';
}
this.container.style.display = 'none';
this.isActive = false;
};
closeUserFeedback = () => {
this.cleanFeedbackTimeout();
this.cleanFadeTimeout();
this.closePopup();
};
isShowed = () => this.isActive;
isEnabled = () => this.enabled;
getType = () => this.feedbackType;
showUserFeedback = (obj) => {
let timeForShow = this.feedbackTime;
let timeForFade = this.feedbackFadeTime;
let tempIcon = this.iconClass;
if (typeof obj.message !== 'undefined') {
this.feedbackMessage.innerHTML = obj.message;
}
if (typeof this.popupClass !== 'undefined') {
this.container.className += ` ${this.popupClass}`;
}
if (this.overrideProps) {
if (typeof obj.fadeTimeout !== 'undefined') {
timeForFade = obj.fadeTimeout;
}
if (typeof obj.temporizedFor !== 'undefined') {
timeForShow = obj.temporizedFor;
}
}
if (typeof obj.icon !== 'undefined' && obj.icon !== '' && this.overrideProps) {
tempIcon = obj.icon;
this.feedbackIcon.className += ` ${tempIcon}`;
} else if (typeof this.iconClass !== 'undefined') {
this.feedbackIcon.className += ` ${this.iconClass}`;
}
if (typeof obj.link !== 'undefined') {
this.feedbackLink.style.display = 'block';
this.feedbackLink.href = obj.link.href;
this.feedbackLink.innerText = obj.link.text;
}
if (this.canClose) {
this.feedbackCloseIcon.style.display = 'block';
UserViewInstance.attachEvent(this.feedbackCloseIcon, { eventName: 'click', event: this.closeUserFeedback });
} else {
this.feedbackCloseIcon.style.display = 'none';
}
this.container.style.display = 'block';
if ((timeForFade > 0) && (timeForShow > 0)) {
this.fadeTimeout = setTimeout(() => {
this.container.className += ' fade';
this.feedbackTimeout = setTimeout(this.closePopup, timeForFade);
}, timeForShow - timeForFade);
} else if (typeof timeForShow !== 'undefined' && timeForShow > 0) {
this.feedbackTimeout = setTimeout(this.closePopup, timeForShow);
} else {
this.feedbackCloseIcon.style.display = 'block';
UserViewInstance.attachEvent(this.feedbackCloseIcon, { eventName: 'click', event: this.closeUserFeedback });
}
this.isActive = true;
}
}
|