forked from kleientertainment/TheGorgeRecipeBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thegorge-filter.js
170 lines (153 loc) · 8.25 KB
/
thegorge-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
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
// wait Until Loading Complete
function GorgeFilterDishes() {
let options = {
discovered_dishes: discovered_dishes,
dishDisplayModeDefault: "dish-display-mode-opacity"
};
main(options);
// Add credits! Thank you both!
$("#recipebook").after('<div class="recipebook-credits">Craving and Cooking Station filtering done by <a href="https://forums.kleientertainment.com/profile/1125024-cheewind/">cheewind</a> and <a href="https://forums.kleientertainment.com/profile/988803-adai1198/">adai1198</a>! Filtering improvement by <a href="https://github.com/LucoEldritch">LucoEldritch</a>.<br>Traditional and Simplified Chinese by <a href="https://forums.kleientertainment.com/profile/1125024-cheewind/">cheewind</a> and <a href="https://forums.kleientertainment.com/profile/793873-xyangy87/">Xheepey87</a>. Russian by <a href="https://github.com/FBNikita">FBNikita</a>, <a href="https://github.com/SkyFly97">SkyFly97</a>, <a href="https://github.com/TiMKroyer">TiMKroyer</a>, and <a href="https://steamcommunity.com/sharedfiles/filedetails/?id=1240565842&tscn=1529691187">RLP-team</a>. Brazilian Portuguese by <a href="https://forums.kleientertainment.com/profile/978076-kynoox_/">Kynoox_</a>, <a href="https://forums.kleientertainment.com/profile/1127938-gians/">GianS</a> and <a href="https://github.com/LucoEldritch">LucoEldritch</a>. Serbian by <a href="https://github.com/JyggalagSila">Bluebell</a>. Turkish by <a href="https://github.com/omergencc">omergencc</a>. Vietnamese by <a href="https://forums.kleientertainment.com/profile/902088-trantuanlinh">BoiRoiVL</a>. Thai by <a href="https://forums.kleientertainment.com/profile/1127762-wadashark">WaDaShark</a>. Polish by <a href="https://forums.kleientertainment.com/profile/408118-thalkas">Thalkas</a>, <a href="https://forums.kleientertainment.com/profile/247694-matek678">Matek678</a>, and <a href="https://github.com/Zeotares">Zeotares</a>. Korean by <a href="https://github.com/StarlitEarth">StarlitEarth</a>. German by <a href="https://forums.kleientertainment.com/profile/541898-mueck">Mueck</a>.<br>Want to add your own language? Submit a request on <a href="https://github.com/kleientertainment/TheGorgeRecipeBook">github</a>.</div>');
}
// this is the code which will be injected into a given page...
function main(options) {
// options
let dishDisplayModeDefault = options.dishDisplayModeDefault;
let discovered_dishes = options.discovered_dishes;
// translate
//let translateToUserLanguage = text => browser.i18n.getMessage(text);
//let t = translateToUserLanguage;
/////////////////////////////////////////////
const stations = ["pot", "oven", "grill"];
let btn_station_div = $('<div/>', {
class: 'btn_station_div'
});
for (let station of stations) {
let label = document.createElement("label");
label.setAttribute('class', "button");
label.setAttribute('for', station);
label.innerText = loc_string(station);
let checkbox = document.createElement("input");
checkbox.setAttribute('id', station);
checkbox.setAttribute('type', 'checkbox');
checkbox.classList.add('class', 'invisible');
checkbox.classList.add('station_btn');
btn_station_div.append(checkbox);
btn_station_div.append(label);
}
const categories = [
"all", "snack", "bread", "veggie", "soup",
"fish", "meat", "cheese", "pasta", "sweet"
];
let btn_cat_div = $('<div/>', {
class: 'btn_cat_div'
});
for (let category of categories) {
let label = document.createElement("label");
label.setAttribute('class', "button");
label.setAttribute('for', category);
label.innerText = loc_string(category);
let radio = document.createElement("input");
radio.setAttribute('id', category);
radio.setAttribute('type', 'radio');
radio.setAttribute('class', 'invisible');
radio.classList.add('cat_btn');
radio.setAttribute('name', 'category');
if (category == 'all') radio.checked = true; // default
btn_cat_div.append(radio);
btn_cat_div.append(label);
}
let div_all_btns = $('<div id="filter-button"/>');
div_all_btns.append(btn_cat_div);
div_all_btns.append(btn_station_div);
$('.recipelist').prepend(div_all_btns);
////////////////////////////////////////////
// no br element
// $('.recipelist-dishes > br').remove(); // the br is necessary to show two rows on mobile
//
let lastClickDish = 0;
let lastHighLight = 0;
let dishElement = $('.recipelist-dishes > li');
let currentCategory = 'all';
let currentStationStatus = {
pot: false,
oven: false,
grill: false
}
$('.station_btn').on('change', e => {
currentStationStatus[e.target.id] = e.target.checked;
highlight();
// relocateClickDish
if (dishElement.eq(lastClickDish).hasClass('lowpoint'))
$('.recipelist-dishes > li:not(.lowpoint)').first()[0].click();
});
$('.cat_btn').on('change', e => {
currentCategory = e.target.id;
highlight();
// relocateClickDish
if (dishElement.eq(lastClickDish).hasClass('lowpoint'))
$('.recipelist-dishes > li:not(.lowpoint)').first()[0].click();
});
$('.recipelist-dishes > li,\
.recipelist-dishes > li > icon-container > *').click(e => {
let index = Number.parseInt(e.target.getAttribute("dish") ||
e.target.parentElement.getAttribute("dish") ||
e.target.parentElement.parentElement.getAttribute("dish") ||
e.target.parentElement.parentElement.parentElement
.getAttribute("dish")) - 1;
if (!isNaN(index)) {
if (dishElement.eq(index).hasClass('lowpoint'))
return;
dishElement.eq(lastHighLight).removeClass('selected');
dishElement.eq(index).addClass('selected');
SelectDish(dishElement[index]);
lastHighLight = lastClickDish = index;
}
});
$('.recipelist-dishes > li,\
.recipelist-dishes > li > icon-container > *').hover(e => {
let index = Number.parseInt(e.target.getAttribute("dish") ||
e.target.parentElement.getAttribute("dish") ||
e.target.parentElement.parentElement.getAttribute("dish") ||
e.target.parentElement.parentElement.parentElement
.getAttribute("dish")) - 1;
if (!isNaN(index)) {
if (dishElement.eq(index).hasClass('lowpoint'))
return;
dishElement.eq(lastHighLight).removeClass('selected');
dishElement.eq(index).addClass('selected');
lastHighLight = index;
SelectDish(dishElement[index]);
}
}, e => {
dishElement.eq(lastHighLight).removeClass('selected');
dishElement.eq(lastClickDish).addClass('selected');
lastHighLight = lastClickDish;
SelectDish(dishElement[lastClickDish]);
});
// default
dishElement[lastClickDish].childNodes[0].click();
function highlight() {
for (let id = 1; id <= 70; ++id) {
let dish = $(".recipelist-dishes .dish[data-index=" + id + "]");
let currentStationAll = !currentStationStatus.pot &&
!currentStationStatus.oven && !currentStationStatus.grill;
// normal dish
let needHighlight = (
(discovered_dishes[id].cravings != null && (currentCategory == 'all' || discovered_dishes[id].cravings.indexOf(currentCategory) != -1))
&&
(currentStationAll || discovered_dishes[id].station.some(dishStations => currentStationStatus[dishStations])))
// dish 70
||
(discovered_dishes[id].cravings == null && currentStationAll && currentCategory == 'all');
if (dishDisplayModeDefault) {
if (needHighlight)
$(dish).removeClass('lowpoint').removeClass('translucent');
else $(dish).addClass('lowpoint').addClass('translucent');
} else {
if (needHighlight)
$(dish).removeClass('lowpoint').removeClass('invisible');
else $(dish).addClass('lowpoint').addClass('invisible');
}
}
}
}