-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathportal.js
40 lines (35 loc) · 1.12 KB
/
portal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(function(angular) {
angular.module('portal', []).component('portal', {
template:
'<div class="detach__container" ng-class="[$ctrl.elementClass]" ng-transclude></div>',
transclude: true,
bindings: {
to: '@',
detach: '<',
class: '<',
elementClass: '@'
},
controller: function($scope, $element) {
var $ctrl = this;
var contentElm = $element[0].querySelector('.detach__container');
function getTarget(targetId) {
if (!targetId || targetId === 'body') {
return document.body;
}
var selector = 'portal-target[name="' + targetId + '"]';
return document.querySelector(selector);
}
function shouldDetach (detach) {
return (typeof detach !== 'boolean') ? true : detach;
}
$scope.$watchGroup(['$ctrl.to', '$ctrl.detach'], function() {
var parentElm = shouldDetach($ctrl.detach) ? getTarget($ctrl.to) : $element[0];
parentElm.append(contentElm);
});
$scope.$on('$destroy', function() {
contentElm.remove();
contentElm = null;
});
}
});
})(window.angular);