-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathcomponent.js
187 lines (150 loc) · 4.26 KB
/
component.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import Component from "@ember/component";
import { get, set, getWithDefault } from "@ember/object";
import { sort } from "@ember/object/computed";
import { on } from "@ember/object/evented";
import { next } from "@ember/runloop";
import { inject as service } from "@ember/service";
import $ from "jquery";
import { vars as varsConfig } from "config";
import HotkeyMixin from "ui/components/-mixins/hotkey";
import Search from "data/models/search/model";
import getStreamFromUrl from "utils/getStreamFromUrl";
import layout from "./template.hbs";
import "./styles.less";
const { "search-history-size": searchHistorySize } = varsConfig;
const { filters } = Search;
// TODO: rewrite SearchBarComponent and Search model
export default Component.extend( HotkeyMixin, {
routing: service( "-routing" ),
store: service(),
layout,
tagName: "nav",
classNames: [ "search-bar-component" ],
// the record array (will be set by init())
model: null,
// needed by SortableMixin's arrangedContent
content: sort( "model", "sortBy" ),
sortBy: [ "date:desc" ],
showDropdown: false,
query: "",
reQuery: /^\S{3,}/,
filters,
filter: "all",
hotkeys: [
{
code: "Slash",
action: "focus"
}
],
init() {
this._super( ...arguments );
const store = get( this, "store" );
store.findAll( "search" )
.then( records => {
set( this, "model", records );
});
},
async addRecord( query, filter ) {
const store = get( this, "store" );
const model = get( this, "model" );
const match = model.filter( record =>
query === get( record, "query" )
&& filter === get( record, "filter" )
);
let record;
// found a matching record? just update the date property, save the record and return
if ( get( match, "length" ) === 1 ) {
set( match[0], "date", new Date() );
await match[0].save();
return;
}
// we don't want to store more than X records
if ( get( model, "length" ) >= searchHistorySize ) {
const oldestRecord = model.sortBy( "date" ).shiftObject();
await oldestRecord.destroyRecord();
}
// create a new record
const id = 1 + Number( getWithDefault( model, "lastObject.id", 0 ) );
const date = new Date();
record = store.createRecord( "search", { id, query, filter, date } );
await record.save();
model.addObject( record );
},
async deleteAllRecords() {
// delete all records at once and then clear the record array
const model = get( this, "model" );
model.forEach( record => record.deleteRecord() );
await model.save();
model.clear();
},
doSearch( query, filter ) {
set( this, "showDropdown", false );
this.addRecord( query, filter );
get( this, "routing" ).transitionTo( "search", [], {
filter,
query
});
},
_prepareDropdown: on( "didInsertElement", function() {
// dropdown
const $element = this.$();
const dropdown = $element.find( ".searchbar-dropdown" )[ 0 ];
const button = $element.find( ".btn-dropdown" )[ 0 ];
const search = $element.find( "input[type='search']" )
.focus(function() {
next( this, this.select );
})[ 0 ];
$( document.body ).click( event => {
const $target = $( event.target );
// ignore clicks on the input, the dropdown button and on the dropdown itself
if (
!$target.closest( search ).length
&& !$target.closest( button ).length
&& !$target.closest( dropdown ).length
) {
set( this, "showDropdown", false );
}
});
}),
actions: {
back() {
get( this, "routing" ).history( -1 );
},
forward() {
get( this, "routing" ).history( +1 );
},
refresh() {
get( this, "routing" ).refresh();
},
focus() {
this.$( "input[type='search']" ).focus();
},
toggleDropdown() {
const showDropdown = get( this, "showDropdown" );
set( this, "showDropdown", !showDropdown );
},
clear() {
set( this, "query", "" );
},
submit() {
let query = get( this, "query" ).trim();
let filter = get( this, "filter" );
const stream = getStreamFromUrl( query );
if ( stream ) {
query = stream;
filter = "channels";
}
if ( this.reQuery.test( query ) ) {
this.doSearch( query, filter );
}
},
searchHistory( record ) {
const query = get( record, "query" );
const filter = get( record, "filter" );
this.doSearch( query, filter );
},
clearHistory() {
this.deleteAllRecords();
}
}
});