From 84f12bd964e8e4d4d230462c6f105f13efcdb813 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Thu, 28 Nov 2019 10:51:46 +0800 Subject: [PATCH] feat:split native module ts --- js-framework/index.ts | 2 +- js-framework/src/native/index.native.ts | 20 ++++ js-framework/src/native/modal.ts | 62 ++++++++++ js-framework/src/native/navbar.ts | 47 ++++++++ js-framework/src/native/navigator.ts | 29 +++++ .../nativeModules.ts => native/network.ts} | 108 +----------------- js-framework/src/native/storage.ts | 33 ++++++ js-framework/src/ui/panel.ts | 8 ++ 8 files changed, 202 insertions(+), 107 deletions(-) create mode 100644 js-framework/src/native/index.native.ts create mode 100644 js-framework/src/native/modal.ts create mode 100644 js-framework/src/native/navbar.ts create mode 100644 js-framework/src/native/navigator.ts rename js-framework/src/{util/nativeModules.ts => native/network.ts} (57%) create mode 100644 js-framework/src/native/storage.ts diff --git a/js-framework/index.ts b/js-framework/index.ts index 6fa48cae..e6b99b6a 100644 --- a/js-framework/index.ts +++ b/js-framework/index.ts @@ -29,4 +29,4 @@ export * from './src/util/gravity' export * from './src/util/candies' export * from './src/vm/mvvm' export * from './src/runtime/global' -export * from './src/util/nativeModules' +export * from './src/native/index.native' diff --git a/js-framework/src/native/index.native.ts b/js-framework/src/native/index.native.ts new file mode 100644 index 00000000..372f9d1b --- /dev/null +++ b/js-framework/src/native/index.native.ts @@ -0,0 +1,20 @@ +/* + * 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 './modal' +export * from './navbar' +export * from './navigator' +export * from './network' +export * from './storage' diff --git a/js-framework/src/native/modal.ts b/js-framework/src/native/modal.ts new file mode 100644 index 00000000..5d56541d --- /dev/null +++ b/js-framework/src/native/modal.ts @@ -0,0 +1,62 @@ + +/* + * 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 { BridgeContext } from "../runtime/global" +import { Gravity } from "../util/gravity" + +export function modal(context: BridgeContext) { + return { + toast: (msg: string, gravity: Gravity = Gravity.Bottom) => { + context.modal.toast({ + msg, + gravity: gravity.toModel(), + }) + }, + alert: (arg: string | { + title: string, + msg: string, + okLabel?: string, + }) => { + if (typeof arg === 'string') { + return context.modal.alert({ msg: arg }) + } else { + return context.modal.alert(arg) + } + }, + confirm: (arg: string | { + title: string, + msg: string, + okLabel?: string, + cancelLabel?: string, + }) => { + if (typeof arg === 'string') { + return context.modal.confirm({ msg: arg }) + } else { + return context.modal.confirm(arg) + } + }, + prompt: (arg: { + title?: string, + msg?: string, + okLabel?: string, + cancelLabel?: string, + text?: string, + defaultText?: string, + }) => { + return context.modal.prompt(arg) as Promise + }, + } +} \ No newline at end of file diff --git a/js-framework/src/native/navbar.ts b/js-framework/src/native/navbar.ts new file mode 100644 index 00000000..bb6890a6 --- /dev/null +++ b/js-framework/src/native/navbar.ts @@ -0,0 +1,47 @@ +/* + * 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 { BridgeContext } from "../runtime/global" +import { Panel } from "../ui/panel" +import { Color } from "../util/color" + +export function navbar(context: BridgeContext) { + const entity = context.entity + let panel: Panel | undefined = undefined + if (entity instanceof Panel) { + panel = entity + } + + return { + isHidden: () => { + return context.navbar.isHidden() as Promise + }, + setHidden: (hidden: boolean) => { + return context.navbar.setHidden({ + hidden, + }) + }, + setTitle: (title: string) => { + return context.navbar.setTitle({ + title, + }) + }, + setBgColor: (color: Color) => { + return context.navbar.setBgColor({ + color: color.toModel(), + }) + }, + } +} \ No newline at end of file diff --git a/js-framework/src/native/navigator.ts b/js-framework/src/native/navigator.ts new file mode 100644 index 00000000..6e849613 --- /dev/null +++ b/js-framework/src/native/navigator.ts @@ -0,0 +1,29 @@ +/* + * 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 { BridgeContext } from "../runtime/global" + +export function navigator(context: BridgeContext) { + return { + push: (scheme: string, alias: string, animated = true) => { + return context.navigator.push({ + scheme, alias, animated + }) + }, + pop: (animated = true) => { + return context.navigator.pop({ animated }) + }, + } +} \ No newline at end of file diff --git a/js-framework/src/util/nativeModules.ts b/js-framework/src/native/network.ts similarity index 57% rename from js-framework/src/util/nativeModules.ts rename to js-framework/src/native/network.ts index bd037cc5..8b688c87 100644 --- a/js-framework/src/util/nativeModules.ts +++ b/js-framework/src/native/network.ts @@ -13,54 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { BridgeContext } from "../runtime/global"; -import { Gravity } from "./gravity"; -import { Panel } from "../ui/panel"; -import { Color } from "./color"; +import { BridgeContext } from "../runtime/global" -export function modal(context: BridgeContext) { - return { - toast: (msg: string, gravity: Gravity = Gravity.Bottom) => { - context.modal.toast({ - msg, - gravity: gravity.toModel(), - }) - }, - alert: (arg: string | { - title: string, - msg: string, - okLabel?: string, - }) => { - if (typeof arg === 'string') { - return context.modal.alert({ msg: arg }) - } else { - return context.modal.alert(arg) - } - }, - confirm: (arg: string | { - title: string, - msg: string, - okLabel?: string, - cancelLabel?: string, - }) => { - if (typeof arg === 'string') { - return context.modal.confirm({ msg: arg }) - } else { - return context.modal.confirm(arg) - } - }, - prompt: (arg: { - title?: string, - msg?: string, - okLabel?: string, - cancelLabel?: string, - text?: string, - defaultText?: string, - }) => { - return context.modal.prompt(arg) as Promise - }, - } -} export interface IRequest { // `url` is the server URL that will be used for the request url?: string, @@ -103,6 +57,7 @@ function transformRequest(request: IRequest) { } return request } + export function network(context: BridgeContext) { return { request: (config: IRequest) => { @@ -151,63 +106,4 @@ export function network(context: BridgeContext) { return context.network.request(transformRequest(finalConfig)) as Promise }, } -} - -export function storage(context: BridgeContext) { - return { - setItem: (key: string, value: string, zone?: string) => { - return context.storage.setItem({ key, value, zone }) - }, - getItem: (key: string, zone?: string) => { - return context.storage.getItem({ key, zone }) as Promise - }, - remove: (key: string, zone?: string) => { - return context.storage.remove({ key, zone }) - }, - clear: (zone: string) => { - return context.storage.clear({ zone }) - }, - } -} - -export function navigator(context: BridgeContext) { - return { - push: (scheme: string, alias: string, animated = true) => { - return context.navigator.push({ - scheme, alias, animated - }) - }, - pop: (animated = true) => { - return context.navigator.pop({ animated }) - }, - } -} - -export function navbar(context: BridgeContext) { - const entity = context.entity - let panel: Panel | undefined = undefined - if (entity instanceof Panel) { - panel = entity - } - - return { - isHidden: () => { - return context.navbar.isHidden() as Promise - }, - setHidden: (hidden: boolean) => { - return context.navbar.setHidden({ - hidden, - }) - }, - setTitle: (title: string) => { - return context.navbar.setTitle({ - title, - }) - }, - setBgColor: (color: Color) => { - return context.navbar.setBgColor({ - color: color.toModel(), - }) - }, - } } \ No newline at end of file diff --git a/js-framework/src/native/storage.ts b/js-framework/src/native/storage.ts new file mode 100644 index 00000000..dda42379 --- /dev/null +++ b/js-framework/src/native/storage.ts @@ -0,0 +1,33 @@ +/* + * 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 { BridgeContext } from "../runtime/global" + +export function storage(context: BridgeContext) { + return { + setItem: (key: string, value: string, zone?: string) => { + return context.storage.setItem({ key, value, zone }) + }, + getItem: (key: string, zone?: string) => { + return context.storage.getItem({ key, zone }) as Promise + }, + remove: (key: string, zone?: string) => { + return context.storage.remove({ key, zone }) + }, + clear: (zone: string) => { + return context.storage.clear({ zone }) + }, + } +} \ No newline at end of file diff --git a/js-framework/src/ui/panel.ts b/js-framework/src/ui/panel.ts index 32115c37..f0ddb530 100644 --- a/js-framework/src/ui/panel.ts +++ b/js-framework/src/ui/panel.ts @@ -50,6 +50,14 @@ export abstract class Panel { this.headviews.set(v.viewId, v) } + removeHeadView(v: View | string) { + if (v instanceof View) { + this.headviews.delete(v.viewId) + } else { + this.headviews.delete(v) + } + } + getRootView() { return this.__root__ }