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/js-framework/demo.ts

119 lines
2.4 KiB
TypeScript
Raw Normal View History

2019-07-24 19:58:30 +08:00
import { StackConfig, ViewHolder, VMPanel, View, ViewModel, WRAP_CONTENT, Gravity, Mutable, NativeCall, Text, Color, VLayout, Panel, log, logw, loge, Group, Stack, } from "./index"
2019-07-24 19:26:25 +08:00
2019-07-24 16:38:34 +08:00
interface CountModel {
count: number
2019-07-24 17:24:00 +08:00
add: () => void
2019-07-24 16:38:34 +08:00
}
2019-07-18 20:11:01 +08:00
2019-07-24 19:26:25 +08:00
class CounterView extends ViewHolder {
number = new Text
counter = new Text
build(root: Group) {
2019-07-24 16:38:34 +08:00
const vlayout = new VLayout
2019-07-24 19:26:25 +08:00
this.number.textSize = 40
this.number.layoutConfig = {
2019-07-24 16:38:34 +08:00
alignment: new Gravity().center()
2019-07-23 13:01:45 +08:00
}
2019-07-24 19:26:25 +08:00
this.counter = new Text
this.counter.text = "点击计数"
this.counter.textSize = 20
2019-07-24 11:43:38 +08:00
2019-07-24 16:38:34 +08:00
vlayout.space = 20
vlayout.layoutConfig = {
alignment: new Gravity().center()
2019-07-24 11:43:38 +08:00
}
2019-07-24 19:26:25 +08:00
vlayout.addChild(this.number)
vlayout.addChild(this.counter)
2019-07-24 17:24:00 +08:00
2019-07-24 16:38:34 +08:00
root.addChild(vlayout)
2019-07-24 19:26:25 +08:00
}
setNumber(n: number) {
this.number.text = n.toString()
}
2019-07-24 11:43:38 +08:00
2019-07-24 19:26:25 +08:00
setCounter(v: Function) {
this.counter.onClick = v
}
}
2019-07-24 10:14:17 +08:00
2019-07-24 19:26:25 +08:00
class CounterVM extends ViewModel<CountModel, CounterView> {
binding(v: CounterView, model: CountModel): void {
v.setCounter(model.add)
v.setNumber(model.count)
2019-07-24 16:38:34 +08:00
}
}
2019-07-24 19:26:25 +08:00
class MyPage extends VMPanel<CountModel, CounterView>{
2019-07-24 16:38:34 +08:00
2019-07-24 19:26:25 +08:00
getVMClass() {
return CounterVM
}
getModel() {
return {
2019-07-24 17:24:00 +08:00
count: 0,
add: function () {
this.count++
2019-07-24 19:26:25 +08:00
},
}
2019-07-18 16:29:24 +08:00
}
2019-07-22 14:36:32 +08:00
2019-07-24 19:26:25 +08:00
getViewHolder() {
return new CounterView
}
2019-07-22 13:41:38 +08:00
@NativeCall
2019-07-18 16:29:24 +08:00
log() {
2019-07-22 13:41:38 +08:00
log("Hello.HEGO")
logw("Hello.HEGO")
loge("Hello.HEGO")
2019-07-18 16:29:24 +08:00
}
2019-07-24 19:58:30 +08:00
}
class Snake {
}
class SnakeView extends ViewHolder {
build(root: Group): void {
root.bgColor = Color.parse('#000000')
const title = new Text
title.text = "Snake"
title.textSize = 20
title.textColor = Color.parse("#ffffff")
title.layoutConfig = {
alignment: new Gravity().centerX().top(),
margin: {
top: 20
}
} as StackConfig
root.addChild(title)
}
}
class SnakeVM extends ViewModel<Snake, SnakeView>{
binding(v: SnakeView, model: Snake) {
}
}
@Entry
class SnakePanel extends VMPanel<Snake, SnakeView>{
getVMClass() {
return SnakeVM
}
getModel() {
return new Snake
}
getViewHolder() {
return new SnakeView
}
2019-07-18 16:29:24 +08:00
}