Completed
Push — master ( 026655...6b3a87 )
by Maxence
03:26
created

searchbox.searching   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 8.8571
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 box_elements = {
37
	searchTimeout: null,
38
39
	// v0.6.0
40
41
	searchBoxInitialized: false,
42
	search_form: null,
43
	search_input: null,
44
	moreOptions: false,
45
	moreDisplayed: false,
46
	search_more: null,
47
	iconSearchOptions: 'options.svg',
48
	iconSearch: 'fulltextsearch.svg',
49
	currentBackgroundImage: ''
50
};
51
52
53
var searchbox = {
54
55
	init: function () {
56
57
		var self = this;
58
		box_elements.search_form = $('FORM.searchbox');
59
		box_elements.search_input = $('INPUT#searchbox');
60
61
62
		// on focus of the searchbox, we remove all keyup event
63
		box_elements.search_input.bind('keydown', function () {
64
			self.initFullTextSearchBox();
65
		});
66
67
		// options
68
		if (OCA.Theming.inverted) {
69
			box_elements.iconSearchOptions = 'options_black.svg';
70
			box_elements.iconSearch = 'fulltextsearch_black.svg';
71
		}
72
73
		box_elements.currentBackgroundImage = box_elements.iconSearch;
74
		box_elements.search_input.css({
75
			'background-image': 'url(/apps/fulltextsearch/img/' + box_elements.iconSearch + ')'
76
		});
77
78
		box_elements.search_input.click(function (e) {
79
			var elm = $(this);
80
			var xPos = e.pageX - elm.offset().left;
81
			if (xPos < 26) {
82
				self.switchSearchOptions();
83
			}
84
		});
85
86
		fullTextSearch.retreiveOptions(settings.searchProviderId);
87
88
		box_elements.search_more =
89
			$('<div>', {class: 'search_more'}).css({'border-color': OCA.Theming.color});
90
91
		box_elements.search_form.append(box_elements.search_more);
92
		box_elements.search_more.fadeTo(0, 0).hide();
93
	},
94
95
96
	initFullTextSearchBox: function () {
97
		if (box_elements.searchBoxInitialized) {
98
			return;
99
		}
100
		var self = this;
101
102
		box_elements.search_input.unbind('keyup');
103
		box_elements.search_input.bind('keyup blur change', function () {
104
			if ($(this).val() === '') {
105
				self.displaySearchOptionsIcon(false);
106
			} else {
107
				self.displaySearchOptionsIcon(true);
108
			}
109
110
			self.searching();
111
		});
112
113
		box_elements.searchBoxInitialized = true;
114
	},
115
116
117
	switchSearchOptions: function () {
118
		this.displaySearchOptions(!box_elements.moreDisplayed);
119
	},
120
121
	displaySearchOptions: function (display) {
122
		if (!box_elements.moreOptions) {
123
			return;
124
		}
125
126
		if (display) {
127
			box_elements.search_more.stop().show().fadeTo(100, 1);
128
		} else {
129
			box_elements.search_more.stop().fadeTo(100, 0, function () {
130
				$(this).hide();
131
			});
132
		}
133
		box_elements.moreDisplayed = display;
134
	},
135
136
137
	displaySearchOptionsIcon: function (display) {
138
139
		if (!box_elements.moreOptions) {
140
			return;
141
		}
142
143
		if (display) {
144
			this.switchInputBackgroundImage(box_elements.iconSearchOptions);
145
		} else {
146
			if (box_elements.search_input.val() !== '') {
147
				return;
148
			}
149
150
			this.displaySearchOptions(false);
151
			this.switchInputBackgroundImage(box_elements.iconSearch);
152
		}
153
	},
154
155
156
	switchInputBackgroundImage: function (image) {
157
158
		if (image === box_elements.currentBackgroundImage) {
159
			return;
160
		}
161
		box_elements.currentBackgroundImage = image;
162
163
		box_elements.search_input.stop().animate({'background-position-x': '-70px'}, 150,
164
			function () {
165
				$(this).css({
166
					'background-image': 'url(/apps/fulltextsearch/img/' + image + ')'
167
				}).animate({'background-position-x': '6px'}, 150);
168
			});
169
	},
170
171
172
	searching: function () {
173
174
		var search = box_elements.search_input.val();
175
		console.log('searching ' + search);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
176
177
		// 	fullTextSearch.search({
178
		// 		providers: settings.searchProviderId,
179
		// 		search: search,
180
		// 		page: curr.page,
181
		// 		options: searchbox.getSearchOptions(),
182
		// 		size: 20
183
		// 	});
184
		// });
185
186
		// if (settings.lockSearchbox === true) {
187
		// 	return;
188
		// }
189
		// settings.lockSearchbox = true;
190
		// searchbox.search_icon.stop().fadeTo(100, 0);
191
		// searchbox.search_form.stop().fadeTo(100, 0.8);
192
		// searchbox.search_input.focus();
193
		// searchbox.search_icon_close.stop().fadeTo(200, 1);
194
		// if (settings.noMoreOptions) {
195
		// 	searchbox.search_icon_more.stop().fadeTo(200, 1);
196
		// }
197
	},
198
199
200
	onOptionsLoaded: function (result) {
201
		if (!result[settings.searchProviderId]) {
202
			box_elements.moreOptions = false;
203
			return;
204
		}
205
206
		box_elements.moreOptions = true;
207
		box_elements.search_input.find('background-image').on('click', function () {
208
			console.log('___');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
209
		});
210
		box_elements.search_more.html(result[settings.searchProviderId]);
211
		box_elements.search_more.find('INPUT').each(function () {
212
			$(this).on('change', searchbox.searching);
213
		})
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...
214
	},
215
216
217
	getSearchOptions: function () {
218
		var options = {};
219
		// searchbox.search_more.find('INPUT').each(function () {
220
		// 	var value = $(this).val();
221
		//
222
		// 	if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
223
		// 		value = '';
224
		// 	}
225
		//
226
		// 	options[$(this).attr('id')] = value;
227
		// });
228
229
		return options;
230
	},
231
232
233
	// TODO: do we really need this initSearch, or should we use the one from fulltextsearch.js !?
234
	initSearch: function (force) {
0 ignored issues
show
Unused Code introduced by
The parameter force is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
235
		// var search = searchbox.search_input.val();
236
		//
237
		// if (!force && search.length < 3) {
238
		// 	return false;
239
		// }
240
		//
241
		// if (curr.lastRequest === search) {
242
		// 	return true;
243
		// }
244
		//
245
		// curr.lastRequest = search;
246
		//
247
		// fullTextSearch.search({
248
		// 	providers: settings.searchProviderId,
249
		// 	search: search,
250
		// 	page: curr.page,
251
		// 	options: searchbar.getSearchOptions(),
252
		// 	size: 20
253
		// });
254
		//
255
		// return true;
256
	}
257
258
259
};
260
261
262