Completed
Push — master ( 83ab72...026655 )
by Maxence
02:38
created

js/navigate.js   A

Complexity

Total Complexity 24
Complexity/F 1.85

Size

Lines of Code 174
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 24
dl 0
loc 174
rs 10
c 0
b 0
f 0
cc 0
nc 8
mnd 4
bc 21
fnc 13
bpm 1.6153
cpm 1.8461
noi 8

10 Functions

Rating   Name   Duplication   Size   Complexity  
A Navigate.deleteEmptyDiv 0 6 2
A Navigate.onEntryGenerated 0 4 1
A navigate.js ➔ Navigate 0 3 1
A Navigate.initSearch 0 16 3
A Navigate.resetSearch 0 5 1
A Navigate.initPanels 0 11 1
B Navigate.displayPanels 0 52 6
A $(document).ready 0 3 1
B Navigate.init 0 31 1
A Navigate.searchResult 0 11 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
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.setEntryTemplateId($('#template_entry'), self);
50
		fullTextSearch.setResultContainerId($('#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 res = {status: -1};
0 ignored issues
show
Unused Code introduced by
The variable res seems to be never used. Consider removing it.
Loading history...
81
		var self = this;
82
83
		$.ajax({
84
			method: 'GET',
85
			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...
86
		}).done(function (res) {
87
			self.displayPanels(res);
88
		});
89
	},
90
91
92
	displayPanels: function (data) {
93
94
		var ak = Object.keys(data);
95
		for (var i = 0; i < ak.length; i++) {
96
			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...
97
			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...
98
99
			var li = $('<li>', {class: (nav.options !== undefined) ? 'collapsible open' : ''});
100
			var aIcon = $('<a>', {
101
				href: '#',
102
				class: 'search_icon'
103
			});
104
			aIcon.text(title);
105
106
			var ul = $('<ul>');
107
108
109
			if (nav.options !== undefined) {
110
				// var button = $('<button>', {class: 'collapse'});
111
				// li.append(button);
112
113
				for (var j = 0; j < nav.options.length; j++) {
114
					var sub = nav.options[j];
115
116
					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...
117
					var subA = $('<a>', {
118
						href: '#',
119
						text: sub.title
120
					});
121
122
					if (sub.type === 'checkbox') {
123
						ul.append($('<li>').append(subA).append($('<input>', {
124
							class: 'search_checkbox_sub',
125
							type: 'checkbox'
126
						})));
127
					}
128
				}
129
			}
130
131
			li.append(aIcon);
132
			li.append($('<input>', {
133
				class: 'search_checkbox',
134
				type: 'checkbox'
135
			}));
136
			li.append(ul);
137
138
			elements.search_panels.append(li);
139
140
141
		}
142
143
	},
144
145
146
	initSearch: function (force) {
147
		var search = elements.search_input.val();
148
149
		if (!force && search.length < 3) {
150
			return false;
151
		}
152
		var request = {
153
			providers: 'all',
154
			search: search,
155
			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...
156
		};
157
158
		fullTextSearch.search(request, this.searchResult);
159
160
		return true;
161
	},
162
163
164
	resetSearch: function () {
165
		// if (elements.search_input.val() !== '') {
166
		// 	return;
167
		// }
168
	},
169
170
171
	searchResult: function (result) {
172
173
		if (elements.search_json !== null) {
174
			elements.search_json.text(JSON.stringify(result));
175
		}
176
177
		// console.log(JSON.stringify(result));
178
//			OCA.notification.onFail('Search returned no result');
179
//		OCA.notification.onSuccess('Search returned ' + res.meta.size + ' result(s)');
180
181
	},
182
183
184
	onEntryGenerated: function (entry) {
185
		this.deleteEmptyDiv(entry, '#line1');
186
		this.deleteEmptyDiv(entry, '#line2');
187
	},
188
189
190
	deleteEmptyDiv: function (entry, divId) {
191
		var div = entry.find(divId);
192
		if (div.text() === '') {
193
			div.remove();
194
		}
195
	}
196
};
197
198
OCA.FullTextSearch.Example = Navigate;
199
200
201
$(document).ready(function () {
202
	OCA.FullTextSearch.example = new Navigate();
203
});
204
205
206
207