js:add lib and .d.ts

This commit is contained in:
pengfei.zhou
2020-01-03 14:44:51 +08:00
committed by osborn
parent 624e90e4a8
commit 7cde16a75d
84 changed files with 3794 additions and 19 deletions

9
doric-js/lib/src/native/animate.d.ts vendored Normal file
View File

@@ -0,0 +1,9 @@
import { BridgeContext } from "../runtime/global";
/**
* Only supports x,y,width,height,corner(just for four corners),rotation,bgColor,
* @param panel @see Panel
*/
export declare function animate(context: BridgeContext): (args: {
animations: () => void;
duration: number;
}) => Promise<any>;

View File

@@ -0,0 +1,57 @@
/*
* 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 { Panel } from "../ui/panel";
import { takeLet } from "../pattern/candies";
/**
* Only supports x,y,width,height,corner(just for four corners),rotation,bgColor,
* @param panel @see Panel
*/
export function animate(context) {
const entity = context.entity;
if (entity instanceof Panel) {
let panel = entity;
return (args) => {
return takeLet(panel.context.animate)(it => {
return it.submit().then(() => {
args.animations();
return takeLet(panel.getRootView())(root => {
if (root.isDirty()) {
const model = root.toModel();
model.duration = args.duration;
const ret = it.animateRender(model);
root.clean();
return ret;
}
for (let v of panel.allHeadViews()) {
if (v.isDirty()) {
const model = v.toModel();
const ret = it.animateRender(model);
it.clean();
return ret;
}
}
throw new Error('Cannot find any animated elements');
});
});
});
};
}
else {
return (args) => {
return Promise.reject(`Cannot find panel in Context:${context.id}`);
};
}
}

View File

@@ -0,0 +1,7 @@
export * from './modal';
export * from './navbar';
export * from './navigator';
export * from './network';
export * from './storage';
export * from './popover';
export * from './animate';

View File

