-
Notifications
You must be signed in to change notification settings - Fork 100
/
view-segment.js
113 lines (87 loc) · 4.59 KB
/
view-segment.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
'use strict';
/**
* appViewSegment directive
* It is based on ngView directive code:
* https://github.com/angular/angular.js/blob/master/src/ngRoute/directive/ngView.js
*/
(function(angular) {
angular.module( 'view-segment', [ 'route-segment' ] ).directive( 'appViewSegment',
['$route', '$compile', '$controller', '$routeParams', '$routeSegment', '$q', '$injector',
function($route, $compile, $controller, $routeParams, $routeSegment, $q, $injector) {
return {
restrict : 'ECA',
priority: 500,
compile : function(tElement, tAttrs) {
var defaultContent = tElement.html(), isDefault = true,
anchor = angular.element(document.createComment(' view-segment '));
tElement.prepend(anchor);
return function($scope) {
var currentScope, currentElement, currentSegment, onloadExp = tAttrs.onload || '', animate,
viewSegmentIndex = parseInt(tAttrs.appViewSegment);
try {
// angular 1.1.x
var $animator = $injector.get('$animator')
animate = $animator($scope, tAttrs);
}
catch(e) {}
try {
// angular 1.2.x
animate = $injector.get('$animate');
}
catch(e) {}
if($routeSegment.chain[viewSegmentIndex])
update($routeSegment.chain[viewSegmentIndex]);
// Watching for the specified route segment and updating contents
$scope.$on('routeSegmentChange', function(event, args) {
if(args.index == viewSegmentIndex && currentSegment != args.segment)
update(args.segment);
});
function clearContent() {
if(currentElement) {
animate.leave(currentElement);
currentElement = null;
}
if (currentScope) {
currentScope.$destroy();
currentScope = null;
}
}
function update(segment) {
currentSegment = segment;
if(isDefault) {
isDefault = false;
tElement.replaceWith(anchor);
}
if(!segment) {
clearContent();
currentElement = tElement.clone();
currentElement.html(defaultContent);
animate.enter( currentElement, null, anchor );
$compile(currentElement, false, 499)($scope);
return;
}
var locals = angular.extend({}, segment.locals),
template = locals && locals.$template;
clearContent();
currentElement = tElement.clone();
currentElement.html(template ? template : defaultContent);
animate.enter( currentElement, null, anchor );
var link = $compile(currentElement, false, 499), controller;
currentScope = $scope.$new();
if (segment.params.controller) {
locals.$scope = currentScope;
controller = $controller(segment.params.controller, locals);
if(segment.params.controllerAs)
currentScope[segment.params.controllerAs] = controller;
currentElement.data('$ngControllerController', controller);
currentElement.children().data('$ngControllerController', controller);
}
link(currentScope);
currentScope.$emit('$viewContentLoaded');
currentScope.$eval(onloadExp);
}
}
}
}
}]);
})(angular);