Skip to content

Commit 0e8d2a6

Browse files
committed
Be able to specify a concrete target for container
1 parent 84327f5 commit 0e8d2a6

File tree

7 files changed

+95
-40
lines changed

7 files changed

+95
-40
lines changed

dist/angular-toastr.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
angular.module('toastr', [])
33
.factory('toastr', toastr);
44

5-
toastr.$inject = ['$animate', '$compile', '$document', '$rootScope', '$sce', 'toastrConfig', '$q'];
5+
toastr.$inject = ['$animate', '$injector', '$document', '$rootScope', '$sce', 'toastrConfig', '$q'];
66

7-
function toastr($animate, $compile, $document, $rootScope, $sce, toastrConfig, $q) {
7+
function toastr($animate, $injector, $document, $rootScope, $sce, toastrConfig, $q) {
88
var container, index = 0, toasts = [];
99
var containerDefer = $q.defer();
1010

@@ -115,9 +115,13 @@
115115
container.addClass(options.positionClass);
116116
container.css({'pointer-events': 'auto'});
117117

118-
var body = $document.find('body').eq(0);
118+
var target = document.querySelector(options.target);
119119

120-
$animate.enter(container, body).then(function() {
120+
if ( ! target) {
121+
throw 'Target for toasts doesn\'t exist';
122+
}
123+
124+
$animate.enter(container, target).then(function() {
121125
containerDefer.resolve();
122126
});
123127

@@ -200,7 +204,8 @@
200204
}
201205

202206
function createToastEl(scope) {
203-
var angularDomEl = angular.element('<div toast></div>');
207+
var angularDomEl = angular.element('<div toast></div>'),
208+
$compile = $injector.get('$compile');
204209
return $compile(angularDomEl)(scope);
205210
}
206211

@@ -210,6 +215,7 @@
210215
}
211216
}
212217
}());
218+
213219
(function() {
214220
angular.module('toastr')
215221
.constant('toastrConfig', {
@@ -233,16 +239,18 @@
233239
tapToDismiss: true,
234240
timeOut: 5000,
235241
titleClass: 'toast-title',
236-
toastClass: 'toast'
242+
toastClass: 'toast',
243+
target: 'body'
237244
});
238245
}());
246+
239247
(function() {
240248
angular.module('toastr')
241249
.directive('toast', toast);
242250

243-
toast.$inject = ['$compile', '$interval', 'toastr'];
251+
toast.$inject = ['$injector', '$interval', 'toastr'];
244252

245-
function toast($compile, $interval, toastr) {
253+
function toast($injector, $interval, toastr) {
246254
return {
247255
replace: true,
248256
templateUrl: 'templates/toastr/toastr.html',
@@ -257,7 +265,8 @@
257265
scope.messageClass = scope.options.messageClass;
258266

259267
if (wantsCloseButton()) {
260-
var button = angular.element(scope.options.closeHtml);
268+
var button = angular.element(scope.options.closeHtml),
269+
$compile = $injector.get('$compile');
261270
button.addClass('toast-close-button');
262271
button.attr('ng-click', 'close()');
263272
$compile(button)(scope);
@@ -305,4 +314,4 @@
305314
}
306315
}
307316
}
308-
}());
317+
}());

dist/angular-toastr.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-toastr.tpls.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
angular.module('toastr', [])
33
.factory('toastr', toastr);
44

5-
toastr.$inject = ['$animate', '$compile', '$document', '$rootScope', '$sce', 'toastrConfig', '$q'];
5+
toastr.$inject = ['$animate', '$injector', '$document', '$rootScope', '$sce', 'toastrConfig', '$q'];
66

7-
function toastr($animate, $compile, $document, $rootScope, $sce, toastrConfig, $q) {
7+
function toastr($animate, $injector, $document, $rootScope, $sce, toastrConfig, $q) {
88
var container, index = 0, toasts = [];
99
var containerDefer = $q.defer();
1010

@@ -115,9 +115,13 @@
115115
container.addClass(options.positionClass);
116116
container.css({'pointer-events': 'auto'});
117117

118-
var body = $document.find('body').eq(0);
118+
var target = document.querySelector(options.target);
119119

120-
$animate.enter(container, body).then(function() {
120+
if ( ! target) {
121+
throw 'Target for toasts doesn\'t exist';
122+
}
123+
124+
$animate.enter(container, target).then(function() {
121125
containerDefer.resolve();
122126
});
123127

@@ -200,7 +204,8 @@
200204
}
201205

202206
function createToastEl(scope) {
203-
var angularDomEl = angular.element('<div toast></div>');
207+
var angularDomEl = angular.element('<div toast></div>'),
208+
$compile = $injector.get('$compile');
204209
return $compile(angularDomEl)(scope);
205210
}
206211

@@ -210,6 +215,7 @@
210215
}
211216
}
212217
}());
218+
213219
(function() {
214220
angular.module('toastr')
215221
.constant('toastrConfig', {
@@ -233,16 +239,18 @@
233239
tapToDismiss: true,
234240
timeOut: 5000,
235241
titleClass: 'toast-title',
236-
toastClass: 'toast'
242+
toastClass: 'toast',
243+
target: 'body'
237244
});
238245
}());
246+
239247
(function() {
240248
angular.module('toastr')
241249
.directive('toast', toast);
242250

243-
toast.$inject = ['$compile', '$interval', 'toastr'];
251+
toast.$inject = ['$injector', '$interval', 'toastr'];
244252

245-
function toast($compile, $interval, toastr) {
253+
function toast($injector, $interval, toastr) {
246254
return {
247255
replace: true,
248256
templateUrl: 'templates/toastr/toastr.html',
@@ -257,7 +265,8 @@
257265
scope.messageClass = scope.options.messageClass;
258266

259267
if (wantsCloseButton()) {
260-
var button = angular.element(scope.options.closeHtml);
268+
var button = angular.element(scope.options.closeHtml),
269+
$compile = $injector.get('$compile');
261270
button.addClass('toast-close-button');
262271
button.attr('ng-click', 'close()');
263272
$compile(button)(scope);
@@ -306,4 +315,5 @@
306315
}
307316
}
308317
}());
318+
309319
angular.module("toastr").run(["$templateCache", function($templateCache) {$templateCache.put("templates/toastr/toastr.html","<div class=\"{{toastClass}} {{toastType}}\" ng-click=\"tapToast()\">\n <div ng-switch on=\"allowHtml\">\n <div ng-switch-default ng-if=\"title\" class=\"{{titleClass}}\">{{title}}</div>\n <div ng-switch-default class=\"{{messageClass}}\">{{message}}</div>\n <div ng-switch-when=\"true\" ng-if=\"title\" class=\"{{titleClass}}\" ng-bind-html=\"title\"></div>\n <div ng-switch-when=\"true\" class=\"{{messageClass}}\" ng-bind-html=\"message\"></div>\n </div>\n</div>");}]);

0 commit comments

Comments
 (0)