3
0

ViewSystem.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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(MS, ES) {
  30. this.views = {};
  31. this.ModuleSystem = MS;
  32. this.EventSystem = ES;
  33. }
  34. getNode(ViewId, NodeId) {
  35. return this.views[ViewId].getNode(NodeId);
  36. }
  37. getView(name) {
  38. return this.views[name];
  39. }
  40. addView(name, view) {
  41. this.views[name] = view;
  42. return view;
  43. }
  44. removeView(name) {
  45. let view = this.views[name];
  46. delete this.views[name];
  47. return view;
  48. }
  49. render(name) {
  50. return this.views[name].render(this.ModuleSystem, this.EventSystem);
  51. }
  52. export() {
  53. let views = {};
  54. for(var i in this.views) {
  55. views[i] = this.views[i].export();
  56. }
  57. return {
  58. views
  59. }
  60. }
  61. import(data) {
  62. let { views } = data;
  63. for(var i in views) {
  64. this.views[i] = new View().import(views[i]);
  65. }
  66. return this;
  67. }
  68. ray(viewId, x, y, id) {
  69. let tree = this.views[viewId].ViewTree;
  70. }
  71. }
  72. export class View {
  73. constructor(){
  74. this.ViewTree = new Tree();
  75. let rootNode = new Node(Math.random(), {stretchContainer: true, selected: true}); // row
  76. this.addViewNode(rootNode);
  77. // this.setColumns(rootNode, 1);
  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(MS, ES) {
  94. let node = this.ViewTree.root;
  95. return (<Renderer ModuleSystem={MS} EventSystem={ES} node={node} tree={this.ViewTree}/>);
  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) {
  195. let { tree } = data;
  196. this.ViewTree = new Tree();
  197. try {
  198. this.ViewTree.import(tree, Node);
  199. this.defaultContainer = this.ViewTree.root;
  200. } catch(e) {
  201. log(e);
  202. throw new Error("ViewSystem import error : ", e);
  203. }
  204. return this;
  205. }
  206. export() {
  207. let tree = this.ViewTree.export();
  208. return {
  209. tree
  210. };
  211. }
  212. }
  213. /// Node = Container -> Everywhere Containers
  214. // Content is Node.content Container property.
  215. // When Content is present, Container for Containers is stretchedToContent
  216. // IF NOT -> just be placed there.
  217. //
  218. let Renderer = (props) => {
  219. let { node , tree, ModuleSystem, EventSystem } = props;
  220. let childrens = tree.getChildren(node);
  221. let childrenReact = childrens.map(child => {
  222. return(
  223. <Renderer node = {child}
  224. tree={tree}
  225. key={child.id}
  226. ModuleSystem={ModuleSystem}
  227. EventSystem={EventSystem} />)
  228. });
  229. let viewNode = node.content;
  230. let ViewNodeReact;
  231. // IF CONTENT
  232. if(viewNode) {
  233. // Create ref
  234. ModuleSystem.createRef(viewNode.id);
  235. // Crete element
  236. ViewNodeReact = ModuleSystem.createElement(
  237. viewNode.value,
  238. {
  239. ...viewNode.props,
  240. ModuleID: viewNode.id,
  241. EventSystem: EventSystem
  242. },
  243. [], // children
  244. viewNode.namespace,
  245. viewNode.id
  246. );
  247. }
  248. ModuleSystem.createRef(node.id);
  249. return ModuleSystem.createElementCtor(
  250. node.ctor,
  251. {
  252. ...node.props,
  253. depth: node.depth,
  254. content: ViewNodeReact,
  255. ID: node.id
  256. },
  257. childrenReact,
  258. node.id
  259. );
  260. }
  261. const styles = StyleSheet.create({
  262. colWrapper: {
  263. "position": "absolute",
  264. "left": 0,
  265. "bottom": 0,
  266. "right": 0,
  267. "top": 0
  268. }
  269. })