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