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

74 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-01-03 16:35:04 +08:00
import { View, Property, Superview, IView, NativeViewModel } from "../ui/view";
2019-12-04 14:15:05 +08:00
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>
}
2020-01-03 16:35:04 +08:00
toModel(): NativeViewModel {
2019-12-04 14:15:05 +08:00
this.dirtyProps.content = this.content.viewId
2020-01-03 13:35:40 +08:00
this.dirtyProps.header = ((this.header || {}) as any).viewId
2019-12-04 14:15:05 +08:00
return super.toModel()
}
}
export function refreshable(config: IRefreshable) {
const ret = new Refreshable
2019-12-14 16:24:58 +08:00
ret.layoutConfig = layoutConfig().fit()
2019-12-04 14:15:05 +08:00
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
}
2019-12-04 18:14:22 +08:00
export function pullable(v: View, config: IPullable) {
2019-12-04 14:15:05 +08:00
Reflect.set(v, 'startAnimation', config.startAnimation)
Reflect.set(v, 'stopAnimation', config.stopAnimation)
Reflect.set(v, 'setPullingDistance', config.setPullingDistance)
return v
}