-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_helper.coffee
173 lines (155 loc) · 4.75 KB
/
search_helper.coffee
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
@SearchHelper = createReactClass
getInitialState: ->
q = new SearchQuery
q.parse Store.state.query
query: q
userInput: null
showCriteriaPicker: false
caretPosition: 0
onShowCriteriaPicker: (e) ->
e.preventDefault()
@setState
showCriteriaPicker: [email protected]
onSelectCriteria: (e, opt) ->
e.preventDefault()
@state.query.options[opt] = if SearchQuery.multiple[opt]
[]
else if SearchQuery.keywords[opt]
true
else
""
@setState
query: @state.query
showCriteriaPicker: false
onClearText: (e) ->
@state.query.parse '', 0
@setState
userInput: ''
query: @state.query
@refs.search.focus()
onChangeCriteriaValue: (e, opt) ->
if SearchQuery.multiple[opt]
values = e.target.value.split /,/
values = [] if e.target.value == ""
@state.query.options[opt] = values
else
@state.query.options[opt] = e.target.value
@setState
query: @state.query
changeUserInput: (e) ->
@state.query.parse e.target.value, e.target.selectionStart
@setState
userInput: e.target.value
query: @state.query
caretPosition: e.target.selectionStart
onFocus: ->
@setState
userInput: @state.query.stringify()
onBlur: ->
@setState
userInput: null
caretPosition: null
onSearch: (e) ->
e.preventDefault()
if @state.query.unknown.length > 0
# Show errors
@setState
userInput: null
return
@props.close()
str = @state.query.stringify()
# Force search since we might be visiting the same URL that we're already
# at, with the same search they already did. (The user might be trying to
# refresh the results.)
Store.search str, true
Store.navigate '/search/' + encodeURI(str)
moveCaret: (e) ->
@changeUserInput(e)
optionHelper: (field, options...) ->
val = ""
<select className="form-control" defaultValue={val}>
{
options.map (opt) =>
<option key={opt[0]} value={opt[0]}>{opt[1]}</option>
}
</select>
render: ->
query = @state.query
string = if @state.userInput?
@state.userInput
else
query.stringify()
<div className="search-helper">
<form onSubmit={@onSearch} className="form-inline">
<div className="form-group">
<div className="input-group">
<input
ref="search"
className="form-control"
placeholder="Search"
value={string}
onChange={@changeUserInput}
onClick={@moveCaret}
onKeyUp={@moveCaret}
onFocus={@onFocus}
onBlur={@onBlur}
type="text"
/>
<span className="input-group-btn">
<button type="button" className="btn btn-secondary" onClick={@onClearText}>×</button>
</span>
</div>
{' '}
<button className="btn btn-default btn-primary">
<i className="fa fa-search"/> Search
</button>
<a href="javascript:void(0)" onClick={@onShowCriteriaPicker} className="btn">
advanced...
</a>
</div>
{
if [email protected]? && query.unknown.length > 0
<div className="alert alert-danger" role="alert">
<strong>Unknown words:</strong> {query.unknown.join(', ')}
</div>
}
{
Object.keys(@state.query.options).map (key) =>
<div key={key}>
{key}: <input className="form-control" type="text" value={query.options[key]} onChange={ (e) => @onChangeCriteriaValue e, key }/>
</div>
}
<div>
{
if @state.showCriteriaPicker
SearchQuery.optionList.map (opt) =>
<button key={opt} className="btn btn-default" onClick={ (e) => @onSelectCriteria e, opt }>{opt}</button>
}
</div>
</form>
<div className="tag-list">
{
used = {}
others = {}
query.tags.map (tag) =>
used[tag.id] = true
query.others.map (tag) =>
others[tag.id] = true
Store.state.tags.map (tag) =>
selected = used[tag.id]
if query.useOthers && !others[tag.id] && !used[tag.id]
return
if !used[tag.id]
onClick = =>
query.tags.push tag
@setState
query: query
else
onClick = =>
query.tags = query.tags.filter (e) -> e.id != tag.id
@setState
query: query
<Tag tag={tag} key={tag.id} selected={selected} label onClick={onClick}/>
}
</div>
</div>