3
0

ViewSystem.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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: false, selected: true}); // row
  76. this.addViewNode(rootNode);
  77. this.defaultContainer = rootNode;
  78. }
  79. getDefaultContainer() {
  80. return this.defaultContainer;
  81. }
  82. getRoot() {
  83. return this.ViewTree.root;
  84. }
  85. getNode(id) {
  86. return this.ViewTree.getNode(id);
  87. }
  88. has(node) {
  89. if (typeof node !== "object") return this.getNode(node);
  90. return this.getNode(node.id);
  91. }
  92. render(MS, ES) {
  93. let node = this.ViewTree.root;
  94. return (<Renderer ModuleSystem={MS} EventSystem={ES} node={node} tree={this.ViewTree}/>);
  95. }
  96. addViewNode(node,parentNode = null){
  97. this.ViewTree.insert(node,parentNode);
  98. return this;
  99. }
  100. setContent(content, container) {
  101. container.content = content;
  102. return this;
  103. }
  104. getParent(node) {
  105. return this.getNode(this.ViewTree.getParent(node));
  106. }
  107. getRows(column) {
  108. log("Deprecated for now")
  109. return true;
  110. if(!column.isCol) {
  111. throw new Error("You must pass a ColNode to this function");
  112. }
  113. return this.getChildren(column);
  114. }
  115. setRows(column, rows = 1) {
  116. log("Deprecated for now")
  117. return true;
  118. if(!column.isCol) throw new Error("You must pass a ColNode to this function");
  119. if(rows < 1) throw new Error("Only positive rows");
  120. let colChildren = this.getChildren(column);
  121. if (colChildren.length > rows) {
  122. for(var i = rows; i < colChildren.length; i++) {
  123. let row = colChildren[i];
  124. if(row.content) {
  125. if(confirm("Are you sure you want to remove this row it has content?"))
  126. this.remove(row);
  127. } else {
  128. this.remove(row); }
  129. }
  130. } else {
  131. for(var i = colChildren.length; i < rows; i++)
  132. this.addViewNode(new Node(Math.random(), true), column); // row
  133. }
  134. return this.getRows(column);
  135. }
  136. getColumns(row) {
  137. log("Deprecated for now")
  138. return true;
  139. if(!row.isRow) {
  140. throw new Error("You must pass a RowNode to this function");
  141. }
  142. return this.getChildren(row);
  143. }
  144. setColumns(row, columns = 1) {
  145. log("Deprecated for now")
  146. return true;
  147. if(!row.isRow) throw new Error("You must pass a RowNode to this function");
  148. if(columns < 1) throw new Error("Only positive cols");
  149. let rowChildren = this.getChildren(row);
  150. if (rowChildren.length > columns) {
  151. for(var i = columns; i < rowChildren.length; i++) {
  152. let col = rowChildren[i];
  153. if(col.content) {
  154. if(confirm("Are you sure you want to remove this col it has content?"))
  155. this.remove(col);
  156. } else {
  157. this.remove(col);
  158. }
  159. }
  160. } else {
  161. for(var i = rowChildren.length; i < columns; i++) {
  162. this.addViewNode(new Node(Math.random()), row);
  163. }
  164. }
  165. return this.getColumns(row);
  166. }
  167. addGrid(column, rowSize, columnSize) {
  168. log("Deprecated for now")
  169. return true;
  170. this.setRows(column, rowSize)
  171. .forEach(row => this.setColumns(row, columnSize)
  172. .forEach(col => col.content = new ViewNode(
  173. Math.random(),
  174. "BaseContainer",
  175. {}
  176. )));
  177. }
  178. remove(node) {
  179. if(node === root) {
  180. console.log("Cannot remove Root node", node);
  181. return this;
  182. }
  183. this.ViewTree.remove(node);
  184. return this;
  185. }
  186. replace(oldNode,newNode){
  187. this.ViewTree.replaceNode(oldNode,newNode);
  188. return this;
  189. }
  190. getChildren(node) {
  191. return this.ViewTree.getChildren(node);
  192. }
  193. import(data, parentNode) {
  194. let { tree } = data;
  195. if(!parentNode)
  196. this.ViewTree = new Tree();
  197. try {
  198. this.ViewTree.import(tree, Node, parentNode);
  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(node) {
  207. let tree = this.ViewTree.export(node);
  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. console.log(viewNode.value)
  234. // Create ref
  235. ModuleSystem.createRef(viewNode.id);
  236. // Crete element
  237. ViewNodeReact = ModuleSystem.createElement(
  238. viewNode.value,
  239. {
  240. ...viewNode.props,
  241. ModuleID: viewNode.id,
  242. EventSystem: EventSystem
  243. },
  244. [], // children
  245. viewNode.namespace,
  246. viewNode.id
  247. );
  248. }
  249. ModuleSystem.createRef(node.id);
  250. return ModuleSystem.createElementCtor(
  251. node.ctor,
  252. {
  253. ...node.props,
  254. depth: node.depth,
  255. content: ViewNodeReact,
  256. ID: node.id
  257. },
  258. childrenReact,
  259. node.id
  260. );
  261. }
  262. const styles = StyleSheet.create({
  263. colWrapper: {
  264. "position": "absolute",
  265. "left": 0,
  266. "bottom": 0,
  267. "right": 0,
  268. "top": 0
  269. }
  270. })