From 0734973b728ba7c204f8b525bdfd5e1079edcf75 Mon Sep 17 00:00:00 2001 From: iapyang Date: Wed, 4 Sep 2019 19:24:32 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-1211=20feat:=20=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E8=A3=85=E9=A5=B0=E7=B1=BB=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsconfig.json | 4 +- typescript/index.ts | 96 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 93d92ad76..cb7147d37 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,8 +12,8 @@ "experimentalDecorators": true, "outDir": "./bin", "baseUrl": ".", - "strict": true, - "strictNullChecks": true, + // "strict": true, + // "strictNullChecks": true, "noImplicitAny": true, "noUnusedLocals": true, "noUnusedParameters": true, diff --git a/typescript/index.ts b/typescript/index.ts index 18b9c6617..855c9a408 100644 --- a/typescript/index.ts +++ b/typescript/index.ts @@ -13,23 +13,93 @@ import { _HighlightBehavior } from "./core/behavior/behavior.highlight"; import { _RedMarkBehavior } from "./core/behavior/behavior.redmark"; -type Constructor = T & { +type ClassConstructor = T & { new(config: any): T; (config: any): T; readonly prototype: T; } export interface _BI extends _func, _i18n, _base { - OB: Constructor<_OB>; - Widget: Constructor<_Widget> & _WidgetStatic; - Single: Constructor<_Single>; - BasicButton: Constructor<_BasicButton>; - Trigger: Constructor<_Trigger>; - Action: Constructor<_Action>; - ActionFactory: Constructor<_ActionFactory>; - ShowAction: Constructor<_ShowAction>; - Behavior: Constructor<_Behavior>; - BehaviorFactory: Constructor<_BehaviorFactory>; - HighlightBehavior: Constructor<_HighlightBehavior>; - RedMarkBehavior: Constructor<_RedMarkBehavior>; + OB: ClassConstructor<_OB>; + Widget: ClassConstructor<_Widget> & _WidgetStatic; + Single: ClassConstructor<_Single>; + BasicButton: ClassConstructor<_BasicButton>; + Trigger: ClassConstructor<_Trigger>; + Action: ClassConstructor<_Action>; + ActionFactory: ClassConstructor<_ActionFactory>; + ShowAction: ClassConstructor<_ShowAction>; + Behavior: ClassConstructor<_Behavior>; + BehaviorFactory: ClassConstructor<_BehaviorFactory>; + HighlightBehavior: ClassConstructor<_HighlightBehavior>; + RedMarkBehavior: ClassConstructor<_RedMarkBehavior>; +} + +interface Obj { + [key: string]: any; +} + +declare let BI: Obj & _BI; + +declare const Fix: Obj; + +export type Constructor = new(...args: any[]) => T; + +/** + * 注册widget + */ +export function shortcut () { + return function decorator (Target: Constructor & {xtype: string}): void { + BI.shortcut(Target.xtype, Target); + }; +} + +/** + * 注册model + */ +export function model () { + return function decorator}>(Target: U): void { + BI.model(Target.xtype, class extends Target { + context = Target.context; + }); + }; +} + +/** + * 类注册_store属性 + * @param Model model类 + * @param opts 额外条件 + */ +export function store (Model?: Constructor & {xtype: string}, opts: { props?(this: unknown): { [key: string]: unknown } } = {}) { + return function classDecorator(constructor:U) { + return class extends constructor { + _store () { + const props = opts.props ? opts.props.apply(this) : undefined; + + return BI.Models.getModel(Model.xtype, props); + } + }; + }; +} + +/** + * Model基类 + */ +export class Model} = {}> extends Fix.Model { + model: Pick<{[key in keyof U["types"]]: U["types"][key]}, U["context"][number]> & {[key in keyof ReturnType]: ReturnType[key]} & {[key in keyof this["computed"]]: ReturnType} & {[key: string]: unknown}; + + store: this["actions"]; + + state (): {[key: string]: any} { + return {}; + } + + context: U["context"]; + + actions:{[key: string]: (...args: any[]) => any}; + + childContext: ReadonlyArray)>; + + TYPE: Pick<{[key in keyof this["computed"]]: ReturnType} & {[key in keyof ReturnType]: ReturnType[key]}, this["childContext"][number]>; + + computed: {[key: string]: () => any}; }