File: app/client/admin/js/alert/AlertApp.js
'use strict';
/**
* Controls alerts for the whole application.
*
* @module ov.alert
* @main ov.alert
*/
(function(angular) {
var app = angular.module('ov.alert', []);
/**
* Defines a generic alert management system to display one or several messages.
*
* @class alertService
*/
function AlertService($rootScope, $timeout) {
$rootScope.alerts = [];
/**
* Closes an alert by its position in alerts array.
*
* @param {Number} index Position in array
* @return {Array} The list of remaining alerts
*/
function closeAlertIdx(index) {
return $rootScope.alerts.splice(index, 1);
}
/**
* Closes an alert.
*
* @param {Object} alert Alert to close
* @return {Array} The list of remaining alerts
*/
function closeAlert(alert) {
return closeAlertIdx($rootScope.alerts.indexOf(alert));
}
var alertService = {
/**
* Displays an alert.
*
* @example
* // Info alert displayed for 4 seconds
* AlertService.add('info', 'Message of the alert', 4000);
*
* // Error alert displayed permanently
* AlertService.add('danger', 'Message of the alert');
*
* @param {String} type The alert type (success, danger, warning or info)
* @param {String} msg The alert message
* @param {Number} [timeout] The timeout (in ms) before closing the alert, if not specified the alert
* will be permanent
* @method add
*/
add: function(type, msg, timeout) {
var alert = {
type: type,
msg: msg,
close: function() {
if (alert.timeout)
$timeout.cancel(this.timeout);
return closeAlert(this);
}
};
if (timeout)
alert.timeout = $timeout(function() {
closeAlert(alert);
}, timeout);
$rootScope.alerts.push(alert);
},
/**
* Closes all opened alerts.
*
* @example
* // Close all alerts
* AlertService.closeAll();
*
* @method closeAll
*/
closeAll: function() {
$rootScope.alerts = [];
}
};
return alertService;
}
app.factory('alertService', AlertService);
AlertService.$inject = ['$rootScope', '$timeout'];
})(angular);