1 angular.module('ui-notification',[]);
3 angular.module('ui-notification').provider('Notification', function() {
10 horizontalSpacing: 10,
13 replaceMessage: false,
14 templateUrl: 'angular-ui-notification.html'
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);
22 this.$get = function($timeout, $http, $compile, $templateCache, $rootScope, $injector, $sce, $q, $window) {
23 var options = this.options;
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;
31 var messageElements = [];
32 var isResizeBound = false;
34 var notify = function(args, t){
35 var deferred = $q.defer();
37 if (typeof args !== 'object') {
38 args = {message:args};
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;
49 $http.get(args.template,{cache: $templateCache}).success(function(template) {
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;
57 var reposite = function() {
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');
69 var elHeight = parseInt(element[0].offsetHeight);
70 var elWidth = parseInt(element[0].offsetWidth);
71 var position = lastPosition[element._positionY+element._positionX];
73 if ((top + elHeight) > window.innerHeight) {
79 var top = (lastTop = position ? (j === 0 ? position : position + verticalSpacing) : startTop);
80 var right = lastRight + (k * (horizontalSpacing + elWidth));
82 element.css(element._positionY, top + 'px');
83 if (element._positionX == 'center') {
84 element.css('left', parseInt(window.innerWidth / 2 - elWidth / 2) + 'px');
86 element.css(element._positionX, right + 'px');
89 lastPosition[element._positionY+element._positionX] = top + elHeight;
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);
107 if (angular.isNumber(args.delay)) {
108 $timeout(function() {
109 templateElement.addClass('killed');
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);
118 scope._templateElement = templateElement;
120 scope.kill = function(isHard) {
122 messageElements.splice(messageElements.indexOf(scope._templateElement), 1);
123 scope._templateElement.remove();
126 scope._templateElement.addClass('killed');
132 if (!isResizeBound) {
133 angular.element($window).bind('resize', function(e) {
136 isResizeBound = true;
139 deferred.resolve(scope);
141 }).error(function(data){
142 throw new Error('Template ('+args.template+') could not be loaded. ' + data);
145 return deferred.promise;
148 notify.primary = function(args) {
149 return this(args, 'primary');
151 notify.error = function(args) {
152 return this(args, 'error');
154 notify.success = function(args) {
155 return this(args, 'success');
157 notify.info = function(args) {
158 return this(args, 'info');
160 notify.warning = function(args) {
161 return this(args, 'warning');
164 notify.clearAll = function() {
165 angular.forEach(messageElements, function(element) {
166 element.addClass('killed');