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

Navigate.init   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
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_json: null
38
};
39
40
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...
41
	this.init();
42
};
43
44
Navigate.prototype = {
45
46
	init: function () {
47
		var self = this;
48
49
		fullTextSearch.setEntryTemplate($('#template_entry'), self);
50
		fullTextSearch.setResultContainer($('#search_result'));
51
52
		elements.search_input = $('#search_input');
53
		elements.search_submit = $('#search_submit');
54
		elements.search_panels = $('#search_navigation');
55
//		elements.search_json = $('#search_json');
56
		elements.divHeader = $('#search_header');
57
58
		elements.search_input.on('input', function () {
59
			self.resetSearch();
60
			if (elements.searchTimeout === null && self.initSearch(false)) {
61
				elements.searchTimeout = _.delay(function () {
62
					self.initSearch(false);
63
					elements.searchTimeout = null;
64
				}, 3000);
65
			}
66
		});
67
68
		//
69
		// $(document).keypress(function (e) {
70
		// 	if (e.which === 13) {
71
		// 		self.initSearch(true);
72
		// 	}
73
		// });
74
75
		self.initPanels();
76
	},
77
78
79
	initPanels: function () {
80
		var self = this;
81
82
		$.ajax({
83
			method: 'GET',
84
			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...
85
		}).done(function (res) {
86
			self.displayPanels(res);
87
		});
88
	},
89
90
91
	displayPanels: function (data) {
92
93
		var ak = Object.keys(data);
94
		for (var i = 0; i < ak.length; i++) {
95
			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...
96
			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...
97
98
			var li = $('<li>', {class: (nav.options !== undefined) ? 'collapsible open' : ''});
99
			var aIcon = $('<a>', {
100
				href: '#',
101
				class: 'search_icon'
102
			});
103
			aIcon.text(title);
104
105
			var ul = $('<ul>');
106
107
108
			if (nav.options !== undefined) {
109
				// var button = $('<button>', {class: 'collapse'});
110
				// li.append(button);
111
112
				for (var j = 0; j < nav.options.length; j++) {
113
					var sub = nav.options[j];
114
115
					console.log('sub: ' + JSON.stringify(sub));
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...
116
					var subA = $('<a>', {
117
						href: '#',
118
						text: sub.title
119
					});
120
121
					if (sub.type === 'checkbox') {
122
						ul.append($('<li>').append(subA).append($('<input>', {
123
							class: 'search_checkbox_sub',
124
							type: 'checkbox'
125
						})));
126
					}
127
				}
128
			}
129
130
			li.append(aIcon);
131
			li.append($('<input>', {
132
				class: 'search_checkbox',
133
				type: 'checkbox'
134
			}));
135
			li.append(ul);
136
137
			elements.search_panels.append(li);
138
		}
139
140
	},
141
142
143
	initSearch: function (force) {
144
		var search = elements.search_input.val();
145
146
		if (!force && search.length < 3) {
147
			return false;
148
		}
149
		var request = {
150
			providers: 'all',
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
		};
154
155
		fullTextSearch.search(request, this.searchResult);
156
157
		return true;
158
	},
159
160
161
	resetSearch: function () {
162
		// if (elements.search_input.val() !== '') {
163
		// 	return;
164
		// }
165
	},
166
167
168
	searchResult: function (result) {
169
170
		if (elements.search_json !== null) {
171
			elements.search_json.text(JSON.stringify(result));
172
		}
173
174
		// console.log(JSON.stringify(result));
175
//			OCA.notification.onFail('Search returned no result');
176
//		OCA.notification.onSuccess('Search returned ' + res.meta.size + ' result(s)');
177
178
	},
179
180
181
	onEntryGenerated: function (entry) {
182
		this.deleteEmptyDiv(entry, '#line1');
183
		this.deleteEmptyDiv(entry, '#line2');
184
	},
185
186
187
	deleteEmptyDiv: function (entry, divId) {
188
		var div = entry.find(divId);
189
		if (div.text() === '') {
190
			div.remove();
191
		}
192
	}
193
};
194
195
OCA.FullTextSearch.Example = Navigate;
196
197
198
$(document).ready(function () {
199
	OCA.FullTextSearch.example = new Navigate();
200
});
201
202
203
204