forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe-common.ts
More file actions
405 lines (321 loc) · 11.8 KB
/
frame-common.ts
File metadata and controls
405 lines (321 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import definition = require("ui/frame");
import view = require("ui/core/view");
import pages = require("ui/page");
import types = require("utils/types");
import trace = require("trace");
import builder = require("ui/builder");
import fs = require("file-system");
import utils = require("utils/utils");
import platform = require("platform");
import fileResolverModule = require("file-system/file-name-resolver");
var frameStack: Array<Frame> = [];
function buildEntryFromArgs(arg: any): definition.NavigationEntry {
var entry: definition.NavigationEntry;
if (arg instanceof pages.Page) {
throw new Error("Navigating to a Page instance is no longer supported. Please navigate by using either a module name or a page factory function.");
} else if (types.isString(arg)) {
entry = {
moduleName: arg
};
} else if (types.isFunction(arg)) {
entry = {
create: arg
}
} else {
entry = arg;
}
return entry;
}
function resolvePageFromEntry(entry: definition.NavigationEntry): pages.Page {
var page: pages.Page;
if (entry.create) {
page = entry.create();
if (!(page && page instanceof pages.Page)) {
throw new Error("Failed to create Page with entry.create() function.");
}
}
else if (entry.moduleName) {
// Current app full path.
var currentAppPath = fs.knownFolders.currentApp().path;
//Full path of the module = current app full path + module name.
var moduleNamePath = fs.path.join(currentAppPath, entry.moduleName);
var moduleExports;
if (fs.File.exists(moduleNamePath + ".js")) {
trace.write("Loading JS file: " + moduleNamePath + ".js", trace.categories.Navigation);
moduleExports = require(moduleNamePath);
}
if (moduleExports && moduleExports.createPage) {
trace.write("Calling createPage()", trace.categories.Navigation);
page = moduleExports.createPage();
}
else {
page = pageFromBuilder(moduleNamePath, moduleExports);
}
if (!(page && page instanceof pages.Page)) {
throw new Error("Failed to load Page from entry.moduleName: " + entry.moduleName);
}
}
return page;
}
var fileNameResolver: fileResolverModule.FileNameResolver;
function resolveFilePath(path, ext) {
if (!fileNameResolver) {
fileNameResolver = new fileResolverModule.FileNameResolver({
width: platform.screen.mainScreen.widthDIPs,
height: platform.screen.mainScreen.heightDIPs,
os: platform.device.os,
deviceType: platform.device.deviceType
});
}
return fileNameResolver.resolveFileName(path, ext);
}
function pageFromBuilder(moduleNamePath: string, moduleExports: any): pages.Page {
var page: pages.Page;
var element: view.View;
// Possible XML file path.
var fileName = resolveFilePath(moduleNamePath, "xml");
if (fileName) {
trace.write("Loading XML file: " + fileName, trace.categories.Navigation);
// Or check if the file exists in the app modules and load the page from XML.
element = builder.load(fileName, moduleExports);
if (element instanceof pages.Page) {
page = <pages.Page>element;
// Possible CSS file path.
var cssFileName = resolveFilePath(moduleNamePath, "css");
if (cssFileName) {
page.addCssFile(cssFileName);
}
}
}
return page;
}
export module knownEvents {
export module android {
export var optionSelected = "optionSelected";
}
}
interface NavigationContext {
entry: definition.BackstackEntry;
isBackNavigation: boolean;
}
export class Frame extends view.CustomLayoutView implements definition.Frame {
private _navigationQueue: Array<NavigationContext>;
private _backStack: Array<definition.BackstackEntry>;
public _currentEntry: definition.BackstackEntry;
private _animated: boolean;
public _isInFrameStack = false;
public static defaultAnimatedNavigation = true;
// TODO: Currently our navigation will not be synchronized in case users directly call native navigation methods like Activity.startActivity.
constructor() {
super();
this._backStack = new Array<definition.BackstackEntry>();
this._navigationQueue = new Array<NavigationContext>();
}
public canGoBack(): boolean {
return this._backStack.length > 0;
}
public goBack() {
trace.write(this._getTraceId() + ".goBack();", trace.categories.Navigation);
if (!this.canGoBack()) {
// TODO: Do we need to throw an error?
return;
}
var backstackEntry = this._backStack.pop();
var navigationContext: NavigationContext = {
entry: backstackEntry,
isBackNavigation: true
}
this._navigationQueue.push(navigationContext);
if (this._navigationQueue.length === 1) {
this._processNavigationContext(navigationContext);
}
else {
trace.write(this._getTraceId() + ".goBack scheduled;", trace.categories.Navigation);
}
}
public navigate(param: any) {
trace.write(this._getTraceId() + ".navigate();", trace.categories.Navigation);
var entry = buildEntryFromArgs(param);
var page = resolvePageFromEntry(entry);
this._pushInFrameStack();
var backstackEntry: definition.BackstackEntry = {
entry: entry,
resolvedPage: page,
};
var navigationContext: NavigationContext = {
entry: backstackEntry,
isBackNavigation: false
}
this._navigationQueue.push(navigationContext);
if (this._navigationQueue.length === 1) {
this._processNavigationContext(navigationContext);
}
else {
trace.write(this._getTraceId() + ".navigation scheduled;", trace.categories.Navigation);
}
}
public _processNavigationQueue(page: pages.Page) {
if (this._navigationQueue.length === 0) {
// This could happen when showing recreated page after activity has been destroyed.
return;
}
var entry = this._navigationQueue[0].entry;
var currentNavigationPage = entry.resolvedPage;
if (page !== currentNavigationPage) {
throw new Error("Corrupted navigation stack.");
}
// remove completed operation.
this._navigationQueue.shift();
if (this._navigationQueue.length > 0) {
var navigationContext = this._navigationQueue[0];
this._processNavigationContext(navigationContext);
}
}
private _processNavigationContext(navigationContext: NavigationContext) {
if (navigationContext.isBackNavigation) {
this.performGoBack(navigationContext);
}
else {
this.performNavigation(navigationContext);
}
}
private performNavigation(navigationContext: NavigationContext) {
var navContext = navigationContext.entry;
this._onNavigatingTo(navContext);
if (this.currentPage) {
this._backStack.push(this._currentEntry);
}
this._navigateCore(navContext);
this._onNavigatedTo(navContext, false);
}
private performGoBack(navigationContext: NavigationContext) {
var navContext = navigationContext.entry;
this._onNavigatingTo(navContext);
this._goBackCore(navContext);
this._onNavigatedTo(navContext, true);
}
public _goBackCore(backstackEntry: definition.BackstackEntry) {
//
}
public _navigateCore(backstackEntry: definition.BackstackEntry) {
//
}
public _onNavigatingTo(backstackEntry: definition.BackstackEntry) {
if (this.currentPage) {
this.currentPage.onNavigatingFrom();
}
backstackEntry.resolvedPage.onNavigatingTo(backstackEntry.entry.context);
}
public _onNavigatedTo(backstackEntry: definition.BackstackEntry, isBack: boolean) {
if (this.currentPage) {
this.currentPage.onNavigatedFrom(isBack);
}
}
public get animated(): boolean {
return this._animated;
}
public set animated(value: boolean) {
this._animated = value;
}
get backStack(): Array<definition.BackstackEntry> {
return this._backStack.slice();
}
get currentPage(): pages.Page {
if (this._currentEntry) {
return this._currentEntry.resolvedPage;
}
return null;
}
get currentEntry(): definition.BackstackEntry {
return this._currentEntry;
}
public _pushInFrameStack() {
if (this._isInFrameStack) {
return;
}
frameStack.push(this);
this._isInFrameStack = true;
}
public _popFromFrameStack() {
if (!this._isInFrameStack) {
return;
}
var top = _topmost();
if (top !== this) {
throw new Error("Cannot pop a Frame which is not at the top of the navigation stack.");
}
frameStack.pop();
this._isInFrameStack = false;
}
get _childrenCount(): number {
if (this.currentPage) {
return 1;
}
return 0;
}
public _eachChildView(callback: (child: view.View) => boolean) {
if (this.currentPage) {
callback(this.currentPage);
}
}
public _getIsAnimatedNavigation(entry: definition.NavigationEntry) {
if (entry && types.isDefined(entry.animated)) {
return entry.animated;
}
if (types.isDefined(this.animated)) {
return this.animated;
}
return Frame.defaultAnimatedNavigation;
}
private _getTraceId(): string {
return "Frame<" + this._domId + ">";
}
protected get navigationBarHeight(): number {
return 0;
}
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
var width = utils.layout.getMeasureSpecSize(widthMeasureSpec);
var widthMode = utils.layout.getMeasureSpecMode(widthMeasureSpec);
var height = utils.layout.getMeasureSpecSize(heightMeasureSpec);
var heightMode = utils.layout.getMeasureSpecMode(heightMeasureSpec);
var result = view.View.measureChild(this, this.currentPage, widthMeasureSpec, utils.layout.makeMeasureSpec(height - this.navigationBarHeight, heightMode));
var widthAndState = view.View.resolveSizeAndState(result.measuredWidth, width, widthMode, 0);
var heightAndState = view.View.resolveSizeAndState(result.measuredHeight, height, heightMode, 0);
this.setMeasuredDimension(widthAndState, heightAndState);
}
public onLayout(left: number, top: number, right: number, bottom: number): void {
view.View.layoutChild(this, this.currentPage, 0, this.navigationBarHeight, right - left, bottom - top);
}
// We don't need to put Page as visual child. Don't call super.
public _addViewToNativeVisualTree(child: view.View): boolean {
return true;
}
// We don't need to put Page as visual child. Don't call super.
public _removeViewFromNativeVisualTree(child: view.View): void {
child._isAddedToNativeVisualTree = false;
}
public _invalidateOptionsMenu() {
//
}
}
var _topmost = function (): Frame {
if (frameStack.length > 0) {
return frameStack[frameStack.length - 1];
}
return undefined;
}
export var topmost = _topmost;
export function goBack(): boolean {
var top = _topmost();
if (top.canGoBack()) {
top.goBack();
return true;
}
if (frameStack.length > 1) {
top._popFromFrameStack();
}
return false;
}
export function stack(): Array<definition.Frame> {
return frameStack;
}