forked from honestbleeps/Reddit-Enhancement-Suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filter.js
137 lines (112 loc) · 3.48 KB
/
Filter.js
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
/* @flow */
import { $ } from '../../vendor';
import type { Thing } from '../../utils';
import {
fastAsync,
} from '../../utils';
import { Case } from './Case';
import type { Filterline } from './Filterline';
export class Filter {
id: string;
parent: ?Filterline;
undeletable: boolean;
updatePromise: ?Promise<*>;
baseCase: Class<Case>;
case: Case;
state: boolean | null;
element: HTMLElement;
constructor(id: *, baseCase: *, conditions: *, state: *, undeletable: * = false) {
this.id = id;
this.baseCase = baseCase;
this.state = state;
this.setCase(baseCase.fromConditions(conditions));
this.undeletable = undeletable;
}
createElement() {}
setParent(parent: *) {
this.parent = parent;
}
getStateText(state: ?boolean = this.state, cased: Case = this.case) {
return state === false ?
cased.falseText || `¬ ${this.baseCase.text.toLowerCase()}` :
cased.trueText || this.baseCase.text.toLowerCase();
}
getSaveValues() {
if (this.baseCase.variant === 'basic') {
return { type: this.baseCase.type, state: this.state, conditions: this.case.conditions };
} else {
// The conditions are found in the type's defaultConditions
return { type: this.baseCase.type, state: this.state };
}
}
clear() {
if (this.undeletable) {
this.update(null, null);
} else {
this.state = null;
if (this.parent) this.parent.removeFilter(this);
}
}
setCase(newCase: Case) {
this.case = newCase;
if (this.state !== null) this.case.observe(this);
}
update(state: *, conditions?: *, describeOnly: boolean = false) {
const cased = typeof conditions === 'undefined' ? this.case : this.baseCase.fromConditions(conditions);
if (describeOnly) {
cased.validate();
return `Show only posts which matches "${this.getStateText(state, cased)}"`;
}
if (state !== this.state || this.case !== cased) {
this.state = state;
this.setCase(cased);
if (this.parent) this.updatePromise = Promise.all(this.parent.refreshAll(this));
if (this.parent) this.parent.save();
}
}
refreshThing = (thing: Thing) => {
if (this.parent) this.parent.refreshThing(thing, this);
};
async updateByInputConstruction({
criterion,
clearFilter,
reverseActive,
fromSelected,
}: {
criterion?: string,
clearFilter?: boolean,
reverseActive?: boolean,
fromSelected?: boolean,
}, describeOnly?: boolean = false): Promise<?string> {
if (clearFilter) {
if (describeOnly) return 'Clear filter';
return this.clear();
}
let state, conditions;
if (fromSelected) {
({ state, conditions } = await this.baseCase.getSelectedEntryValue());
} else {
conditions = this.baseCase.criterionToConditions(criterion);
state = this.state;
}
state = reverseActive ? new Map([[false, true], [null, false], [true, false]]).get(state) :
new Map([[false, false], [null, true], [true, true]]).get(state);
return this.update(state, conditions, describeOnly);
}
matches = fastAsync(function*(thing: Thing) {
const result = yield this.case.evaluate(thing);
return this.state === !result;
});
showThingFilterReason(thing: Thing) {
thing.element.setAttribute('filter-reason', this.getStateText(!this.state));
$('<span>', {
class: 'res-filter-remove-entry',
title: JSON.stringify(this.case.conditions, null, ' '),
click: () => { this.update(null); this.refreshThing(thing); },
}).prependTo(thing.element);
}
clearThingFilterReason(thing: Thing) {
thing.element.removeAttribute('filter-reason');
$(thing.element).find('> .res-filter-remove-entry').remove();
}
}