react-native-navigation的迁移库

platformSpecificDeprecated.ios.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /*eslint-disable*/
  2. import {Component} from 'react';
  3. import {findNodeHandle} from 'react-native';
  4. import Navigation from './../Navigation';
  5. import Controllers, {Modal, Notification, ScreenUtils} from './controllers';
  6. const React = Controllers.hijackReact();
  7. const {
  8. ControllerRegistry,
  9. TabBarControllerIOS,
  10. NavigationControllerIOS,
  11. DrawerControllerIOS
  12. } = React;
  13. import _ from 'lodash';
  14. import PropRegistry from '../PropRegistry';
  15. async function startTabBasedApp(params) {
  16. if (!params.tabs) {
  17. console.error('startTabBasedApp(params): params.tabs is required');
  18. return;
  19. }
  20. const controllerID = _.uniqueId('controllerID');
  21. params.tabs.map(function (tab, index) {
  22. const navigatorID = controllerID + '_nav' + index;
  23. const screenInstanceID = _.uniqueId('screenInstanceID');
  24. const components = tab.components;
  25. if (!tab.screen && !components) {
  26. console.error('startTabBasedApp(params): every tab must include a screen property, take a look at tab#' + (index + 1));
  27. return;
  28. }
  29. if (components) {
  30. params.tabs[index].components = components;
  31. Object.assign(tab, components[0]);
  32. components.shift();
  33. components.forEach(component => {
  34. const screenInstanceID = _.uniqueId('screenInstanceID');
  35. const {
  36. navigatorStyle,
  37. navigatorButtons,
  38. navigatorEventID
  39. } = _mergeScreenSpecificSettings(component.screen, screenInstanceID, params);
  40. _saveNavigatorButtonsProps(navigatorButtons);
  41. _saveNavBarComponentProps(navigatorStyle);
  42. const passProps = Object.assign({}, component.passProps);
  43. passProps.navigatorID = navigatorID;
  44. passProps.screenInstanceID = screenInstanceID;
  45. passProps.navigatorEventID = navigatorEventID;
  46. component.navigationParams = {
  47. screenInstanceID,
  48. navigatorStyle,
  49. navigatorButtons,
  50. navigatorEventID,
  51. navigatorID: navigatorID,
  52. passProps
  53. };
  54. component.subtitle = params.subtitle;
  55. component.passProps = passProps;
  56. component.navigatorStyle = navigatorStyle;
  57. savePassProps(component);
  58. });
  59. }
  60. const {
  61. navigatorStyle,
  62. navigatorButtons,
  63. navigatorEventID
  64. } = _mergeScreenSpecificSettings(tab.screen, screenInstanceID, tab);
  65. _saveNavigatorButtonsProps(navigatorButtons);
  66. _saveNavBarComponentProps(navigatorStyle);
  67. tab.navigationParams = {
  68. screenInstanceID,
  69. navigatorStyle,
  70. navigatorButtons,
  71. navigatorEventID,
  72. navigatorID
  73. };
  74. });
  75. const Controller = Controllers.createClass({
  76. render: function () {
  77. if (!params.drawer || (!params.drawer.left && !params.drawer.right)) {
  78. return this.renderBody();
  79. } else {
  80. const navigatorID = controllerID + '_drawer';
  81. const leftScreenId = _.uniqueId('screenInstanceID');
  82. const rightScreenId = _.uniqueId('screenInstanceID')
  83. const {navigatorStyle: leftNavigatorStyle} = params.drawer.left
  84. ? _mergeScreenSpecificSettings(params.drawer.left.screen, leftScreenId, params.drawer.left)
  85. : {};
  86. const {navigatorStyle: rightNavigatorStyle} = params.drawer.right
  87. ? _mergeScreenSpecificSettings(params.drawer.right.screen, rightScreenId, params.drawer.right)
  88. : {};
  89. return (
  90. <DrawerControllerIOS id={navigatorID}
  91. componentLeft={params.drawer.left ? params.drawer.left.screen : undefined}
  92. styleLeft={leftNavigatorStyle}
  93. passPropsLeft={{navigatorID: navigatorID}}
  94. componentRight={params.drawer.right ? params.drawer.right.screen : undefined}
  95. styleRight={rightNavigatorStyle}
  96. passPropsRight={{navigatorID: navigatorID}}
  97. disableOpenGesture={params.drawer.disableOpenGesture}
  98. type={params.drawer.type ? params.drawer.type : 'MMDrawer'}
  99. animationType={params.drawer.animationType ? params.drawer.animationType : 'slide'}
  100. style={params.drawer.style}
  101. appStyle={params.appStyle}
  102. >
  103. {this.renderBody()}
  104. </DrawerControllerIOS>
  105. );
  106. }
  107. },
  108. renderBody: function () {
  109. return (
  110. <TabBarControllerIOS
  111. id={controllerID + '_tabs'}
  112. style={params.tabsStyle}
  113. appStyle={params.appStyle}
  114. initialTabIndex={params.initialTabIndex}>
  115. {
  116. params.tabs.map(function (tab, index) {
  117. return (
  118. <TabBarControllerIOS.Item {...tab} title={tab.label}>
  119. <NavigationControllerIOS
  120. id={tab.navigationParams.navigatorID}
  121. title={tab.title}
  122. subtitle={tab.subtitle}
  123. titleImage={tab.titleImage}
  124. component={tab.screen}
  125. components={tab.components}
  126. passProps={{
  127. navigatorID: tab.navigationParams.navigatorID,
  128. screenInstanceID: tab.navigationParams.screenInstanceID,
  129. navigatorEventID: tab.navigationParams.navigatorEventID,
  130. }}
  131. style={tab.navigationParams.navigatorStyle}
  132. leftButtons={tab.navigationParams.navigatorButtons.leftButtons}
  133. rightButtons={tab.navigationParams.navigatorButtons.rightButtons}
  134. />
  135. </TabBarControllerIOS.Item>
  136. );
  137. })
  138. }
  139. </TabBarControllerIOS>
  140. );
  141. }
  142. });
  143. savePassProps(params);
  144. _.set(params, 'passProps.timestamp', Date.now());
  145. ControllerRegistry.registerController(controllerID, () => Controller);
  146. return await ControllerRegistry.setRootController(controllerID, params.animationType, params.passProps || {});
  147. }
  148. async function startSingleScreenApp(params) {
  149. const components = params.components;
  150. let screen = params.screen;
  151. if (!screen && !components) {
  152. console.error('startSingleScreenApp(params): params.screen is required');
  153. return;
  154. }
  155. const controllerID = _.uniqueId('controllerID');
  156. const navigatorID = controllerID + '_nav';
  157. if (components) {
  158. screen = components[0];
  159. components.shift();
  160. components.forEach(component => {
  161. const screenInstanceID = _.uniqueId('screenInstanceID');
  162. const {
  163. navigatorStyle,
  164. navigatorButtons,
  165. navigatorEventID
  166. } = _mergeScreenSpecificSettings(component.screen, screenInstanceID, params);
  167. _saveNavigatorButtonsProps(navigatorButtons);
  168. _saveNavBarComponentProps(navigatorStyle);
  169. const passProps = Object.assign({}, params.passProps);
  170. passProps.navigatorID = navigatorID;
  171. passProps.screenInstanceID = screenInstanceID;
  172. passProps.navigatorEventID = navigatorEventID;
  173. component.navigationParams = {
  174. screenInstanceID,
  175. navigatorStyle,
  176. navigatorButtons,
  177. navigatorEventID,
  178. navigatorID: navigatorID,
  179. passProps
  180. };
  181. component.subtitle = params.subtitle;
  182. component.passProps = passProps;
  183. savePassProps(component);
  184. });
  185. }
  186. if (!screen.screen) {
  187. console.error('startSingleScreenApp(params): screen must include a screen property');
  188. return;
  189. }
  190. const screenInstanceID = _.uniqueId('screenInstanceID');
  191. const {
  192. navigatorStyle,
  193. navigatorButtons,
  194. navigatorEventID
  195. } = _mergeScreenSpecificSettings(screen.screen, screenInstanceID, screen);
  196. _saveNavigatorButtonsProps(navigatorButtons);
  197. _saveNavBarComponentProps(navigatorStyle);
  198. params.navigationParams = {
  199. screenInstanceID,
  200. navigatorStyle,
  201. navigatorButtons,
  202. navigatorEventID,
  203. navigatorID
  204. };
  205. const passProps = {
  206. navigatorID: navigatorID,
  207. screenInstanceID: screenInstanceID,
  208. navigatorEventID: navigatorEventID,
  209. ...screen.passProps
  210. };
  211. const Controller = Controllers.createClass({
  212. render: function () {
  213. if (!params.drawer || (!params.drawer.left && !params.drawer.right)) {
  214. return this.renderBody();
  215. } else {
  216. const navigatorID = controllerID + '_drawer';
  217. return (
  218. <DrawerControllerIOS id={navigatorID}
  219. componentLeft={params.drawer.left ? params.drawer.left.screen : undefined}
  220. passPropsLeft={{navigatorID: navigatorID}}
  221. componentRight={params.drawer.right ? params.drawer.right.screen : undefined}
  222. passPropsRight={{navigatorID: navigatorID}}
  223. disableOpenGesture={params.drawer.disableOpenGesture}
  224. type={params.drawer.type ? params.drawer.type : 'MMDrawer'}
  225. animationType={params.drawer.animationType ? params.drawer.animationType : 'slide'}
  226. style={params.drawer.style}
  227. appStyle={params.appStyle}
  228. >
  229. {this.renderBody()}
  230. </DrawerControllerIOS>
  231. );
  232. }
  233. },
  234. renderBody: function () {
  235. return (
  236. <NavigationControllerIOS
  237. id={navigatorID}
  238. title={screen.title}
  239. subtitle={params.subtitle}
  240. titleImage={screen.titleImage}
  241. component={screen.screen}
  242. components={components}
  243. passProps={passProps}
  244. style={navigatorStyle}
  245. leftButtons={navigatorButtons.leftButtons}
  246. rightButtons={navigatorButtons.rightButtons}
  247. appStyle={params.appStyle}
  248. />
  249. );
  250. }
  251. });
  252. savePassProps(params);
  253. ControllerRegistry.registerController(controllerID, () => Controller);
  254. return await ControllerRegistry.setRootController(controllerID, params.animationType, params.passProps || {});
  255. }
  256. function _mergeScreenSpecificSettings(screenID, screenInstanceID, params) {
  257. const screenClass = Navigation.getRegisteredScreen(screenID);
  258. if (!screenClass) {
  259. console.error('Cannot create screen ' + screenID + '. Are you it was registered with Navigation.registerScreen?');
  260. return;
  261. }
  262. const navigatorStyle = Object.assign({}, screenClass.navigatorStyle);
  263. if (params.navigatorStyle) {
  264. Object.assign(navigatorStyle, params.navigatorStyle);
  265. }
  266. let navigatorEventID = screenInstanceID + '_events';
  267. let navigatorButtons = _.cloneDeep(screenClass.navigatorButtons);
  268. if (params.navigatorButtons) {
  269. navigatorButtons = _.cloneDeep(params.navigatorButtons);
  270. }
  271. if (navigatorButtons.leftButtons) {
  272. for (let i = 0; i < navigatorButtons.leftButtons.length; i++) {
  273. navigatorButtons.leftButtons[i].onPress = navigatorEventID;
  274. }
  275. }
  276. if (navigatorButtons.rightButtons) {
  277. for (let i = 0; i < navigatorButtons.rightButtons.length; i++) {
  278. navigatorButtons.rightButtons[i].onPress = navigatorEventID;
  279. }
  280. }
  281. return {navigatorStyle, navigatorButtons, navigatorEventID};
  282. }
  283. function navigatorPush(navigator, params) {
  284. if (!params.screen) {
  285. console.error('Navigator.push(params): params.screen is required');
  286. return;
  287. }
  288. let previewViewID;
  289. const screenInstanceID = _.uniqueId('screenInstanceID');
  290. if (params.previewView instanceof Component) {
  291. previewViewID = findNodeHandle(params.previewView)
  292. } else if (typeof params.previewView === 'number') {
  293. previewViewID = params.previewView;
  294. } else if (params.previewView) {
  295. console.error('Navigator.push(params): params.previewView is not a valid react view');
  296. }
  297. const {
  298. navigatorStyle,
  299. navigatorButtons,
  300. navigatorEventID
  301. } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
  302. _saveNavigatorButtonsProps(navigatorButtons);
  303. _saveNavBarComponentProps(navigatorStyle);
  304. const passProps = Object.assign({}, params.passProps);
  305. passProps.navigatorID = navigator.navigatorID;
  306. passProps.screenInstanceID = screenInstanceID;
  307. passProps.navigatorEventID = navigatorEventID;
  308. passProps.previewViewID = previewViewID;
  309. passProps.isPreview = !!previewViewID;
  310. params.navigationParams = {
  311. screenInstanceID,
  312. navigatorStyle,
  313. navigatorButtons,
  314. navigatorEventID,
  315. navigatorID: navigator.navigatorID
  316. };
  317. savePassProps(params);
  318. Controllers.NavigationControllerIOS(navigator.navigatorID).push({
  319. title: params.title,
  320. subtitle: params.subtitle,
  321. titleImage: params.titleImage,
  322. component: params.screen,
  323. animated: params.animated,
  324. animationType: params.animationType,
  325. passProps: passProps,
  326. style: navigatorStyle,
  327. backButtonTitle: params.backButtonTitle,
  328. backButtonHidden: params.backButtonHidden,
  329. leftButtons: navigatorButtons.leftButtons,
  330. rightButtons: navigatorButtons.rightButtons,
  331. previewViewID: previewViewID,
  332. previewActions: params.previewActions,
  333. previewHeight: params.previewHeight,
  334. previewCommit: params.previewCommit,
  335. timestamp: Date.now()
  336. });
  337. }
  338. function navigatorPop(navigator, params) {
  339. Controllers.NavigationControllerIOS(navigator.navigatorID).pop({
  340. animated: params.animated,
  341. animationType: params.animationType,
  342. timestamp: Date.now()
  343. });
  344. }
  345. function navigatorPopToRoot(navigator, params) {
  346. Controllers.NavigationControllerIOS(navigator.navigatorID).popToRoot({
  347. animated: params.animated,
  348. animationType: params.animationType
  349. });
  350. }
  351. function navigatorResetTo(navigator, params) {
  352. if (!params.screen) {
  353. console.error('Navigator.resetTo(params): params.screen is required');
  354. return;
  355. }
  356. const screenInstanceID = _.uniqueId('screenInstanceID');
  357. const {
  358. navigatorStyle,
  359. navigatorButtons,
  360. navigatorEventID
  361. } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
  362. _saveNavigatorButtonsProps(navigatorButtons);
  363. _saveNavBarComponentProps(navigatorStyle);
  364. const passProps = Object.assign({}, params.passProps);
  365. passProps.navigatorID = navigator.navigatorID;
  366. passProps.screenInstanceID = screenInstanceID;
  367. passProps.navigatorEventID = navigatorEventID;
  368. params.navigationParams = {
  369. screenInstanceID,
  370. navigatorStyle,
  371. navigatorButtons,
  372. navigatorEventID,
  373. navigatorID: navigator.navigatorID
  374. };
  375. savePassProps(params);
  376. Controllers.NavigationControllerIOS(navigator.navigatorID).resetTo({
  377. title: params.title,
  378. subtitle: params.subtitle,
  379. titleImage: params.titleImage,
  380. component: params.screen,
  381. animated: params.animated,
  382. animationType: params.animationType,
  383. passProps: passProps,
  384. style: navigatorStyle,
  385. leftButtons: navigatorButtons.leftButtons,
  386. rightButtons: navigatorButtons.rightButtons
  387. });
  388. }
  389. function navigatorSetDrawerEnabled(navigator, params) {
  390. const controllerID = navigator.navigatorID.split('_')[0];
  391. Controllers.NavigationControllerIOS(controllerID + '_drawer').setDrawerEnabled(params)
  392. }
  393. function navigatorSetTitle(navigator, params) {
  394. Controllers.NavigationControllerIOS(navigator.navigatorID).setTitle({
  395. title: params.title,
  396. subtitle: params.subtitle,
  397. titleImage: params.titleImage,
  398. style: params.navigatorStyle,
  399. isSetSubtitle: false
  400. });
  401. }
  402. function navigatorSetSubtitle(navigator, params) {
  403. Controllers.NavigationControllerIOS(navigator.navigatorID).setTitle({
  404. title: params.title,
  405. subtitle: params.subtitle,
  406. titleImage: params.titleImage,
  407. style: params.navigatorStyle,
  408. isSetSubtitle: true
  409. });
  410. }
  411. function navigatorSetTitleImage(navigator, params) {
  412. Controllers.NavigationControllerIOS(navigator.navigatorID).setTitleImage({
  413. titleImage: params.titleImage
  414. });
  415. }
  416. function navigatorToggleNavBar(navigator, params) {
  417. Controllers.NavigationControllerIOS(navigator.navigatorID).setHidden({
  418. hidden: ((params.to === 'hidden') ? true : false),
  419. animated: params.animated
  420. });
  421. }
  422. function navigatorSetStyle(navigator, params) {
  423. _saveNavBarComponentProps(params);
  424. Controllers.NavigationControllerIOS(navigator.navigatorID).setStyle(params)
  425. }
  426. function navigatorToggleDrawer(navigator, params) {
  427. const controllerID = navigator.navigatorID.split('_')[0];
  428. if (params.to == 'open') {
  429. Controllers.DrawerControllerIOS(controllerID + '_drawer').open({
  430. side: params.side,
  431. animated: params.animated
  432. });
  433. } else if (params.to == 'closed') {
  434. Controllers.DrawerControllerIOS(controllerID + '_drawer').close({
  435. side: params.side,
  436. animated: params.animated
  437. });
  438. } else {
  439. Controllers.DrawerControllerIOS(controllerID + '_drawer').toggle({
  440. side: params.side,
  441. animated: params.animated
  442. });
  443. }
  444. }
  445. function navigatorToggleTabs(navigator, params) {
  446. const controllerID = navigator.navigatorID.split('_')[0];
  447. Controllers.TabBarControllerIOS(controllerID + '_tabs').setHidden({
  448. hidden: params.to == 'hidden',
  449. animated: !(params.animated === false)
  450. });
  451. }
  452. function navigatorSetTabBadge(navigator, params) {
  453. const controllerID = navigator.navigatorID.split('_')[0];
  454. if (params.tabIndex || params.tabIndex === 0) {
  455. Controllers.TabBarControllerIOS(controllerID + '_tabs').setBadge({
  456. tabIndex: params.tabIndex,
  457. badge: params.badge,
  458. badgeColor: params.badgeColor
  459. });
  460. } else {
  461. Controllers.TabBarControllerIOS(controllerID + '_tabs').setBadge({
  462. contentId: navigator.navigatorID,
  463. contentType: 'NavigationControllerIOS',
  464. badge: params.badge
  465. });
  466. }
  467. }
  468. function navigatorSetTabButton(navigator, params) {
  469. const controllerID = navigator.navigatorID.split('_')[0];
  470. if (params.tabIndex || params.tabIndex === 0) {
  471. Controllers.TabBarControllerIOS(controllerID + '_tabs').setTabButton({
  472. tabIndex: params.tabIndex,
  473. icon: params.icon,
  474. selectedIcon: params.selectedIcon,
  475. label: params.label,
  476. });
  477. } else {
  478. Controllers.TabBarControllerIOS(controllerID + '_tabs').setTabButton({
  479. contentId: navigator.navigatorID,
  480. contentType: 'NavigationControllerIOS',
  481. icon: params.icon,
  482. selectedIcon: params.selectedIcon,
  483. label: params.label,
  484. });
  485. }
  486. }
  487. function navigatorSwitchToTab(navigator, params) {
  488. const controllerID = navigator.navigatorID.split('_')[0];
  489. if (params.tabIndex || params.tabIndex === 0) {
  490. Controllers.TabBarControllerIOS(controllerID + '_tabs').switchTo({
  491. tabIndex: params.tabIndex
  492. });
  493. } else {
  494. Controllers.TabBarControllerIOS(controllerID + '_tabs').switchTo({
  495. contentId: navigator.navigatorID,
  496. contentType: 'NavigationControllerIOS'
  497. });
  498. }
  499. }
  500. function navigatorSetButtons(navigator, navigatorEventID, params) {
  501. _saveNavigatorButtonsProps(params);
  502. if (params.leftButtons) {
  503. const buttons = params.leftButtons.slice(); // clone
  504. for (let i = 0; i < buttons.length; i++) {
  505. buttons[i].onPress = navigatorEventID;
  506. }
  507. Controllers.NavigationControllerIOS(navigator.navigatorID).setLeftButtons(buttons, params.animated);
  508. }
  509. if (params.rightButtons) {
  510. const buttons = params.rightButtons.slice(); // clone
  511. for (let i = 0; i < buttons.length; i++) {
  512. buttons[i].onPress = navigatorEventID;
  513. }
  514. Controllers.NavigationControllerIOS(navigator.navigatorID).setRightButtons(buttons, params.animated);
  515. }
  516. }
  517. function showModal(params) {
  518. if (!params.screen) {
  519. console.error('showModal(params): params.screen is required');
  520. return;
  521. }
  522. const controllerID = _.uniqueId('controllerID');
  523. const navigatorID = controllerID + '_nav';
  524. const screenInstanceID = _.uniqueId('screenInstanceID');
  525. const {
  526. navigatorStyle,
  527. navigatorButtons,
  528. navigatorEventID
  529. } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
  530. _saveNavigatorButtonsProps(navigatorButtons);
  531. _saveNavBarComponentProps(navigatorStyle);
  532. const passProps = Object.assign({}, params.passProps);
  533. passProps.navigatorID = navigatorID;
  534. passProps.screenInstanceID = screenInstanceID;
  535. passProps.navigatorEventID = navigatorEventID;
  536. passProps.timestamp = Date.now();
  537. params.navigationParams = {
  538. screenInstanceID,
  539. navigatorStyle,
  540. navigatorButtons,
  541. navigatorEventID,
  542. navigatorID: navigator.navigatorID
  543. };
  544. const Controller = Controllers.createClass({
  545. render: function () {
  546. return (
  547. <NavigationControllerIOS
  548. id={navigatorID}
  549. title={params.title}
  550. subtitle={params.subtitle}
  551. titleImage={params.titleImage}
  552. component={params.screen}
  553. passProps={passProps}
  554. style={navigatorStyle}
  555. leftButtons={navigatorButtons.leftButtons}
  556. rightButtons={navigatorButtons.rightButtons} />
  557. );
  558. }
  559. });
  560. savePassProps(params);
  561. ControllerRegistry.registerController(controllerID, () => Controller);
  562. Modal.showController(controllerID, params.animationType);
  563. }
  564. async function dismissModal(params) {
  565. return await Modal.dismissController(params.animationType);
  566. }
  567. async function dismissAllModals(params) {
  568. return await Modal.dismissAllControllers(params.animationType);
  569. }
  570. function showLightBox(params) {
  571. if (!params.screen) {
  572. console.error('showLightBox(params): params.screen is required');
  573. return;
  574. }
  575. const controllerID = _.uniqueId('controllerID');
  576. const navigatorID = controllerID + '_nav';
  577. const screenInstanceID = _.uniqueId('screenInstanceID');
  578. const {
  579. navigatorStyle,
  580. navigatorButtons,
  581. navigatorEventID
  582. } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
  583. const passProps = Object.assign({}, params.passProps);
  584. passProps.navigatorID = navigatorID;
  585. passProps.screenInstanceID = screenInstanceID;
  586. passProps.navigatorEventID = navigatorEventID;
  587. params.navigationParams = {
  588. screenInstanceID,
  589. navigatorStyle,
  590. navigatorButtons,
  591. navigatorEventID,
  592. navigatorID
  593. };
  594. savePassProps(params);
  595. Modal.showLightBox({
  596. component: params.screen,
  597. passProps: passProps,
  598. style: params.style
  599. });
  600. }
  601. function dismissLightBox(params) {
  602. Modal.dismissLightBox();
  603. }
  604. function showInAppNotification(params) {
  605. if (!params.screen) {
  606. console.error('showInAppNotification(params): params.screen is required');
  607. return;
  608. }
  609. const controllerID = _.uniqueId('controllerID');
  610. const navigatorID = controllerID + '_nav';
  611. const screenInstanceID = _.uniqueId('screenInstanceID');
  612. const {
  613. navigatorStyle,
  614. navigatorButtons,
  615. navigatorEventID
  616. } = _mergeScreenSpecificSettings(params.screen, screenInstanceID, params);
  617. const passProps = Object.assign({}, params.passProps);
  618. passProps.navigatorID = navigatorID;
  619. passProps.screenInstanceID = screenInstanceID;
  620. passProps.navigatorEventID = navigatorEventID;
  621. params.navigationParams = {
  622. screenInstanceID,
  623. navigatorStyle,
  624. navigatorButtons,
  625. navigatorEventID,
  626. navigatorID
  627. };
  628. savePassProps(params);
  629. let args = {
  630. component: params.screen,
  631. passProps: passProps,
  632. style: params.style,
  633. animation: params.animation || Notification.AnimationPresets.default,
  634. position: params.position,
  635. shadowRadius: params.shadowRadius,
  636. dismissWithSwipe: params.dismissWithSwipe || true,
  637. autoDismissTimerSec: params.autoDismissTimerSec || 5
  638. };
  639. if (params.autoDismiss === false) delete args.autoDismissTimerSec;
  640. Notification.show(args);
  641. }
  642. function dismissInAppNotification(params) {
  643. Notification.dismiss(params);
  644. }
  645. function savePassProps(params) {
  646. //TODO this needs to be handled in a common place,
  647. //TODO also, all global passProps should be handled differently
  648. if (params.navigationParams && params.passProps) {
  649. PropRegistry.save(params.navigationParams.screenInstanceID, params.passProps);
  650. }
  651. if (params.screen && params.screen.passProps) {
  652. PropRegistry.save(params.screen.navigationParams.screenInstanceID, params.screen.passProps);
  653. }
  654. if (_.get(params, 'screen.topTabs')) {
  655. _.forEach(params.screen.topTabs, (tab) => savePassProps(tab));
  656. }
  657. if (params.tabs) {
  658. _.forEach(params.tabs, (tab) => {
  659. if (!tab.passProps) {
  660. tab.passProps = params.passProps;
  661. }
  662. savePassProps(tab);
  663. });
  664. }
  665. }
  666. function showContextualMenu() {
  667. // Android only
  668. }
  669. function dismissContextualMenu() {
  670. // Android only
  671. }
  672. async function getCurrentlyVisibleScreenId() {
  673. return await ScreenUtils.getCurrentlyVisibleScreenId();
  674. }
  675. function _saveNavBarComponentProps(navigatorStyle) {
  676. if (navigatorStyle.navBarCustomViewInitialProps) {
  677. const passPropsKey = _.uniqueId('navBarComponent');
  678. PropRegistry.save(passPropsKey, navigatorStyle.navBarCustomViewInitialProps);
  679. navigatorStyle.navBarCustomViewInitialProps = {passPropsKey};
  680. }
  681. }
  682. function _saveNavigatorButtonsProps({rightButtons, leftButtons}) {
  683. _saveNavigatorButtonsPassProps(rightButtons);
  684. _saveNavigatorButtonsPassProps(leftButtons);
  685. }
  686. function _saveNavigatorButtonsPassProps(buttons = []) {
  687. buttons.forEach((button) => {
  688. if (button.component) {
  689. const passPropsKey = _.uniqueId('customButtonComponent');
  690. PropRegistry.save(passPropsKey, button.passProps);
  691. button.passProps = {passPropsKey};
  692. }
  693. })
  694. }
  695. async function getLaunchArgs() {
  696. return await ControllerRegistry.getLaunchArgs();
  697. }
  698. export default {
  699. startTabBasedApp,
  700. startSingleScreenApp,
  701. navigatorPush,
  702. navigatorPop,
  703. navigatorPopToRoot,
  704. navigatorResetTo,
  705. showModal,
  706. dismissModal,
  707. dismissAllModals,
  708. showLightBox,
  709. dismissLightBox,
  710. showInAppNotification,
  711. dismissInAppNotification,
  712. navigatorSetButtons,
  713. navigatorSetDrawerEnabled,
  714. navigatorSetTitle,
  715. navigatorSetSubtitle,
  716. navigatorSetStyle,
  717. navigatorSetTitleImage,
  718. navigatorToggleDrawer,
  719. navigatorToggleTabs,
  720. navigatorSetTabBadge,
  721. navigatorSetTabButton,
  722. navigatorSwitchToTab,
  723. navigatorToggleNavBar,
  724. showContextualMenu,
  725. dismissContextualMenu,
  726. getCurrentlyVisibleScreenId,
  727. getLaunchArgs
  728. };