|
1 | 1 | /**
|
2 |
| - * angular-route-segment 1.3.3 |
| 2 | + * angular-route-segment 1.4.0 |
3 | 3 | * https://angular-route-segment.com
|
4 | 4 | * @author Artem Chivchalov
|
5 | 5 | * @license MIT License http://opensource.org/licenses/MIT
|
@@ -74,7 +74,7 @@ mod.provider( '$routeSegment',
|
74 | 74 | * @returns {Object} The same level pointer.
|
75 | 75 | */
|
76 | 76 | segment: function(name, params) {
|
77 |
| - segment[camelCase(name)] = {params: params}; |
| 77 | + segment[camelCase(name)] = {name: name, params: params}; |
78 | 78 | lastAddedName = name;
|
79 | 79 | return this;
|
80 | 80 | },
|
@@ -302,7 +302,7 @@ mod.provider( '$routeSegment',
|
302 | 302 | (function(i, children, index) {
|
303 | 303 | if (children[i].params['default']) {
|
304 | 304 | defaultChildUpdatePromise = defaultChildUpdatePromise.then(function () {
|
305 |
| - return updateSegment(index, {name: i, params: children[i].params}) |
| 305 | + return updateSegment(index, {name: children[i].name, params: children[i].params}) |
306 | 306 | .then(function (result) {
|
307 | 307 | if (result.success) broadcast(result.success);
|
308 | 308 | });
|
@@ -566,111 +566,118 @@ mod.filter('routeSegmentParam', ['$routeSegment', function($routeSegment) {
|
566 | 566 | (function(angular) {
|
567 | 567 |
|
568 | 568 | angular.module( 'view-segment', [ 'route-segment' ] ).directive( 'appViewSegment',
|
569 |
| - ['$route', '$compile', '$controller', '$routeParams', '$routeSegment', '$q', '$injector', '$timeout', |
570 |
| - function($route, $compile, $controller, $routeParams, $routeSegment, $q, $injector, $timeout) { |
| 569 | + ['$route', '$compile', '$controller', '$routeParams', '$routeSegment', '$q', '$injector', '$timeout', '$animate', |
| 570 | + function($route, $compile, $controller, $routeParams, $routeSegment, $q, $injector, $timeout, $animate) { |
571 | 571 |
|
572 | 572 | return {
|
573 | 573 | restrict : 'ECA',
|
574 |
| - priority: 500, |
575 |
| - compile : function(tElement, tAttrs) { |
576 |
| - |
577 |
| - var defaultContent = tElement.html(), isDefault = true, |
578 |
| - anchor = angular.element(document.createComment(' view-segment ')); |
| 574 | + priority: 400, |
| 575 | + transclude: 'element', |
579 | 576 |
|
580 |
| - tElement.prepend(anchor); |
| 577 | + compile : function(tElement, tAttrs) { |
581 | 578 |
|
582 |
| - return function($scope) { |
| 579 | + return function($scope, element, attrs, ctrl, $transclude) { |
583 | 580 |
|
584 |
| - var currentScope, currentElement, currentSegment, onloadExp = tAttrs.onload || '', animate, |
585 |
| - viewSegmentIndex = parseInt(tAttrs.appViewSegment), updatePromise; |
| 581 | + var currentScope, currentElement, currentSegment = {}, onloadExp = tAttrs.onload || '', |
| 582 | + viewSegmentIndex = parseInt(tAttrs.appViewSegment), updatePromise, previousLeaveAnimation; |
586 | 583 |
|
587 |
| - try { |
588 |
| - // angular 1.1.x |
589 |
| - var $animator = $injector.get('$animator') |
590 |
| - animate = $animator($scope, tAttrs); |
591 |
| - } |
592 |
| - catch(e) {} |
593 |
| - try { |
594 |
| - // angular 1.2.x |
595 |
| - animate = $injector.get('$animate'); |
596 |
| - } |
597 |
| - catch(e) {} |
598 |
| - |
599 |
| - if($routeSegment.chain[viewSegmentIndex]) |
600 |
| - updatePromise = $timeout(function() { |
| 584 | + if($routeSegment.chain[viewSegmentIndex]) { |
| 585 | + updatePromise = $timeout(function () { |
601 | 586 | update($routeSegment.chain[viewSegmentIndex]);
|
602 | 587 | }, 0);
|
| 588 | + } |
| 589 | + else { |
| 590 | + update(); |
| 591 | + } |
603 | 592 |
|
604 | 593 | // Watching for the specified route segment and updating contents
|
605 | 594 | $scope.$on('routeSegmentChange', function(event, args) {
|
606 | 595 |
|
607 | 596 | if(updatePromise)
|
608 | 597 | $timeout.cancel(updatePromise);
|
609 | 598 |
|
610 |
| - if(args.index == viewSegmentIndex && currentSegment != args.segment) |
| 599 | + if(args.index == viewSegmentIndex && currentSegment != args.segment) { |
611 | 600 | update(args.segment);
|
| 601 | + } |
612 | 602 | });
|
613 | 603 |
|
614 | 604 | function clearContent() {
|
615 |
| - |
616 |
| - if(currentElement) { |
617 |
| - animate.leave(currentElement); |
618 |
| - currentElement = null; |
| 605 | + if (previousLeaveAnimation) { |
| 606 | + $animate.cancel(previousLeaveAnimation); |
| 607 | + previousLeaveAnimation = null; |
619 | 608 | }
|
620 | 609 |
|
621 | 610 | if (currentScope) {
|
622 | 611 | currentScope.$destroy();
|
623 | 612 | currentScope = null;
|
624 | 613 | }
|
| 614 | + if (currentElement) { |
| 615 | + previousLeaveAnimation = $animate.leave(currentElement); |
| 616 | + if(previousLeaveAnimation) { |
| 617 | + previousLeaveAnimation.then(function () { |
| 618 | + previousLeaveAnimation = null; |
| 619 | + }); |
| 620 | + } |
| 621 | + currentElement = null; |
| 622 | + } |
625 | 623 | }
|
626 | 624 |
|
627 |
| - |
628 | 625 | function update(segment) {
|
629 | 626 |
|
630 | 627 | currentSegment = segment;
|
631 | 628 |
|
632 |
| - if(isDefault) { |
633 |
| - isDefault = false; |
634 |
| - tElement.replaceWith(anchor); |
635 |
| - } |
| 629 | + var newScope = $scope.$new(); |
636 | 630 |
|
637 |
| - if(!segment) { |
| 631 | + var clone = $transclude(newScope, function(clone) { |
| 632 | + if(segment) { |
| 633 | + clone.data('viewSegment', segment); |
| 634 | + } |
| 635 | + $animate.enter(clone, null, currentElement || element); |
638 | 636 | clearContent();
|
639 |
| - currentElement = tElement.clone(); |
640 |
| - currentElement.html(defaultContent); |
641 |
| - animate.enter( currentElement, null, anchor ); |
642 |
| - $compile(currentElement, false, 499)($scope); |
643 |
| - return; |
644 |
| - } |
| 637 | + }); |
| 638 | + |
| 639 | + currentElement = clone; |
| 640 | + currentScope = newScope; |
| 641 | + currentScope.$emit('$viewContentLoaded'); |
| 642 | + currentScope.$eval(onloadExp); |
| 643 | + } |
| 644 | + } |
| 645 | + } |
| 646 | + } |
| 647 | + }]); |
645 | 648 |
|
646 |
| - var locals = angular.extend({}, segment.locals), |
647 |
| - template = locals && locals.$template; |
| 649 | + angular.module( 'view-segment').directive( 'appViewSegment', |
| 650 | + ['$route', '$compile', '$controller', function($route, $compile, $controller) { |
648 | 651 |
|
649 |
| - clearContent(); |
| 652 | + return { |
| 653 | + restrict: 'ECA', |
| 654 | + priority: -400, |
| 655 | + link: function ($scope, element) { |
650 | 656 |
|
651 |
| - currentElement = tElement.clone(); |
652 |
| - currentElement.html(template ? template : defaultContent); |
653 |
| - animate.enter( currentElement, null, anchor ); |
| 657 | + var segment = element.data('viewSegment') || {}; |
654 | 658 |
|
655 |
| - var link = $compile(currentElement, false, 499), controller; |
| 659 | + var locals = angular.extend({}, segment.locals), |
| 660 | + template = locals && locals.$template; |
656 | 661 |
|
657 |
| - currentScope = $scope.$new(); |
658 |
| - if (segment.params.controller) { |
659 |
| - locals.$scope = currentScope; |
660 |
| - controller = $controller(segment.params.controller, locals); |
| 662 | + if(template) { |
| 663 | + element.html(template); |
| 664 | + } |
| 665 | + |
| 666 | + var link = $compile(element.contents()); |
| 667 | + |
| 668 | + if (segment.params && segment.params.controller) { |
| 669 | + locals.$scope = $scope; |
| 670 | + var controller = $controller(segment.params.controller, locals); |
661 | 671 | if(segment.params.controllerAs)
|
662 |
| - currentScope[segment.params.controllerAs] = controller; |
663 |
| - currentElement.data('$ngControllerController', controller); |
664 |
| - currentElement.children().data('$ngControllerController', controller); |
| 672 | + $scope[segment.params.controllerAs] = controller; |
| 673 | + element.data('$ngControllerController', controller); |
| 674 | + element.children().data('$ngControllerController', controller); |
665 | 675 | }
|
666 | 676 |
|
667 |
| - link(currentScope); |
668 |
| - currentScope.$emit('$viewContentLoaded'); |
669 |
| - currentScope.$eval(onloadExp); |
670 |
| - } |
| 677 | + link($scope); |
671 | 678 | }
|
672 | 679 | }
|
673 |
| - } |
674 |
| - }]); |
| 680 | + |
| 681 | + }]); |
675 | 682 |
|
676 | 683 | })(angular);
|
0 commit comments