This commit is contained in:
pengfei.zhou
2019-12-21 21:37:51 +08:00
parent b5cb29ca03
commit 9b5fbedfca
47 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,129 @@
/*
* 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 { Stack } from './layouts'
import { Property, IView, Superview, View } from '../ui/view'
import { layoutConfig } from '../util/index.util'
export class FlowLayoutItem extends Stack {
/**
* Set to reuse native view
*/
@Property
identifier?: string
}
export interface IFlowLayout extends IView {
renderItem: (index: number) => FlowLayoutItem
itemCount: number
batchCount?: number
columnCount?: number
columnSpace?: number
rowSpace?: number
}
export class FlowLayout extends Superview implements IFlowLayout {
private cachedViews: Map<string, FlowLayoutItem> = new Map
private ignoreDirtyCallOnce = false
allSubviews() {
if (this.loadMoreView) {
return [...this.cachedViews.values(), this.loadMoreView]
} else {
return this.cachedViews.values()
}
}
@Property
columnCount = 2
@Property
columnSpace?: number
@Property
rowSpace?: number
@Property
itemCount = 0
@Property
renderItem!: (index: number) => FlowLayoutItem
@Property
batchCount = 15
@Property
onLoadMore?: () => void
@Property
loadMore?: boolean
@Property
loadMoreView?: FlowLayoutItem
reset() {
this.cachedViews.clear()
this.itemCount = 0
}
private getItem(itemIdx: number) {
let view = this.renderItem(itemIdx)
view.superview = this
this.cachedViews.set(`${itemIdx}`, view)
return view
}
isDirty() {
if (this.ignoreDirtyCallOnce) {
this.ignoreDirtyCallOnce = false
//Ignore the dirty call once.
return false
}
return super.isDirty()
}
private renderBunchedItems(start: number, length: number) {
this.ignoreDirtyCallOnce = true;
return new Array(Math.min(length, this.itemCount - start)).fill(0).map((_, idx) => {
const listItem = this.getItem(start + idx)
return listItem.toModel()
})
}
toModel() {
if (this.loadMoreView) {
this.dirtyProps['loadMoreView'] = this.loadMoreView.viewId
}
return super.toModel()
}
}
export function flowlayout(config: IFlowLayout) {
const ret = new FlowLayout
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}
export function flowItem(item: View) {
return (new FlowLayoutItem).also((it) => {
it.layoutConfig = layoutConfig().fit()
it.addChild(item)
})
}

View File

@@ -0,0 +1,54 @@
/*
* 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 { IView, View, Property } from "../ui/view"
import { layoutConfig } from "../util/layoutconfig"
export enum ScaleType {
ScaleToFill = 0,
ScaleAspectFit,
ScaleAspectFill,
}
export interface IImage extends IView {
imageUrl?: string
imageBase64?: string
scaleType?: ScaleType
isBlur?: boolean
loadCallback?: (image: { width: number; height: number } | undefined) => void
}
export class Image extends View implements IImage {
@Property
imageUrl?: string
@Property
imageBase64?: string
@Property
scaleType?: ScaleType
@Property
isBlur?: boolean
@Property
loadCallback?: (image: { width: number; height: number } | undefined) => void
}
export function image(config: IImage) {
const ret = new Image
ret.layoutConfig = layoutConfig().fit()
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}

View File

@@ -0,0 +1,25 @@
/*
* 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.
*/
export * from './layouts'
export * from './text'
export * from './image'
export * from './list'
export * from './slider'
export * from './scroller'
export * from './refreshable'
export * from './flowlayout'
export * from './input'
export * from './nestedSlider'

View File

