summaryrefslogtreecommitdiffstats
path: root/web/react/stores/search_store.jsx
blob: 549f355ef5ad0624e0ff24c18bf3b3f6b3aed780 (plain)
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
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import EventEmitter from 'events';

import BrowserStore from '../stores/browser_store.jsx';

import Constants from '../utils/constants.jsx';
var ActionTypes = Constants.ActionTypes;

var CHANGE_EVENT = 'change';
var SEARCH_CHANGE_EVENT = 'search_change';
var SEARCH_TERM_CHANGE_EVENT = 'search_term_change';
var SHOW_SEARCH_EVENT = 'show_search';

class SearchStoreClass extends EventEmitter {
    constructor() {
        super();

        this.emitChange = this.emitChange.bind(this);
        this.addChangeListener = this.addChangeListener.bind(this);
        this.removeChangeListener = this.removeChangeListener.bind(this);

        this.emitSearchChange = this.emitSearchChange.bind(this);
        this.addSearchChangeListener = this.addSearchChangeListener.bind(this);
        this.removeSearchChangeListener = this.removeSearchChangeListener.bind(this);

        this.emitSearchTermChange = this.emitSearchTermChange.bind(this);
        this.addSearchTermChangeListener = this.addSearchTermChangeListener.bind(this);
        this.removeSearchTermChangeListener = this.removeSearchTermChangeListener.bind(this);

        this.emitShowSearch = this.emitShowSearch.bind(this);
        this.addShowSearchListener = this.addShowSearchListener.bind(this);
        this.removeShowSearchListener = this.removeShowSearchListener.bind(this);

        this.getSearchResults = this.getSearchResults.bind(this);
        this.getIsMentionSearch = this.getIsMentionSearch.bind(this);

        this.storeSearchTerm = this.storeSearchTerm.bind(this);
        this.getSearchTerm = this.getSearchTerm.bind(this);

        this.storeSearchResults = this.storeSearchResults.bind(this);
    }

    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);
    }

    emitShowSearch() {
        this.emit(SHOW_SEARCH_EVENT);
    }

    addShowSearchListener(callback) {
        this.on(SHOW_SEARCH_EVENT, callback);
    }

    removeShowSearchListener(callback) {
        this.removeListener(SHOW_SEARCH_EVENT, callback);
    }

    getSearchResults() {
        return BrowserStore.getItem('search_results');
    }

    getIsMentionSearch() {
        return BrowserStore.getItem('is_mention_search');
    }

    storeSearchTerm(term) {
        BrowserStore.setItem('search_term', term);
    }

    getSearchTerm() {
        return BrowserStore.getItem('search_term');
    }

    storeSearchResults(results, isMentionSearch) {
        BrowserStore.setItem('search_results', results);
        BrowserStore.setItem('is_mention_search', Boolean(isMentionSearch));
    }
}

var SearchStore = new SearchStoreClass();

SearchStore.dispatchToken = AppDispatcher.register((payload) => {
    var action = payload.action;

    switch (action.type) {
    case ActionTypes.RECEIVED_SEARCH:
        SearchStore.storeSearchResults(action.results, action.is_mention_search);
        SearchStore.emitSearchChange();
        break;
    case ActionTypes.RECEIVED_SEARCH_TERM:
        SearchStore.storeSearchTerm(action.term);
        SearchStore.emitSearchTermChange(action.do_search, action.is_mention_search);
        break;
    case ActionTypes.SHOW_SEARCH:
        SearchStore.emitShowSearch();
        break;
    default:
    }
});

export default SearchStore;