1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import uniq from 'lodash/uniq';
import Marionette from 'backbone.marionette';
export default Marionette.Controller.extend({
pageSize: 50,
initialize(options) {
this.app = options.app;
this.listenTo(options.app.state, 'change:query', this.fetchList);
},
_allFacets() {
return this.options.app.state.get('allFacets').map(facet => {
return { property: facet };
});
},
_enabledFacets() {
const that = this;
let facets = this.options.app.state.get('facets');
const criteria = Object.keys(this.options.app.state.get('query'));
facets = facets.concat(criteria);
facets = facets.map(facet => {
return that.options.app.state.get('transform')[facet] != null
? that.options.app.state.get('transform')[facet]
: facet;
});
facets = uniq(facets);
return facets.filter(facet => that.options.app.state.get('allFacets').indexOf(facet) !== -1);
},
_facetsFromServer() {
const that = this;
const facets = this._enabledFacets();
return facets.filter(
facet => that.options.app.state.get('facetsFromServer').indexOf(facet) !== -1
);
},
fetchList() {},
fetchNextPage() {
this.options.app.state.nextPage();
return this.fetchList(false);
},
enableFacet(id) {
const facet = this.options.app.facets.get(id);
if (facet.has('values') || this.options.app.state.get('facetsFromServer').indexOf(id) === -1) {
facet.set({ enabled: true });
} else {
this.requestFacet(id).done(() => {
facet.set({ enabled: true });
});
}
},
disableFacet(id) {
const facet = this.options.app.facets.get(id);
facet.set({ enabled: false });
this.options.app.facetsView.children.findByModel(facet).disable();
},
toggleFacet(id) {
const facet = this.options.app.facets.get(id);
if (facet.get('enabled')) {
this.disableFacet(id);
} else {
this.enableFacet(id);
}
},
enableFacets(facets) {
facets.forEach(this.enableFacet, this);
},
newSearch() {
this.options.app.state.setQuery({});
},
parseQuery(query, separator) {
separator = separator || '|';
const q = {};
(query || '').split(separator).forEach(t => {
const tokens = t.split('=');
if (tokens[0] && tokens[1] != null) {
q[tokens[0]] = decodeURIComponent(tokens[1]);
}
});
return q;
},
getQuery(separator) {
separator = separator || '|';
const filter = this.options.app.state.get('query');
const route = [];
Object.keys(filter).forEach(property => {
route.push(`${property}=${encodeURIComponent(filter[property])}`);
});
return route.join(separator);
},
getRoute(separator) {
separator = separator || '|';
return this.getQuery(separator);
},
selectNext() {
const index = this.options.app.state.get('selectedIndex') + 1;
if (index < this.options.app.list.length) {
this.options.app.state.set({ selectedIndex: index });
} else if (!this.options.app.state.get('maxResultsReached')) {
const that = this;
this.fetchNextPage().done(() => {
that.options.app.state.set({ selectedIndex: index });
});
} else {
this.options.app.list.trigger('limitReached');
}
},
selectPrev() {
const index = this.options.app.state.get('selectedIndex') - 1;
if (index >= 0) {
this.options.app.state.set({ selectedIndex: index });
} else {
this.options.app.list.trigger('limitReached');
}
}
});
|