diff --git a/angular-notify.js b/angular-notify.js index 7fbdf6e..0b03636 100755 --- a/angular-notify.js +++ b/angular-notify.js @@ -1,5 +1,5 @@ -angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile','$templateCache','$rootScope', - function($timeout,$http,$compile,$templateCache,$rootScope){ +angular.module('cgNotify', []).factory('notify', ['$timeout', '$http', '$compile', '$templateCache', '$rootScope', + function ($timeout, $http, $compile, $templateCache, $rootScope) { var startTop = 10; var verticalSpacing = 15; @@ -12,10 +12,10 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' var messageElements = []; var openNotificationsScope = []; - var notify = function(args){ + var notify = function (args) { - if (typeof args !== 'object'){ - args = {message:args}; + if (typeof args !== 'object') { + args = { message: args }; } args.duration = args.duration ? args.duration : defaultDuration; @@ -36,94 +36,88 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' } } - $http.get(args.templateUrl,{cache: $templateCache}).success(function(template){ + $http.get(args.templateUrl, { cache: $templateCache }).success(function (template) { var templateElement = $compile(template)(scope); - templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', function(e){ - if (e.propertyName === 'opacity' || e.currentTarget.style.opacity === 0 || - (e.originalEvent && e.originalEvent.propertyName === 'opacity')){ + templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', function (e) { + if (e.propertyName === 'opacity' || e.currentTarget.style.opacity === 0 || + (e.originalEvent && e.originalEvent.propertyName === 'opacity')) { templateElement.remove(); - messageElements.splice(messageElements.indexOf(templateElement),1); - openNotificationsScope.splice(openNotificationsScope.indexOf(scope),1); + messageElements.splice(messageElements.indexOf(templateElement), 1); + openNotificationsScope.splice(openNotificationsScope.indexOf(scope), 1); layoutMessages(); } }); - if (args.messageTemplate){ - var messageTemplateElement; - for (var i = 0; i < templateElement.children().length; i ++){ - if (angular.element(templateElement.children()[i]).hasClass('cg-notify-message-template')){ - messageTemplateElement = angular.element(templateElement.children()[i]); - break; - } - } - if (messageTemplateElement){ + if (args.messageTemplate) { + var messageTemplateElement = templateElement.find('.cg-notify-message-template'); + if (messageTemplateElement) { messageTemplateElement.append($compile(args.messageTemplate)(scope)); } else { - throw new Error('cgNotify could not find the .cg-notify-message-template element in '+args.templateUrl+'.'); + throw new Error('cgNotify could not find the .cg-notify-message-template element in ' + args.templateUrl + '.'); } } angular.element(args.container).append(templateElement); messageElements.push(templateElement); - if (scope.$position === 'center'){ - $timeout(function(){ - scope.$centerMargin = '-' + (templateElement[0].offsetWidth /2) + 'px'; + if (scope.$position === 'center') { + $timeout(function () { + scope.$centerMargin = '-' + (templateElement[0].offsetWidth / 2) + 'px'; }); } - scope.$close = function(){ - templateElement.css('opacity',0).attr('data-closing','true'); + scope.$close = function () { + templateElement.css('opacity', 0).attr('data-closing', 'true'); layoutMessages(); }; - var layoutMessages = function(){ + var layoutMessages = function () { var j = 0; var currentY = startTop; - for(var i = messageElements.length - 1; i >= 0; i --){ + for (var i = messageElements.length - 1; i >= 0; i--) { var shadowHeight = 10; var element = messageElements[i]; var height = element[0].offsetHeight; var top = currentY + height + shadowHeight; - if (element.attr('data-closing')){ + if (element.attr('data-closing')) { top += 20; } else { currentY += height + verticalSpacing; } - element.css('top',top + 'px').css('margin-top','-' + (height+shadowHeight) + 'px').css('visibility','visible'); - j ++; + element.css('top', top + 'px').css('margin-top', '-' + (height + shadowHeight) + 'px').css('visibility', 'visible'); + j++; } }; - $timeout(function(){ + $timeout(function () { layoutMessages(); }); - if (args.duration > 0){ - $timeout(function(){ + if (args.duration > 0) { + $timeout(function () { scope.$close(); - },args.duration); + }, args.duration); } - }).error(function(data){ - throw new Error('Template specified for cgNotify ('+args.templateUrl+') could not be loaded. ' + data); + }).error(function (data) { + throw new Error('Template specified for cgNotify (' + args.templateUrl + ') could not be loaded. ' + data); }); var retVal = {}; - - retVal.close = function(){ - if (scope.$close){ + + retVal.close = function () { + if (scope.$close) { scope.$close(); } }; - Object.defineProperty(retVal,'message',{ - get: function(){ + Object.defineProperty(retVal, 'message', { + get: function () { return scope.$message; }, - set: function(val){ + set: function (val) { scope.$message = val; } }); @@ -134,7 +128,7 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' }; - notify.config = function(args){ + notify.config = function (args) { startTop = !angular.isUndefined(args.startTop) ? args.startTop : startTop; verticalSpacing = !angular.isUndefined(args.verticalSpacing) ? args.verticalSpacing : verticalSpacing; defaultDuration = !angular.isUndefined(args.duration) ? args.duration : defaultDuration; @@ -144,10 +138,10 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' maximumOpen = args.maximumOpen ? args.maximumOpen : maximumOpen; }; - notify.closeAll = function(){ - for(var i = messageElements.length - 1; i >= 0; i --){ + notify.closeAll = function () { + for (var i = messageElements.length - 1; i >= 0; i--) { var element = messageElements[i]; - element.css('opacity',0); + element.css('opacity', 0); } }; diff --git a/dist/angular-notify.js b/dist/angular-notify.js index b23e7bb..4a224a1 100644 --- a/dist/angular-notify.js +++ b/dist/angular-notify.js @@ -1,5 +1,5 @@ -angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile','$templateCache','$rootScope', - function($timeout,$http,$compile,$templateCache,$rootScope){ +angular.module('cgNotify', []).factory('notify', ['$timeout', '$http', '$compile', '$templateCache', '$rootScope', + function ($timeout, $http, $compile, $templateCache, $rootScope) { var startTop = 10; var verticalSpacing = 15; @@ -12,10 +12,10 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' var messageElements = []; var openNotificationsScope = []; - var notify = function(args){ + var notify = function (args) { - if (typeof args !== 'object'){ - args = {message:args}; + if (typeof args !== 'object') { + args = { message: args }; } args.duration = args.duration ? args.duration : defaultDuration; @@ -36,94 +36,88 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' } } - $http.get(args.templateUrl,{cache: $templateCache}).success(function(template){ + $http.get(args.templateUrl, { cache: $templateCache }).success(function (template) { var templateElement = $compile(template)(scope); - templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', function(e){ - if (e.propertyName === 'opacity' || e.currentTarget.style.opacity === 0 || - (e.originalEvent && e.originalEvent.propertyName === 'opacity')){ + templateElement.bind('webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd', function (e) { + if (e.propertyName === 'opacity' || e.currentTarget.style.opacity === 0 || + (e.originalEvent && e.originalEvent.propertyName === 'opacity')) { templateElement.remove(); - messageElements.splice(messageElements.indexOf(templateElement),1); - openNotificationsScope.splice(openNotificationsScope.indexOf(scope),1); + messageElements.splice(messageElements.indexOf(templateElement), 1); + openNotificationsScope.splice(openNotificationsScope.indexOf(scope), 1); layoutMessages(); } }); - if (args.messageTemplate){ - var messageTemplateElement; - for (var i = 0; i < templateElement.children().length; i ++){ - if (angular.element(templateElement.children()[i]).hasClass('cg-notify-message-template')){ - messageTemplateElement = angular.element(templateElement.children()[i]); - break; - } - } - if (messageTemplateElement){ + if (args.messageTemplate) { + var messageTemplateElement = templateElement.find('.cg-notify-message-template'); + if (messageTemplateElement) { messageTemplateElement.append($compile(args.messageTemplate)(scope)); } else { - throw new Error('cgNotify could not find the .cg-notify-message-template element in '+args.templateUrl+'.'); + throw new Error('cgNotify could not find the .cg-notify-message-template element in ' + args.templateUrl + '.'); } } angular.element(args.container).append(templateElement); messageElements.push(templateElement); - if (scope.$position === 'center'){ - $timeout(function(){ - scope.$centerMargin = '-' + (templateElement[0].offsetWidth /2) + 'px'; + if (scope.$position === 'center') { + $timeout(function () { + scope.$centerMargin = '-' + (templateElement[0].offsetWidth / 2) + 'px'; }); } - scope.$close = function(){ - templateElement.css('opacity',0).attr('data-closing','true'); + scope.$close = function () { + templateElement.css('opacity', 0).attr('data-closing', 'true'); layoutMessages(); }; - var layoutMessages = function(){ + var layoutMessages = function () { var j = 0; var currentY = startTop; - for(var i = messageElements.length - 1; i >= 0; i --){ + for (var i = messageElements.length - 1; i >= 0; i--) { var shadowHeight = 10; var element = messageElements[i]; var height = element[0].offsetHeight; var top = currentY + height + shadowHeight; - if (element.attr('data-closing')){ + if (element.attr('data-closing')) { top += 20; } else { currentY += height + verticalSpacing; } - element.css('top',top + 'px').css('margin-top','-' + (height+shadowHeight) + 'px').css('visibility','visible'); - j ++; + element.css('top', top + 'px').css('margin-top', '-' + (height + shadowHeight) + 'px').css('visibility', 'visible'); + j++; } }; - $timeout(function(){ + $timeout(function () { layoutMessages(); }); - if (args.duration > 0){ - $timeout(function(){ + if (args.duration > 0) { + $timeout(function () { scope.$close(); - },args.duration); + }, args.duration); } - }).error(function(data){ - throw new Error('Template specified for cgNotify ('+args.templateUrl+') could not be loaded. ' + data); + }).error(function (data) { + throw new Error('Template specified for cgNotify (' + args.templateUrl + ') could not be loaded. ' + data); }); var retVal = {}; - - retVal.close = function(){ - if (scope.$close){ + + retVal.close = function () { + if (scope.$close) { scope.$close(); } }; - Object.defineProperty(retVal,'message',{ - get: function(){ + Object.defineProperty(retVal, 'message', { + get: function () { return scope.$message; }, - set: function(val){ + set: function (val) { scope.$message = val; } }); @@ -134,7 +128,7 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' }; - notify.config = function(args){ + notify.config = function (args) { startTop = !angular.isUndefined(args.startTop) ? args.startTop : startTop; verticalSpacing = !angular.isUndefined(args.verticalSpacing) ? args.verticalSpacing : verticalSpacing; defaultDuration = !angular.isUndefined(args.duration) ? args.duration : defaultDuration; @@ -144,10 +138,10 @@ angular.module('cgNotify', []).factory('notify',['$timeout','$http','$compile',' maximumOpen = args.maximumOpen ? args.maximumOpen : maximumOpen; }; - notify.closeAll = function(){ - for(var i = messageElements.length - 1; i >= 0; i --){ + notify.closeAll = function () { + for (var i = messageElements.length - 1; i >= 0; i--) { var element = messageElements[i]; - element.css('opacity',0); + element.css('opacity', 0); } }; @@ -159,24 +153,43 @@ angular.module('cgNotify').run(['$templateCache', function($templateCache) { 'use strict'; $templateCache.put('angular-notify.html', - "