From 6e7c179d5bc14b03c397b937682a4f82b817f533 Mon Sep 17 00:00:00 2001 From: Cleverson Nascimento Date: Mon, 25 Apr 2016 17:42:20 -0300 Subject: [PATCH 1/2] Fixed not finding cg-notify-message-template when its not immediately below the root element. --- angular-notify.js | 88 ++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 47 deletions(-) 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); } }; From 6738323b4ad2664dd6a6dc567f74efc3e380ff24 Mon Sep 17 00:00:00 2001 From: Cleverson Nascimento Date: Mon, 25 Apr 2016 17:46:41 -0300 Subject: [PATCH 2/2] Build the distribution code. --- dist/angular-notify.js | 143 ++++++++++++++++++++----------------- dist/angular-notify.min.js | 2 +- 2 files changed, 79 insertions(+), 66 deletions(-) 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', - "
\n" + - "\n" + - "
\n" + - " {{$message}}\n" + - "
\n" + - "\n" + - "
\n" + - " \n" + - "
\n" + - "\n" + - " \n" + + "
\r" + + "\n" + + "\r" + + "\n" + + "
\r" + + "\n" + + " {{$message}}\r" + + "\n" + + "
\r" + + "\n" + + "\r" + + "\n" + + "
\r" + + "\n" + + " \r" + + "\n" + + "
\r" + + "\n" + + "\r" + + "\n" + + " \r" + + "\n" + + "\r" + "\n" + "
" ); diff --git a/dist/angular-notify.min.js b/dist/angular-notify.min.js index 8607d9f..1b1aa42 100644 --- a/dist/angular-notify.min.js +++ b/dist/angular-notify.min.js @@ -1 +1 @@ -angular.module("cgNotify",[]).factory("notify",["$timeout","$http","$compile","$templateCache","$rootScope",function(a,b,c,d,e){var f=10,g=15,h=1e4,i="angular-notify.html",j="center",k=document.body,l=0,m=[],n=[],o=function(o){"object"!=typeof o&&(o={message:o}),o.duration=o.duration?o.duration:h,o.templateUrl=o.templateUrl?o.templateUrl:i,o.container=o.container?o.container:k,o.classes=o.classes?o.classes:"";var p=o.scope?o.scope.$new():e.$new();if(p.$position=o.position?o.position:j,p.$message=o.message,p.$classes=o.classes,p.$messageTemplate=o.messageTemplate,l>0)for(var q=n.length+1-l,r=0;q>r;r++)n[r].$close();b.get(o.templateUrl,{cache:d}).success(function(b){var d=c(b)(p);if(d.bind("webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd",function(a){("opacity"===a.propertyName||0===a.currentTarget.style.opacity||a.originalEvent&&"opacity"===a.originalEvent.propertyName)&&(d.remove(),m.splice(m.indexOf(d),1),n.splice(n.indexOf(p),1),i())}),o.messageTemplate){for(var e,h=0;h=0;c--){var d=10,e=m[c],h=e[0].offsetHeight,i=b+h+d;e.attr("data-closing")?i+=20:b+=h+g,e.css("top",i+"px").css("margin-top","-"+(h+d)+"px").css("visibility","visible"),a++}};a(function(){i()}),o.duration>0&&a(function(){p.$close()},o.duration)}).error(function(a){throw new Error("Template specified for cgNotify ("+o.templateUrl+") could not be loaded. "+a)});var s={};return s.close=function(){p.$close&&p.$close()},Object.defineProperty(s,"message",{get:function(){return p.$message},set:function(a){p.$message=a}}),n.push(p),s};return o.config=function(a){f=angular.isUndefined(a.startTop)?f:a.startTop,g=angular.isUndefined(a.verticalSpacing)?g:a.verticalSpacing,h=angular.isUndefined(a.duration)?h:a.duration,i=a.templateUrl?a.templateUrl:i,j=angular.isUndefined(a.position)?j:a.position,k=a.container?a.container:k,l=a.maximumOpen?a.maximumOpen:l},o.closeAll=function(){for(var a=m.length-1;a>=0;a--){var b=m[a];b.css("opacity",0)}},o}]),angular.module("cgNotify").run(["$templateCache",function(a){"use strict";a.put("angular-notify.html","
\n\n
\n {{$message}}\n
\n\n"+'
\n \n
\n\n \n\n
')}]); \ No newline at end of file +angular.module("cgNotify",[]).factory("notify",["$timeout","$http","$compile","$templateCache","$rootScope",function(a,b,c,d,e){var f=10,g=15,h=1e4,i="angular-notify.html",j="center",k=document.body,l=0,m=[],n=[],o=function(o){"object"!=typeof o&&(o={message:o}),o.duration=o.duration?o.duration:h,o.templateUrl=o.templateUrl?o.templateUrl:i,o.container=o.container?o.container:k,o.classes=o.classes?o.classes:"";var p=o.scope?o.scope.$new():e.$new();if(p.$position=o.position?o.position:j,p.$message=o.message,p.$classes=o.classes,p.$messageTemplate=o.messageTemplate,l>0)for(var q=n.length+1-l,r=0;q>r;r++)n[r].$close();b.get(o.templateUrl,{cache:d}).success(function(b){var d=c(b)(p);if(d.bind("webkitTransitionEnd oTransitionEnd otransitionend transitionend msTransitionEnd",function(a){("opacity"===a.propertyName||0===a.currentTarget.style.opacity||a.originalEvent&&"opacity"===a.originalEvent.propertyName)&&(d.remove(),m.splice(m.indexOf(d),1),n.splice(n.indexOf(p),1),h())}),o.messageTemplate){var e=d.find(".cg-notify-message-template");if(!e)throw new Error("cgNotify could not find the .cg-notify-message-template element in "+o.templateUrl+".");e.append(c(o.messageTemplate)(p))}angular.element(o.container).append(d),m.push(d),"center"===p.$position&&a(function(){p.$centerMargin="-"+d[0].offsetWidth/2+"px"}),p.$close=function(){d.css("opacity",0).attr("data-closing","true"),h()};var h=function(){for(var a=0,b=f,c=m.length-1;c>=0;c--){var d=10,e=m[c],h=e[0].offsetHeight,i=b+h+d;e.attr("data-closing")?i+=20:b+=h+g,e.css("top",i+"px").css("margin-top","-"+(h+d)+"px").css("visibility","visible"),a++}};a(function(){h()}),o.duration>0&&a(function(){p.$close()},o.duration)}).error(function(a){throw new Error("Template specified for cgNotify ("+o.templateUrl+") could not be loaded. "+a)});var s={};return s.close=function(){p.$close&&p.$close()},Object.defineProperty(s,"message",{get:function(){return p.$message},set:function(a){p.$message=a}}),n.push(p),s};return o.config=function(a){f=angular.isUndefined(a.startTop)?f:a.startTop,g=angular.isUndefined(a.verticalSpacing)?g:a.verticalSpacing,h=angular.isUndefined(a.duration)?h:a.duration,i=a.templateUrl?a.templateUrl:i,j=angular.isUndefined(a.position)?j:a.position,k=a.container?a.container:k,l=a.maximumOpen?a.maximumOpen:l},o.closeAll=function(){for(var a=m.length-1;a>=0;a--){var b=m[a];b.css("opacity",0)}},o}]),angular.module("cgNotify").run(["$templateCache",function(a){"use strict";a.put("angular-notify.html","
\r\n\r\n
\r\n {{$message}}\r\n
\r\n\r\n"+'
\r\n \r\n
\r\n\r\n \r\n\r\n
')}]); \ No newline at end of file