forked from gurbitz/tpp-badgemarket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.js
227 lines (195 loc) · 4.84 KB
/
fields.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
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
216
217
218
219
220
221
222
223
224
225
226
227
const no_data_text = "––";
const table_field_generation = {
caption: "Gen",
get_td: badge => {
const td = dom.td(isNumber(badge.gen) ? "G" + badge.gen : badge.gen);
td.style.textAlign = "center";
return td;
},
sorter: (a, b) => {
const cmp = isNumber(b.gen) - isNumber(a.gen);
if (cmp != 0) return cmp;
if (a.gen < b.gen) return -1;
if (a.gen > b.gen) return 1;
return 0;
}
};
const table_field_icon = {
caption: "",
get_td: badge => {
const td = dom.td();
td.style.width = "0px";
td.style.textAlign = "center";
if (badge.icon_data_url !== null) {
const icon = document.createElement("img");
icon.src = badge.icon_data_url;
icon.style.margin = "-4px 0px -4px 0px";
td.appendChild(icon);
}
return td;
},
};
const table_field_name = {
caption: "Name",
get_td: badge => {
return dom.td(badge.bid_name);
},
sorter: (a, b) => {
if (a.index < b.index) return -1;
return 1;
}
};
const table_field_count = {
caption: "Count",
get_td: badge => {
const td = dom.td(badge.count !== null ? badge.count : no_data_text);
td.style.textAlign = "right";
td.style.paddingLeft = "3px";
td.style.paddingRight = "3px";
return td;
},
sorter: (a, b) => {
if (a.count === null) return 0;
if (a.count < b.count) return -1;
if (a.count > b.count) return 1;
return 0;
}
};
const table_field_builder_owned = user => {
const user_id = user.user_id;
return {
caption: user.username,
get_td: badge => {
const owned = badge.owned_by[user_id];
const td = dom.td();
td.style.textAlign = "right";
td.style.paddingRight = "3px";
function show_count() {
td.innerHTML = "";
td.innerText = owned.length > 0 ? owned.length : "";
td.data_list = false;
}
function show_list() {
const table = dom.table();
table.style.width = "100%";
for (const badge of owned) {
const tr = dom.tr([
dom.td(
badge.created_at.split(" ")[0],
{ style: { width: "6em", textAlign: "left" } }
),
dom.td(
badge.source.substr(0, 3),
{ style: { width: "2.5em", textAlign: "left" } }
),
]);
table.appendChild(tr);
}
td.innerHTML = "";
td.appendChild(table);
td.data_list = true;
}
if (owned) {
if (form.checkbox_owned_list.data_checked) {
show_list();
} else {
show_count();
}
if (owned.length > 0) {
td.style.cursor = "pointer";
td.onclick = debug(() => {
if (td.data_list) {
show_count();
} else {
show_list();
}
});
}
} else {
td.innerText = no_data_text;
}
return td;
},
sorter: (a, b) => {
if (a.owned_by[user_id] === undefined) return 0;
if (a.owned_by[user_id].length < b.owned_by[user_id].length) return -1;
if (a.owned_by[user_id].length > b.owned_by[user_id].length) return 1;
return 0;
},
};
};
function add_join_offers(container, badge, offers, buy)
{
for (const offer of offers) {
const span = dom.span(offer.price);
if (buy) {
span.style.marginLeft = "6px";
} else {
span.style.marginRight = "6px";
}
// TODO: Optimize
for (const user of users) {
if (offer.user != user.user_id) continue;
user.apply_style(span);
break;
}
container.appendChild(span);
}
}
const table_field_buy_offers = {
caption: "Buy offers",
get_td: badge => {
const td = dom.td();
td.style.textAlign = "right";
td.style.paddingRight = "12px";
if (badge.buy_offers === null) {
td.innerText = no_data_text;
return td;
}
const offers = [];
for (const offer of badge.buy_offers) {
for (let i = 0; i < offer.amount; ++i) {
offers.push(offer);
if (offers.length >= form.input_max_offers.value) break;
}
if (offers.length >= form.input_max_offers.value) break;
}
offers.reverse();
add_join_offers(td, badge, offers, true);
return td;
},
sorter: (a, b) => {
if (a.buy_offers === null) return 0;
const a_offer = a.buy_offers.length ? a.buy_offers[0].price : 0;
const b_offer = b.buy_offers.length ? b.buy_offers[0].price : 0;
if (a_offer < b_offer) return -1;
if (a_offer > b_offer) return 1;
return 0;
}
};
const table_field_sell_offers = {
caption: "Sell offers",
get_td: badge => {
const td = dom.td();
td.style.paddingLeft = "12px";
if (badge.sell_offers === null) {
td.innerText = no_data_text;
return td;
}
const offers = [];
for (const offer of badge.sell_offers) {
offers.push(offer);
if (offers.length >= form.input_max_offers.value) break;
}
add_join_offers(td, badge, offers, false);
return td;
},
sorter: (a, b) => {
if (a.sell_offers === null) return 0;
const a_offer = a.sell_offers.length ? a.sell_offers[0].price : 0;
const b_offer = b.sell_offers.length ? b.sell_offers[0].price : 0;
if (a_offer < b_offer) return -1;
if (a_offer > b_offer) return 1;
return 0;
}
};