Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. import { Navigation } from 'react-native-navigation';
  2.  
  3. class SFNavigation {
  4.  
  5. static createRootStackWithDrawer(componentName: string, drawerName: string, componentId: string) {
  6. return {
  7. root: {
  8. sideMenu: {
  9. left: {
  10. component: {
  11. name: drawerName,
  12. },
  13. },
  14. center: {
  15. stack: {
  16. children: [
  17. {
  18. component: {
  19. id: componentId,
  20. name: componentName,
  21. },
  22. },
  23. ],
  24. },
  25. },
  26. },
  27. },
  28. };
  29. }
  30.  
  31. static createRootStack(componentName: string, componentId: string) {
  32. return {
  33. root: {
  34. stack: {
  35. children: [{
  36. component: {
  37. id: componentId,
  38. name: componentName,
  39. },
  40. }],
  41. },
  42. },
  43. };
  44. }
  45.  
  46. static createScreenId(): string {
  47. const randomString = Math.random().toString(36).substr(2);
  48. const now = Date.now();
  49. return `SfScreenId-${randomString}.${now}`;
  50. }
  51.  
  52. private componentIds: string[] = [];
  53. private drawerComponent: string = '';
  54.  
  55. constructor() {
  56. Navigation.events().registerAppLaunchedListener(() => {
  57. Navigation.setDefaultOptions({
  58. topBar: {
  59. visible: false,
  60. height: 0,
  61. },
  62. });
  63. });
  64. }
  65.  
  66. public setDrawerComponent(componentName: string) {
  67. this.drawerComponent = componentName;
  68. }
  69.  
  70. public pop(componentId?: string) {
  71. const id = componentId || this.getLastComponentId();
  72. Navigation.pop(id);
  73. this.componentIds.pop();
  74. }
  75.  
  76. public push(componentName: string, componentId?: string) {
  77. const id = componentId || SFNavigation.createScreenId();
  78. const currentId = this.getLastComponentId();
  79. Navigation.push(currentId, {
  80. component: {
  81. id,
  82. name: componentName,
  83. },
  84. });
  85. this.componentIds.push(id);
  86. }
  87.  
  88. public resetTo(componentName: string, withDrawer: boolean = false) {
  89. if (withDrawer && !!this.drawerComponent)
  90. return this.resetWithDrawer(componentName);
  91. return this.resetWithoutDrawer(componentName);
  92. }
  93.  
  94. private resetWithDrawer(componentName: string, drawerComponent?: string) {
  95. const id = SFNavigation.createScreenId();
  96. this.componentIds = [id];
  97. const drawer = drawerComponent || this.drawerComponent;
  98. Navigation.setRoot(
  99. SFNavigation.createRootStackWithDrawer(
  100. componentName,
  101. drawer,
  102. id,
  103. ));
  104. }
  105.  
  106. private resetWithoutDrawer(componentName: string) {
  107. const id = SFNavigation.createScreenId();
  108. this.componentIds = [id];
  109. Navigation.setRoot(SFNavigation.createRootStack(componentName, id));
  110. }
  111.  
  112. private getLastComponentId(): string {
  113. return this.componentIds[this.componentIds.length - 1] || '';
  114. }
  115.  
  116. }
  117.  
  118. const nav: SFNavigation = new SFNavigation();
  119. export default nav;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement