Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / angular-ui-notification / src / angular-ui-notification.js
1 angular.module('ui-notification',[]);
2
3 angular.module('ui-notification').provider('Notification', function() {
4
5     this.options = {
6         delay: 5000,
7         startTop: 10,
8         startRight: 10,
9         verticalSpacing: 10,
10         horizontalSpacing: 10,
11         positionX: 'right',
12         positionY: 'top',
13         replaceMessage: false,
14         templateUrl: 'angular-ui-notification.html'
15     };
16
17     this.setOptions = function(options) {
18         if (!angular.isObject(options)) throw new Error("Options should be an object!");
19         this.options = angular.extend({}, this.options, options);
20     };
21
22     this.$get = function($timeout, $http, $compile, $templateCache, $rootScope, $injector, $sce, $q, $window) {
23         var options = this.options;
24
25         var startTop = options.startTop;
26         var startRight = options.startRight;
27         var verticalSpacing = options.verticalSpacing;
28         var horizontalSpacing = options.horizontalSpacing;
29         var delay = options.delay;
30
31         var messageElements = [];
32         var isResizeBound = false;
33
34         var notify = function(args, t){
35             var deferred = $q.defer();
36
37             if (typeof args !== 'object') {
38                 args = {message:args};
39             }
40
41             args.scope = args.scope ? args.scope : $rootScope;
42             args.template = args.templateUrl ? args.templateUrl : options.templateUrl;
43             args.delay = !angular.isUndefined(args.delay) ? args.delay : delay;
44             args.type = t ? t : '';
45             args.positionY = args.positionY ? args.positionY : options.positionY;
46             args.positionX = args.positionX ? args.positionX : options.positionX;
47             args.replaceMessage = args.replaceMessage ? args.replaceMessage : options.replaceMessage;
48
49             $http.get(args.template,{cache: $templateCache}).success(function(template) {
50
51                 var scope = args.scope.$new();
52                 scope.message = $sce.trustAsHtml(args.message);
53                 scope.title = $sce.trustAsHtml(args.title);
54                 scope.t = args.type.substr(0,1);
55                 scope.delay = args.delay;
56
57                 var reposite = function() {
58                     var j = 0;
59                     var k = 0;
60                     var lastTop = startTop;
61                     var lastRight = startRight;
62                     var lastPosition = [];
63                     for(var i = messageElements.length - 1; i >= 0; i --) {
64                         var element  = messageElements[i];
65                         if (args.replaceMessage && i < messageElements.length - 1) {
66                             element.addClass('killed');
67                             continue;
68                         }
69                         var elHeight = parseInt(element[0].offsetHeight);
70                         var elWidth  = parseInt(element[0].offsetWidth);
71                         var position = lastPosition[element._positionY+element._positionX];
72
73                         if ((top + elHeight) > window.innerHeight) {
74                             position = startTop;
75                             k ++;
76                             j = 0;
77                         }
78
79                         var top = (lastTop = position ? (j === 0 ? position : position + verticalSpacing) : startTop);
80                         var right = lastRight + (k * (horizontalSpacing + elWidth));
81
82                         element.css(element._positionY, top + 'px');
83                         if (element._positionX == 'center') {
84                             element.css('left', parseInt(window.innerWidth / 2 - elWidth / 2) + 'px');
85                         } else {
86                             element.css(element._positionX, right + 'px');
87                         }
88
89                         lastPosition[element._positionY+element._positionX] = top + elHeight;
90
91                         j ++;
92                     }
93                 };
94
95                 var templateElement = $compile(template)(scope);
96                 templateElement._positionY = args.positionY;
97                 templateElement._positionX = args.positionX;
98                 templateElement.addClass(args.type);
99                 templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd click', function(e){
100                     e = e.originalEvent || e;
101                     if (e.type === 'click' || (e.propertyName === 'opacity' && e.elapsedTime >= 1)){
102                         templateElement.remove();
103                         messageElements.splice(messageElements.indexOf(templateElement), 1);
104                         reposite();
105                     }
106                 });
107                 if (angular.isNumber(args.delay)) {
108                     $timeout(function() {
109                         templateElement.addClass('killed');
110                     }, args.delay);
111                 }
112
113                 angular.element(document.getElementsByTagName('body')).append(templateElement);
114                 var offset = -(parseInt(templateElement[0].offsetHeight) + 50);
115                 templateElement.css(templateElement._positionY, offset + "px");
116                 messageElements.push(templateElement);
117
118                 scope._templateElement = templateElement;
119
120                 scope.kill = function(isHard) {
121                     if (isHard) {
122                         messageElements.splice(messageElements.indexOf(scope._templateElement), 1);
123                         scope._templateElement.remove();
124                         $timeout(reposite);
125                     } else {
126                         scope._templateElement.addClass('killed');
127                     }
128                 };
129
130                 $timeout(reposite);
131
132                 if (!isResizeBound) {
133                     angular.element($window).bind('resize', function(e) {
134                         $timeout(reposite);
135                     });
136                     isResizeBound = true;
137                 }
138
139                 deferred.resolve(scope);
140
141             }).error(function(data){
142                 throw new Error('Template ('+args.template+') could not be loaded. ' + data);
143             });
144
145             return deferred.promise;
146         };
147
148         notify.primary = function(args) {
149             return this(args, 'primary');
150         };
151         notify.error = function(args) {
152             return this(args, 'error');
153         };
154         notify.success = function(args) {
155             return this(args, 'success');
156         };
157         notify.info = function(args) {
158             return this(args, 'info');
159         };
160         notify.warning = function(args) {
161             return this(args, 'warning');
162         };
163
164         notify.clearAll = function() {
165             angular.forEach(messageElements, function(element) {
166                 element.addClass('killed');
167             });
168         };
169
170         return notify;
171     };
172 });