forked from artch/angular-route-segment
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
'use strict'; | ||
|
||
/** | ||
* The directive app:view is more powerful replacement of built-in ng:view. It allows views to be nested, where each | ||
* following view in the chain corresponds to the next route segment (see $routeSegment service). | ||
* | ||
* Sample: | ||
* <div> | ||
* <h4>Section</h4> | ||
* <div app:view>Nothing selected</div> | ||
* </div> | ||
* | ||
* Initial contents of an element with app:view will display if corresponding route segment doesn't exist. | ||
* | ||
* View resolving are depends on route segment params: | ||
* - `template` define contents of the view | ||
* - `controller` is attached to view contents when compiled and linked | ||
*/ | ||
|
||
(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, 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) {} | ||
|
||
// Watching for the specified route segment and updating contents | ||
$scope.$on('routeSegmentChange', function(event, args) { | ||
if(args.index == viewSegmentIndex) | ||
update(args.segment); | ||
}); | ||
|
||
function clearContent() { | ||
|
||
if(currentElement) { | ||
animate.leave(currentElement); | ||
currentElement = null; | ||
} | ||
|
||
if (currentScope) { | ||
currentScope.$destroy(); | ||
currentScope = null; | ||
} | ||
} | ||
|
||
|
||
function update(segment) { | ||
|
||
if(!segment) { | ||
clearContent(); | ||
currentElement = tElement.clone(); | ||
currentElement.html(defaultContent); | ||
animate.enter( currentElement, null, anchor ); | ||
$compile(currentElement, false, 499)($scope); | ||
return; | ||
} | ||
|
||
if(isDefault) { | ||
isDefault = false; | ||
tElement.replaceWith(anchor); | ||
} | ||
|
||
var locals = angular.extend({}, segment.locals), | ||
template = locals && locals.$template; | ||
|
||
if (template) { | ||
|
||
clearContent(); | ||
|
||
currentElement = tElement.clone(); | ||
currentElement.html(template); | ||
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); | ||
currentElement.data('$ngControllerController', controller); | ||
currentElement.children().data('$ngControllerController', controller); | ||
} | ||
|
||
link(currentScope); | ||
currentScope.$emit('$viewContentLoaded'); | ||
currentScope.$eval(onloadExp); | ||
} else { | ||
clearContent(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}]); | ||
|
||
})(angular); |