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' /> 17class ListEditModeModifier extends ModifierWithKey<boolean> { 18 static identity: Symbol = Symbol('editMode'); 19 applyPeer(node: KNode, reset: boolean): void { 20 if (reset) { 21 getUINativeModule().list.resetEditMode(node); 22 } else { 23 getUINativeModule().list.setEditMode(node, this.value!); 24 } 25 } 26} 27 28class ListMultiSelectableModifier extends ModifierWithKey<boolean> { 29 static identity: Symbol = Symbol('listMultiSelectable'); 30 applyPeer(node: KNode, reset: boolean): void { 31 if (reset) { 32 getUINativeModule().list.resetMultiSelectable(node); 33 } else { 34 getUINativeModule().list.setMultiSelectable(node, this.value!); 35 } 36 } 37} 38 39class ListAlignListItemModifier extends ModifierWithKey<ListItemAlign> { 40 static identity: Symbol = Symbol('listAlignListItem'); 41 applyPeer(node: KNode, reset: boolean): void { 42 if (reset) { 43 getUINativeModule().list.resetAlignListItem(node); 44 } else { 45 getUINativeModule().list.setAlignListItem(node, this.value!); 46 } 47 } 48} 49 50class ListScrollSnapAlignModifier extends ModifierWithKey<ScrollSnapAlign> { 51 static identity: Symbol = Symbol('listScrollSnapAlign'); 52 applyPeer(node: KNode, reset: boolean): void { 53 if (reset) { 54 getUINativeModule().list.resetScrollSnapAlign(node); 55 } else { 56 getUINativeModule().list.setScrollSnapAlign(node, this.value!); 57 } 58 } 59} 60 61class ContentStartOffsetModifier extends ModifierWithKey<number> { 62 constructor(value: number) { 63 super(value); 64 } 65 static identity: Symbol = Symbol('contentStartOffset'); 66 applyPeer(node: KNode, reset: boolean): void { 67 if (reset) { 68 getUINativeModule().list.resetContentStartOffset(node); 69 } else { 70 getUINativeModule().list.setContentStartOffset(node, this.value); 71 } 72 } 73} 74 75class ContentEndOffsetModifier extends ModifierWithKey<number> { 76 constructor(value: number) { 77 super(value); 78 } 79 static identity: Symbol = Symbol('contentEndOffset'); 80 applyPeer(node: KNode, reset: boolean): void { 81 if (reset) { 82 getUINativeModule().list.resetContentEndOffset(node); 83 } else { 84 getUINativeModule().list.setContentEndOffset(node, this.value); 85 } 86 } 87} 88 89class ListDividerModifier extends ModifierWithKey<DividerStyle> { 90 constructor(value: DividerStyle) { 91 super(value); 92 } 93 static identity: Symbol = Symbol('listDivider'); 94 applyPeer(node: KNode, reset: boolean): void { 95 if (reset) { 96 getUINativeModule().list.resetDivider(node); 97 } else { 98 getUINativeModule().list.setDivider(node, this.value?.strokeWidth!, this.value?.color, this.value?.startMargin, this.value?.endMargin); 99 } 100 } 101 102 checkObjectDiff(): boolean { 103 return !(this.stageValue?.strokeWidth === this.value?.strokeWidth && 104 this.stageValue?.color === this.value?.color && 105 this.stageValue?.startMargin === this.value?.startMargin && 106 this.stageValue?.endMargin === this.value?.endMargin); 107 } 108} 109 110class ChainAnimationOptionsModifier extends ModifierWithKey<ChainAnimationOptions> { 111 static identity: Symbol = Symbol('chainAnimationOptions'); 112 applyPeer(node: KNode, reset: boolean): void { 113 if (reset) { 114 getUINativeModule().list.resetChainAnimationOptions(node); 115 } else { 116 getUINativeModule().list.setChainAnimationOptions(node, this.value?.minSpace!, 117 this.value?.maxSpace!, this.value?.conductivity, this.value?.intensity, 118 this.value?.edgeEffect, this.value?.stiffness, this.value?.damping); 119 } 120 } 121 122 checkObjectDiff(): boolean { 123 return !(this.stageValue.minSpace === this.value.minSpace && this.stageValue.maxSpace === this.value.maxSpace && 124 this.stageValue.conductivity === this.value.conductivity && this.stageValue.intensity === this.value.intensity && 125 this.stageValue.edgeEffect === this.value.edgeEffect && this.stageValue.stiffness === this.value.stiffness && 126 this.stageValue.damping === this.value.damping); 127 } 128} 129 130class ListChainAnimationModifier extends ModifierWithKey<boolean> { 131 static identity: Symbol = Symbol('listChainAnimation'); 132 applyPeer(node: KNode, reset: boolean): void { 133 if (reset) { 134 getUINativeModule().list.resetChainAnimation(node); 135 } else { 136 getUINativeModule().list.setChainAnimation(node, this.value!); 137 } 138 } 139} 140 141class ListCachedCountModifier extends ModifierWithKey<number> { 142 static identity: Symbol = Symbol('listCachedCount'); 143 applyPeer(node: KNode, reset: boolean): void { 144 if (reset) { 145 getUINativeModule().list.resetCachedCount(node); 146 } else { 147 getUINativeModule().list.setCachedCount(node, this.value!); 148 } 149 } 150} 151 152class ListEnableScrollInteractionModifier extends ModifierWithKey<boolean> { 153 static identity: Symbol = Symbol('listEnableScrollInteraction'); 154 applyPeer(node: KNode, reset: boolean): void { 155 if (reset) { 156 getUINativeModule().list.resetEnableScrollInteraction(node); 157 } else { 158 getUINativeModule().list.setEnableScrollInteraction(node, this.value!); 159 } 160 } 161} 162 163class ListStickyModifier extends ModifierWithKey<number> { 164 static identity: Symbol = Symbol('listSticky'); 165 applyPeer(node: KNode, reset: boolean): void { 166 if (reset) { 167 getUINativeModule().list.resetSticky(node); 168 } else { 169 getUINativeModule().list.setSticky(node, this.value!); 170 } 171 } 172} 173 174class ListEdgeEffectModifier extends ModifierWithKey<ArkListEdgeEffect> { 175 static identity: Symbol = Symbol('listEdgeEffect'); 176 applyPeer(node: KNode, reset: boolean): void { 177 if (reset) { 178 getUINativeModule().list.resetListEdgeEffect(node); 179 } else { 180 getUINativeModule().list.setListEdgeEffect(node, this.value.value!, this.value.options?.alwaysEnabled); 181 } 182 } 183 checkObjectDiff(): boolean { 184 return !((this.stageValue.value === this.value.value) && 185 (this.stageValue.options === this.value.options)); 186 } 187} 188 189class ListListDirectionModifier extends ModifierWithKey<number> { 190 static identity: Symbol = Symbol('listListDirection'); 191 applyPeer(node: KNode, reset: boolean): void { 192 if (reset) { 193 getUINativeModule().list.resetListDirection(node); 194 } else { 195 getUINativeModule().list.setListDirection(node, this.value!); 196 } 197 } 198} 199 200class ListFrictionModifier extends ModifierWithKey<number | Resource> { 201 static identity: Symbol = Symbol('listFriction'); 202 applyPeer(node: KNode, reset: boolean): void { 203 if (reset) { 204 getUINativeModule().list.resetListFriction(node); 205 } else { 206 if (!isNumber(this.value) && !isResource(this.value)) { 207 getUINativeModule().list.resetListFriction(node); 208 } else { 209 getUINativeModule().list.setListFriction(node, this.value); 210 } 211 } 212 } 213 214 checkObjectDiff(): boolean { 215 return !isBaseOrResourceEqual(this.stageValue, this.value); 216 } 217} 218 219class ListMaintainVisibleContentPositionModifier extends ModifierWithKey<boolean | undefined> { 220 constructor(value: boolean | undefined) { 221 super(value); 222 } 223 static identity: Symbol = Symbol('listMaintainVisibleContentPosition'); 224 applyPeer(node: KNode, reset: boolean): void { 225 if (reset) { 226 getUINativeModule().list.resetListMaintainVisibleContentPosition(node); 227 } else { 228 getUINativeModule().list.setListMaintainVisibleContentPosition(node, this.value); 229 } 230 } 231} 232 233class ListNestedScrollModifier extends ModifierWithKey<NestedScrollOptions> { 234 constructor(value: NestedScrollOptions) { 235 super(value); 236 } 237 static identity: Symbol = Symbol('listNestedScroll'); 238 applyPeer(node: KNode, reset: boolean): void { 239 if (reset) { 240 getUINativeModule().list.resetListNestedScroll(node); 241 } else { 242 getUINativeModule().list.setListNestedScroll(node, this.value?.scrollForward, this.value?.scrollBackward); 243 } 244 } 245} 246 247class ListScrollBarModifier extends ModifierWithKey<number> { 248 static identity: Symbol = Symbol('listScrollBar'); 249 applyPeer(node: KNode, reset: boolean): void { 250 if (reset) { 251 getUINativeModule().list.resetListScrollBar(node); 252 } else { 253 getUINativeModule().list.setListScrollBar(node, this.value!); 254 } 255 } 256} 257 258class ListScrollBarWidthModifier extends ModifierWithKey<string | number> { 259 constructor(value: string | number) { 260 super(value); 261 } 262 static identity: Symbol = Symbol('listScrollBarWidth'); 263 applyPeer(node: KNode, reset: boolean): void { 264 if (reset) { 265 getUINativeModule().list.resetScrollBarWidth(node); 266 } else { 267 getUINativeModule().list.setScrollBarWidth(node, this.value); 268 } 269 } 270 checkObjectDiff(): boolean { 271 return !isBaseOrResourceEqual(this.stageValue, this.value); 272 } 273} 274 275class ListScrollBarColorModifier extends ModifierWithKey<string | number | Color> { 276 constructor(value: string | number | Color) { 277 super(value); 278 } 279 static identity: Symbol = Symbol('listScrollBarColor'); 280 applyPeer(node: KNode, reset: boolean): void { 281 if (reset) { 282 getUINativeModule().list.resetScrollBarColor(node); 283 } else { 284 getUINativeModule().list.setScrollBarColor(node, this.value); 285 } 286 } 287 checkObjectDiff(): boolean { 288 return !isBaseOrResourceEqual(this.stageValue, this.value); 289 } 290} 291 292class ListFlingSpeedLimitModifier extends ModifierWithKey<number> { 293 constructor(value: number) { 294 super(value); 295 } 296 static identity: Symbol = Symbol('listFlingSpeedLimit'); 297 applyPeer(node: KNode, reset: boolean): void { 298 if (reset) { 299 getUINativeModule().list.resetFlingSpeedLimit(node); 300 } else { 301 getUINativeModule().list.setFlingSpeedLimit(node, this.value); 302 } 303 } 304 checkObjectDiff(): boolean { 305 return !isBaseOrResourceEqual(this.stageValue, this.value); 306 } 307} 308 309class ListLanesModifier extends ModifierWithKey<ArkLanesOpt> { 310 static identity: Symbol = Symbol('listLanes'); 311 applyPeer(node: KNode, reset: boolean): void { 312 if (reset) { 313 getUINativeModule().list.resetListLanes(node); 314 } else { 315 getUINativeModule().list.setListLanes(node, this.value.lanesNum, 316 this.value.minLength, this.value.maxLength, this.value.gutter); 317 } 318 } 319 checkObjectDiff(): boolean { 320 return true; 321 } 322} 323 324class ListClipModifier extends ModifierWithKey<boolean | object> { 325 constructor(value: boolean | object) { 326 super(value); 327 } 328 static identity: Symbol = Symbol('listClip'); 329 applyPeer(node: KNode, reset: boolean): void { 330 if (reset) { 331 getUINativeModule().common.resetClipWithEdge(node); 332 } else { 333 getUINativeModule().common.setClipWithEdge(node, this.value); 334 } 335 } 336 checkObjectDiff(): boolean { 337 return true; 338 } 339} 340 341class ListChildrenMainSizeModifier extends ModifierWithKey<ChildrenMainSize> { 342 constructor(value: ChildrenMainSize) { 343 super(value); 344 } 345 static identity: Symbol = Symbol('listChildrenMainSize'); 346 applyPeer(node: KNode, reset: boolean): void { 347 if (reset) { 348 getUINativeModule().list.resetListChildrenMainSize(node); 349 } else { 350 getUINativeModule().list.setListChildrenMainSize(node, this.value); 351 } 352 } 353 checkObjectDiff(): boolean { 354 return true; 355 } 356} 357 358class ListSpaceModifier extends ModifierWithKey<number | string> { 359 constructor(value: number | string) { 360 super(value); 361 } 362 static identity: Symbol = Symbol('listSpace'); 363 applyPeer(node: KNode, reset: boolean): void { 364 if (reset) { 365 getUINativeModule().list.resetSpace(node); 366 } 367 else { 368 getUINativeModule().list.setSpace(node, this.value); 369 } 370 } 371} 372 373class ListInitialIndexModifier extends ModifierWithKey<number> { 374 constructor(value: number) { 375 super(value); 376 } 377 static identity: Symbol = Symbol('listInitialIndex'); 378 applyPeer(node: KNode, reset: boolean): void { 379 if (reset) { 380 getUINativeModule().list.resetInitialIndex(node); 381 } 382 else { 383 getUINativeModule().list.setInitialIndex(node, this.value); 384 } 385 } 386} 387 388class ListInitialScrollerModifier extends ModifierWithKey<number> { 389 constructor(value: number) { 390 super(value); 391 } 392 static identity: Symbol = Symbol('listInitialScroller'); 393 applyPeer(node: KNode, reset: boolean): void { 394 if (reset) { 395 getUINativeModule().list.resetInitialScroller(node); 396 } 397 else { 398 getUINativeModule().list.setInitialScroller(node, this.value); 399 } 400 } 401} 402 403interface ListParam { 404 initialIndex?: number; 405 space?: number | string; 406 scroller?: Scroller; 407} 408 409class ArkListComponent extends ArkComponent implements ListAttribute { 410 constructor(nativePtr: KNode, classType?: ModifierType) { 411 super(nativePtr, classType); 412 } 413 initialize(value: Object[]): this { 414 if (value[0] !== undefined) { 415 if ((value[0] as ListParam).initialIndex !== undefined) { 416 modifierWithKey(this._modifiersWithKeys, ListInitialIndexModifier.identity, ListInitialIndexModifier, (value[0] as ListParam).initialIndex); 417 } 418 if ((value[0] as ListParam).space !== undefined) { 419 modifierWithKey(this._modifiersWithKeys, ListSpaceModifier.identity, ListSpaceModifier, (value[0] as ListParam).space); 420 } 421 if ((value[0] as ListParam).scroller !== undefined) { 422 modifierWithKey(this._modifiersWithKeys, ListInitialScrollerModifier.identity, ListInitialScrollerModifier, (value[0] as ListParam).scroller); 423 } 424 } 425 return this; 426 } 427 allowChildTypes(): string[] { 428 return ["ListItem", "ListItemGroup"]; 429 } 430 lanes(value: number | LengthConstrain, gutter?: any): this { 431 let opt: ArkLanesOpt = new ArkLanesOpt(); 432 opt.gutter = gutter; 433 if (isUndefined(value)) { 434 opt.lanesNum = undefined; 435 } else if (isNumber(value)) { 436 opt.lanesNum = value as number; 437 } else { 438 const lc = value as LengthConstrain; 439 opt.minLength = lc.minLength; 440 opt.maxLength = lc.maxLength; 441 } 442 modifierWithKey(this._modifiersWithKeys, ListLanesModifier.identity, ListLanesModifier, opt); 443 return this; 444 } 445 alignListItem(value: ListItemAlign): this { 446 modifierWithKey(this._modifiersWithKeys, ListAlignListItemModifier.identity, ListAlignListItemModifier, value); 447 return this; 448 } 449 listDirection(value: Axis): this { 450 modifierWithKey(this._modifiersWithKeys, ListListDirectionModifier.identity, ListListDirectionModifier, value); 451 return this; 452 } 453 scrollBar(value: BarState): this { 454 modifierWithKey(this._modifiersWithKeys, ListScrollBarModifier.identity, ListScrollBarModifier, value); 455 return this; 456 } 457 scrollBarWidth(value: string | number): this { 458 modifierWithKey(this._modifiersWithKeys, ListScrollBarWidthModifier.identity, ListScrollBarWidthModifier, value); 459 return this; 460 } 461 scrollBarColor(value: string | number | Color): this { 462 modifierWithKey(this._modifiersWithKeys, ListScrollBarColorModifier.identity, ListScrollBarColorModifier, value); 463 return this; 464 } 465 flingSpeedLimit(value: number): this { 466 modifierWithKey(this._modifiersWithKeys, ListFlingSpeedLimitModifier.identity, ListFlingSpeedLimitModifier, value); 467 return this; 468 } 469 edgeEffect(value: EdgeEffect, options?: EdgeEffectOptions | undefined): this { 470 let effect: ArkListEdgeEffect = new ArkListEdgeEffect(); 471 effect.value = value; 472 effect.options = options; 473 modifierWithKey(this._modifiersWithKeys, ListEdgeEffectModifier.identity, ListEdgeEffectModifier, effect); 474 return this; 475 } 476 contentStartOffset(value: number): this { 477 modifierWithKey(this._modifiersWithKeys, ContentStartOffsetModifier.identity, ContentStartOffsetModifier, value); 478 return this; 479 } 480 contentEndOffset(value: number): this { 481 modifierWithKey(this._modifiersWithKeys, ContentEndOffsetModifier.identity, ContentEndOffsetModifier, value); 482 return this; 483 } 484 divider(value: { strokeWidth: any; color?: any; startMargin?: any; endMargin?: any; } | null): this { 485 modifierWithKey(this._modifiersWithKeys, ListDividerModifier.identity, ListDividerModifier, value); 486 return this; 487 } 488 editMode(value: boolean): this { 489 modifierWithKey(this._modifiersWithKeys, ListEditModeModifier.identity, ListEditModeModifier, value); 490 return this; 491 } 492 multiSelectable(value: boolean): this { 493 modifierWithKey(this._modifiersWithKeys, ListMultiSelectableModifier.identity, ListMultiSelectableModifier, value); 494 return this; 495 } 496 cachedCount(value: number): this { 497 modifierWithKey(this._modifiersWithKeys, ListCachedCountModifier.identity, ListCachedCountModifier, value); 498 return this; 499 } 500 chainAnimation(value: boolean): this { 501 modifierWithKey(this._modifiersWithKeys, ListChainAnimationModifier.identity, ListChainAnimationModifier, value); 502 return this; 503 } 504 chainAnimationOptions(value: ChainAnimationOptions): this { 505 modifierWithKey(this._modifiersWithKeys, ChainAnimationOptionsModifier.identity, ChainAnimationOptionsModifier, value); 506 return this; 507 } 508 sticky(value: StickyStyle): this { 509 modifierWithKey(this._modifiersWithKeys, ListStickyModifier.identity, ListStickyModifier, value); 510 return this; 511 } 512 scrollSnapAlign(value: ScrollSnapAlign): this { 513 modifierWithKey(this._modifiersWithKeys, ListScrollSnapAlignModifier.identity, ListScrollSnapAlignModifier, value); 514 return this; 515 } 516 nestedScroll(value: NestedScrollOptions): this { 517 modifierWithKey(this._modifiersWithKeys, ListNestedScrollModifier.identity, ListNestedScrollModifier, value); 518 return this; 519 } 520 enableScrollInteraction(value: boolean): this { 521 modifierWithKey(this._modifiersWithKeys, ListEnableScrollInteractionModifier.identity, ListEnableScrollInteractionModifier, value); 522 return this; 523 } 524 friction(value: any): this { 525 modifierWithKey(this._modifiersWithKeys, ListFrictionModifier.identity, ListFrictionModifier, value); 526 return this; 527 } 528 maintainVisibleContentPosition(value: boolean | undefined): this { 529 modifierWithKey(this._modifiersWithKeys, ListMaintainVisibleContentPositionModifier.identity, 530 ListMaintainVisibleContentPositionModifier, value); 531 return this; 532 } 533 clip(value: boolean | CircleAttribute | EllipseAttribute | PathAttribute | RectAttribute): this { 534 modifierWithKey(this._modifiersWithKeys, ListClipModifier.identity, ListClipModifier, value); 535 return this; 536 } 537 onScroll(event: (scrollOffset: number, scrollState: ScrollState) => void): this { 538 throw new Error('Method not implemented.'); 539 } 540 onScrollIndex(event: (start: number, end: number, center: number) => void): this { 541 throw new Error('Method not implemented.'); 542 } 543 onReachStart(event: () => void): this { 544 throw new Error('Method not implemented.'); 545 } 546 onReachEnd(event: () => void): this { 547 throw new Error('Method not implemented.'); 548 } 549 onScrollStart(event: () => void): this { 550 throw new Error('Method not implemented.'); 551 } 552 onScrollStop(event: () => void): this { 553 throw new Error('Method not implemented.'); 554 } 555 onItemDelete(event: (index: number) => boolean): this { 556 throw new Error('Method not implemented.'); 557 } 558 onItemMove(event: (from: number, to: number) => boolean): this { 559 throw new Error('Method not implemented.'); 560 } 561 onItemDragStart(event: (event: ItemDragInfo, itemIndex: number) => void | (() => any)): this { 562 throw new Error('Method not implemented.'); 563 } 564 onItemDragEnter(event: (event: ItemDragInfo) => void): this { 565 throw new Error('Method not implemented.'); 566 } 567 onItemDragMove(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number) => void): this { 568 throw new Error('Method not implemented.'); 569 } 570 onItemDragLeave(event: (event: ItemDragInfo, itemIndex: number) => void): this { 571 throw new Error('Method not implemented.'); 572 } 573 onItemDrop(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => void): this { 574 throw new Error('Method not implemented.'); 575 } 576 onScrollFrameBegin(event: (offset: number, state: ScrollState) => { offsetRemain: number; }): this { 577 throw new Error('Method not implemented.'); 578 } 579 childrenMainSize(value: ChildrenMainSize): this { 580 modifierWithKey(this._modifiersWithKeys, ListChildrenMainSizeModifier.identity, ListChildrenMainSizeModifier, value); 581 return this; 582 } 583} 584 585// @ts-ignore 586globalThis.List.attributeModifier = function (modifier: ArkComponent): void { 587 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 588 return new ArkListComponent(nativePtr); 589 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 590 return new modifierJS.ListModifier(nativePtr, classType); 591 }); 592}; 593