|
9 | 9 | (function(angular) {
|
10 | 10 |
|
11 | 11 | angular.module( 'view-segment', [ 'route-segment' ] ).directive( 'appViewSegment',
|
12 |
| - ['$route', '$compile', '$controller', '$routeParams', '$routeSegment', '$q', '$injector', '$timeout', |
13 |
| - function($route, $compile, $controller, $routeParams, $routeSegment, $q, $injector, $timeout) { |
| 12 | + ['$route', '$compile', '$controller', '$routeParams', '$routeSegment', '$q', '$injector', '$timeout', '$animate', |
| 13 | + function($route, $compile, $controller, $routeParams, $routeSegment, $q, $injector, $timeout, $animate) { |
14 | 14 |
|
15 | 15 | return {
|
16 | 16 | restrict : 'ECA',
|
17 |
| - priority: 500, |
18 |
| - compile : function(tElement, tAttrs) { |
19 |
| - |
20 |
| - var defaultContent = tElement.html(), isDefault = true, |
21 |
| - anchor = angular.element(document.createComment(' view-segment ')); |
| 17 | + priority: 400, |
| 18 | + transclude: 'element', |
22 | 19 |
|
23 |
| - tElement.prepend(anchor); |
| 20 | + compile : function(tElement, tAttrs) { |
24 | 21 |
|
25 |
| - return function($scope) { |
| 22 | + return function($scope, element, attrs, ctrl, $transclude) { |
26 | 23 |
|
27 |
| - var currentScope, currentElement, currentSegment, onloadExp = tAttrs.onload || '', animate, |
28 |
| - viewSegmentIndex = parseInt(tAttrs.appViewSegment), updatePromise; |
| 24 | + var currentScope, currentElement, currentSegment = {}, onloadExp = tAttrs.onload || '', |
| 25 | + viewSegmentIndex = parseInt(tAttrs.appViewSegment), updatePromise, previousLeaveAnimation; |
29 | 26 |
|
30 |
| - try { |
31 |
| - // angular 1.1.x |
32 |
| - var $animator = $injector.get('$animator') |
33 |
| - animate = $animator($scope, tAttrs); |
34 |
| - } |
35 |
| - catch(e) {} |
36 |
| - try { |
37 |
| - // angular 1.2.x |
38 |
| - animate = $injector.get('$animate'); |
39 |
| - } |
40 |
| - catch(e) {} |
41 |
| - |
42 |
| - if($routeSegment.chain[viewSegmentIndex]) |
43 |
| - updatePromise = $timeout(function() { |
| 27 | + if($routeSegment.chain[viewSegmentIndex]) { |
| 28 | + updatePromise = $timeout(function () { |
44 | 29 | update($routeSegment.chain[viewSegmentIndex]);
|
45 | 30 | }, 0);
|
| 31 | + } |
| 32 | + else { |
| 33 | + update(); |
| 34 | + } |
46 | 35 |
|
47 | 36 | // Watching for the specified route segment and updating contents
|
48 | 37 | $scope.$on('routeSegmentChange', function(event, args) {
|
49 | 38 |
|
50 | 39 | if(updatePromise)
|
51 | 40 | $timeout.cancel(updatePromise);
|
52 | 41 |
|
53 |
| - if(args.index == viewSegmentIndex && currentSegment != args.segment) |
| 42 | + if(args.index == viewSegmentIndex && currentSegment != args.segment) { |
54 | 43 | update(args.segment);
|
| 44 | + } |
55 | 45 | });
|
56 | 46 |
|
57 | 47 | function clearContent() {
|
58 |
| - |
59 |
| - if(currentElement) { |
60 |
| - animate.leave(currentElement); |
61 |
| - currentElement = null; |
| 48 | + if (previousLeaveAnimation) { |
| 49 | + $animate.cancel(previousLeaveAnimation); |
| 50 | + previousLeaveAnimation = null; |
62 | 51 | }
|
63 | 52 |
|
64 | 53 | if (currentScope) {
|
65 | 54 | currentScope.$destroy();
|
66 | 55 | currentScope = null;
|
67 | 56 | }
|
| 57 | + if (currentElement) { |
| 58 | + previousLeaveAnimation = $animate.leave(currentElement); |
| 59 | + if(previousLeaveAnimation) { |
| 60 | + previousLeaveAnimation.then(function () { |
| 61 | + previousLeaveAnimation = null; |
| 62 | + }); |
| 63 | + } |
| 64 | + currentElement = null; |
| 65 | + } |
68 | 66 | }
|
69 | 67 |
|
70 |
| - |
71 | 68 | function update(segment) {
|
72 | 69 |
|
73 | 70 | currentSegment = segment;
|
74 | 71 |
|
75 |
| - if(isDefault) { |
76 |
| - isDefault = false; |
77 |
| - tElement.replaceWith(anchor); |
78 |
| - } |
| 72 | + var newScope = $scope.$new(); |
79 | 73 |
|
80 |
| - if(!segment) { |
| 74 | + var clone = $transclude(newScope, function(clone) { |
| 75 | + if(segment) { |
| 76 | + clone.data('viewSegment', segment); |
| 77 | + } |
| 78 | + $animate.enter(clone, null, currentElement || element); |
81 | 79 | clearContent();
|
82 |
| - currentElement = tElement.clone(); |
83 |
| - currentElement.html(defaultContent); |
84 |
| - animate.enter( currentElement, null, anchor ); |
85 |
| - $compile(currentElement, false, 499)($scope); |
86 |
| - return; |
87 |
| - } |
| 80 | + }); |
88 | 81 |
|
89 |
| - var locals = angular.extend({}, segment.locals), |
90 |
| - template = locals && locals.$template; |
| 82 | + currentElement = clone; |
| 83 | + currentScope = newScope; |
| 84 | + currentScope.$emit('$viewContentLoaded'); |
| 85 | + currentScope.$eval(onloadExp); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + }]); |
| 91 | + |
| 92 | + angular.module( 'view-segment').directive( 'appViewSegment', |
| 93 | + ['$route', '$compile', '$controller', function($route, $compile, $controller) { |
| 94 | + |
| 95 | + return { |
| 96 | + restrict: 'ECA', |
| 97 | + priority: -400, |
| 98 | + link: function ($scope, element) { |
91 | 99 |
|
92 |
| - clearContent(); |
| 100 | + var segment = element.data('viewSegment') || {}; |
93 | 101 |
|
94 |
| - currentElement = tElement.clone(); |
95 |
| - currentElement.html(template ? template : defaultContent); |
96 |
| - animate.enter( currentElement, null, anchor ); |
| 102 | + var locals = angular.extend({}, segment.locals), |
| 103 | + template = locals && locals.$template; |
97 | 104 |
|
98 |
| - var link = $compile(currentElement, false, 499), controller; |
| 105 | + if(template) { |
| 106 | + element.html(template); |
| 107 | + } |
| 108 | + |
| 109 | + var link = $compile(element.contents()); |
99 | 110 |
|
100 |
| - currentScope = $scope.$new(); |
101 |
| - if (segment.params.controller) { |
102 |
| - locals.$scope = currentScope; |
103 |
| - controller = $controller(segment.params.controller, locals); |
| 111 | + if (segment.params && segment.params.controller) { |
| 112 | + locals.$scope = $scope; |
| 113 | + var controller = $controller(segment.params.controller, locals); |
104 | 114 | if(segment.params.controllerAs)
|
105 |
| - currentScope[segment.params.controllerAs] = controller; |
106 |
| - currentElement.data('$ngControllerController', controller); |
107 |
| - currentElement.children().data('$ngControllerController', controller); |
| 115 | + $scope[segment.params.controllerAs] = controller; |
| 116 | + element.data('$ngControllerController', controller); |
| 117 | + element.children().data('$ngControllerController', controller); |
108 | 118 | }
|
109 | 119 |
|
110 |
| - link(currentScope); |
111 |
| - currentScope.$emit('$viewContentLoaded'); |
112 |
| - currentScope.$eval(onloadExp); |
113 |
| - } |
| 120 | + link($scope); |
114 | 121 | }
|
115 | 122 | }
|
116 |
| - } |
117 |
| - }]); |
| 123 | + |
| 124 | + }]); |
118 | 125 |
|
119 | 126 | })(angular);
|
0 commit comments