gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7a53c0c27a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre7a53c0c2/components/main.hl

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

Branches

Latest commits

  • 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
  • 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre