-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfilter.js
More file actions
215 lines (196 loc) · 6.31 KB
/
filter.js
File metadata and controls
215 lines (196 loc) · 6.31 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import eventManager from 'src/utils/eventManager.js';
import * as settings from 'src/utils/settings/index.js';
import { global, globalSet } from 'src/utils/global.js';
import style from 'src/utils/style.js';
import onPage from 'src/utils/onPage.js';
import { max } from 'src/utils/cardHelper.js';
import Translation from 'src/structures/constants/translation';
import { getTranslationArray } from '../underscript/translation.js';
export const crafting = onPage('Crafting');
export const decks = onPage('Decks');
export const filters = [
/**
* @param {Card} card
* @param {boolean} removed
* @returns {boolean | undefined}
*/
function templateFilter(card, removed) {
return removed;
},
];
filters.shift(); // Remove template
const base = {
onChange: () => applyLook(),
category: Translation.Setting('category.filter'),
page: 'Library',
default: true,
};
const setting = settings.register({
...base,
default: false,
name: Translation.Setting('filter.disable'),
key: 'underscript.deck.filter.disable',
});
const splitBaseGen = settings.register({
...base,
name: Translation.Setting('filter.split'),
key: 'underscript.deck.filter.split',
});
const tribe = settings.register({
...base,
name: Translation.Setting('filter.tribe'),
key: 'underscript.deck.filter.tribe',
});
const owned = settings.register({
...base,
name: Translation.Setting('filter.collection'),
key: 'underscript.deck.filter.collection',
});
const shiny = settings.register({
...base,
name: Translation.Setting('filter.shiny'),
key: 'underscript.deck.filter.shiny',
options: () => {
const { key } = Translation.Setting('filter.shiny.option');
const options = getTranslationArray(key);
return ['Never (default)', 'Deck', 'Always'].map((val, i) => [
options[i],
val,
]);
},
default: 'Deck',
});
style.add(
'#collectionType { margin-bottom: 10px; }',
'.filter input+* { opacity: 0.4; }',
'.filter input:checked+* { opacity: 1; }',
'.filter input:disabled, .filter input:disabled+* { display: none; }',
);
function applyLook(refresh = decks || crafting) {
$('input[onchange^="applyFilters();"]').parent().parent().toggleClass('filter', !setting.value());
if (crafting) {
if (setting.value() || !splitBaseGen.value()) {
$('#baseGenInput').prop('checked', false).prop('disabled', false);
$('.rarityInput.custom').parent().remove();
} else if (!$('#baseRarityInput').length) {
// Add BASE
$('#commonRarityInput').parent().before(createButton('BASE'), ' ');
$('#baseGenInput').prop('checked', true).prop('disabled', true).parent()
.after(createButton('TOKEN'));
}
}
// Tribe filter
if (setting.value()) {
$('#allTribeInput').parent().remove();
} else if (!$('#allTribeInput').length) {
$('#monsterInput').parent().before(allTribeButton(), ' ');
}
$('#allTribeInput').prop('disabled', !tribe.value());
const allCardsElement = $('[data-i18n="[html]crafting-all-cards"]');
if (setting.value() || !owned.value()) {
$('#collectionType').remove();
allCardsElement.removeClass('invisible');
} else if (!$('#collectionType').length) {
eventManager.on('underscript:ready', () => {
allCardsElement.addClass('invisible')
.after(ownSelect());
});
}
$('#shinyInput').prop('disabled', mergeShiny());
if (refresh) {
global('applyFilters')();
global('showPage')(0);
}
}
eventManager.on(':preload:Decks :preload:Crafting', () => {
// Update filter visuals
applyLook(false);
globalSet('isRemoved', function newFilter(card) {
if (setting.value()) return this.super(card);
return filters.reduce((removed, func) => {
const val = func.call(this, card, removed);
if (typeof val === 'boolean') {
return val;
}
return removed;
}, false);
});
});
function mergeShiny() {
return shiny.value() === 'Always' || (decks && shiny.value() === 'Deck');
}
function createButton(type) {
return $(`
<label>
<input type="checkbox" id="${type.toLowerCase()}RarityInput" class="rarityInput custom" rarity="${type}" onchange="applyFilters(); showPage(0);">
<img src="images/rarity/BASE_${type}.png">
</label>`);
}
function allTribeButton() {
return $(`
<label>
<input type="checkbox" id="allTribeInput" onchange="applyFilters(); showPage(0);">
<img src="images/tribes/ALL.png">
</label>`);
}
function ownSelect() {
return $(`
<select id="collectionType" onchange="applyFilters(); showPage(0);">
<option value="all">${Translation.Vanilla('crafting-all-cards')}</option>
<option value="owned">${Translation.General('cards.owned')}</option>
<option value="unowned">${Translation.General('cards.unowned')}</option>
<option value="maxed">${Translation.General('cards.maxed')}</option>
<option value="surplus">${Translation.General('cards.surplus')}</option>
<option value="craftable">${Translation.General('cards.craftable')}</option>
</select>
`);
}
filters.push(
function basicFilter(card) {
// Rarity, Type, Extension, Search
return this.super(card);
},
function shinyFilter(card, removed) {
if (removed && mergeShiny()) {
return this.super({
...card,
shiny: !card.shiny,
});
}
return removed;
},
function baseGenFilter(card, removed) {
if (!removed && crafting && splitBaseGen.value()) {
if (card.rarity === 'BASE' && !card.shiny && !$('#baseRarityInput').prop('checked')) {
return true;
}
if (card.rarity === 'TOKEN' && !$('#tokenRarityInput').prop('checked')) {
return true;
}
}
return removed;
},
function tribeFilter(card, removed) {
if (!removed && tribe.value() && $('#allTribeInput').prop('checked')) {
return !card.tribes.length;
}
return removed;
},
function searchFilter(card, removed) { // Custom keywords
return removed;
},
function ownedFilter(card, removed) {
if (!removed && crafting && owned.value()) {
switch ($('#collectionType').val()) {
case 'owned': return !card.quantity;
case 'unowned': return card.quantity > 0;
case 'maxed': return card.quantity < max(card.rarity);
case 'surplus': return card.quantity <= max(card.rarity);
case 'craftable': return card.quantity >= max(card.rarity);
case 'all':
default: break;
}
}
return removed;
},
);