Completed
Push — master ( 716024...81e73e )
by Maxence
02:01
created

searchbox.timingRequest   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 7 2
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
	searchInput: null,
38
	searchMore: null,
39
	divFullTextSearchIcon: null,
40
	divFullTextSearchPopup: null
41
};
42
43
44
var searchbox = {
45
46
	init: function () {
47
48
		var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
49
50
		// we remove old search
51
		var search_form = $('FORM.searchbox');
52
		if (search_form.length > 0) {
53
			search_form.remove();
54
		}
55
56
57
		var divHeaderRight = $('DIV.header-right');
58
		var divFullTextSearch = $('<div>', {id: 'fulltextsearch'});
59
		divHeaderRight.prepend(divFullTextSearch);
60
61
		box_elements.divFullTextSearchIcon = searchbox.generateFullTextSearchIcon();
62
		box_elements.divFullTextSearchPopup = searchbox.generateFullTextSearchPopup();
63
		divFullTextSearch.append(box_elements.divFullTextSearchIcon);
64
		divFullTextSearch.append(box_elements.divFullTextSearchPopup);
65
66
		OC.registerMenu(box_elements.divFullTextSearchIcon, box_elements.divFullTextSearchPopup,
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ 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...
67
			searchbox.displayedSearchPopup);
68
69
		api.retrieveOptions(settings.searchProviderId);
70
71
		$(window).bind('keydown', function (event) {
72
			if (event.ctrlKey || event.metaKey) {
73
				if (String.fromCharCode(event.which).toLowerCase() === 'f') {
74
					event.preventDefault();
75
					searchbox.displaySearchPopup(true);
76
				}
77
78
				return;
79
			}
80
81
			if (event.which === 27) {
82
				searchbox.displaySearchPopup(false);
83
			}
84
		});
85
86
87
	},
88
89
90
	generateFullTextSearchIcon: function () {
91
		var className = 'icon-fulltextsearch';
92
		if (!OCA.Theming.inverted) {
93
			className = 'icon-fulltextsearch-white';
94
		}
95
96
		var icon = $('<div>', {
97
			id: 'fts-icon',
98
			tabindex: 0,
99
			role: 'link',
100
			class: className + ' menutoggle'
101
		});
102
103
		icon.fadeTo(0, 0.6);
104
105
		return icon;
106
	},
107
108
109
	generateFullTextSearchPopup: function () {
110
		var popup = $('<div>', {
111
			id: 'fts-popup'
112
		});
113
114
		var self = this;
115
		box_elements.searchInput = $('<input>', {
116
			id: 'fts-input',
117
			placeholder: 'Search ' + settings.searchProviderName
118
		}).on('keyup', self.searching);
119
		box_elements.searchMore = $('<div>', {id: 'fts-more'});
120
121
		var divHeader = $('<div>', {id: 'fts-header'});
122
		divHeader.append($('<div>').append(box_elements.searchInput));
123
124
		popup.append(divHeader);
125
		popup.append(box_elements.searchMore);
126
127
		return popup;
128
	},
129
130
131
	displaySearchPopup: function (display) {
132
		if (display) {
133
			OC.showMenu(box_elements.divFullTextSearchIcon, box_elements.divFullTextSearchPopup,
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ 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...
134
				searchbox.displayedSearchPopup);
135
		} else {
136
			OC.hideMenus(null);
137
		}
138
	},
139
140
141
	displayedSearchPopup: function () {
142
		box_elements.searchInput.focus();
143
	},
144
145
146
	searching: function (force) {
147
		var search = box_elements.searchInput.val();
148
		if (force === undefined) {
149
			force = false;
150
		}
151
152
		if (search.length < 3) {
153
			return;
154
		}
155
156
		if (!force && 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...
157
			return;
158
		}
159
		curr.lastRequest = search;
160
		if (!searchbox.timingRequest(force)) {
161
			return;
162
		}
163
164
		api.search({
165
			providers: settings.searchProviderId,
166
			search: search,
167
			page: curr.page,
168
			options: searchbox.getSearchOptions(),
169
			size: 20
170
		});
171
	},
172
173
174
	timingRequest: function (force) {
175
		if (curr.lastRequestTimer === null) {
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...
176
			curr.lastRequestTimer = window.setTimeout(function () {
177
				curr.lastRequestTimer = null;
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...
178
				if (curr.lastRequestTimerQueued) {
179
					curr.lastRequestTimerQueued = false;
180
					searchbox.searching(curr.lastRequestTimerForcing);
181
				}
182
			}, settings.searchTimer);
183
		} else {
184
			curr.lastRequestTimerQueued = true;
185
			curr.lastRequestTimerForcing = force;
186
			return false;
187
		}
188
189
		return true;
190
	},
191
192
193
	onOptionsLoaded: function (result) {
194
		if (!result[settings.searchProviderId]) {
195
			return;
196
		}
197
198
		box_elements.searchMore.html(result[settings.searchProviderId]);
199
		box_elements.searchMore.find('INPUT').each(function () {
200
			$(this).on('change keyup', function () {
201
				searchbox.searching(true);
202
			});
203
		})
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...
204
	},
205
206
207
	/**
208
	 *
209
	 * 0.6.0
210
	 *
211
	 *
212
	 */
213
	//
214
	//
215
	// initFullTextSearchBox: function () {
216
	// 	if (box_elements.searchBoxInitialized) {
217
	// 		return;
218
	// 	}
219
	// 	var self = this;
220
	//
221
	// 	box_elements.search_input.unbind('keyup');
222
	// 	box_elements.search_input.bind('keyup blur change', function () {
223
	// 		if ($(this).val() === '') {
224
	// 			self.displaySearchOptionsIcon(false);
225
	// 		} else {
226
	// 			self.displaySearchOptionsIcon(true);
227
	// 		}
228
	//
229
	// 		self.searching();
230
	// 	});
231
	//
232
	// 	box_elements.searchBoxInitialized = true;
233
	// },
234
	//
235
236
237
	getSearchOptions: function () {
238
		var options = {};
239
240
		if (box_elements.searchMore === null) {
241
			return options;
242
		}
243
244
		box_elements.searchMore.find('INPUT').each(function () {
245
			var value = $(this).val();
246
247
			if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
248
				value = '';
249
			}
250
251
			options[$(this).attr('id')] = value;
252
		});
253
254
		return options;
255
	},
256
257
258
	// TODO: do we really need this initSearch, or should we use the one from fulltextsearch.js !?
259
	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...
260
		// var search = searchbox.search_input.val();
261
		//
262
		// if (!force && search.length < 3) {
263
		// 	return false;
264
		// }
265
		//
266
		// if (curr.lastRequest === search) {
267
		// 	return true;
268
		// }
269
		//
270
		// curr.lastRequest = search;
271
		//
272
		// fullTextSearch.search({
273
		// 	providers: settings.searchProviderId,
274
		// 	search: search,
275
		// 	page: curr.page,
276
		// 	options: searchbar.getSearchOptions(),
277
		// 	size: 20
278
		// });
279
		//
280
		// return true;
281
	}
282
283
284
};
285
286
287