gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit33072bb533072bb5|||PR Record day72 gates and Chrome proofmre33072bb5/components/main.hl

30.4 KB

  1. // components/main.hl — THE SHELL: the layouts header with the main menu (the phone's sidebar behind the toggle), the slot every screen renders
  2. // into, and the PLAYER BAR at the bottom. The shell stays when a screen is replaced by a click on a link (hl:web navigates in the page), so the song keeps playing
  3. // while you browse. A screen starts a song with `$parent.jbPlay(ctx, index, id)`: the context says which list the song was tapped in (`{ kind, sort, minPlays, ... }`,
  4. // lib/rows.hl), the id WHICH SONG (the id decides, the server looks the song up in the list as it is now; the index is only where the screen saw it, counted plays move songs). A screen starts a shuffle with `$parent.jbShuffle(spec)` (lib/queue.hl: favourites or the list the screen shows); the server then holds the
  5. // shuffled order and the browser asks for 40 songs at a time. The queue of the media player always starts at the song that plays: the bar shows it, the overlay lists the 20 after it.
  6. // THE NOW-PLAYING OVERLAY is the same bar and the same media player, drawn bigger by CSS: a tap on the bar opens it (full screen on the phone, a side panel from 60rem), the
  7. // close button, the Escape key or a swipe down closes it. THE COUNT: a song counts as played after half of its length or 4 minutes of listening (whichever comes first); the shell counts
  8. // the seconds the audio really plays (a one second timer), asks the server once (`jbPlayed`) and the jukebox database adds 1 to its own counter. The files are never written.
  9. // KEYBOARD: NOT HERE. Every key handler of the shell (document listener, body, the app div) made the audio element load its source again on each key press (the song
  10. // restarted): Hybriel gap, repro in reports/JB3b-keyboard-gap.md. The media player's own keys (Space, arrows on its audio element) stay as they are.
  11. // THE BAR'S BUTTONS, left to right: Shuffle, Previous, Play / pause, Next, Repeat (no 10 second jumps). Shuffle is a switch: on = the songs still to come are shuffled from the song that plays
  12. // (the shuffle context below, seeded), off = the list's own order goes on after it; the song that plays is kept. Repeat steps off, all, one: one = the media player's own loop,
  13. // all = at the end of the queue the list starts again from its first song, off = it stops. Both are kept in storage/ui.json (lib/remember.hl).
  14. // LOCK SCREEN: the browser's media session shows title, artist, album and cover and gives play, pause, previous and next to the lock screen and the headphone keys.
  15. import AppHeader from '../theme/shell/app-header.hl'
  16. import MainMenu from '../theme/shell/main-menu.hl'
  17. import Sidebar from '../theme/shell/app-sidebar.hl'
  18. import MediaPlayer from './media-player/media-player.hl'
  19. import Icon from './icons/icons.hl'
  20. import { now } from 'hl:time'
  21. import { rowsFor, queueFor } from '../lib/rows.hl'
  22. import { countPlay } from '../lib/plays.hl'
  23. import { showOf } from '../lib/dups.hl'
  24. import { lastShuffle, lastRepeat, rememberPlayer } from '../lib/remember.hl'
  25. slot = null
  26. menu = [ { label = 'Songs' href = '/' current = false } { label = 'Artists' href = '/artists' } { label = 'Albums' href = '/albums' } { label = 'Search' href = '/search' } ]
  27. // the player: what plays now, and the songs that follow in the list it came from (the first item of the queue is the song that plays)
  28. String nowSrc = ''
  29. String nowTitle = ''
  30. String nowArtist = ''
  31. String nowAlbum = ''
  32. String nowCover = ''
  33. String nowArt = ''
  34. String nowId = ''
  35. String autoPlay = 'false'
  36. Hybrid nowQueue = []
  37. Hybrid nowCtx = {}
  38. Number nowIdx = 0
  39. // the last user action whose server emit failed (a value-form emit answers null when the socket is dead and nobody knew yet); run once when the server answers again
  40. Hybrid pend = null
  41. // the number of the newest user action: a failure that comes late from an older action must not put it back into pend
  42. Number gen = 0
  43. // the server's reachability (hl:web tells the page; 'up' runs the lost action, see retryPend)
  44. __hlReach = 'connecting'
  45. String overlay = 'false'
  46. String barClass = 'jb-app'
  47. String coverNow = overlay == 'true' ? nowArt : nowCover
  48. Number heard = 0
  49. String countedId = ''
  50. Number swipeY = -1
  51. String ticking = 'false'
  52. String shuffleMode = lastShuffle()
  53. String repeatMode = lastRepeat()
  54. Boolean shuffleOn = shuffleMode == 'on'
  55. Boolean repeatOn = repeatMode != 'off'
  56. String repeatValue = repeatMode == 'one' ? 'one' : 'off'
  57. String repeatIcon = repeatMode == 'one' ? 'repeatOne' : 'repeat'
  58. String shuffleTitle = 'Shuffle: ' + shuffleMode
  59. String repeatTitle = 'Repeat: ' + (repeatMode == 'off' ? 'off' : repeatMode == 'all' ? 'all songs' : 'this song')
  60. Style {
  61. // Keep the quoted class selector and its original specificity.
  62. '.jb-app' { '--jb-bar' = '0rem' }
  63. // Keyframes use the documented quoted animation key.
  64. '@keyframes jb-mount' {
  65. 'from' { opacity = '.999' }
  66. to { opacity = '1' }
  67. }
  68. jbTick {
  69. display = 'block'
  70. height = '0'
  71. animation = 'jb-mount 100ms linear infinite'
  72. }
  73. // Caller page supplies jb-screen through the slot; retain its exact tag selector.
  74. 'jb-screen' {
  75. display = 'flex'
  76. flexDirection = 'column'
  77. height = 'calc(100dvh - var(--layout-header) - var(--jb-bar))'
  78. paddingInline = 'max(var(--layout-gutter), calc((100% - var(--layout-column)) / 2))'
  79. paddingTop = '.5rem'
  80. // Keep the quoted child selector into the composed virtual list.
  81. '> virtual-list' {
  82. flex = '1 1 auto'
  83. height = 'auto'
  84. minHeight = '0'
  85. marginInline = '-.6rem'
  86. }
  87. @media {
  88. minWidth = '60rem'
  89. paddingTop = '1rem'
  90. }
  91. }
  92. // Keep the quoted class selector and its original specificity.
  93. '.jb-bar' {
  94. display = 'none'
  95. mediaPlayer {
  96. display = 'contents'
  97. // Keep the quoted selector into the composed media player.
  98. '.mp-cover' {
  99. gridArea = 'cover'
  100. display = 'flex'
  101. alignItems = 'center'
  102. gap = '.6rem'
  103. margin = '0'
  104. minWidth = '0'
  105. paddingInline = 'max(var(--layout-gutter), calc((100% - var(--layout-column)) / 2))'
  106. cursor = 'pointer'
  107. div { minWidth = '0' }
  108. p {
  109. margin = '0'
  110. fontSize = '.8rem'
  111. overflow = 'hidden'
  112. textOverflow = 'ellipsis'
  113. whiteSpace = 'nowrap'
  114. }
  115. img {
  116. width = '2.5rem'
  117. height = '2.5rem'
  118. }
  119. h2 {
  120. margin = '0'
  121. fontSize = '.95rem'
  122. lineHeight = '1.3'
  123. fontWeight = '600'
  124. overflow = 'hidden'
  125. textOverflow = 'ellipsis'
  126. whiteSpace = 'nowrap'
  127. }
  128. }
  129. mpButtons {
  130. display = 'contents'
  131. // Keep the quoted selector into the composed media player.
  132. '.mp-play' {
  133. gridArea = 'buttons'
  134. background = 'transparent'
  135. }
  136. // Keep the quoted selector into the composed media player.
  137. '.mp-mute' { display = 'none' }
  138. // Keep the quoted selector into the composed media player.
  139. '.mp-speed' { display = 'none' }
  140. // Keep the quoted selector into the composed media player.
  141. '.mp-times' { display = 'none' }
  142. }
  143. mpSeek { gridArea = 'seek' }
  144. audio { display = 'none' }
  145. }
  146. // Keep the quoted class selector and its original specificity.
  147. '.jb-close' {
  148. display = 'none'
  149. hover { background = 'var(--color-surface)' }
  150. }
  151. // Keep the quoted class selector and its original specificity.
  152. '.jb-album' { display = 'none' }
  153. // Keep the quoted class selector and its original specificity.
  154. 'button.jb-prev' {
  155. display = 'inline-flex'
  156. alignItems = 'center'
  157. justifyContent = 'center'
  158. minWidth = '2.75rem'
  159. minHeight = '2.75rem'
  160. margin = '0'
  161. padding = '0'
  162. color = 'var(--color-text)'
  163. background = 'transparent'
  164. borderRadius = 'var(--radius-small)'
  165. gridArea = 'prev'
  166. hover { background = 'var(--color-surface)' }
  167. }
  168. // Keep the quoted class selector and its original specificity.
  169. 'button.jb-next' {
  170. display = 'inline-flex'
  171. alignItems = 'center'
  172. justifyContent = 'center'
  173. minWidth = '2.75rem'
  174. minHeight = '2.75rem'
  175. margin = '0'
  176. padding = '0'
  177. color = 'var(--color-text)'
  178. background = 'transparent'
  179. borderRadius = 'var(--radius-small)'
  180. gridArea = 'next'
  181. hover { background = 'var(--color-surface)' }
  182. }
  183. // Shuffle, before Previous: a switch, the accent colour and a surface behind it when on.
  184. #jbShuffle {
  185. display = 'inline-flex'
  186. alignItems = 'center'
  187. justifyContent = 'center'
  188. minWidth = '2.75rem'
  189. minHeight = '2.75rem'
  190. margin = '0'
  191. padding = '0'
  192. color = 'var(--color-text)'
  193. background = 'transparent'
  194. borderRadius = 'var(--radius-small)'
  195. gridArea = 'shuffle'
  196. hover { background = 'var(--color-surface)' }
  197. }
  198. // Repeat, after Next: off / all / one; the accent colour and a surface behind it when not off.
  199. #jbRepeat {
  200. display = 'inline-flex'
  201. alignItems = 'center'
  202. justifyContent = 'center'
  203. minWidth = '2.75rem'
  204. minHeight = '2.75rem'
  205. margin = '0'
  206. padding = '0'
  207. color = 'var(--color-text)'
  208. background = 'transparent'
  209. borderRadius = 'var(--radius-small)'
  210. gridArea = 'repeat'
  211. marginRight = 'max(.25rem, calc((100vw - var(--layout-column)) / 2))'
  212. hover { background = 'var(--color-surface)' }
  213. }
  214. }
  215. // Keep the quoted class selector and its original specificity.
  216. '.jb-app.playing' {
  217. '--jb-bar' = '4.625rem'
  218. // Keep the quoted class selector and its original specificity.
  219. '.jb-bar' {
  220. display = 'grid'
  221. position = 'fixed'
  222. left = '0'
  223. right = '0'
  224. bottom = '0'
  225. zIndex = '15'
  226. height = 'var(--jb-bar)'
  227. background = 'var(--color-background-sunken)'
  228. gridTemplateColumns = 'minmax(0, 1fr) auto auto auto auto auto'
  229. gridTemplateRows = '1.375rem 1fr'
  230. gridTemplateAreas = '"seek seek seek seek seek seek" "cover shuffle prev buttons next repeat"'
  231. alignItems = 'center'
  232. }
  233. @media {
  234. minWidth = '60rem'
  235. '--jb-bar' = '5.125rem'
  236. }
  237. }
  238. // Shuffle and Repeat when on: a nested quoted compound would be a descendant, so the state rule is whole (id and attribute on the same button).
  239. // The framework writes an id kebab-case (hybriel#256) in nested rules but not inside a quoted key: here the id is written as it is sent.
  240. '.jb-bar #jb-shuffle[aria-pressed="true"], .jb-bar #jb-repeat[aria-pressed="true"]' {
  241. color = 'var(--color-accent)'
  242. background = 'var(--color-surface)'
  243. }
  244. // Keep the quoted condition: :not() has no bare form.
  245. '.jb-app:not(.open)' {
  246. // Keep the quoted class selector and its original specificity.
  247. '.jb-bar' {
  248. mediaPlayer {
  249. mpSeek {
  250. height = '1.375rem'
  251. margin = '0'
  252. // Keep the quoted selector into the composed media player.
  253. '.mp-track' {
  254. top = '0'
  255. left = '0'
  256. right = '0'
  257. height = '1.375rem'
  258. borderRadius = '0'
  259. }
  260. // Keep the quoted selector into the composed media player.
  261. '.mp-thumb' { display = 'none' }
  262. }
  263. // Keep the quoted condition: :not() has no bare form.
  264. '> div:not(.mp-cover)' { display = 'none' }
  265. mpButtons {
  266. // Keep the quoted selector into the composed media player.
  267. '.mp-times' {
  268. @media {
  269. minWidth = '60rem'
  270. display = 'inline-flex'
  271. marginLeft = '.5rem'
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. // Keep the quoted class selector and its original specificity.
  279. '.jb-app.open' {
  280. '--jb-bar' = '0rem'
  281. // Keep the quoted class selector and its original specificity.
  282. '.jb-bar' {
  283. display = 'grid'
  284. position = 'fixed'
  285. top = '0'
  286. left = '0'
  287. right = '0'
  288. bottom = '0'
  289. zIndex = '40'
  290. height = 'auto'
  291. background = 'var(--color-background)'
  292. gridTemplateColumns = '1fr 3rem 3rem 3.5rem 3rem 3rem 1fr'
  293. gridTemplateRows = 'auto auto auto auto auto auto minmax(0, 1fr)'
  294. gridTemplateAreas = 'none'
  295. alignItems = 'center'
  296. justifyItems = 'center'
  297. touchAction = 'none'
  298. paddingBottom = 'env(safe-area-inset-bottom)'
  299. // Keep the quoted class selector and its original specificity.
  300. '.jb-close' {
  301. display = 'inline-flex'
  302. gridColumn = '1 / -1'
  303. gridRow = '1'
  304. justifySelf = 'end'
  305. alignItems = 'center'
  306. justifyContent = 'center'
  307. minWidth = '2.75rem'
  308. minHeight = '2.75rem'
  309. margin = '.25rem .5rem 0'
  310. padding = '0'
  311. color = 'var(--color-text)'
  312. background = 'transparent'
  313. borderRadius = 'var(--radius-small)'
  314. }
  315. mediaPlayer {
  316. // Keep the quoted selector into the composed media player.
  317. '.mp-cover' {
  318. gridArea = 'auto'
  319. gridColumn = '1 / -1'
  320. gridRow = '2'
  321. flexDirection = 'column'
  322. gap = '.6rem'
  323. width = '100%'
  324. paddingInline = '1.5rem'
  325. cursor = 'default'
  326. textAlign = 'center'
  327. p {
  328. fontSize = '1rem'
  329. whiteSpace = 'normal'
  330. textAlign = 'center'
  331. }
  332. img {
  333. width = 'clamp(0px, calc(100dvh - 36rem), min(calc(100vw - 3rem), 32dvh))'
  334. height = 'clamp(0px, calc(100dvh - 36rem), min(calc(100vw - 3rem), 32dvh))'
  335. maxWidth = '100%'
  336. @media {
  337. maxHeight = '41rem'
  338. display = 'none'
  339. }
  340. @media {
  341. minWidth = '60rem'
  342. width = 'clamp(0px, calc(100dvh - 36rem), min(18rem, 32dvh))'
  343. height = 'clamp(0px, calc(100dvh - 36rem), min(18rem, 32dvh))'
  344. }
  345. }
  346. h2 {
  347. fontSize = '1.25rem'
  348. overflow = 'visible'
  349. whiteSpace = 'normal'
  350. textAlign = 'center'
  351. }
  352. }
  353. mpSeek {
  354. gridArea = 'auto'
  355. gridColumn = '1 / -1'
  356. gridRow = '4'
  357. justifySelf = 'stretch'
  358. height = '1.375rem'
  359. marginInline = '1.25rem'
  360. // Keep the quoted selector into the composed media player.
  361. '.mp-track' {
  362. top = '0'
  363. height = '1.375rem'
  364. }
  365. // Keep the quoted selector into the composed media player.
  366. '.mp-thumb' { display = 'none' }
  367. }
  368. mpButtons {
  369. // Keep the quoted selector into the composed media player.
  370. '.mp-play' {
  371. gridArea = 'auto'
  372. gridColumn = '4'
  373. gridRow = '5'
  374. background = 'var(--color-surface)'
  375. }
  376. // Keep the quoted selector into the composed media player.
  377. '.mp-times' {
  378. display = 'inline-flex'
  379. gridColumn = '2 / 5'
  380. gridRow = '6'
  381. justifySelf = 'start'
  382. marginLeft = '0'
  383. }
  384. // Keep the quoted selector into the composed media player.
  385. '.mp-mute' {
  386. display = 'inline-flex'
  387. gridColumn = '5'
  388. gridRow = '6'
  389. }
  390. // Keep the quoted selector into the composed media player.
  391. '.mp-speed' {
  392. display = 'inline-flex'
  393. gridColumn = '6'
  394. gridRow = '6'
  395. }
  396. }
  397. // Keep the quoted condition: :not() has no bare form.
  398. '> div:not(.mp-cover)' {
  399. display = 'block'
  400. gridColumn = '1 / -1'
  401. gridRow = '7'
  402. justifySelf = 'stretch'
  403. alignSelf = 'stretch'
  404. minHeight = '0'
  405. overflowY = 'auto'
  406. padding = '0 .75rem .75rem'
  407. touchAction = 'pan-y'
  408. }
  409. // Keep the quoted selector into the composed media player.
  410. '.mp-queue-title' {
  411. margin = '.5rem .25rem .1rem'
  412. fontSize = '.85rem'
  413. letterSpacing = '.04em'
  414. textTransform = 'uppercase'
  415. color = 'var(--color-text-muted)'
  416. }
  417. // Keep the quoted selector into the composed media player.
  418. 'ol.mp-queue' {
  419. // One column the width of the panel: a long title in the queue must not widen the grid past the screen (the times went off to the right).
  420. gridTemplateColumns = 'minmax(0, 1fr)'
  421. // Keep the quoted functional pseudo-class.
  422. 'li:nth-child(n+22)' { display = 'none' }
  423. // Keep the quoted selector into the composed media player.
  424. '.mp-q-title' {
  425. overflow = 'hidden'
  426. textOverflow = 'ellipsis'
  427. whiteSpace = 'nowrap'
  428. overflowWrap = 'normal'
  429. }
  430. }
  431. }
  432. // Keep the quoted class selector and its original specificity.
  433. '.jb-album' {
  434. display = 'block'
  435. gridColumn = '1 / -1'
  436. gridRow = '3'
  437. margin = '.1rem 1.5rem 0'
  438. textAlign = 'center'
  439. color = 'var(--color-text-muted)'
  440. @media {
  441. maxHeight = '41rem'
  442. display = 'none'
  443. }
  444. }
  445. // Keep the quoted class selector and its original specificity.
  446. 'button.jb-prev' {
  447. gridArea = 'auto'
  448. gridColumn = '3'
  449. gridRow = '5'
  450. }
  451. // Keep the quoted class selector and its original specificity.
  452. 'button.jb-next' {
  453. gridArea = 'auto'
  454. gridColumn = '5'
  455. gridRow = '5'
  456. }
  457. #jbShuffle {
  458. gridArea = 'auto'
  459. gridColumn = '2'
  460. gridRow = '5'
  461. }
  462. #jbRepeat {
  463. gridArea = 'auto'
  464. gridColumn = '6'
  465. gridRow = '5'
  466. marginRight = '0'
  467. }
  468. @media {
  469. minWidth = '60rem'
  470. top = 'var(--layout-header)'
  471. left = 'auto'
  472. width = '24rem'
  473. zIndex = '15'
  474. }
  475. }
  476. @media {
  477. minWidth = '60rem'
  478. '--jb-bar' = '0rem'
  479. }
  480. // Caller page supplies jb-screen through the slot; retain its exact tag selector.
  481. 'jb-screen' {
  482. @media {
  483. minWidth = '60rem'
  484. marginRight = '24rem'
  485. }
  486. }
  487. }
  488. }
  489. View {
  490. body {
  491. AppHeader { brand = "Jukebox"
  492. MainMenu { items = menu }
  493. }
  494. Sidebar { desktop = "false" MainMenu { items = menu } }
  495. div { class = barClass
  496. slot
  497. jbTick { on animationiteration(e) { emit jbMount(e) } }
  498. div { class = "jb-bar" on click(e) { emit jbBarClick(e) } on pointerdown(e) { emit jbDown(e) } on pointerup(e) { emit jbUp(e) } on pointercancel(e) { emit jbCancel(e) }
  499. button { type = "button" class = "jb-close" title = "Close" 'aria-label' = "Close" Icon { name = "close" size = "1.5em" } on click(e) { emit jbClose(e) } }
  500. MediaPlayer { queueLabel = "Next up" src = nowSrc kind = "audio" title = nowTitle artist = nowArtist cover = coverNow queue = nowQueue onPlay = pickedNext autoPlay = autoPlay skip = false repeat = repeatValue }
  501. p { class = "jb-album" nowAlbum }
  502. button { id = "jbShuffle" type = "button" title = shuffleTitle 'aria-label' = "Shuffle" 'aria-pressed' = shuffleOn Icon { name = "shuffle" size = "1.5em" } on click(e) { emit jbShuffleClick(e) } }
  503. button { type = "button" class = "jb-prev" title = "Previous song" 'aria-label' = "Previous song" Icon { name = "prevTrack" size = "1.5em" } on click(e) { emit jbPrevClick(e) } }
  504. button { type = "button" class = "jb-next" title = "Next song" 'aria-label' = "Next song" Icon { name = "nextTrack" size = "1.5em" } on click(e) { emit jbNextClick(e) } }
  505. button { id = "jbRepeat" type = "button" title = repeatTitle 'aria-label' = "Repeat" 'aria-pressed' = repeatOn Icon { name = repeatIcon size = "1.5em" } on click(e) { emit jbRepeatClick(e) } }
  506. }
  507. }
  508. }
  509. }
  510. // ---- the server's part -------------------------------------------------------------------------------------------------------
  511. // rows of a list for a screen: `{ total, offset, rows }`
  512. on server jbRows(ctx, offset, limit) {
  513. return rowsFor(ctx, offset, limit)
  514. }
  515. // the songs of a list from `index` on, as queue items
  516. on server jbQueue(ctx, index, count, id, step, again) {
  517. return queueFor(ctx, index, count, id, step, again)
  518. }
  519. // a new shuffle of a pool (`{ kind: 'favourites', minPlays }` or `{ kind: 'list', ctx }`): its context, with a seed from the clock.
  520. // `first` is the id of a song that must come first (a shuffle turned on while it plays), or null
  521. on server jbShuffleCtx(spec, first) {
  522. return { kind = 'shuffle' seed = now() % 2147483000 spec = spec first = first }
  523. }
  524. // Shuffle and Repeat were changed: kept for the next visit
  525. on server jbRemember(shuffle, repeat) {
  526. return rememberPlayer(shuffle, repeat)
  527. }
  528. // the song was listened to long enough: the jukebox database adds 1 to its own counter (the same call as POST /api/played/<id>).
  529. // The open screen is told the new count and writes that row itself.
  530. on server jbPlayed(id) {
  531. c = countPlay(id, now())
  532. if (c != null) { emit client jbPlays(c.id, showOf(c.id, c.filePlays).plays) }
  533. return c
  534. }
  535. // ---- playing -----------------------------------------------------------------------------------------------------------------
  536. setBar = () => {
  537. barClass = 'jb-app' + (nowId != '' ? ' playing' : '') + (overlay == 'true' ? ' open' : '')
  538. }
  539. // the audio element of the media player
  540. audioEl = () => { return document.querySelector('media-player audio') }
  541. // show what plays (a queue item) and start it
  542. showNow = (item) => {
  543. nowSrc = item.src
  544. nowTitle = item.title
  545. nowArtist = item.artist
  546. nowAlbum = item.album
  547. nowCover = item.cover
  548. nowArt = item.art
  549. nowId = item.id
  550. nowIdx = item.idx
  551. heard = 0
  552. countedId = ''
  553. autoPlay = 'true'
  554. setBar()
  555. let ms = window.navigator.mediaSession
  556. if (ms != null) {
  557. ms.metadata = window.Reflect.construct(window.MediaMetadata, [ { title = item.title artist = item.artist album = item.album artwork = [ { src = item.art } ] } ])
  558. }
  559. }
  560. // a queue that starts at the song that plays
  561. fromPlace = (at) => {
  562. out = []
  563. let i = at
  564. while (i < nowQueue.length) {
  565. out.push(nowQueue[i])
  566. i = i + 1
  567. }
  568. return out
  569. }
  570. // the button or row that started a song gives the focus back (the player's own buttons keep it), so Space keeps meaning play / pause
  571. on jbUnfocus() {
  572. f = document.activeElement
  573. if (f != null && f.blur != null && f.closest('media-player') == null) { f.blur() }
  574. return null
  575. }
  576. // a new user action starts: whatever waited for the server is dropped, and the action's number is returned
  577. begin = () => {
  578. gen = gen + 1
  579. pend = null
  580. return gen
  581. }
  582. // the action numbered g lost its emit: it waits for the server, unless a newer action has started since
  583. lose = (Number g, Hybrid what) => {
  584. if (g == gen) { pend = what }
  585. }
  586. // a song was tapped in a screen: its list becomes the queue
  587. jbPlay(Hybrid ctx, Number index, String id = null) {
  588. emit jbUnfocus()
  589. g = begin()
  590. let playCtx = ctx
  591. let at = index
  592. if (shuffleMode == 'on' && ctx.kind != 'shuffle' && id != null && id != '') {
  593. playCtx = emit server jbShuffleCtx({ kind = 'list' ctx = ctx }, id)
  594. at = 0
  595. if (playCtx == null) { lose(g, { what = 'play' ctx = ctx index = index id = id }) return null }
  596. }
  597. items = emit server jbQueue(playCtx, at, 40, id, 0, againNow())
  598. if (items == null) { lose(g, { what = 'play' ctx = ctx index = index id = id }) return null }
  599. if (items.length == 0) { return null }
  600. nowCtx = playCtx
  601. nowQueue = items
  602. showNow(items[0])
  603. return null
  604. }
  605. // a shuffle was asked for (a Shuffle button of a screen): the Shuffle switch goes on, a new random order of the pool is played from its start
  606. jbShuffle(Hybrid spec) {
  607. emit jbUnfocus()
  608. setShuffle('on')
  609. g = begin()
  610. ctx = emit server jbShuffleCtx(spec, null)
  611. if (ctx == null) { lose(g, { what = 'shuffle' spec = spec }) return null }
  612. emit jbPlay(ctx, 0)
  613. return null
  614. }
  615. // the place of a song in the queue, -1 when it is not there
  616. placeOf = (src) => {
  617. let at = -1
  618. let i = 0
  619. while (i < nowQueue.length) {
  620. if (nowQueue[i].src == src) { at = i }
  621. i = i + 1
  622. }
  623. return at
  624. }
  625. // the player picked a queue item (a tap on it, or the end of the song before): it becomes the first of the queue; the queue is refilled when fewer than 25 songs follow
  626. pickedNext = (src) => {
  627. // the player calls this for a tap on a queue entry and for the automatic advance at a song's end; only the tap is a new user action (at the end the audio has ended)
  628. a = audioEl()
  629. if (a == null || !a.ended) { begin() }
  630. at = placeOf(src)
  631. if (at < 0) { return }
  632. item = nowQueue[at]
  633. sameSrc = nowSrc
  634. nowQueue = fromPlace(at)
  635. showNow(item)
  636. if (item.src == sameSrc) { replayNow() }
  637. if (nowQueue.length < 25) {
  638. more = emit server jbQueue(nowCtx, item.idx, 40, item.id, 0, againNow())
  639. if (more != null && more.length > 0) { nowQueue = more }
  640. }
  641. }
  642. // the request of a Next or Previous that was decided already (the song before / after the one that plays, asked of the server); it waits when the server does not answer
  643. stepTo = (Number g, Hybrid step) => {
  644. items = emit server jbQueue(step.ctx, step.at, 40, step.id, step.dir, againNow())
  645. if (items == null) { lose(g, { what = 'step' ctx = step.ctx at = step.at dir = step.dir id = step.id }) return }
  646. if (items.length > 0) {
  647. nowQueue = items
  648. showNow(items[0])
  649. }
  650. }
  651. // next song: the second of the queue, or a request when the list has run out of items here
  652. on jbNext() {
  653. if (nowId == '') { return null }
  654. g = begin()
  655. if (nowQueue.length > 1) { pickedNext(nowQueue[1].src) return null }
  656. stepTo(g, { ctx = nowCtx at = nowIdx + 1 dir = 1 id = nowId })
  657. return null
  658. }
  659. // previous song: after 3 seconds into a song the first press starts it again, then the song before it in the list
  660. on jbPrev() {
  661. if (nowId == '') { return null }
  662. g = begin()
  663. let a = audioEl()
  664. if (a != null && a.currentTime > 3) { a.currentTime = 0 return null }
  665. if (nowIdx < 1) { if (a != null) { a.currentTime = 0 } return null }
  666. stepTo(g, { ctx = nowCtx at = nowIdx - 1 dir = -1 id = nowId })
  667. return null
  668. }
  669. // the buttons give the focus back, so Space keeps meaning play / pause
  670. on jbNextClick(e) { e.target.closest('button').blur() emit jbNext() return null }
  671. on jbPrevClick(e) { e.target.closest('button').blur() emit jbPrev() return null }
  672. // ---- Shuffle and Repeat ----------------------------------------------------------------------------------------------------
  673. // Repeat all: the queue never ends (asked of the server with every queue request)
  674. againNow = () => { return repeatMode == 'all' }
  675. // start the song that plays again (the same song follows itself: Repeat all on a list of one song)
  676. replayNow = () => {
  677. let a = audioEl()
  678. if (a == null) { return }
  679. a.currentTime = 0
  680. a.play()
  681. }
  682. // both switches are written into the page and kept for the next visit
  683. setShuffle = (mode) => {
  684. shuffleMode = mode
  685. emit server jbRemember(shuffleMode, repeatMode)
  686. }
  687. setRepeat = (mode) => {
  688. repeatMode = mode
  689. emit server jbRemember(shuffleMode, repeatMode)
  690. }
  691. // the plain list a shuffle was made from
  692. baseOf = (ctx) => {
  693. if (ctx.spec.kind == 'favourites') { return { kind = 'songs' sort = 'title' minPlays = ctx.spec.minPlays } }
  694. return ctx.spec.ctx
  695. }
  696. // a new queue from the song that plays on, in the given context: the song itself stays the first (its src does not change, so the audio goes on)
  697. requeue = (Number g, Hybrid ctx) => {
  698. items = emit server jbQueue(ctx, ctx.kind == 'shuffle' ? 0 : nowIdx, 40, nowId, 0, againNow())
  699. if (items == null) { lose(g, { what = 'requeue' ctx = ctx id = nowId }) return }
  700. if (items.length == 0 || items[0].id != nowId) { return }
  701. nowCtx = ctx
  702. nowQueue = items
  703. nowIdx = items[0].idx
  704. }
  705. // Shuffle on: what is still to come is shuffled from the song that plays; the song stays
  706. on jbShuffleOn() {
  707. setShuffle('on')
  708. g = begin()
  709. if (nowId != '' && nowCtx.kind != 'shuffle') {
  710. ctx = emit server jbShuffleCtx({ kind = 'list' ctx = nowCtx }, nowId)
  711. if (ctx == null) { lose(g, { what = 'shuffleOn' id = nowId }) return null }
  712. requeue(g, ctx)
  713. }
  714. return null
  715. }
  716. // Shuffle off: the list's own order goes on after the song that plays; the song stays
  717. on jbShuffleOff() {
  718. setShuffle('off')
  719. g = begin()
  720. if (nowId != '' && nowCtx.kind == 'shuffle') { requeue(g, baseOf(nowCtx)) }
  721. return null
  722. }
  723. on jbShuffleClick(e) {
  724. e.target.closest('button').blur()
  725. if (shuffleMode == 'on') { emit jbShuffleOff() } else { emit jbShuffleOn() }
  726. return null
  727. }
  728. // Repeat: off, then all, then one, then off again
  729. on jbRepeatClick(e) {
  730. e.target.closest('button').blur()
  731. setRepeat(repeatMode == 'off' ? 'all' : repeatMode == 'all' ? 'one' : 'off')
  732. g = begin()
  733. if (nowId != '') { requeue(g, nowCtx) }
  734. return null
  735. }
  736. // the server answers again: the one action that was lost runs once (a newer action had cleared it; a song that is no longer the one it was asked for is let go)
  737. retryPend = () => {
  738. if (pend == null) { return }
  739. p = pend
  740. pend = null
  741. if (p.what == 'play') { emit jbPlay(p.ctx, p.index, p.id) return }
  742. if (p.what == 'shuffle') { emit jbShuffle(p.spec) return }
  743. if (p.id != nowId) { return }
  744. if (p.what == 'step') { stepTo(begin(), p) }
  745. if (p.what == 'requeue') { requeue(begin(), p.ctx) }
  746. if (p.what == 'shuffleOn' && shuffleMode == 'on') { emit jbShuffleOn() }
  747. }
  748. on client reachability(Hybrid r) {
  749. if (r.reach == 'up') { retryPend() }
  750. return null
  751. }
  752. // a reopened socket: a server that runs another build than the page gets the page reloaded; otherwise the lost action runs
  753. on client reconnected(Hybrid r) {
  754. if (r.stale) { location.reload() return null }
  755. retryPend()
  756. return null
  757. }
  758. // a song ended with nothing after it (Repeat all on a list of one song): it plays again; with more songs queued the media player picks the next one itself
  759. on jbEnded() {
  760. if (repeatMode == 'all' && nowQueue.length == 1) { replayNow() }
  761. return null
  762. }
  763. toggleNow = () => {
  764. let a = audioEl()
  765. if (a == null || nowId == '') { return }
  766. if (a.paused) { a.play() } else { a.pause() }
  767. }
  768. jumpBy = (seconds) => {
  769. let a = audioEl()
  770. if (a == null || nowId == '') { return }
  771. let t = a.currentTime + seconds
  772. if (t < 0) { t = 0 }
  773. if (a.duration > 0 && t > a.duration) { t = a.duration }
  774. a.currentTime = t
  775. }
  776. // ---- the overlay ---------------------------------------------------------------------------------------------------------------
  777. on jbOpen() {
  778. if (nowId == '') { return null }
  779. overlay = 'true'
  780. setBar()
  781. return null
  782. }
  783. on jbClose(e) {
  784. if (e.target != null && e.target.closest != null && e.target.closest('button') != null) { e.target.closest('button').blur() }
  785. overlay = 'false'
  786. setBar()
  787. return null
  788. }
  789. // a tap on the bar (not on a button or the seek line) opens the overlay
  790. on jbBarClick(e) {
  791. if (overlay == 'true') { return null }
  792. if (e.target.closest('button, mp-seek') != null) { return null }
  793. emit jbOpen()
  794. return null
  795. }
  796. // a swipe down over the cover or the title closes it (not over the buttons, the seek line or the queue, which scroll)
  797. on jbDown(e) {
  798. if (overlay != 'true' || e.target.closest('button, mp-seek, ol') != null) { swipeY = -1 return null }
  799. swipeY = e.clientY
  800. return null
  801. }
  802. on jbUp(e) {
  803. if (swipeY >= 0 && e.clientY - swipeY > 80) { overlay = 'false' setBar() }
  804. swipeY = -1
  805. return null
  806. }
  807. on jbCancel(e) { swipeY = -1 return null }
  808. // ---- counting, keys and the lock screen -----------------------------------------------------------------------------------
  809. // every second: the seconds the audio really played; at half the song or 4 minutes the server counts the play, once
  810. on jbTick() {
  811. let a = audioEl()
  812. if (a == null || nowId == '' || a.paused || a.ended) { return null }
  813. heard = heard + 1
  814. let need = 240
  815. if (a.duration > 0 && a.duration / 2 < 240) { need = a.duration / 2 }
  816. if (heard >= need && countedId != nowId) {
  817. countedId = nowId
  818. emit server jbPlayed(nowId)
  819. }
  820. return null
  821. }
  822. // the mount tick (the media player's trick): once the page is live the timer, the keys and the lock screen's buttons are set up
  823. on jbMount(e) {
  824. e.target.style.animationName = 'none'
  825. if (ticking == 'true') { return null }
  826. ticking = 'true'
  827. window.setInterval(() => { emit jbTick() }, 1000)
  828. document.addEventListener('ended', (ev) => { emit jbEnded() }, true)
  829. let ms = window.navigator.mediaSession
  830. if (ms != null) {
  831. ms.setActionHandler('play', () => { toggleNow() })
  832. ms.setActionHandler('pause', () => { toggleNow() })
  833. ms.setActionHandler('previoustrack', () => { emit jbPrev() })
  834. ms.setActionHandler('nexttrack', () => { emit jbNext() })
  835. }
  836. return null
  837. }
  838. // Keep the shell's own event calls local.
  839. on jbPlay(Hybrid ctx, Number index, String id) { return jbPlay(ctx, index, id) }
  840. on jbShuffle(Hybrid spec) { return jbShuffle(spec) }

Branches

Latest commits

  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre