2016-03-23 11:39:10 -05:00
|
|
|
///<reference path="../../headers/common.d.ts" />
|
|
|
|
|
|
|
|
import angular from 'angular';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import coreModule from 'app/core/core_module';
|
|
|
|
import appEvents from 'app/core/app_events';
|
|
|
|
|
|
|
|
export class AlertSrv {
|
|
|
|
list: any[];
|
|
|
|
|
|
|
|
/** @ngInject */
|
2017-09-21 09:40:18 -05:00
|
|
|
constructor(private $timeout, private $rootScope, private $modal) {
|
2016-03-23 11:39:10 -05:00
|
|
|
this.list = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.$rootScope.onAppEvent('alert-error', (e, alert) => {
|
2017-09-12 02:05:32 -05:00
|
|
|
this.set(alert[0], alert[1], 'error', 12000);
|
2016-03-23 11:39:10 -05:00
|
|
|
}, this.$rootScope);
|
|
|
|
|
|
|
|
this.$rootScope.onAppEvent('alert-warning', (e, alert) => {
|
|
|
|
this.set(alert[0], alert[1], 'warning', 5000);
|
|
|
|
}, this.$rootScope);
|
|
|
|
|
|
|
|
this.$rootScope.onAppEvent('alert-success', (e, alert) => {
|
|
|
|
this.set(alert[0], alert[1], 'success', 3000);
|
|
|
|
}, this.$rootScope);
|
|
|
|
|
2017-01-11 12:40:05 -06:00
|
|
|
appEvents.on('alert-warning', options => this.set(options[0], options[1], 'warning', 5000));
|
|
|
|
appEvents.on('alert-success', options => this.set(options[0], options[1], 'success', 3000));
|
|
|
|
appEvents.on('alert-error', options => this.set(options[0], options[1], 'error', 7000));
|
2016-03-23 11:39:10 -05:00
|
|
|
appEvents.on('confirm-modal', this.showConfirmModal.bind(this));
|
|
|
|
}
|
|
|
|
|
2017-09-12 01:01:44 -05:00
|
|
|
getIconForSeverity(severity) {
|
|
|
|
switch (severity) {
|
|
|
|
case 'success': return 'fa fa-check';
|
|
|
|
case 'error': return 'fa fa-exclamation-triangle';
|
|
|
|
default: return 'fa fa-exclamation';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:39:10 -05:00
|
|
|
set(title, text, severity, timeout) {
|
2016-08-11 03:29:28 -05:00
|
|
|
if (_.isObject(text)) {
|
|
|
|
console.log('alert error', text);
|
|
|
|
if (text.statusText) {
|
|
|
|
text = `HTTP Error (${text.status}) ${text.statusText}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 11:39:10 -05:00
|
|
|
var newAlert = {
|
|
|
|
title: title || '',
|
|
|
|
text: text || '',
|
|
|
|
severity: severity || 'info',
|
2017-09-12 01:01:44 -05:00
|
|
|
icon: this.getIconForSeverity(severity)
|
2016-03-23 11:39:10 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var newAlertJson = angular.toJson(newAlert);
|
|
|
|
|
|
|
|
// remove same alert if it already exists
|
|
|
|
_.remove(this.list, function(value) {
|
|
|
|
return angular.toJson(value) === newAlertJson;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.list.push(newAlert);
|
|
|
|
if (timeout > 0) {
|
|
|
|
this.$timeout(() => {
|
|
|
|
this.list = _.without(this.list, newAlert);
|
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.$rootScope.$$phase) {
|
|
|
|
this.$rootScope.$digest();
|
|
|
|
}
|
|
|
|
|
|
|
|
return(newAlert);
|
|
|
|
}
|
|
|
|
|
|
|
|
clear(alert) {
|
|
|
|
this.list = _.without(this.list, alert);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAll() {
|
|
|
|
this.list = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
showConfirmModal(payload) {
|
|
|
|
var scope = this.$rootScope.$new();
|
|
|
|
|
2016-05-16 04:32:08 -05:00
|
|
|
scope.onConfirm = function() {
|
2016-09-05 06:38:25 -05:00
|
|
|
payload.onConfirm();
|
|
|
|
scope.dismiss();
|
2016-05-16 04:32:08 -05:00
|
|
|
};
|
2016-05-16 07:29:54 -05:00
|
|
|
|
2016-05-16 04:32:08 -05:00
|
|
|
scope.updateConfirmText = function(value) {
|
2016-05-16 09:52:51 -05:00
|
|
|
scope.confirmTextValid = payload.confirmText.toLowerCase() === value.toLowerCase();
|
2016-05-16 04:32:08 -05:00
|
|
|
};
|
|
|
|
|
2016-09-05 06:38:25 -05:00
|
|
|
scope.title = payload.title;
|
|
|
|
scope.text = payload.text;
|
|
|
|
scope.text2 = payload.text2;
|
|
|
|
scope.confirmText = payload.confirmText;
|
|
|
|
|
2016-03-23 11:39:10 -05:00
|
|
|
scope.onConfirm = payload.onConfirm;
|
2016-07-12 01:41:56 -05:00
|
|
|
scope.onAltAction = payload.onAltAction;
|
|
|
|
scope.altActionText = payload.altActionText;
|
2016-03-23 11:39:10 -05:00
|
|
|
scope.icon = payload.icon || "fa-check";
|
|
|
|
scope.yesText = payload.yesText || "Yes";
|
|
|
|
scope.noText = payload.noText || "Cancel";
|
2016-09-05 06:38:25 -05:00
|
|
|
scope.confirmTextValid = scope.confirmText ? false : true;
|
2016-03-23 11:39:10 -05:00
|
|
|
|
|
|
|
var confirmModal = this.$modal({
|
|
|
|
template: 'public/app/partials/confirm_modal.html',
|
|
|
|
persist: false,
|
|
|
|
modalClass: 'confirm-modal',
|
|
|
|
show: false,
|
|
|
|
scope: scope,
|
|
|
|
keyboard: false
|
|
|
|
});
|
|
|
|
|
|
|
|
confirmModal.then(function(modalEl) {
|
|
|
|
modalEl.modal('show');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
coreModule.service('alertSrv', AlertSrv);
|