-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
276 lines (256 loc) · 7.92 KB
/
Copy pathsort.js
File metadata and controls
276 lines (256 loc) · 7.92 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Wall layout sort — resolution, date, EXIF, format/codec, CDN source & region.
*/
(function (global) {
const CDN_HINTS = [
["wikimedia", "commons"],
["upload.wikimedia", "commons"],
["staticflickr", "flickr-us"],
["flickr", "flickr"],
["jamendo", "eu-audio"],
["archive.org", "archive"],
["cloudfront", "aws-cdn"],
["akamai", "akamai"],
["fastly", "fastly"],
["cloudflare", "cf"],
["googleusercontent", "gcp"],
["amazonaws", "aws"],
["digitaloceanspaces", "do"]
];
function summary(entry) {
return entry?.probeMeta?.summary || entry?.probeMeta?.browser || {};
}
function itemOf(entry) {
return entry?.item || entry;
}
function resolutionOf(entry) {
const s = summary(entry);
const w = Number(s.width) || 0;
const h = Number(s.height) || 0;
return w * h;
}
function dateOf(entry) {
const s = summary(entry);
const it = itemOf(entry);
const raw =
s.format_creation_time ||
s.creation_time ||
it.published ||
it.indexed_on ||
null;
if (!raw) return 0;
const t = Date.parse(String(raw));
return Number.isFinite(t) ? t : 0;
}
function exifKeyOf(entry) {
const s = summary(entry);
const it = itemOf(entry);
return [
s.make,
s.model,
s.lens,
s.camera,
s.format_encoder,
s.encoder,
it.title
]
.filter(Boolean)
.join(" ")
.toLowerCase();
}
function formatKeyOf(entry) {
const s = summary(entry);
const it = itemOf(entry);
return [
it.mediaType,
it.mime,
it.filetype,
s.video_codec,
s.audio_codec,
s.pix_fmt,
s.content_type
]
.filter(Boolean)
.join("|")
.toLowerCase();
}
function framingKeyOf(entry) {
const s = summary(entry);
const w = Number(s.width) || 0;
const h = Number(s.height) || 0;
if (!w || !h) return "4:unknown";
const ratio = w / h;
if (ratio >= 2.1) return "0:ultrawide";
if (ratio >= 1.35) return "1:landscape";
if (ratio <= 0.7) return "3:portrait";
return "2:square";
}
function cameraAngleKeyOf(entry) {
const s = summary(entry);
const it = itemOf(entry);
const corpus = [
s.camera_angle,
s.viewpoint,
s.description,
it.title,
it.snippet
]
.filter(Boolean)
.join(" ")
.toLowerCase();
if (!corpus) return "9:unknown";
if (/(aerial|drone|bird'?s[- ]eye|top[- ]down|overhead)/.test(corpus)) return "0:top";
if (/(low[- ]angle|worm'?s[- ]eye|upward|from below)/.test(corpus)) return "1:low";
if (/(high[- ]angle|looking down|from above)/.test(corpus)) return "2:high";
if (/(close[- ]up|macro|detail shot)/.test(corpus)) return "3:close";
if (/(wide shot|establishing|panorama|long shot)/.test(corpus)) return "4:wide";
return "5:neutral";
}
function positionKeyOf(entry) {
const s = summary(entry);
const it = itemOf(entry);
const corpus = [
s.position,
s.subject_position,
s.camera_position,
it.title,
it.snippet
]
.filter(Boolean)
.join(" ")
.toLowerCase();
if (!corpus) return "9:unknown";
if (/(center|centred|centered|middle)/.test(corpus)) return "0:center";
if (/(left|left-side|left side)/.test(corpus)) return "1:left";
if (/(right|right-side|right side)/.test(corpus)) return "2:right";
if (/(foreground|front)/.test(corpus)) return "3:front";
if (/(background|rear|back)/.test(corpus)) return "4:back";
return "5:mixed";
}
function sourceKeyOf(entry) {
const it = itemOf(entry);
return (it.source || it.provider || it.host || "").toLowerCase();
}
function regionKeyOf(entry) {
const it = itemOf(entry);
const host = (it.host || "").toLowerCase();
if (!host) return "zzz";
let region = "global";
for (const [needle, label] of CDN_HINTS) {
if (host.includes(needle)) {
region = label;
break;
}
}
const parts = host.split(".");
const tld = parts.length > 1 ? parts[parts.length - 1] : "";
const sub = parts.length > 2 ? parts[0] : "";
return `${region}:${tld}:${sub}`;
}
function typeOrder(entry, mediaMeta) {
const mt = itemOf(entry).mediaType || "image";
return mediaMeta?.[mt]?.order ?? 99;
}
function statusOrder(entry) {
const s = entry?.thumbState;
if (s === "loaded") return 0;
if (s === "loading") return 1;
if (s === "failed") return 3;
return 2;
}
function sortList(list, mode, mediaMeta) {
if (!mode || mode === "mixed") return list;
const sorted = [...list];
const cmpStr = (a, b) => a.localeCompare(b);
const cmpNum = (a, b, dir = -1) => (a - b) * dir;
// Finance / ticker heatmaps — change, size, sector, venue, region
const sample = list[0] && (list[0].item || list[0]);
const isFinance = !!(sample && (sample.symbol || sample.mediaType === "ticker"));
if (isFinance) {
const getQ = (e) => (e && (e.item ? e.item.quote : e.quote)) || {};
const getItem = (e) => (e && e.item) || e || {};
switch (mode) {
case "change":
sorted.sort((a, b) => (getQ(b).changePct || 0) - (getQ(a).changePct || 0));
return sorted;
case "volume":
sorted.sort((a, b) => (getQ(b).volume || 0) - (getQ(a).volume || 0));
return sorted;
case "marketcap":
sorted.sort((a, b) => (getQ(b).marketCap || 0) - (getQ(a).marketCap || 0));
return sorted;
case "sector":
sorted.sort((a, b) => {
const sa = getItem(a).sector || "";
const sb = getItem(b).sector || "";
return cmpStr(sa, sb) || cmpStr(getItem(a).symbol || "", getItem(b).symbol || "");
});
return sorted;
case "exchange":
sorted.sort((a, b) => cmpStr(getItem(a).exchange || "", getItem(b).exchange || ""));
return sorted;
case "region":
sorted.sort((a, b) => cmpStr(getItem(a).region || "", getItem(b).region || ""));
return sorted;
default:
break;
}
}
switch (mode) {
case "type":
sorted.sort((a, b) => typeOrder(a, mediaMeta) - typeOrder(b, mediaMeta));
break;
case "loaded":
sorted.sort((a, b) => statusOrder(a) - statusOrder(b));
break;
case "resolution":
sorted.sort((a, b) => cmpNum(resolutionOf(a), resolutionOf(b), -1));
break;
case "resolution-asc":
sorted.sort((a, b) => cmpNum(resolutionOf(a), resolutionOf(b), 1));
break;
case "date":
sorted.sort((a, b) => cmpNum(dateOf(a), dateOf(b), -1));
break;
case "date-asc":
sorted.sort((a, b) => cmpNum(dateOf(a), dateOf(b), 1));
break;
case "exif":
sorted.sort((a, b) => cmpStr(exifKeyOf(a), exifKeyOf(b)));
break;
case "framing":
sorted.sort((a, b) => cmpStr(framingKeyOf(a), framingKeyOf(b)));
break;
case "camera-angle":
sorted.sort((a, b) => cmpStr(cameraAngleKeyOf(a), cameraAngleKeyOf(b)));
break;
case "position":
sorted.sort((a, b) => cmpStr(positionKeyOf(a), positionKeyOf(b)));
break;
case "format":
sorted.sort((a, b) => cmpStr(formatKeyOf(a), formatKeyOf(b)));
break;
case "source":
sorted.sort((a, b) => cmpStr(sourceKeyOf(a), sourceKeyOf(b)));
break;
case "cdn":
case "region":
sorted.sort((a, b) => cmpStr(regionKeyOf(a), regionKeyOf(b)));
break;
default:
break;
}
return sorted;
}
function currentMode() {
return document.getElementById("sortMode")?.value || "mixed";
}
global.VWallSort = {
sortList,
sortEntries: (entries, mode, mediaMeta) => sortList(entries, mode || currentMode(), mediaMeta),
sortItems: (items, mode, mediaMeta) => sortList(items, mode || currentMode(), mediaMeta),
resolutionOf,
dateOf,
currentMode
};
})(window);