Wiki source code of Uncategorized Videos
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{velocity}} | ||
2 | #pagePicker_import ## load the native Page Picker resources once | ||
3 | |||
4 | ## 1) Collect video attachments from the current page | ||
5 | #set($videoExts = ['mp4','webm','ogg','avi','mov','wmv','flv','m4v']) | ||
6 | #set($videos = []) | ||
7 | #foreach($att in $doc.getAttachmentList()) | ||
8 | #set($n = $att.getFilename()) | ||
9 | #set($ln = $n.toLowerCase()) | ||
10 | #foreach($e in $videoExts) | ||
11 | #if($ln.endsWith("." + $e)) | ||
12 | #set($discard = $videos.add($att)) | ||
13 | #break | ||
14 | #end | ||
15 | #end | ||
16 | #end | ||
17 | |||
18 | ## 2) Cache the rendered HTML (auto-bust on each save via version) | ||
19 | {{cache id="vid-list-$doc.fullName-$doc.version" timeToLive="21600"}} | ||
20 | {{html wiki="false" clean="false"}} | ||
21 | <div id="xwiki-video-manager" style="margin:20px 0;"> | ||
22 | <h2>📹 Videos on: ${escapetool.xml($doc.fullName)}</h2> | ||
23 | |||
24 | #if($videos.size() == 0) | ||
25 | <div style="text-align:center;padding:40px;background:#f8f9fa;border-radius:8px;"> | ||
26 | <h3>No Videos Found</h3> | ||
27 | <p>Attach video files to this page to see them here.</p> | ||
28 | </div> | ||
29 | #else | ||
30 | <script> | ||
31 | window.VID_CHUNK_SIZE = 48; | ||
32 | window.VID_LAZY_MARGIN = '600px'; | ||
33 | window.XWIKI_WIKI = ${jsontool.serialize($xcontext.database)}; // e.g., "xwiki" | ||
34 | window.SOURCE_SPACE = ${jsontool.serialize($doc.space)}; // e.g., "Main" or "Main.Sub" | ||
35 | window.SOURCE_PAGE = ${jsontool.serialize($doc.name)}; // e.g., "WebHome" | ||
36 | </script> | ||
37 | |||
38 | <div id="video-chunks"> | ||
39 | #set($i = 0) | ||
40 | #set($chunkIndex = 0) | ||
41 | #foreach($att in $videos) | ||
42 | #set($i = $i + 1) | ||
43 | #set($filename = $att.getFilename()) | ||
44 | #set($lname = $filename.toLowerCase()) | ||
45 | #set($url = $doc.getAttachmentURL($filename)) | ||
46 | |||
47 | ## MIME guess | ||
48 | #set($videoType = "video/mp4") | ||
49 | #if($lname.endsWith(".webm")) | ||
50 | #set($videoType = "video/webm") | ||
51 | #elseif($lname.endsWith(".ogg")) | ||
52 | #set($videoType = "video/ogg") | ||
53 | #elseif($lname.endsWith(".avi")) | ||
54 | #set($videoType = "video/x-msvideo") | ||
55 | #elseif($lname.endsWith(".mov")) | ||
56 | #set($videoType = "video/quicktime") | ||
57 | #elseif($lname.endsWith(".wmv")) | ||
58 | #set($videoType = "video/x-ms-wmv") | ||
59 | #elseif($lname.endsWith(".flv")) | ||
60 | #set($videoType = "video/x-flv") | ||
61 | #elseif($lname.endsWith(".m4v")) | ||
62 | #set($videoType = "video/mp4") | ||
63 | #end | ||
64 | |||
65 | #if($i == 1 || ($i - 1) % 48 == 0) | ||
66 | #set($chunkIndex = $chunkIndex + 1) | ||
67 | <div class="vid-chunk" data-chunk="$chunkIndex" style="display: #if($chunkIndex == 1) block #else none #end;"> | ||
68 | <div class="video-display-grid" style="display:grid;grid-template-columns:repeat(auto-fit,minmax(320px,1fr));gap:20px;"> | ||
69 | #end | ||
70 | |||
71 | <div class="video-container" style="border:1px solid #ddd;border-radius:8px;padding:12px;background:#fff;"> | ||
72 | <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;"> | ||
73 | <h4 style="margin:0;flex:1;min-width:0;">${escapetool.xml($filename)}</h4> | ||
74 | </div> | ||
75 | |||
76 | <!-- Placeholder (poster drawn on-demand) --> | ||
77 | <div class="video-frame" | ||
78 | data-src="${url}" | ||
79 | data-type="${videoType}" | ||
80 | data-name="${escapetool.xml($filename)}" | ||
81 | style="position:relative;width:100%;aspect-ratio:16/9;background:#111;border-radius:4px;overflow:hidden;display:flex;align-items:center;justify-content:center;cursor:pointer;"> | ||
82 | <canvas class="vid-canvas" width="320" height="180" style="position:absolute;inset:0;width:100%;height:100%;object-fit:cover;"></canvas> | ||
83 | <button class="btn btn-sm btn-primary" type="button" style="position:relative;z-index:1;">Load & Play</button> | ||
84 | </div> | ||
85 | |||
86 | <div class="video-controls" style="margin-top:8px;display:flex;flex-wrap:wrap;gap:6px;align-items:center;"> | ||
87 | <a href="${url}" download="${escapetool.xml($filename)}" class="btn btn-sm btn-success">📥 Download</a> | ||
88 | <span class="vid-duration" style="font-size:12px;color:#666;">Duration: —</span> | ||
89 | </div> | ||
90 | |||
91 | <!-- Move-to-page (native Page Picker) --> | ||
92 | <div class="move-box" style="margin-top:10px;"> | ||
93 | <label style="font-size:12px;color:#555;">Move to page:</label> | ||
94 | <div style="display:flex;gap:6px;align-items:center;flex-wrap:wrap;"> | ||
95 | <input type="text" | ||
96 | class="move-input suggest-pages" | ||
97 | placeholder="Find a page…" | ||
98 | data-search-scope="wiki:${escapetool.xml($xcontext.database)}" | ||
99 | data-filename="${escapetool.xml($filename)}" | ||
100 | style="flex:1;min-width:220px;padding:4px;border:1px solid #ccc;border-radius:4px;"> | ||
101 | </div> | ||
102 | <small style="color:#888;">Pick a page (e.g., <code>Main.SomePage</code> or a titled page). Selecting it moves this file.</small> | ||
103 | </div> | ||
104 | </div> | ||
105 | |||
106 | #if(($i % 48 == 0) || $foreach.last) | ||
107 | </div> | ||
108 | #if(!$foreach.last) | ||
109 | <div style="text-align:center;margin:12px 0 28px;"> | ||
110 | <button class="btn btn-secondary load-more" data-next="$mathtool.add($chunkIndex,1)">Load more</button> | ||
111 | </div> | ||
112 | #end | ||
113 | </div> | ||
114 | #end | ||
115 | #end | ||
116 | </div> | ||
117 | #end | ||
118 | </div> | ||
119 | |||
120 | <script> | ||
121 | (function(){ | ||
122 | // ---- Helpers | ||
123 | function spacesPath(dotPath){ | ||
124 | if(!dotPath) return ''; | ||
125 | return dotPath.split('.').map(function(s){ return 'spaces/' + encodeURIComponent(s); }).join('/'); | ||
126 | } | ||
127 | function parseFullName(full){ | ||
128 | // Accept "xwiki:Main.Sub.Page" or "Main.Sub.Page" | ||
129 | full = String(full || '').replace(/^[^:]+:/,''); | ||
130 | var parts = full.split('.'); | ||
131 | var page = parts.pop(); | ||
132 | return {spacePath: parts.join('.'), page: page}; | ||
133 | } | ||
134 | |||
135 | // ---- Poster generation (cheap first-frame) | ||
136 | async function makePoster(frame){ | ||
137 | if(frame.getAttribute('data-poster-ready')==='1') return; | ||
138 | var src = frame.getAttribute('data-src'); | ||
139 | try{ | ||
140 | var v = document.createElement('video'); | ||
141 | v.preload = 'metadata'; | ||
142 | v.muted = true; v.playsInline = true; | ||
143 | v.src = src; | ||
144 | |||
145 | await new Promise(function(res, rej){ | ||
146 | var done=false; | ||
147 | function finish(){ if(done) return; done=true; res(); } | ||
148 | v.addEventListener('loadeddata', finish, {once:true}); | ||
149 | v.addEventListener('loadedmetadata', function(){ try{ v.currentTime = 0.04; }catch(e){} }); | ||
150 | v.addEventListener('error', function(){ rej(new Error('metadata error')); }); | ||
151 | setTimeout(finish, 2500); | ||
152 | }); | ||
153 | |||
154 | var canvas = frame.querySelector('.vid-canvas'); | ||
155 | if(canvas){ | ||
156 | var w = 320, h = Math.round(320 * (v.videoHeight||9) / (v.videoWidth||16)); | ||
157 | canvas.width = 320; canvas.height = h>0?h:180; | ||
158 | var ctx = canvas.getContext('2d'); | ||
159 | if(ctx){ ctx.drawImage(v, 0, 0, canvas.width, canvas.height); } | ||
160 | } | ||
161 | frame.setAttribute('data-poster-ready','1'); | ||
162 | }catch(e){ /* keep dark box */ } | ||
163 | } | ||
164 | |||
165 | // ---- Mount real <video> on click (preload="none") | ||
166 | function mountVideo(frame){ | ||
167 | if(frame.getAttribute('data-mounted')==='1') return; | ||
168 | var src = frame.getAttribute('data-src'); | ||
169 | var type = frame.getAttribute('data-type') || 'video/mp4'; | ||
170 | var v = document.createElement('video'); | ||
171 | v.setAttribute('controls',''); v.setAttribute('preload','none'); | ||
172 | v.style.width='100%'; v.style.maxWidth='100%'; v.style.borderRadius='4px'; | ||
173 | var s = document.createElement('source'); s.src=src; s.type=type; v.appendChild(s); | ||
174 | v.addEventListener('loadedmetadata', function(){ | ||
175 | var d = Math.round(v.duration||0), mm = Math.floor(d/60), ss = String(d%60).padStart(2,'0'); | ||
176 | var dur = frame.parentElement.querySelector('.vid-duration'); if(dur) dur.textContent = 'Duration: '+mm+':'+ss; | ||
177 | }); | ||
178 | frame.replaceChildren(v); | ||
179 | frame.setAttribute('data-mounted','1'); | ||
180 | } | ||
181 | |||
182 | // ---- Lazy poster | ||
183 | if('IntersectionObserver' in window){ | ||
184 | var io = new IntersectionObserver(function(entries){ | ||
185 | entries.forEach(function(e){ | ||
186 | if(e.isIntersecting){ makePoster(e.target); io.unobserve(e.target); } | ||
187 | }); | ||
188 | }, { rootMargin: (window.VID_LAZY_MARGIN||'600px') }); | ||
189 | document.querySelectorAll('.video-frame').forEach(function(el){ io.observe(el); }); | ||
190 | } | ||
191 | |||
192 | // ---- Click to load & play | ||
193 | document.addEventListener('click', function(ev){ | ||
194 | var frame = ev.target.closest('.video-frame'); | ||
195 | if(frame){ | ||
196 | mountVideo(frame); | ||
197 | var v = frame.querySelector('video'); if(v) v.play().catch(function(){}); | ||
198 | } | ||
199 | }); | ||
200 | |||
201 | // ---- Chunk reveal | ||
202 | document.addEventListener('click', function(ev){ | ||
203 | var b = ev.target.closest('.load-more'); if(!b) return; | ||
204 | var next = b.getAttribute('data-next'); | ||
205 | var nxt = document.querySelector('.vid-chunk[data-chunk="'+ next +'"]'); | ||
206 | if(nxt){ nxt.style.display = 'block'; b.parentElement.style.display = 'none'; } | ||
207 | }); | ||
208 | |||
209 | // ---- Move: when a page is picked in the native Page Picker (fires change) | ||
210 | var wiki = window.XWIKI_WIKI; | ||
211 | function moveAttachment(opts){ | ||
212 | return (async function(){ | ||
213 | var srcSpace = opts.srcSpace, srcPage = opts.srcPage, filename = opts.filename, dstFull = opts.dstFull; | ||
214 | var pf = (function(full){ full = String(full||'').replace(/^[^:]+:/,''); var p=full.split('.'); var page=p.pop(); return {spacePath:p.join('.'), page:page}; })(dstFull); | ||
215 | var srcSpacesPath = spacesPath(srcSpace), dstSpacesPath = spacesPath(pf.spacePath); | ||
216 | |||
217 | // Download -> Upload -> Delete original | ||
218 | var cardInputSel = '.video-container input.move-input[data-filename="' + CSS.escape(filename) + '"]'; | ||
219 | var inputEl = document.querySelector(cardInputSel); | ||
220 | var card = inputEl ? inputEl.closest('.video-container') : null; | ||
221 | var frame = card ? card.querySelector('.video-frame') : null; | ||
222 | var srcURL = frame ? frame.getAttribute('data-src') : null; | ||
223 | if(!srcURL) throw new Error('Missing source URL'); | ||
224 | |||
225 | var downloading = await fetch(srcURL, {credentials:'same-origin'}); | ||
226 | if(!downloading.ok) throw new Error('Download failed: ' + downloading.status); | ||
227 | var blob = await downloading.blob(); | ||
228 | |||
229 | var putURL = '/rest/wikis/' + encodeURIComponent(wiki) + '/' + dstSpacesPath + | ||
230 | '/pages/' + encodeURIComponent(pf.page) + | ||
231 | '/attachments/' + encodeURIComponent(filename) + '?media=json'; | ||
232 | var uploading = await fetch(putURL, { | ||
233 | method: 'PUT', | ||
234 | body: blob, | ||
235 | headers: {'Content-Type':'application/octet-stream'}, | ||
236 | credentials:'same-origin' | ||
237 | }); | ||
238 | if(!(uploading.status===201 || uploading.status===202)){ | ||
239 | var txt = await uploading.text().catch(function(){ return String(uploading.status); }); | ||
240 | throw new Error('Upload failed: ' + txt); | ||
241 | } | ||
242 | |||
243 | var delURL = '/rest/wikis/' + encodeURIComponent(wiki) + '/' + srcSpacesPath + | ||
244 | '/pages/' + encodeURIComponent(srcPage) + | ||
245 | '/attachments/' + encodeURIComponent(filename); | ||
246 | var deleting = await fetch(delURL, {method:'DELETE', credentials:'same-origin'}); | ||
247 | if(!(deleting.status===204)){ console.warn('Delete original failed', deleting.status); } | ||
248 | return true; | ||
249 | })(); | ||
250 | } | ||
251 | |||
252 | document.addEventListener('change', function(e){ | ||
253 | var inp = e.target.closest('.move-input.suggest-pages'); if(!inp) return; | ||
254 | var selected = (inp.value || '').trim(); if(!selected) return; | ||
255 | |||
256 | var filename = inp.getAttribute('data-filename'); | ||
257 | var notice = document.createElement('div'); | ||
258 | notice.style.fontSize='12px'; notice.style.color='#666'; notice.textContent='Moving…'; | ||
259 | inp.parentElement.appendChild(notice); | ||
260 | |||
261 | moveAttachment({ | ||
262 | srcSpace: window.SOURCE_SPACE, | ||
263 | srcPage: window.SOURCE_PAGE, | ||
264 | filename: filename, | ||
265 | dstFull: selected | ||
266 | }).then(function(){ | ||
267 | notice.textContent = 'Moved ✔ — reloading…'; | ||
268 | setTimeout(function(){ location.reload(); }, 600); | ||
269 | }).catch(function(err){ | ||
270 | notice.style.color = '#b00020'; | ||
271 | notice.textContent = 'Move failed: ' + (err && err.message ? err.message : err); | ||
272 | }); | ||
273 | }); | ||
274 | })(); | ||
275 | </script> | ||
276 | |||
277 | <style> | ||
278 | .video-container:hover{box-shadow:0 4px 8px rgba(0,0,0,0.08);transition:box-shadow .25s;} | ||
279 | .btn{padding:4px 8px;border:1px solid #ddd;background:#f8f9fa;border-radius:4px;cursor:pointer;text-decoration:none;display:inline-block;} | ||
280 | .btn:hover{background:#e9ecef;} | ||
281 | .btn-primary{background:#007bff;color:#fff;border-color:#007bff;} | ||
282 | .btn-secondary{background:#6c757d;color:#fff;border-color:#6c757d;} | ||
283 | .btn-success{background:#28a745;color:#fff;border-color:#28a745;} | ||
284 | .btn-sm{font-size:12px;padding:2px 6px;} | ||
285 | @media (max-width:768px){.video-display-grid{grid-template-columns:1fr}} | ||
286 | </style> | ||
287 | {{/html}} | ||
288 | {{/cache}} | ||
289 | {{/velocity}} |