2015-10-26 15:16:14 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2015-11-19 21:12:56 -05:00
|
|
|
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
|
|
|
|
|
import EventEmitter from 'events';
|
2015-10-26 15:16:14 -04:00
|
|
|
|
2016-03-14 08:50:46 -04:00
|
|
|
import Constants from 'utils/constants.jsx';
|
2015-10-26 15:16:14 -04:00
|
|
|
var ActionTypes = Constants.ActionTypes;
|
|
|
|
|
|
|
|
|
|
var CHANGE_EVENT = 'change';
|
|
|
|
|
var SEARCH_CHANGE_EVENT = 'search_change';
|
|
|
|
|
var SEARCH_TERM_CHANGE_EVENT = 'search_term_change';
|
2015-11-17 14:47:19 -05:00
|
|
|
var SHOW_SEARCH_EVENT = 'show_search';
|
2015-10-26 15:16:14 -04:00
|
|
|
|
|
|
|
|
class SearchStoreClass extends EventEmitter {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
2016-03-24 20:04:40 -04:00
|
|
|
this.searchResults = null;
|
2016-03-23 15:58:31 -04:00
|
|
|
this.isMentionSearch = false;
|
2016-08-04 11:38:09 -04:00
|
|
|
this.isFlaggedPosts = false;
|
2016-10-11 15:07:38 +02:00
|
|
|
this.isVisible = false;
|
2016-03-23 15:58:31 -04:00
|
|
|
this.searchTerm = '';
|
2015-10-26 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emitChange() {
|
|
|
|
|
this.emit(CHANGE_EVENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addChangeListener(callback) {
|
|
|
|
|
this.on(CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeChangeListener(callback) {
|
|
|
|
|
this.removeListener(CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emitSearchChange() {
|
|
|
|
|
this.emit(SEARCH_CHANGE_EVENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addSearchChangeListener(callback) {
|
|
|
|
|
this.on(SEARCH_CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeSearchChangeListener(callback) {
|
|
|
|
|
this.removeListener(SEARCH_CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emitSearchTermChange(doSearch, isMentionSearch) {
|
|
|
|
|
this.emit(SEARCH_TERM_CHANGE_EVENT, doSearch, isMentionSearch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addSearchTermChangeListener(callback) {
|
|
|
|
|
this.on(SEARCH_TERM_CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeSearchTermChangeListener(callback) {
|
|
|
|
|
this.removeListener(SEARCH_TERM_CHANGE_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 14:47:19 -05:00
|
|
|
emitShowSearch() {
|
|
|
|
|
this.emit(SHOW_SEARCH_EVENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addShowSearchListener(callback) {
|
|
|
|
|
this.on(SHOW_SEARCH_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeShowSearchListener(callback) {
|
|
|
|
|
this.removeListener(SHOW_SEARCH_EVENT, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 15:16:14 -04:00
|
|
|
getSearchResults() {
|
2016-03-23 15:58:31 -04:00
|
|
|
return this.searchResults;
|
2015-10-26 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getIsMentionSearch() {
|
2016-03-23 15:58:31 -04:00
|
|
|
return this.isMentionSearch;
|
2015-10-26 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
|
2016-08-04 11:38:09 -04:00
|
|
|
getIsFlaggedPosts() {
|
|
|
|
|
return this.isFlaggedPosts;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-26 15:16:14 -04:00
|
|
|
storeSearchTerm(term) {
|
2016-03-23 15:58:31 -04:00
|
|
|
this.searchTerm = term;
|
2015-10-26 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSearchTerm() {
|
2016-03-23 15:58:31 -04:00
|
|
|
return this.searchTerm;
|
2015-10-26 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
|
2016-08-04 11:38:09 -04:00
|
|
|
storeSearchResults(results, isMentionSearch, isFlaggedPosts) {
|
2016-03-23 15:58:31 -04:00
|
|
|
this.searchResults = results;
|
|
|
|
|
this.isMentionSearch = isMentionSearch;
|
2016-08-04 11:38:09 -04:00
|
|
|
this.isFlaggedPosts = isFlaggedPosts;
|
2015-10-26 15:16:14 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var SearchStore = new SearchStoreClass();
|
|
|
|
|
|
|
|
|
|
SearchStore.dispatchToken = AppDispatcher.register((payload) => {
|
|
|
|
|
var action = payload.action;
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
2016-02-08 10:33:59 -05:00
|
|
|
case ActionTypes.RECEIVED_SEARCH:
|
2016-08-04 11:38:09 -04:00
|
|
|
SearchStore.storeSearchResults(action.results, action.is_mention_search, action.is_flagged_posts);
|
2015-10-26 15:16:14 -04:00
|
|
|
SearchStore.emitSearchChange();
|
|
|
|
|
break;
|
2016-02-08 10:33:59 -05:00
|
|
|
case ActionTypes.RECEIVED_SEARCH_TERM:
|
2015-10-26 15:16:14 -04:00
|
|
|
SearchStore.storeSearchTerm(action.term);
|
|
|
|
|
SearchStore.emitSearchTermChange(action.do_search, action.is_mention_search);
|
|
|
|
|
break;
|
2015-11-17 14:47:19 -05:00
|
|
|
case ActionTypes.SHOW_SEARCH:
|
|
|
|
|
SearchStore.emitShowSearch();
|
|
|
|
|
break;
|
2015-10-26 15:16:14 -04:00
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default SearchStore;
|