@@ -19,7 +19,8 @@ module.exports = React.createClass({
1919 } ,
2020 getInitialState : function ( ) {
2121 return {
22- currentPlanet : null
22+ currentPlanet : null ,
23+ filteredArticles : [ ]
2324 }
2425 } ,
2526 componentDidMount : function ( ) {
@@ -35,13 +36,13 @@ module.exports = React.createClass({
3536 var index = 0
3637
3738 if ( this . isActive ( 'snippets' ) ) {
38- this . state . currentPlanet . Articles . some ( function ( _article , _index ) {
39+ this . state . filteredArticles . some ( function ( _article , _index ) {
3940 if ( _article . type === 'snippet' && _article . localId === parseInt ( params . localId , 10 ) ) {
4041 index = _index
4142 }
4243 } )
4344 } else if ( this . isActive ( 'blueprints' ) ) {
44- this . state . currentPlanet . Articles . some ( function ( _article , _index ) {
45+ this . state . filteredArticles . some ( function ( _article , _index ) {
4546 if ( _article . type === 'blueprint' && _article . localId === parseInt ( params . localId , 10 ) ) {
4647 index = _index
4748 return true
@@ -53,7 +54,7 @@ module.exports = React.createClass({
5354 return index
5455 } ,
5556 selectArticleByIndex : function ( index ) {
56- var article = this . state . currentPlanet . Articles [ index ]
57+ var article = this . state . filteredArticles [ index ]
5758 var params = this . props . params
5859
5960 if ( article == null ) {
@@ -73,16 +74,16 @@ module.exports = React.createClass({
7374 }
7475 } ,
7576 selectNextArticle : function ( ) {
76- if ( this . state . currentPlanet == null || this . state . currentPlanet . Articles . length === 0 ) return
77+ if ( this . state . currentPlanet == null || this . state . filteredArticles . length === 0 ) return
7778
7879 var index = this . getIndexOfCurrentArticle ( )
7980
80- if ( index < this . state . currentPlanet . Articles . length ) {
81+ if ( index < this . state . filteredArticles . length ) {
8182 this . selectArticleByIndex ( index + 1 )
8283 }
8384 } ,
8485 selectPriorArticle : function ( ) {
85- if ( this . state . currentPlanet == null || this . state . currentPlanet . Articles . length === 0 ) return
86+ if ( this . state . currentPlanet == null || this . state . filteredArticles . length === 0 ) return
8687
8788 var index = this . getIndexOfCurrentArticle ( )
8889
@@ -95,7 +96,7 @@ module.exports = React.createClass({
9596
9697 if ( res . status === 'planetFetched' ) {
9798 var planet = res . data
98- this . setState ( { currentPlanet : planet } , function ( ) {
99+ this . setState ( { currentPlanet : planet , filteredArticles : planet . Articles } , function ( ) {
99100 if ( planet . Articles . length > 0 ) {
100101 if ( this . isActive ( 'snippets' ) ) {
101102 this . transitionTo ( 'snippets' , {
@@ -146,6 +147,37 @@ module.exports = React.createClass({
146147 }
147148 }
148149 } ,
150+ handleSearchChange : function ( search ) {
151+ var firstFiltered = this . state . currentPlanet . Articles . filter ( function ( article ) {
152+ if ( search === '' || search == null ) return true
153+
154+ var first = article . type === 'snippet' ? article . callSign : article . title
155+ if ( first . match ( new RegExp ( search , 'i' ) ) ) return true
156+
157+ return false
158+ } )
159+
160+ var secondFiltered = this . state . currentPlanet . Articles . filter ( function ( article ) {
161+ var second = article . type === 'snippet' ? article . description : article . content
162+ if ( second . match ( new RegExp ( search , 'i' ) ) ) return true
163+
164+ return false
165+ } )
166+
167+ var thirdFiltered = this . state . currentPlanet . Articles . filter ( function ( article ) {
168+ if ( article . type === 'snippet' ) {
169+ if ( article . content . match ( new RegExp ( search , 'i' ) ) ) return true
170+ }
171+ return false
172+ } )
173+
174+ var filteredArticles = firstFiltered . concat ( secondFiltered , thirdFiltered ) . filter ( function ( value , index , self ) {
175+ return self . indexOf ( value ) === index
176+ } )
177+ this . setState ( { filteredArticles : filteredArticles } , function ( ) {
178+ this . selectArticleByIndex ( 0 )
179+ } )
180+ } ,
149181 render : function ( ) {
150182 var user = AuthStore . getUser ( )
151183 if ( user == null ) return ( < div /> )
@@ -177,9 +209,9 @@ module.exports = React.createClass({
177209
178210 return (
179211 < div className = 'PlanetContainer' >
180- < PlanetHeader currentPlanet = { this . state . currentPlanet } currentUser = { user } />
212+ < PlanetHeader onSearchChange = { this . handleSearchChange } currentPlanet = { this . state . currentPlanet } currentUser = { user } />
181213 < PlanetNavigator currentPlanet = { this . state . currentPlanet } currentUser = { user } />
182- < PlanetArticleList onPressUp = { this . selectPriorArticle } onPressDown = { this . selectNextArticle } planet = { this . state . currentPlanet } />
214+ < PlanetArticleList onPressUp = { this . selectPriorArticle } onPressDown = { this . selectNextArticle } articles = { this . state . filteredArticles } />
183215 { content }
184216 </ div >
185217 )
0 commit comments