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