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/refreshable.ts
pengfei.zhou 680b95f05d update js
2020-01-03 17:05:24 +08:00

74 lines
2.0 KiB
TypeScript

import { View, Property, Superview, IView, NativeViewModel } from "../ui/view";
import { List } from "./list";
import { Scroller } from "./scroller";
import { BridgeContext } from "../runtime/global";
import { layoutConfig } from "../util/layoutconfig";
export interface IRefreshable extends IView {
content: View
header?: View
onRefresh?: () => void
}
export class Refreshable extends Superview implements IRefreshable {
content!: List | Scroller
header?: View
@Property
onRefresh?: () => void
allSubviews() {
const ret: View[] = [this.content]
if (this.header) {
ret.push(this.header)
}
return ret
}
setRefreshable(context: BridgeContext, refreshable: boolean) {
return this.nativeChannel(context, 'setRefreshable')(refreshable)
}
setRefreshing(context: BridgeContext, refreshing: boolean) {
return this.nativeChannel(context, 'setRefreshing')(refreshing)
}
isRefreshable(context: BridgeContext) {
return this.nativeChannel(context, 'isRefreshable')() as Promise<boolean>
}
isRefreshing(context: BridgeContext) {
return this.nativeChannel(context, 'isRefreshing')() as Promise<boolean>
}
toModel(): NativeViewModel {
this.dirtyProps.content = this.content.viewId
this.dirtyProps.header = ((this.header || {}) as any).viewId
return super.toModel()
}
}
export function refreshable(config: IRefreshable) {
const ret = new Refreshable
ret.layoutConfig = layoutConfig().fit()
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}
export interface IPullable {
startAnimation(): void
stopAnimation(): void
setPullingDistance(distance: number): void
}
export function pullable(v: View, config: IPullable) {
Reflect.set(v, 'startAnimation', config.startAnimation)
Reflect.set(v, 'stopAnimation', config.stopAnimation)
Reflect.set(v, 'setPullingDistance', config.setPullingDistance)
return v
}