Completed
Push — master ( 924656...92b011 )
by Maxence
02:05
created

Navigate.getOptions   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 3
nc 3
nop 0
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
27
/** global: OCA */
28
/** global: _ */
29
30
const fullTextSearch = OCA.FullTextSearch.api;
0 ignored issues
show
Backwards Compatibility introduced by
'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
31
32
33
var elements = {
34
	searchTimeout: null,
35
	search_input: null,
36
	search_submit: null,
37
	search_result: null,
38
	search_json: null
39
};
40
41
const Navigate = function () {
0 ignored issues
show
Backwards Compatibility introduced by
'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
42
	this.init();
43
};
44
45
Navigate.prototype = {
46
47
	init: function () {
48
		var self = this;
49
50
		elements.search_input = $('#search_input');
51
		elements.search_submit = $('#search_submit');
52
		elements.search_result = $('#search_result');
53
		elements.search_panels = $('#search_navigation');
54
//		elements.search_json = $('#search_json');
55
		elements.divHeader = $('#search_header');
56
57
		fullTextSearch.setEntryTemplate($('#template_entry'), self);
58
		fullTextSearch.setResultContainer(elements.search_result);
59
60
		elements.search_input.on('input', function () {
61
			self.resetSearch();
62
			if (elements.searchTimeout === null && self.initSearch()) {
63
				elements.searchTimeout = _.delay(function () {
64
					self.initSearch();
65
					elements.searchTimeout = null;
66
				}, 3000);
67
			}
68
		});
69
70
		//
71
		// $(document).keypress(function (e) {
72
		// 	if (e.which === 13) {
73
		// 		self.initSearch(true);
74
		// 	}
75
		// });
76
77
		self.initPanels();
78
	},
79
80
81
	initPanels: function () {
82
		var self = this;
83
84
		$.ajax({
85
			method: 'GET',
86
			url: OC.generateUrl('/apps/fulltextsearch/navigation/panels')
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...
87
		}).done(function (res) {
88
			self.displayPanels(res);
89
		});
90
	},
91
92
93
	displayPanels: function (data) {
94
		var self = this;
95
96
		var ak = Object.keys(data);
97
		for (var i = 0; i < ak.length; i++) {
98
			var title = data[ak[i]]['title'];
0 ignored issues
show
Coding Style introduced by
['title'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
99
			var nav = data[ak[i]]['navigation'];
0 ignored issues
show
Coding Style introduced by
['navigation'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
100
101
			var li = $('<li>', {class: (nav.options !== undefined) ? 'collapsible open' : ''});
102
			var aIcon = $('<a>', {
103
				href: '#',
104
				class: 'search_icon'
105
			});
106
			aIcon.text(title);
107
108
			var ul = $('<ul>');
109
			if (nav.options !== undefined) {
110
111
				aIcon.on('click', function () {
0 ignored issues
show
Bug introduced by
It is generally not recommended to make functions within a loop.

While making functions in a loop will not lead to any runtime error, the code might not behave as you expect as the variables in the scope are not imported by value, but by reference. Let’s take a look at an example:

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(function() {
        alert(i);
    });
}

funcs[0](); // alert(10);
funcs[1](); // alert(10);
/// ...
funcs[9](); // alert(10);

If you would instead like to bind the function inside the loop to the value of the variable during that specific iteration, you can create the function from another function:

var createFunc = function(i) {
    return function() {
        alert(i);
    };
};

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(createFunc(i));
}

funcs[0](); // alert(0)
funcs[1](); // alert(1)
// ...
funcs[9](); // alert(9)
Loading history...
112
					var li = $(this).closest('li');
113
					if (li.hasClass('open')) {
114
						li.removeClass('open');
115
					} else {
116
						li.addClass('open');
117
					}
118
				});
119
120
				for (var j = 0; j < nav.options.length; j++) {
121
					var sub = nav.options[j];
122
123
					var subA = $('<a>', {
124
						href: '#',
125
						text: sub.title
126
					});
127
128
					if (sub.type === 'checkbox') {
129
						var subAInput = $('<input>', {
130
							class: 'search_checkbox_sub',
131
							type: 'checkbox',
132
							'data-option': sub.name
133
						});
134
						subAInput.change(function () {
0 ignored issues
show
Bug introduced by
It is generally not recommended to make functions within a loop.

While making functions in a loop will not lead to any runtime error, the code might not behave as you expect as the variables in the scope are not imported by value, but by reference. Let’s take a look at an example:

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(function() {
        alert(i);
    });
}

funcs[0](); // alert(10);
funcs[1](); // alert(10);
/// ...
funcs[9](); // alert(10);

If you would instead like to bind the function inside the loop to the value of the variable during that specific iteration, you can create the function from another function:

var createFunc = function(i) {
    return function() {
        alert(i);
    };
};

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(createFunc(i));
}

funcs[0](); // alert(0)
funcs[1](); // alert(1)
// ...
funcs[9](); // alert(9)
Loading history...
135
							self.initSearch();
136
						});
137
						ul.append($('<li>').append(subA).append(subAInput));
138
					}
139
				}
140
			}
141
142
			li.append(aIcon);
143
			var aInput = $('<input>', {
144
				class: 'search_checkbox',
145
				type: 'checkbox',
146
				'data-provider': ak[i]
147
			});
148
			aInput.change(function () {
0 ignored issues
show
Bug introduced by
It is generally not recommended to make functions within a loop.

While making functions in a loop will not lead to any runtime error, the code might not behave as you expect as the variables in the scope are not imported by value, but by reference. Let’s take a look at an example:

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(function() {
        alert(i);
    });
}

funcs[0](); // alert(10);
funcs[1](); // alert(10);
/// ...
funcs[9](); // alert(10);

If you would instead like to bind the function inside the loop to the value of the variable during that specific iteration, you can create the function from another function:

var createFunc = function(i) {
    return function() {
        alert(i);
    };
};

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(createFunc(i));
}

funcs[0](); // alert(0)
funcs[1](); // alert(1)
// ...
funcs[9](); // alert(9)
Loading history...
149
				self.initSearch();
150
			});
