3
0

CoreSystem.js 3.2 KB

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