This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-js/src/widget/flowlayout.ts

151 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-12-04 14:15:05 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Stack } from './layouts'
import { Property, Superview, View, NativeViewModel } from '../ui/view'
2019-12-04 14:15:05 +08:00
import { layoutConfig } from '../util/index.util'
import { BridgeContext } from "../runtime/global";
2019-12-04 14:15:05 +08:00
export class FlowLayoutItem extends Stack {
2019-12-04 14:15:05 +08:00
/**
* Set to reuse native view
*/
@Property
identifier?: string
2021-10-11 16:26:56 +08:00
/**
* When set to true, the item will layout using all span area.
* LoadMoreView is default to true.
2021-10-11 16:26:56 +08:00
*/
@Property
fullSpan?: boolean
2019-12-04 14:15:05 +08:00
}
2020-01-04 19:13:15 +08:00
export class FlowLayout extends Superview {
2019-12-04 14:15:05 +08:00
private cachedViews: Map<string, FlowLayoutItem> = new Map
allSubviews() {
const ret = [...this.cachedViews.values()]
2019-12-13 14:32:59 +08:00
if (this.loadMoreView) {
ret.push(this.loadMoreView)
}
return ret
2019-12-04 14:15:05 +08:00
}
@Property
columnCount = 2
@Property
columnSpace?: number
@Property
rowSpace?: number
@Property
itemCount = 0
@Property
renderItem!: (index: number) => FlowLayoutItem
@Property
batchCount = 15
2019-12-13 14:32:59 +08:00
@Property
onLoadMore?: () => void
@Property
loadMore?: boolean
@Property
loadMoreView?: FlowLayoutItem
@Property
onScroll?: (offset: { x: number, y: number }) => void
@Property
onScrollEnd?: (offset: { x: number, y: number }) => void
2021-04-23 17:47:11 +08:00
@Property
scrollable?: boolean
2021-04-30 14:47:34 +08:00
/**
* Take effect only on iOS
*/
@Property
bounces?: boolean
2021-10-11 18:02:56 +08:00
/**
* @param context
* @returns Returns the range of the visible views for each column.
*/
findVisibleItems(context: BridgeContext) {
return this.nativeChannel(context, 'findVisibleItems')() as Promise<{ first: number, last: number }[]>
}
/**
* @param context
* @returns Returns the range of the completely visible views for each column.
*/
findCompletelyVisibleItems(context: BridgeContext) {
return this.nativeChannel(context, 'findCompletelyVisibleItems')() as Promise<{ first: number, last: number }[]>
}
2019-12-04 14:15:05 +08:00
reset() {
this.cachedViews.clear()
this.itemCount = 0
}
private getItem(itemIdx: number) {
let view = this.renderItem(itemIdx)
view.superview = this
this.cachedViews.set(`${itemIdx}`, view)
2019-12-04 14:15:05 +08:00
return view
}
private renderBunchedItems(start: number, length: number) {
return new Array(Math.min(length, this.itemCount - start)).fill(0).map((_, idx) => {
const listItem = this.getItem(start + idx)
return listItem.toModel()
})
}
2019-12-13 14:32:59 +08:00
2020-01-03 16:35:04 +08:00
toModel(): NativeViewModel {
2019-12-13 14:32:59 +08:00
if (this.loadMoreView) {
this.dirtyProps['loadMoreView'] = this.loadMoreView.viewId
}
return super.toModel()
}
2019-12-04 14:15:05 +08:00
}
export function flowlayout(config: Partial<FlowLayout>) {
2019-12-04 14:15:05 +08:00
const ret = new FlowLayout
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}
export function flowItem(item: View | View[], config?: Partial<FlowLayoutItem>) {
2019-12-04 14:15:05 +08:00
return (new FlowLayoutItem).also((it) => {
2019-12-14 16:24:58 +08:00
it.layoutConfig = layoutConfig().fit()
2020-01-04 19:13:15 +08:00
if (item instanceof View) {
it.addChild(item)
} else {
item.forEach(e => {
it.addChild(e)
})
}
if (config) {
2021-08-31 12:49:47 +08:00
it.apply(config)
2020-01-04 19:13:15 +08:00
}
2019-12-04 14:15:05 +08:00
})
}