3
0

CoreSystem.js 4.9 KB

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