Skip to content

Commit 0bc5621

Browse files
authored
Merge pull request NativeScript#440 from NativeScript/hdeshev/ng-rc6
Upgrade nativescript-angular to ng2 RC6
2 parents 7bee459 + 79cc361 commit 0bc5621

File tree

84 files changed

+1797
-543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1797
-543
lines changed

nativescript-angular/animation-driver.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { AnimationKeyframe } from '@angular/core/src/animation/animation_keyframe';
2-
import { AnimationPlayer } from '@angular/core/src/animation/animation_player';
3-
import { AnimationStyles } from '@angular/core/src/animation/animation_styles';
4-
import { AnimationDriver } from '@angular/platform-browser/src/dom/animation_driver';
1+
import { AnimationPlayer, AnimationStyles, AnimationKeyframe } from "./private_import_core";
52
import { NativeScriptAnimationPlayer } from './animation-player';
63
import {View} from "ui/core/view";
74
import styleProperty = require('ui/styling/style-property');
85

6+
export abstract class AnimationDriver {
7+
abstract animate(
8+
element: any, startingStyles: AnimationStyles, keyframes: AnimationKeyframe[],
9+
duration: number, delay: number, easing: string): AnimationPlayer;
10+
}
11+
912
export class NativeScriptAnimationDriver implements AnimationDriver {
1013

1114
computeStyle(element: any, prop: string): string {

nativescript-angular/animation-player.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { AnimationKeyframe } from '@angular/core/src/animation/animation_keyframe';
2-
import { AnimationPlayer } from '@angular/core/src/animation/animation_player';
1+
import { AnimationPlayer, AnimationKeyframe } from "./private_import_core";
32
import { KeyframeAnimation, KeyframeAnimationInfo, KeyframeInfo, KeyframeDeclaration } from 'ui/animation/keyframe-animation';
43
import { View } from "ui/core/view";
54
import enums = require("ui/enums");
@@ -11,7 +10,8 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer {
1110

1211
public parentPlayer: AnimationPlayer;
1312

14-
private _subscriptions: Function[] = [];
13+
private _startSubscriptions: Function[] = [];
14+
private _doneSubscriptions: Function[] = [];
1515
private _finished = false;
1616
private _started = false;
1717
private animation: KeyframeAnimation;
@@ -72,20 +72,29 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer {
7272
}
7373

7474

75-
onDone(fn: Function): void { this._subscriptions.push(fn); }
75+
onStart(fn: Function): void { this._startSubscriptions.push(fn); }
76+
onDone(fn: Function): void { this._doneSubscriptions.push(fn); }
77+
78+
private _onStart() {
79+
if (!this._started) {
80+
this._started = true;
81+
this._startSubscriptions.forEach(fn => fn());
82+
this._startSubscriptions = [];
83+
}
84+
}
7685

7786
private _onFinish() {
7887
if (!this._finished) {
7988
this._finished = true;
8089
this._started = false;
81-
this._subscriptions.forEach(fn => fn());
82-
this._subscriptions = [];
90+
this._doneSubscriptions.forEach(fn => fn());
91+
this._doneSubscriptions = [];
8392
}
8493
}
8594

8695
play(): void {
8796
if (this.animation) {
88-
this._started = true;
97+
this._onStart();
8998
this.animation.play(this.target)
9099
.then(() => { this._onFinish(); })
91100
.catch((e) => { });
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
//Copied unexported functions from @angular/core/src/facade/collection
2+
import {
3+
isJsObject, isArray, getSymbolIterator,
4+
isPresent, isBlank
5+
} from "./lang-facade";
6+
7+
export function isListLikeIterable(obj: any): boolean {
8+
if (!isJsObject(obj)) return false;
9+
return isArray(obj) ||
10+
(!(obj instanceof Map) && // JS Map are iterables but return entries as [k, v]
11+
getSymbolIterator() in obj); // JS Iterable have a Symbol.iterator prop
12+
}
13+
14+
export class ListWrapper {
15+
// JS has no way to express a statically fixed size list, but dart does so we
16+
// keep both methods.
17+
static createFixedSize(size: number): any[] { return new Array(size); }
18+
static createGrowableSize(size: number): any[] { return new Array(size); }
19+
static clone<T>(array: T[]): T[] { return array.slice(0); }
20+
static forEachWithIndex<T>(array: T[], fn: (t: T, n: number) => void) {
21+
for (var i = 0; i < array.length; i++) {
22+
fn(array[i], i);
23+
}
24+
}
25+
static first<T>(array: T[]): T {
26+
if (!array) return null;
27+
return array[0];
28+
}
29+
static last<T>(array: T[]): T {
30+
if (!array || array.length == 0) return null;
31+
return array[array.length - 1];
32+
}
33+
static indexOf<T>(array: T[], value: T, startIndex: number = 0): number {
34+
return array.indexOf(value, startIndex);
35+
}
36+
static contains<T>(list: T[], el: T): boolean { return list.indexOf(el) !== -1; }
37+
static reversed<T>(array: T[]): T[] {
38+
var a = ListWrapper.clone(array);
39+
return a.reverse();
40+
}
41+
static concat(a: any[], b: any[]): any[] { return a.concat(b); }
42+
static insert<T>(list: T[], index: number, value: T) { list.splice(index, 0, value); }
43+
static removeAt<T>(list: T[], index: number): T {
44+
var res = list[index];
45+
list.splice(index, 1);
46+
return res;
47+
}
48+
static removeAll<T>(list: T[], items: T[]) {
49+
for (var i = 0; i < items.length; ++i) {
50+
var index = list.indexOf(items[i]);
51+
list.splice(index, 1);
52+
}
53+
}
54+
static remove<T>(list: T[], el: T): boolean {
55+
var index = list.indexOf(el);
56+
if (index > -1) {
57+
list.splice(index, 1);
58+
return true;
59+
}
60+
return false;
61+
}
62+
static clear(list: any[]) { list.length = 0; }
63+
static isEmpty(list: any[]): boolean { return list.length == 0; }
64+
static fill(list: any[], value: any, start: number = 0, end: number = null) {
65+
(<any>list).fill(value, start, end === null ? list.length : end);
66+
}
67+
static equals(a: any[], b: any[]): boolean {
68+
if (a.length != b.length) return false;
69+
for (var i = 0; i < a.length; ++i) {
70+
if (a[i] !== b[i]) return false;
71+
}
72+
return true;
73+
}
74+
static slice<T>(l: T[], from: number = 0, to: number = null): T[] {
75+
return l.slice(from, to === null ? undefined : to);
76+
}
77+
static splice<T>(l: T[], from: number, length: number): T[] { return l.splice(from, length); }
78+
static sort<T>(l: T[], compareFn?: (a: T, b: T) => number) {
79+
if (isPresent(compareFn)) {
80+
l.sort(compareFn);
81+
} else {
82+
l.sort();
83+
}
84+
}
85+
static toString<T>(l: T[]): string { return l.toString(); }
86+
static toJSON<T>(l: T[]): string { return JSON.stringify(l); }
87+
88+
static maximum<T>(list: T[], predicate: (t: T) => number): T {
89+
if (list.length == 0) {
90+
return null;
91+
}
92+
var solution: any /** TODO #???? */ = null;
93+
var maxValue = -Infinity;
94+
for (var index = 0; index < list.length; index++) {
95+
var candidate = list[index];
96+
if (isBlank(candidate)) {
97+
continue;
98+
}
99+
var candidateValue = predicate(candidate);
100+
if (candidateValue > maxValue) {
101+
solution = candidate;
102+
maxValue = candidateValue;
103+
}
104+
}
105+
return solution;
106+
}
107+
108+
static flatten<T>(list: Array<T|T[]>): T[] {
109+
var target: any[] = [];
110+
_flattenArray(list, target);
111+
return target;
112+
}
113+
114+
static addAll<T>(list: Array<T>, source: Array<T>): void {
115+
for (var i = 0; i < source.length; i++) {
116+
list.push(source[i]);
117+
}
118+
}
119+
}
120+
121+
function _flattenArray(source: any[], target: any[]): any[] {
122+
if (isPresent(source)) {
123+
for (var i = 0; i < source.length; i++) {
124+
var item = source[i];
125+
if (isArray(item)) {
126+
_flattenArray(item, target);
127+
} else {
128+
target.push(item);
129+
}
130+
}
131+
}
132+
return target;
133+
}
134+
135+
export class StringMapWrapper {
136+
static create(): {[k: /*any*/ string]: any} {
137+
// Note: We are not using Object.create(null) here due to
138+
// performance!
139+
// http://jsperf.com/ng2-object-create-null
140+
return {};
141+
}
142+
static contains(map: {[key: string]: any}, key: string): boolean {
143+
return map.hasOwnProperty(key);
144+
}
145+
static get<V>(map: {[key: string]: V}, key: string): V {
146+
return map.hasOwnProperty(key) ? map[key] : undefined;
147+
}
148+
static set<V>(map: {[key: string]: V}, key: string, value: V) { map[key] = value; }
149+
150+
static keys(map: {[key: string]: any}): string[] { return Object.keys(map); }
151+
static values<T>(map: {[key: string]: T}): T[] {
152+
return Object.keys(map).map((k: string): T => map[k]);
153+
}
154+
static isEmpty(map: {[key: string]: any}): boolean {
155+
for (var prop in map) {
156+
return false;
157+
}
158+
return true;
159+
}
160+
static delete (map: {[key: string]: any}, key: string) { delete map[key]; }
161+
static forEach<K, V>(map: {[key: string]: V}, callback: (v: V, K: string) => void) {
162+
for (let k of Object.keys(map)) {
163+
callback(map[k], k);
164+
}
165+
}
166+
167+
static merge<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): {[key: string]: V} {
168+
var m: {[key: string]: V} = {};
169+
170+
for (let k of Object.keys(m1)) {
171+
m[k] = m1[k];
172+
}
173+
174+
for (let k of Object.keys(m2)) {
175+
m[k] = m2[k];
176+
}
177+
178+
return m;
179+
}
180+
181+
static equals<V>(m1: {[key: string]: V}, m2: {[key: string]: V}): boolean {
182+
var k1 = Object.keys(m1);
183+
var k2 = Object.keys(m2);
184+
if (k1.length != k2.length) {
185+
return false;
186+
}
187+
var key: any /** TODO #???? */;
188+
for (var i = 0; i < k1.length; i++) {
189+
key = k1[i];
190+
if (m1[key] !== m2[key]) {
191+
return false;
192+
}
193+
}
194+
return true;
195+
}
196+
}

nativescript-angular/common/detached-loader.ts

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import {ComponentRef, ComponentFactory, ViewContainerRef, Component,
2-
Type, ViewChild, ComponentResolver, ChangeDetectorRef, Host} from '@angular/core';
1+
import {
2+
ComponentRef, ComponentFactory, ViewContainerRef,
3+
Component, Type, ViewChild, Compiler,
4+
ComponentFactoryResolver, ChangeDetectorRef, Host
5+
} from '@angular/core';
36
import trace = require("trace");
47

58
type AnyComponentRef = ComponentRef<any>;
69
interface PendingLoadEntry {
7-
componentType: Type;
10+
componentType: Type<any>;
811
resolveCallback: (AnyComponentRef) => void;
912
}
1013

@@ -23,58 +26,26 @@ function log(message: string) {
2326
template: `<Placeholder #loader></Placeholder>`
2427
})
2528
export class DetachedLoader {
26-
@ViewChild('loader', { read: ViewContainerRef }) childContainerRef: ViewContainerRef;
29+
constructor(private resolver: ComponentFactoryResolver, private changeDetector: ChangeDetectorRef, private containerRef: ViewContainerRef) { }
2730

28-
private viewLoaded = false;
29-
private pendingLoads: PendingLoadEntry[] = [];
31+
private loadInLocation(componentType: Type<any>): Promise<ComponentRef<any>> {
32+
const factory = this.resolver.resolveComponentFactory(componentType)
33+
const componentRef = this.containerRef.createComponent(
34+
factory, this.containerRef.length, this.containerRef.parentInjector);
3035

31-
constructor(private compiler: ComponentResolver, private changeDetector: ChangeDetectorRef, private containerRef: ViewContainerRef) { }
36+
// Component is created, buit may not be checked if we are loading
37+
// inside component with OnPush CD strategy. Mark us for check to be sure CD will reach us.
38+
// We are inside a promise here so no need for setTimeout - CD should trigger after the promise.
39+
log("DetachedLoader.loadInLocation component loaded -> markForCheck");
40+
this.changeDetector.markForCheck();
3241

33-
public ngAfterViewInit() {
34-
log("DetachedLoader.ngAfterViewInit");
35-
36-
this.viewLoaded = true;
37-
this.pendingLoads.forEach(loadEntry => {
38-
this.loadInLocation(loadEntry.componentType).then(loadedRef => {
39-
loadEntry.resolveCallback(loadedRef);
40-
});
41-
});
42-
}
43-
44-
private loadInLocation(componentType: Type): Promise<ComponentRef<any>> {
45-
return this.compiler.resolveComponent(componentType).then((componentFactory) => {
46-
return this.childContainerRef.createComponent(componentFactory, this.childContainerRef.length, this.childContainerRef.parentInjector, null);
47-
}).then((compRef) => {
48-
log("DetachedLoader.loadInLocation component loaded -> markForCheck");
49-
// Component is created, buit may not be checked if we are loading
50-
// inside component with OnPush CD strategy. Mark us for check to be sure CD will reach us.
51-
// We are inside a promise here so no need for setTimeout - CD should trigger after the promise.
52-
this.changeDetector.markForCheck();
53-
return compRef;
54-
})
42+
return Promise.resolve(componentRef);
5543
}
5644

57-
public loadComponent(componentType: Type): Promise<ComponentRef<any>> {
58-
log("DetachedLoader.loadComponent viewLoaded: " + this.viewLoaded);
59-
60-
// Check if called before placeholder is initialized.
61-
// Delay load if so.
62-
if (this.viewLoaded) {
63-
return this.loadInLocation(componentType);
64-
} else {
65-
// loadComponent called, but detached-loader is still not initialized.
66-
// Mark it for change and trigger change detection to be sure it will be initialized,
67-
// so that loading can conitionue.
68-
log("DetachedLoader.loadComponent -> markForCheck(with setTimeout())")
69-
setTimeout(() => this.changeDetector.markForCheck(), 0);
70-
71-
return new Promise((resolve, reject) => {
72-
this.pendingLoads.push({
73-
componentType: componentType,
74-
resolveCallback: resolve
75-
});
76-
});
77-
}
45+
//TODO: change this API -- async promises not needed here anymore.
46+
public loadComponent(componentType: Type<any>): Promise<ComponentRef<any>> {
47+
log("DetachedLoader.loadComponent");
48+
return this.loadInLocation(componentType);
7849
}
7950

8051
public loadWithFactory<T>(factory: ComponentFactory<T>): ComponentRef<T> {

nativescript-angular/common/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isBlank, isNumber} from '@angular/core/src/facade/lang';
1+
import {isBlank, isNumber} from "../lang-facade";
22

33
export function convertToInt(value): number {
44
let normalizedValue;
@@ -13,4 +13,4 @@ export function convertToInt(value): number {
1313
}
1414
}
1515
return Math.round(normalizedValue);
16-
}
16+
}

nativescript-angular/directives.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import {Type} from '@angular/core/src/facade/lang';
21
import {ListViewComponent, SetupItemViewArgs} from './directives/list-view-comp';
32
import {TabViewDirective, TabViewItemDirective} from './directives/tab-view';
43
import {ActionBarComponent, ActionBarScope, ActionItemDirective, NavigationButtonDirective} from './directives/action-bar';
54
import {AndroidFilterComponent, IosFilterComponent} from './directives/platform-filters';
65

7-
8-
export const NS_DIRECTIVES: Type[] = [
6+
export const NS_DIRECTIVES = [
97
ListViewComponent,
108
TabViewDirective,
119
TabViewItemDirective,
@@ -14,7 +12,7 @@ export const NS_DIRECTIVES: Type[] = [
1412
ActionItemDirective,
1513
NavigationButtonDirective,
1614
AndroidFilterComponent,
17-
IosFilterComponent
15+
IosFilterComponent,
1816
];
1917

2018
export {ListViewComponent, SetupItemViewArgs} from './directives/list-view-comp';

0 commit comments

Comments
 (0)