-
Notifications
You must be signed in to change notification settings - Fork 0
/
content__hyop.ts
365 lines (365 loc) · 11.1 KB
/
content__hyop.ts
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { type YT_Player } from '@btakita/domain--browser--brookebrodack/youtube'
import { lg_px_num } from '@btakita/ui--server--brookebrodack/css'
import { browser_ctx__ensure } from '@rappstack/domain--browser/ctx'
import {
be_,
be_memo_pair_,
be_sig_triple_,
calling,
memo_,
type memo_T,
type nullish,
nullish__none_,
ref__bind,
rmemo__wait,
tup,
type wide_ctx_T
} from 'ctx-core/rmemo'
import { wanimato__new, type wanimato_T } from 'ctx-core/web_animation'
import { spinner__attach, spinner__remove } from '../../spinner/index.js'
import {
YT_player_,
YT_PlayerState_,
YT_PlayerState__BUFFERING_,
YT_PlayerState__CUED_,
YT_PlayerState__PAUSED_,
YT_PlayerState__PLAYING_,
YT_PlayerState__UNSTARTED_,
} from '../../youtube/index.js'
export function content__hyop(content:HTMLElement) {
content__set(browser_ctx__ensure(), content)
window.addEventListener('keydown', evt=>{
if (evt.key === 'Escape') {
video__div__close(browser_ctx__ensure())
.catch(err=>console.error(err))
}
})
}
export function content_feed__a__hyop(content_feed__a:HTMLAnchorElement) {
const ctx = browser_ctx__ensure()
content_feed__a__icon_cause__ensure(content_feed__a)
/** @see {content__hyop} */
function content_feed__a__icon_cause__ensure(content_feed__a:HTMLAnchorElement) {
ref__bind(content_feed__a, calling(memo_(()=>{
const video__a = video__a_(ctx)
const is_current = video__a === content_feed__a
const video_url = YT_player_(ctx)?.getVideoUrl()
const video_searchParams = video_url ? new URL(video_url).searchParams : null
const content_feed__a_searchParams = new URL(content_feed__a.href).searchParams
const same_video = video_searchParams?.get('v') === content_feed__a_searchParams.get('v')
content_feed__a.classList.toggle(
'play',
is_current
&& same_video
&& YT_PlayerState__PLAYING_(ctx))
content_feed__a.classList.toggle(
'pause',
is_current
&& same_video
&& YT_PlayerState__PAUSED_(ctx))
if (is_current && (
YT_PlayerState_(ctx) == null
|| YT_PlayerState__UNSTARTED_(ctx)
|| YT_PlayerState__BUFFERING_(ctx)
|| YT_PlayerState__CUED_(ctx)
)) {
spinner__attach(content_feed__a)
} else {
spinner__remove(content_feed__a)
}
content_feed__a.addEventListener('click', content_feed__a__onclick)
})))
}
/** @see {content_feed__a__icon_cause__ensure} */
function content_feed__a__onclick(evt:MouseEvent) {
evt.stopPropagation()
evt.preventDefault()
const currentTarget = evt.currentTarget as HTMLAnchorElement
const video__a = video__a_(ctx)
if (video__a === currentTarget) {
if (YT_PlayerState__PLAYING_(ctx)) {
YT_player_(ctx)?.pauseVideo()
} else if (YT_PlayerState__PAUSED_(ctx)) {
YT_player_(ctx)?.playVideo()
}
} else {
video__div__open(ctx).catch(err=>console.error(err))
video__a__set(ctx, currentTarget)
content_feed__a__onclick__YT_player__run()
.catch(err=>console.error(err))
}
/** @see {content_feed__a__onclick} */
function content_feed__a__onclick__YT_player__run() {
return rmemo__wait(
()=>YT_player_(ctx),
YT_player=>YT_player,
10_000
).then(YT_player=>{
if (video__a_(ctx) !== currentTarget) return
YT_player = YT_player as YT_Player
YT_iframe__wanimato_(ctx)
site__header_video__div__background_(ctx)
YT_player.stopVideo()
const props = JSON.parse(decodeURIComponent(currentTarget.dataset.op!))
YT_player.loadVideoById(props)
YT_player.playVideo()
})
}
}
}
export function content__back_link__a__hyop(back_link__a:HTMLAnchorElement) {
back_link__a__set(browser_ctx__ensure(), back_link__a)
}
export function content__video__div__hyop(video__div:HTMLElement) {
video__div__set(browser_ctx__ensure(), <video__div_T>video__div)
}
export function content__video_close__div__hyop(video_close__div:HTMLElement) {
video_close__div__set(browser_ctx__ensure(), <video_close__div_T>video_close__div)
}
export function content__site__header__hyop(site__header:HTMLElement) {
site__header__set(browser_ctx__ensure(), site__header)
}
const [
,
/** @see {content_feed__a__icon_cause__ensure} */
/** @see {content_feed__a__onclick} */
/** @see {content_feed__a__onclick__YT_player__run} */
video__a_,
/** @see {content_feed__a__onclick} */
video__a__set
] = be_sig_triple_<HTMLAnchorElement|nullish>(()=>undefined)
const [
,
/** @see {video__div_} */
content_,
/** @see {content__hyop} */
content__set,
] = be_sig_triple_<HTMLElement|undefined>(
()=>undefined,
[
ctx=>memo_(()=>{
video__div_(ctx)
})
])
const [
,
back_link__a_,
back_link__a__set,
] = be_sig_triple_<HTMLAnchorElement>(()=>undefined!)
const [
,
back_link__a__svg_
] = be_memo_pair_(ctx=>
back_link__a_(ctx).querySelector<SVGSVGElement>('svg')!)
const [
,
/** @see {video__div__wanimato_} */
video__div_,
video__div__set,
] = be_sig_triple_<video__div_T>(
()=>undefined!,
[
(ctx, video__div$)=>
memo_(()=>{
const video__div = video__div$()
if (video__div) {
video__div.style.height = '0'
video__div.spinner_cause$ = spinner_cause$_(ctx)
}
})
])
const [
,
,
video_close__div__set
] = be_sig_triple_<video_close__div_T|undefined>(
()=>undefined,
[
(ctx, video_close__div$)=>memo_<video_close__div_T|undefined>($=>
nullish__none_([video_close__div$()], video_close__div=>{
$.val?.removeEventListener('click', video_close__div__onclick)
video_close__div.addEventListener('click', video_close__div__onclick)
return video_close__div
})
),
(ctx, video_close__div$)=>memo_(()=>
nullish__none_([video_close__div$()], video_close__div=>{
video_close__div.classList.toggle('block', video__div__is_open_(ctx))
video_close__div.classList.toggle('hidden', !video__div__is_open_(ctx))
}))
])
function video_close__div__onclick() {
const ctx = browser_ctx__ensure()
video__div__close(ctx).catch(err=>console.error(err))
}
const [
,
/** @see {site__header__img_} */
/** @see {site__header__wanimato_} */
site__header_,
site__header__set,
] = be_sig_triple_<HTMLElement>(()=>undefined!)
const [
,
/** @see {site_header__img__wanimato_} */
site__header__img_,
] = be_memo_pair_(ctx=>
site__header_(ctx).querySelector<HTMLImageElement>('img')!)
const spinner_cause$_ = be_(ctx=>
calling(memo_(()=>{
nullish__none_([video__div_(ctx)],
video__div=>{
if (video__a_(ctx) && YT_PlayerState__CUED_(ctx)) {
spinner__attach(video__div)
} else {
spinner__remove(video__div)
}
return video__a_(ctx)
})
})))
function video__div__animation_height_() {
return (
window.innerWidth > lg_px_num
? '600px'
: screen.orientation.type === 'landscape-primary'
? '100dvh'
: '50dvh'
)
}
const [
,
video__div__is_open_,
video__div__is_open__set
] = be_sig_triple_(()=>false)
async function video__div__open(ctx:wide_ctx_T) {
video__div__is_open__set(ctx, true)
const video__div__wanimato = video__div__wanimato_(ctx)
await video__div__wanimato?.animation.ready
if (video__div__wanimato && !video__div__wanimato?.finish_currentTime) {
video__div__wanimato_(ctx)?.animation.play()
YT_iframe__wanimato_(ctx)?.animation.play()
site__header__wanimato_(ctx)?.animation.play()
site_header__img__wanimato_(ctx)?.animation.play()
}
}
async function video__div__close(ctx:wide_ctx_T) {
video__a__set(ctx, null)
YT_player_(ctx)?.stopVideo()
video__div__is_open__set(ctx, false)
await video__div__wanimato_(ctx)?.animation.ready
if (video__div__wanimato_(ctx)?.finish_currentTime) {
video__div__wanimato_(ctx)?.animation.reverse()
YT_iframe__wanimato_(ctx)?.animation.reverse()
site__header__wanimato_(ctx)?.animation.reverse()
site_header__img__wanimato_(ctx)?.animation.reverse()
}
(<HTMLElement>document.activeElement)?.blur()
}
const [
,
/** @see {video__div__open} */
/** @see {video__div__close} */
video__div__wanimato_
] = be_memo_pair_<wanimato_T|nullish>((ctx, $)=>{
if (reduced_motion_(ctx)) return
return nullish__none_([video__div_(ctx)], video__div=>
wanimato__new($, video__div, ()=>video__div.animate([
{ height: '0px' },
{ height: video__div__animation_height_() }
], { duration: 25, fill: 'both' })))
}, [
(ctx, video__div__wanimato$)=>
memo_(()=>{
content_(ctx)?.classList.toggle('z-20',
!!(video__div__wanimato$()?.is_finish && video__div__wanimato$()?.finish_currentTime))
content_(ctx)?.classList.toggle('z-10',
!(video__div__wanimato$()?.is_finish && video__div__wanimato$()?.finish_currentTime))
})
])
const [
,
/** @see {content_feed__a__onclick__YT_player__run} */
YT_iframe__wanimato_
] = be_memo_pair_<wanimato_T|nullish>((ctx, $)=>{
if (reduced_motion_(ctx)) return
return nullish__none_([YT_player_(ctx)], YT_player=>
wanimato__new($, YT_player.getIframe(), iframe=>
iframe.animate([
{ transform: 'scale(0)' },
{ transform: 'scale(1)' },
], { duration: 50, fill: 'forwards' })))
}, [
(ctx, YT_iframe__wanimato$)=>memo_(()=>
nullish__none_([YT_iframe__wanimato$()], YT_iframe__wanimato=>
YT_iframe__wanimato.animation.ready.then(()=>{
YT_iframe__wanimato.el.classList.toggle('hidden',
!YT_iframe__wanimato.finish_currentTime)
})))
])
const [
,
/** @see {content_feed__a__onclick__YT_player__run} */
site__header_video__div__background_
] = be_memo_pair_(ctx=>
nullish__none_(tup(
content_(ctx),
site__header_(ctx),
video__div_(ctx),
YT_iframe__wanimato_(ctx)
), (content, site__header, video__div, YT_iframe__wanimato)=>{
const is_end_state = YT_iframe__wanimato.is_finish && !!YT_iframe__wanimato.finish_currentTime
site__header.classList.toggle('bg-cyan-600/90', is_end_state)
site__header.classList.toggle('text-white', is_end_state)
back_link__a__svg_(ctx).classList.toggle('!stroke-white', is_end_state)
video__div.classList.toggle('sticky', is_end_state)
video__div.classList.toggle('bg-cyan-600/90', is_end_state)
video__div.classList.toggle('border-b-1px', is_end_state)
video__div.classList.toggle('border-white/.3', is_end_state)
video__div.classList.toggle('shadow-md', is_end_state)
}))
const [
,
/** @see {content_feed__a__onclick} */
site__header__wanimato_
] = be_memo_pair_<wanimato_T|nullish>((ctx, $)=>{
if (reduced_motion_(ctx)) return
return wanimato__new(
$,
site__header_(ctx),
site__header=>site__header.animate([
{ height: '144px' },
{ height: '72px' },
], { duration: 25, fill: 'both' }))
}, [
(ctx, site__header__wanimato$)=>memo_(()=>{
site__header_(ctx).classList.toggle(
'h-32',
!site__header__wanimato$()?.is_finish
|| !site__header__wanimato$()?.finish_currentTime)
})
])
const [
,
/** @see {content_feed__a__onclick} */
site_header__img__wanimato_,
] = be_memo_pair_<wanimato_T|nullish>((ctx, $)=>{
if (reduced_motion_(ctx)) return
return wanimato__new(
$,
site__header__img_(ctx),
site__header__img=>site__header__img.animate([
{ height: '108px', width: '108px' },
{ height: '54px', width: `54px` },
], { duration: 25, fill: 'both' }))
})
const [
,
/** @see {video__div__wanimato_} */
/** @see {YT_iframe__wanimato_} */
/** @see {site__header__wanimato_} */
/** @see {site_header__img__wanimato_} */
reduced_motion_
] = be_memo_pair_(()=>
window.matchMedia('(prefers-reduced-motion: reduce)').matches)
type video__div_T = HTMLDivElement&{ spinner_cause$:memo_T<void> }
type video_close__div_T = HTMLDivElement&{ visible$:memo_T<unknown> }