File size: 12,414 Bytes
0d77187
 
 
 
 
eb4f25e
0d77187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf0ce78
0d77187
 
 
b1fe0ec
0d77187
b1fe0ec
0d77187
 
 
 
 
 
 
 
 
b1fe0ec
0d77187
 
 
 
 
 
 
 
 
b1fe0ec
0d77187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ba70fe
 
0d77187
 
 
 
 
 
 
 
 
 
 
 
 
 
eb4f25e
0d77187
 
eb4f25e
0d77187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb4f25e
0d77187
 
eb4f25e
0d77187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";

(() => {
  const DATASET = "ankanmbz/worldguide-results";
  const REMOTE_BASE = `https://huggingface.co/datasets/${DATASET}/resolve/main/`;
  const MODEL_ORDER = ["Our", "Cosmos2.5", "HunyuanVideo-1.5", "MiniMax-H3", "SkyReels", "LTX-2.5", "Barnini", "TempAct", "PhysAgent"];
  const params = new URLSearchParams(location.search);
  const local = ["localhost", "127.0.0.1", "[::1]"].includes(location.hostname) && params.get("local") === "1";
  const $ = (id) => document.getElementById(id);
  let manifest, selectedId, category = "All", query = "", requestVersion = 0, toastTimer;
  let videos = [];

  function element(tag, className, text) {
    const node = document.createElement(tag);
    if (className) node.className = className;
    if (text !== undefined) node.textContent = text;
    return node;
  }
  function assetURL(path, poster = false) {
    const encoded = path.split("/").map(encodeURIComponent).join("/");
    return local ? new URL((poster ? "./" : "../") + encoded, location.href).href : REMOTE_BASE + encoded;
  }
  function announce(message) { $("playback-status").textContent = message; }
  function toast(message) {
    clearTimeout(toastTimer);
    $("toast").textContent = message;
    $("toast").hidden = false;
    toastTimer = setTimeout(() => { $("toast").hidden = true; }, 3500);
  }
  function pauseAll() {
    requestVersion++;
    videos.forEach(video => video.pause());
    announce("All videos paused.");
  }
  function filteredSamples() {
    return manifest.samples.filter(sample =>
      (category === "All" || sample.category === category) &&
      `${sample.title} ${sample.category}`.toLowerCase().includes(query)
    );
  }
  function renderList() {
    const samples = filteredSamples();
    const fragment = document.createDocumentFragment();
    for (const sample of samples) {
      const button = element("button", "sample-button");
      button.type = "button";
      button.dataset.sampleId = sample.id;
      button.setAttribute("aria-pressed", String(sample.id === selectedId));
      button.setAttribute("aria-controls", "comparison");
      const preview = sample.videos.Our || Object.values(sample.videos)[0];
      const image = element("img", "sample-thumbnail");
      image.src = assetURL(preview.poster, true);
      image.alt = "";
      image.loading = "lazy";
      image.width = 58;
      image.height = 42;
      const text = element("span", "sample-text");
      text.append(element("span", "sample-name", sample.title), element("span", "sample-subtitle", sample.category));
      button.append(image, text);
      if (sample.id === selectedId) button.append(element("span", "sample-current-dot"));
      button.addEventListener("click", () => {
        selectSample(sample.id);
        if (matchMedia("(max-width: 800px)").matches) {
          $("sample-browser").open = false;
          $("comparison").focus({ preventScroll: true });
          $("comparison").scrollIntoView({ behavior: "instant", block: "start" });
        }
      });
      fragment.append(button);
    }
    $("sample-list").replaceChildren(fragment);
    $("sample-count").textContent = String(samples.length);
    $("browse-hint").hidden = samples.length <= 5;
    $("browse-hint").textContent = `Scroll to browse all ${samples.length} samples ↓`;
    $("empty-results").hidden = samples.length > 0;
  }
  function renderCategories() {
    const fragment = document.createDocumentFragment();
    for (const name of ["All", ...new Set(manifest.samples.map(sample => sample.category))]) {
      const button = element("button", "category-button", name);
      button.type = "button";
      button.setAttribute("aria-pressed", String(name === category));
      button.addEventListener("click", () => {
        category = name;
        for (const peer of $("categories").children) peer.setAttribute("aria-pressed", String(peer === button));
        renderList();
      });
      fragment.append(button);
    }
    $("categories").replaceChildren(fragment);
  }
  function makeCard(model, sample) {
    const metadata = sample.videos[model.id];
    const label = model.id === "Our" ? "WorldGuide" : model.id === "Barnini" ? "Bernini*" : model.label;
    const card = element("article", `video-card${model.ours ? " ours" : ""}`);
    card.dataset.model = model.id;
    const heading = element("div", "card-heading");
    const title = element("h3", "", label);
    title.id = `model-${model.id}`;
    heading.append(title, element("span", "model-dot"));
    const frame = element("div", "video-frame");
    const video = element("video");
    video.controls = true;
    video.muted = true;
    video.defaultMuted = true;
    video.playsInline = true;
    video.preload = "none";
    video.poster = assetURL(metadata.poster, true);
    video.src = assetURL(metadata.path);
    video.setAttribute("aria-label", `${label}: ${sample.title}`);
    video.playbackRate = Number($("playback-speed").value);
    frame.append(video);
    const footer = element("div", "card-footer");
    const specs = element("span", "video-specs mono");
    specs.append(element("span", "", `${metadata.duration.toFixed(1)}s`), element("span", "", `${metadata.width} × ${metadata.height}`));
    const open = element("a", "", "Open video ↗");
    open.href = video.src;
    open.target = "_blank";
    open.rel = "noopener noreferrer";
    open.setAttribute("aria-label", `Open ${label} video`);
    footer.append(specs, open);
    const error = element("div", "video-error");
    error.hidden = true;
    error.setAttribute("role", "status");
    error.append(element("span", "", "Video couldn’t load."));
    const retry = element("button", "text-button", "Retry");
    retry.type = "button";
    retry.addEventListener("click", () => {
      error.hidden = true;
      video.load();
      video.play().catch(() => { if (video.isConnected) error.hidden = false; });
    });
    error.append(retry);
    video.addEventListener("error", () => { error.hidden = false; });
    video.addEventListener("playing", () => { error.hidden = true; });
    card.append(heading, frame, footer, error,
      window.WorldGuideRatings.createPanel({ sampleId: sample.id, modelId: model.id, label }));
    videos.push(video);
    return card;
  }
  function selectSample(id, updateURL = true) {
    const sample = manifest.samples.find(item => item.id === id) || manifest.samples[0];
    pauseAll();
    // Release previous requests when moving between samples.
    videos.forEach(video => { video.removeAttribute("src"); video.load(); });
    videos = [];
    selectedId = sample.id;
    $("sample-title").textContent = sample.title;
    $("sample-category").textContent = sample.category;
    $("sample-position").textContent = `${manifest.samples.indexOf(sample) + 1} / ${manifest.samples.length}`;
    document.title = `${sample.title} — WorldGuide`;
    const models = MODEL_ORDER.map(id => manifest.models.find(model => model.id === id));
    $("video-grid").replaceChildren(...models.map(model => makeCard(model, sample)));
    renderList();
    announce(`${sample.title}. ${models.length} model videos ready to play.`);
    if (updateURL) {
      const url = new URL(location.href);
      url.searchParams.set("sample", sample.id);
      history.replaceState(null, "", url);
    }
  }
  async function playAll() {
    const version = ++requestVersion;
    announce("Loading and playing all videos.");
    const results = await Promise.allSettled(videos.map(video => {
      if (video.ended) video.currentTime = 0;
      return video.play();
    }));
    if (version !== requestVersion) return;
    const failed = results.filter(result => result.status === "rejected").length;
    if (failed) toast("Some videos couldn’t start. Try their individual play controls.");
    announce(failed ? "Some videos could not start." : "All videos playing.");
  }
  function moveSample(delta) {
    const index = manifest.samples.findIndex(sample => sample.id === selectedId);
    selectSample(manifest.samples[(index + delta + manifest.samples.length) % manifest.samples.length].id);
    $("sample-list").querySelector('[aria-pressed="true"]')?.scrollIntoView({ block: "nearest" });
  }
  async function copyLink() {
    const url = new URL(location.href);
    url.searchParams.delete("local");
    url.searchParams.set("sample", selectedId);
    // A Space can be framed, but its direct URL is still a valid share link.
    try {
      await navigator.clipboard.writeText(url.href);
      toast("Comparison link copied.");
    } catch {
      window.prompt("Copy this comparison link:", url.href);
    }
  }
  function validate(data) {
    if (data.schemaVersion !== 1 || data.dataset !== DATASET || !Array.isArray(data.samples) || !data.samples.length || !Array.isArray(data.models) || data.models.length !== MODEL_ORDER.length) throw new Error("Invalid collection manifest");
    const ids = new Set();
    const modelIds = new Set(data.models.map(model => model.id));
    if (modelIds.size !== MODEL_ORDER.length || !MODEL_ORDER.every(id => modelIds.has(id))) throw new Error("Invalid model list");
    for (const sample of data.samples) {
      if (typeof sample.id !== "string" || ids.has(sample.id) || typeof sample.title !== "string" || typeof sample.category !== "string") throw new Error("Invalid sample");
      ids.add(sample.id);
      for (const model of data.models) {
        const video = sample.videos?.[model.id];
        if (!video || !Number.isFinite(video.duration) || video.duration <= 0 || !Number.isFinite(video.width) || !Number.isFinite(video.height)) throw new Error("Missing video metadata");
        for (const path of [video.path, video.poster]) {
          if (typeof path !== "string" || path.startsWith("/") || path.split("/").includes("..") || path.includes(":")) throw new Error("Invalid asset path");
        }
      }
    }
    return data;
  }
  async function fetchManifest(url) {
    const response = await fetch(url, { signal: AbortSignal.timeout(12000) });
    if (!response.ok) throw new Error(`Manifest request failed: ${response.status}`);
    return validate(await response.json());
  }
  async function init() {
    try {
      if (local) manifest = await fetchManifest("./manifest.json");
      else {
        try { manifest = await fetchManifest(REMOTE_BASE + "manifest.json"); }
        catch { manifest = await fetchManifest("./manifest.json"); }
      }
      $("sample-total").textContent = String(manifest.samples.length);
      $("model-total").textContent = String(manifest.models.length);
      $("video-total").textContent = String(manifest.samples.length * manifest.models.length);
      renderCategories();
      selectSample(params.get("sample"), false);
      $("comparison").setAttribute("aria-busy", "false");
      for (const id of ["previous", "next", "play-all", "pause-all", "restart", "copy-link"]) $(id).disabled = false;
    } catch (error) {
      console.error("WorldGuide collection failed to load:", error.message);
      $("comparison-content").hidden = true;
      $("load-error").hidden = false;
      $("comparison").setAttribute("aria-busy", "false");
    }
  }
  $("sample-browser").open = !matchMedia("(max-width: 800px)").matches;
  $("sample-search").addEventListener("input", event => { query = event.target.value.trim().toLowerCase(); if (manifest) renderList(); });
  $("clear-filters").addEventListener("click", () => { category = "All"; query = ""; $("sample-search").value = ""; renderCategories(); renderList(); $("sample-search").focus(); });
  $("previous").addEventListener("click", () => moveSample(-1));
  $("next").addEventListener("click", () => moveSample(1));
  $("play-all").addEventListener("click", playAll);
  $("pause-all").addEventListener("click", pauseAll);
  $("restart").addEventListener("click", () => { pauseAll(); videos.forEach(video => { video.currentTime = 0; }); announce("All videos reset to the beginning."); });
  $("playback-speed").addEventListener("change", event => { videos.forEach(video => { video.playbackRate = Number(event.target.value); }); });
  $("copy-link").addEventListener("click", copyLink);
  $("reload").addEventListener("click", () => location.reload());
  window.addEventListener("popstate", () => { if (manifest) selectSample(new URLSearchParams(location.search).get("sample"), false); });
  window.addEventListener("pagehide", pauseAll);
  init();
})();