CoreSystem.js 4.4 KB

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