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

47
doric-js/lib/src/util/color.d.ts vendored Normal file
View File

@@ -0,0 +1,47 @@
import { Modeling } from "./types";
/**
* Store color as format AARRGGBB or RRGGBB
*/
export declare class Color implements Modeling {
static BLACK: Color;
static DKGRAY: Color;
static GRAY: Color;
static LTGRAY: Color;
static WHITE: Color;
static RED: Color;
static GREEN: Color;
static BLUE: Color;
static YELLOW: Color;
static CYAN: Color;
static MAGENTA: Color;
static TRANSPARENT: Color;
_value: number;
constructor(v: number);
static parse(str: string): Color;
static safeParse(str: string, defVal?: Color): Color;
alpha(v: number): Color;
toModel(): number;
}
export declare enum GradientOrientation {
/** draw the gradient from the top to the bottom */
TOP_BOTTOM = 0,
/** draw the gradient from the top-right to the bottom-left */
TR_BL = 1,
/** draw the gradient from the right to the left */
RIGHT_LEFT = 2,
/** draw the gradient from the bottom-right to the top-left */
BR_TL = 3,
/** draw the gradient from the bottom to the top */
BOTTOM_TOP = 4,
/** draw the gradient from the bottom-left to the top-right */
BL_TR = 5,
/** draw the gradient from the left to the right */
LEFT_RIGHT = 6,
/** draw the gradient from the top-left to the bottom-right */
TL_BR = 7
}
export interface GradientColor {
start: Color;
end: Color;
orientation: GradientOrientation;
}

View File

@@ -0,0 +1,73 @@
/**
* Store color as format AARRGGBB or RRGGBB
*/
export class Color {
constructor(v) {
this._value = 0;
this._value = v | 0x0;
}
static parse(str) {
if (!str.startsWith("#")) {
throw new Error(`Parse color error with ${str}`);
}
const val = parseInt(str.substr(1), 16);
if (str.length === 7) {
return new Color(val | 0xff000000);
}
else if (str.length === 9) {
return new Color(val);
}
else {
throw new Error(`Parse color error with ${str}`);
}
}
static safeParse(str, defVal = Color.TRANSPARENT) {
let color = defVal;
try {
color = Color.parse(str);
}
catch (e) {
}
finally {
return color;
}
}
alpha(v) {
v = v * 255;
return new Color((this._value & 0xffffff) | ((v & 0xff) << 24));
}
toModel() {
return this._value;
}
}
Color.BLACK = new Color(0xFF000000);
Color.DKGRAY = new Color(0xFF444444);
Color.GRAY = new Color(0xFF888888);
Color.LTGRAY = new Color(0xFFCCCCCC);
Color.WHITE = new Color(0xFFFFFFFF);
Color.RED = new Color(0xFFFF0000);
Color.GREEN = new Color(0xFF00FF00);
Color.BLUE = new Color(0xFF0000FF);
Color.YELLOW = new Color(0xFFFFFF00);
Color.CYAN = new Color(0xFF00FFFF);
Color.MAGENTA = new Color(0xFFFF00FF);
Color.TRANSPARENT = new Color(0);
export var GradientOrientation;
(function (GradientOrientation) {
/** draw the gradient from the top to the bottom */
GradientOrientation[GradientOrientation["TOP_BOTTOM"] = 0] = "TOP_BOTTOM";
/** draw the gradient from the top-right to the bottom-left */
GradientOrientation[GradientOrientation["TR_BL"] = 1] = "TR_BL";
/** draw the gradient from the right to the left */
GradientOrientation[GradientOrientation["RIGHT_LEFT"] = 2] = "RIGHT_LEFT";
/** draw the gradient from the bottom-right to the top-left */
GradientOrientation[GradientOrientation["BR_TL"] = 3] = "BR_TL";
/** draw the gradient from the bottom to the top */
GradientOrientation[GradientOrientation["BOTTOM_TOP"] = 4] = "BOTTOM_TOP";
/** draw the gradient from the bottom-left to the top-right */
GradientOrientation[GradientOrientation["BL_TR"] = 5] = "BL_TR";
/** draw the gradient from the left to the right */
GradientOrientation[GradientOrientation["LEFT_RIGHT"] = 6] = "LEFT_RIGHT";
/** draw the gradient from the top-left to the bottom-right */
GradientOrientation[GradientOrientation["TL_BR"] = 7] = "TL_BR";
})(GradientOrientation || (GradientOrientation = {}));

26
doric-js/lib/src/util/gravity.d.ts vendored Normal file
View File

