X Tutup
Skip to content
Closed
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
54 changes: 41 additions & 13 deletions modules/angular1_router/src/module_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ angular.module('ngComponentRouter').
// Because Angular 1 has no notion of a root component, we use an object with unique identity
// to represent this. Can be overloaded with a component name
value('$routerRootComponent', new Object()).
factory('$rootRouter', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', '$routerRootComponent', routerFactory]);
factory('$rootRouter', ['$q', '$location', '$browser', '$rootScope', '$injector', '$routerRootComponent', routerFactory]);

function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootScope, $injector, $routerRootComponent) {
function routerFactory($q, $location, $browser, $rootScope, $injector, $routerRootComponent) {

// When this file is processed, the line below is replaced with
// the contents of `../lib/facades.es5`.
Expand All @@ -23,11 +23,24 @@ function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootSc
// the contents of the compiled TypeScript classes.
//{{SHARED_CODE}}

function getComponentConstructor(name) {
var serviceName = name + 'Directive';
if ($injector.has(serviceName)) {
var definitions = $injector.get(serviceName);
if (definitions.length > 1) {
throw new BaseException('too many directives named "' + name + '"');
}
return definitions[0].controller;
} else {
throw new BaseException('directive "' + name + '" is not registered');
}
}

//TODO: this is a hack to replace the exiting implementation at run-time
exports.getCanActivateHook = function (directiveName) {
var factory = $$directiveIntrospector.getTypeByName(directiveName);
return factory && factory.$canActivate && function (next, prev) {
return $injector.invoke(factory.$canActivate, null, {
var controller = getComponentConstructor(directiveName);
return controller.$canActivate && function (next, prev) {
return $injector.invoke(controller.$canActivate, null, {
$nextInstruction: next,
$prevInstruction: prev
});
Expand All @@ -45,17 +58,32 @@ function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootSc
var RouteRegistry = exports.RouteRegistry;
var RootRouter = exports.RootRouter;

// Override this method to actually get hold of the child routes
RouteRegistry.prototype.configFromComponent = function (component) {
var that = this;
if (isString(component)) {
// Don't read the annotations component a type more than once –
// this prevents an infinite loop if a component routes recursively.
if (this._rules.has(component)) {
return;
}
var controller = getComponentConstructor(component);
if (angular.isArray(controller.$routeConfig)) {
controller.$routeConfig.forEach(function (config) {
var loader = config.loader;
if (isPresent(loader)) {
config = angular.extend({}, config, { loader: () => $injector.invoke(loader) });
}
that.config(component, config);
});
}
}

}

var registry = new RouteRegistry($routerRootComponent);
var location = new Location();

$$directiveIntrospector(function (name, factory) {
if (angular.isArray(factory.$routeConfig)) {
factory.$routeConfig.forEach(function (config) {
registry.config(name, config);
});
}
});

var router = new RootRouter(registry, location, $routerRootComponent);
$rootScope.$watch(function () { return $location.url(); }, function (path) {
if (router.lastNavigationAttempt !== path) {
Expand Down
52 changes: 0 additions & 52 deletions modules/angular1_router/src/ng_outlet.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,6 @@
///<reference path="../typings/angularjs/angular.d.ts"/>

/*
* decorates $compileProvider so that we have access to routing metadata
*/
function compilerProviderDecorator($compileProvider,
$$directiveIntrospectorProvider: DirectiveIntrospectorProvider) {
let directive = $compileProvider.directive;
$compileProvider.directive = function(name: string, factory: Function) {
$$directiveIntrospectorProvider.register(name, factory);
return directive.apply(this, arguments);
};
}

/*
* private service that holds route mappings for each controller
*/
class DirectiveIntrospectorProvider {
private directiveBuffer: any[] = [];
private directiveFactoriesByName: {[name: string]: Function} = {};
private onDirectiveRegistered: (name: string, factory: Function) => any = null;

register(name: string, factory: Function) {
if (angular.isArray(factory)) {
factory = factory[factory.length - 1];
}
this.directiveFactoriesByName[name] = factory;
if (this.onDirectiveRegistered) {
this.onDirectiveRegistered(name, factory);
} else {
this.directiveBuffer.push({name: name, factory: factory});
}
}

$get() {
let fn: any = newOnControllerRegistered => {
this.onDirectiveRegistered = newOnControllerRegistered;
while (this.directiveBuffer.length > 0) {
let directive = this.directiveBuffer.pop();
this.onDirectiveRegistered(directive.name, directive.factory);
}
};

fn.getTypeByName = name => this.directiveFactoriesByName[name];

return fn;
}
}

/**
* @name ngOutlet
Expand Down Expand Up @@ -303,10 +258,3 @@ angular.module('ngComponentRouter', [])
.directive('ngOutlet', ['$compile', ngOutletFillContentDirective])
.directive('ngLink', ['$rootRouter', '$parse', ngLinkDirective])
.directive('$router', ['$q', routerTriggerDirective]);

/*
* A module for inspecting controller constructors
*/
angular.module('ng')
.provider('$$directiveIntrospector', DirectiveIntrospectorProvider)
.config(['$compileProvider', '$$directiveIntrospectorProvider', compilerProviderDecorator]);
2 changes: 1 addition & 1 deletion modules/angular1_router/src/ng_route_shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
}];

// we resolve the locals in a canActivate block
componentDefinition.$canActivate = function() {
componentDefinition.controller.$canActivate = function() {
var locals = angular.extend({}, routeCopy.resolve);

angular.forEach(locals, function(value, key) {
Expand Down
38 changes: 0 additions & 38 deletions modules/angular1_router/test/directive_introspector_spec.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,10 @@ describe('Navigation lifecycle', function () {
}

if (options.$canActivate) {
factory.$canActivate = options.$canActivate;
controller.$canActivate = options.$canActivate;
}
if (options.$routeConfig) {
factory.$routeConfig = options.$routeConfig;
controller.$routeConfig = options.$routeConfig;
}

$compileProvider.directive(name, factory);
Expand Down
7 changes: 4 additions & 3 deletions modules/angular1_router/test/integration/navigation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,15 @@ describe('navigation', function () {
}));

function registerDirective(name, options) {
var controller = getController(options);
function factory() {
return {
template: options.template || '',
controllerAs: name,
controller: getController(options)
controller: controller
};
}
applyStaticProperties(factory, options);
applyStaticProperties(controller, options);
$compileProvider.directive(name, factory);
}

Expand All @@ -323,7 +324,7 @@ describe('navigation', function () {
template: options.template || '',
controller: getController(options),
}
applyStaticProperties(definition, options);
applyStaticProperties(definition.controller, options);
$compileProvider.component(name, definition);
}

Expand Down
Loading
X Tutup