Skip to content

Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.

5.2 KB
Raw Blame
(() => {
  'use strict';
  const $ = (s, r = document) => r.querySelector(s);
  const $$ = (s, r = document) => [...r.querySelectorAll(s)];
  const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
  const canHover = matchMedia('(hover: hover) and (pointer: fine)').matches;
  let data = {};
  try { data = JSON.parse($('#data').textContent || '{}'); } catch { data = {}; }

  // ---------- hero ----------
  const hero = $('#hero-video');
  if (hero && reduce) { hero.removeAttribute('autoplay'); hero.pause(); hero.controls = true; }

  // ---------- video thẻ: chỉ gắn src khi gần màn hình, chỉ phát khi rê/chạm ----------
  const cards = $$('.card-media');
  let current = null;
  const attach = (box) => {
    const v = $('video', box);
    if (!v.getAttribute('src') && v.dataset.src) v.setAttribute('src', v.dataset.src);
    return v;
  };
  const stop = (box) => {
    if (!box) return;
    const v = $('video', box);
    v.pause();
    box.classList.remove('is-playing');
    $('.play', box).setAttribute('aria-label', 'Phát mẫu #' + box.dataset.id);
    if (current === box) current = null;
  };
  const start = (box) => {
    if (current && current !== box) stop(current);
    const v = attach(box);
    current = box;
    const p = v.play();
    box.classList.add('is-playing');
    $('.play', box).setAttribute('aria-label', 'Dừng mẫu #' + box.dataset.id);
    if (p && p.catch) p.catch(() => stop(box));
  };
  if ('IntersectionObserver' in window) {
    const io = new IntersectionObserver((entries) => {
      for (const e of entries) {
        if (e.isIntersecting) attach(e.target);
        else if (e.target === current) stop(e.target);
      }
    }, { rootMargin: '200px 0px' });
    cards.forEach((c) => io.observe(c));
  }
  for (const box of cards) {
    if (canHover) {
      box.addEventListener('pointerenter', () => start(box));
      box.addEventListener('pointerleave', () => stop(box));
    }
    box.addEventListener('click', (ev) => {
      if (canHover && !ev.target.closest('.play')) { openSheet(box.dataset.id); return; }
      box.classList.contains('is-playing') ? stop(box) : start(box);
    });
  }

  // ---------- lọc ----------
  const state = { group: 'all', tech: 'all' };
  const items = $$('.card');
  const empty = $('#empty');
  const apply = () => {
    let shown = 0;
    for (const li of items) {
      const ok = (state.group === 'all' || li.dataset.group === state.group)
        && (state.tech === 'all' || li.dataset.tech.split(' ').includes(state.tech));
      li.hidden = !ok;
      if (ok) shown++;
      else stop($('.card-media', li));
    }
    empty.hidden = shown > 0;
  };
  for (const group of $$('.chips')) {
    group.addEventListener('click', (ev) => {
      const b = ev.target.closest('.chip');
      if (!b) return;
      $$('.chip', group).forEach((c) => c.setAttribute('aria-pressed', String(c === b)));
      state[group.dataset.filter] = b.dataset.value;
      apply();
    });
  }
  $('#reset')?.addEventListener('click', () => {
    for (const group of $$('.chips')) {
      $$('.chip', group).forEach((c) => c.setAttribute('aria-pressed', String(c.dataset.value === 'all')));
      state[group.dataset.filter] = 'all';
    }
    apply();
  });

  // ---------- hộp chi tiết ----------
  const sheet = $('#sheet');
  const sv = $('#sheet-video');
  function openSheet(id) {
    const d = data[id];
    if (!d || !sheet || typeof sheet.showModal !== 'function') return;
    if (current) stop(current);
    $('#sheet-id').textContent = 'Mẫu #' + id;
    $('#sheet-title').textContent = d.name;
    $('#sheet-tags').replaceChildren(...d.tags.map((t) => Object.assign(document.createElement('span'), { textContent: t })));
    $('#sheet-script').textContent = d.script || '(Chưa có kịch bản)';
    const v = d.v ? '?v=' + d.v : '';
    sv.poster = 'media/' + id + '.webp' + v;
    sv.src = 'media/' + id + '.mp4' + v;
    sheet.showModal();
    if (!reduce) sv.play().catch(() => {});
  }
  const closeSheet = () => { sv.pause(); sv.removeAttribute('src'); sv.load(); };
  $$('.card-more').forEach((b) => b.addEventListener('click', () => openSheet(b.dataset.id)));
  $('#sheet-close')?.addEventListener('click', () => sheet.close());
  sheet?.addEventListener('close', closeSheet);
  sheet?.addEventListener('click', (ev) => { if (ev.target === sheet) sheet.close(); });

  // ---------- tab ----------
  for (const tabs of $$('[data-tabs]')) {
    const btns = $$('[role="tab"]', tabs);
    const select = (b, focus) => {
      for (const x of btns) {
        const on = x === b;
        x.setAttribute('aria-selected', String(on));
        x.tabIndex = on ? 0 : -1;
        document.getElementById(x.getAttribute('aria-controls')).hidden = !on;
      }
      if (focus) b.focus();
    };
    btns.forEach((b, i) => {
      b.addEventListener('click', () => select(b));
      b.addEventListener('keydown', (ev) => {
        const k = ev.key;
        if (k !== 'ArrowRight' && k !== 'ArrowLeft' && k !== 'Home' && k !== 'End') return;
        ev.preventDefault();
        const n = k === 'Home' ? 0 : k === 'End' ? btns.length - 1 : (i + (k === 'ArrowRight' ? 1 : -1) + btns.length) % btns.length;
        select(btns[n], true);
      });
    });
  }
})();