gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0549f0380549f038Mission 010: jb20 livemre0549f038/components/main.hl

27.4 KB

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

Branches

Latest commits

  • 0549f038Mission 010: jb20 livemre
  • 273494e2STATUS rewrittenmre
  • 227c9b96Records: jb19 livemre
  • 9f365376README: the bar's controls (jb19)mre
  • d18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mre
  • 82b54c0ejb19: bar Shuffle · Previous · Play · Next · Repeat, no 10 s buttons; Shuffle switch (shuffle from the playing song, back to the list), Repeat off/all/one, both remembered in ui.json; Shuffle my favourites an ordinary button; gate tests/jb19.mjsmre
  • ba354710jb19: icons pinned to components.hybriel 46fefaa (shuffle, repeat, repeatOne, prevTrack, nextTrack)mre
  • d8e8d059jb19 step 2: media-player from components.hybriel c96709c (skip, repeat options; the end of a song only calls onPlay), sync-components.sh and SOURCE point to it, jb11 counts media loads and metadata writes (from jb21 d3a026e)mre
  • 7b048c1bRecords: jb19 done, L2 live stepmre
  • 665185aeMission 009: jb19 livemre
  • 47b2d3beRecords: P1 resumedmre
  • 8e326e94Records: resetKey blockedmre
  • f72dd7edRecords: icons comingmre
  • b264e5deRecords: P1 icons stopmre
  • 514d7cbeRecords: mission 008 startedmre
  • 61c40127Mission 008: #19 + #21mre
  • 6dcaeb46Records: resetKey planmre
  • c5dbf2ecRecords: R2 resultmre
  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre