CoreSystem.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import RoutingSystem from './RoutingSystem.js';
  2. import ViewSystem, { View } from './ViewSystem.js';
  3. import ModuleSystem from './ModuleSystem.js';
  4. import Modules from '../../modules';
  5. import EventSystem from './EventSystem';
  6. import ActionSystem from './ActionSystem';
  7. import * as Font from 'expo-font';
  8. export default class CoreSystem {
  9. constructor(dev = false) {
  10. // TODO -- Make correct Initialization
  11. this.Routing = new RoutingSystem();
  12. this.ModuleSystem = new ModuleSystem();
  13. this.EventSystem = new EventSystem();
  14. this.ActionSystem = new ActionSystem(this);
  15. this.ActionSystem.onUpdate(() => this.forceUpdate());
  16. this.ViewSystem = new ViewSystem(this);
  17. this.loadFonts()
  18. this.__loadModules(Modules);
  19. this._devMode = dev;
  20. }
  21. onUpdate(fn) {this._onUpdate = fn;}
  22. forceUpdate() {
  23. this._onUpdate && this._onUpdate();
  24. }
  25. fresh() {
  26. this.ActionSystem = new ActionSystem(this);
  27. this.ActionSystem.onUpdate(() => this.forceUpdate());
  28. this.Routing = new RoutingSystem();
  29. this.ModuleSystem = new ModuleSystem();
  30. this.EventSystem = new EventSystem();
  31. this.ViewSystem = new ViewSystem(this);
  32. let HomeView = new View();
  33. this.addPage('Home', HomeView, 'VSHome').setHome('Home');
  34. this.__loadModules(Modules);
  35. }
  36. loadFonts(){
  37. Font.loadAsync({
  38. 'black': require('../assets/fonts/SFCompactDisplay-Black_0.otf'),
  39. 'bold': require('../assets/fonts/SFCompactDisplay-Bold_0.otf'),
  40. 'heavy': require('../assets/fonts/SFCompactDisplay-Heavy_0.otf'),
  41. 'light': require('../assets/fonts/SFCompactDisplay-Light_0.otf'),
  42. 'medium': require('../assets/fonts/SFCompactDisplay-Medium_0.otf'),
  43. 'regular': require('../assets/fonts/SFCompactDisplay-Regular_0.otf'),
  44. 'semibold': require('../assets/fonts/SFCompactDisplay-Semibold_0.otf'),
  45. 'thin': require('../assets/fonts/SFCompactDisplay-Thin_0.otf'),
  46. 'ultralight': require('../assets/fonts/SFCompactDisplay-Ultralight_0.otf'),
  47. 'roboto-black':require('../assets/fonts/Roboto-Black.ttf'),
  48. 'roboto-black-italic':require('../assets/fonts/Roboto-BlackItalic.ttf'),
  49. 'roboto-bold':require('../assets/fonts/Roboto-Bold.ttf'),
  50. 'roboto-bold-italic':require('../assets/fonts/Roboto-BoldItalic.ttf'),
  51. 'roboto-light':require('../assets/fonts/Roboto-Light.ttf'),
  52. 'roboto-light-italic':require('../assets/fonts/Roboto-LightItalic.ttf'),
  53. 'roboto-medium':require('../assets/fonts/Roboto-Medium.ttf'),
  54. 'roboto-medium-italic':require('../assets/fonts/Roboto-MediumItalic.ttf'),
  55. 'roboto-regular':require('../assets/fonts/Roboto-Regular.ttf'),
  56. 'roboto-regular-italic':require('../assets/fonts/Roboto-RegularItalic.ttf'),
  57. 'roboto-thin':require('../assets/fonts/Roboto-Thin.ttf'),
  58. 'roboto-thin-italic':require('../assets/fonts/Roboto-ThinItalic.ttf')
  59. });
  60. }
  61. getCurrentView() {
  62. return this.ViewSystem.getView(this.Routing.getCurrentView());
  63. }
  64. setHome( route , View ) {
  65. this.Routing.setHome(route);
  66. return this;
  67. }
  68. goto( route ) {
  69. let res = this.Routing.goTo(route);
  70. this.forceUpdate();
  71. return res;
  72. }
  73. addPage( route , View, ViewID = Math.random()) {
  74. this.Routing.addRoute(route,route);
  75. this.Routing.setView(route,ViewID);
  76. this.ViewSystem.addView(ViewID, View);
  77. return this;
  78. }
  79. removePage(route) {
  80. this.Routing.removeRoute(route);
  81. return this;
  82. }
  83. goBack() {
  84. }
  85. __loadModules() {
  86. this.ModuleSystem.load(Modules);
  87. }
  88. addModule(mod, namespace) {
  89. this.ModuleSystem.loadModule(mod.name, mod, namespace);
  90. return this;
  91. }
  92. render() {
  93. let ViewID = this.Routing.getCurrentView();
  94. return this.ViewSystem.render(ViewID);
  95. }
  96. import(data) {
  97. // Perform dependency injection here!
  98. // must Convert Data to Structures before importing
  99. // Import Systems Seperately from leaf to root
  100. let {
  101. Routing,
  102. Views
  103. } = data;
  104. try {
  105. this.Routing.import(Routing);
  106. this.ViewSystem.import(Views);
  107. } catch(e) {
  108. throw new Error("CoreSystem cannot import data : ", e);
  109. }
  110. console.log("Successful import");
  111. }
  112. export() {
  113. let RS = this.Routing.export();
  114. let VS = this.ViewSystem.export();
  115. return {
  116. Routing: RS,
  117. Views: VS
  118. };
  119. }
  120. ray(data) {
  121. let {
  122. event
  123. } = data;
  124. let targets = event.path.map(item => findReactElement(item));
  125. let resViewNode = null;
  126. for (var i in targets) {
  127. if(targets[i] && targets[i].ID) {
  128. resViewNode = targets[i].ID;
  129. break;
  130. }
  131. }
  132. let ViewID = this.Routing.getCurrentView();
  133. return this.ViewSystem.getNode(ViewID, resViewNode);
  134. }
  135. }
  136. const findReactElement = (node) => {
  137. for (var key in node) {
  138. if (key.startsWith("__reactInternalInstance$")) {
  139. return (node[key]._debugOwner && node[key]._debugOwner.stateNode.props) || (node[key].return.memoizedProps);
  140. }
  141. }
  142. return null;
  143. };