2020-03-13 13:01:21 +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.
|
|
|
|
*/
|
2020-04-16 19:21:24 +08:00
|
|
|
import { Superview, View, Property } from "../ui/view";
|
|
|
|
import { Stack } from "./layouts";
|
2019-12-04 14:15:05 +08:00
|
|
|
import { layoutConfig } from "../util/layoutconfig";
|
2019-12-06 15:45:53 +08:00
|
|
|
import { BridgeContext } from "../runtime/global";
|
2019-12-04 14:15:05 +08:00
|
|
|
|
2020-01-04 19:13:15 +08:00
|
|
|
|
|
|
|
|
2020-04-16 19:21:24 +08:00
|
|
|
export class SlideItem extends Stack {
|
2019-12-04 14:15:05 +08:00
|
|
|
/**
|
|
|
|
* Set to reuse native view
|
|
|
|
*/
|
|
|
|
@Property
|
|
|
|
identifier?: string
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:21:24 +08:00
|
|
|
export class Slider extends Superview {
|
2019-12-04 14:15:05 +08:00
|
|
|
private cachedViews: Map<string, SlideItem> = new Map
|
|
|
|
|
|
|
|
allSubviews() {
|
|
|
|
return this.cachedViews.values()
|
|
|
|
}
|
|
|
|
@Property
|
|
|
|
itemCount = 0
|
|
|
|
|
|
|
|
@Property
|
|
|
|
renderPage!: (index: number) => SlideItem
|
|
|
|
|
|
|
|
@Property
|
|
|
|
batchCount = 3
|
|
|
|
|
2019-12-06 15:45:53 +08:00
|
|
|
@Property
|
|
|
|
onPageSlided?: (index: number) => void
|
2019-12-04 14:15:05 +08:00
|
|
|
|
2020-04-09 16:58:47 +08:00
|
|
|
@Property
|
|
|
|
loop?: boolean
|
|
|
|
|
2021-04-23 17:55:59 +08:00
|
|
|
@Property
|
|
|
|
scrollable?: boolean
|
|
|
|
|
2019-12-04 14:15:05 +08:00
|
|
|
private getItem(itemIdx: number) {
|
2020-04-09 16:58:47 +08:00
|
|
|
let view = this.renderPage(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 slideItem = this.getItem(start + idx)
|
|
|
|
return slideItem.toModel()
|
|
|
|
})
|
|
|
|
}
|
2019-12-06 15:45:53 +08:00
|
|
|
|
|
|
|
slidePage(context: BridgeContext, page: number, smooth = false) {
|
2019-12-06 15:53:31 +08:00
|
|
|
return this.nativeChannel(context, "slidePage")({ page, smooth })
|
2019-12-06 15:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getSlidedPage(context: BridgeContext) {
|
2019-12-06 15:53:31 +08:00
|
|
|
return this.nativeChannel(context, "getSlidedPage")() as Promise<number>
|
2019-12-06 15:45:53 +08:00
|
|
|
}
|
|
|
|
|
2019-12-04 14:15:05 +08:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:21:24 +08:00
|
|
|
export function slider(config: Partial<Slider>) {
|
2019-12-04 14:15:05 +08:00
|
|
|
const ret = new Slider
|
|
|
|
for (let key in config) {
|
|
|
|
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
|
|
|
|
}
|
|
|
|
return ret
|
2020-01-04 19:13:15 +08:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:21:24 +08:00
|
|
|
export function slideItem(item: View | View[], config?: Partial<SlideItem>) {
|
2020-01-04 19:13:15 +08:00
|
|
|
return (new SlideItem).also((it) => {
|
2020-04-07 10:58:44 +08:00
|
|
|
it.layoutConfig = layoutConfig().most()
|
2020-01-04 19:13:15 +08:00
|
|
|
if (item instanceof View) {
|
|
|
|
it.addChild(item)
|
|
|
|
} else {
|
|
|
|
item.forEach(e => {
|
|
|
|
it.addChild(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (config) {
|
|
|
|
for (let key in config) {
|
|
|
|
Reflect.set(it, key, Reflect.get(config, key, config), it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-04 14:15:05 +08:00
|
|
|
}
|