CoreSystem.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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() {
  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. }
  17. onUpdate(fn) {this._onUpdate = fn;}
  18. forceUpdate() {
  19. this._onUpdate && this._onUpdate();
  20. }
  21. loadFonts(){
  22. Font.loadAsync({
  23. 'black': require('../assets/fonts/SFCompactDisplay-Black_0.otf'),
  24. 'bold': require('../assets/fonts/SFCompactDisplay-Bold_0.otf'),
  25. 'heavy': require('../assets/fonts/SFCompactDisplay-Heavy_0.otf'),
  26. 'light': require('../assets/fonts/SFCompactDisplay-Light_0.otf'),
  27. 'medium': require('../assets/fonts/SFCompactDisplay-Medium_0.otf'),
  28. 'regular': require('../assets/fonts/SFCompactDisplay-Regular_0.otf'),
  29. 'semibold': require('../assets/fonts/SFCompactDisplay-Semibold_0.otf'),
  30. 'thin': require('../assets/fonts/SFCompactDisplay-Thin_0.otf'),
  31. 'ultralight': require('../assets/fonts/SFCompactDisplay-Ultralight_0.otf')
  32. });
  33. }
  34. getCurrentView() {
  35. return this.ViewSystem.getView(this.Routing.getCurrentView());
  36. }
  37. setHome( route , View ) {
  38. this.Routing.setHome(route);
  39. return this;
  40. }
  41. goto( route ) {
  42. return this.Routing.goTo(route);
  43. }
  44. addPage( route , View, ViewID = Math.random()) {
  45. this.Routing.addRoute(route,route);
  46. this.Routing.setView(route,ViewID);
  47. this.ViewSystem.addView(ViewID, View);
  48. return this;
  49. }
  50. goBack() {
  51. }
  52. __loadModules() {
  53. this.ModuleSystem.load(Modules);
  54. }
  55. addModule(mod, namespace) {
  56. this.ModuleSystem.loadModule(mod.name, mod, namespace);
  57. return this;
  58. }
  59. render() {
  60. let ViewID = this.Routing.getCurrentView();
  61. return this.ViewSystem.render(ViewID);
  62. }
  63. import(data) {
  64. // Perform dependency injection here!
  65. // must Convert Data to Structures before importing
  66. // Import Systems Seperately from leaf to root
  67. let {
  68. Routing,
  69. Views
  70. } = data;
  71. try {
  72. this.Routing.import(Routing);
  73. this.ViewSystem.import(Views);
  74. } catch(e) {
  75. throw new Error("CoreSystem cannot import data : ", e);
  76. }
  77. console.log("Successful import");
  78. }
  79. export() {
  80. let RS = this.Routing.export();
  81. let VS = this.ViewSystem.export();
  82. return {
  83. Routing: RS,
  84. Views: VS
  85. };
  86. }
  87. ray(data) {
  88. let {
  89. event
  90. } = data;
  91. let targets = event.path.map(item => findReactElement(item));
  92. let resViewNode = null;
  93. for (var i in targets) {
  94. if(targets[i] && targets[i].ID) {
  95. resViewNode = targets[i].ID;
  96. break;
  97. }
  98. }
  99. let ViewID = this.Routing.getCurrentView();
  100. return this.ViewSystem.getNode(ViewID, resViewNode);
  101. }
  102. }
  103. const findReactElement = (node) => {
  104. for (var key in node) {
  105. if (key.startsWith("__reactInternalInstance$")) {
  106. return (node[key]._debugOwner && node[key]._debugOwner.stateNode.props) || (node[key].return.memoizedProps);
  107. }
  108. }
  109. return null;
  110. };