PageListSearch.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // This is the root component for #page-list-search
  2. import React from 'react';
  3. import axios from 'axios'
  4. import SearchResult from './SearchPage/SearchResult';
  5. export default class PageListSearch extends React.Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. location: location,
  10. tree: $('#search-listpage-input').data('path'),
  11. searchingKeyword: this.props.query.q || '',
  12. searchedKeyword: '',
  13. searchedPages: [],
  14. searchResultMeta: {},
  15. searchError: null,
  16. }
  17. this.changeURL = this.changeURL.bind(this);
  18. this.search = this.search.bind(this);
  19. this.handleChange = this.handleChange.bind(this);
  20. }
  21. componentDidMount() {
  22. const $pageListSearchForm = $('#search-listpage-input');
  23. // search after page initialized
  24. if (this.state.searchingKeyword !== '') {
  25. const keyword = this.state.searchingKeyword;
  26. $pageListSearchForm.val(keyword);
  27. this.search({keyword});
  28. }
  29. // This is temporary data bind ... (out of component)
  30. $('#search-listpage-form').on('submit', () => {
  31. const keyword = $pageListSearchForm.val();
  32. console.log('submit with', keyword);
  33. if (keyword != this.state.searchingKeyword) {
  34. this.search({keyword});
  35. }
  36. return false;
  37. });
  38. $('#search-listpage-clear').on('click', () => {
  39. $pageListSearchForm.val('');
  40. this.search({keyword: ''});
  41. return false;
  42. });
  43. }
  44. componentWillUnmount() {
  45. }
  46. static getQueryByLocation(location) {
  47. let search = location.search || '';
  48. let query = {};
  49. search.replace(/^\?/, '').split('&').forEach(function(element) {
  50. let queryParts = element.split('=');
  51. query[queryParts[0]] = decodeURIComponent(queryParts[1]).replace(/\+/g, ' ');
  52. });
  53. return query;
  54. }
  55. handleChange(event) {
  56. // this is not fired now because of force-data-bound by jQuery
  57. const keyword = event.target.value;
  58. this.setState({searchedKeyword: keyword});
  59. console.log('Changed');
  60. }
  61. stopSearching() {
  62. $('#content-main').show();
  63. $('#search-listpage-clear').hide();
  64. $('.main-container').removeClass('aside-hidden');
  65. }
  66. startSearching() {
  67. $('#content-main').hide();
  68. $('#search-listpage-clear').show();
  69. $('.main-container').addClass('aside-hidden');
  70. }
  71. changeURL(keyword, refreshHash) {
  72. const tree = this.state.tree;
  73. let hash = location.hash || '';
  74. // TODO 整理する
  75. if (refreshHash || this.state.searchedKeyword !== '') {
  76. hash = '';
  77. }
  78. if (window.history && window.history.pushState){
  79. window.history.pushState('', `Search - ${keyword}`, `${tree}?q=${keyword}${hash}`);
  80. }
  81. }
  82. search(data) {
  83. const keyword = data.keyword || '';
  84. const tree = this.state.tree;
  85. console.log('search with', keyword, tree);
  86. this.changeURL(keyword);
  87. if (keyword === '') {
  88. this.stopSearching();
  89. this.setState({
  90. searchingKeyword: '',
  91. searchedPages: [],
  92. searchResultMeta: {},
  93. });
  94. return true;
  95. }
  96. this.startSearching();
  97. this.setState({
  98. searchingKeyword: keyword,
  99. });
  100. axios.get('/_api/search', {params: {q: keyword, tree: tree}})
  101. .then((res) => {
  102. if (res.data.ok) {
  103. this.setState({
  104. searchedKeyword: keyword,
  105. searchedPages: res.data.data,
  106. searchResultMeta: res.data.meta,
  107. });
  108. }
  109. // TODO error
  110. })
  111. .catch((res) => {
  112. // TODO error
  113. });
  114. };
  115. render() {
  116. return (
  117. <div>
  118. <input
  119. type="hidden"
  120. name="q"
  121. value={this.state.searchingKeyword}
  122. onChange={this.handleChange}
  123. className="form-control"
  124. />
  125. <SearchResult
  126. tree={this.state.tree}
  127. pages={this.state.searchedPages}
  128. searchingKeyword={this.state.searchingKeyword}
  129. searchResultMeta={this.state.searchResultMeta}
  130. />
  131. </div>
  132. );
  133. }
  134. }
  135. PageListSearch.propTypes = {
  136. query: React.PropTypes.object,
  137. };
  138. PageListSearch.defaultProps = {
  139. //pollInterval: 1000,
  140. query: PageListSearch.getQueryByLocation(location || {}),
  141. };