@@ -0,0 +1,22 @@
/*
* 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';
export * from './popover';
export * from './animate';

24
doric-js/lib/src/native/modal.d.ts vendored Normal file
View File

@@ -0,0 +1,24 @@
import { BridgeContext } from "../runtime/global";
import { Gravity } from "../util/gravity";
export declare function modal(context: BridgeContext): {
toast: (msg: string, gravity?: Gravity) => void;
alert: (arg: string | {
title: string;
msg: string;
okLabel?: string | undefined;
}) => Promise<any>;
confirm: (arg: string | {
title: string;
msg: string;
okLabel?: string | undefined;
cancelLabel?: string | undefined;
}) => Promise<any>;
prompt: (arg: {
title?: string | undefined;
msg?: string | undefined;
okLabel?: string | undefined;
cancelLabel?: string | undefined;
text?: string | undefined;
defaultText?: string | undefined;
}) => Promise<string>;
};

View File

@@ -0,0 +1,30 @@
import { Gravity } from "../util/gravity";
export function modal(context) {
return {
toast: (msg, gravity = Gravity.Bottom) => {
context.modal.toast({
msg,
gravity: gravity.toModel(),
});
},
alert: (arg) => {
if (typeof arg === 'string') {
return context.modal.alert({ msg: arg });
}
else {
return context.modal.alert(arg);
}
},
confirm: (arg) => {
if (typeof arg === 'string') {
return context.modal.confirm({ msg: arg });
}
else {
return context.modal.confirm(arg);
}
},
prompt: (arg) => {
return context.modal.prompt(arg);
},
};
}

8
doric-js/lib/src/native/navbar.d.ts vendored Normal file
View File

@@ -0,0 +1,8 @@
import { BridgeContext } from "../runtime/global";
import { Color } from "../util/color";
export declare function navbar(context: BridgeContext): {
isHidden: () => Promise<boolean>;
setHidden: (hidden: boolean) => Promise<any>;
setTitle: (title: string) => Promise<any>;
setBgColor: (color: Color) => Promise<any>;
};

View File

@@ -0,0 +1,28 @@
import { Panel } from "../ui/panel";
export function navbar(context) {
const entity = context.entity;
let panel = undefined;
if (entity instanceof Panel) {
panel = entity;
}
return {
isHidden: () => {
return context.navbar.isHidden();
},
setHidden: (hidden) => {
return context.navbar.setHidden({
hidden,
});
},
setTitle: (title) => {
return context.navbar.setTitle({
title,
});
},
setBgColor: (color) => {
return context.navbar.setBgColor({
color: color.toModel(),
});
},
};
}

View File

@@ -0,0 +1,9 @@
import { BridgeContext } from "../runtime/global";
export declare function navigator(context: BridgeContext): {
push: (scheme: string, config?: {
alias?: string | undefined;
animated?: boolean | undefined;
extra?: object | undefined;
} | undefined) => Promise<any>;
pop: (animated?: boolean) => Promise<any>;
};

View File

@@ -0,0 +1,15 @@
export function navigator(context) {
return {
push: (scheme, config) => {
if (config && config.extra) {
config.extra = JSON.stringify(config.extra);
}
return context.navigator.push({
scheme, config
});
},
pop: (animated = true) => {
return context.navigator.pop({ animated });
},
};
}

27
doric-js/lib/src/native/network.d.ts vendored Normal file
View File

@@ -0,0 +1,27 @@
import { BridgeContext } from "../runtime/global";
export interface IRequest {
url?: string;
method?: "get" | "post" | "put" | "delete";
headers?: {
[index: string]: string;
};
params?: {
[index: string]: string;
};
data?: object | string;
timeout?: number;
}
export interface IResponse {
data: any;
status: number;
headers?: {
[index: string]: string;
};
}
export declare function network(context: BridgeContext): {
request: (config: IRequest) => Promise<IResponse>;
get: (url: string, config?: IRequest | undefined) => Promise<IResponse>;
post: (url: string, data?: string | object | undefined, config?: IRequest | undefined) => Promise<IResponse>;
put: (url: string, data?: string | object | undefined, config?: IRequest | undefined) => Promise<IResponse>;
delete: (url: string, data?: string | object | undefined, config?: IRequest | undefined) => Promise<IResponse>;
};

View File

@@ -0,0 +1,63 @@
function transformRequest(request) {
let url = request.url || "";
if (request.params !== undefined) {
const queryStrings = [];
for (let key in request.params) {
queryStrings.push(`${key}=${encodeURIComponent(request.params[key])}`);
}
request.url = `${request.url}${url.indexOf('?') >= 0 ? '&' : '?'}${queryStrings.join('&')}`;
}
if (typeof request.data === 'object') {
request.data = JSON.stringify(request.data);
}
return request;
}
export function network(context) {
return {
request: (config) => {
return context.network.request(transformRequest(config));
},
get: (url, config) => {
let finalConfig = config;
if (finalConfig === undefined) {
finalConfig = {};
}
finalConfig.url = url;
finalConfig.method = "get";
return context.network.request(transformRequest(finalConfig));
},
post: (url, data, config) => {
let finalConfig = config;
if (finalConfig === undefined) {
finalConfig = {};
}
finalConfig.url = url;
finalConfig.method = "post";
if (data !== undefined) {
finalConfig.data = data;
}
return context.network.request(transformRequest(finalConfig));
},
put: (url, data, config) => {
let finalConfig = config;
if (finalConfig === undefined) {
finalConfig = {};
}
finalConfig.url = url;
finalConfig.method = "put";
if (data !== undefined) {
finalConfig.data = data;
}
return context.network.request(transformRequest(finalConfig));
},
delete: (url, data, config) => {
let finalConfig = config;
if (finalConfig === undefined) {
finalConfig = {};
}
finalConfig.url = url;
finalConfig.method = "delete";
return context.network.request(transformRequest(finalConfig));
},
};
}

6
doric-js/lib/src/native/popover.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
import { BridgeContext } from "../runtime/global";
import { View } from "../ui/view";
export declare function popover(context: BridgeContext): {
show: (view: View) => Promise<any>;
dismiss: (view?: View | undefined) => Promise<any>;
};

View File

@@ -0,0 +1,27 @@
import { Panel } from "../ui/panel";
export function popover(context) {
const entity = context.entity;
let panel = undefined;
if (entity instanceof Panel) {
panel = entity;
}
return {
show: (view) => {
if (panel) {
panel.addHeadView(view);
}
return context.popover.show(view.toModel());
},
dismiss: (view = undefined) => {
if (panel) {
if (view) {
panel.removeHeadView(view);
}
else {
panel.clearHeadViews();
}
}
return context.popover.dismiss(view ? { id: view.viewId } : undefined);
},
};
}

7
doric-js/lib/src/native/storage.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
import { BridgeContext } from "../runtime/global";
export declare function storage(context: BridgeContext): {
setItem: (key: string, value: string, zone?: string | undefined) => Promise<any>;
getItem: (key: string, zone?: string | undefined) => Promise<string>;
remove: (key: string, zone?: string | undefined) => Promise<any>;
clear: (zone: string) => Promise<any>;
};

View File

@@ -0,0 +1,16 @@
export function storage(context) {
return {
setItem: (key, value, zone) => {
return context.storage.setItem({ key, value, zone });
},
getItem: (key, zone) => {
return context.storage.getItem({ key, zone });
},
remove: (key, zone) => {
return context.storage.remove({ key, zone });
},
clear: (zone) => {
return context.storage.clear({ zone });
},
};
}