Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed not finding cg-notify-message-template when its not immediately below the root element #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 41 additions & 47 deletions angular-notify.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
});
Expand All @@ -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;
Expand All @@ -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);
}
};

Expand Down
Loading