jsactions/core/EventRouter.js

  1. /**
  2. * @license
  3. * Copyright (c) 2023 Gaurang Lade
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /**
  26. * EventRouter holds and manages array of event routes.
  27. * Event Routes are used by ViewNavigator to find associated View and Viewstack
  28. *
  29. * Viewnavigator find View and ViewStack based on Routes info set in EventRouter Object see Example
  30. *
  31. */
  32. class EventRouter {
  33. /**
  34. * @example Sample Event Router Object is in "SimpleNavigator" class (subclass of ViewNavigator)
  35. initEventRoutes() {
  36. let evtRoutes = [
  37. { navEvent: " Login_NavEvent ", viewstackId: “LoginStack", viewId: “loginView", path: "/login" },
  38. { navEvent: " Register_NavEvent ", viewstackId: “LoginStack", viewId: “registerView", path: "/login" },
  39. { navEvent: " ForgotPwd_NavEvent", viewstackId: “LoginStack", viewId: “forgotpwdView", path: "/login" }
  40. ]
  41. this.eventRouter = new EventRouter(evtRoutes);
  42. }
  43. Here "navEvent" is Event Name, "viewstackId" is ViewStack Id, "viewId" is ViewId and "path" is routeID by which ViewNavigator associated.
  44. *
  45. * @param {array} [_routes=[]] - Events routes array
  46. * @memberof EventRouter
  47. */
  48. constructor(_routes = []) {
  49. this.routes = _routes;
  50. }
  51. /**
  52. *
  53. * @description Adds new Event Route
  54. * @param {string} _navEvent - Navigation Event Name
  55. * @param {string} _viewstackId - ViewStack ID
  56. * @param {string} _viewId - View ID
  57. * @param {string} _path - Navigator Route path associted
  58. * @memberof EventRouter
  59. */
  60. addRoute(_navEvent, _viewstackId, _viewId, _path) {
  61. let route = {};
  62. route.navEvent = _navEvent;
  63. route.viewstackId = _viewstackId;
  64. route.viewId = _viewId;
  65. route.path = _path;
  66. this.routes.push(route);
  67. }
  68. /**
  69. *
  70. * @description find Event Route using Naviagtion EventName
  71. * @param {string} _navEvent
  72. * @returns {Object} Event Route Object
  73. * @memberof EventRouter
  74. */
  75. findRoute(_navEvent) {
  76. let tmpRoute = [];
  77. for (let x = 0; x < this.routes.length; x++) {
  78. if (this.routes[x].navEvent == _navEvent)
  79. tmpRoute[x] = this.routes[x];
  80. }
  81. return tmpRoute;
  82. }
  83. /**
  84. *
  85. * @description finds associated view by Navigation EventName and Path
  86. * @param {string} _navEvent
  87. * @param {string} _path
  88. * @returns {string} - ViewID
  89. * @memberof EventRouter
  90. */
  91. findViewId(_navEvent, _path) {
  92. let tmpViewId = [];
  93. for (let i = 0; i < this.routes.length; i++) {
  94. if ((this.routes[i].navEvent == _navEvent) && (this.routes[i].path == _path))
  95. tmpViewId = this.routes[i].viewId;
  96. }
  97. return tmpViewId;
  98. }
  99. /**
  100. *
  101. * @description finds associated viewstack by Navigation EventName and Path
  102. * @param {string} _navEvent
  103. * @param {string} _path
  104. * @returns {string} - ViewStackID
  105. * @memberof EventRouter
  106. */
  107. findViewStackId(_navEvent, _path) {
  108. let tmpViewStackId = null;
  109. for (let j = 0; j < this.routes.length; j++) {
  110. if ((this.routes[j].navEvent == _navEvent) && (this.routes[j].path == _path))
  111. tmpViewStackId = this.routes[j].viewstackId;
  112. }
  113. return tmpViewStackId;
  114. }
  115. /**
  116. *
  117. * @description Remove and Resets existing event routes
  118. * @memberof EventRouter
  119. */
  120. reset() {
  121. this.routes = [];
  122. }
  123. /**
  124. *
  125. * @description prints Event Routes
  126. * @memberof EventRouter
  127. */
  128. printRoutes() {
  129. if (this.routes.length > 0) {
  130. for (let i = 0; i < this.routes.length; i++) {
  131. console.log("navEvent:" + this.routes[i].navEvent + " viewstackId:" + this.routes[i].viewstackId + " viewId:" + this.routes[i].viewId);
  132. }
  133. } else {
  134. console.log("No Routes Found");
  135. }
  136. }
  137. }
  138. export default EventRouter;