|
| 1 | +(function(angular, undefined) { |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var djangoShopModule = angular.module('django.shop.messages', []); |
| 5 | + |
| 6 | + |
| 7 | +djangoShopModule.provider('djangoMessages', function() { |
| 8 | + var endpointURL; |
| 9 | + |
| 10 | + this.alertLevelMap = { |
| 11 | + 'debug': 'bg-secondary text-white', |
| 12 | + 'info': 'bg-info text-white', |
| 13 | + 'success': 'bg-success text-white', |
| 14 | + 'warning': 'bg-warning text-dark', |
| 15 | + 'error': 'bg-danger text-white', |
| 16 | + }; |
| 17 | + this.alertIconMap = { |
| 18 | + 'debug': 'fa-bug', |
| 19 | + 'info': 'fa-info-circle', |
| 20 | + 'success': 'fa-check-circle', |
| 21 | + 'warning': 'fa-exclamation-circle', |
| 22 | + 'error': 'fa-exclamation-triangle', |
| 23 | + }; |
| 24 | + |
| 25 | + this.setEndpoint = function(url) { |
| 26 | + endpointURL = url; |
| 27 | + }; |
| 28 | + |
| 29 | + this.$get = ['$http', '$rootScope', '$timeout', function($http, $rootScope, $timeout) { |
| 30 | + var self = this; |
| 31 | + $rootScope.$on('shop.messages.fetch', fetchNewMessages); |
| 32 | + $timeout(function() { |
| 33 | + fetchNewMessages(); |
| 34 | + }); |
| 35 | + |
| 36 | + function fetchNewMessages() { |
| 37 | + $http.get(endpointURL).then(function(response) { |
| 38 | + $rootScope.djangoMessages = angular.isArray($rootScope.djangoMessages) ? $rootScope.djangoMessages : []; |
| 39 | + angular.extend($rootScope.djangoMessages, response.data.django_messages); |
| 40 | + angular.forEach($rootScope.djangoMessages, function(message) { |
| 41 | + if (message.delay > 0 && angular.isUndefined(message.timeout)) { |
| 42 | + message.timeout = $timeout(function() { |
| 43 | + self.removeMessage(message); |
| 44 | + }, message.delay); |
| 45 | + } |
| 46 | + }); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + this.removeMessage = function(message) { |
| 51 | + message.hide = true; |
| 52 | + $timeout(function() { |
| 53 | + var index = $rootScope.djangoMessages.indexOf(message); |
| 54 | + if (index >= 0) { |
| 55 | + $rootScope.djangoMessages.splice(index, 1); |
| 56 | + } |
| 57 | + }, 250); |
| 58 | + }; |
| 59 | + |
| 60 | + return this; |
| 61 | + }]; |
| 62 | +}); |
| 63 | + |
| 64 | + |
| 65 | +djangoShopModule.directive('shopToastMessages', ['$rootScope', 'djangoMessages', function($rootScope, djangoMessages) { |
| 66 | + return { |
| 67 | + restrict: 'E', |
| 68 | + scope: true, |
| 69 | + templateUrl: 'shop/toast-messages.html', |
| 70 | + link: function(scope, element, attrs) { |
| 71 | + scope.alertLevel = function(message) { |
| 72 | + return djangoMessages.alertLevelMap[message.level] || 'text-dark'; |
| 73 | + }; |
| 74 | + scope.alertIcon = function(message) { |
| 75 | + return djangoMessages.alertIconMap[message.level] || 'fa-circle-o'; |
| 76 | + }; |
| 77 | + scope.appear = function(message) { |
| 78 | + if (!message.hide) { |
| 79 | + return 'show'; |
| 80 | + } |
| 81 | + }; |
| 82 | + scope.close = djangoMessages.removeMessage; |
| 83 | + } |
| 84 | + }; |
| 85 | +}]); |
| 86 | + |
| 87 | + |
| 88 | +/* |
| 89 | +djangoShopModule.directive('toast', ['$timeout', function($timeout) { |
| 90 | + return { |
| 91 | + restrict: 'C', |
| 92 | + scope: true, |
| 93 | + link: function(scope, element, attrs) { |
| 94 | + $timeout(function() { |
| 95 | + element.addClass('show'); |
| 96 | + }, 500); |
| 97 | + scope.close = function() { |
| 98 | + element.removeClass('show'); |
| 99 | + $timeout(function() { |
| 100 | + element.addClass('hide'); |
| 101 | + }, 500); |
| 102 | + }; |
| 103 | + scope.alertLevel = function(levelTag) { |
| 104 | + switch (levelTag) { |
| 105 | + case 'debug': return 'bg-secondary text-white'; |
| 106 | + case 'info': return 'bg-info text-white'; |
| 107 | + case 'success': return 'bg-success text-white'; |
| 108 | + case 'warning': return 'bg-warning text-dark'; |
| 109 | + case 'error': return 'bg-danger text-white'; |
| 110 | + default: return 'text-dark'; |
| 111 | + } |
| 112 | + }; |
| 113 | + scope.alertIcon = function(levelTag) { |
| 114 | + switch (levelTag) { |
| 115 | + case 'debug': return 'fa-bug'; |
| 116 | + case 'info': return 'fa-info-circle'; |
| 117 | + case 'success': return 'fa-check-circle'; |
| 118 | + case 'warning': return 'fa-exclamation-circle'; |
| 119 | + case 'error': return 'fa-exclamation-triangle'; |
| 120 | + default: return 'fa-circle-o'; |
| 121 | + } |
| 122 | + }; |
| 123 | + if (parseInt(attrs.delay)) { |
| 124 | + $timeout(scope.close, attrs.delay); |
| 125 | + } |
| 126 | + } |
| 127 | + }; |
| 128 | +}]); |
| 129 | +*/ |
| 130 | + |
| 131 | +})(window.angular); |
0 commit comments