react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. var OriginalReactNative = require('react-native');
  2. var RCCManager = OriginalReactNative.NativeModules.RCCManager;
  3. var NativeAppEventEmitter = OriginalReactNative.NativeAppEventEmitter;
  4. var utils = require('./utils');
  5. var Constants = require('./Constants');
  6. var resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');
  7. var processColor = OriginalReactNative.processColor;
  8. var _controllerRegistry = {};
  9. function _getRandomId() {
  10. return (Math.random()*1e20).toString(36);
  11. }
  12. function _processProperties(properties) {
  13. for (var property in properties) {
  14. if (properties.hasOwnProperty(property)) {
  15. if (property === 'icon' || property.endsWith('Icon') || property.endsWith('Image')) {
  16. properties[property] = resolveAssetSource(properties[property]);
  17. }
  18. if (property === 'color' || property.endsWith('Color')) {
  19. properties[property] = processColor(properties[property]);
  20. }
  21. if (property === 'buttons' || property.endsWith('Buttons')) {
  22. _processButtons(properties[property]);
  23. }
  24. }
  25. }
  26. }
  27. function _setListener(callbackId, func) {
  28. return NativeAppEventEmitter.addListener(callbackId, (...args) => func(...args));
  29. }
  30. function _processButtons(buttons) {
  31. if (!buttons) return;
  32. var unsubscribes = [];
  33. for (var i = 0 ; i < buttons.length ; i++) {
  34. buttons[i] = Object.assign({}, buttons[i]);
  35. var button = buttons[i];
  36. _processProperties(button);
  37. if (typeof button.onPress === "function") {
  38. var onPressId = _getRandomId();
  39. var onPressFunc = button.onPress;
  40. button.onPress = onPressId;
  41. var unsubscribe = _setListener(onPressId, onPressFunc);
  42. unsubscribes.push(unsubscribe);
  43. }
  44. }
  45. return function () {
  46. for (var i = 0 ; i < unsubscribes.length ; i++) {
  47. if (unsubscribes[i]) { unsubscribes[i](); }
  48. }
  49. };
  50. }
  51. function _validateDrawerProps(layout) {
  52. if (layout.type === "DrawerControllerIOS") {
  53. let shouldSetToDefault = true;
  54. const drawerProps = layout.props;
  55. if (drawerProps.type === "MMDrawer") {
  56. [ Constants.MMDRAWER_DOOR, Constants.MMDRAWER_PARALLAX, Constants.MMDRAWER_SLIDE, Constants.MMDRAWER_SLIDE_AND_SCALE].forEach(function(type) {
  57. if (type === drawerProps.animationType){
  58. shouldSetToDefault = false;
  59. }
  60. })
  61. }
  62. else if (drawerProps.type === "TheSideBar") {
  63. [Constants.THE_SIDEBAR_AIRBNB, Constants.THE_SIDEBAR_FACEBOOK, Constants.THE_SIDEBAR_LUVOCRACY, Constants.THE_SIDEBAR_WUNDER_LIST].forEach(function(type) {
  64. if (type === drawerProps.animationType){
  65. shouldSetToDefault = false;
  66. }
  67. })
  68. }
  69. if (shouldSetToDefault) {
  70. console.warn("Set to default type=MMDrawer animationType=slide");
  71. drawerProps.type = "MMDrawer";
  72. drawerProps.animationType = "slide";
  73. }
  74. }
  75. }
  76. var Controllers = {
  77. createClass: function (app) {
  78. return app;
  79. },
  80. hijackReact: function () {
  81. return {
  82. createElement: function(type, props) {
  83. var children = Array.prototype.slice.call(arguments, 2);
  84. var flatChildren = utils.flattenDeep(children);
  85. props = Object.assign({}, props);
  86. _processProperties(props);
  87. if (props['style']) {
  88. props['style'] = Object.assign({}, props['style']);
  89. _processProperties(props['style']);
  90. }
  91. return {
  92. 'type': type.name,
  93. 'props': props,
  94. 'children': flatChildren
  95. };
  96. },
  97. ControllerRegistry: Controllers.ControllerRegistry,
  98. TabBarControllerIOS: {name: 'TabBarControllerIOS', Item: {name: 'TabBarControllerIOS.Item'}},
  99. NavigationControllerIOS: {name: 'NavigationControllerIOS'},
  100. ViewControllerIOS: {name: 'ViewControllerIOS'},
  101. DrawerControllerIOS: {name: 'DrawerControllerIOS'},
  102. };
  103. },
  104. ControllerRegistry: {
  105. registerController: function (appKey, getControllerFunc) {
  106. _controllerRegistry[appKey] = getControllerFunc();
  107. },
  108. setRootController: function (appKey, animationType = 'none', passProps = {}) {
  109. var controller = _controllerRegistry[appKey];
  110. if (controller === undefined) return;
  111. var layout = controller.render();
  112. _validateDrawerProps(layout);
  113. RCCManager.setRootController(layout, animationType, passProps);
  114. }
  115. },
  116. NavigationControllerIOS: function (id) {
  117. return {
  118. push: function (params) {
  119. var unsubscribes = [];
  120. if (params['style']) {
  121. params['style'] = Object.assign({}, params['style']);
  122. _processProperties(params['style']);
  123. }
  124. if (params['titleImage']) {
  125. params['titleImage'] = resolveAssetSource(params['titleImage']);
  126. }
  127. if (params['leftButtons']) {
  128. var unsubscribe = _processButtons(params['leftButtons']);
  129. unsubscribes.push(unsubscribe);
  130. }
  131. if (params['rightButtons']) {
  132. var unsubscribe = _processButtons(params['rightButtons']);
  133. unsubscribes.push(unsubscribe);
  134. }
  135. RCCManager.NavigationControllerIOS(id, "push", params);
  136. return function() {
  137. for (var i = 0 ; i < unsubscribes.length ; i++) {
  138. if (unsubscribes[i]) { unsubscribes[i](); }
  139. }
  140. };
  141. },
  142. pop: function (params) {
  143. RCCManager.NavigationControllerIOS(id, "pop", params);
  144. },
  145. popToRoot: function (params) {
  146. RCCManager.NavigationControllerIOS(id, "popToRoot", params);
  147. },
  148. setTitle: function (params) {
  149. if (params['style']) {
  150. params['style'] = Object.assign({}, params['style']);
  151. _processProperties(params['style']);
  152. }
  153. if (params['titleImage']) {
  154. params['titleImage'] = resolveAssetSource(params['titleImage']);
  155. }
  156. RCCManager.NavigationControllerIOS(id, "setTitle", params);
  157. },
  158. resetTo: function (params) {
  159. var unsubscribes = [];
  160. if (params['style']) {
  161. params['style'] = Object.assign({}, params['style']);
  162. _processProperties(params['style']);
  163. }
  164. if (params['leftButtons']) {
  165. var unsubscribe = _processButtons(params['leftButtons']);
  166. unsubscribes.push(unsubscribe);
  167. }
  168. if (params['rightButtons']) {
  169. var unsubscribe = _processButtons(params['rightButtons']);
  170. unsubscribes.push(unsubscribe);
  171. }
  172. RCCManager.NavigationControllerIOS(id, "resetTo", params);
  173. return function() {
  174. for (var i = 0 ; i < unsubscribes.length ; i++) {
  175. if (unsubscribes[i]) { unsubscribes[i](); }
  176. }
  177. };
  178. },
  179. setLeftButton: function () {
  180. console.error('setLeftButton is deprecated, see setLeftButtons');
  181. },
  182. setLeftButtons: function (buttons, animated = false) {
  183. var unsubscribe = _processButtons(buttons);
  184. RCCManager.NavigationControllerIOS(id, "setButtons", {buttons: buttons, side: "left", animated: animated});
  185. return unsubscribe;
  186. },
  187. setRightButtons: function (buttons, animated = false) {
  188. var unsubscribe = _processButtons(buttons);
  189. RCCManager.NavigationControllerIOS(id, "setButtons", {buttons: buttons, side: "right", animated: animated});
  190. return unsubscribe;
  191. },
  192. setHidden: function(params = {}) {
  193. RCCManager.NavigationControllerIOS(id, "setHidden", params);
  194. }
  195. };
  196. },
  197. DrawerControllerIOS: function (id) {
  198. return {
  199. open: function (params) {
  200. return RCCManager.DrawerControllerIOS(id, "open", params);
  201. },
  202. close: function (params) {
  203. return RCCManager.DrawerControllerIOS(id, "close", params);
  204. },
  205. toggle: function (params) {
  206. return RCCManager.DrawerControllerIOS(id, "toggle", params);
  207. },
  208. setStyle: function (params) {
  209. return RCCManager.DrawerControllerIOS(id, "setStyle", params);
  210. }
  211. };
  212. },
  213. TabBarControllerIOS: function (id) {
  214. return {
  215. setHidden: function (params) {
  216. return RCCManager.TabBarControllerIOS(id, "setTabBarHidden", params);
  217. },
  218. setBadge: function (params) {
  219. return RCCManager.TabBarControllerIOS(id, "setBadge", params);
  220. },
  221. switchTo: function (params) {
  222. return RCCManager.TabBarControllerIOS(id, "switchTo", params);
  223. }
  224. };
  225. },
  226. Modal: {
  227. showLightBox: function(params) {
  228. params['style'] = Object.assign({}, params['style']);
  229. _processProperties(params['style']);
  230. RCCManager.modalShowLightBox(params);
  231. },
  232. dismissLightBox: function() {
  233. RCCManager.modalDismissLightBox();
  234. },
  235. showController: function(appKey, animationType = 'slide-up', passProps = {}) {
  236. var controller = _controllerRegistry[appKey];
  237. if (controller === undefined) return;
  238. var layout = controller.render();
  239. _validateDrawerProps(layout);
  240. RCCManager.showController(layout, animationType, passProps);
  241. },
  242. dismissController: function(animationType = 'slide-down') {
  243. RCCManager.dismissController(animationType);
  244. },
  245. dismissAllControllers: function(animationType = 'slide-down') {
  246. RCCManager.dismissAllControllers(animationType);
  247. }
  248. },
  249. Notification: {
  250. show: async function(params = {}) {
  251. await RCCManager.showNotification(params);
  252. },
  253. dismiss: async function(params = {}) {
  254. await RCCManager.dismissNotification(params);
  255. },
  256. AnimationPresets: {
  257. default: {
  258. animated: true,
  259. duration: 0.5,
  260. damping: 0.65,
  261. type: 'slide-down',
  262. fade: true
  263. },
  264. simple: {
  265. animated: true,
  266. duration: 0.3,
  267. type: 'slide-down',
  268. fade: true
  269. },
  270. swing: {
  271. animated: true,
  272. duration: 0.65,
  273. damping: 0.6,
  274. type: 'swing'
  275. },
  276. fade: {
  277. animated: true,
  278. duration: 0.3,
  279. fade: true
  280. }
  281. }
  282. },
  283. NavigationToolBarIOS: OriginalReactNative.requireNativeComponent('RCCToolBar', null),
  284. Constants: Constants
  285. };
  286. module.exports = Controllers;