-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathSelection.js
More file actions
182 lines (157 loc) · 5.23 KB
/
Selection.js
File metadata and controls
182 lines (157 loc) · 5.23 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import parse from 'voll';
let switchPlace = (clause, criterion, toDelete, threeState) => {
let index;
if ((index = clause.selection.indexOf(criterion)) > -1) {
clause.selection.splice(index, 1);
if (!toDelete)
clause.exclusion.push(criterion);
} else if ((index = clause.exclusion.indexOf(criterion)) > -1) {
clause.exclusion.splice(index, 1);
if (!toDelete && !threeState)
clause.selection.push(criterion);
} else {
clause.selection.push(criterion);
}
};
let clauseToString = (x) => (
(x.data)
? x.data.map((y) => `(${clauseToString(y)})`)
: [
...x.selection.map((y) => `"${y}"`),
...x.exclusion.map((y) => `NOT("${y}")`)
]
)
.join((x.type === 'intersection') ? ' AND ' : ' OR ')
.replace('\'', '’');
export default class Selection {
constructor(query) {
let data = (query)
? [{
type: 'intersection',
selection: [query],
exclusion: []
}]
: [];
this.selectionJSON = {type: 'intersection', data};
}
/**
* Loads actual selection from the URI.
*
* @returns {Selection} current instance
*/
static fromURI = () => {
const selection = new Selection();
const actualData = JSON.parse((new URLSearchParams(window.location.search)).get('t'));
if (actualData) {
selection.selectionJSON = actualData;
}
return selection;
}
toJSON = () => JSON.stringify(this.selectionJSON);
toURI = () => `/?t=${this.toJSON()}`;
toBooleanString = () => clauseToString(this.selectionJSON);
toFilter = () => parse(this.toBooleanString());
getSelected = () => this.selectionJSON.data.map(s => s.selection).flat();
getExcluded = () => this.selectionJSON.data.map(s => s.exclusion).flat();
isSelectedOrExcluded = (criterion) => {
if (this.getSelected().includes(criterion))
return 'Selected';
if (this.getExcluded().includes(criterion))
return 'Excluded';
}
setJSON = (json) => {
this.selectionJSON = JSON.parse(json);
}
/**
* Toggle topic selection.
*
* Create a new clause.
* If there is an existing clause, move it from selection to exclusion,
* then delete.
*
* @param {string} topicID Topic identifier
* @param {object} [viewpoint] Viewpoint object
*/
toggleTopic = (topicID, viewpoint = null) => {
const existingClause = viewpoint ? this.selectionJSON.data.find(s => {
const allTopics = [...s.selection, ...s.exclusion];
if (allTopics.length === 0 || viewpoint[allTopics[0]] === undefined) {
return false;
}
return (!viewpoint[allTopics[0]].hasOwnProperty('broader') && !viewpoint[topicID].hasOwnProperty('broader'))
|| (
viewpoint[allTopics[0]].hasOwnProperty('broader')
&& viewpoint[allTopics[0]].broader[0].id
) === (
viewpoint[topicID].hasOwnProperty('broader')
&& viewpoint[topicID].broader[0].id
);
}) : false;
if (existingClause) {
switchPlace(existingClause, topicID, false, true);
if ([...existingClause.selection, ...existingClause.exclusion].length === 0)
this.selectionJSON.data.splice(this.selectionJSON.data.indexOf(existingClause), 1);
} else {
this.addTopic(topicID);
}
}
/**
* Add topic to selection.
*
* @param {string} topicID Topic identifier
*/
addTopic = (topicID) => {
const existing = this.selectionJSON.data.some(e =>
e.selection.find(topic => topic === topicID)
|| e.exclusion.find(topic => topic === topicID)
);
if (!existing) {
this.selectionJSON.data.push({type: 'intersection', selection: [topicID], exclusion: []});
}
}
addTopicArray = (arrayAttributes) => {
const data = this.selectionJSON.data;
try {
for (let i = 0; i < data.length; i++) {
if (data[i].selection[0].includes(':')) {
if (data[i].selection[0].split(':')[0] === arrayAttributes[0].split(':')[0]) {
data.splice(i, 1);
}
}
}
this.selectionJSON.data.push({type: 'union', selection: arrayAttributes, exclusion: []});
} catch (error) {
console.log(error);
}
}
/**
* Remove topic from selection.
*
* First remove topicID from clause element.
* Then remove clause element with empty content.
*
* @param {string} topicID Topic identifier
*/
removeTopic = (topicID) => {
this.selectionJSON.data = this.selectionJSON.data
.map(e => ({
...e,
selection: e.selection.filter(s => s !== topicID),
exclusion: e.exclusion.filter(s => s !== topicID),
}))
.filter(e => e.selection.length || e.exclusion.length);
}
toggleCriterion = (criterion, toDelete) => {
let clause = this.selectionJSON.data.find(s => [...s.selection, ...s.exclusion].includes(criterion));
switchPlace(clause, criterion, toDelete);
if ([...clause.selection, ...clause.exclusion].length === 0)
this.selectionJSON.data.splice(this.selectionJSON.data.indexOf(clause), 1);
}
toggleOperator = (clause) => {
clause.type = (clause.type === 'intersection') ? 'union' : 'intersection';
}
getClauses = () => this.selectionJSON.data;
getMainClause = () => this.selectionJSON;
isEmpty = () => this.selectionJSON.data.length === 0;
getOperator = () => this.selectionJSON.type;
}