Skip to content

Commit 1b78342

Browse files
committed
chore(ngUpgrade): Move into Angular2
This is moving ngUpgrade into the main repository per angular#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 angular#4931
1 parent 54f7e62 commit 1b78342

19 files changed

+183
-175
lines changed

modules/angular2/angular2.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './core';
22
export * from './profile';
33
export * from './lifecycle_hooks';
44
export * from './bootstrap';
5+
export * from './upgrade';
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
export interface IModule {
2+
config(fn: any): IModule;
3+
directive(selector: string, factory: any): IModule;
4+
controller(name: string, type: any): IModule;
5+
factory(key: string, factoryFn: any): IModule;
6+
value(key: string, value: any): IModule;
7+
run(a: any): void;
8+
}
9+
export interface ICompileService {
10+
(element: Element | NodeList | string, transclude?: Function): ILinkFn;
11+
}
12+
export interface ILinkFn {
13+
(scope: IScope, cloneAttachFn?: Function, options?: ILinkFnOptions): void;
14+
}
15+
export interface ILinkFnOptions {
16+
parentBoundTranscludeFn?: Function;
17+
transcludeControllers?: {[key: string]: any};
18+
futureParentElement?: Node;
19+
}
20+
export interface IRootScopeService {
21+
$new(isolate?: boolean): IScope;
22+
$id: string;
23+
$watch(expr: any, fn?: (a1?: any, a2?: any) => void): Function;
24+
$apply(): any;
25+
$apply(exp: string): any;
26+
$apply(exp: Function): any;
27+
$$childTail: IScope;
28+
$$childHead: IScope;
29+
$$nextSibling: IScope;
30+
}
31+
export interface IScope extends IRootScopeService {}
32+
export interface IAngularBootstrapConfig {}
33+
export interface IDirective {
34+
compile?: IDirectiveCompileFn;
35+
controller?: any;
36+
controllerAs?: string;
37+
bindToController?: boolean | Object;
38+
link?: IDirectiveLinkFn | IDirectivePrePost;
39+
name?: string;
40+
priority?: number;
41+
replace?: boolean;
42+
require?: any;
43+
restrict?: string;
44+
scope?: any;
45+
template?: any;
46+
templateUrl?: any;
47+
terminal?: boolean;
48+
transclude?: any;
49+
}
50+
export interface IDirectiveCompileFn {
51+
(templateElement: IAugmentedJQuery, templateAttributes: IAttributes,
52+
transclude: ITranscludeFunction): IDirectivePrePost;
53+
}
54+
export interface IDirectivePrePost {
55+
pre?: IDirectiveLinkFn;
56+
post?: IDirectiveLinkFn;
57+
}
58+
export interface IDirectiveLinkFn {
59+
(scope: IScope, instanceElement: IAugmentedJQuery, instanceAttributes: IAttributes,
60+
controller: any, transclude: ITranscludeFunction): void;
61+
}
62+
export interface IAttributes { $observe(attr: string, fn: (v: string) => void): void; }
63+
export interface ITranscludeFunction {
64+
// If the scope is provided, then the cloneAttachFn must be as well.
65+
(scope: IScope, cloneAttachFn: ICloneAttachFunction): IAugmentedJQuery;
66+
// If one argument is provided, then it's assumed to be the cloneAttachFn.
67+
(cloneAttachFn?: ICloneAttachFunction): IAugmentedJQuery;
68+
}
69+
export interface ICloneAttachFunction {
70+
// Let's hint but not force cloneAttachFn's signature
71+
(clonedElement?: IAugmentedJQuery, scope?: IScope): any;
72+
}
73+
export interface IAugmentedJQuery {
74+
bind(name: string, fn: () => void): void;
75+
data(name: string, value?: any): any;
76+
inheritedData(name: string, value?: any): any;
77+
contents(): IAugmentedJQuery;
78+
parent(): IAugmentedJQuery;
79+
length: number;
80+
[index: number]: Node;
81+
}
82+
export interface IParseService { (expression: string): ICompiledExpression; }
83+
export interface ICompiledExpression { assign(context: any, value: any): any; }
84+
export interface IHttpBackendService {
85+
(method: string, url: string, post?: any, callback?: Function, headers?: any, timeout?: number,
86+
withCredentials?: boolean): void;
87+
}
88+
export interface ICacheObject {
89+
put<T>(key: string, value?: T): T;
90+
get(key: string): any;
91+
}
92+
export interface ITemplateCacheService extends ICacheObject {}
93+
export interface IControllerService {
94+
(controllerConstructor: Function, locals?: any, later?: any, ident?: any): any;
95+
(controllerName: string, locals?: any): any;
96+
}
97+
98+
export interface IInjectorService { get(key: string): any; }
99+
100+
function noNg() {
101+
throw new Error('AngularJS v1.x is not loaded!');
102+
}
103+
104+
var angular: {
105+
bootstrap: (e: Element, modules: string[], config: IAngularBootstrapConfig) => void,
106+
module: (prefix: string, dependencies?: string[]) => IModule,
107+
element: (e: Element) => IAugmentedJQuery,
108+
version: {major: number}
109+
} = <any>{bootstrap: noNg, module: noNg, element: noNg, version: noNg};
110+
111+
112+
try {
113+
if (window.hasOwnProperty('angular')) {
114+
angular = (<any>window).angular;
115+
}
116+
} catch (e) {
117+
// ignore in CJS mode.
118+
}
119+
120+
export var bootstrap = angular.bootstrap;
121+
export var module = angular.module;
122+
export var element = angular.element;
123+
export var version = angular.version;

