-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'template_based_view_refresh'
Closes #7
- Loading branch information
Showing
4 changed files
with
170 additions
and
36 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 |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
</li> | ||
</ul> | ||
</nav> | ||
<div ng-view></div> | ||
<div kbn-view></div> | ||
</div> | ||
</body> | ||
</html> |
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
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,121 @@ | ||
define(function (require) { | ||
var angular = require('angular'); | ||
|
||
angular | ||
.module('kibana/directives') | ||
/****** | ||
****** COPIED directive from angular-router | ||
****** https://github.com/angular/angular.js/blob/6f0503514f/src/ngRoute/directive/ngView.js#L183 | ||
****** | ||
****** Modification made: | ||
****** - prevent the view from being recreated unnecessarily | ||
****** | ||
******/ | ||
.directive('kbnView', function modifiedNgViewFactory($route, $anchorScroll, $animate) { | ||
return { | ||
restrict: 'ECA', | ||
terminal: true, | ||
priority: 400, | ||
transclude: 'element', | ||
link: function (scope, $element, attr, ctrl, $transclude) { | ||
var currentScope; | ||
var currentElement; | ||
var currentTemplateUrl; | ||
var autoScrollExp = attr.autoscroll; | ||
var onloadExp = attr.onload || ''; | ||
|
||
scope.$on('$routeChangeSuccess', update); | ||
update(); | ||
|
||
function cleanupLastView() { | ||
if (currentScope) { | ||
currentScope.$destroy(); | ||
currentScope = null; | ||
} | ||
if (currentElement) { | ||
$animate.leave(currentElement); | ||
currentElement = null; | ||
} | ||
} | ||
|
||
function update() { | ||
/****** START modification *******/ | ||
if ($route.current) { | ||
if (currentTemplateUrl && $route.current.templateUrl === currentTemplateUrl) { | ||
return; | ||
} else { | ||
currentTemplateUrl = $route.current.templateUrl; | ||
} | ||
} | ||
/****** STOP modification *******/ | ||
|
||
var locals = $route.current && $route.current.locals; | ||
var template = locals && locals.$template; | ||
|
||
if (angular.isDefined(template)) { | ||
var newScope = scope.$new(); | ||
var current = $route.current; | ||
|
||
// Note: This will also link all children of ng-view that were contained in the original | ||
// html. If that content contains controllers, ... they could pollute/change the scope. | ||
// However, using ng-view on an element with additional content does not make sense... | ||
// Note: We can't remove them in the cloneAttchFn of $transclude as that | ||
// function is called before linking the content, which would apply child | ||
// directives to non existing elements. | ||
var clone = $transclude(newScope, function (clone) { | ||
$animate.enter(clone, null, currentElement || $element, function onNgViewEnter() { | ||
if (angular.isDefined(autoScrollExp) | ||
&& (!autoScrollExp || scope.$eval(autoScrollExp))) { | ||
$anchorScroll(); | ||
} | ||
}); | ||
cleanupLastView(); | ||
}); | ||
|
||
currentElement = clone; | ||
currentScope = current.scope = newScope; | ||
currentScope.$emit('$viewContentLoaded'); | ||
currentScope.$eval(onloadExp); | ||
} else { | ||
cleanupLastView(); | ||
} | ||
} | ||
} | ||
}; | ||
}) | ||
|
||
/****** | ||
****** COPIED directive from angular-router | ||
****** https://github.com/angular/angular.js/blob/6f0503514f/src/ngRoute/directive/ngView.js#L251 | ||
****** | ||
****** No Modifications made | ||
****** | ||
******/ | ||
.directive('kbnView', function modifiedNgViewFillContentFactory($compile, $controller, $route) { | ||
return { | ||
restrict: 'ECA', | ||
priority: -400, | ||
link: function (scope, $element) { | ||
var current = $route.current, | ||
locals = current.locals; | ||
|
||
$element.html(locals.$template); | ||
|
||
var link = $compile($element.contents()); | ||
|
||
if (current.controller) { | ||
locals.$scope = scope; | ||
var controller = $controller(current.controller, locals); | ||
if (current.controllerAs) { | ||
scope[current.controllerAs] = controller; | ||
} | ||
$element.data('$ngControllerController', controller); | ||
$element.children().data('$ngControllerController', controller); | ||
} | ||
|
||
link(scope); | ||
} | ||
}; | ||
}); | ||
|
||
}); |
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