This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathngOutlet.js
More file actions
170 lines (163 loc) · 5.82 KB
/
ngOutlet.js
File metadata and controls
170 lines (163 loc) · 5.82 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function createOutlet($q, $animate) {
function Outlet(router, scope, element, controller, $transclude, attrs) {
this.router = router;
this.scope = scope;
this.element = element;
this.controller = controller;
this.$transclude = $transclude;
}
Outlet.prototype.cleanupLastView = function () {
var _this = this;
if (this.previousLeaveAnimation) {
$animate.cancel(this.previousLeaveAnimation);
this.previousLeaveAnimation = null;
}
if (this.currentScope) {
this.currentScope.$destroy();
this.currentScope = null;
}
if (this.currentElement) {
this.previousLeaveAnimation = $animate.leave(this.currentElement);
this.previousLeaveAnimation.then(function () { _this.previousLeaveAnimation = null; });
this.currentElement = null;
}
};
Outlet.prototype.reuse = function (instruction) {
var next = $q.when(true);
var previousInstruction = this.currentInstruction;
this.currentInstruction = instruction;
if (this.currentController && this.currentController.$routerOnReuse) {
next = $q.when(
this.currentController.$routerOnReuse(this.currentInstruction, previousInstruction));
}
return next;
};
Outlet.prototype.routerCanReuse = function (nextInstruction) {
var result;
if (!this.currentInstruction ||
this.currentInstruction.componentType !== nextInstruction.componentType) {
result = false;
}
else if (this.currentController && this.currentController.$routerCanReuse) {
result = this.currentController.$routerCanReuse(nextInstruction, this.currentInstruction);
}
else {
result = nextInstruction === this.currentInstruction ||
angular.equals(nextInstruction.params, this.currentInstruction.params);
}
return $q.when(result);
};
Outlet.prototype.routerCanDeactivate = function (instruction) {
if (this.currentController && this.currentController.$routerCanDeactivate) {
return $q.when(
this.currentController.$routerCanDeactivate(instruction, this.currentInstruction));
}
return $q.when(true);
};
Outlet.prototype.deactivate = function (instruction) {
if (this.currentController && this.currentController.$routerOnDeactivate) {
return $q.when(
this.currentController.$routerOnDeactivate(instruction, this.currentInstruction));
}
return $q.when();
};
Outlet.prototype.activate = function (instruction) {
var _this = this;
this.previousInstruction = this.currentInstruction;
this.currentInstruction = instruction;
var componentName = this.controller.$$componentName = instruction.componentType;
if (typeof componentName !== 'string') {
throw new Error('Component is not a string for ' + instruction.urlPath);
}
var passedAttributes = " ";
for (var key in attrs) {
// TODO rewrite 'if' to pass only 'bindings' attributes of current component
if (key && key[0] !== '$' && key != 'class' && key != 'id') {
passedAttributes += key + '="' + attrs[key] + '"';
}
}
this.controller.$$template = '<' + dashCase(componentName) + passedAttributes + ' $router="::$$router"></' +
dashCase(componentName) + '>';
this.controller.$$router = this.router.childRouter(instruction.componentType);
this.controller.$$outlet = this;
var newScope = this.scope.$new();
newScope.$$router = this.controller.$$router;
this.deferredActivation = $q.defer();
var clone = this.$transclude(newScope, function (clone) {
$animate.enter(clone, null, _this.currentElement || _this.element);
_this.cleanupLastView();
});
this.currentElement = clone;
this.currentScope = newScope;
return this.deferredActivation.promise;
};
return Outlet;
}
/**
* @name ngOutlet
*
* @description
* An ngOutlet is where resolved content goes.
*
* ## Use
*
* ```html
* <div ng-outlet="name"></div>
* ```
*
* The value for the `ngOutlet` attribute is optional.
*/
exports.ngOutletDirective = function($animate, $q, $rootRouter) {
var Outlet = createOutlet($q, $animate);
var rootRouter = $rootRouter;
return {
restrict: 'AE',
transclude: 'element',
terminal: true,
priority: 400,
require: ['?^^ngOutlet', 'ngOutlet'],
link: function outletLink(scope, element, attrs, ctrls, $transclude) {
var parentCtrl = ctrls[0], myCtrl = ctrls[1],
router = (parentCtrl && parentCtrl.$$router) || rootRouter;
myCtrl.$$currentComponent = null;
router.registerPrimaryOutlet(new Outlet(router, scope, element, myCtrl, $transclude, attrs));
},
controller: function() {},
controllerAs: '$$ngOutlet'
};
};
/**
* This directive is responsible for compiling the contents of ng-outlet
*/
exports.ngOutletFillContentDirective = function($compile) {
return {
restrict: 'EA',
priority: -400,
require: 'ngOutlet',
link: function(scope, element, attrs, ctrl) {
var template = ctrl.$$template;
element.html(template);
$compile(element.contents())(scope);
}
};
};
exports.routerTriggerDirective = function($q) {
return {
require: '^ngOutlet',
priority: -1000,
link: function(scope, element, attr, ngOutletCtrl) {
var promise = $q.when();
var outlet = ngOutletCtrl.$$outlet;
var currentComponent = outlet.currentController =
element.controller(ngOutletCtrl.$$componentName);
if (currentComponent.$routerOnActivate) {
promise = $q.when(currentComponent.$routerOnActivate(outlet.currentInstruction,
outlet.previousInstruction));
}
promise.then(outlet.deferredActivation.resolve, outlet.deferredActivation.reject);
}
};
};
function dashCase(str) {
return str.replace(/[A-Z]/g, function(match) { return '-' + match.toLowerCase(); });
}