@@ -0,0 +1,80 @@
/*
* 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 { View, IView, Property } from "../ui/view";
import { Color } from "../util/color";
import { Gravity } from "../util/gravity";
import { BridgeContext } from "../runtime/global";
export interface IInput extends IView {
text?: string
textColor?: Color
textSize?: number
hintText?: string
hintTextColor?: Color
multilines?: boolean
textAlignment?: Gravity
onTextChange?: (text: string) => void
onFocusChange?: (focused: boolean) => void
}
export class Input extends View implements IInput {
@Property
text?: string
@Property
textColor?: Color
@Property
textSize?: number
@Property
hintText?: string
@Property
hintTextColor?: Color
@Property
multiline?: boolean
@Property
textAlignment?: Gravity
@Property
onTextChange?: (text: string) => void
@Property
onFocusChange?: (focused: boolean) => void
getText(context: BridgeContext) {
return this.nativeChannel(context, 'getText')() as Promise<string>
}
setSelection(context: BridgeContext, start: number, end: number = start) {
return this.nativeChannel(context, 'setSelection')({
start,
end,
}) as Promise<string>
}
requestFocus(context: BridgeContext) {
return this.nativeChannel(context, 'requestFocus')()
}
releaseFocus(context: BridgeContext) {
return this.nativeChannel(context, 'releaseFocus')()
}
}

View File

@@ -0,0 +1,79 @@
/*
* 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 { Group, Property, IView, View } from "../ui/view";
import { Gravity } from "../util/gravity";
import { layoutConfig } from "../util/layoutconfig";
export interface IStack extends IView {
}
export class Stack extends Group implements IStack {
}
export class Root extends Stack {
}
class LinearLayout extends Group {
@Property
space?: number
@Property
gravity?: Gravity
}
export interface IVLayout extends IView {
space?: number
gravity?: Gravity
}
export class VLayout extends LinearLayout implements VLayout {
}
export interface IHLayout extends IView {
space?: number
gravity?: Gravity
}
export class HLayout extends LinearLayout implements IHLayout {
}
export function stack(views: View[]) {
const ret = new Stack
ret.layoutConfig = layoutConfig().fit()
for (let v of views) {
ret.addChild(v)
}
return ret
}
export function hlayout(views: View[]) {
const ret = new HLayout
ret.layoutConfig = layoutConfig().fit()
for (let v of views) {
ret.addChild(v)
}
return ret
}
export function vlayout(views: View[]) {
const ret = new VLayout
ret.layoutConfig = layoutConfig().fit()
for (let v of views) {
ret.addChild(v)
}
return ret
}

117
doric-js/src/widget/list.ts Normal file
View File

@@ -0,0 +1,117 @@
/*
* 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 { View, Property, Superview, IView } from "../ui/view";
import { Stack } from "./layouts";
import { layoutConfig, LayoutSpec } from "../util/layoutconfig";
export class ListItem extends Stack {
/**
* Set to reuse native view
*/
@Property
identifier?: string
}
export interface IList extends IView {
renderItem: (index: number) => ListItem
itemCount: number
batchCount?: number
}
export class List extends Superview implements IList {
private cachedViews: Map<string, ListItem> = new Map
private ignoreDirtyCallOnce = false
allSubviews() {
if (this.loadMoreView) {
return [...this.cachedViews.values(), this.loadMoreView]
} else {
return this.cachedViews.values()
}
}
@Property
itemCount = 0
@Property
renderItem!: (index: number) => ListItem
@Property
batchCount = 15
@Property
onLoadMore?: () => void
@Property
loadMore?: boolean
@Property
loadMoreView?: ListItem
reset() {
this.cachedViews.clear()
this.itemCount = 0
}
private getItem(itemIdx: number) {
let view = this.cachedViews.get(`${itemIdx}`)
if (view === undefined) {
view = this.renderItem(itemIdx)
view.superview = this
this.cachedViews.set(`${itemIdx}`, view)
}
return view
}
isDirty() {
if (this.ignoreDirtyCallOnce) {
this.ignoreDirtyCallOnce = false
//Ignore the dirty call once.
return false
}
return super.isDirty()
}
private renderBunchedItems(start: number, length: number) {
this.ignoreDirtyCallOnce = true;
return new Array(Math.min(length, this.itemCount - start)).fill(0).map((_, idx) => {
const listItem = this.getItem(start + idx)
return listItem.toModel()
})
}
toModel() {
if (this.loadMoreView) {
this.dirtyProps['loadMoreView'] = this.loadMoreView.viewId
}
return super.toModel()
}
}
export function list(config: IList) {
const ret = new List
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}
export function listItem(item: View) {
return (new ListItem).also((it) => {
it.layoutConfig = layoutConfig().most().configHeight(LayoutSpec.FIT)
it.addChild(item)
})
}

