Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebViewScreen.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import WebView from "react-native-webview";
  5. import {ActivityIndicator, withTheme} from 'react-native-paper';
  6. import HeaderButton from "./HeaderButton";
  7. type Props = {
  8. navigation: Object,
  9. data: Array<{
  10. url: string,
  11. icon: string,
  12. name: string,
  13. customJS: string
  14. }>,
  15. headerTitle: string,
  16. hasHeaderBackButton: boolean,
  17. hasSideMenu: boolean,
  18. hasFooter: boolean,
  19. }
  20. /**
  21. * Class defining a webview screen.
  22. */
  23. class WebViewScreen extends React.PureComponent<Props> {
  24. static defaultProps = {
  25. hasBackButton: false,
  26. hasSideMenu: true,
  27. hasFooter: true,
  28. };
  29. webviewRef: Object;
  30. onRefreshClicked: Function;
  31. onWebviewRef: Function;
  32. onGoBackWebview: Function;
  33. onGoForwardWebview: Function;
  34. getRenderLoading: Function;
  35. colors: Object;
  36. constructor(props) {
  37. super(props);
  38. this.onRefreshClicked = this.onRefreshClicked.bind(this);
  39. this.onWebviewRef = this.onWebviewRef.bind(this);
  40. this.onGoBackWebview = this.onGoBackWebview.bind(this);
  41. this.onGoForwardWebview = this.onGoForwardWebview.bind(this);
  42. this.getRenderLoading = this.getRenderLoading.bind(this);
  43. this.colors = props.theme.colors;
  44. }
  45. componentDidMount() {
  46. const rightButton = this.getRefreshButton.bind(this);
  47. this.props.navigation.setOptions({
  48. headerRight: rightButton,
  49. });
  50. }
  51. getHeaderButton(clickAction: Function, icon: string) {
  52. return (
  53. <HeaderButton icon={icon} onPress={clickAction}/>
  54. );
  55. }
  56. getRefreshButton() {
  57. return (
  58. <View style={{
  59. flexDirection: 'row',
  60. marginRight: 10
  61. }}>
  62. {this.getHeaderButton(this.onRefreshClicked, 'refresh')}
  63. </View>
  64. );
  65. };
  66. onRefreshClicked() {
  67. if (this.webviewRef !== null)
  68. this.webviewRef.reload();
  69. }
  70. onGoBackWebview() {
  71. if (this.webviewRef !== null)
  72. this.webviewRef.goBack();
  73. }
  74. onGoForwardWebview() {
  75. if (this.webviewRef !== null)
  76. this.webviewRef.goForward();
  77. }
  78. onWebviewRef(ref: Object) {
  79. this.webviewRef = ref
  80. }
  81. getRenderLoading() {
  82. return (
  83. <View style={{
  84. backgroundColor: this.colors.background,
  85. position: 'absolute',
  86. top: 0,
  87. right: 0,
  88. width: '100%',
  89. height: '100%',
  90. flex: 1,
  91. alignItems: 'center',
  92. justifyContent: 'center'
  93. }}>
  94. <ActivityIndicator
  95. animating={true}
  96. size={'large'}
  97. color={this.colors.primary}/>
  98. </View>
  99. );
  100. }
  101. render() {
  102. // console.log("rendering WebViewScreen");
  103. return (
  104. <WebView
  105. ref={this.onWebviewRef}
  106. source={{uri: this.props.data[0]['url']}}
  107. style={{
  108. width: '100%',
  109. height: '100%',
  110. }}
  111. startInLoadingState={true}
  112. injectedJavaScript={this.props.data[0]['customJS']}
  113. javaScriptEnabled={true}
  114. renderLoading={this.getRenderLoading}
  115. />
  116. );
  117. }
  118. }
  119. export default withTheme(WebViewScreen);