modules/upgrade/src/downgrade_ng2_adapter.ts renamed to modules/angular2/src/upgrade/downgrade_ng2_adapter.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import {
66
HostViewRef,
77
Injector,
88
ProtoViewRef,
9-
SimpleChange,
10-
ViewRef
9+
SimpleChange
1110
} from 'angular2/angular2';
1211
import {NG1_SCOPE} from './constants';
1312
import {ComponentInfo} from './metadata';
14-
import {ViewRef_} from "../../angular2/src/core/linker/view_ref";
1513
import Element = protractor.Element;
14+
import * as angular from './angular_js';
1615

1716
const INITIAL_VALUE = {
1817
__UNINITIALIZED__: true
@@ -50,7 +49,7 @@ export class DowngradeNg2ComponentAdapter {
5049
this.contentInserctionPoint = renderer.rootContentInsertionPoints[0];
5150
}
5251

53-
setupInputs() {
52+
setupInputs(): void {
5453
var attrs = this.attrs;
5554
var inputs = this.info.inputs;
5655
for (var i = 0; i < inputs.length; i++) {
@@ -67,7 +66,7 @@ export class DowngradeNg2ComponentAdapter {
6766
prevValue = value;
6867
}
6968
this.component[prop] = value;
70-
}
69+
};
7170
})(input.prop);
7271
attrs.$observe(input.attr, observeFn);
7372
} else if (attrs.hasOwnProperty(input.bindAttr)) {

modules/upgrade/src/metadata.ts renamed to modules/angular2/src/upgrade/metadata.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import {Type, ComponentMetadata, DirectiveResolver, DirectiveMetadata} from 'angular2/angular2';
2-
import {stringify} from 'upgrade/src/util';
2+
import {stringify} from './util';
33

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

8-
interface Reflect {
9-
getOwnMetadata(name: string, type: Function): any;
10-
defineMetadata(name: string, value: any, cls: Type): void;
11-
}
12-
var Reflect: Reflect = <Reflect>(<any>window).Reflect;
13-
if (!(Reflect && (<any>Reflect)['getOwnMetadata'])) {
14-
throw 'reflect-metadata shim is required when using class decorators';
15-
}
16-
178
export interface AttrProp {
189
prop: string;
1910
attr: string;

modules/upgrade/src/upgrade_adapter.ts renamed to modules/angular2/src/upgrade/upgrade_adapter.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
///<reference path="./angular.d.ts"/>
2-
31
import {
42
bind,
53
provide,
@@ -36,6 +34,7 @@ import {
3634
} from './constants';
3735
import {DowngradeNg2ComponentAdapter} from './downgrade_ng2_adapter';
3836
import {UpgradeNg1ComponentAdapterBuilder} from './upgrade_ng1_adapter';
37+
import * as angular from './angular_js';
3938

4039
var upgradeCount: number = 0;
4140

@@ -295,7 +294,7 @@ export class UpgradeAdapter {
295294
bootstrap(element: Element, modules?: any[],
296295
config?: angular.IAngularBootstrapConfig): UpgradeAdapterRef {
297296
var upgrade = new UpgradeAdapterRef();
298-
var ng1Injector: angular.auto.IInjectorService = null;
297+
var ng1Injector: angular.IInjectorService = null;
299298
var platformRef: PlatformRef = platform();
300299
var applicationRef: ApplicationRef = platformRef.application([
301300
applicationCommonProviders(),
@@ -341,7 +340,7 @@ export class UpgradeAdapter {
341340
.run([
342341
'$injector',
343342
'$rootScope',
344-
(injector: angular.auto.IInjectorService, rootScope: angular.IRootScopeService) => {
343+
(injector: angular.IInjectorService, rootScope: angular.IRootScopeService) => {
345344
ng1Injector = injector;
346345
ngZone.overrideOnTurnDone(() => rootScope.$apply());
347346
ng1compilePromise =
@@ -440,7 +439,7 @@ export class UpgradeAdapter {
440439
public upgradeNg1Provider(name: string, options?: {asToken: any}) {
441440
var token = options && options.asToken || name;
442441
this.providers.push(provide(token, {
443-
useFactory: (ng1Injector: angular.auto.IInjectorService) => ng1Injector.get(name),
442+
useFactory: (ng1Injector: angular.IInjectorService) => ng1Injector.get(name),
444443
deps: [NG1_INJECTOR]
445444
}));
446445
}
@@ -469,7 +468,7 @@ export class UpgradeAdapter {
469468
*/
470469
public downgradeNg2Provider(token: any): Function {
471470
var factory = function(injector: Injector) { return injector.get(token); };
472-
factory.$inject = [NG2_INJECTOR];
471+
(<any>factory).$inject = [NG2_INJECTOR];
473472
return factory;
474473
}
475474

@@ -496,7 +495,7 @@ interface ProtoViewRefMap {
496495
}
497496

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

534533
public ng1RootScope: angular.IRootScopeService = null;
535-
public ng1Injector: angular.auto.IInjectorService = null;
534+
public ng1Injector: angular.IInjectorService = null;
536535
public ng2ApplicationRef: ApplicationRef = null;
537536
public ng2Injector: Injector = null;
538537

539538
/* @internal */
540-
private _bootstrapDone(applicationRef: ApplicationRef,
541-
ng1Injector: angular.auto.IInjectorService) {
539+
private _bootstrapDone(applicationRef: ApplicationRef, ng1Injector: angular.IInjectorService) {
542540
this.ng2ApplicationRef = applicationRef;
543541
this.ng2Injector = applicationRef.injector;
544542
this.ng1Injector = ng1Injector;

modules/upgrade/src/upgrade_ng1_adapter.ts renamed to modules/angular2/src/upgrade/upgrade_ng1_adapter.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
NG1_CONTROLLER
1717
} from './constants';
1818
import {controllerKey} from './util';
19+
import * as angular from './angular_js';
1920

2021
const CAMEL_CASE = /([A-Z])/g;
2122
const INITIAL_VALUE = {
@@ -57,7 +58,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
5758
});
5859
}
5960

60-
extractDirective(injector: angular.auto.IInjectorService): angular.IDirective {
61+
extractDirective(injector: angular.IInjectorService): angular.IDirective {
6162
var directives: angular.IDirective[] = injector.get(this.name + 'Directive');
6263
if (directives.length > 1) {
6364
throw new Error('Only support single directive definition for: ' + this.name);
@@ -141,15 +142,15 @@ export class UpgradeNg1ComponentAdapterBuilder {
141142
throw new Error(`Directive '${this.name}' is not a component, it is missing template.`);
142143
}
143144
return null;
144-
function compileHtml(html) {
145+
function compileHtml(html): angular.ILinkFn {
145146
var div = document.createElement('div');
146147
div.innerHTML = html;
147148
return compile(div.childNodes);
148149
}
149150
}
150151

151152
static resolve(exportedComponents: {[name: string]: UpgradeNg1ComponentAdapterBuilder},
152-
injector: angular.auto.IInjectorService): Promise<any> {
153+
injector: angular.IInjectorService): Promise<any> {
153154
var promises = [];
154155
var compile: angular.ICompileService = injector.get(NG1_COMPILE);
155156
var templateCache: angular.ITemplateCacheService = injector.get(NG1_TEMPLATE_CACHE);
@@ -162,7 +163,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
162163
exportedComponent.$controller = $controller;
163164
exportedComponent.extractBindings();
164165
var promise = exportedComponent.compileTemplate(compile, templateCache, httpBackend);
165-
if (promise) promises.push(promise)
166+
if (promise) promises.push(promise);
166167
}
167168
}
168169
return Promise.all(promises);
@@ -208,7 +209,7 @@ class UpgradeNg1ComponentAdapter implements OnChanges, DoCheck {
208209
for (var i = 0, ii = clonedElement.length; i < ii; i++) {
209210
element.appendChild(clonedElement[i]);
210211
}
211-
}, {parentBoundTranscludeFn: (scope, cloneAttach) => { cloneAttach(childNodes) }});
212+
}, {parentBoundTranscludeFn: (scope, cloneAttach) => { cloneAttach(childNodes); }});
212213

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

226-
onChanges(changes) {
227+
onChanges(changes: {[name: string]: SimpleChange}) {
227228
for (var name in changes) {
228-
if (changes.hasOwnProperty(name)) {
229+
if ((<Object>changes).hasOwnProperty(name)) {
229230
var change: SimpleChange = changes[name];
230231
this.setComponentProperty(name, change.currentValue);
231232
}
232233
}
233234
}
234235

235-
doCheck() {
236+
doCheck(): number {
236237
var count = 0;
237238
var destinationObj = this.destinationObj;
238239
var lastValues = this.checkLastValues;
File renamed without changes.

modules/angular2/test/public_api_spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {SymbolsDiff} from './symbol_inspector/symbol_differ';
2525
// =================================================================================================
2626
// =================================================================================================
2727

28-
var NG_API = [
28+
var NG_ALL = [
2929
'APP_COMPONENT',
3030
'APP_ID',
3131
'AbstractProviderError',
@@ -1450,6 +1450,22 @@ var NG_API = [
14501450
'Stream.where():dart',
14511451
];
14521452

1453+
var NG_UPGRADE = [
1454+
'UpgradeAdapter:js',
1455+
'UpgradeAdapter.addProvider():js',
1456+
'UpgradeAdapter.bootstrap():js',
1457+
'UpgradeAdapter.compileNg2Components():js',
1458+
'UpgradeAdapter.downgradeNg2Component():js',
1459+
'UpgradeAdapter.downgradeNg2Provider():js',
1460+
'UpgradeAdapter.upgradeNg1Component():js',
1461+
'UpgradeAdapter.upgradeNg1Provider():js',
1462+
'UpgradeAdapterRef:js',
1463+
'UpgradeAdapterRef.dispose():js',
1464+
'UpgradeAdapterRef.ready():js'
1465+
];
1466+
1467+
var NG_API = [].concat(NG_ALL).concat(NG_UPGRADE);
1468+
14531469
export function main() {
14541470
/**
14551471
var x = getSymbolsFromLibrary('ng');

modules/upgrade/test/metadata_spec.ts renamed to modules/angular2/test/upgrade/metadata_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import {
1212
} from 'angular2/testing_internal';
1313

1414
import {Component, View} from 'angular2/angular2';
15-
import {getComponentInfo, parseFields} from 'upgrade/src/metadata';
15+
import {getComponentInfo, parseFields} from 'angular2/src/upgrade/metadata';
16+
import {DOM} from 'angular2/src/core/dom/dom_adapter';
1617

1718
export function main() {
19+
if (!DOM.supportsDOMEvents()) return;
1820
describe('upgrade metadata', () => {
1921
it('should extract component selector', () => {
2022
expect(getComponentInfo(ElementNameComponent).selector).toEqual('elementNameDashed');

modules/upgrade/test/integration_spec.ts renamed to modules/angular2/test/upgrade/upgrade_spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import {
1010
xdescribe,
1111
xit,
1212
} from 'angular2/testing_internal';
13+
import {DOM} from 'angular2/src/core/dom/dom_adapter';
1314

1415
import {Component, Class, Inject, EventEmitter, ApplicationRef, provide} from 'angular2/angular2';
15-
import {UpgradeAdapter} from 'upgrade/upgrade';
16+
import {UpgradeAdapter} from 'angular2/upgrade';
17+
import * as angular from 'angular2/src/upgrade/angular_js';
1618

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

modules/angular2/upgrade.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @module
3+
* @description
4+
* Adapter allowing AngularJS v1 and Angular v2 to run side by side in the same application.
5+
*/
6+
export {UpgradeAdapter, UpgradeAdapterRef} from './src/upgrade/upgrade_adapter';

0 commit comments

Comments
 (0)