mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-23 12:52:20 +01:00
183 lines
5.9 KiB
JavaScript
183 lines
5.9 KiB
JavaScript
/**
|
|
* angular-growl - v0.4.0 - 2013-11-19
|
|
* https://github.com/marcorinck/angular-growl
|
|
* Copyright (c) 2013 Marco Rinck; Licensed MIT
|
|
*/
|
|
angular.module('angular-growl', []);
|
|
angular.module('angular-growl').directive('growl', [
|
|
'$rootScope',
|
|
function ($rootScope) {
|
|
'use strict';
|
|
return {
|
|
restrict: 'A',
|
|
template: '<div class="growl">' + '\t<div class="growl-item alert" ng-repeat="message in messages" ng-class="computeClasses(message)">' + '\t\t<button type="button" class="close" ng-click="deleteMessage(message)">×</button>' + ' <div ng-switch="message.enableHtml">' + ' <div ng-switch-when="true" ng-bind-html="message.text"></div>' + ' <div ng-switch-default ng-bind="message.text"></div>' + ' </div>' + '\t</div>' + '</div>',
|
|
replace: false,
|
|
scope: true,
|
|
controller: [
|
|
'$scope',
|
|
'$timeout',
|
|
'growl',
|
|
function ($scope, $timeout, growl) {
|
|
var onlyUnique = growl.onlyUnique();
|
|
$scope.messages = [];
|
|
function addMessage(message) {
|
|
$scope.messages.push(message);
|
|
if (message.ttl && message.ttl !== -1) {
|
|
$timeout(function () {
|
|
$scope.deleteMessage(message);
|
|
}, message.ttl);
|
|
}
|
|
}
|
|
$rootScope.$on('growlMessage', function (event, message) {
|
|
var found;
|
|
if (onlyUnique) {
|
|
angular.forEach($scope.messages, function (msg) {
|
|
if (message.text === msg.text && message.severity === msg.severity) {
|
|
found = true;
|
|
}
|
|
});
|
|
if (!found) {
|
|
addMessage(message);
|
|
}
|
|
} else {
|
|
addMessage(message);
|
|
}
|
|
});
|
|
$scope.deleteMessage = function (message) {
|
|
var index = $scope.messages.indexOf(message);
|
|
if (index > -1) {
|
|
$scope.messages.splice(index, 1);
|
|
}
|
|
};
|
|
$scope.computeClasses = function (message) {
|
|
return {
|
|
'alert-success': message.severity === 'success',
|
|
'alert-error': message.severity === 'error',
|
|
'alert-danger': message.severity === 'error',
|
|
'alert-info': message.severity === 'info',
|
|
'alert-warning': message.severity === 'warn'
|
|
};
|
|
};
|
|
}
|
|
]
|
|
};
|
|
}
|
|
]);
|
|
angular.module('angular-growl').provider('growl', function () {
|
|
'use strict';
|
|
var _ttl = null, _enableHtml = false, _messagesKey = 'messages', _messageTextKey = 'text', _messageSeverityKey = 'severity', _onlyUniqueMessages = true;
|
|
this.globalTimeToLive = function (ttl) {
|
|
_ttl = ttl;
|
|
};
|
|
this.globalEnableHtml = function (enableHtml) {
|
|
_enableHtml = enableHtml;
|
|
};
|
|
this.messagesKey = function (messagesKey) {
|
|
_messagesKey = messagesKey;
|
|
};
|
|
this.messageTextKey = function (messageTextKey) {
|
|
_messageTextKey = messageTextKey;
|
|
};
|
|
this.messageSeverityKey = function (messageSeverityKey) {
|
|
_messageSeverityKey = messageSeverityKey;
|
|
};
|
|
this.onlyUniqueMessages = function (onlyUniqueMessages) {
|
|
_onlyUniqueMessages = onlyUniqueMessages;
|
|
};
|
|
this.serverMessagesInterceptor = [
|
|
'$q',
|
|
'growl',
|
|
function ($q, growl) {
|
|
function checkResponse(response) {
|
|
if (response.data[_messagesKey] && response.data[_messagesKey].length > 0) {
|
|
growl.addServerMessages(response.data[_messagesKey]);
|
|
}
|
|
}
|
|
function success(response) {
|
|
checkResponse(response);
|
|
return response;
|
|
}
|
|
function error(response) {
|
|
checkResponse(response);
|
|
return $q.reject(response);
|
|
}
|
|
return function (promise) {
|
|
return promise.then(success, error);
|
|
};
|
|
}
|
|
];
|
|
this.$get = [
|
|
'$rootScope',
|
|
'$filter',
|
|
function ($rootScope, $filter) {
|
|
var translate;
|
|
try {
|
|
translate = $filter('translate');
|
|
} catch (e) {
|
|
}
|
|
function broadcastMessage(message) {
|
|
if (translate) {
|
|
message.text = translate(message.text);
|
|
}
|
|
$rootScope.$broadcast('growlMessage', message);
|
|
}
|
|
function sendMessage(text, config, severity) {
|
|
var _config = config || {}, message;
|
|
message = {
|
|
text: text,
|
|
severity: severity,
|
|
ttl: _config.ttl || _ttl,
|
|
enableHtml: _config.enableHtml || _enableHtml
|
|
};
|
|
broadcastMessage(message);
|
|
}
|
|
function addWarnMessage(text, config) {
|
|
sendMessage(text, config, 'warn');
|
|
}
|
|
function addErrorMessage(text, config) {
|
|
sendMessage(text, config, 'error');
|
|
}
|
|
function addInfoMessage(text, config) {
|
|
sendMessage(text, config, 'info');
|
|
}
|
|
function addSuccessMessage(text, config) {
|
|
sendMessage(text, config, 'success');
|
|
}
|
|
function addServerMessages(messages) {
|
|
var i, message, severity, length;
|
|
length = messages.length;
|
|
for (i = 0; i < length; i++) {
|
|
message = messages[i];
|
|
if (message[_messageTextKey] && message[_messageSeverityKey]) {
|
|
switch (message[_messageSeverityKey]) {
|
|
case 'warn':
|
|
severity = 'warn';
|
|
break;
|
|
case 'success':
|
|
severity = 'success';
|
|
break;
|
|
case 'info':
|
|
severity = 'info';
|
|
break;
|
|
case 'error':
|
|
severity = 'error';
|
|
break;
|
|
}
|
|
sendMessage(message[_messageTextKey], undefined, severity);
|
|
}
|
|
}
|
|
}
|
|
function onlyUnique() {
|
|
return _onlyUniqueMessages;
|
|
}
|
|
return {
|
|
addWarnMessage: addWarnMessage,
|
|
addErrorMessage: addErrorMessage,
|
|
addInfoMessage: addInfoMessage,
|
|
addSuccessMessage: addSuccessMessage,
|
|
addServerMessages: addServerMessages,
|
|
onlyUnique: onlyUnique
|
|
};
|
|
}
|
|
];
|
|
}); |