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
1 change: 1 addition & 0 deletions modules/angular2/angular2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './core';
export * from './profile';
export * from './lifecycle_hooks';
export * from './bootstrap';
export * from './upgrade';
117 changes: 117 additions & 0 deletions modules/angular2/src/upgrade/angular_js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
export interface IModule {
config(fn: any): IModule;
directive(selector: string, factory: any): IModule;
controller(name: string, type: any): IModule;
factory(key: string, factoryFn: any): IModule;
value(key: string, value: any): IModule;
run(a: any): void;
}
export interface ICompileService {
(element: Element | NodeList | string, transclude?: Function): ILinkFn;
}
export interface ILinkFn {
(scope: IScope, cloneAttachFn?: Function, options?: ILinkFnOptions): void;
}
export interface ILinkFnOptions {
parentBoundTranscludeFn?: Function;
transcludeControllers?: {[key: string]: any};
futureParentElement?: Node;
}
export interface IRootScopeService {
$new(isolate?: boolean): IScope;
$id: string;
$watch(expr: any, fn?: (a1?: any, a2?: any) => void): Function;
$apply(): any;
$apply(exp: string): any;
$apply(exp: Function): any;
$$childTail: IScope;
$$childHead: IScope;
$$nextSibling: IScope;
}
export interface IScope extends IRootScopeService {}
export interface IAngularBootstrapConfig {}
export interface IDirective {
compile?: IDirectiveCompileFn;
controller?: any;
controllerAs?: string;
bindToController?: boolean | Object;
link?: IDirectiveLinkFn | IDirectivePrePost;
name?: string;
priority?: number;
replace?: boolean;
require?: any;
restrict?: string;
scope?: any;
template?: any;
templateUrl?: any;
terminal?: boolean;
transclude?: any;
}
export interface IDirectiveCompileFn {
(templateElement: IAugmentedJQuery, templateAttributes: IAttributes,
transclude: ITranscludeFunction): IDirectivePrePost;
}
export interface IDirectivePrePost {
pre?: IDirectiveLinkFn;
post?: IDirectiveLinkFn;
}
export interface IDirectiveLinkFn {
(scope: IScope, instanceElement: IAugmentedJQuery, instanceAttributes: IAttributes,
controller: any, transclude: ITranscludeFunction): void;
}
export interface IAttributes { $observe(attr: string, fn: (v: string) => void): void; }
export interface ITranscludeFunction {
// If the scope is provided, then the cloneAttachFn must be as well.
(scope: IScope, cloneAttachFn: ICloneAttachFunction): IAugmentedJQuery;
// If one argument is provided, then it's assumed to be the cloneAttachFn.
(cloneAttachFn?: ICloneAttachFunction): IAugmentedJQuery;
}
export interface ICloneAttachFunction {
// Let's hint but not force cloneAttachFn's signature
(clonedElement?: IAugmentedJQuery, scope?: IScope): any;
}
export interface IAugmentedJQuery {
bind(name: string, fn: () => void): void;
data(name: string, value?: any): any;
inheritedData(name: string, value?: any): any;
contents(): IAugmentedJQuery;
parent(): IAugmentedJQuery;
length: number;
[index: number]: Node;
}
export interface IParseService { (expression: string): ICompiledExpression; }
export interface ICompiledExpression { assign(context: any, value: any): any; }
export interface IHttpBackendService {
(method: string, url: string, post?: any, callback?: Function, headers?: any, timeout?: number,
withCredentials?: boolean): void;
}
export interface ICacheObject {
put<T>(key: string, value?: T): T;
get(key: string): any;
}
export interface ITemplateCacheService extends ICacheObject {}
export interface IControllerService {
(controllerConstructor: Function, locals?: any, later?: any, ident?: any): any;
(controllerName: string, locals?: any): any;
}

export interface IInjectorService { get(key: string): any; }

