Commit 860ceae5 authored by rockyl's avatar rockyl

增加script类型

parent 31ca7375
......@@ -22,6 +22,11 @@ export type raw = any;
*/
export type dynamic = any;
/**
* 脚本数据
*/
export type script = any;
/**
* 常用框类型
*/
......
......@@ -33,7 +33,7 @@ export function unregisterDef(name: string) {
delete defMap[name];
}
export function setGetResProxy(func: Function){
export function setGetResProxy(func: Function) {
_getResProxy = func;
}
......@@ -76,7 +76,7 @@ export function instantiate(config: any): Entity {
* @param root
* @param pid
*/
function instantiateConfig(config, root?: Entity, pid?:number): Entity {
function instantiateConfig(config, root?: Entity, pid?: number): Entity {
let rootConfig = config.root;
const entity = setupEntity(rootConfig, root, pid);
......@@ -209,7 +209,7 @@ function injectComponents(entity: Entity, config: any, pid?: number) {
* @param config
* @param pid
*/
export function injectComponentProperties(component, config, pid?: number){
export function injectComponentProperties(component, config, pid?: number) {
const {properties} = config;
if (properties) {
......@@ -223,7 +223,7 @@ export function injectComponentProperties(component, config, pid?: number){
* @param config
*/
export function instantiateComponent(entity: Entity, config: any) {
const {script, } = config;
const {script,} = config;
let def = getDefByName(script);
......@@ -279,7 +279,7 @@ function injectProperties(node, propertiesConfig, pid?: number) {
let propertyOfInstance = node[key];
if (typeof propertyOfConfig === 'object') {
if (propertyOfInstance instanceof ScillaEvent) {
if(!EngineConfig.editorMode){
if (!EngineConfig.editorMode) {
injectEvent(propertyOfInstance, propertyOfConfig, pid);
}
} else if (propertyOfConfig._type_ === 'raw') {
......@@ -325,14 +325,14 @@ function injectBaseType(node, key, propertyOfConfig, pid?: number) {
}
let keyAvatar = key;
if(propertyValue instanceof Promise){
if (propertyValue instanceof Promise) {
keyAvatar = 'async_' + keyAvatar;
}
if (typeof propertyValue === 'function') {
Object.defineProperty(node, keyAvatar, {
get() {
return this[`func_${keyAvatar}`]()
return this[`func_${keyAvatar}`](node, engine.dataCenter)
},
set(v) {
this[`func_${keyAvatar}`] = v;
......@@ -372,7 +372,10 @@ function getLink(str: string, pid?: number) {
result = () => {
return engine.dataCenter.parse(type, expression)
};
} else {
} else if (str.indexOf('script|') == 0) { //script
const script = str.substr(7);
result = wrapperScript(script);
} else {
result = str;
}
return result;
......@@ -381,3 +384,11 @@ function getLink(str: string, pid?: number) {
function transPrefabUUID(uuid, pid: number) {
return pid ? pid + '_' + uuid : uuid;
}
function wrapperScript(script){
try {
return new Function('component', 'dataCenter', script);
}catch (e) {
console.warn('script is correct :', script);
}
}
/**
* 数据中心类
*/
import EventEmitter from "./EventEmitter";
import {utils} from "../tools";
export default class DataCenter extends EventEmitter {
private store: any = {};
export default class DataCenter extends EventEmitter{
private store: any = {};
/**
* 注册一个数据根
* @param type
*/
public register(type: string) {
this.store[type] = this.store[type] || {};
}
/**
* 注册一个数据根
* @param type
*/
public register(type: string) {
this.store[type] = this.store[type] || {};
}
/**
* 清空数据,如果type为false值,就清空整个数据中心
* @param type
*/
public clean(type?: string) {
let target = type ? this.store[type] : this.store;
if (target) {
let keys = Object.keys(target);
for (let key of keys) {
delete target[key];
}
}
}
/**
* 清空数据,如果type为false值,就清空整个数据中心
* @param type
*/
public clean(type?: string){
let target = type ? this.store[type] : this.store;
if(target){
let keys = Object.keys(target);
for(let key of keys){
delete target[key];
}
}
}
/**
* 设置数据
* @param type
* @param key
* @param value
*/
public set(type: string, key: string, value?) {
if (arguments.length <= 2) {
//if (this.store[type]) console.warn(`This operation will override all ${this.store[type]}`);
this.store[type] = key;
} else {
this.store[type][key] = value;
}
}
/**
* 设置数据
* @param type
* @param key
* @param value
*/
public set(type: string, key: string, value?) {
if (arguments.length <= 2) {
//if (this.store[type]) console.warn(`This operation will override all ${this.store[type]}`);
this.store[type] = key;
} else {
this.store[type][key] = value;
}
}
/**
* 获取数据
* @param type
* @param key
*/
public get(type: string, key?: string) {
if (!key) return this.store[type];
return this.store[type][key];
}
/**
* 获取数据
* @param type
* @param key
*/
public get(type: string, key?: string) {
if (!key) return this.store[type];
return this.store[type][key];
}
public use(type: string, key: string) {
return resopnse => {
this.set(type, key, resopnse);
return resopnse
}
}
public use(type: string, key: string) {
return resopnse => {
this.set(type, key, resopnse);
return resopnse
}
}
/**
* 转换成真实数据
* @param type
* @param expression
*/
public parse(type: string, expression: string) {
let node = this.store[type];
/**
* 转换成真实数据
* @param type
* @param expression
*/
public parse(type: string, expression: string) {
let node = this.store[type];
let segments = expression.split('.');
while(segments.length > 0){
const segment = segments.shift();
if(!node){
console.warn(`can not get ${segment} on ${segment}`);
break;
}
node = node[segment];
}
let result = null;
try {
result = utils.dotEval(expression, node);
} catch (e) {
return node;
//return eval(`(this.store['${type}'].${expression})`);
}
}
return result;
}
}
......@@ -28,7 +28,7 @@ export function injectProp(target: any, data?: any, callback?: Function, ignoreM
} else {
try {
target[key] = value;
}catch (e) {
} catch (e) {
}
}
......@@ -45,18 +45,18 @@ export function injectProp(target: any, data?: any, callback?: Function, ignoreM
* @param data
* @param config
*/
export function copyProp(target, data?, config?){
if(config){
for(let key in config){
export function copyProp(target, data?, config?) {
if (config) {
for (let key in config) {
let valueConfig = config[key];
if(Array.isArray(valueConfig)){
if (Array.isArray(valueConfig)) {
target[key] = {};
for(let field of valueConfig){
for (let field of valueConfig) {
target[key][field] = data[key][field];
}
}else if(typeof valueConfig === 'string'){
} else if (typeof valueConfig === 'string') {
target[valueConfig] = data[valueConfig];
}else if(typeof valueConfig === 'object'){
} else if (typeof valueConfig === 'object') {
target[key] = {};
copyProp(target[key], data[key], valueConfig)
}
......@@ -138,3 +138,35 @@ export function supplement(value: number, count: number): string {
}
return zeros[index] + value;
}
/**
* 点eval
* @param expression
* @param context
* @param strict
* @param dotChar
*/
export function dotEval(expression: string, context = window, strict = true, dotChar = '.') {
if (!expression || !context) {
return context;
}
let result = expression
.split(dotChar)
.reduce(function (pre, cur, index, arr) {
let ref;
if (strict) {
if (pre && pre.hasOwnProperty(cur)) {
ref = pre[cur];
} else {
const e = `can not get ${cur} on ${index === 0 ? 'context' : arr.slice(0, index).join(dotChar)}`;
console.warn(e);
throw new Error(e);
}
} else {
ref = cur && pre[cur] || pre
}
return ref;
}, context);
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment