Application Android et IOS pour l'amicale des élèves
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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // @flow
  2. import * as React from 'react';
  3. import {Linking, Platform, View} from 'react-native';
  4. import {Spinner, Footer, Right, Left, Body, Tab, TabHeading, Text, Tabs} from 'native-base';
  5. import WebView from "react-native-webview";
  6. import Touchable from "react-native-platform-touchable";
  7. import CustomMaterialIcon from "../components/CustomMaterialIcon";
  8. import ThemeManager from "../utils/ThemeManager";
  9. import BaseContainer from "../components/BaseContainer";
  10. import {NavigationActions} from 'react-navigation';
  11. type Props = {
  12. navigation: Object,
  13. data: Array<{
  14. url: string,
  15. icon: string,
  16. name: string,
  17. customJS: string
  18. }>,
  19. headerTitle: string,
  20. hasHeaderBackButton: boolean,
  21. hasSideMenu: boolean,
  22. hasFooter: boolean,
  23. }
  24. /**
  25. * Class defining a webview screen.
  26. */
  27. export default class WebViewScreen extends React.Component<Props> {
  28. static defaultProps = {
  29. hasBackButton: false,
  30. hasSideMenu: true,
  31. hasFooter: true,
  32. };
  33. webviewArray: Array<WebView> = [];
  34. openWebLink(url: string) {
  35. Linking.openURL(url).catch((err) => console.error('Error opening link', err));
  36. }
  37. getHeaderButton(clickAction: Function, icon: string) {
  38. return (
  39. <Touchable
  40. style={{padding: 6}}
  41. onPress={() => clickAction()}>
  42. <CustomMaterialIcon
  43. color={Platform.OS === 'ios' ? ThemeManager.getCurrentThemeVariables().brandPrimary : "#fff"}
  44. icon={icon}/>
  45. </Touchable>
  46. );
  47. }
  48. getRefreshButton() {
  49. return (
  50. <View style={{flexDirection: 'row'}}>
  51. {this.getHeaderButton(() => this.refreshWebview(), 'refresh')}
  52. </View>
  53. );
  54. };
  55. refreshWebview() {
  56. for (let view of this.webviewArray) {
  57. if (view !== null)
  58. view.reload();
  59. }
  60. }
  61. goBackWebview() {
  62. for (let view of this.webviewArray) {
  63. if (view !== null)
  64. view.goBack();
  65. }
  66. }
  67. goForwardWebview() {
  68. for (let view of this.webviewArray) {
  69. if (view !== null)
  70. view.goForward();
  71. }
  72. }
  73. getWebview(obj: Object) {
  74. return (
  75. <WebView
  76. ref={ref => (this.webviewArray.push(ref))}
  77. source={{uri: obj['url']}}
  78. style={{
  79. width: '100%',
  80. height: '100%',
  81. }}
  82. startInLoadingState={true}
  83. injectedJavaScript={obj['customJS']}
  84. javaScriptEnabled={true}
  85. renderLoading={() =>
  86. <View style={{
  87. backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor,
  88. position: 'absolute',
  89. top: 0,
  90. right: 0,
  91. width: '100%',
  92. height: '100%',
  93. flex: 1,
  94. alignItems: 'center',
  95. justifyContent: 'center'
  96. }}>
  97. <Spinner/>
  98. </View>
  99. }
  100. />
  101. );
  102. }
  103. getTabbedWebview() {
  104. let tabbedView = [];
  105. for (let i = 0; i < this.props.data.length; i++) {
  106. tabbedView.push(
  107. <Tab heading={
  108. <TabHeading>
  109. <CustomMaterialIcon
  110. icon={this.props.data[i]['icon']}
  111. color={ThemeManager.getCurrentThemeVariables().tabIconColor}
  112. fontSize={20}
  113. />
  114. <Text>{this.props.data[i]['name']}</Text>
  115. </TabHeading>}
  116. key={this.props.data[i]['url']}
  117. style={{backgroundColor: ThemeManager.getCurrentThemeVariables().containerBgColor}}>
  118. {this.getWebview(this.props.data[i])}
  119. </Tab>);
  120. }
  121. return tabbedView;
  122. }
  123. render() {
  124. const nav = this.props.navigation;
  125. this.webviewArray = [];
  126. return (
  127. <BaseContainer
  128. navigation={nav}
  129. headerTitle={this.props.headerTitle}
  130. headerRightButton={this.getRefreshButton()}
  131. hasBackButton={this.props.hasHeaderBackButton}
  132. hasSideMenu={this.props.hasSideMenu}
  133. enableRotation={true}
  134. hideHeaderOnLandscape={true}>
  135. {this.props.data.length === 1 ?
  136. this.getWebview(this.props.data[0]) :
  137. <Tabs
  138. tabContainerStyle={{
  139. elevation: 0, // Fix for android shadow
  140. }}
  141. locked={true}
  142. style = {{
  143. backgroundColor: Platform.OS === 'ios' ?
  144. ThemeManager.getCurrentThemeVariables().tabDefaultBg :
  145. ThemeManager.getCurrentThemeVariables().brandPrimary
  146. }}
  147. >
  148. {this.getTabbedWebview()}
  149. </Tabs>}
  150. {this.props.hasFooter && this.props.data.length === 1 ?
  151. <Footer>
  152. <Left style={{
  153. paddingLeft: 6,
  154. }}>
  155. {this.getHeaderButton(() => this.openWebLink(this.props.data[0]['url']), 'open-in-new')}
  156. </Left>
  157. <Body/>
  158. <Right style={{
  159. flexDirection: 'row',
  160. alignItems: 'flex-end',
  161. paddingRight: 6
  162. }}>
  163. <View style={{
  164. flexDirection: 'row',
  165. marginRight: 0,
  166. marginLeft: 'auto'
  167. }}>
  168. {this.getHeaderButton(() => this.goBackWebview(), 'chevron-left')}
  169. {this.getHeaderButton(() => this.goForwardWebview(), 'chevron-right')}
  170. </View>
  171. </Right>
  172. </Footer> : <View/>}
  173. </BaseContainer>
  174. );
  175. }
  176. }