Mailspring/internal_packages/search-bar/lib/search-suggestion-store.coffee
Ben Gotow 9378f4480c fix(naming): Move atom/inbox/nilas refs to Nylas
Conflicts:
	internal_packages/inbox-activity-bar/lib/activity-bar-long-poll-item.cjsx
2015-05-15 11:07:28 -07:00

72 lines
1.7 KiB
CoffeeScript

Reflux = require 'reflux'
{Actions,
Contact,
ContactStore} = require 'nylas-exports'
_ = require 'underscore-plus'
# Stores should closely match the needs of a particular part of the front end.
# For example, we might create a "MessageStore" that observes this store
# for changes in selectedThread, "DatabaseStore" for changes to the underlying database,
# and vends up the array used for that view.
SearchSuggestionStore = Reflux.createStore
init: ->
@_suggestions = []
@_query = ""
@_committedQuery = ""
@listenTo Actions.searchQueryChanged, @onSearchQueryChanged
@listenTo Actions.searchQueryCommitted, @onSearchQueryCommitted
@listenTo Actions.searchBlurred, @onSearchBlurred
onSearchQueryChanged: (query) ->
@_query = query
@repopulate()
onSearchQueryCommitted: (query) ->
@_query = query
@_committedQuery = query
@_suggestions = []
@trigger()
onSearchBlurred: ->
@_suggestions = []
@trigger()
repopulate: ->
@_suggestions = []
term = @_query?[0]
return @trigger(@) unless term
key = Object.keys(term)[0]
val = term[key]
return @trigger(@) unless val
contactResults = ContactStore.searchContacts(val, limit:10)
@_suggestions.push
label: "Message Contains: #{val}"
value: [{"all": val}]
if contactResults.length
@_suggestions.push
divider: 'People'
_.each contactResults, (contact) =>
@_suggestions.push
contact: contact
value: [{"participants": contact.email}]
@trigger(@)
# Exposed Data
query: -> @_query
committedQuery: -> @_committedQuery
suggestions: ->
@_suggestions
module.exports = SearchSuggestionStore