2015-11-27 13:39:00 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule UIManager
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-09 15:20:08 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
2016-09-23 18:12:54 +00:00
|
|
|
const Platform = require('Platform');
|
2016-05-09 15:20:08 +00:00
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
2016-08-05 18:19:11 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-11-27 13:39:00 +00:00
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
const { UIManager } = NativeModules;
|
|
|
|
|
2016-08-05 18:19:11 +00:00
|
|
|
invariant(UIManager, 'UIManager is undefined. The native module config is probably incorrect.');
|
2016-02-23 10:26:11 +00:00
|
|
|
|
2017-03-25 07:37:51 +00:00
|
|
|
// In past versions of ReactNative users called UIManager.takeSnapshot()
|
|
|
|
// However takeSnapshot was moved to ReactNative in order to support flat
|
|
|
|
// bundles and to avoid a cyclic dependency between UIManager and ReactNative.
|
|
|
|
// UIManager.takeSnapshot still exists though. In order to avoid confusion or
|
|
|
|
// accidental usage, mask the method with a deprecation warning.
|
|
|
|
UIManager.__takeSnapshot = UIManager.takeSnapshot;
|
|
|
|
UIManager.takeSnapshot = function() {
|
|
|
|
invariant(
|
|
|
|
false,
|
|
|
|
'UIManager.takeSnapshot should not be called directly. ' +
|
|
|
|
'Use ReactNative.takeSnapshot instead.'
|
|
|
|
);
|
2016-02-23 10:26:11 +00:00
|
|
|
};
|
|
|
|
|
2016-05-09 15:20:08 +00:00
|
|
|
/**
|
|
|
|
* Copies the ViewManager constants and commands into UIManager. This is
|
|
|
|
* only needed for iOS, which puts the constants in the ViewManager
|
|
|
|
* namespace instead of UIManager, unlike Android.
|
|
|
|
*/
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
Object.keys(UIManager).forEach(viewName => {
|
|
|
|
const viewConfig = UIManager[viewName];
|
|
|
|
if (viewConfig.Manager) {
|
2016-09-23 18:12:54 +00:00
|
|
|
defineLazyObjectProperty(viewConfig, 'Constants', {
|
2016-05-09 15:20:08 +00:00
|
|
|
get: () => {
|
2016-09-23 18:12:54 +00:00
|
|
|
const viewManager = NativeModules[viewConfig.Manager];
|
2016-09-23 18:12:54 +00:00
|
|
|
const constants = {};
|
2016-05-09 15:20:08 +00:00
|
|
|
viewManager && Object.keys(viewManager).forEach(key => {
|
|
|
|
const value = viewManager[key];
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
constants[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return constants;
|
|
|
|
},
|
|
|
|
});
|
2016-09-23 18:12:54 +00:00
|
|
|
defineLazyObjectProperty(viewConfig, 'Commands', {
|
2016-05-09 15:20:08 +00:00
|
|
|
get: () => {
|
2016-09-23 18:12:54 +00:00
|
|
|
const viewManager = NativeModules[viewConfig.Manager];
|
2016-09-23 18:12:54 +00:00
|
|
|
const commands = {};
|
2016-05-09 15:20:08 +00:00
|
|
|
let index = 0;
|
|
|
|
viewManager && Object.keys(viewManager).forEach(key => {
|
|
|
|
const value = viewManager[key];
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
commands[key] = index++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return commands;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-10-07 12:31:30 +00:00
|
|
|
} else if (Platform.OS === 'android' && UIManager.AndroidLazyViewManagersEnabled) {
|
2017-01-20 23:53:42 +00:00
|
|
|
UIManager.ViewManagerNames.forEach(viewManagerName => {
|
|
|
|
defineLazyObjectProperty(UIManager, viewManagerName, {
|
|
|
|
get: () => NativeModules[viewManagerName.replace(/^(RCT|RK)/, '')],
|
|
|
|
});
|
|
|
|
});
|
2016-05-09 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:39:00 +00:00
|
|
|
module.exports = UIManager;
|