1/* 2 * Copyright (c) 2023-2024 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/** 17 * SDK API of Repeat and its attribute functions. 18 */ 19 20interface RepeatItem<T> { 21 readonly item: T, 22 readonly index?: number 23} 24 25interface ItemDragEventHandler { 26 onLongPress?: (index: number) => void; 27 onDragStart?: (index: number) => void; 28 onMoveThrough?: (from: number, to: number) => void; 29 onDrop?: (index: number) => void; 30} 31 32type RepeatItemGenFunc<T> = (i: RepeatItem<T>) => void; 33type RepeatTTypeGenFunc<T> = (item: T, index: number) => string; 34type RepeatKeyGenFunc<T> = (item: T, index?: number) => string; 35type RepeatTemplateOptions = { cachedCount?: number }; 36type RepeatTemplateImplOptions = { cachedCountSpecified: boolean, cachedCount?: number }; 37type OnMoveHandler = (from: number, to: number) => void; 38 39/* 40 global function Repeat() 41 returns an object that retains the state of Repeat instance between render calls 42 exec attribute functions on this instance. 43*/ 44const Repeat: <T>(arr: Array<T>, owningView?: PUV2ViewBase) => RepeatAPI<T> = 45 <T>(arr: Array<T>, owningView?: PUV2ViewBase): RepeatAPI<T> => { 46 if (!owningView) { 47 throw new Error("Transpilation error, Repeat lacks 2nd parameter owningView"); 48 } 49 return owningView!.__mkRepeatAPI(arr); 50 }; 51 52/* 53 repeat attribute function and internal function render() 54*/ 55interface RepeatAPI<T> { 56 // unique key from array item, to identify changed, added array items 57 key: (keyGenFunc: RepeatKeyGenFunc<T>) => RepeatAPI<T>; 58 59 // default render function for data items, framework will use when no template found 60 each: (itemGenFunc: RepeatItemGenFunc<T>) => RepeatAPI<T>; 61 62 // when call virtualScroll, framework will use virtual scroll 63 // totalCount: number of logical items, can be larger than number of loaded 64 // data items of lazy loading array source, can be larger than that array.length 65 virtualScroll: (options?: { 66 totalCount?: number, 67 onTotalCount?(): number, 68 onLazyLoading?(index: number): void, 69 reusable?: boolean 70 }) => RepeatAPI<T>; 71 72 // function to decide which template to use, each template has an id 73 templateId: (typeFunc: RepeatTTypeGenFunc<T>) => RepeatAPI<T>; 74 75 // template: id + builder function to render specific type of data item 76 template: (type: string, itemGenFunc: RepeatItemGenFunc<T>, options?: RepeatTemplateOptions) => RepeatAPI<T>; 77 78 // do NOT use in app, transpiler adds as last of chained call 79 render(isInitialRender: boolean): void; 80 81 // not used by Repeat 82 onMove: (handler: OnMoveHandler, eventHandler: ItemDragEventHandler) => RepeatAPI<T>; 83} 84