151
152
			li.append(aInput);
153
			li.append(ul);
154
155
			elements.search_panels.append(li);
156
		}
157
158
	},
159
160
161
	getProviders: function () {
162
		var providers = [];
163
		elements.search_panels.find('input').each(function () {
164
			if ($(this).hasClass('search_checkbox') && $(this).is(":checked")) {
165
				providers.push($(this).attr('data-provider'));
166
			}
167
		});
168
169
		if (providers.length === 0) {
170
			return 'all';
171
		}
172
173
		return providers;
174
	},
175
176
177
	getOptions: function () {
178
		var options = {};
179
		elements.search_panels.find('input').each(function () {
180
			if ($(this).hasClass('search_checkbox_sub')) {
181
				options[$(this).attr('data-option')] = (($(this).is(':checked')) ? '1' : '0');
182
			}
183
		});
184
185
		return options;
186
	},
187
188
189
	initSearch: function () {
190
		var search = elements.search_input.val();
191
192
		if (search.length < 3) {
193
			return false;
194
		}
195
196
		var providers = this.getProviders();
197
		var options = this.getOptions();
198
199
		this.displayProviderResults(providers);
200
201
		var request = {
202
			providers: providers,
203
			options: options,
204
			search: search,
205
			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...
206
		};
207
208
		fullTextSearch.search(request, this.searchResult);
209
		return true;
210
	},
211
212
213
	displayProviderResults: function (providers) {
214
		elements.search_result.children('DIV.provider_header').each(function () {
215
			if (providers === 'all' || providers.indexOf($(this).attr('data-id')) > -1) {
216
				$(this).stop().slideDown(100).fadeTo(settings.delay_provider, 1);
0 ignored issues
show
Bug introduced by
The variable settings seems to be never declared. If this is a global, consider adding a /** global: settings */ 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...
217
			} else {
218
				$(this).stop().fadeTo(settings.delay_provider, 0).slideUp(100);
219
			}
220
		});
221
	},
222
223
224
	resetSearch: function () {
225
		// if (elements.search_input.val() !== '') {
226
		// 	return;
227
		// }
228
	},
229
230
231
	searchResult: function (result) {
232
233
		if (elements.search_json !== null) {
234
			elements.search_json.text(JSON.stringify(result));
235
		}
236
237
		// console.log(JSON.stringify(result));
238
//			OCA.notification.onFail('Search returned no result');
239
//		OCA.notification.onSuccess('Search returned ' + res.meta.size + ' result(s)');
240
241
	},
242
243
244
	onEntryGenerated: function (entry) {
245
		this.deleteEmptyDiv(entry, '#line1');
246
		this.deleteEmptyDiv(entry, '#line2');
247
	},
248
249
250
	deleteEmptyDiv: function (entry, divId) {
251
		var div = entry.find(divId);
252
		if (div.text() === '') {
253
			div.remove();
254
		}
255
	}
256
257
258
};
259
260
OCA.FullTextSearch.Example = Navigate;
261
262
263
$(document).ready(function () {
264
	OCA.FullTextSearch.example = new Navigate();
265
});
266
267
268
269