Repository navigation
Expand file tree
/
Copy pathMainContainer.js
More file actions
158 lines (142 loc) · 4.92 KB
/
Copy pathMainContainer.js
File metadata and controls
158 lines (142 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react';
import {Container} from 'semantic-ui-react';
import SearchBar from '../components/SearchBar';
import Title from '../components/Title';
import SearchCodeModel from '../models/SearchCodeModel';
import HashHandler from '../utils/HashHandler';
import VariableList from '../components/VariableList';
import SearchError from '../components/SearchError';
import Loading from '../components/Loading';
import Donate from '../components/Donate';
import NoticeLinks from '../components/NoticeLinks';
import Suggestion from '../components/Suggestion';
import NavBar from '../components/NavBar';
import SourceCode from '../components/SourceCode';
import AppModel from '../models/AppModel';
export default class MainContainer extends React.Component {
state = {
isZH: false,
isError: false,
requestingVariable: false,
searchValue: SearchCodeModel.searchValue,
searchLang: SearchCodeModel.searchLang,
page: SearchCodeModel.page,
variableList: SearchCodeModel.variableList,
suggestion: SearchCodeModel.suggestion,
requestingSourceCode: false,
sourceCodeVisible: false,
sourceCodeVariable: null,
sourceCodeRepo: null,
}
repoList = null;
constructor(props) {
super(props);
SearchCodeModel.onUpdated(this.handleSearchCodeModelUpdate);
window.addEventListener('hashchange', this.handleLocationHashChanged, false);
AppModel.analytics();
}
componentDidUpdate(prevProps) {
this.state.variableList.length && document.body.classList.add('dark');
}
componentDidMount() {
this.handleLocationHashChanged();
}
checkError(data) {
if (this.state.requestingVariable) {
// no search result
if (data.variableList.length < 1 || data.variableList[data.variableList.length - 1].length < 1) {
return true;
}
}
return false;
}
requestVariable = (val, lang) => {
val = decodeURIComponent(val);
let page = this.state.page;
if (val === this.state.searchValue) {
page += 1;
} else {
page = 0;
}
this.setState({searchValue: val, requestingVariable: true});
SearchCodeModel.requestVariable(val, page, lang || this.state.searchLang);
AppModel.analytics('q=' + val);
}
requestSourceCode(repo) {
this.setState({
sourceCodeVisible: true,
requestingSourceCode: true,
sourceCodeRepo: repo
});
SearchCodeModel.requestSourceCode(repo.id);
AppModel.analytics('vc&q=' + this.sourceCodeVariable.keyword);
}
handleLocationHashChanged = e => {
e && e.preventDefault();
const hash = HashHandler.get();
hash && this.requestVariable(hash.replace(/(\?.*)/, ''));
}
handleSearchCodeModelUpdate = (curr, prev, mutation) => {
if (mutation.variableList) {
this.setState({
isZH: SearchCodeModel.isZH || this.state.isZH,
isError: this.checkError(curr),
requestingVariable: !mutation.variableList,
searchValue: SearchCodeModel.searchValue,
searchLang: SearchCodeModel.searchLang,
page: SearchCodeModel.page,
variableList: SearchCodeModel.variableList,
suggestion: SearchCodeModel.suggestion
});
}
if (mutation.sourceCode) {
this.setState({
requestingSourceCode: false,
sourceCode: SearchCodeModel.sourceCode
});
}
}
handleSearch = (val, lang) => {
this.setState({searchLang: lang});
if (val === null || val === undefined || this.state.requestingVariable) { return; }
val = val.trim().replace(/\s+/ig, ' '); // filter spaces
if (val.length < 1) { return; }
if (val == this.state.searchValue) {
this.requestVariable(val, lang);
} else {
HashHandler.set(val); // update window.location.hash
}
}
handleOpenSourceCode = variable => {
this.setState({sourceCodeVariable: variable});
this.requestSourceCode(variable.repoList[0]);
}
handleCloseSourceCode = () => {
this.setState({sourceCodeVisible: false});
}
handleRequestSourceCode = repo => {
this.requestSourceCode(repo);
}
renderSloganImage() {
if (this.state.page > 0 || this.state.variableList.length) { return ''; }
return <div className='slogan-image'><img src='images/twohardtings.jpg'/></div>;
}
render() {
return (
<Container className='main'>
<Title {...this.state}/>
<SearchBar placeholder='AI 人工智能' {...this.state} onSearch={this.handleSearch}/>
<Suggestion {...this.state}/>
{this.state.requestingVariable ? <Loading/> : (this.state.isError ? <SearchError/> : '')}
{this.renderSloganImage()}
<VariableList {...this.state} onOpenSourceCode={this.handleOpenSourceCode}/>
{this.state.variableList.length ? <Donate {...this.state}/> : ''}
<NoticeLinks/>
<NavBar/>
<SourceCode {...this.state}
onRequestSourceCode={this.handleRequestSourceCode}
onCloseSourceCode={this.handleCloseSourceCode}/>
</Container>
)
}
}