Download index.html from Nfs117/CosmicApp: direct link, hf CLI and curl.
- Browser
- Download file 26.3 kB
-
https://huggingface.co/spaces/Nfs117/CosmicApp/resolve/main/index.html
- Command line
-
hf download hf://spaces/Nfs117/CosmicApp/index.html
-
curl -L -o index.html https://huggingface.co/spaces/Nfs117/CosmicApp/resolve/main/index.html
26.3 kB
| <html lang="id"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| <title>CosmicApp v1.0 β Standalone</title> | |
| <meta name="description" content="Prototype aplikasi sosial media client-side dengan penyimpanan lokal, like, komentar, pencarian, hashtag, chat global, dan tema gelap/terang." /> | |
| <style> | |
| :root{ | |
| --bg:#000; /* dark */ | |
| --panel:#0a0a0a; | |
| --text:#fff; | |
| --muted:rgba(255,255,255,.12); | |
| --subtle:rgba(255,255,255,.6); | |
| --brand1:#0015ff; | |
| --brand2:#00ffff; | |
| --accent:linear-gradient(135deg,var(--brand1),var(--brand2)); | |
| --glass:linear-gradient(180deg, rgba(255,255,255,0.06), rgba(255,255,255,0.02)); | |
| --shadow: 0 10px 30px rgba(0,0,0,.35); | |
| font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial; | |
| } | |
| .light{ | |
| --bg:#f6f7fb; | |
| --panel:#ffffff; | |
| --text:#0b1020; | |
| --muted:rgba(13,23,55,.12); | |
| --subtle:rgba(13,23,55,.7); | |
| --shadow: 0 10px 24px rgba(28,34,66,.12); | |
| } | |
| *{box-sizing:border-box} | |
| html,body{height:100%;margin:0;background:var(--bg);color:var(--text)} | |
| img,video{max-width:100%;height:auto;border-radius:12px} | |
| a{color:var(--brand2);text-decoration:none} | |
| /* Layout */ | |
| .app{display:grid;grid-template-columns:88px 1fr 340px;gap:16px;padding:16px;min-height:100vh} | |
| @media (max-width:1024px){.app{grid-template-columns:72px 1fr 320px}} | |
| @media (max-width:900px){.app{grid-template-columns:1fr}} | |
| .panel{background:var(--panel);border-radius:16px;border:1px solid var(--muted);box-shadow:var(--shadow)} | |
| /* Sidebar */ | |
| .sidebar{padding:12px;display:flex;flex-direction:column;gap:12px;align-items:center} | |
| .logo{font-weight:800;font-size:14px;letter-spacing:0.6px} | |
| .nav-btn{width:100%;display:flex;align-items:center;justify-content:center;gap:8px;padding:10px;border-radius:12px;border:1px solid transparent;cursor:pointer} | |
| .nav-btn:hover{background:rgba(255,255,255,0.05)} | |
| .icon-circle{width:48px;height:48px;border-radius:12px;background:var(--accent);display:flex;align-items:center;justify-content:center;font-weight:800;color:#000} | |
| .sep{height:1px;width:100%;background:var(--muted);margin:4px 0} | |
| /* Main */ | |
| .main{display:flex;flex-direction:column;gap:16px} | |
| .toolbar{display:flex;gap:8px;align-items:center} | |
| .search{flex:1;display:flex;gap:8px} | |
| .input{flex:1;background:transparent;border:1px solid var(--muted);padding:10px 12px;border-radius:12px;color:var(--text)} | |
| .btn{background:linear-gradient(90deg,var(--brand1),var(--brand2));color:#fff;border:none;padding:10px 12px;border-radius:12px;font-weight:700;cursor:pointer} | |
| .btn.ghost{background:transparent;border:1px solid var(--muted);color:var(--text)} | |
| /* Composer */ | |
| .composer{padding:12px;display:flex;flex-direction:column;gap:10px} | |
| textarea{resize:none;padding:12px;border-radius:12px;border:1px solid var(--muted);background:transparent;color:var(--text);min-height:84px} | |
| .composer-foot{display:flex;gap:8px;align-items:center;flex-wrap:wrap} | |
| .avatar{width:56px;height:56px;border-radius:12px;flex-shrink:0;background:var(--accent);display:flex;align-items:center;justify-content:center;font-weight:800;color:#000} | |
| .composer-row{display:flex;gap:12px;align-items:flex-start} | |
| .chips{display:flex;gap:8px;flex-wrap:wrap} | |
| .pill{padding:6px 10px;border-radius:999px;border:1px solid var(--muted);font-size:13px;color:var(--subtle)} | |
| .dropzone{border:1px dashed var(--muted);border-radius:12px;padding:10px;display:flex;gap:10px;align-items:center;justify-content:center} | |
| .media-preview{display:flex;gap:8px;flex-wrap:wrap} | |
| .media-preview > *{max-width:120px;max-height:96px;border-radius:10px} | |
| /* Feed */ | |
| .feed{display:flex;flex-direction:column;gap:12px} | |
| .post{padding:12px;display:flex;gap:12px} | |
| .post-body{flex:1;display:flex;flex-direction:column;gap:8px} | |
| .meta{display:flex;gap:8px;align-items:center;font-size:13px;color:var(--subtle)} | |
| .post-actions{display:flex;gap:8px;align-items:center;flex-wrap:wrap} | |
| .small-btn{padding:8px 10px;border-radius:12px;border:1px solid var(--muted);background:transparent;color:var(--text);font-weight:700;cursor:pointer} | |
| .tag{display:inline-block;padding:2px 6px;border-radius:6px;border:1px solid var(--muted);font-size:12px;margin-right:6px;cursor:pointer} | |
| .post-media{max-height:480px;overflow:hidden;border-radius:12px} | |
| .comment{background:rgba(255,255,255,.05);padding:8px;border-radius:10px} | |
| .comments{display:flex;flex-direction:column;gap:8px} | |
| /* Right */ | |
| .right{padding:12px;display:flex;flex-direction:column;gap:12px} | |
| .row{display:flex;gap:8px;align-items:center} | |
| .kv{display:flex;justify-content:space-between;align-items:center} | |
| /* Chat */ | |
| .chat{display:flex;flex-direction:column;height:360px;border-radius:12px;overflow:hidden} | |
| .chat-messages{flex:1;padding:8px;overflow:auto;display:flex;flex-direction:column;gap:8px;background:rgba(0,0,0,.06)} | |
| .chat-input{display:flex;gap:8px;padding:8px;border-top:1px solid var(--muted)} | |
| /* Utilities */ | |
| .muted{color:var(--subtle);font-size:13px} | |
| .hidden{display:none} | |
| </style> | |
| </head> | |
| <body> | |
| <div class="app"> | |
| <!-- SIDEBAR --> | |
| <aside class="sidebar panel" aria-label="Sidebar"> | |
| <div class="logo">CosmicApp</div> | |
| <div class="nav-btn" id="homeBtn" title="Beranda"><div class="icon-circle">CA</div></div> | |
| <button class="nav-btn" id="exploreBtn" title="Explore">π</button> | |
| <button class="nav-btn" id="notifBtn" title="Notifikasi">π</button> | |
| <div class="sep"></div> | |
| <button class="nav-btn" id="themeBtn" title="Ganti Tema">π</button> | |
| <div class="sep"></div> | |
| <div class="pill muted">Tema: <span id="themeLabel">Gelap</span></div> | |
| </aside> | |
| <!-- MAIN --> | |
| <main class="main"> | |
| <!-- Toolbar --> | |
| <div class="toolbar"> | |
| <div class="search"> | |
| <input id="searchInput" class="input" placeholder="Cari postingan, #hashtag, atau @user" /> | |
| <button id="clearSearch" class="btn ghost" aria-label="Bersihkan pencarian">Clear</button> | |
| </div> | |
| <button id="newPostBtn" class="btn">Posting</button> | |
| </div> | |
| <!-- Composer --> | |
| <section class="panel composer" aria-label="Buat postingan"> | |
| <div class="composer-row"> | |
| <div class="avatar" id="composerAvatar">UA</div> | |
| <textarea id="postText" placeholder="Apa yang sedang terjadi? (teks, #hashtag, @mention)" maxlength="2000"></textarea> | |
| </div> | |
| <div class="dropzone" id="dropzone">Tarik & letakkan media di sini atau | |
| <label class="small-btn" for="mediaInput">Pilih Media</label> | |
| <input id="mediaInput" type="file" accept="image/*,video/*,audio/*" multiple hidden /> | |
| </div> | |
| <div id="mediaPreview" class="media-preview"></div> | |
| <div class="composer-foot"> | |
| <div class="chips"> | |
| <span class="pill">Maks 4 media</span> | |
| <span class="pill">Gambar diperkecil otomatis</span> | |
| <span class="pill">Video/Audio tidak tersimpan permanen (demo)</span> | |
| </div> | |
| <div style="flex:1"></div> | |
| <button id="postBtn" class="btn">Kirim</button> | |
| </div> | |
| </section> | |
| <!-- Feed --> | |
| <section class="feed" id="feed" aria-live="polite"></section> | |
| </main> | |
| <!-- RIGHT COLUMN --> | |
| <aside class="right panel"> | |
| <div class="kv"><strong>Profil</strong><button class="small-btn" id="resetProfileBtn">Reset</button></div> | |
| <div class="row"> | |
| <div class="avatar" id="profileAvatar">UA</div> | |
| <div style="flex:1;display:flex;flex-direction:column;gap:6px"> | |
| <input class="input" id="displayName" placeholder="Nama (max 30)" maxlength="30" /> | |
| <input class="input" id="avatarInitials" placeholder="Inisial (max 3)" maxlength="3" /> | |
| </div> | |
| </div> | |
| <div class="row"> | |
| <button class="btn" id="saveProfileBtn">Simpan</button> | |
| <button class="small-btn" id="newIdBtn" title="Buat ID lokal baru">Ganti ID</button> | |
| </div> | |
| <div> | |
| <div class="kv"><strong>Chat Global</strong><span class="muted" id="chatCount">0</span></div> | |
| <div class="chat panel" style="border:none"> | |
| <div class="chat-messages" id="chatMessages"></div> | |
| <div class="chat-input"> | |
| <input class="input" id="chatInput" placeholder="Ketik pesan..." /> | |
| <button class="btn" id="chatSend">Kirim</button> | |
| </div> | |
| </div> | |
| </div> | |
| <div> | |
| <div class="kv"><strong>Aktivitas</strong><button class="small-btn" id="clearActivity">Bersihkan</button></div> | |
| <div id="activityLog" class="muted">Tidak ada aktivitas</div> | |
| </div> | |
| <div> | |
| <div class="kv"><strong>Notifikasi</strong><button class="small-btn" id="clearNotif">Bersihkan</button></div> | |
| <div id="notifList" class="muted">Belum ada notifikasi</div> | |
| </div> | |
| </aside> | |
| </div> | |
| <!-- Templates --> | |
| <template id="postTemplate"> | |
| <article class="panel post"> | |
| <div class="avatar post-avatar">XX</div> | |
| <div class="post-body"> | |
| <div class="meta"><strong class="post-name">Nama</strong><span class="muted">β’</span><span class="muted post-time">Baru saja</span></div> | |
| <div class="post-text">Teks</div> | |
| <div class="post-tags"></div> | |
| <div class="post-media"></div> | |
| <div class="post-actions"> | |
| <button class="small-btn likeBtn">π <span class="likeCount">0</span></button> | |
| <button class="small-btn replyBtn">π¬</button> | |
| <button class="small-btn editPostBtn">βοΈ</button> | |
| <button class="small-btn deletePostBtn">ποΈ</button> | |
| </div> | |
| <div class="comments"></div> | |
| <div class="add-comment" style="display:flex;gap:8px;margin-top:8px"> | |
| <input class="input commentInput" placeholder="Tulis komentar..." /> | |
| <button class="small-btn postCommentBtn">Kirim</button> | |
| </div> | |
| </div> | |
| </article> | |
| </template> | |
| <script> | |
| // ===================== | |
| // UTIL & STORAGE LAYER | |
| // ===================== | |
| const store = { | |
| get(k, def){ try{ return JSON.parse(localStorage.getItem(k)) ?? def }catch{ return def } }, | |
| set(k, v){ localStorage.setItem(k, JSON.stringify(v)) }, | |
| del(k){ localStorage.removeItem(k) } | |
| } | |
| const uid = () => Math.random().toString(36).slice(2)+Date.now().toString(36); | |
| const nowText = () => new Date().toLocaleString(); | |
| const el = sel => document.querySelector(sel); | |
| function escapeHTML(str){ return str.replace(/[&<>"']/g, m=>({"&":"&","<":"<", | |
| ">":">","\"":""","'":"'"}[m])) } | |
| // very light bad-words filter (demo only) | |
| const BAD_WORDS = [/\b(kontol|anjing|goblok|tolol)\b/gi]; | |
| function filterBadWords(text){ let t=text; BAD_WORDS.forEach(rx=>{ t=t.replace(rx, m=>"*".repeat(m.length)) }); return t } | |
| // ============== | |
| // APP STATE | |
| // ============== | |
| const STATE = { | |
| user: store.get('cosmic.user', { id: uid(), name:'User Astro', initials:'UA' }), | |
| theme: store.get('cosmic.theme', 'dark'), | |
| posts: store.get('cosmic.posts', []), | |
| chat: store.get('cosmic.chat', []), | |
| notif: store.get('cosmic.notif', []) | |
| } | |
| // ===================== | |
| // THEME | |
| // ===================== | |
| function applyTheme(){ | |
| if(STATE.theme==='light') document.body.classList.add('light'); else document.body.classList.remove('light'); | |
| el('#themeLabel').textContent = STATE.theme==='light' ? 'Terang' : 'Gelap'; | |
| store.set('cosmic.theme', STATE.theme); | |
| } | |
| // ===================== | |
| // PROFILE | |
| // ===================== | |
| function applyProfileUI(){ | |
| el('#profileAvatar').textContent = STATE.user.initials; | |
| el('#composerAvatar').textContent = STATE.user.initials; | |
| el('#displayName').value = STATE.user.name; | |
| el('#avatarInitials').value = STATE.user.initials; | |
| } | |
| function saveProfile(){ | |
| STATE.user.name = (el('#displayName').value || 'User Astro').slice(0,30); | |
| STATE.user.initials = (el('#avatarInitials').value || 'UA').toUpperCase().slice(0,3); | |
| store.set('cosmic.user', STATE.user); | |
| applyProfileUI(); | |
| setActivity('Profil diperbarui β '+nowText()); | |
| renderAll(); | |
| } | |
| // ===================== | |
| // POSTS | |
| // ===================== | |
| const MAX_MEDIA = 4; | |
| function parseTagsAndMentions(text){ | |
| const tags = Array.from(text.matchAll(/#([a-z0-9_]{1,32})/gi)).map(m=>m[1].toLowerCase()); | |
| const mentions = Array.from(text.matchAll(/@([a-z0-9_]{1,32})/gi)).map(m=>m[1]); | |
| return {tags:[...new Set(tags)], mentions:[...new Set(mentions)]}; | |
| } | |
| function fileToImageDataURL(file, maxDim=1280){ | |
| return new Promise((resolve)=>{ | |
| const fr = new FileReader(); | |
| fr.onload = () => { | |
| const img = new Image(); | |
| img.onload = () => { | |
| const canvas = document.createElement('canvas'); | |
| let {width, height} = img; | |
| const scale = Math.min(1, maxDim/Math.max(width,height)); | |
| width = Math.round(width*scale); height=Math.round(height*scale); | |
| canvas.width = width; canvas.height = height; | |
| const ctx = canvas.getContext('2d'); | |
| ctx.drawImage(img,0,0,width,height); | |
| resolve(canvas.toDataURL('image/jpeg', .85)); | |
| }; | |
| img.src = fr.result; | |
| }; | |
| fr.readAsDataURL(file); | |
| }); | |
| } | |
| async function collectMedia(files){ | |
| const out=[]; const fs = Array.from(files).slice(0,MAX_MEDIA); | |
| for(const f of fs){ | |
| if(f.type.startsWith('image/')){ | |
| const data = await fileToImageDataURL(f); | |
| out.push({kind:'image', data, name:f.name}); | |
| } else if(f.type.startsWith('video/')){ | |
| // for demo, keep only name+type; will not persist across reload | |
| const url = URL.createObjectURL(f); | |
| out.push({kind:'video', url, name:f.name}); | |
| } else if(f.type.startsWith('audio/')){ | |
| const url = URL.createObjectURL(f); | |
| out.push({kind:'audio', url, name:f.name}); | |
| } | |
| } | |
| return out; | |
| } | |
| function persistPosts(){ store.set('cosmic.posts', STATE.posts) } | |
| function addPost(text, media){ | |
| const safeText = filterBadWords(text).slice(0,2000); | |
| const {tags, mentions} = parseTagsAndMentions(safeText); | |
| const post = { | |
| id: uid(), | |
| authorId: STATE.user.id, | |
| name: STATE.user.name, | |
| initials: STATE.user.initials, | |
| time: nowText(), | |
| text: safeText, | |
| tags, mentions, | |
| media, // images persisted; video/audio session-only | |
| likes: [], | |
| comments: [] | |
| }; | |
| STATE.posts.unshift(post); | |
| persistPosts(); | |
| setActivity('Postingan dikirim β '+nowText()); | |
| notifyMentions(post); | |
| renderAll(); | |
| } | |
| function updatePost(id, newText){ | |
| const p = STATE.posts.find(p=>p.id===id); if(!p) return; | |
| const safeText = filterBadWords(newText).slice(0,2000); | |
| const {tags, mentions} = parseTagsAndMentions(safeText); | |
| p.text=safeText; p.tags=tags; p.mentions=mentions; p.time=nowText(); | |
| persistPosts(); | |
| setActivity('Postingan diedit β '+nowText()); | |
| renderAll(); | |
| } | |
| function deletePost(id){ | |
| STATE.posts = STATE.posts.filter(p=>p.id!==id); | |
| persistPosts(); | |
| setActivity('Postingan dihapus β '+nowText()); | |
| renderAll(); | |
| } | |
| function toggleLike(post){ | |
| const i = post.likes.indexOf(STATE.user.id); | |
| if(i===-1) post.likes.push(STATE.user.id); else post.likes.splice(i,1); | |
| persistPosts(); | |
| } | |
| function addComment(post, text){ | |
| const safe = filterBadWords(text).slice(0,1000); | |
| post.comments.push({ id: uid(), authorId: STATE.user.id, author: STATE.user.name, text: safe, time: nowText() }); | |
| persistPosts(); | |
| if(post.authorId!==STATE.user.id){ | |
| STATE.notif.unshift({ id: uid(), time: nowText(), text: `${STATE.user.name} mengomentari postinganmu: "${safe.slice(0,60)}"`}); | |
| store.set('cosmic.notif', STATE.notif); | |
| renderNotif(); | |
| } | |
| } | |
| // ============== | |
| // NOTIFICATIONS | |
| // ============== | |
| function notifyMentions(post){ | |
| if(post.mentions.length){ | |
| STATE.notif.unshift({ id: uid(), time: nowText(), text: `${post.name} menyebut ${post.mentions.map(m=>"@"+m).join(', ')} di sebuah postingan.` }); | |
| store.set('cosmic.notif', STATE.notif); | |
| } | |
| } | |
| function renderNotif(){ | |
| const list = el('#notifList'); | |
| if(!STATE.notif.length){ list.textContent='Belum ada notifikasi'; return } | |
| list.innerHTML=''; | |
| STATE.notif.slice(0,30).forEach(n=>{ | |
| const div=document.createElement('div'); | |
| div.className='comment'; | |
| div.textContent = `${n.time} β ${n.text}`; | |
| list.appendChild(div); | |
| }) | |
| } | |
| // ============== | |
| // RENDER FEED | |
| // ============== | |
| const feedEl = el('#feed'); | |
| function renderFeed(){ | |
| const q = el('#searchInput').value.trim().toLowerCase(); | |
| let filtered = STATE.posts; | |
| if(q){ | |
| if(q.startsWith('#')){ | |
| const tag = q.slice(1); | |
| filtered = filtered.filter(p=>p.tags.includes(tag)); | |
| } else if(q.startsWith('@')){ | |
| const who = q.slice(1); | |
| filtered = filtered.filter(p=>p.mentions.includes(who) || p.name.toLowerCase().includes(who)); | |
| } else { | |
| filtered = filtered.filter(p=>p.text.toLowerCase().includes(q) || p.name.toLowerCase().includes(q)); | |
| } | |
| } | |
| feedEl.innerHTML=''; | |
| if(filtered.length===0){ | |
| const empty = document.createElement('div'); | |
| empty.className='muted'; | |
| empty.textContent = 'Belum ada postingan. Buat postingan pertama!'; | |
| feedEl.appendChild(empty); | |
| return; | |
| } | |
| const tpl = el('#postTemplate'); | |
| filtered.forEach(post=>{ | |
| const node = tpl.content.cloneNode(true); | |
| const art = node.querySelector('.post'); | |
| art.dataset.id = post.id; | |
| node.querySelector('.post-avatar').textContent = post.initials; | |
| node.querySelector('.post-name').textContent = post.name; | |
| node.querySelector('.post-time').textContent = post.time; | |
| // text with simple linkify for tags/mentions (safe) | |
| const ptext = node.querySelector('.post-text'); | |
| ptext.innerHTML = escapeHTML(post.text) | |
| .replace(/#([a-z0-9_]{1,32})/gi, '<span class="tag" data-tag="$1">#$1</span>') | |
| .replace(/@([a-z0-9_]{1,32})/gi, '<span class="tag" data-mention="$1">@$1</span>'); | |
| // tags list (clickable) | |
| const tagWrap = node.querySelector('.post-tags'); | |
| post.tags.forEach(t=>{ const s=document.createElement('span'); s.className='tag'; s.textContent = '#'+t; s.addEventListener('click', ()=>{el('#searchInput').value='#'+t; renderFeed()}); tagWrap.appendChild(s) }); | |
| // media | |
| const mediaWrap = node.querySelector('.post-media'); | |
| post.media.forEach(m=>{ | |
| let elx; | |
| if(m.kind==='image'){ elx = document.createElement('img'); elx.src = m.data } | |
| else if(m.kind==='video'){ elx = document.createElement('video'); elx.src=m.url; elx.controls=true } | |
| else { elx = document.createElement('audio'); elx.src=m.url; elx.controls=true } | |
| mediaWrap.appendChild(elx); | |
| }) | |
| // likes | |
| const likeBtn = node.querySelector('.likeBtn'); | |
| const likeCount = node.querySelector('.likeCount'); | |
| likeCount.textContent = post.likes.length; | |
| const refreshLikeState = ()=>{ | |
| const liked = post.likes.includes(STATE.user.id); | |
| likeBtn.style.borderColor = liked? 'transparent' : 'var(--muted)'; | |
| likeBtn.style.background = liked? 'var(--accent)' : 'transparent'; | |
| likeBtn.style.color = liked? '#000' : 'var(--text)'; | |
| } | |
| refreshLikeState(); | |
| likeBtn.addEventListener('click', ()=>{ toggleLike(post); likeCount.textContent = post.likes.length; refreshLikeState(); }) | |
| // comments | |
| const commentsWrap = node.querySelector('.comments'); | |
| post.comments.forEach(c=>{ | |
| const cdiv=document.createElement('div'); cdiv.className='comment'; | |
| cdiv.textContent = `${c.author}: ${c.text}`; | |
| commentsWrap.appendChild(cdiv); | |
| }) | |
| // actions | |
| node.querySelector('.replyBtn').addEventListener('click', ()=>{ art.querySelector('.commentInput').focus() }) | |
| node.querySelector('.postCommentBtn').addEventListener('click', ()=>{ | |
| const input = art.querySelector('.commentInput'); | |
| if(!input.value.trim()) return; | |
| addComment(post, input.value.trim()); | |
| input.value=''; | |
| renderFeed(); | |
| setActivity('Komentar ditambahkan β '+nowText()); | |
| }) | |
| node.querySelector('.deletePostBtn').addEventListener('click', ()=>{ | |
| if(post.authorId!==STATE.user.id){ alert('Hanya pembuat postingan yang bisa menghapus.'); return } | |
| if(confirm('Hapus postingan?')) deletePost(post.id); | |
| }) | |
| node.querySelector('.editPostBtn').addEventListener('click', ()=>{ | |
| if(post.authorId!==STATE.user.id){ alert('Hanya pembuat postingan yang bisa mengedit.'); return } | |
| const nt = prompt('Edit postingan:', post.text); | |
| if(nt!==null) updatePost(post.id, nt); | |
| }) | |
| // clickable tag/mention inside text | |
| ptext.querySelectorAll('[data-tag]').forEach(s=> s.addEventListener('click', ()=>{ el('#searchInput').value = '#'+s.dataset.tag; renderFeed() })) | |
| ptext.querySelectorAll('[data-mention]').forEach(s=> s.addEventListener('click', ()=>{ el('#searchInput').value = '@'+s.dataset.mention; renderFeed() })) | |
| feedEl.appendChild(node); | |
| }) | |
| } | |
| // ============== | |
| // COMPOSER & MEDIA | |
| // ============== | |
| const mediaPreview = el('#mediaPreview'); | |
| const mediaInput = el('#mediaInput'); | |
| const dropzone = el('#dropzone'); | |
| let pendingFiles = []; | |
| function showPreview(items){ | |
| mediaPreview.innerHTML=''; | |
| items.forEach(m=>{ | |
| let v; | |
| if(m.kind==='image'){ v=document.createElement('img'); v.src=m.data } | |
| else if(m.kind==='video'){ v=document.createElement('video'); v.src=m.url; v.controls=true } | |
| else { v=document.createElement('audio'); v.src=m.url; v.controls=true } | |
| mediaPreview.appendChild(v); | |
| }) | |
| } | |
| mediaInput.addEventListener('change', async ()=>{ | |
| const media = await collectMedia(mediaInput.files); | |
| pendingFiles = media.slice(0,MAX_MEDIA); | |
| showPreview(pendingFiles); | |
| }) | |
| ;['dragenter','dragover'].forEach(ev=> dropzone.addEventListener(ev, (e)=>{ e.preventDefault(); dropzone.style.background='rgba(255,255,255,.06)' })) | |
| ;['dragleave','drop'].forEach(ev=> dropzone.addEventListener(ev, (e)=>{ e.preventDefault(); dropzone.style.background='transparent' })) | |
| dropzone.addEventListener('drop', async (e)=>{ | |
| const dt = e.dataTransfer; if(!dt) return; | |
| const media = await collectMedia(dt.files); | |
| pendingFiles = media.slice(0,MAX_MEDIA); | |
| showPreview(pendingFiles); | |
| }) | |
| el('#postBtn').addEventListener('click', ()=>{ | |
| const text = el('#postText').value.trim(); | |
| if(!text && pendingFiles.length===0){ alert('Isi teks atau tambahkan media.'); return } | |
| addPost(text, pendingFiles); | |
| el('#postText').value=''; pendingFiles=[]; showPreview([]); mediaInput.value=''; | |
| }) | |
| // new-post shortcut | |
| el('#newPostBtn').addEventListener('click', ()=> el('#postText').focus()); | |
| // ============== | |
| // SEARCH | |
| // ============== | |
| el('#searchInput').addEventListener('input', renderFeed); | |
| el('#clearSearch').addEventListener('click', ()=>{ el('#searchInput').value=''; renderFeed() }) | |
| // ============== | |
| // CHAT | |
| // ============== | |
| const chatMessages = el('#chatMessages'); | |
| const chatInput = el('#chatInput'); | |
| function renderChat(){ | |
| chatMessages.innerHTML=''; | |
| STATE.chat.slice(-100).forEach(m=>{ | |
| const div=document.createElement('div'); div.className='comment'; | |
| div.textContent = `${m.time} β ${m.name}: ${m.text}`; | |
| chatMessages.appendChild(div); | |
| }); | |
| el('#chatCount').textContent = STATE.chat.length; | |
| chatMessages.scrollTop = chatMessages.scrollHeight; | |
| } | |
| function sendChat(){ | |
| const t = chatInput.value.trim(); if(!t) return; | |
| const safe = filterBadWords(t).slice(0,500); | |
| STATE.chat.push({ id: uid(), user: STATE.user.id, name: STATE.user.name, text: safe, time: nowText() }); | |
| store.set('cosmic.chat', STATE.chat); | |
| chatInput.value=''; | |
| renderChat(); | |
| setActivity('Pesan chat dikirim β '+nowText()); | |
| } | |
| el('#chatSend').addEventListener('click', sendChat); | |
| chatInput.addEventListener('keydown', e=>{ if(e.key==='Enter'){ e.preventDefault(); sendChat() } }) | |
| // ============== | |
| // NOTIF UI & ACTIVITY | |
| // ============== | |
| function setActivity(text){ el('#activityLog').textContent = text } | |
| el('#clearActivity').addEventListener('click', ()=> el('#activityLog').textContent='Tidak ada aktivitas') | |
| el('#clearNotif').addEventListener('click', ()=>{ STATE.notif=[]; store.set('cosmic.notif', STATE.notif); renderNotif(); }) | |
| // ============== | |
| // THEME & NAV | |
| // ============== | |
| el('#themeBtn').addEventListener('click', ()=>{ STATE.theme = STATE.theme==='light' ? 'dark' : 'light'; applyTheme() }) | |
| el('#homeBtn').addEventListener('click', ()=>{ el('#searchInput').value=''; renderFeed() }) | |
| el('#notifBtn').addEventListener('click', ()=>{ el('#notifList').scrollIntoView({behavior:'smooth'}) }) | |
| el('#exploreBtn').addEventListener('click', ()=>{ el('#searchInput').value='#'; el('#searchInput').focus(); renderFeed() }) | |
| // ============== | |
| // PROFILE UI | |
| // ============== | |
| el('#saveProfileBtn').addEventListener('click', saveProfile) | |
| el('#resetProfileBtn').addEventListener('click', ()=>{ STATE.user={ id:STATE.user.id, name:'User Astro', initials:'UA' }; store.set('cosmic.user', STATE.user); applyProfileUI(); renderAll(); setActivity('Profil direset β '+nowText()) }) | |
| el('#newIdBtn').addEventListener('click', ()=>{ if(confirm('Buat ID lokal baru? Data postingan lama tetap tersimpan, namun like/kepemilikan mengikuti ID baru.')){ STATE.user.id=uid(); store.set('cosmic.user', STATE.user); setActivity('ID diganti β '+nowText()) }}) | |
| // ============== | |
| // INIT | |
| // ============== | |
| function renderAll(){ applyProfileUI(); renderFeed(); renderChat(); renderNotif(); } | |
| (function init(){ applyTheme(); applyProfileUI(); renderAll(); setActivity('Siap β '+nowText()) })(); | |
| </script> | |
| </body> | |
| </html> |