@chief-editor/flow
Version:
Old核心数据流
1 lines • 4 kB
JavaScript
var __extends=this&&this.__extends||function(){var e=function(t,o){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var o in t)t.hasOwnProperty(o)&&(e[o]=t[o])})(t,o)};return function(t,o){function r(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(r.prototype=o.prototype,new r)}}();import{DomNode}from"@co-hooks/dom-node";import{Emitter}from"@co-hooks/emitter";import{NestWatcher}from"@co-hooks/nest-watcher";import{deepClone,getObjectProperty,setObjectProperty}from"@co-hooks/util";import{NodeType}from"@chief-editor/base";import{MemCache}from"@co-hooks/mem-cache";import{getRelativeKey,getRelativeKeys}from"../util/keys";var FlowModel=function(e){function t(t){var o=e.call(this)||this;o.model={},o.templateMap={},o.brickMap={};var r=t.context,n=t.name,i=t.model,s=void 0===i?{}:i,p=t.parent,a=t.disableCache,c=void 0!==a&&a,h=t.keepWhenReset,l=void 0!==h&&h;if(/^$(context|scope|model|path|root|input|hooks|data)/.test(n))throw new Error(n+" is not a valid model name");return o.root=new DomNode({type:NodeType.ROOT,cache:new MemCache}),o.context=r,o.parentModel=p?r.getModel(p):o,o.model=s,o.watcher=r.getWatcher(),o.disableCache=c,o.keepWhenReset=l,o.name=n,o.init(),o}return __extends(t,e),t.prototype.getTemplate=function(e){return this.templateMap[e]},t.prototype.unregisterTemplate=function(e){delete this.templateMap[e]},t.prototype.registerTemplate=function(e,t){this.templateMap[e]=t},t.prototype.getOwnerContext=function(){return this.context},t.prototype.setContext=function(e,t){"string"==typeof e?this.getOwnerContext().setContext(e,t):this.getOwnerContext().setContext(e)},t.prototype.getContext=function(e,t){return void 0===e&&(e=!1),void 0===t&&(t=!1),"string"==typeof e?this.getOwnerContext().getContext(e,t):this.getOwnerContext().getContext(e)},t.prototype.getBrickById=function(e,t){if(t)return this.getOwnerContext().getModel(t).getBrickById(e);var o=this.brickMap[e];if(!o)throw new Error("component :"+e+" is not found");return o},t.prototype.getHooks=function(){return this.getOwnerContext().getHooks()},t.prototype.getHook=function(e){return this.getOwnerContext().getHook(e)},t.prototype.registerWatcher=function(e,t){this.watcher.register(this.name,e,t)},t.prototype.getParentModel=function(e){void 0===e&&(e=1);for(var t=this;e-- >0;){var o=t.parentModel;if(o===t)break;t=o}return t},t.prototype.getRootModel=function(){for(var e=this;e.getParentModel(1)!==e;)e=e.getParentModel(1);return e},t.prototype.createSubModel=function(e,t){return this.context.createModel({name:e,parent:this.name,model:t,disableCache:this.disableCache,keepWhenReset:this.keepWhenReset})},t.prototype.updateModel=function(e,t){void 0===t&&(t=!1),this.model!==e&&(this.model=e,t||this.watcher.notify([this.name],{model:!0}))},t.prototype.publishModelChange=function(e,t){this.watcher.notify(e,t)},t.prototype.resetModel=function(e,t){void 0===t&&(t=!1)},t.prototype.getWatcher=function(){return this.watcher},t.prototype.getValueByBrick=function(e,t){if(void 0===t&&(t=!1),e.readonly)return null;var o=getObjectProperty(this.model,getRelativeKeys(e.getNode()));return t?o:deepClone(o)},t.prototype.setValueByPath=function(e,t,o,r){if(void 0===o&&(o=!1),void 0===r&&(r=!1),e.readonly)console.error("can't set value for readonly brick, set field prop first ",e);else{var n=getRelativeKeys(e.getNode());setObjectProperty(this.model,t,n)?r||this.publishModelChange([this.name+"."+getRelativeKey(e)],{manual:o}):console.error("can't set prop of non object, keys = "+n.join("."))}},t.prototype.getModelInfo=function(e){return void 0===e&&(e=!1),e?this.model:deepClone(this.model)},t.prototype.dispose=function(){this.watcher.unregister(this.name)},t.prototype.init=function(){var e=this,t=NestWatcher.combineKeys([this.name]);this.watcher.register(this.name,[t],(function(o,r){return e.emit("model-change",o.map((function(e){return e.slice(t.length+1)})),r),Promise.resolve()}),!1)},t}(Emitter);export{FlowModel};