react-native-navigation的迁移库

index.js 10KB

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