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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import WebView from "react-native-webview";
  5. import Touchable from "react-native-platform-touchable";
  6. import {MaterialCommunityIcons} from "@expo/vector-icons";
  7. import ThemeManager from "../utils/ThemeManager";
  8. import {ActivityIndicator} from 'react-native-paper';
  9. type Props = {
  10. navigation: Object,
  11. data: Array<{
  12. url: string,
  13. icon: string,
  14. name: string,
  15. customJS: string
  16. }>,
  17. headerTitle: string,
  18. hasHeaderBackButton: boolean,
  19. hasSideMenu: boolean,
  20. hasFooter: boolean,
  21. }
  22. /**
  23. * Class defining a webview screen.
  24. */
  25. export default class WebViewScreen extends React.Component<Props> {
  26. static defaultProps = {
  27. hasBackButton: false,
  28. hasSideMenu: true,
  29. hasFooter: true,
  30. };
  31. webviewArray: Array<WebView> = [];
  32. onRefreshClicked: Function;
  33. onWebviewRef: Function;
  34. onGoBackWebview: Function;
  35. onGoForwardWebview: Function;
  36. onOpenWebLink: Function;
  37. constructor() {
  38. super();
  39. this.onRefreshClicked = this.onRefreshClicked.bind(this);
  40. this.onWebviewRef = this.onWebviewRef.bind(this);
  41. this.onGoBackWebview = this.onGoBackWebview.bind(this);
  42. this.onGoForwardWebview = this.onGoForwardWebview.bind(this);
  43. this.onOpenWebLink = this.onOpenWebLink.bind(this);
  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. <Touchable
  54. style={{padding: 6}}
  55. onPress={clickAction}>
  56. <MaterialCommunityIcons
  57. name={icon}
  58. size={26}
  59. color={ThemeManager.getCurrentThemeVariables().text}/>
  60. </Touchable>
  61. );
  62. }
  63. getRefreshButton() {
  64. return (
  65. <View style={{
  66. flexDirection: 'row',
  67. marginRight: 10
  68. }}>
  69. {this.getHeaderButton(this.onRefreshClicked, 'refresh')}
  70. </View>
  71. );
  72. };
  73. onRefreshClicked() {
  74. for (let view of this.webviewArray) {
  75. if (view !== null)
  76. view.reload();
  77. }
  78. }
  79. onGoBackWebview() {
  80. for (let view of this.webviewArray) {
  81. if (view !== null)
  82. view.goBack();
  83. }
  84. }
  85. onGoForwardWebview() {
  86. for (let view of this.webviewArray) {
  87. if (view !== null)
  88. view.goForward();
  89. }
  90. }
  91. onOpenWebLink() {
  92. this.openWebLink(this.props.data[0]['url'])
  93. }
  94. onWebviewRef(ref: WebView) {
  95. this.webviewArray.push(ref)
  96. }
  97. getRenderLoading() {
  98. return (
  99. <View style={{
  100. backgroundColor: ThemeManager.getCurrentThemeVariables().background,
  101. position: 'absolute',
  102. top: 0,
  103. right: 0,
  104. width: '100%',
  105. height: '100%',
  106. flex: 1,
  107. alignItems: 'center',
  108. justifyContent: 'center'
  109. }}>
  110. <ActivityIndicator
  111. animating={true}
  112. size={'large'}
  113. color={ThemeManager.getCurrentThemeVariables().primary}/>
  114. </View>
  115. );
  116. }
  117. getWebview(obj: Object) {
  118. return (
  119. <WebView
  120. ref={this.onWebviewRef}
  121. source={{uri: obj['url']}}
  122. style={{
  123. width: '100%',
  124. height: '100%',
  125. }}
  126. startInLoadingState={true}
  127. injectedJavaScript={obj['customJS']}
  128. javaScriptEnabled={true}
  129. renderLoading={this.getRenderLoading}
  130. />
  131. );
  132. }
  133. render() {
  134. // console.log("rendering WebViewScreen");
  135. this.webviewArray = [];
  136. return (
  137. this.getWebview(this.props.data[0])
  138. );
  139. }
  140. }