View File

@@ -0,0 +1,35 @@
/*
* 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 { Group, View, Property } from '../ui/view'
import { BridgeContext } from '../runtime/global'
export class NestedSlider extends Group {
@Property
onPageSlided?: (index: number) => void
addSlideItem(view: View) {
this.addChild(view)
}
slidePage(context: BridgeContext, page: number, smooth = false) {
return this.nativeChannel(context, "slidePage")({ page, smooth })
}
getSlidedPage(context: BridgeContext) {
return this.nativeChannel(context, "getSlidedPage")() as Promise<number>
}
}

View File

@@ -0,0 +1,74 @@
import { View, Property, Superview, IView } 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() {
this.dirtyProps.content = this.content.viewId
this.dirtyProps.header = (this.header || {}).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
}

View File

@@ -0,0 +1,41 @@
/*
* 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, IView } from '../ui/view'
import { layoutConfig } from '../util/layoutconfig'
export function scroller(content: View) {
return (new Scroller).also(v => {
v.layoutConfig = layoutConfig().fit()
v.content = content
})
}
export interface IScroller extends IView {
content: View
}
export class Scroller extends Superview implements IScroller {
content!: View
allSubviews() {
return [this.content]
}
toModel() {
this.dirtyProps.content = this.content.viewId
return super.toModel()
}
}

View File

@@ -0,0 +1,91 @@
import { Superview, View, Property, IView } from "../ui/view";
import { Stack } from "./layouts";
import { layoutConfig } from "../util/layoutconfig";
import { BridgeContext } from "../runtime/global";
export class SlideItem extends Stack {
/**
* Set to reuse native view
*/
@Property
identifier?: string
}
export interface ISlider extends IView {
renderPage: (index: number) => SlideItem
itemCount: number
batchCount?: number
onPageSlided?: (index: number) => void
}
export class Slider extends Superview implements ISlider {
private cachedViews: Map<string, SlideItem> = new Map
private ignoreDirtyCallOnce = false
allSubviews() {
return this.cachedViews.values()
}
@Property
itemCount = 0
@Property
renderPage!: (index: number) => SlideItem
@Property
batchCount = 3
@Property
onPageSlided?: (index: number) => void
private getItem(itemIdx: number) {
let view = this.cachedViews.get(`${itemIdx}`)
if (view === undefined) {
view = this.renderPage(itemIdx)
view.superview = this
this.cachedViews.set(`${itemIdx}`, view)
}
return view
}
isDirty() {
if (this.ignoreDirtyCallOnce) {
this.ignoreDirtyCallOnce = false
//Ignore the dirty call once.
return false
}
return super.isDirty()
}
private renderBunchedItems(start: number, length: number) {
this.ignoreDirtyCallOnce = true;
return new Array(Math.min(length, this.itemCount - start)).fill(0).map((_, idx) => {
const slideItem = this.getItem(start + idx)
return slideItem.toModel()
})
}
slidePage(context: BridgeContext, page: number, smooth = false) {
return this.nativeChannel(context, "slidePage")({ page, smooth })
}
getSlidedPage(context: BridgeContext) {
return this.nativeChannel(context, "getSlidedPage")() as Promise<number>
}
}
export function slideItem(item: View) {
return (new SlideItem).also((it) => {
it.layoutConfig = layoutConfig().fit()
it.addChild(item)
})
}
export function slider(config: ISlider) {
const ret = new Slider
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}

View File

@@ -0,0 +1,53 @@
/*
* 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 { IView, View, Property } from "../ui/view"
import { Color } from "../util/color"
import { Gravity } from "../util/gravity"
import { layoutConfig } from "../util/layoutconfig"
export interface IText extends IView {
text?: string
textColor?: Color
textSize?: number
maxLines?: number
textAlignment?: Gravity
}
export class Text extends View implements IText {
@Property
text?: string
@Property
textColor?: Color
@Property
textSize?: number
@Property
maxLines?: number
@Property
textAlignment?: Gravity
}
export function text(config: IText) {
const ret = new Text
ret.layoutConfig = layoutConfig().fit()
for (let key in config) {
Reflect.set(ret, key, Reflect.get(config, key, config), ret)
}
return ret
}