@@ -0,0 +1,26 @@
import { Modeling } from "./types";
export declare const LEFT: number;
export declare const RIGHT: number;
export declare const TOP: number;
export declare const BOTTOM: number;
export declare const CENTER_X: number;
export declare const CENTER_Y: number;
export declare const CENTER: number;
export declare class Gravity implements Modeling {
val: number;
left(): Gravity;
right(): Gravity;
top(): Gravity;
bottom(): Gravity;
center(): Gravity;
centerX(): Gravity;
centerY(): Gravity;
toModel(): number;
private static origin;
static Center: Gravity;
static Left: Gravity;
static Right: Gravity;
static Top: Gravity;
static Bottom: Gravity;
}
export declare function gravity(): Gravity;

View File

@@ -0,0 +1,71 @@
const SPECIFIED = 1;
const START = 1 << 1;
const END = 1 << 2;
const SHIFT_X = 0;
const SHIFT_Y = 4;
export const LEFT = (START | SPECIFIED) << SHIFT_X;
export const RIGHT = (END | SPECIFIED) << SHIFT_X;
export const TOP = (START | SPECIFIED) << SHIFT_Y;
export const BOTTOM = (END | SPECIFIED) << SHIFT_Y;
export const CENTER_X = SPECIFIED << SHIFT_X;
export const CENTER_Y = SPECIFIED << SHIFT_Y;
export const CENTER = CENTER_X | CENTER_Y;
export class Gravity {
constructor() {
this.val = 0;
}
left() {
const val = this.val | LEFT;
const ret = new Gravity;
ret.val = val;
return ret;
}
right() {
const val = this.val | RIGHT;
const ret = new Gravity;
ret.val = val;
return ret;
}
top() {
const val = this.val | TOP;
const ret = new Gravity;
ret.val = val;
return ret;
}
bottom() {
const val = this.val | BOTTOM;
const ret = new Gravity;
ret.val = val;
return ret;
}
center() {
const val = this.val | CENTER;
const ret = new Gravity;
ret.val = val;
return ret;
}
centerX() {
const val = this.val | CENTER_X;
const ret = new Gravity;
ret.val = val;
return ret;
}
centerY() {
const val = this.val | CENTER_Y;
const ret = new Gravity;
ret.val = val;
return ret;
}
toModel() {
return this.val;
}
}
Gravity.origin = new Gravity;
Gravity.Center = Gravity.origin.center();
Gravity.Left = Gravity.origin.left();
Gravity.Right = Gravity.origin.right();
Gravity.Top = Gravity.origin.top();
Gravity.Bottom = Gravity.origin.bottom();
export function gravity() {
return new Gravity;
}

6
doric-js/lib/src/util/index.util.d.ts vendored Normal file
View File

@@ -0,0 +1,6 @@
export * from './color';
export * from './gravity';
export * from './layoutconfig';
export * from './log';
export * from './types';
export * from './uniqueId';

View File

@@ -0,0 +1,21 @@
/*
* 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 './color';
export * from './gravity';
export * from './layoutconfig';
export * from './log';
export * from './types';
export * from './uniqueId';

66
doric-js/lib/src/util/layoutconfig.d.ts vendored Normal file
View File

@@ -0,0 +1,66 @@
import { Gravity } from "./gravity";
import { Modeling } from "./types";
export declare enum LayoutSpec {
/**
* Depends on what's been set on width or height.
*/
JUST = 0,
/**
* Depends on it's content.
*/
FIT = 1,
/**
* Extend as much as parent let it take.
*/
MOST = 2
}
export interface LayoutConfig {
widthSpec?: LayoutSpec;
heightSpec?: LayoutSpec;
margin?: {
left?: number;
right?: number;
top?: number;
bottom?: number;
};
alignment?: Gravity;
weight?: number;
}
export declare class LayoutConfigImpl implements LayoutConfig, Modeling {
widthSpec?: LayoutSpec;
heightSpec?: LayoutSpec;
margin?: {
left?: number;
right?: number;
top?: number;
bottom?: number;
};
alignment?: Gravity;
weight?: number;
fit(): this;
most(): this;
just(): this;
configWidth(w: LayoutSpec): this;
configHeight(h: LayoutSpec): this;
configMargin(m: {
left?: number;
right?: number;
top?: number;
bottom?: number;
}): this;
configAlignment(a: Gravity): this;
configWeight(w: number): this;
toModel(): {
widthSpec: LayoutSpec | undefined;
heightSpec: LayoutSpec | undefined;
margin: {
left?: number | undefined;
right?: number | undefined;
top?: number | undefined;
bottom?: number | undefined;
} | undefined;
alignment: number | undefined;
weight: number | undefined;
};
}
export declare function layoutConfig(): LayoutConfigImpl;

View File

