SmartIT_Extensions/BMC/smart-it-full-helix/components/system-alert/system-alert-service.js

122 lines
5.1 KiB
JavaScript

"use strict";
(function () {
'use strict';
/*
options = {
text: 'error text', //obligatory
hilde: 5000, //optional, milliseconds to hide error after shown event
clear: true, //optional, clear errors before pushing new one
displayOnStateChange: false, //optional, if error needs to be displayed after state change then make it true
}
*/
angular.module('bmcSystemAlert')
.service('idService', [function () {
this.getRandomId = function b(a) { return a ? (0 | Math.random() * 16).toString(16) : ("" + 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(/1|0/g, b); };
}])
.factory('systemAlertService', ['$timeout', '$modal', '$q', 'idService',
function ($timeout, $modal, $q, idService) {
var iconMap = {
'error': 'icon-cross_circle',
'success': 'icon-check_circle',
'info': 'icon-exclamation_triangle',
'warning': 'icon-exclamation_triangle'
};
var systemAlert = {
alertStack: [],
error: function (options) {
options.hide = false;
return _add(angular.extend(options, { type: 'error' }));
},
success: function (options) {
return _add(angular.extend(options, { type: 'success' }));
},
info: function (options) {
return _add(angular.extend(options, { type: 'info' }));
},
warning: function (options) {
return _add(angular.extend(options, { type: 'warning' }));
},
modal: function (options) {
var showModelAlert = _showModalAlert(options);
showModelAlert.rendered.then(function () {
//set focus to the first (primary) button
var primaryBtn = angular.element(".modal-btn") && angular.element(".modal-btn")[0];
primaryBtn.focus();
});
return showModelAlert;
},
dismissAlert: function (key) {
_.find(systemAlert.alertStack, { key: key }).dismiss();
},
dismissAllAlerts: function () {
angular.forEach(systemAlert.alertStack, function (alert) {
if (!alert.options.displayOnStateChange) {
alert.dismiss();
}
});
}
};
var _add = function (options) {
var alert = {
key: idService.getRandomId(),
options: options,
result: $q.defer(),
close: function (result) {
this.result.resolve(result);
},
dismiss: function (reason) {
this.result.reject(reason);
},
click: function () {
if (this.options.click && angular.isFunction(this.options.click)) {
this.options.click();
this.result.resolve();
}
}
}, resultPromise = alert.result.promise;
resultPromise.finally(function () {
_.remove(systemAlert.alertStack, { key: alert.key });
});
if (!options.text) {
return alert.dismiss('Obligatory parameter title is missing.');
}
if (!options.icon) {
options.icon = 'icon-' + (options.type == 'success' ? 'check' : 'alert');
}
if (options.clear)
systemAlert.dismissAllAlerts();
systemAlert.alertStack.push(alert);
if (options.hide) {
$timeout(function () {
alert.dismiss();
}, options.hide);
}
return {
key: alert.key,
result: resultPromise
};
};
var _showModalAlert = function (options) {
if (!options.type) {
options.type = 'info';
}
var windowClass = 'bmc-system-alert-modal';
if (options && options.isPopoutWindow) {
windowClass = 'popout-window-alert-modal';
}
options.icon = iconMap[options.type];
return $modal.open({
templateUrl: 'components/system-alert/system-alert-modal.html',
windowClass: windowClass,
backdrop: options.backdrop || 'static',
controller: ['$scope', function ($scope) {
$scope.modalOptions = options;
}]
});
};
return systemAlert;
}
]);
}());