Completed
Push — master ( 312875...a15f40 )
by Maxence
01:56
created

searchbar.searching   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 9.4285
1
/*
2
 * FullTextSearch - Full text search framework for Nextcloud
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OCA */
27
/** global: nav */
28
/** global: _ */
29
/** global: api */
30
/** global: search */
31
/** global: result */
32
/** global: fullTextSearch */
33
/** global: settings */
34
35
36
var searchbox = {
37
	searchTimeout: null,
38
	search_more: null,
39
	search_icon_more: null,
40
	search_icon_close: null,
41
	search_icon: null,
42
	search_input: null,
43
	search_form: null
44
};
45
46
47
var searchbar = {
48
49
	init: function () {
50
		var divHeaderRight = $('div.header-right');
51
52
		searchbox.search_div = $('<div>', {class: 'next_search_div'});
53
		divHeaderRight.prepend(searchbox.search_div);
54
55
		searchbox.search_icon = $('<div>', {class: 'icon-fulltextsearch'});
56
		searchbox.search_icon.css('background-image',
57
			"url('/apps/fulltextsearch/img/fulltextsearch.svg')");
58
		searchbox.search_icon.fadeTo(0, 0.7);
59
		searchbox.search_div.append(searchbox.search_icon);
60
61
		searchbox.search_form = $('<div>');
62
		searchbox.search_form.fadeTo(0, 0);
63
64
		searchbox.search_input = $('<input>', {
65
			id: 'next_search_input',
66
			placeholder: 'Search'
67
		});
68
		searchbox.search_form.append(searchbox.search_input);
69
70
		searchbox.search_more = $('<div>', {class: 'search_more'});
71
		searchbox.search_more.fadeTo(0, 0).hide();
72
73
		searchbox.search_icon_more = $('<div>', {class: 'icon-more-white icon-more-fulltextsearch'});
74
		searchbox.search_icon_more.fadeTo(0, 0);
75
		searchbox.search_icon_more.on('click', function () {
76
			if (curr.moreDisplayed) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
77
				searchbox.search_more.stop().fadeTo(100, 0, function () {
78
					$(this).hide();
79
				});
80
				curr.moreDisplayed = false;
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
81
			} else {
82
				searchbox.search_more.stop().show().fadeTo(100, 1);
83
				curr.moreDisplayed = true;
84
			}
85
		});
86
		searchbox.search_form.append(searchbox.search_icon_more);
87
88
		searchbox.search_icon_close = $('<div>', {class: 'icon-close-white icon-close-fulltextsearch'});
89
		searchbox.search_icon_close.fadeTo(0, 0);
90
		searchbox.search_icon_close.on('click', function () {
91
			settings.lockSearchbox = false;
92
			searchbox.search_icon_more.stop().fadeTo(100, 0);
93
			searchbox.search_icon_close.stop().fadeTo(100, 0);
94
			searchbox.search_more.stop().fadeTo(100, 0, function () {
95
				$(this).hide();
96
			});
97
			curr.moreDisplayed = false;
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
98
			searchbox.search_input.val('');
99
			nav.onSearchReset();
100
		});
101
		searchbox.search_form.append(searchbox.search_icon_close);
102
103
		searchbox.search_form.hover(function () {
104
			searchbox.search_icon.stop().fadeTo(100, 0);
105
			searchbox.search_form.stop().fadeTo(100, 0.8);
106
		}, function () {
107
			if (settings.lockSearchbox === true) {
108
				return;
109
			}
110
			searchbox.search_form.stop().fadeTo(500, 0);
111
			searchbox.search_icon.stop().fadeTo(800, 0.7);
112
		});
113
		searchbox.search_div.append(searchbox.search_form);
114
		searchbox.search_div.append(searchbox.search_more);
115
116
		searchbox.search_input.on('focus', function () {
117
			searchbar.searching();
118
		});
119
120
		searchbox.search_input.on('input', function () {
121
122
			if ($(this).val() === '') {
123
				nav.onSearchReset();
124
			}
125
126
			if (settings.parentHasMethod('onEntryGenerated')) {
127
				settings.parent.onEntryGenerated();
128
			}
129
130
			if (searchbox.searchTimeout === null && searchbar.initSearch(false)) {
131
				searchbox.searchTimeout = _.delay(function () {
132
					searchbar.initSearch(false);
133
					searchbox.searchTimeout = null;
134
				}, 2000);
135
			}
136
		});
137
138
		fullTextSearch.options(settings.searchProviderId);
139
140
		$(window).bind('keydown', function (event) {
141
			if (event.ctrlKey || event.metaKey) {
142
				if (String.fromCharCode(event.which).toLowerCase() === 'f') {
143
					event.preventDefault();
144
					searchbar.searching();
145
				}
146
			}
147
		});
148
	},
149
150
151
	searching: function () {
152
		if (settings.lockSearchbox === true) {
153
			return;
154
		}
155
		settings.lockSearchbox = true;
156
		searchbox.search_icon.stop().fadeTo(100, 0);
157
		searchbox.search_form.stop().fadeTo(100, 0.8);
158
		searchbox.search_input.focus();
159
		searchbox.search_icon_more.stop().fadeTo(200, 1);
160
		searchbox.search_icon_close.stop().fadeTo(200, 1);
161
	},
162
163
164
	onOptionsLoaded: function (result) {
165
		searchbox.search_more.html(result[settings.searchProviderId]);
166
		searchbox.search_more.find('INPUT').each(function () {
167
			$(this).on('change', function () {
168
				var search = searchbox.search_input.val();
169
				fullTextSearch.search({
170
					providers: settings.searchProviderId,
171
					search: search,
172
					page: curr.page,
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
173
					options: searchbar.getSearchOptions(),
174
					size: 20
175
				});
176
			});
177
		})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
178
	},
179
180
181
	getSearchOptions: function () {
182
		var options = {};
183
		searchbox.search_more.find('INPUT').each(function () {
184
			var value = $(this).val();
185
186
			if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
187
				value = '';
188
			}
189
190
			options[$(this).attr('id')] = value;
191
		});
192
193
		return options;
194
	},
195
196
197
	// TODO: do we really need this initSearch, or should we use the one from fulltextsearch.js !?
198
	initSearch: function (force) {
199
		var search = searchbox.search_input.val();
200
201
		if (!force && search.length < 3) {
202
			return false;
203
		}
204
205
		if (curr.lastRequest === search) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
206
			return true;
207
		}
208
209
		curr.lastRequest = search;
210
211
		fullTextSearch.search({
212
			providers: settings.searchProviderId,
213
			search: search,
214
			page: curr.page,
215
			options: searchbar.getSearchOptions(),
216
			size: 20
217
		});
218
219
		return true;
220
	}
221
222
223
};
224
225
226