Completed
Push — master ( 58d870...e50603 )
by
unknown
02:12
created

api.search   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 9.1359
cc 1
nc 1
nop 3

2 Functions

Rating   Name   Duplication   Size   Complexity  
A 0 5 2
A 0 12 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: OC */
28
/** global: settings */
29
/** global: result */
30
/** global: search */
31
/** global: nav */
32
33
34
var api = {
35
36
37
	search: function (request, callback, callbackError) {
38
		var res = {
39
			status: -1,
40
			error: 'Failed to reach server. Try reloading the page'
41
		};
42
43
		nav.onSearchRequest(request);
44
45
		$.ajax({
46
			method: 'GET',
47
			url: OC.generateUrl('/apps/fulltextsearch/v1/search'),
48
			data: {
49
				request: JSON.stringify(request)
50
			}
51
		}).done(function (res) {
52
			if (_.has(res, 'error')) {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ 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...
53
				result.displayError(res);
54
				nav.onError(res.error);
55
				api.onCallback(callbackError, res);
56
				return;
57
			}
58
59
			result.displayResult(res);
60
			nav.onResultDisplayed(res);
61
			api.onCallback(callback, res);
62
		}).fail(function () {
63
			if (!api.onCallback(callbackError, res)) {
64
				nav.onError(res.error);
65
			}
66
		});
67
	},
68
69
70
	retrieveOptions: function (providerId, callback) {
71
		var res = {status: -1};
72
73
		$.ajax({
74
			method: 'GET',
75
			url: OC.generateUrl('/apps/fulltextsearch/options/' + providerId)
76
		}).done(function (res) {
77
			searchbox.onOptionsLoaded(res);
0 ignored issues
show
Bug introduced by
The variable searchbox seems to be never declared. If this is a global, consider adding a /** global: searchbox */ 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...
78
			api.onCallback(callback, res);
79
		}).fail(function () {
80
			nav.failedToAjax();
81
			api.onCallback(callback, res);
82
		});
83
	},
84
85
86
	onCallback: function (callback, result) {
87
		if (callback && (typeof callback === 'function')) {
88
			if (typeof result === 'object') {
89
				callback(result);
90
			} else {
91
				callback({status: -1});
92
			}
93
94
			return true;
95
		}
96
97
		return false;
98
	}
99
100
};
101