ViewSystem.js 10 KB

1
  1. var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.View=exports.default=exports.Node=exports.ViewNode=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _react=_interopRequireDefault(require("react"));var _tree=require("../helpers/tree");var _Row=_interopRequireDefault(require("../../modules/Row"));var _Col=_interopRequireDefault(require("../../modules/Col"));var _BaseHolder=_interopRequireDefault(require("../../modules/BaseHolder"));var _reactNative=require("react-native");var _jsxFileName="C:\\Users\\Karteri\\Desktop\\Development\\TrapiLib\\lib\\systems\\ViewSystem.js";function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(source,true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(source).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}function log(){var _console;(_console=console).log.apply(_console,arguments);}var ViewNode=function(_TreeNode){(0,_inherits2.default)(ViewNode,_TreeNode);function ViewNode(id,value,props){var _this;var namespace=arguments.length>3&&arguments[3]!==undefined?arguments[3]:"default";(0,_classCallCheck2.default)(this,ViewNode);_this=(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(ViewNode).call(this,id));_this.value=value;_this.namespace=namespace;_this.props=props||{};return _this;}return ViewNode;}(_tree.TreeNode);exports.ViewNode=ViewNode;var Node=function(_TreeNode2){(0,_inherits2.default)(Node,_TreeNode2);function Node(id,props,content){var _this2;(0,_classCallCheck2.default)(this,Node);_this2=(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Node).call(this,id));_this2.ctor=_BaseHolder.default;_this2.props=props||{};if(content){_this2.content=content;}return _this2;}return Node;}(_tree.TreeNode);exports.Node=Node;var ViewSystem=function(){function ViewSystem(CS){(0,_classCallCheck2.default)(this,ViewSystem);this.views={};this.CoreSystem=CS;this.ModuleSystem=CS.ModuleSystem;this.EventSystem=CS.EventSystem;this.ghosts={};}(0,_createClass2.default)(ViewSystem,[{key:"getNode",value:function getNode(ViewId,NodeId){return this.views[ViewId].getNode(NodeId);}},{key:"getView",value:function getView(name){return this.views[name];}},{key:"addView",value:function addView(name,view){this.views[name]=view;return view;}},{key:"removeView",value:function removeView(name){var view=this.views[name];delete this.views[name];return view;}},{key:"render",value:function render(name){return this.views[name].render(this.CoreSystem);}},{key:"renderModule",value:function renderModule(mod,id){var selfView=this.CoreSystem.getCurrentView();var nodeId="___"+id+"Node";var value=mod.value,props=mod.props,namespace=mod.namespace;if(!selfView.ghosts[nodeId]){var viewNode=new ViewNode(id,value,props,namespace);var node=new Node(nodeId,{},viewNode);selfView.ghosts[nodeId]=node;}var vn=selfView.ghosts[nodeId].content;vn.value=value;vn.namespace=namespace;var ViewNodeReact=renderContent(selfView.ghosts[nodeId],this.CoreSystem,selfView);return renderNode(selfView.ghosts[nodeId],this.CoreSystem,ViewNodeReact);}},{key:"export",value:function _export(){var views={};for(var i in this.views){views[i]=this.views[i].export();}return{views:views};}},{key:"import",value:function _import(data){var views=data.views;for(var i in views){this.views[i]=new View().import(views[i]);}return this;}},{key:"ray",value:function ray(viewId,x,y,id){var tree=this.views[viewId].ViewTree;}}]);return ViewSystem;}();exports.default=ViewSystem;var View=function(){function View(){(0,_classCallCheck2.default)(this,View);this.ViewTree=new _tree.Tree();var rootNode=new Node(Math.random(),{stretchContainer:false,selected:true});this.addViewNode(rootNode);this.defaultContainer=rootNode;this.ghosts={};}(0,_createClass2.default)(View,[{key:"getDefaultContainer",value:function getDefaultContainer(){return this.defaultContainer;}},{key:"getRoot",value:function getRoot(){return this.ViewTree.root;}},{key:"getNode",value:function getNode(id){return this.ghosts[id]||this.ViewTree.getNode(id);}},{key:"has",value:function has(node){if(typeof node!=="object")return this.getNode(node);return this.getNode(node.id);}},{key:"render",value:function render(CS){var node=this.ViewTree.root;return _react.default.createElement(Renderer,{CoreSystem:CS,node:node,tree:this.ViewTree,selfView:this,__source:{fileName:_jsxFileName,lineNumber:139}});}},{key:"addViewNode",value:function addViewNode(node){var parentNode=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;this.ViewTree.insert(node,parentNode);return this;}},{key:"setContent",value:function setContent(content,container){container.content=content;return this;}},{key:"getParent",value:function getParent(node){if(!this.has(node))return false;return this.getNode(this.ViewTree.getParent(node));}},{key:"getRows",value:function getRows(column){log("Deprecated for now");return true;if(!column.isCol){throw new Error("You must pass a ColNode to this function");}return this.getChildren(column);}},{key:"setRows",value:function setRows(column){var rows=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;log("Deprecated for now");return true;if(!column.isCol)throw new Error("You must pass a ColNode to this function");if(rows<1)throw new Error("Only positive rows");var colChildren=this.getChildren(column);if(colChildren.length>rows){for(var i=rows;i<colChildren.length;i++){var row=colChildren[i];if(row.content){if(confirm("Are you sure you want to remove this row it has content?"))this.remove(row);}else{this.remove(row);}}}else{for(var i=colChildren.length;i<rows;i++){this.addViewNode(new Node(Math.random(),true),column);}}return this.getRows(column);}},{key:"getColumns",value:function getColumns(row){log("Deprecated for now");return true;if(!row.isRow){throw new Error("You must pass a RowNode to this function");}return this.getChildren(row);}},{key:"setColumns",value:function setColumns(row){var columns=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;log("Deprecated for now");return true;if(!row.isRow)throw new Error("You must pass a RowNode to this function");if(columns<1)throw new Error("Only positive cols");var rowChildren=this.getChildren(row);if(rowChildren.length>columns){for(var i=columns;i<rowChildren.length;i++){var col=rowChildren[i];if(col.content){if(confirm("Are you sure you want to remove this col it has content?"))this.remove(col);}else{this.remove(col);}}}else{for(var i=rowChildren.length;i<columns;i++){this.addViewNode(new Node(Math.random()),row);}}return this.getColumns(row);}},{key:"addGrid",value:function addGrid(column,rowSize,columnSize){var _this3=this;log("Deprecated for now");return true;this.setRows(column,rowSize).forEach(function(row){return _this3.setColumns(row,columnSize).forEach(function(col){return col.content=new ViewNode(Math.random(),"BaseContainer",{});});});}},{key:"remove",value:function remove(node){if(node===root){console.log("Cannot remove Root node",node);return this;}this.ViewTree.remove(node);return this;}},{key:"replace",value:function replace(oldNode,newNode){this.ViewTree.replaceNode(oldNode,newNode);return this;}},{key:"getChildren",value:function getChildren(node){return this.ViewTree.getChildren(node);}},{key:"import",value:function _import(data,parentNode){var tree=data.tree,ghosts=data.ghosts;if(!parentNode)this.ViewTree=new _tree.Tree();try{this.ViewTree.import(tree,Node,parentNode);this.defaultContainer=this.ViewTree.root;for(var i in ghosts){this.ghosts[i]=new Node().import(ghosts[i]);}}catch(e){log(e);throw new Error("ViewSystem import error : ",e);}return this;}},{key:"export",value:function _export(node){var tree=this.ViewTree.export(node);var ghostsExported={};for(var i in this.ghosts){ghostsExported[i]=this.ghosts[i].export();}return{tree:tree,ghosts:ghostsExported};}}]);return View;}();exports.View=View;var Renderer=function Renderer(props){var node=props.node,tree=props.tree,CoreSystem=props.CoreSystem,selfView=props.selfView;var childrens=tree.getChildren(node);var childrenReact=childrens.map(function(child){return _react.default.createElement(Renderer,{node:child,tree:tree,key:child.id,CoreSystem:CoreSystem,selfView:selfView,__source:{fileName:_jsxFileName,lineNumber:292}});});var ModuleSystem=CoreSystem.ModuleSystem;var EventSystem=CoreSystem.EventSystem;var viewNode=node.content;var ViewNodeReact;if(viewNode){ViewNodeReact=renderContent(node,CoreSystem,selfView);}return renderNode(node,CoreSystem,ViewNodeReact,childrenReact);};var renderNode=function renderNode(node,CoreSystem,ViewNodeReact,childrenReact){CoreSystem.ModuleSystem.createRef(node.id);return CoreSystem.ModuleSystem.createElementCtor(node.ctor,_objectSpread({},node.props,{depth:node.depth,content:ViewNodeReact,ID:node.id,CoreSystem:CoreSystem}),childrenReact,node.id);};var renderContent=function renderContent(node,CoreSystem,selfView){var viewNode=node.content;CoreSystem.ModuleSystem.createRef(viewNode.id);return CoreSystem.ModuleSystem.createElement(viewNode.value,_objectSpread({},viewNode.props,{ModuleID:viewNode.id,NodeID:node.id,EventSystem:CoreSystem.EventSystem,CoreSystem:CoreSystem,View:selfView}),null,viewNode.namespace,viewNode.id);};var styles=_reactNative.StyleSheet.create({colWrapper:{"position":"absolute","left":0,"bottom":0,"right":0,"top":0}});