/* * 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 { Superview, View, Property, InconsistProperty } from "../ui/view"; import { Stack } from "./layouts"; import { layoutConfig } from "../util/layoutconfig"; import { BridgeContext } from "../runtime/global"; import { deepClone } from "./utils"; export class SlideItem extends Stack { /** * Set to reuse native view */ @Property identifier?: string } export class Slider extends Superview { private cachedViews: Map = new Map allSubviews() { return this.cachedViews.values() } @Property itemCount = 0 @Property renderPage!: (index: number) => SlideItem @Property batchCount = 3 @Property onPageSlided?: (index: number) => void @Property loop?: boolean @Property scrollable?: boolean /** * Take effect only on iOS */ @Property bounces?: boolean /** * Take effect only on iOS */ @Property scrollsToTop?: boolean /** * Set the effect when sliding * ZoomOut is currently supported */ @Property slideStyle?: "zoomOut" | { type: "zoomOut", minScale: number, maxScale: number } | { type: "gallery", itemWidth: number, minScale: number, minAlpha: number } @InconsistProperty slidePosition?: number /** * Reload all list items. * @param context * @returns */ reload(context: BridgeContext) { return this.nativeChannel(context, 'reload')() as Promise } reset() { this.cachedViews.clear() this.itemCount = 0 } private getItem(itemIdx: number) { let view = this.renderPage(itemIdx) view.superview = this this.cachedViews.set(`${itemIdx}`, view) return view } private renderBunchedItems(start: number, length: number) { const items = new Array(Math.max(0, Math.min(length, this.itemCount - start))) .fill(0).map((_, idx) => this.getItem(start + idx)) const ret = items.map(e => deepClone(e.toModel())) items.forEach(e => e.clean()) return ret } slidePage(context: BridgeContext, page: number, smooth = false) { return this.nativeChannel(context, "slidePage")({ page, smooth }) } getSlidedPage(context: BridgeContext) { return this.nativeChannel(context, "getSlidedPage")() as Promise } } export function slider(config: Partial) { const ret = new Slider ret.apply(config) return ret } export function slideItem(item: View | View[], config?: Partial) { return (new SlideItem).also((it) => { it.layoutConfig = layoutConfig().most() 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) } } }) }