@@ -0,0 +1,64 @@
export var LayoutSpec;
(function (LayoutSpec) {
/**
* Depends on what's been set on width or height.
*/
LayoutSpec[LayoutSpec["JUST"] = 0] = "JUST";
/**
* Depends on it's content.
*/
LayoutSpec[LayoutSpec["FIT"] = 1] = "FIT";
/**
* Extend as much as parent let it take.
*/
LayoutSpec[LayoutSpec["MOST"] = 2] = "MOST";
})(LayoutSpec || (LayoutSpec = {}));
export class LayoutConfigImpl {
fit() {
this.widthSpec = LayoutSpec.FIT;
this.heightSpec = LayoutSpec.FIT;
return this;
}
most() {
this.widthSpec = LayoutSpec.MOST;
this.heightSpec = LayoutSpec.MOST;
return this;
}
just() {
this.widthSpec = LayoutSpec.JUST;
this.heightSpec = LayoutSpec.JUST;
return this;
}
configWidth(w) {
this.widthSpec = w;
return this;
}
configHeight(h) {
this.heightSpec = h;
return this;
}
configMargin(m) {
this.margin = m;
return this;
}
configAlignment(a) {
this.alignment = a;
return this;
}
configWeight(w) {
this.weight = w;
return this;
}
toModel() {
return {
widthSpec: this.widthSpec,
heightSpec: this.heightSpec,
margin: this.margin,
alignment: this.alignment ? this.alignment.toModel() : undefined,
weight: this.weight,
};
}
}
export function layoutConfig() {
return new LayoutConfigImpl;
}

3
doric-js/lib/src/util/log.d.ts vendored Normal file
View File

@@ -0,0 +1,3 @@
export declare function log(...args: any): void;
export declare function loge(...message: any): void;
export declare function logw(...message: any): void;

View File

@@ -0,0 +1,49 @@
function toString(message) {
if (message instanceof Function) {
return message.toString();
}
else if (message instanceof Object) {
try {
return JSON.stringify(message);
}
catch (e) {
return message.toString();
}
}
else if (message === undefined) {
return "undefined";
}
else {
return message.toString();
}
}
export function log(...args) {
let out = "";
for (let i = 0; i < arguments.length; i++) {
if (i > 0) {
out += ',';
}
out += toString(arguments[i]);
}
nativeLog('d', out);
}
export function loge(...message) {
let out = "";
for (let i = 0; i < arguments.length; i++) {
if (i > 0) {
out += ',';
}
out += toString(arguments[i]);
}
nativeLog('e', out);
}
export function logw(...message) {
let out = "";
for (let i = 0; i < arguments.length; i++) {
if (i > 0) {
out += ',';
}
out += toString(arguments[i]);
}
nativeLog('w', out);
}

19
doric-js/lib/src/util/types.d.ts vendored Normal file
View File

@@ -0,0 +1,19 @@
export interface Modeling {
toModel(): Model;
}
export declare function obj2Model(obj: Model): Model;
declare type _M = string | number | boolean | Modeling | {
[index: string]: Model;
} | undefined;
export declare type Model = _M | Array<_M>;
export declare type Binder<T> = (v: T) => void;
export declare class Mutable<T> {
private val;
private binders;
get: () => T;
set: (v: T) => void;
private constructor();
bind(binder: Binder<T>): void;
static of<E>(v: E): Mutable<E>;
}
export {};

View File

@@ -0,0 +1,43 @@
export function obj2Model(obj) {
if (obj instanceof Array) {
return obj.map(e => obj2Model(e));
}
else if (obj instanceof Object) {
if (Reflect.has(obj, 'toModel') && Reflect.get(obj, 'toModel') instanceof Function) {
obj = Reflect.apply(Reflect.get(obj, 'toModel'), obj, []);
return obj;
}
else {
for (let key in obj) {
const val = Reflect.get(obj, key);
Reflect.set(obj, key, obj2Model(val));
}
return obj;
}
}
else {
return obj;
}
}
export class Mutable {
constructor(v) {
this.binders = new Set;
this.get = () => {
return this.val;
};
this.set = (v) => {
this.val = v;
this.binders.forEach(e => {
Reflect.apply(e, undefined, [this.val]);
});
};
this.val = v;
}
bind(binder) {
this.binders.add(binder);
Reflect.apply(binder, undefined, [this.val]);
}
static of(v) {
return new Mutable(v);
}
}

1
doric-js/lib/src/util/uniqueId.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
export declare function uniqueId(prefix: string): string;

View File

@@ -0,0 +1,19 @@
/*
* 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.
*/
let __uniqueId__ = 0;
export function uniqueId(prefix) {
return `__${prefix}_${__uniqueId__++}__`;
}