react-native-navigation的迁移库

platformSpecificDeprecated.ios.js 20KB

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