3
0

ViewSystem.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import React from 'react';
  2. import {TreeNode, Tree} from '../helpers/tree'
  3. import Row from '../../modules/Row';
  4. import Col from '../../modules/Col';
  5. import BaseHolder from '../../modules/BaseHolder';
  6. import {View as Div, StyleSheet} from 'react-native';
  7. function log(...m) {
  8. console.log(...m);
  9. }
  10. export class ViewNode extends TreeNode {
  11. constructor(id,value, props, namespace = "default"){
  12. super(id);
  13. this.value = value; // constructor Component
  14. this.namespace = namespace;
  15. this.props = props || {};
  16. }
  17. }
  18. export class Node extends TreeNode {
  19. constructor(id, props, content) {
  20. super(id);
  21. this.ctor = BaseHolder;
  22. this.props = props || {};
  23. if(content){
  24. this.content = content; // this is a viewNode
  25. }
  26. }
  27. }
  28. export default class ViewSystem {
  29. constructor(CS) {
  30. this.views = {};
  31. this.CoreSystem = CS;
  32. this.ModuleSystem = CS.ModuleSystem;
  33. this.EventSystem = CS.EventSystem;
  34. }
  35. getNode(ViewId, NodeId) {
  36. return this.views[ViewId].getNode(NodeId);
  37. }
  38. getView(name) {
  39. return this.views[name];
  40. }
  41. addView(name, view) {
  42. this.views[name] = view;
  43. return view;
  44. }
  45. removeView(name) {
  46. let view = this.views[name];
  47. delete this.views[name];
  48. return view;
  49. }
  50. render(name) {
  51. return this.views[name].render(this.CoreSystem);
  52. }
  53. export() {
  54. let views = {};
  55. for(var i in this.views) {
  56. views[i] = this.views[i].export();
  57. }
  58. return {
  59. views
  60. }
  61. }
  62. import(data) {
  63. let { views } = data;
  64. for(var i in views) {
  65. this.views[i] = new View().import(views[i]);
  66. }
  67. return this;
  68. }
  69. ray(viewId, x, y, id) {
  70. let tree = this.views[viewId].ViewTree;
  71. }
  72. }
  73. export class View {
  74. constructor(){
  75. this.ViewTree = new Tree();
  76. let rootNode = new Node(Math.random(), {stretchContainer: false, selected: true}); // row
  77. this.addViewNode(rootNode);
  78. this.defaultContainer = rootNode;
  79. }
  80. getDefaultContainer() {
  81. return this.defaultContainer;
  82. }
  83. getRoot() {
  84. return this.ViewTree.root;
  85. }
  86. getNode(id) {
  87. return this.ViewTree.getNode(id);
  88. }
  89. has(node) {
  90. if (typeof node !== "object") return this.getNode(node);
  91. return this.getNode(node.id);
  92. }
  93. render(CS) {
  94. let node = this.ViewTree.root;
  95. return (<Renderer CoreSystem={CS} node={node} tree={this.ViewTree} selfView={this}/>);
  96. }
  97. addViewNode(node,parentNode = null){
  98. this.ViewTree.insert(node,parentNode);
  99. return this;
  100. }
  101. setContent(content, container) {
  102. container.content = content;
  103. return this;
  104. }
  105. getParent(node) {
  106. return this.getNode(this.ViewTree.getParent(node));
  107. }
  108. getRows(column) {
  109. log("Deprecated for now")
  110. return true;
  111. if(!column.isCol) {
  112. throw new Error("You must pass a ColNode to this function");
  113. }
  114. return this.getChildren(column);
  115. }
  116. setRows(column, rows = 1) {
  117. log("Deprecated for now")
  118. return true;
  119. if(!column.isCol) throw new Error("You must pass a ColNode to this function");
  120. if(rows < 1) throw new Error("Only positive rows");
  121. let colChildren = this.getChildren(column);
  122. if (colChildren.length > rows) {
  123. for(var i = rows; i < colChildren.length; i++) {
  124. let row = colChildren[i];
  125. if(row.content) {
  126. if(confirm("Are you sure you want to remove this row it has content?"))
  127. this.remove(row);
  128. } else {
  129. this.remove(row); }
  130. }
  131. } else {
  132. for(var i = colChildren.length; i < rows; i++)
  133. this.addViewNode(new Node(Math.random(), true), column); // row
  134. }
  135. return this.getRows(column);
  136. }
  137. getColumns(row) {
  138. log("Deprecated for now")
  139. return true;
  140. if(!row.isRow) {
  141. throw new Error("You must pass a RowNode to this function");
  142. }
  143. return this.getChildren(row);
  144. }
  145. setColumns(row, columns = 1) {
  146. log("Deprecated for now")
  147. return true;
  148. if(!row.isRow) throw new Error("You must pass a RowNode to this function");
  149. if(columns < 1) throw new Error("Only positive cols");
  150. let rowChildren = this.getChildren(row);
  151. if (rowChildren.length > columns) {
  152. for(var i = columns; i < rowChildren.length; i++) {
  153. let col = rowChildren[i];
  154. if(col.content) {
  155. if(confirm("Are you sure you want to remove this col it has content?"))
  156. this.remove(col);
  157. } else {
  158. this.remove(col);
  159. }
  160. }
  161. } else {
  162. for(var i = rowChildren.length; i < columns; i++) {
  163. this.addViewNode(new Node(Math.random()), row);
  164. }
  165. }
  166. return this.getColumns(row);
  167. }
  168. addGrid(column, rowSize, columnSize) {
  169. log("Deprecated for now")
  170. return true;
  171. this.setRows(column, rowSize)
  172. .forEach(row => this.setColumns(row, columnSize)
  173. .forEach(col => col.content = new ViewNode(
  174. Math.random(),
  175. "BaseContainer",
  176. {}
  177. )));
  178. }
  179. remove(node) {
  180. if(node === root) {
  181. console.log("Cannot remove Root node", node);
  182. return this;
  183. }
  184. this.ViewTree.remove(node);
  185. return this;
  186. }
  187. replace(oldNode,newNode){
  188. this.ViewTree.replaceNode(oldNode,newNode);
  189. return this;
  190. }
  191. getChildren(node) {
  192. return this.ViewTree.getChildren(node);
  193. }
  194. import(data, parentNode) {
  195. let { tree } = data;
  196. if(!parentNode)
  197. this.ViewTree = new Tree();
  198. try {
  199. this.ViewTree.import(tree, Node, parentNode);
  200. this.defaultContainer = this.ViewTree.root;
  201. } catch(e) {
  202. log(e);
  203. throw new Error("ViewSystem import error : ", e);
  204. }
  205. return this;
  206. }
  207. export(node) {
  208. let tree = this.ViewTree.export(node);
  209. return {
  210. tree
  211. };
  212. }
  213. }
  214. /// Node = Container -> Everywhere Containers
  215. // Content is Node.content Container property.
  216. // When Content is present, Container for Containers is stretchedToContent
  217. // IF NOT -> just be placed there.
  218. //
  219. let Renderer = (props) => {
  220. let { node , tree, CoreSystem, selfView } = props;
  221. let childrens = tree.getChildren(node);
  222. let childrenReact = childrens.map(child => {
  223. return(
  224. <Renderer node = {child}
  225. tree={tree}
  226. key={child.id}
  227. CoreSystem={CoreSystem}
  228. selfView={selfView} />)
  229. });
  230. let ModuleSystem = CoreSystem.ModuleSystem;
  231. let EventSystem = CoreSystem.EventSystem;
  232. let viewNode = node.content;
  233. let ViewNodeReact;
  234. // IF CONTENT
  235. if(viewNode) {
  236. // Create ref
  237. ModuleSystem.createRef(viewNode.id);
  238. // Crete element
  239. ViewNodeReact = ModuleSystem.createElement(
  240. viewNode.value,
  241. {
  242. ...viewNode.props,
  243. ModuleID: viewNode.id,
  244. NodeID: node.id,
  245. EventSystem: EventSystem,
  246. CoreSystem: CoreSystem,
  247. View: selfView
  248. },
  249. null, // children
  250. viewNode.namespace,
  251. viewNode.id
  252. );
  253. }
  254. ModuleSystem.createRef(node.id);
  255. return ModuleSystem.createElementCtor(
  256. node.ctor,
  257. {
  258. ...node.props,
  259. depth: node.depth,
  260. content: ViewNodeReact,
  261. ID: node.id,
  262. CoreSystem: CoreSystem
  263. },
  264. childrenReact,
  265. node.id
  266. );
  267. }
  268. const styles = StyleSheet.create({
  269. colWrapper: {
  270. "position": "absolute",
  271. "left": 0,
  272. "bottom": 0,
  273. "right": 0,
  274. "top": 0
  275. }
  276. })