Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

refactor(NgView): Make use of the ViewPort #910

Merged
merged 2 commits into from
Apr 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/core_dom/view_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class ViewCache {
ViewFactory fromHtml(String html, DirectiveMap directives) {
ViewFactory viewFactory = _viewFactoryCache.get(html);
if (viewFactory == null) {
var div = new dom.Element.tag('div');
var div = new dom.DivElement();
div.setInnerHtml(html, treeSanitizer: treeSanitizer);
viewFactory = compiler(div.nodes, directives);
_viewFactoryCache.put(html, viewFactory);
Expand Down
5 changes: 1 addition & 4 deletions lib/directive/ng_base_css.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
part of angular.directive;

@Decorator(
selector: '[ng-base-css]',
visibility: Directive.CHILDREN_VISIBILITY
)
@Decorator(selector: '[ng-base-css]')
class NgBaseCss {
List<String> _urls = const [];

Expand Down
14 changes: 5 additions & 9 deletions lib/directive/ng_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ part of angular.directive;
*/
@Decorator(
selector: 'form',
module: NgForm.module,
visibility: Directive.CHILDREN_VISIBILITY)
module: NgForm.module)
@Decorator(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these changes have nothing to do with NgView

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only removed this (because this is the default value) everywhere for consistency

selector: 'fieldset',
module: NgForm.module,
visibility: Directive.CHILDREN_VISIBILITY)
module: NgForm.module)
@Decorator(
selector: '.ng-form',
module: NgForm.module,
visibility: Directive.CHILDREN_VISIBILITY)
module: NgForm.module)
@Decorator(
selector: '[ng-form]',
module: NgForm.module,
map: const { 'ng-form': '@name' },
visibility: Directive.CHILDREN_VISIBILITY)
map: const { 'ng-form': '@name' })
class NgForm extends NgControl {
static final Module _module = new Module()
..factory(NgControl, (i) => i.get(NgForm), visibility: Directive.CHILDREN_VISIBILITY);
..factory(NgControl, (i) => i.get(NgForm));
static module() => _module;

final Scope _scope;
Expand Down
3 changes: 1 addition & 2 deletions lib/directive/ng_model_select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ part of angular.directive;
*
*/
@Decorator(
selector: 'select[ng-model]',
visibility: Directive.CHILDREN_VISIBILITY)
selector: 'select[ng-model]')
class InputSelect implements AttachAware {
final expando = new Expando<OptionValue>();
final dom.SelectElement _selectElement;
Expand Down
1 change: 0 additions & 1 deletion lib/routing/ng_bind_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ part of angular.routing;
* however it does not effect view resolution by nested ng-view(s).
*/
@Decorator(
visibility: Directive.CHILDREN_VISIBILITY,
selector: '[ng-bind-route]',
module: NgBindRoute.module,
map: const {'ng-bind-route': '@routeName'})
Expand Down
86 changes: 47 additions & 39 deletions lib/routing/ng_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,49 +58,49 @@ part of angular.routing;
@Decorator(
selector: 'ng-view',
module: NgView.module,
visibility: Directive.CHILDREN_VISIBILITY)
children: Directive.TRANSCLUDE_CHILDREN)
class NgView implements DetachAware, RouteProvider {
static final Module _module = new Module()
..factory(RouteProvider,
(i) => i.get(NgView),
visibility: Directive.CHILDREN_VISIBILITY);
..factory(RouteProvider, (i) => i.get(NgView));

static module() => _module;

final NgRoutingHelper locationService;
final ViewCache viewCache;
final Injector injector;
final Element element;
final Scope scope;
final NgRoutingHelper _locationService;
final ViewCache _viewCache;
final Injector _injector;
final Scope _scope;
RouteHandle _route;

final ViewPort _viewPort;

View _view;
Scope _scope;
Scope _childScope;
Route _viewRoute;

NgView(this.element, this.viewCache,
Injector injector, Router router,
this.scope)
: injector = injector,
locationService = injector.get(NgRoutingHelper)

NgView(this._viewCache, Injector injector, Router router,
this._scope, this._viewPort)
: _injector = injector,
_locationService = injector.get(NgRoutingHelper)
{
RouteProvider routeProvider = injector.parent.get(NgView);
RouteProvider routeProvider = _injector.parent.get(NgView);
_route = routeProvider != null ?
routeProvider.route.newHandle() :
router.root.newHandle();
locationService._registerPortal(this);
_locationService._registerPortal(this);
_maybeReloadViews();
}

void _maybeReloadViews() {
if (_route.isActive) locationService._reloadViews(startingFrom: _route);
if (_route.isActive) _locationService._reloadViews(startingFrom: _route);
}

detach() {
void detach() {
_route.discard();
locationService._unregisterPortal(this);
_locationService._unregisterPortal(this);
}

_show(_View viewDef, Route route, List<Module> modules) {
void _show(_View viewDef, Route route, List<Module> modules) {
assert(route.isActive);

if (_viewRoute != null) return;
Expand All @@ -114,44 +114,52 @@ class NgView implements DetachAware, RouteProvider {
_cleanUp();
});

var viewInjector = injector;
if (modules != null) {
viewInjector = forceNewDirectivesAndFilters(viewInjector, modules);
}
var viewInjector = modules == null ?
_injector :
forceNewDirectivesAndFilters(_injector, modules);

var newDirectives = viewInjector.get(DirectiveMap);
var viewFuture = viewDef.templateHtml != null ?
new Future.value(viewCache.fromHtml(viewDef.templateHtml, newDirectives)) :
viewCache.fromUrl(viewDef.template, newDirectives);
new Future.value(_viewCache.fromHtml(viewDef.templateHtml, newDirectives)) :
_viewCache.fromUrl(viewDef.template, newDirectives);

viewFuture.then((viewFactory) {
_cleanUp();
_scope = scope.createChild(new PrototypeMap(scope.context));
_childScope = _scope.createChild(new PrototypeMap(_scope.context));
_view = viewFactory(
viewInjector.createChild(
[new Module()..value(Scope, _scope)]));
viewInjector.createChild([new Module()..value(Scope, _childScope)]));

_view.nodes.forEach((elm) => element.append(elm));
var view = _view;
_scope.rootScope.domWrite(() {
_viewPort.insert(view);
});
});
}

_cleanUp() {
void _cleanUp() {
if (_view == null) return;

_view.nodes.forEach((node) => node.remove());
_scope.destroy();
var view = _view;
var childScope = _childScope;
_scope.rootScope.domWrite(() {
_viewPort.remove(view);
childScope.destroy();
});

_view = null;
_scope = null;
_childScope = null;
}

Route get route => _viewRoute;

String get routeName => _viewRoute.name;

Map<String, String> get parameters {
var res = <String, String>{};
var p = _viewRoute;
while (p != null) {
res.addAll(p.parameters);
p = p.parent;
var route = _viewRoute;
while (route != null) {
res.addAll(route.parameters);
route = route.parent;
}
return res;
}
Expand Down
18 changes: 8 additions & 10 deletions lib/routing/routing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RouteViewFactory {

_configure(Route route, Map<String, NgRouteCfg> config) {
config.forEach((name, cfg) {
var moduledCalled = false;
var modulesCalled = false;
List<Module> newModules;
route.addRoute(
name: name,
Expand All @@ -37,8 +37,8 @@ class RouteViewFactory {
}
},
preEnter: (RoutePreEnterEvent e) {
if (cfg.modules != null && !moduledCalled) {
moduledCalled = true;
if (cfg.modules != null && !modulesCalled) {
modulesCalled = true;
var modules = cfg.modules();
if (modules is Future) {
e.allowEnter(modules.then((List<Module> m) {
Expand Down Expand Up @@ -92,10 +92,8 @@ class NgRouteCfg {
*
* The [init] method will be called by the framework once the router is
* instantiated but before [NgBindRouteDirective] and [NgViewDirective].
*
* Deprecated: use RouteInitializerFn instead.
*/
@deprecated
@Deprecated("use RouteInitializerFn instead")
abstract class RouteInitializer {
void init(Router router, RouteViewFactory viewFactory);
}
Expand Down Expand Up @@ -146,7 +144,7 @@ class NgRoutingHelper {
router.listen(appRoot: _ngApp.element);
}

_reloadViews({Route startingFrom}) {
void _reloadViews({Route startingFrom}) {
var alreadyActiveViews = [];
var activePath = router.activePath;
if (startingFrom != null) {
Expand All @@ -169,16 +167,16 @@ class NgRoutingHelper {
}
}

_route(Route route, String template, {bool fromEvent, List<Module> modules,
void _route(Route route, String template, {bool fromEvent, List<Module> modules,
String templateHtml}) {
_templates[_routePath(route)] = new _View(template, templateHtml, modules);
}

_registerPortal(NgView ngView) {
void _registerPortal(NgView ngView) {
portals.add(ngView);
}

_unregisterPortal(NgView ngView) {
void _unregisterPortal(NgView ngView) {
portals.remove(ngView);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/directive/ng_if_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ main() {
compile(html);
expect(element).toHaveText('content');
element.querySelector('span').classes.remove('my-class');
expect(element.querySelector('span').classes.contains('my-class')).not.toBe(true);
expect(element.querySelector('span')).not.toHaveClass('my-class');
rootScope.apply(() {
rootScope.context['isVisible'] = false;
});
Expand All @@ -150,7 +150,7 @@ main() {
rootScope.context['isVisible'] = true;
});
// The newly inserted node should be a copy of the compiled state.
expect(element.querySelector('span').classes.contains('my-class')).toBe(true);
expect(element.querySelector('span')).toHaveClass('my-class');
}
);

Expand Down
Loading