var angular: {
bootstrap: (e: Element, modules: string[], config: IAngularBootstrapConfig) => void,
module: (prefix: string, dependencies?: string[]) => IModule,
element: (e: Element) => IAugmentedJQuery,
version: {major: number}
} = null;

try {
angular = (<any>window).angular;
} catch (e) {
// ignore in CJS mode.
angular = {bootstrap: null, module: null, element: null, version: null};
}

export var bootstrap = angular.bootstrap;
export var module = angular.module;
export var element = angular.element;
export var version = angular.version;
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import {
HostViewRef,
Injector,
ProtoViewRef,
SimpleChange,
ViewRef
SimpleChange
} from 'angular2/angular2';
import {NG1_SCOPE} from './constants';
import {ComponentInfo} from './metadata';
import {ViewRef_} from "../../angular2/src/core/linker/view_ref";
import Element = protractor.Element;
import * as angular from './angular_js';

const INITIAL_VALUE = {
__UNINITIALIZED__: true
Expand Down Expand Up @@ -50,7 +49,7 @@ export class DowngradeNg2ComponentAdapter {
this.contentInserctionPoint = renderer.rootContentInsertionPoints[0];
}

setupInputs() {
setupInputs(): void {
var attrs = this.attrs;
var inputs = this.info.inputs;
for (var i = 0; i < inputs.length; i++) {
Expand All @@ -67,7 +66,7 @@ export class DowngradeNg2ComponentAdapter {
prevValue = value;
}
this.component[prop] = value;
}
};
})(input.prop);
attrs.$observe(input.attr, observeFn);
} else if (attrs.hasOwnProperty(input.bindAttr)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import {Type, ComponentMetadata, DirectiveResolver, DirectiveMetadata} from 'angular2/angular2';
import {stringify} from 'upgrade/src/util';
import {stringify} from './util';

var COMPONENT_SELECTOR = /^[\w|-]*$/;
var SKEWER_CASE = /-(\w)/g;
var directiveResolver = new DirectiveResolver();

interface Reflect {
getOwnMetadata(name: string, type: Function): any;
defineMetadata(name: string, value: any, cls: Type): void;
}
var Reflect: Reflect = <Reflect>(<any>window).Reflect;
if (!(Reflect && (<any>Reflect)['getOwnMetadata'])) {
throw 'reflect-metadata shim is required when using class decorators';
}

export interface AttrProp {
prop: string;
attr: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
///<reference path="./angular.d.ts"/>

import {
bind,
provide,
Expand Down Expand Up @@ -36,6 +34,7 @@ import {
} from './constants';
import {DowngradeNg2ComponentAdapter} from './downgrade_ng2_adapter';
import {UpgradeNg1ComponentAdapterBuilder} from './upgrade_ng1_adapter';
import * as angular from './angular_js';

var upgradeCount: number = 0;

Expand Down Expand Up @@ -295,7 +294,7 @@ export class UpgradeAdapter {
bootstrap(element: Element, modules?: any[],
config?: angular.IAngularBootstrapConfig): UpgradeAdapterRef {
var upgrade = new UpgradeAdapterRef();
var ng1Injector: angular.auto.IInjectorService = null;
var ng1Injector: angular.IInjectorService = null;
var platformRef: PlatformRef = platform();
var applicationRef: ApplicationRef = platformRef.application([
applicationCommonProviders(),
Expand Down Expand Up @@ -341,7 +340,7 @@ export class UpgradeAdapter {
.run([
'$injector',
'$rootScope',
(injector: angular.auto.IInjectorService, rootScope: angular.IRootScopeService) => {
(injector: angular.IInjectorService, rootScope: angular.IRootScopeService) => {
ng1Injector = injector;
ngZone.overrideOnTurnDone(() => rootScope.$apply());
ng1compilePromise =
Expand Down Expand Up @@ -440,7 +439,7 @@ export class UpgradeAdapter {
public upgradeNg1Provider(name: string, options?: {asToken: any}) {
var token = options && options.asToken || name;
this.providers.push(provide(token, {
useFactory: (ng1Injector: angular.auto.IInjectorService) => ng1Injector.get(name),
useFactory: (ng1Injector: angular.IInjectorService) => ng1Injector.get(name),
deps: [NG1_INJECTOR]
}));
}
Expand Down Expand Up @@ -469,7 +468,7 @@ export class UpgradeAdapter {
*/
public downgradeNg2Provider(token: any): Function {
var factory = function(injector: Injector) { return injector.get(token); };
factory.$inject = [NG2_INJECTOR];
(<any>factory).$inject = [NG2_INJECTOR];
return factory;
}

Expand All @@ -496,7 +495,7 @@ interface ProtoViewRefMap {
}

function ng1ComponentDirective(info: ComponentInfo, idPrefix: string): Function {
directiveFactory.$inject = [NG2_PROTO_VIEW_REF_MAP, NG2_APP_VIEW_MANAGER, NG1_PARSE];
(<any>directiveFactory).$inject = [NG2_PROTO_VIEW_REF_MAP, NG2_APP_VIEW_MANAGER, NG1_PARSE];
function directiveFactory(protoViewRefMap: ProtoViewRefMap, viewManager: AppViewManager,
parse: angular.IParseService): angular.IDirective {
var protoView: ProtoViewRef = protoViewRefMap[info.selector];
Expand Down Expand Up @@ -532,13 +531,12 @@ export class UpgradeAdapterRef {
private _readyFn: (upgradeAdapterRef?: UpgradeAdapterRef) => void = null;

public ng1RootScope: angular.IRootScopeService = null;
public ng1Injector: angular.auto.IInjectorService = null;
public ng1Injector: angular.IInjectorService = null;
public ng2ApplicationRef: ApplicationRef = null;
public ng2Injector: Injector = null;

/* @internal */
private _bootstrapDone(applicationRef: ApplicationRef,
ng1Injector: angular.auto.IInjectorService) {
private _bootstrapDone(applicationRef: ApplicationRef, ng1Injector: angular.IInjectorService) {
this.ng2ApplicationRef = applicationRef;
this.ng2Injector = applicationRef.injector;
this.ng1Injector = ng1Injector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
NG1_CONTROLLER
} from './constants';
import {controllerKey} from './util';
import * as angular from './angular_js';

const CAMEL_CASE = /([A-Z])/g;
const INITIAL_VALUE = {
Expand Down Expand Up @@ -57,7 +58,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
});
}

extractDirective(injector: angular.auto.IInjectorService): angular.IDirective {
extractDirective(injector: angular.IInjectorService): angular.IDirective {
var directives: angular.IDirective[] = injector.get(this.name + 'Directive');
if (directives.length > 1) {
throw new Error('Only support single directive definition for: ' + this.name);
Expand Down Expand Up @@ -141,15 +142,15 @@ export class UpgradeNg1ComponentAdapterBuilder {
throw new Error(`Directive '${this.name}' is not a component, it is missing template.`);
}
return null;
function compileHtml(html) {
function compileHtml(html): angular.ILinkFn {
var div = document.createElement('div');
div.innerHTML = html;
return compile(div.childNodes);
}
}

static resolve(exportedComponents: {[name: string]: UpgradeNg1ComponentAdapterBuilder},
injector: angular.auto.IInjectorService): Promise<any> {
injector: angular.IInjectorService): Promise<any> {
var promises = [];
var compile: angular.ICompileService = injector.get(NG1_COMPILE);
var templateCache: angular.ITemplateCacheService = injector.get(NG1_TEMPLATE_CACHE);
Expand All @@ -162,7 +163,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
exportedComponent.$controller = $controller;
exportedComponent.extractBindings();
var promise = exportedComponent.compileTemplate(compile, templateCache, httpBackend);
if (promise) promises.push(promise)
if (promise) promises.push(promise);
}
}
return Promise.all(promises);
Expand Down Expand Up @@ -208,7 +209,7 @@ class UpgradeNg1ComponentAdapter implements OnChanges, DoCheck {
for (var i = 0, ii = clonedElement.length; i < ii; i++) {
element.appendChild(clonedElement[i]);
}
}, {parentBoundTranscludeFn: (scope, cloneAttach) => { cloneAttach(childNodes) }});
}, {parentBoundTranscludeFn: (scope, cloneAttach) => { cloneAttach(childNodes); }});

for (var i = 0; i < inputs.length; i++) {
this[inputs[i]] = null;
Expand All @@ -223,16 +224,16 @@ class UpgradeNg1ComponentAdapter implements OnChanges, DoCheck {
}
}

onChanges(changes) {
onChanges(changes: {[name: string]: SimpleChange}) {
for (var name in changes) {
if (changes.hasOwnProperty(name)) {
if ((<Object>changes).hasOwnProperty(name)) {
var change: SimpleChange = changes[name];
this.setComponentProperty(name, change.currentValue);
}
}
}

doCheck() {
doCheck(): number {
var count = 0;
var destinationObj = this.destinationObj;
var lastValues = this.checkLastValues;
Expand Down
File renamed without changes.
18 changes: 17 additions & 1 deletion modules/angular2/test/public_api_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {SymbolsDiff} from './symbol_inspector/symbol_differ';
// =================================================================================================
// =================================================================================================

var NG_API = [
var NG_ALL = [
'APP_COMPONENT',
'APP_ID',
'AbstractProviderError',
Expand Down Expand Up @@ -1450,6 +1450,22 @@ var NG_API = [
'Stream.where():dart',
];

var NG_UPGRADE = [
'UpgradeAdapter:js',
'UpgradeAdapter.addProvider():js',
'UpgradeAdapter.bootstrap():js',
'UpgradeAdapter.compileNg2Components():js',
'UpgradeAdapter.downgradeNg2Component():js',
'UpgradeAdapter.downgradeNg2Provider():js',
'UpgradeAdapter.upgradeNg1Component():js',
'UpgradeAdapter.upgradeNg1Provider():js',
'UpgradeAdapterRef:js',
'UpgradeAdapterRef.dispose():js',
'UpgradeAdapterRef.ready():js'
];

var NG_API = [].concat(NG_ALL).concat(NG_UPGRADE);

export function main() {
/**
var x = getSymbolsFromLibrary('ng');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {
} from 'angular2/testing_internal';

import {Component, View} from 'angular2/angular2';
import {getComponentInfo, parseFields} from 'upgrade/src/metadata';
import {getComponentInfo, parseFields} from 'angular2/src/upgrade/metadata';
import {DOM} from 'angular2/src/core/dom/dom_adapter';

export function main() {
if (!DOM.supportsDOMEvents()) return;
describe('upgrade metadata', () => {
it('should extract component selector', () => {
expect(getComponentInfo(ElementNameComponent).selector).toEqual('elementNameDashed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
xdescribe,
xit,
} from 'angular2/testing_internal';
import {DOM} from 'angular2/src/core/dom/dom_adapter';

import {Component, Class, Inject, EventEmitter, ApplicationRef, provide} from 'angular2/angular2';
import {UpgradeAdapter} from 'upgrade/upgrade';
import {UpgradeAdapter} from 'angular2/upgrade';
import * as angular from 'angular2/src/upgrade/angular_js';

export function main() {
if (!DOM.supportsDOMEvents()) return;
describe('adapter: ng1 to ng2', () => {
it('should have angular 1 loaded', () => expect(angular.version.major).toBe(1));

Expand Down
6 changes: 6 additions & 0 deletions modules/angular2/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @module
* @description
* Adapter allowing AngularJS v1 and Angular v2 to run side by side in the same application.
*/
export {UpgradeAdapter, UpgradeAdapterRef} from './src/upgrade/upgrade_adapter';
Loading
X Tutup