3
0

CoreSystem.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. export default class CoreSystem {
  7. constructor() {
  8. // TODO -- Make correct Initialization
  9. this.Routing = new RoutingSystem();
  10. this.ModuleSystem = new ModuleSystem();
  11. this.EventSystem = new EventSystem();
  12. this.ViewSystem = new ViewSystem(this.ModuleSystem, this.EventSystem);
  13. this.__loadModules(Modules);
  14. }
  15. getCurrentView() {
  16. return this.ViewSystem.getView(this.Routing.getCurrentView());
  17. }
  18. setHome( route , View ) {
  19. this.Routing.setHome(route);
  20. return this;
  21. }
  22. goto( route ) {
  23. return this.Routing.goTo(route);
  24. }
  25. addPage( route , View, ViewID = Math.random()) {
  26. this.Routing.addRoute(route,route);
  27. this.Routing.setView(route,ViewID);
  28. this.ViewSystem.addView(ViewID, View);
  29. return this;
  30. }
  31. goBack() {
  32. }
  33. __loadModules() {
  34. this.ModuleSystem.load(Modules);
  35. }
  36. addModule(mod, namespace) {
  37. this.ModuleSystem.loadModule(mod.name, mod, namespace);
  38. return this;
  39. }
  40. render() {
  41. let ViewID = this.Routing.getCurrentView();
  42. return this.ViewSystem.render(ViewID);
  43. }
  44. import(data) {
  45. // Perform dependency injection here!
  46. // must Convert Data to Structures before importing
  47. // Import Systems Seperately from leaf to root
  48. let {
  49. Routing,
  50. Views
  51. } = data;
  52. try {
  53. this.Routing.import(Routing);
  54. this.ViewSystem.import(Views);
  55. } catch(e) {
  56. throw new Error("CoreSystem cannot import data : ", e);
  57. }
  58. console.log("Successful import");
  59. }
  60. export() {
  61. let RS = this.Routing.export();
  62. let VS = this.ViewSystem.export();
  63. return {
  64. Routing: RS,
  65. Views: VS
  66. };
  67. }
  68. ray(data) {
  69. let {
  70. event
  71. } = data;
  72. let targets = event.path.map(item => findReactElement(item));
  73. let resViewNode = null;
  74. for (var i in targets) {
  75. if(targets[i] && targets[i].ID) {
  76. resViewNode = targets[i].ID;
  77. break;
  78. }
  79. }
  80. let ViewID = this.Routing.getCurrentView();
  81. return this.ViewSystem.getNode(ViewID, resViewNode);
  82. }
  83. }
  84. const findReactElement = (node) => {
  85. for (var key in node) {
  86. if (key.startsWith("__reactInternalInstance$")) {
  87. return (node[key]._debugOwner && node[key]._debugOwner.stateNode.props) || (node[key].return.memoizedProps);
  88. }
  89. }
  90. return null;
  91. };