gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3eb6695f3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre3eb6695f/components/main.hl

30.0 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 `emit 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 `emit 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. on jbPlay(ctx, index, id) {
  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. on jbShuffle(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. begin()
  628. at = placeOf(src)
  629. if (at < 0) { return }
  630. item = nowQueue[at]
  631. sameSrc = nowSrc
  632. nowQueue = fromPlace(at)
  633. showNow(item)
  634. if (item.src == sameSrc) { replayNow() }
  635. if (nowQueue.length < 25) {
  636. more = emit server jbQueue(nowCtx, item.idx, 40, item.id, 0, againNow())
  637. if (more != null && more.length > 0) { nowQueue = more }
  638. }
  639. }
  640. // 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
  641. stepTo = (Number g, Hybrid step) => {
  642. items = emit server jbQueue(step.ctx, step.at, 40, step.id, step.dir, againNow())
  643. if (items == null) { lose(g, { what = 'step' ctx = step.ctx at = step.at dir = step.dir id = step.id }) return }
  644. if (items.length > 0) {
  645. nowQueue = items
  646. showNow(items[0])
  647. }
  648. }
  649. // next song: the second of the queue, or a request when the list has run out of items here
  650. on jbNext() {
  651. if (nowId == '') { return null }
  652. g = begin()
  653. if (nowQueue.length > 1) { pickedNext(nowQueue[1].src) return null }
  654. stepTo(g, { ctx = nowCtx at = nowIdx + 1 dir = 1 id = nowId })
  655. return null
  656. }
  657. // previous song: after 3 seconds into a song the first press starts it again, then the song before it in the list
  658. on jbPrev() {
  659. if (nowId == '') { return null }
  660. g = begin()
  661. let a = audioEl()
  662. if (a != null && a.currentTime > 3) { a.currentTime = 0 return null }
  663. if (nowIdx < 1) { if (a != null) { a.currentTime = 0 } return null }
  664. stepTo(g, { ctx = nowCtx at = nowIdx - 1 dir = -1 id = nowId })
  665. return null
  666. }
  667. // the buttons give the focus back, so Space keeps meaning play / pause
  668. on jbNextClick(e) { e.target.closest('button').blur() emit jbNext() return null }
  669. on jbPrevClick(e) { e.target.closest('button').blur() emit jbPrev() return null }
  670. // ---- Shuffle and Repeat ----------------------------------------------------------------------------------------------------
  671. // Repeat all: the queue never ends (asked of the server with every queue request)
  672. againNow = () => { return repeatMode == 'all' }
  673. // start the song that plays again (the same song follows itself: Repeat all on a list of one song)
  674. replayNow = () => {
  675. let a = audioEl()
  676. if (a == null) { return }
  677. a.currentTime = 0
  678. a.play()
  679. }
  680. // both switches are written into the page and kept for the next visit
  681. setShuffle = (mode) => {
  682. shuffleMode = mode
  683. emit server jbRemember(shuffleMode, repeatMode)
  684. }
  685. setRepeat = (mode) => {
  686. repeatMode = mode
  687. emit server jbRemember(shuffleMode, repeatMode)
  688. }
  689. // the plain list a shuffle was made from
  690. baseOf = (ctx) => {
  691. if (ctx.spec.kind == 'favourites') { return { kind = 'songs' sort = 'title' minPlays = ctx.spec.minPlays } }
  692. return ctx.spec.ctx
  693. }
  694. // 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)
  695. requeue = (Number g, Hybrid ctx) => {
  696. items = emit server jbQueue(ctx, ctx.kind == 'shuffle' ? 0 : nowIdx, 40, nowId, 0, againNow())
  697. if (items == null) { lose(g, { what = 'requeue' ctx = ctx id = nowId }) return }
  698. if (items.length == 0 || items[0].id != nowId) { return }
  699. nowCtx = ctx
  700. nowQueue = items
  701. nowIdx = items[0].idx
  702. }
  703. // Shuffle on: what is still to come is shuffled from the song that plays; the song stays
  704. on jbShuffleOn() {
  705. setShuffle('on')
  706. g = begin()
  707. if (nowId != '' && nowCtx.kind != 'shuffle') {
  708. ctx = emit server jbShuffleCtx({ kind = 'list' ctx = nowCtx }, nowId)
  709. if (ctx == null) { lose(g, { what = 'shuffleOn' id = nowId }) return null }
  710. requeue(g, ctx)
  711. }
  712. return null
  713. }
  714. // Shuffle off: the list's own order goes on after the song that plays; the song stays
  715. on jbShuffleOff() {
  716. setShuffle('off')
  717. g = begin()
  718. if (nowId != '' && nowCtx.kind == 'shuffle') { requeue(g, baseOf(nowCtx)) }
  719. return null
  720. }
  721. on jbShuffleClick(e) {
  722. e.target.closest('button').blur()
  723. if (shuffleMode == 'on') { emit jbShuffleOff() } else { emit jbShuffleOn() }
  724. return null
  725. }
  726. // Repeat: off, then all, then one, then off again
  727. on jbRepeatClick(e) {
  728. e.target.closest('button').blur()
  729. setRepeat(repeatMode == 'off' ? 'all' : repeatMode == 'all' ? 'one' : 'off')
  730. g = begin()
  731. if (nowId != '') { requeue(g, nowCtx) }
  732. return null
  733. }
  734. // 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)
  735. retryPend = () => {
  736. if (pend == null) { return }
  737. p = pend
  738. pend = null
  739. if (p.what == 'play') { emit jbPlay(p.ctx, p.index, p.id) return }
  740. if (p.what == 'shuffle') { emit jbShuffle(p.spec) return }
  741. if (p.id != nowId) { return }
  742. if (p.what == 'step') { stepTo(begin(), p) }
  743. if (p.what == 'requeue') { requeue(begin(), p.ctx) }
  744. if (p.what == 'shuffleOn' && shuffleMode == 'on') { emit jbShuffleOn() }
  745. }
  746. on client reachability(Hybrid r) {
  747. if (r.reach == 'up') { retryPend() }
  748. return null
  749. }
  750. // a reopened socket: a server that runs another build than the page gets the page reloaded; otherwise the lost action runs
  751. on client reconnected(Hybrid r) {
  752. if (r.stale) { location.reload() return null }
  753. retryPend()
  754. return null
  755. }
  756. // 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
  757. on jbEnded() {
  758. if (repeatMode == 'all' && nowQueue.length == 1) { replayNow() }
  759. return null
  760. }
  761. toggleNow = () => {
  762. let a = audioEl()
  763. if (a == null || nowId == '') { return }
  764. if (a.paused) { a.play() } else { a.pause() }
  765. }
  766. jumpBy = (seconds) => {
  767. let a = audioEl()
  768. if (a == null || nowId == '') { return }
  769. let t = a.currentTime + seconds
  770. if (t < 0) { t = 0 }
  771. if (a.duration > 0 && t > a.duration) { t = a.duration }
  772. a.currentTime = t
  773. }
  774. // ---- the overlay ---------------------------------------------------------------------------------------------------------------
  775. on jbOpen() {
  776. if (nowId == '') { return null }
  777. overlay = 'true'
  778. setBar()
  779. return null
  780. }
  781. on jbClose(e) {
  782. if (e.target != null && e.target.closest != null && e.target.closest('button') != null) { e.target.closest('button').blur() }
  783. overlay = 'false'
  784. setBar()
  785. return null
  786. }
  787. // a tap on the bar (not on a button or the seek line) opens the overlay
  788. on jbBarClick(e) {
  789. if (overlay == 'true') { return null }
  790. if (e.target.closest('button, mp-seek') != null) { return null }
  791. emit jbOpen()
  792. return null
  793. }
  794. // a swipe down over the cover or the title closes it (not over the buttons, the seek line or the queue, which scroll)
  795. on jbDown(e) {
  796. if (overlay != 'true' || e.target.closest('button, mp-seek, ol') != null) { swipeY = -1 return null }
  797. swipeY = e.clientY
  798. return null
  799. }
  800. on jbUp(e) {
  801. if (swipeY >= 0 && e.clientY - swipeY > 80) { overlay = 'false' setBar() }
  802. swipeY = -1
  803. return null
  804. }
  805. on jbCancel(e) { swipeY = -1 return null }
  806. // ---- counting, keys and the lock screen -----------------------------------------------------------------------------------
  807. // every second: the seconds the audio really played; at half the song or 4 minutes the server counts the play, once
  808. on jbTick() {
  809. let a = audioEl()
  810. if (a == null || nowId == '' || a.paused || a.ended) { return null }
  811. heard = heard + 1
  812. let need = 240
  813. if (a.duration > 0 && a.duration / 2 < 240) { need = a.duration / 2 }
  814. if (heard >= need && countedId != nowId) {
  815. countedId = nowId
  816. emit server jbPlayed(nowId)
  817. }
  818. return null
  819. }
  820. // 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
  821. on jbMount(e) {
  822. e.target.style.animationName = 'none'
  823. if (ticking == 'true') { return null }
  824. ticking = 'true'
  825. window.setInterval(() => { emit jbTick() }, 1000)
  826. document.addEventListener('ended', (ev) => { emit jbEnded() }, true)
  827. let ms = window.navigator.mediaSession
  828. if (ms != null) {
  829. ms.setActionHandler('play', () => { toggleNow() })
  830. ms.setActionHandler('pause', () => { toggleNow() })
  831. ms.setActionHandler('previoustrack', () => { emit jbPrev() })
  832. ms.setActionHandler('nexttrack', () => { emit jbNext() })
  833. }
  834. return null
  835. }

Branches

Latest commits

  • 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
  • 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
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox
  • 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
  • 29d2029cRecords: PR-only rule (Kunibert)mre
  • adacb0abRecords: neutral ticket rule, #22 decision recordedmre
  • b2d6f121Records: #308 hold liftedmre
  • 7ab67533Records: day39 on hold (hybriel#308)mre
  • 22f21739Records: note rebase before mergemre
  • f7368ea3Records: mission 011 day39, waits for lisbethmre
  • 9515d2cdRecords: jb20 livemre