react-native-navigation的迁移库

LayoutTreeCrawler.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const _ = require('lodash');
  2. const LayoutTypes = require('./LayoutTypes');
  3. const OptionsProcessor = require('./OptionsProcessor');
  4. class LayoutTreeCrawler {
  5. constructor(uniqueIdProvider, store) {
  6. this.uniqueIdProvider = uniqueIdProvider;
  7. this.store = store;
  8. this.crawl = this.crawl.bind(this);
  9. }
  10. crawl(node) {
  11. this._assertKnownLayoutType(node.type);
  12. node.id = this.uniqueIdProvider.generate(node.type);
  13. node.data = node.data || {};
  14. node.children = node.children || [];
  15. if (_.isEqual(node.type, LayoutTypes.Component)) {
  16. this._handleContainer(node);
  17. }
  18. _.forEach(node.children, this.crawl);
  19. }
  20. _handleContainer(node) {
  21. this._assertContainerDataName(node);
  22. this._savePropsToStore(node);
  23. this._applyStaticNavigationOptions(node);
  24. OptionsProcessor.processOptions(node.data.navigationOptions);
  25. }
  26. _savePropsToStore(node) {
  27. this.store.setPropsForContainerId(node.id, node.data.passProps);
  28. }
  29. _applyStaticNavigationOptions(node) {
  30. const clazz = this.store.getOriginalContainerClassForName(node.data.name) || {};
  31. const staticOptions = _.cloneDeep(clazz.navigationOptions) || {};
  32. const passedOptions = _.cloneDeep(node.data.navigationOptions) || {};
  33. node.data.navigationOptions = _.merge({}, staticOptions, passedOptions);
  34. }
  35. _assertKnownLayoutType(type) {
  36. if (!_.includes(LayoutTypes, type)) {
  37. throw new Error(`Unknown layout type ${type}`);
  38. }
  39. }
  40. _assertContainerDataName(container) {
  41. if (!container.data.name) {
  42. throw new Error('Missing container data.name');
  43. }
  44. }
  45. }
  46. module.exports = LayoutTreeCrawler;