Guest User

Untitled

a guest
Dec 10th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { View, Text, StyleSheet, TextInput, TouchableOpacity, Image, ListView } from 'react-native';
  3. import { Actions } from 'react-native-router-flux';
  4.  
  5. class Search extends Component {
  6.  
  7. /**
  8. * represents state tracker
  9. * @field (string) text - from input field, used as parameter for search
  10. * @field (array) searchPages - result from search f'n, resource for render rows
  11. */
  12. state = {
  13. text: '',
  14. searchPages: [],
  15. searchFiles: [],
  16. searchMenus: [],
  17. buttonActive: 'content'
  18. }
  19. //napravi objekte za ispisivanje search-a
  20. createObject() {
  21. let rat = [];
  22. switch (this.state.buttonActive) {
  23. case 'content':
  24. rat = this.state.searchPages.map((element) => {
  25. return <Text key={element.pageId}>{element.title}</Text>
  26. });
  27. break;
  28. case 'video':
  29. rat = this.state.searchFiles.map((element) => {
  30.  
  31. if (element.type == 'video')
  32. return <Text key={element.filename}>{element.pageId + ' ' + element.filename +'.'+ element.ext}</Text>
  33. });
  34. break;
  35.  
  36. default:
  37. break;
  38. }
  39.  
  40. return (
  41. <View>
  42. {rat}
  43. </View>
  44. )
  45. }
  46.  
  47. searchDoTitlesMENU = (item, where) => {
  48.  
  49. return new Promise((resolve, reject) => {
  50. let rslt = where.filter(x =>
  51. x.title.toLowerCase().includes(item.toLowerCase())
  52. )
  53. resolve(rslt);
  54. })
  55. }
  56.  
  57. searchDoTitlesPromiseWrapper = (item, where) => {
  58. return new Promise((resolve, reject) => {
  59. let foundMenus = this.searchDoTitlesMENU(item, where);
  60. resolve(foundMenus);
  61. })
  62. }
  63.  
  64. searchDoPages = (item, where) => {
  65. return new Promise((resolve, reject) => {
  66. let foundPages = where.filter(x => {
  67. if (x.text != null)
  68. return x.text.includes(item) || x.subtitle.includes(item) || x.title.includes(item);
  69. else if (x.text == null)
  70. return x.subtitle.includes(item) || x.title.includes(item);
  71. })
  72. resolve(foundPages)
  73. })
  74. }
  75.  
  76. searchDoFiles = (where) => new Promise((resolve, reject) => {
  77. let foundFiles = [];
  78. if (where != null)
  79. where.forEach(e => {
  80. if (e.files != null)
  81. e.files.forEach(ei => foundFiles.push(ei))
  82. })
  83. resolve(foundFiles);
  84. })
  85.  
  86. searchPromise = (input) => {
  87. return new Promise((resolve, reject) => {
  88. let idLang = 1 // engleski
  89.  
  90. let pages = global.globalJson.pages;
  91. let menus = global.globalJson.menus[idLang].menu;
  92.  
  93. let resultMenus = [];
  94. this.searchDoTitlesPromiseWrapper(input, menus)
  95. .then(r => { r.forEach(val => { resultMenus.push(val) }) })
  96.  
  97. let resultPages = [];
  98. let resultFiles = [];
  99. this.searchDoPages(input, pages)
  100. .then(r => {
  101. r.forEach(val => resultPages.push(val));
  102. this.searchDoFiles(r)
  103. .then((rlt) => {
  104. resultFiles = rlt;
  105. })
  106. .then(() => resolve({
  107. foundMenus: resultMenus,
  108. foundPages: resultPages,
  109. foundFiles: resultFiles
  110. }))
  111. })
  112. })
  113. }
  114.  
  115. callMe = (stuff) => {
  116. if (stuff.length >= 3)
  117. this.searchPromise(stuff).then(({ foundMenus, foundPages, foundFiles }) => {
  118. this.setState({
  119. searchPages: foundPages,
  120. searchFiles: foundFiles,
  121. searchMenus: foundMenus
  122. });
  123. // console.log(this.state.searchPages)
  124. });
  125. }
  126.  
  127.  
  128. render() {
  129.  
  130.  
  131. // if (this.state.text.length >= 3)
  132. // this.searchPromise(this.state.text).then((r) => this.setState({ searchPages: r.foundPages }));
  133.  
  134. return (
  135.  
  136. <View style={styles.searchCont} >
  137.  
  138. <View>
  139. <View style={{ alignItems: 'center', padding: 20 }}>
  140. <Text style={{ color: 'black', fontSize: 20 }}>Choose the Category:</Text>
  141. </View>
  142. <View style={styles.ButtonsView}>
  143. <TouchableOpacity style={styles.ButtonContent} onPress={() => this.setState({ buttonActive: 'content' })}>
  144. <Image
  145. style={styles.ButtonIconStyle2}
  146. source={require('./ico/32/rnd.png')}
  147. />
  148. <Text style={styles.ButtonTextStyle}>CONTENT</Text>
  149. </TouchableOpacity >
  150. <TouchableOpacity style={styles.ButtonContent} onPress={() => this.setState({ buttonActive: 'video' })}>
  151. <Image
  152. style={styles.ButtonIconStyle2}
  153. source={require('./ico/play-button.png')}
  154. />
  155. <Text style={styles.ButtonTextStyle}>VIDEO</Text>
  156. </TouchableOpacity >
  157. </View>
  158. <View style={{ flexDirection: 'row', justifyContent: 'center', alignItems: 'center', marginTop: 30, }}>
  159. <TouchableOpacity>
  160. <Image
  161. style={{ width: 32, height: 32 }}
  162. source={require('./ico/32/search-2.png')}
  163. />
  164. </TouchableOpacity>
  165. <View style={{ padding: 10 }}>
  166. <TextInput
  167. placeholder="Search"
  168. style={styles.textInput}
  169. onChangeText={(text) => {
  170. this.setState({ text });
  171. this.callMe(text)
  172. }}
  173. value={this.state.text}
  174. />
  175. </View>
  176. <TouchableOpacity>
  177. <Image
  178. style={{ width: 32, height: 32 }}
  179. source={require('./ico/32/right.png')}
  180. />
  181. </TouchableOpacity>
  182. </View>
  183. <View>
  184. {this.createObject()}
  185. </View>
  186. </View>
  187. </View>
  188. );
  189. }
  190. }
  191.  
  192. const styles = StyleSheet.create({
  193. searchCont: {
  194. position: 'absolute',
  195. backgroundColor: '#FFFFFF',
  196. height: 500,
  197. width: '100%',
  198. top: 50,
  199. zIndex: 3
  200. },
  201. textInput: {
  202. backgroundColor: 'white',
  203. width: 300,
  204. height: 50,
  205. borderBottomWidth: 2,
  206. borderColor: '#4169e1'
  207. },
  208. ButtonIconStyle2: {
  209. marginRight: 10,
  210. width: 32,
  211. height: 32
  212. },
  213. ButtonContent: {
  214. width: 200,
  215. height: 50,
  216. borderColor: '#4169e1',
  217. borderWidth: 3,
  218. borderRadius: 4,
  219. paddingHorizontal: 40,
  220. backgroundColor: '#4169e1',
  221. padding: 18,
  222. flexDirection: 'row',
  223. justifyContent: 'center',
  224. alignItems: 'center',
  225. marginRight: 20,
  226. marginLeft: 20
  227. },
  228. ButtonsView: {
  229. flexDirection: 'row',
  230. justifyContent: 'center',
  231. },
  232. ButtonTextStyle: {
  233. fontSize: 20,
  234. color: 'white'
  235. },
  236. ico: {
  237. height: 35,
  238. width: 35,
  239. margin: 10,
  240.  
  241. },
  242. });
  243.  
  244. export default Search;
Add Comment
Please, Sign In to add comment