• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/// <reference path='./import.ts' />
17const TITLE_MODE_RANGE = 2;
18const NAV_BAR_POSITION_RANGE = 1;
19const NAVIGATION_MODE_RANGE = 2;
20const DEFAULT_NAV_BAR_WIDTH = 240;
21const MIN_NAV_BAR_WIDTH_DEFAULT = '240vp';
22const MAX_NAV_BAR_WIDTH_DEFAULT = '40%';
23const NAVIGATION_TITLE_MODE_DEFAULT = 0;
24const DEFAULT_UNIT = 'vp';
25const NAV_SAFE_AREA_TYPE_LIMIT = 3;
26const NAV_SAFE_AREA_EDGE_LIMIT = 4;
27const NAV_SAFE_AREA_LOWER_LIMIT = 0;
28
29class ArkNavigationComponent extends ArkComponent implements NavigationAttribute {
30  constructor(nativePtr: KNode, classType?: ModifierType) {
31    super(nativePtr, classType);
32  }
33  navBarWidth(value: Length): NavigationAttribute {
34    modifierWithKey(this._modifiersWithKeys, NavBarWidthModifier.identity, NavBarWidthModifier, value);
35    return this;
36  }
37  navBarPosition(value: number): NavigationAttribute {
38    modifierWithKey(this._modifiersWithKeys, NavBarPositionModifier.identity, NavBarPositionModifier, value);
39    return this;
40  }
41  navBarWidthRange(value: [Dimension, Dimension]): NavigationAttribute {
42    modifierWithKey(this._modifiersWithKeys, NavBarWidthRangeModifier.identity, NavBarWidthRangeModifier, value);
43    return this;
44  }
45  minContentWidth(value: Dimension): NavigationAttribute {
46    modifierWithKey(this._modifiersWithKeys, MinContentWidthModifier.identity, MinContentWidthModifier, value);
47
48    return this;
49  }
50  mode(value: number): NavigationAttribute {
51    modifierWithKey(this._modifiersWithKeys, ModeModifier.identity, ModeModifier, value);
52    return this;
53  }
54  backButtonIcon(value: any): NavigationAttribute {
55    modifierWithKey(this._modifiersWithKeys, BackButtonIconModifier.identity, BackButtonIconModifier, value);
56    return this;
57  }
58  hideNavBar(value: boolean): NavigationAttribute {
59    modifierWithKey(this._modifiersWithKeys, HideNavBarModifier.identity, HideNavBarModifier, value);
60    return this;
61  }
62  title(value: ResourceStr | CustomBuilder | NavigationCommonTitle | NavigationCustomTitle | undefined,
63    options?: NavigationTitleOptions): NavigationAttribute {
64    if (isUndefined(value) || isNull(value)) {
65      modifierWithKey(this._modifiersWithKeys, TitleModifier.identity,
66        TitleModifier, undefined);
67      return this;
68    }
69    let arkNavigationTitle = new ArkNavigationTitle();
70    arkNavigationTitle.value = value;
71    if (!isUndefined(options) && !isNull(options) && isObject(options)) {
72      if (Object.keys(options).length !== 0) {
73        arkNavigationTitle.navigationTitleOptions = options;
74      }
75    }
76    modifierWithKey(this._modifiersWithKeys, TitleModifier.identity,
77      TitleModifier, arkNavigationTitle);
78    return this;
79  }
80  subTitle(value: string): NavigationAttribute {
81    modifierWithKey(this._modifiersWithKeys, SubTitleModifier.identity, SubTitleModifier, value);
82    return this;
83  }
84  enableModeChangeAnimation(value: boolean): NavigationAttribute {
85    modifierWithKey(this._modifiersWithKeys, EnableModeChangeAnimationModifier.identity, EnableModeChangeAnimationModifier, value);
86    return this;
87  }
88  hideTitleBar(isHide: boolean, animated?: boolean): NavigationAttribute {
89    let arkNavigationHideTitleBar = new ArkNavHideTitleBarOrToolBar();
90    if (!isUndefined(isHide) && !isNull(isHide)) {
91      arkNavigationHideTitleBar.isHide = isHide;
92    }
93    if (!isUndefined(animated) && !isNull(animated)) {
94      arkNavigationHideTitleBar.animated = animated;
95    }
96    if (arkNavigationHideTitleBar.isHide === undefined && arkNavigationHideTitleBar.animated === undefined) {
97        modifierWithKey(this._modifiersWithKeys, NavigationHideTitleBarModifier.identity, NavigationHideTitleBarModifier, undefined);
98    } else {
99        modifierWithKey(this._modifiersWithKeys, NavigationHideTitleBarModifier.identity, NavigationHideTitleBarModifier, arkNavigationHideTitleBar);
100    }
101    return this;
102  }
103  hideBackButton(value: boolean): NavigationAttribute {
104    modifierWithKey(this._modifiersWithKeys, HideBackButtonModifier.identity, HideBackButtonModifier, value);
105    return this;
106  }
107  titleMode(value: NavigationTitleMode): NavigationAttribute {
108    modifierWithKey(this._modifiersWithKeys, TitleModeModifier.identity, TitleModeModifier, value);
109    return this;
110  }
111  menus(value: Array<NavigationMenuItem> | undefined): NavigationAttribute {
112    if (isUndefined(value)) {
113      modifierWithKey(this._modifiersWithKeys, MenusModifier.identity, MenusModifier, undefined);
114      return this;
115    }
116    modifierWithKey(this._modifiersWithKeys, MenusModifier.identity, MenusModifier, value);
117    return this;
118  }
119  toolBar(value: any): NavigationAttribute {
120    throw new Error('Method not implemented.');
121  }
122  toolbarConfiguration(value: any): NavigationAttribute {
123    throw new Error('Method not implemented.');
124  }
125  hideToolBar(isHide: boolean, animated?: boolean): NavigationAttribute {
126    let arkNavigationHideToolBar = new ArkNavHideTitleBarOrToolBar();
127    if (!isUndefined(isHide) && !isNull(isHide)) {
128      arkNavigationHideToolBar.isHide = isHide;
129    }
130    if (!isUndefined(animated) && !isNull(animated)) {
131      arkNavigationHideToolBar.animated = animated;
132    }
133    if (arkNavigationHideToolBar.isHide === undefined && arkNavigationHideToolBar.animated === undefined) {
134        modifierWithKey(this._modifiersWithKeys, HideToolBarModifier.identity, HideToolBarModifier, undefined);
135    } else {
136        modifierWithKey(this._modifiersWithKeys, HideToolBarModifier.identity, HideToolBarModifier, arkNavigationHideToolBar);
137    }
138    return this;
139  }
140  onTitleModeChange(callback: (titleMode: NavigationTitleMode) => void): NavigationAttribute {
141    throw new Error('Method not implemented.');
142  }
143  onNavBarStateChange(callback: (isVisible: boolean) => void): NavigationAttribute {
144    throw new Error('Method not implemented.');
145  }
146  onNavigationModeChange(callback: (mode: NavigationMode) => void): NavigationAttribute {
147    throw new Error('Method not implemented.');
148  }
149  navDestination(builder: (name: string, param: unknown) => void): NavigationAttribute {
150    throw new Error('Method not implemented.');
151  }
152  ignoreLayoutSafeArea(types?: Array<SafeAreaType>, edges?: Array<SafeAreaEdge>): NavigationAttribute {
153    let opts = new ArkSafeAreaExpandOpts();
154    if (types && types.length >= 0) {
155      let safeAreaType: string | number = '';
156      for (let param of types) {
157        if (!isNumber(param) || param >= NAV_SAFE_AREA_TYPE_LIMIT || param < NAV_SAFE_AREA_LOWER_LIMIT) {
158          safeAreaType = undefined;
159          break;
160        }
161        if (safeAreaType) {
162          safeAreaType += '|';
163          safeAreaType += param.toString();
164        } else {
165          safeAreaType += param.toString();
166        }
167      }
168      opts.type = safeAreaType;
169    }
170    if (edges && edges.length >= 0) {
171      let safeAreaEdge: string | number = '';
172      for (let param of edges) {
173        if (!isNumber(param) || param >= NAV_SAFE_AREA_EDGE_LIMIT || param < NAV_SAFE_AREA_LOWER_LIMIT) {
174          safeAreaEdge = undefined;
175          break;
176        }
177        if (safeAreaEdge) {
178          safeAreaEdge += '|';
179          safeAreaEdge += param.toString();
180        } else {
181          safeAreaEdge += param.toString();
182        }
183      }
184      opts.edges = safeAreaEdge;
185    }
186    if (opts.type === undefined && opts.edges === undefined) {
187      modifierWithKey(this._modifiersWithKeys, IgnoreNavLayoutSafeAreaModifier.identity, IgnoreNavLayoutSafeAreaModifier, undefined);
188    } else {
189      modifierWithKey(this._modifiersWithKeys, IgnoreNavLayoutSafeAreaModifier.identity, IgnoreNavLayoutSafeAreaModifier, opts);
190    }
191    return this;
192  }
193  recoverable(value: boolean | undefined): NavigationAttribute {
194    modifierWithKey(this._modifiersWithKeys, NavigationRecoverableModifier.identity, NavigationRecoverableModifier, value);
195    return this;
196  }
197
198  enableDargBar(value: boolean | undefined): NavigationAttribute {
199    modifierWithKey(this._modifiersWithKeys, NavigationEnableDragBarModifier.identity, NavigationEnableDragBarModifier, value);
200    return this;
201  }
202
203  enableToolBarAdaptation(value: boolean | undefined): NavigationAttribute {
204    modifierWithKey(this._modifiersWithKeys, NavigationEnableToolBarAdaptationModifier.identity, NavigationEnableToolBarAdaptationModifier, value);
205    return this;
206  }
207}
208
209class BackButtonIconModifier extends ModifierWithKey<boolean | object> {
210  constructor(value: boolean | object) {
211    super(value);
212  }
213  static identity: Symbol = Symbol('backButtonIcon');
214  applyPeer(node: KNode, reset: boolean): void {
215    if (reset) {
216      getUINativeModule().navigation.resetBackButtonIcon(node);
217    } else {
218      getUINativeModule().navigation.setBackButtonIcon(node, this.value);
219    }
220  }
221
222  checkObjectDiff(): boolean {
223    return !isBaseOrResourceEqual(this.stageValue, this.value);
224  }
225}
226
227class NavBarWidthRangeModifier extends ModifierWithKey<[Dimension, Dimension]> {
228  constructor(value: [Dimension, Dimension]) {
229    super(value);
230  }
231  static identity: Symbol = Symbol('navBarWidthRange');
232  applyPeer(node: KNode, reset: boolean): void {
233    if (reset) {
234      getUINativeModule().navigation.resetNavBarWidthRange(node);
235    } else {
236      getUINativeModule().navigation.setNavBarWidthRange(node, this.value);
237    }
238  }
239
240  checkObjectDiff(): boolean {
241    return !isBaseOrResourceEqual(this.stageValue, this.value);
242  }
243}
244
245class MinContentWidthModifier extends ModifierWithKey<Dimension> {
246  constructor(value: Dimension) {
247    super(value);
248  }
249  static identity: Symbol = Symbol('minContentWidth');
250
251  applyPeer(node: KNode, reset: boolean): void {
252    if (reset) {
253      getUINativeModule().navigation.resetMinContentWidth(node);
254    } else {
255      getUINativeModule().navigation.setMinContentWidth(node, this.value);
256    }
257  }
258
259  checkObjectDiff(): boolean {
260    return !isBaseOrResourceEqual(this.stageValue, this.value);
261  }
262}
263
264class NavBarWidthModifier extends ModifierWithKey<Length> {
265  constructor(value: Length) {
266    super(value);
267  }
268  static identity: Symbol = Symbol('navBarWidth');
269
270  applyPeer(node: KNode, reset: boolean): void {
271    if (reset) {
272      getUINativeModule().navigation.resetNavBarWidth(node);
273    } else {
274      getUINativeModule().navigation.setNavBarWidth(node, this.value);
275    }
276  }
277
278  checkObjectDiff(): boolean {
279    return !isBaseOrResourceEqual(this.stageValue, this.value);
280  }
281}
282
283class NavBarPositionModifier extends ModifierWithKey<number> {
284  constructor(value: number) {
285    super(value);
286  }
287  static identity: Symbol = Symbol('navBarPosition');
288
289  applyPeer(node: KNode, reset: boolean): void {
290    if (reset) {
291      getUINativeModule().navigation.resetNavBarPosition(node);
292    } else {
293      getUINativeModule().navigation.setNavBarPosition(node, this.value);
294    }
295  }
296}
297
298class ModeModifier extends ModifierWithKey<number> {
299  constructor(value: number) {
300    super(value);
301  }
302  static identity: Symbol = Symbol('mode');
303
304  applyPeer(node: KNode, reset: boolean): void {
305    if (reset) {
306      getUINativeModule().navigation.resetMode(node);
307    } else {
308      getUINativeModule().navigation.setMode(node, this.value);
309    }
310  }
311}
312
313class HideToolBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
314  constructor(value: ArkNavHideTitleBarOrToolBar) {
315    super(value);
316  }
317  static identity: Symbol = Symbol('hideToolBar');
318
319  applyPeer(node: KNode, reset: boolean): void {
320    if (reset) {
321      getUINativeModule().navigation.resetHideToolBar(node);
322    } else {
323      getUINativeModule().navigation.setHideToolBar(node, this.value?.isHide, this.value?.animated);
324    }
325  }
326}
327
328class TitleModeModifier extends ModifierWithKey<number> {
329  constructor(value: number) {
330    super(value);
331  }
332  static identity: Symbol = Symbol('titleMode');
333
334  applyPeer(node: KNode, reset: boolean): void {
335    if (reset) {
336      getUINativeModule().navigation.resetTitleMode(node);
337    } else {
338      getUINativeModule().navigation.setTitleMode(node, this.value);
339    }
340  }
341}
342
343class MenusModifier extends ModifierWithKey<Array<NavigationMenuItem> | undefined> {
344  constructor(value: Array<NavigationMenuItem> | undefined) {
345    super(value);
346  }
347  static identity: Symbol = Symbol('menus');
348
349  applyPeer(node: KNode, reset: boolean): void {
350    if (reset) {
351      getUINativeModule().navigation.resetMenus(node);
352    } else {
353      getUINativeModule().navigation.setMenus(node, this.value);
354    }
355  }
356
357  checkObjectDiff(): boolean {
358    if (!Array.isArray(this.value) || !Array.isArray(this.stageValue)) {
359      return true;
360    }
361    if (this.value.length !== this.stageValue.length) {
362      return true;
363    }
364    for (let i = 0; i < this.value.length; i++) {
365      if (!(isBaseOrResourceEqual(this.stageValue[i].value, this.value[i].value) &&
366        isBaseOrResourceEqual(this.stageValue[i].icon, this.value[i].icon) &&
367        isBaseOrResourceEqual(this.stageValue[i].isEnabled, this.value[i].isEnabled) &&
368        isBaseOrResourceEqual(this.stageValue[i].action, this.value[i].action) &&
369        isBaseOrResourceEqual(this.stageValue[i].symbolIcon, this.value[i].symbolIcon)
370      )) {
371        return true;
372      }
373    }
374    return false;
375  }
376}
377
378class HideBackButtonModifier extends ModifierWithKey<boolean> {
379  constructor(value: boolean) {
380    super(value);
381  }
382  static identity: Symbol = Symbol('hideBackButton');
383
384  applyPeer(node: KNode, reset: boolean): void {
385    if (reset) {
386      getUINativeModule().navigation.resetHideBackButton(node);
387    } else {
388      getUINativeModule().navigation.setHideBackButton(node, this.value);
389    }
390  }
391}
392
393class TitleModifier extends ModifierWithKey<ArkNavigationTitle | undefined> {
394  constructor(value: ArkNavigationTitle | undefined) {
395    super(value);
396  }
397  static identity: Symbol = Symbol('title');
398  applyPeer(node: KNode, reset: boolean): void {
399    if (reset) {
400      getUINativeModule().navigation.resetTitle(node);
401    } else {
402      getUINativeModule().navigation.setTitle(node, this.value?.value, this.value?.navigationTitleOptions);
403    }
404  }
405  checkObjectDiff(): boolean {
406    return !this.value.isEqual(this.stageValue);
407  }
408}
409
410class SubTitleModifier extends ModifierWithKey<string> {
411  constructor(value: string) {
412    super(value);
413  }
414  static identity: Symbol = Symbol('subTitle');
415
416  applyPeer(node: KNode, reset: boolean): void {
417    if (reset) {
418      getUINativeModule().navigation.resetSubTitle(node);
419    } else {
420      getUINativeModule().navigation.setSubTitle(node, this.value);
421    }
422  }
423}
424
425class NavigationHideTitleBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
426  constructor(value: ArkNavHideTitleBarOrToolBar) {
427    super(value);
428  }
429  static identity: Symbol = Symbol('hideTitleBar');
430
431  applyPeer(node: KNode, reset: boolean): void {
432    if (reset) {
433      getUINativeModule().navigation.resetHideTitleBar(node);
434    } else {
435      getUINativeModule().navigation.setHideTitleBar(node, this.value?.isHide, this.value?.animated);
436    }
437  }
438}
439
440class EnableModeChangeAnimationModifier extends ModifierWithKey<boolean | undefined> {
441  constructor(value: boolean | undefined) {
442    super(value);
443  }
444  static identity: Symbol = Symbol('enableModeChangeAnimation');
445
446  applyPeer(node: KNode, reset: boolean): void {
447    if (reset) {
448      getUINativeModule().navigation.resetEnableModeChangeAnimation(node);
449    } else {
450      getUINativeModule().navigation.setEnableModeChangeAnimation(node, this.value);
451    }
452  }
453}
454
455class HideNavBarModifier extends ModifierWithKey<boolean> {
456  constructor(value: boolean) {
457    super(value);
458  }
459  static identity: Symbol = Symbol('hideNavBar');
460
461  applyPeer(node: KNode, reset: boolean): void {
462    if (reset) {
463      getUINativeModule().navigation.resetHideNavBar(node);
464    } else {
465      getUINativeModule().navigation.setHideNavBar(node, this.value);
466    }
467  }
468}
469
470class IgnoreNavLayoutSafeAreaModifier extends ModifierWithKey<ArkSafeAreaExpandOpts | undefined> {
471  constructor(value: ArkSafeAreaExpandOpts | undefined) {
472    super(value);
473  }
474  static identity: Symbol = Symbol('ignoreLayoutSafeArea');
475  applyPeer(node: KNode, reset: boolean): void {
476    if (reset) {
477      getUINativeModule().navigation.resetIgnoreLayoutSafeArea(node);
478    } else {
479      getUINativeModule().navigation.setIgnoreLayoutSafeArea(node, this.value.type, this.value.edges);
480    }
481  }
482  checkObjectDiff(): boolean {
483    return !isBaseOrResourceEqual(this.stageValue.type, this.value.type) ||
484      !isBaseOrResourceEqual(this.stageValue.edges, this.value.edges);
485  }
486}
487
488class NavigationRecoverableModifier extends ModifierWithKey<boolean | undefined> {
489  constructor(value: boolean | undefined) {
490    super(value);
491  }
492  static identity: Symbol = Symbol('recoverable');
493
494  applyPeer(node: KNode, reset: boolean): void {
495    if (reset) {
496      getUINativeModule().navigation.resetRecoverable(node);
497    } else {
498      getUINativeModule().navigation.setRecoverable(node, this.value);
499    }
500  }
501}
502
503class NavigationEnableDragBarModifier extends ModifierWithKey<boolean | undefined> {
504  constructor(value: boolean | undefined) {
505    super(value);
506  }
507  static identity: Symbol = Symbol('enableDragBar');
508
509  applyPeer(node: KNode, reset: boolean): void {
510    if (reset) {
511      getUINativeModule().navigation.resetEnableDragBar(node);
512    } else {
513      getUINativeModule().navigation.setEnableDragBar(node, this.value);
514    }
515  }
516}
517
518class NavigationEnableToolBarAdaptationModifier extends ModifierWithKey<boolean | undefined> {
519  constructor(value: boolean | undefined) {
520    super(value);
521  }
522  static identity: Symbol = Symbol('enableToolBarAdaptation');
523
524  applyPeer(node: KNode, reset: boolean): void {
525    if (reset) {
526      getUINativeModule().navigation.resetEnableToolBarAdaptation(node);
527    } else {
528      getUINativeModule().navigation.setEnableToolBarAdaptation(node, this.value);
529    }
530  }
531}
532
533// @ts-ignore
534globalThis.Navigation.attributeModifier = function (modifier: ArkComponent): void {
535  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
536    return new ArkNavigationComponent(nativePtr);
537  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
538    return new modifierJS.NavigationModifier(nativePtr, classType);
539  });
540};
541