Nav apraksta

index.ios.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict'
  2. import React, {
  3. Component,
  4. PropTypes
  5. } from 'react';
  6. import {
  7. Dimensions,
  8. View,
  9. WebView
  10. } from 'react-native';
  11. import ImmutableComponent from 'react-immutable-component';
  12. export default class AutoHeightWebView extends ImmutableComponent {
  13. constructor(props) {
  14. super(props);
  15. this.handleNavigationStateChange = this.handleNavigationStateChange.bind(this);
  16. const initialScript = props.files ? this.appendFilesToHead(props.files, BaseScript) : BaseScript;
  17. this.state = {
  18. height: 0,
  19. script: initialScript
  20. };
  21. }
  22. componentWillReceiveProps(nextProps) {
  23. let currentScript = BaseScript;
  24. if (nextProps.files) {
  25. currentScript = this.appendFilesToHead(nextProps.files, BaseScript);
  26. }
  27. this.setState({ script: currentScript });
  28. }
  29. appendFilesToHead(files, script) {
  30. if (!files) {
  31. return script;
  32. }
  33. for (let file of files) {
  34. script =
  35. `
  36. var link = document.createElement('link');
  37. link.rel = '` + file.rel + `';
  38. link.type = '` + file.type + `';
  39. link.href = '` + file.href + `';
  40. document.head.appendChild(link);
  41. `+ script;
  42. }
  43. return script;
  44. }
  45. handleNavigationStateChange(navState) {
  46. const height = Number(navState.title);
  47. if (height) {
  48. this.setState({ height });
  49. if (this.props.onHeightUpdated) {
  50. this.props.onHeightUpdated(height);
  51. }
  52. }
  53. }
  54. render() {
  55. const { height, script } = this.state;
  56. const { source, heightOffset, customScript, style } = this.props;
  57. const webViewSource = Object.assign({}, source, { baseUrl: 'web/' });
  58. return (
  59. <View style={[{
  60. width: ScreenWidth,
  61. height: height + heightOffset,
  62. backgroundColor: 'transparent'
  63. }, style]}>
  64. <WebView
  65. style={{
  66. flex: 1,
  67. backgroundColor: 'transparent'
  68. }}
  69. injectedJavaScript={script + customScript}
  70. scrollEnabled={false}
  71. source={webViewSource}
  72. onNavigationStateChange={this.handleNavigationStateChange} />
  73. </View>
  74. );
  75. }
  76. }
  77. AutoHeightWebView.propTypes = {
  78. source: WebView.propTypes.source,
  79. onHeightUpdated: PropTypes.func,
  80. customScript: PropTypes.string,
  81. // offset rn webview margin
  82. heightOffset: PropTypes.number,
  83. style: View.propTypes.style,
  84. // add web/files... to project root
  85. files: PropTypes.arrayOf(PropTypes.shape({
  86. href: PropTypes.string,
  87. type: PropTypes.string,
  88. rel: PropTypes.string
  89. }))
  90. }
  91. AutoHeightWebView.defaultProps = {
  92. heightOffset: 12
  93. }
  94. const ScreenWidth = Dimensions.get('window').width;
  95. const BaseScript =
  96. `
  97. ; (function () {
  98. var wrapper = document.createElement('div');
  99. wrapper.id = 'height-wrapper';
  100. while (document.body.firstChild) {
  101. wrapper.appendChild(document.body.firstChild);
  102. }
  103. document.body.appendChild(wrapper);
  104. var i = 0;
  105. function updateHeight() {
  106. document.title = wrapper.clientHeight;
  107. window.location.hash = ++i;
  108. }
  109. updateHeight();
  110. window.addEventListener('load', updateHeight);
  111. window.addEventListener('resize', updateHeight);
  112. } ());
  113. `;