-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathviewDirective.js
179 lines (144 loc) · 5.14 KB
/
viewDirective.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
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
171
172
173
174
175
176
177
178
179
/**
* @ngdoc directive
* @name ui.router.state.diretive.ui-view
*
* @requires ui.router.state.$state
* @requires $compile
* @requires $controller
* @requires $injector
*
* @restrict ECA
*
* @description
* The ui-view directive tells $state where to place your templates.
* A view can be unnamed or named.
*
* @param {string} ui-view A view name.
*/
$ViewDirective.$inject = ['$state', '$compile', '$controller', '$injector', '$uiViewScroll', '$document'];
function $ViewDirective( $state, $compile, $controller, $injector, $uiViewScroll, $document) {
function getService() {
return ($injector.has) ? function(service) {
return $injector.has(service) ? $injector.get(service) : null;
} : function(service) {
try {
return $injector.get(service);
} catch (e) {
return null;
}
};
}
var viewIsUpdating = false,
service = getService(),
$animator = service('$animator'),
$animate = service('$animate');
// Returns a set of DOM manipulation functions based on whether animation
// should be performed
function getRenderer(element, attrs, scope) {
var statics = function() {
return {
leave: function (element) { element.remove(); },
enter: function (element, parent, anchor) { anchor.after(element); }
};
};
if ($animate) {
return function(shouldAnimate) {
return !shouldAnimate ? statics() : {
enter: function(element, parent, anchor) { $animate.enter(element, null, anchor); },
leave: function(element) { $animate.leave(element, function() { element.remove(); }); }
};
};
}
if ($animator) {
var animate = $animator && $animator(scope, attrs);
return function(shouldAnimate) {
return !shouldAnimate ? statics() : {
enter: function(element, parent, anchor) { animate.enter(element, parent); },
leave: function(element) { animate.leave(element.contents(), element); }
};
};
}
return statics;
}
var directive = {
restrict: 'ECA',
compile: function (element, attrs) {
var initial = element.html(),
isDefault = true,
anchor = angular.element($document[0].createComment(' ui-view-anchor ')),
parentEl = element.parent();
element.prepend(anchor);
return function ($scope) {
var inherited = parentEl.inheritedData('$uiView');
var currentScope, currentEl, viewLocals,
name = attrs[directive.name] || attrs.name || '',
onloadExp = attrs.onload || '',
autoscrollExp = attrs.autoscroll,
renderer = getRenderer(element, attrs, $scope);
if (name.indexOf('@') < 0) name = name + '@' + (inherited ? inherited.state.name : '');
var view = { name: name, state: null };
var eventHook = function () {
if (viewIsUpdating) return;
viewIsUpdating = true;
try { updateView(true); } catch (e) {
viewIsUpdating = false;
throw e;
}
viewIsUpdating = false;
};
$scope.$on('$stateChangeSuccess', eventHook);
$scope.$on('$viewContentLoading', eventHook);
updateView(false);
function cleanupLastView() {
if (currentEl) {
renderer(true).leave(currentEl);
currentEl = null;
}
if (currentScope) {
currentScope.$destroy();
currentScope = null;
}
}
function updateView(shouldAnimate) {
var locals = $state.$current && $state.$current.locals[name];
if (isDefault) {
isDefault = false;
element.replaceWith(anchor);
}
if (!locals) {
cleanupLastView();
currentEl = element.clone();
currentEl.html(initial);
renderer(shouldAnimate).enter(currentEl, parentEl, anchor);
currentScope = $scope.$new();
$compile(currentEl.contents())(currentScope);
return;
}
if (locals === viewLocals) return; // nothing to do
cleanupLastView();
currentEl = element.clone();
currentEl.html(locals.$template ? locals.$template : initial);
renderer(true).enter(currentEl, parentEl, anchor);
currentEl.data('$uiView', view);
viewLocals = locals;
view.state = locals.$$state;
var link = $compile(currentEl.contents());
currentScope = $scope.$new();
if (locals.$$controller) {
locals.$scope = currentScope;
var controller = $controller(locals.$$controller, locals);
currentEl.children().data('$ngControllerController', controller);
}
link(currentScope);
currentScope.$emit('$viewContentLoaded');
if (onloadExp) currentScope.$eval(onloadExp);
if (!angular.isDefined(autoscrollExp) || !autoscrollExp || $scope.$eval(autoscrollExp)) {
$uiViewScroll(currentEl);
}
}
};
}
};
return directive;
}
angular.module('ui.router.state').directive('uiView', $ViewDirective);