chore(ngUpgrade): Move into Angular2

This is moving ngUpgrade into the main repository per #4838.
The ngUpgrade is published from the main import consistent with
https://docs.google.com/document/d/1rbVTKTYLz6p2smQNYI8h4-QN-m2PS6F3iQIDmSzn0Ww/edit#heading=h.6cxvr9awtf5r
Closes #4931
This commit is contained in:
Misko Hevery 2015-10-26 20:17:46 -07:00
parent 54f7e62c43
commit 1b78342e23
19 changed files with 183 additions and 175 deletions

View File

@ -2,3 +2,4 @@ export * from './core';
export * from './profile';
export * from './lifecycle_hooks';
export * from './bootstrap';
export * from './upgrade';

View File

@ -0,0 +1,123 @@
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; }
function noNg() {
throw new Error('AngularJS v1.x is not loaded!');
}
var angular: {
bootstrap: (e: Element, modules: string[], config: IAngularBootstrapConfig) => void,
module: (prefix: string, dependencies?: string[]) => IModule,
element: (e: Element) => IAugmentedJQuery,
version: {major: number}
} = <any>{bootstrap: noNg, module: noNg, element: noNg, version: noNg};
try {
if (window.hasOwnProperty('angular')) {
angular = (<any>window).angular;
}
} catch (e) {
// ignore in CJS mode.
}
export var bootstrap = angular.bootstrap;
export var module = angular.module;
export var element = angular.element;
export var version = angular.version;

View File

@ -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
@ -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++) {
@ -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)) {

View File

@ -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;

View File

@ -1,5 +1,3 @@
///<reference path="./angular.d.ts"/>
import {
bind,
provide,
@ -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;
@ -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(),
@ -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 =
@ -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]
}));
}
@ -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;
}
@ -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];
@ -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;

View File

@ -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 = {
@ -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);
@ -141,7 +142,7 @@ 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);
@ -149,7 +150,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
}
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);
@ -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);
@ -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;
@ -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;

View File

@ -25,7 +25,7 @@ import {SymbolsDiff} from './symbol_inspector/symbol_differ';
// =================================================================================================
// =================================================================================================
var NG_API = [
var NG_ALL = [
'APP_COMPONENT',
'APP_ID',
'AbstractProviderError',
@ -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');

View File

@ -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');

View File

@ -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));

View File

@ -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';

View File

@ -1,5 +1,6 @@
import {Component, Input, Output, EventEmitter} from 'angular2/angular2';
import {UpgradeAdapter} from 'upgrade/upgrade';
import {UpgradeAdapter} from 'angular2/angular2';
import * as angular from '../../../angular2/src/upgrade/angular_js';
var styles = [
`

View File

@ -1,115 +0,0 @@
declare namespace angular {
function module(prefix: string, dependencies?: string[]);
interface IModule {
config(fn: any): IModule;
directive(selector: string, factory: any): IModule;
value(key: string, value: any): IModule;
run(a: any);
}
interface ICompileService {
(element: Element | NodeList | string, transclude?: Function): ILinkFn;
}
interface ILinkFn {
(scope: IScope, cloneAttachFn?: Function, options?: ILinkFnOptions): void
}
interface ILinkFnOptions {
parentBoundTranscludeFn?: Function, transcludeControllers?: {[key: string]: any},
futureParentElement?: Node
}
interface IRootScopeService {
$new(isolate?: boolean): IScope;
$id: string;
$watch(expr: any, fn?: (a1?: any, a2?: any) => void);
$apply(): any;
$apply(exp: string): any;
$apply(exp: Function): any;
$$childTail: IScope;
$$childHead: IScope;
$$nextSibling: IScope;
}
interface IScope extends IRootScopeService {}
interface IAngularBootstrapConfig {}
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;
}
interface IDirectiveCompileFn {
(templateElement: IAugmentedJQuery, templateAttributes: IAttributes,
transclude: ITranscludeFunction): IDirectivePrePost;
}
interface IDirectivePrePost {
pre?: IDirectiveLinkFn;
post?: IDirectiveLinkFn;
}
interface IDirectiveLinkFn {
(scope: IScope, instanceElement: IAugmentedJQuery, instanceAttributes: IAttributes,
controller: any, transclude: ITranscludeFunction): void;
}
interface IAttributes {
$observe(attr: string, fn: (v: string) => void);
}
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;
}
interface ICloneAttachFunction {
// Let's hint but not force cloneAttachFn's signature
(clonedElement?: IAugmentedJQuery, scope?: IScope): any;
}
interface IAugmentedJQuery {
bind(name: string, fn: () => void);
data(name: string, value?: any);
inheritedData(name: string, value?: any);
contents(): IAugmentedJQuery;
parent(): IAugmentedJQuery;
length: number;
[index: number]: Node;
}
interface IParseService {
(expression: string): ICompiledExpression;
}
interface ICompiledExpression {
assign(context: any, value: any): any;
}
function element(e: Element): IAugmentedJQuery;
function bootstrap(e: Element, modules: string[], config: IAngularBootstrapConfig);
interface IHttpBackendService {
(method: string, url: string, post?: any, callback?: Function, headers?: any, timeout?: number,
withCredentials?: boolean): void;
}
interface ICacheObject {
put<T>(key: string, value?: T): T;
get(key: string): any;
}
interface ITemplateCacheService extends ICacheObject {}
interface IControllerService {
(controllerConstructor: Function, locals?: any, later?: any, ident?: any): any;
(controllerName: string, locals?: any): any;
}
namespace auto {
interface IInjectorService {
get(key: string): any;
}
}
var version: {major: number};
}
interface Function {
$inject?: string[];
}

View File

@ -1,17 +0,0 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es3",
"noImplicitAny": false,
"rootDir": ".",
"sourceMap": false,
"noEmit": true
},
"files": [
"src/angular.d.ts",
"upgrade.ts"
],
"exclude": [
"node_modules"
]
}

View File

@ -1 +0,0 @@
export {UpgradeAdapter} from './src/upgrade_adapter';

View File

@ -14,8 +14,7 @@ System.config({
'benchpress/*': 'dist/js/dev/es5/benchpress/*.js',
'angular2/*': 'dist/js/dev/es5/angular2/*.js',
'angular2_material/*': 'dist/js/dev/es5/angular2_material/*.js',
'@reactivex/rxjs/*': 'node_modules/@reactivex/rxjs/*.js',
'upgrade/*': 'dist/js/dev/es5/upgrade/*.js'
'@reactivex/rxjs/*': 'node_modules/@reactivex/rxjs/*.js'
}
});

View File

@ -5,7 +5,6 @@
baseURL: '/',
packages: {
'angular2_material': {defaultExtension: 'js'},
'upgrade': {defaultExtension: 'js'},
'benchmarks': {defaultExtension: 'js'},
'playground': {defaultExtension: 'js'},
// TODO(rado): These helpers don't end up in the bundle, thus they should

View File

@ -15,14 +15,16 @@ import replace from '../broccoli-replace';
var global_excludes = [
'angular2/http*',
'angular2/upgrade*',
'angular2/examples/*/ts/**/*',
'angular2/src/http/**/*',
'angular2/test/http/**/*',
'angular2/src/upgrade/**/*',
'angular2/test/upgrade/**/*',
'playground/src/http/**/*',
'playground/test/http/**/*',
'playground/src/jsonp/**/*',
'playground/test/jsonp/**/*',
'upgrade/**/*'
'playground/test/jsonp/**/*'
];