gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchday39cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmreday39/components/main.hl

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

Branches

Latest commits

  • 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
  • 277a2722README: provenance linemre
  • 4169ca6aREADME: sync pinmre
  • a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
  • f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
  • d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
  • df7920ceRecords: R3 startedmre
  • 0549f038Mission 010: jb20 livemre
  • 273494e2STATUS rewrittenmre