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,6 +372,9 @@ function getLink(str: string, pid?: number) {
result = () => {
return engine.dataCenter.parse(type, expression)
};
} else if (str.indexOf('script|') == 0) { //script
const script = str.substr(7);
result = wrapperScript(script);
} else {
result = str;
}
......@@ -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{
export default class DataCenter extends EventEmitter {
private store: any = {};
/**
......@@ -18,11 +20,11 @@ export default class DataCenter extends EventEmitter{
* 清空数据,如果type为false值,就清空整个数据中心
* @param type
*/
public clean(type?: string){
public clean(type?: string) {
let target = type ? this.store[type] : this.store;
if(target){
if (target) {
let keys = Object.keys(target);
for(let key of keys){
for (let key of keys) {
delete target[key];
}
}
......@@ -67,17 +69,13 @@ export default class DataCenter extends EventEmitter{
*/
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];
}
return node;
//return eval(`(this.store['${type}'].${expression})`);
let result = null;
try {
result = utils.dotEval(expression, node);
} catch (e) {
}
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