Completed
Push — master ( 6b3a87...e02f9a )
by Maxence
02:11
created

js/navigate.js   A

Complexity

Total Complexity 24
Complexity/F 1.85

Size

Lines of Code 172
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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