• 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' />
17
18import { ArkScrollable } from "./ArkScrollable";
19
20class ArkGridComponent extends ArkScrollable<GridAttribute> implements GridAttribute {
21  constructor(nativePtr: KNode, classType?: ModifierType) {
22    super(nativePtr, classType);
23  }
24  allowChildTypes(): string[] {
25    return ['GridItem'];
26  }
27  initialize(value: Object[]): void {
28    if (value.length === 1 && isObject(value[0])) {
29      modifierWithKey(this._modifiersWithKeys, GridScrollerModifier.identity, GridScrollerModifier, value[0]);
30      modifierWithKey(this._modifiersWithKeys, GridLayoutOptionsModifier.identity, GridLayoutOptionsModifier, undefined);
31    } else if (value.length === 2 && isObject(value[0]) && isObject(value[1])) {
32      modifierWithKey(this._modifiersWithKeys, GridScrollerModifier.identity, GridScrollerModifier, value[0]);
33      modifierWithKey(this._modifiersWithKeys, GridLayoutOptionsModifier.identity, GridLayoutOptionsModifier, value[1]);
34    } else {
35      modifierWithKey(this._modifiersWithKeys, GridScrollerModifier.identity, GridScrollerModifier, undefined);
36      modifierWithKey(this._modifiersWithKeys, GridLayoutOptionsModifier.identity, GridLayoutOptionsModifier, undefined);
37    }
38    return this;
39  }
40  columnsTemplate(value: string): this {
41    modifierWithKey(this._modifiersWithKeys, GridColumnsTemplateModifier.identity, GridColumnsTemplateModifier, value);
42    return this;
43  }
44  rowsTemplate(value: string): this {
45    modifierWithKey(this._modifiersWithKeys, GridRowsTemplateModifier.identity, GridRowsTemplateModifier, value);
46    return this;
47  }
48  columnsGap(value: Length): this {
49    modifierWithKey(this._modifiersWithKeys, GridColumnsGapModifier.identity, GridColumnsGapModifier, value);
50    return this;
51  }
52  rowsGap(value: Length): this {
53    modifierWithKey(this._modifiersWithKeys, GridRowsGapModifier.identity, GridRowsGapModifier, value);
54    return this;
55  }
56  scrollBarWidth(value: string | number): this {
57    modifierWithKey(this._modifiersWithKeys, GridScrollBarWidthModifier.identity, GridScrollBarWidthModifier, value);
58    return this;
59  }
60  scrollBarColor(value: string | number | Color): this {
61    modifierWithKey(this._modifiersWithKeys, GridScrollBarColorModifier.identity, GridScrollBarColorModifier, value);
62    return this;
63  }
64  scrollBar(value: BarState): this {
65    modifierWithKey(this._modifiersWithKeys, GridScrollBarModifier.identity, GridScrollBarModifier, value);
66    return this;
67  }
68  onScrollBarUpdate(event: (index: number, offset: number) => ComputedBarAttribute): this {
69    throw new Error('Method not implemented.');
70  }
71  onScrollIndex(event: (first: number, last: number) => void): this {
72    throw new Error('Method not implemented.');
73  }
74  cachedCount(count: number, show?: boolean): GridAttribute {
75    let opt = new ArkScrollableCacheOptions(count, show ? show : false);
76    modifierWithKey(this._modifiersWithKeys, GridCachedCountModifier.identity, GridCachedCountModifier, opt);
77    return this;
78  }
79  editMode(value: boolean): this {
80    modifierWithKey(this._modifiersWithKeys, GridEditModeModifier.identity, GridEditModeModifier, value);
81    return this;
82  }
83  multiSelectable(value: boolean): this {
84    modifierWithKey(this._modifiersWithKeys, GridMultiSelectableModifier.identity, GridMultiSelectableModifier, value);
85    return this;
86  }
87  maxCount(value: number): this {
88    modifierWithKey(this._modifiersWithKeys, GridMaxCountModifier.identity, GridMaxCountModifier, value);
89    return this;
90  }
91  minCount(value: number): this {
92    modifierWithKey(this._modifiersWithKeys, GridMinCountModifier.identity, GridMinCountModifier, value);
93    return this;
94  }
95  cellLength(value: number): this {
96    modifierWithKey(this._modifiersWithKeys, GridCellLengthModifier.identity, GridCellLengthModifier, value);
97    return this;
98  }
99  layoutDirection(value: GridDirection): this {
100    modifierWithKey(this._modifiersWithKeys, GridLayoutDirectionModifier.identity, GridLayoutDirectionModifier, value);
101    return this;
102  }
103  supportAnimation(value: boolean): this {
104    modifierWithKey(this._modifiersWithKeys, GridSupportAnimationModifier.identity, GridSupportAnimationModifier, value);
105    return this;
106  }
107  onItemDragStart(event: (event: ItemDragInfo, itemIndex: number) => void | (() => any)): this {
108    throw new Error('Method not implemented.');
109  }
110  onItemDragEnter(event: (event: ItemDragInfo) => void): this {
111    throw new Error('Method not implemented.');
112  }
113  onItemDragMove(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number) => void): this {
114    throw new Error('Method not implemented.');
115  }
116  onItemDragLeave(event: (event: ItemDragInfo, itemIndex: number) => void): this {
117    throw new Error('Method not implemented.');
118  }
119  onItemDrop(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => void): this {
120    throw new Error('Method not implemented.');
121  }
122  edgeEffect(value: EdgeEffect, options?: EdgeEffectOptions | undefined): this {
123    let effect: ArkGridEdgeEffect = new ArkGridEdgeEffect();
124    effect.value = value;
125    effect.options = options;
126    modifierWithKey(this._modifiersWithKeys, GridEdgeEffectModifier.identity, GridEdgeEffectModifier, effect);
127    return this;
128  }
129  fadingEdge(value: boolean, options?: FadingEdgeOptions | undefined): this {
130    let fadingEdge: ArkFadingEdge = new ArkFadingEdge();
131    fadingEdge.value = value;
132    fadingEdge.options = options;
133    modifierWithKey(this._modifiersWithKeys, GridFadingEdgeModifier.identity, GridFadingEdgeModifier, fadingEdge);
134    return this;
135  }
136  nestedScroll(value: NestedScrollOptions): this {
137    modifierWithKey(this._modifiersWithKeys, GridNestedScrollModifier.identity, GridNestedScrollModifier, value);
138    return this;
139  }
140  enableScrollInteraction(value: boolean): this {
141    modifierWithKey(this._modifiersWithKeys, GridEnableScrollModifier.identity, GridEnableScrollModifier, value);
142    return this;
143  }
144  friction(value: number | Resource): this {
145    modifierWithKey(this._modifiersWithKeys, GridFrictionModifier.identity, GridFrictionModifier, value);
146    return this;
147  }
148  onScroll(event: (scrollOffset: number, scrollState: ScrollState) => void): this {
149    throw new Error('Method not implemented.');
150  }
151  onReachStart(event: () => void): this {
152    throw new Error('Method not implemented.');
153  }
154  onReachEnd(event: () => void): this {
155    throw new Error('Method not implemented.');
156  }
157  onScrollStart(event: () => void): this {
158    throw new Error('Method not implemented.');
159  }
160  onScrollStop(event: () => void): this {
161    throw new Error('Method not implemented.');
162  }
163  onScrollFrameBegin(event: (offset: number, state: ScrollState) => { offsetRemain: number; }): this {
164    throw new Error('Method not implemented.');
165  }
166  clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): this {
167    modifierWithKey(this._modifiersWithKeys, GridClipModifier.identity, GridClipModifier, value);
168    return this;
169  }
170  flingSpeedLimit(value: number): this {
171    modifierWithKey(this._modifiersWithKeys, GridFlingSpeedLimitModifier.identity, GridFlingSpeedLimitModifier, value);
172    return this;
173  }
174  alignItems(value: GridItemAlignment): this {
175    modifierWithKey(this._modifiersWithKeys, GridAlignItemsModifier.identity, GridAlignItemsModifier, value);
176    return this;
177  }
178}
179
180class GridScrollerModifier extends ModifierWithKey<Scroller> {
181  constructor(value: Scroller) {
182    super(value);
183  }
184  static identity: Symbol = Symbol('gridScroller');
185  applyPeer(node: KNode, reset: boolean): void {
186    if (reset) {
187      getUINativeModule().grid.setGridScroller(node, undefined);
188    } else {
189      getUINativeModule().grid.setGridScroller(node, this.value);
190    }
191  }
192  checkObjectDiff(): boolean {
193    return !isBaseOrResourceEqual(this.stageValue, this.value);
194  }
195}
196
197class GridLayoutOptionsModifier extends ModifierWithKey<GridLayoutOptions> {
198  constructor(value: GridLayoutOptions) {
199    super(value);
200  }
201  static identity: Symbol = Symbol('gridLayoutOptions');
202  applyPeer(node: KNode, reset: boolean): void {
203    if (reset) {
204      getUINativeModule().grid.setGridLayoutOptions(node, undefined, undefined, undefined, undefined, undefined);
205    } else {
206      getUINativeModule().grid.setGridLayoutOptions(node,
207        isArray(this.value.regularSize) ? this.value.regularSize : undefined,
208        isArray(this.value?.irregularIndexes) ? this.value.irregularIndexes : undefined,
209        isArray(this.value?.irregularIndexes) ? this.value.irregularIndexes.length : undefined,
210        isFunction(this.value?.onGetIrregularSizeByIndex) ? this.value.onGetIrregularSizeByIndex : undefined,
211        isFunction(this.value?.onGetRectByIndex) ? this.value.onGetRectByIndex : undefined);
212    }
213  }
214  checkObjectDiff(): boolean {
215    return !isBaseOrResourceEqual(this.stageValue?.regularSize, this.value?.regularSize) ||
216      !isBaseOrResourceEqual(this.stageValue?.irregularIndexes, this.value?.irregularIndexes) ||
217      !isBaseOrResourceEqual(this.stageValue?.onGetIrregularSizeByIndex, this.value?.onGetIrregularSizeByIndex) ||
218      !isBaseOrResourceEqual(this.stageValue?.onGetRectByIndex, this.value?.onGetRectByIndex);
219  }
220}
221
222class GridColumnsTemplateModifier extends ModifierWithKey<string> {
223  static identity: Symbol = Symbol('gridColumnsTemplate');
224  applyPeer(node: KNode, reset: boolean): void {
225    if (reset) {
226      getUINativeModule().grid.resetColumnsTemplate(node);
227    } else {
228      getUINativeModule().grid.setColumnsTemplate(node, this.value);
229    }
230  }
231}
232
233class GridRowsTemplateModifier extends ModifierWithKey<string> {
234  static identity: Symbol = Symbol('gridRowsTemplate');
235  applyPeer(node: KNode, reset: boolean): void {
236    if (reset) {
237      getUINativeModule().grid.resetRowsTemplate(node);
238    } else {
239      getUINativeModule().grid.setRowsTemplate(node, this.value);
240    }
241  }
242}
243
244class GridColumnsGapModifier extends ModifierWithKey<Length> {
245  static identity: Symbol = Symbol('gridColumnsGap');
246  applyPeer(node: KNode, reset: boolean): void {
247    if (reset) {
248      getUINativeModule().grid.resetColumnsGap(node);
249    } else {
250      getUINativeModule().grid.setColumnsGap(node, this.value!);
251    }
252  }
253  checkObjectDiff(): boolean {
254    return !isBaseOrResourceEqual(this.stageValue, this.value);
255  }
256}
257
258class GridRowsGapModifier extends ModifierWithKey<Length> {
259  static identity: Symbol = Symbol('gridRowsGap');
260  applyPeer(node: KNode, reset: boolean): void {
261    if (reset) {
262      getUINativeModule().grid.resetRowsGap(node);
263    } else {
264      getUINativeModule().grid.setRowsGap(node, this.value!);
265    }
266  }
267  checkObjectDiff(): boolean {
268    return !isBaseOrResourceEqual(this.stageValue, this.value);
269  }
270}
271
272class GridScrollBarWidthModifier extends ModifierWithKey<string | number> {
273  static identity: Symbol = Symbol('gridScrollBarWidth');
274  applyPeer(node: KNode, reset: boolean): void {
275    if (reset) {
276      getUINativeModule().grid.resetScrollBarWidth(node);
277    } else {
278      getUINativeModule().grid.setScrollBarWidth(node, this.value);
279    }
280  }
281}
282
283class GridScrollBarModifier extends ModifierWithKey<BarState> {
284  static identity: Symbol = Symbol('gridScrollBar');
285  applyPeer(node: KNode, reset: boolean): void {
286    if (reset) {
287      getUINativeModule().grid.resetScrollBar(node);
288    } else {
289      getUINativeModule().grid.setScrollBar(node, this.value);
290    }
291  }
292}
293
294class GridScrollBarColorModifier extends ModifierWithKey<string | number | Color> {
295  static identity: Symbol = Symbol('gridScrollBarColor');
296  applyPeer(node: KNode, reset: boolean): void {
297    if (reset) {
298      getUINativeModule().grid.resetScrollBarColor(node);
299    } else {
300      getUINativeModule().grid.setScrollBarColor(node, this.value);
301    }
302  }
303}
304
305class GridEditModeModifier extends ModifierWithKey<boolean> {
306  static identity: Symbol = Symbol('gridEditMode');
307  applyPeer(node: KNode, reset: boolean): void {
308    if (reset) {
309      getUINativeModule().grid.resetEditMode(node);
310    } else {
311      getUINativeModule().grid.setEditMode(node, this.value);
312    }
313  }
314}
315
316class GridCachedCountModifier extends ModifierWithKey<ArkScrollableCacheOptions> {
317  constructor(value: ArkScrollableCacheOptions) {
318    super(value);
319  }
320  static identity: Symbol = Symbol('gridCachedCount');
321  applyPeer(node: KNode, reset: boolean): void {
322    if (reset) {
323      getUINativeModule().grid.resetCachedCount(node);
324    } else {
325      getUINativeModule().grid.setCachedCount(node, this.value.count, this.value.show);
326    }
327  }
328}
329
330class GridMultiSelectableModifier extends ModifierWithKey<boolean> {
331  static identity: Symbol = Symbol('gridMultiSelectable');
332  applyPeer(node: KNode, reset: boolean): void {
333    if (reset) {
334      getUINativeModule().grid.resetMultiSelectable(node);
335    } else {
336      getUINativeModule().grid.setMultiSelectable(node, this.value);
337    }
338  }
339}
340
341class GridEdgeEffectModifier extends ModifierWithKey<ArkGridEdgeEffect> {
342  static identity: Symbol = Symbol('gridEdgeEffect');
343  applyPeer(node: KNode, reset: boolean): void {
344    if (reset) {
345      getUINativeModule().grid.resetEdgeEffect(node);
346    } else {
347      getUINativeModule().grid.setEdgeEffect(node, this.value?.value, this.value.options?.alwaysEnabled);
348    }
349  }
350
351  checkObjectDiff(): boolean {
352    return !((this.stageValue.value === this.value.value) &&
353      (this.stageValue.options === this.value.options));
354  }
355}
356
357class GridFadingEdgeModifier extends ModifierWithKey<ArkFadingEdge> {
358  constructor(value: ArkFadingEdge) {
359    super(value);
360  }
361  static identity: Symbol = Symbol('gridFadingEdge');
362  applyPeer(node: KNode, reset: boolean): void {
363    if (reset) {
364      getUINativeModule().grid.resetFadingEdge(node);
365    } else {
366      getUINativeModule().grid.setFadingEdge(node, this.value.value!, this.value.options?.fadingEdgeLength);
367    }
368  }
369  checkObjectDiff(): boolean {
370    return !((this.stageValue.value === this.value.value) &&
371      (this.stageValue.options === this.value.options));
372  }
373}
374
375class GridNestedScrollModifier extends ModifierWithKey<NestedScrollOptions> {
376  static identity: Symbol = Symbol('gridNestedScroll');
377  applyPeer(node: KNode, reset: boolean): void {
378    if (reset) {
379      getUINativeModule().grid.resetNestedScroll(node);
380    } else {
381      getUINativeModule().grid.setNestedScroll(node, this.value?.scrollForward, this.value?.scrollBackward);
382    }
383  }
384
385  checkObjectDiff(): boolean {
386    return !((this.stageValue.scrollForward === this.value.scrollForward) &&
387      (this.stageValue.scrollBackward === this.value.scrollBackward));
388  }
389}
390
391class GridEnableScrollModifier extends ModifierWithKey<boolean> {
392  static identity: Symbol = Symbol('gridEnableScroll');
393  applyPeer(node: KNode, reset: boolean): void {
394    if (reset) {
395      getUINativeModule().grid.resetEnableScroll(node);
396    } else {
397      getUINativeModule().grid.setEnableScroll(node, this.value);
398    }
399  }
400}
401
402class GridFrictionModifier extends ModifierWithKey<number | Resource> {
403  static identity: Symbol = Symbol('gridFriction');
404  applyPeer(node: KNode, reset: boolean): void {
405    if (reset) {
406      getUINativeModule().grid.resetFriction(node);
407    } else {
408      getUINativeModule().grid.setFriction(node, this.value);
409    }
410  }
411
412  checkObjectDiff(): boolean {
413    return !isBaseOrResourceEqual(this.stageValue, this.value);
414  }
415}
416
417class GridMaxCountModifier extends ModifierWithKey<number> {
418  static identity: Symbol = Symbol('gridMaxCount');
419  applyPeer(node: KNode, reset: boolean): void {
420    if (reset) {
421      getUINativeModule().grid.resetMaxCount(node);
422    } else {
423      getUINativeModule().grid.setMaxCount(node, this.value);
424    }
425  }
426}
427
428class GridMinCountModifier extends ModifierWithKey<number> {
429  static identity: Symbol = Symbol('gridMinCount');
430  applyPeer(node: KNode, reset: boolean): void {
431    if (reset) {
432      getUINativeModule().grid.resetMinCount(node);
433    } else {
434      getUINativeModule().grid.setMinCount(node, this.value);
435    }
436  }
437}
438
439class GridCellLengthModifier extends ModifierWithKey<number> {
440  static identity: Symbol = Symbol('gridCellLength');
441  applyPeer(node: KNode, reset: boolean): void {
442    if (reset) {
443      getUINativeModule().grid.resetCellLength(node);
444    } else {
445      getUINativeModule().grid.setCellLength(node, this.value);
446    }
447  }
448}
449
450class GridLayoutDirectionModifier extends ModifierWithKey<GridDirection> {
451  static identity: Symbol = Symbol('gridLayoutDirection');
452  applyPeer(node: KNode, reset: boolean): void {
453    if (reset) {
454      getUINativeModule().grid.resetLayoutDirection(node);
455    } else {
456      getUINativeModule().grid.setLayoutDirection(node, this.value);
457    }
458  }
459}
460
461class GridSupportAnimationModifier extends ModifierWithKey<boolean> {
462  static identity: Symbol = Symbol('gridSupportAnimation');
463  applyPeer(node: KNode, reset: boolean): void {
464    if (reset) {
465      getUINativeModule().grid.resetSupportAnimation(node);
466    } else {
467      getUINativeModule().grid.setSupportAnimation(node, this.value);
468    }
469  }
470}
471
472class GridClipModifier extends ModifierWithKey<boolean | object> {
473  constructor(value: boolean | object) {
474    super(value);
475  }
476  static identity: Symbol = Symbol('gridClip');
477  applyPeer(node: KNode, reset: boolean): void {
478    if (reset) {
479      getUINativeModule().common.resetClipWithEdge(node);
480    } else {
481      getUINativeModule().common.setClipWithEdge(node, this.value);
482    }
483  }
484  checkObjectDiff(): boolean {
485    return true;
486  }
487}
488
489class GridFlingSpeedLimitModifier extends ModifierWithKey<number> {
490  constructor(value: number) {
491    super(value);
492  }
493  static identity: Symbol = Symbol('gridFlingSpeedLimit');
494  applyPeer(node: KNode, reset: boolean): void {
495    if (reset) {
496      getUINativeModule().grid.resetFlingSpeedLimit(node);
497    } else {
498      getUINativeModule().grid.setFlingSpeedLimit(node, this.value);
499    }
500  }
501}
502
503class GridAlignItemsModifier extends ModifierWithKey<GridItemAlignment> {
504  constructor(value: GridItemAlignment) {
505    super(value);
506  }
507  static identity: Symbol = Symbol('gridAlignItems');
508  applyPeer(node: KNode, reset: boolean): void {
509    if (reset) {
510      getUINativeModule().grid.resetAlignItems(node);
511    } else {
512      getUINativeModule().grid.setAlignItems(node, this.value);
513    }
514  }
515}
516
517// @ts-ignore
518globalThis.Grid.attributeModifier = function (modifier: ArkComponent): void {
519  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
520    return new ArkGridComponent(nativePtr);
521  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
522    return new modifierJS.GridModifier(nativePtr, classType);
523  });
524};
525