Completed
Push — master ( 66eda7...93bf15 )
by Maxence
02:13
created

searchbar.init   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

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 3
rs 10
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
			settings.lockSearchbox = true;
118
			searchbox.search_icon_more.stop().fadeTo(200, 1);
119
			searchbox.search_icon_close.stop().fadeTo(200, 1);
120
		});
121
122
		searchbox.search_input.on('input', function () {
123
124
			if ($(this).val() === '') {
125
				nav.onSearchReset();
126
			}
127
128
			if (settings.parentHasMethod('onEntryGenerated')) {
129
				settings.parent.onEntryGenerated();
130
			}
131
132
			if (searchbox.searchTimeout === null && searchbar.initSearch(false)) {
133
				searchbox.searchTimeout = _.delay(function () {
134
					searchbar.initSearch(false);
135
					searchbox.searchTimeout = null;
136
				}, 2000);
137
			}
138
		});
139
140
		fullTextSearch.options(settings.searchProviderId);
141
	},
142
143
144
	onOptionsLoaded: function (result) {
145
		searchbox.search_more.html(result[settings.searchProviderId]);
146
		searchbox.search_more.find('INPUT').each(function () {
147
			$(this).on('change', function () {
148
				var search = searchbox.search_input.val();
149
				fullTextSearch.search({
150
					providers: settings.searchProviderId,
151
					search: search,
152
					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...
153
					options: searchbar.getSearchOptions(),
154
					size: 20
155
				});
156
			});
157
		})
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...
158
	},
159
160
161
	getSearchOptions: function () {
162
		var options = {};
163
		searchbox.search_more.find('INPUT').each(function () {
164
			var value = $(this).val();
165
166
			if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
167
				value = '';
168
			}
169
170
			options[$(this).attr('id')] = value;
171
		});
172
173
		return options;
174
	},
175
176
177
	// TODO: do we really need this initSearch, or should we use the one from fulltextsearch.js !?
178
	initSearch: function (force) {
179
		var search = searchbox.search_input.val();
180
181
		if (!force && search.length < 3) {
182
			return false;
183
		}
184
185
		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...
186
			return true;
187
		}
188
189
		curr.lastRequest = search;
190
191
		fullTextSearch.search({
192
			providers: settings.searchProviderId,
193
			search: search,
194
			page: curr.page,
195
			options: searchbar.getSearchOptions(),
196
			size: 20
197
		});
198
199
		return true;
200
	}
201
202
203
};
204
205
206