Skip to content

Commit 054daac

Browse files
committed
add filter
1 parent 867ec25 commit 054daac

3 files changed

Lines changed: 68 additions & 13 deletions

File tree

browser/main/Components/PlanetNavigator.jsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ var PlanetNavigator = React.createClass({
77
Users: React.PropTypes.array
88
}),
99
openLaunchModal: React.PropTypes.func,
10-
openAddUserModal: React.PropTypes.func
10+
openAddUserModal: React.PropTypes.func,
11+
showAll: React.PropTypes.func,
12+
showOnlySnippets: React.PropTypes.func,
13+
showOnlyBlueprints: React.PropTypes.func
1114
},
1215
getInitialState: function () {
1316
return {
1417
isLaunchModalOpen: false
1518
}
1619
},
1720
submitLaunchModal: function (ret) {
18-
console.log(ret)
1921
this.setState({isLaunchModalOpen: false})
2022
},
2123
render: function () {
@@ -31,13 +33,13 @@ var PlanetNavigator = React.createClass({
3133
<i className='fa fa-rocket fa-fw'/> Launch
3234
</button>
3335
<nav>
34-
<a>
36+
<a onClick={this.props.showAll}>
3537
<i className='fa fa-home fa-fw'/> Home
3638
</a>
37-
<a>
39+
<a onClick={this.props.showOnlySnippets}>
3840
<i className='fa fa-code fa-fw'/> Snippets
3941
</a>
40-
<a>
42+
<a onClick={this.props.showOnlyBlueprints}>
4143
<i className='fa fa-file-text-o fa-fw'/> Blueprints
4244
</a>
4345
</nav>

browser/main/Containers/PlanetContainer.jsx

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ var PlanetActions = require('../Actions/PlanetActions')
1919
var AuthStore = require('../Stores/AuthStore')
2020
var PlanetStore = require('../Stores/PlanetStore')
2121

22-
var searchArticle = function (search, articles) {
23-
if (search === '' || search == null) return articles
22+
function basicFilter (keyword, articles) {
23+
if (keyword === '' || keyword == null) return articles
2424
var firstFiltered = articles.filter(function (article) {
2525

2626
var first = article.type === 'snippet' ? article.callSign : article.title
27-
if (first.match(new RegExp(search, 'i'))) return true
27+
if (first.match(new RegExp(keyword, 'i'))) return true
2828

2929
return false
3030
})
3131

3232
var secondFiltered = articles.filter(function (article) {
3333
var second = article.type === 'snippet' ? article.description : article.content
34-
if (second.match(new RegExp(search, 'i'))) return true
34+
if (second.match(new RegExp(keyword, 'i'))) return true
3535

3636
return false
3737
})
3838

3939
var thirdFiltered = articles.filter(function (article) {
4040
if (article.type === 'snippet') {
41-
if (article.content.match(new RegExp(search, 'i'))) return true
41+
if (article.content.match(new RegExp(keyword, 'i'))) return true
4242
}
4343
return false
4444
})
@@ -48,6 +48,46 @@ var searchArticle = function (search, articles) {
4848
})
4949
}
5050

51+
function snippetFilter (articles) {
52+
return articles.filter(function (article) {
53+
return article.type === 'snippet'
54+
})
55+
}
56+
57+
function blueprintFilter (articles) {
58+
return articles.filter(function (article) {
59+
return article.type === 'blueprint'
60+
})
61+
}
62+
63+
function tagFilter (keyword, articles) {
64+
return articles.filter(function (article) {
65+
return article.Tags.some(function (tag) {
66+
return tag.name.match(new RegExp(keyword, 'i'))
67+
})
68+
})
69+
}
70+
71+
function searchArticle (search, articles) {
72+
var keywords = search.split(' ')
73+
74+
for (var keyword of keywords) {
75+
if (keyword.match(/^\$s/, 'i')) {
76+
articles = snippetFilter(articles)
77+
continue
78+
} else if (keyword.match(/^\$b/, 'i')) {
79+
articles = blueprintFilter(articles)
80+
continue
81+
} else if (keyword.match(/^#[A-Za-z0-9]+/)) {
82+
articles = tagFilter(keyword.substring(1, keyword.length), articles)
83+
continue
84+
}
85+
articles = basicFilter(keyword, articles)
86+
}
87+
88+
return articles
89+
}
90+
5191
module.exports = React.createClass({
5292
mixins: [ReactRouter.Navigation, ReactRouter.State],
5393
propTypes: {
@@ -208,8 +248,9 @@ module.exports = React.createClass({
208248
return
209249
}
210250

251+
var user
211252
if (res.status === 'userAdded') {
212-
var user = res.data
253+
user = res.data
213254
if (user == null) {
214255
return null
215256
}
@@ -221,7 +262,7 @@ module.exports = React.createClass({
221262
}
222263

223264
if (res.status === 'userRemoved') {
224-
var user = res.data
265+
user = res.data
225266
if (user == null) {
226267
return null
227268
}
@@ -286,6 +327,15 @@ module.exports = React.createClass({
286327
this.selectArticleByIndex(0)
287328
})
288329
},
330+
showAll: function () {
331+
this.setState({search: ''})
332+
},
333+
showOnlySnippets: function () {
334+
this.setState({search: '$s'})
335+
},
336+
showOnlyBlueprints: function () {
337+
this.setState({search: '$b'})
338+
},
289339
openLaunchModal: function () {
290340
this.setState({isLaunchModalOpen: true})
291341
},
@@ -493,7 +543,9 @@ module.exports = React.createClass({
493543
<PlanetHeader search={this.state.search}
494544
openSettingModal={this.openSettingModal} onSearchChange={this.handleSearchChange} currentPlanet={this.state.currentPlanet}/>
495545

496-
<PlanetNavigator openLaunchModal={this.openLaunchModal} openAddUserModal={this.openAddUserModal} currentPlanet={this.state.currentPlanet}/>
546+
<PlanetNavigator openLaunchModal={this.openLaunchModal} openAddUserModal={this.openAddUserModal}
547+
showAll={this.showAll}
548+
showOnlySnippets={this.showOnlySnippets} showOnlyBlueprints={this.showOnlyBlueprints} currentPlanet={this.state.currentPlanet}/>
497549

498550
<PlanetArticleList ref='list' articles={filteredArticles}/>
499551

browser/styles/main/containers/PlanetContainer.styl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
text-decoration none
117117
background-color transparent
118118
color textColor
119+
cursor pointer
119120
transition 0.1s
120121
&:hover, &.hover
121122
background-color hoverBackgroundColor

0 commit comments

Comments
 (0)