gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitadfe2c4dadfe2c4dAdd shuffle, repeat and repeatOne shared iconsmreadfe2c4d/components/file-upload/file-upload.hl

5.7 KB

  1. // file-upload.hl — THE FILE UPLOAD (one file), pure Hybriel: a drop zone with a "Choose a file" button. The chosen or dropped file goes
  2. // to the app through `emit server saveFile(file)` (the framework carries it over HTTP, in parts, with progress: hybriel#94); the app's
  3. // `on server saveFile(file)` decides where it is stored and answers anything but null when it took it (a face that raises an error or
  4. // answers null = the upload failed). Name, size and progress show while it goes; a finished file is listed under "Uploaded".
  5. // One file at a time: a drop of several takes the first, a file dropped during an upload is ignored. USE ONE PER PAGE (a composed
  6. // component's members are the page's state). The page is written by DOM calls in the handlers: the emit blocks its handler, and
  7. // `on client uploadProgress` paints the bar meanwhile.
  8. String dropText = 'Drop a file here or'
  9. String chooseText = 'Choose a file'
  10. String uploadedTitle = 'Uploaded'
  11. sizeText(n) {
  12. if (n < 1024) { return n + ' B' }
  13. if (n < 1048576) { return (n / 1024).toFixed(1) + ' KB' }
  14. return (n / 1048576).toFixed(1) + ' MB'
  15. }
  16. View {
  17. fileUpload {
  18. dropZone { on dragover(e) { emit uploadOver(e) } on dragleave(e) { emit uploadLeave(e) } on drop(e) { emit uploadDrop(e) }
  19. p { dropText }
  20. label { class = "choose" chooseText input { type = "file" on change(e) { emit uploadChosen(e) } } }
  21. uploadStatus { 'aria-live' = "polite"
  22. strong { class = "name" }
  23. span { class = "size" }
  24. progress { max = "100" value = "0" }
  25. output { class = "state" }
  26. }
  27. }
  28. uploadedList {
  29. h2 { uploadedTitle }
  30. ul { class = "uploaded" }
  31. }
  32. }
  33. }
  34. rootOf(node) { return node.closest('file-upload') }
  35. on uploadOver(e) {
  36. e.preventDefault()
  37. rootOf(e.target).classList.add('over')
  38. }
  39. on uploadLeave(e) { rootOf(e.target).classList.remove('over') }
  40. on uploadDrop(e) {
  41. e.preventDefault()
  42. root = rootOf(e.target)
  43. root.classList.remove('over')
  44. if (e.dataTransfer.files.length > 0) { send(root, e.dataTransfer.files[0]) }
  45. }
  46. on uploadChosen(e) {
  47. root = rootOf(e.target)
  48. input = e.target
  49. if (input.files.length > 0) { send(root, input.files[0]) }
  50. input.value = ''
  51. }
  52. on client uploadProgress(p) {
  53. for (root of document.querySelectorAll('file-upload.busy')) {
  54. let bar = root.querySelector('progress')
  55. bar.max = p.size > 0 ? p.size : 1
  56. bar.value = p.sent
  57. }
  58. }
  59. send(root, file) {
  60. if (root.classList.contains('busy')) { return null }
  61. root.classList.add('busy')
  62. root.classList.remove('done')
  63. root.classList.remove('failed')
  64. root.classList.add('shown')
  65. root.querySelector('.name').textContent = file.name
  66. root.querySelector('.size').textContent = sizeText(file.size)
  67. bar = root.querySelector('progress')
  68. bar.max = 100
  69. bar.value = 0
  70. state = root.querySelector('.state')
  71. state.textContent = 'Uploading…'
  72. saved = emit server saveFile(file)
  73. root.classList.remove('busy')
  74. if (saved == null) {
  75. root.classList.add('failed')
  76. state.textContent = 'Failed'
  77. return null
  78. }
  79. bar.max = 100
  80. bar.value = 100
  81. root.classList.add('done')
  82. state.textContent = 'Uploaded'
  83. li = document.createElement('li')
  84. li.textContent = file.name + ' — ' + sizeText(file.size)
  85. root.querySelector('ul.uploaded').appendChild(li)
  86. }
  87. // ---- look: the component's own Style; the base look is black, white and the greys #555 #999 #ddd #f2f2f2 (shared/base.hl names them;
  88. // README "Look"); every colour is a semantic token with its base value as fallback, so an app that declares the tokens (layouts.worldapi.org's
  89. // theme) restyles it. No frames: the one border is the drop zone's dashed one (it means "drop here")
  90. Style {
  91. fileUpload {
  92. display = 'block'
  93. color = 'var(--color-text, #000)'
  94. dropZone {
  95. display = 'block'
  96. padding = '1rem'
  97. textAlign = 'center'
  98. border = '2px dashed var(--color-border, #999)'
  99. borderRadius = '.5rem'
  100. p {
  101. margin = '0 0 .5rem'
  102. color = 'var(--color-text-muted, #555)'
  103. }
  104. }
  105. // Class or attribute assigned by the View or code; no compound-selector form.
  106. 'label.choose' {
  107. display = 'inline-block'
  108. padding = '.35rem .8rem'
  109. cursor = 'pointer'
  110. border = '0'
  111. borderRadius = '.35rem'
  112. background = 'var(--color-surface, #f2f2f2)'
  113. hover { filter = 'brightness(.93)' }
  114. input { display = 'none' }
  115. }
  116. uploadStatus {
  117. display = 'none'
  118. marginTop = '.8rem'
  119. textAlign = 'left'
  120. // Class or attribute assigned by the View or code; no compound-selector form.
  121. '.name' { overflowWrap = 'anywhere' }
  122. // Class or attribute assigned by the View or code; no compound-selector form.
  123. '.size' {
  124. marginLeft = '.6rem'
  125. color = 'var(--color-text-muted, #555)'
  126. }
  127. // Class or attribute assigned by the View or code; no compound-selector form.
  128. '.state' { fontSize = '.9rem' }
  129. }
  130. progress {
  131. display = 'block'
  132. width = '100%'
  133. margin = '.4rem 0'
  134. accentColor = 'var(--color-accent, #000)'
  135. }
  136. uploadedList {
  137. h2 {
  138. margin = '1rem 0 .3rem'
  139. fontSize = '1rem'
  140. }
  141. }
  142. // Class or attribute assigned by the View or code; no compound-selector form.
  143. 'ul.uploaded' {
  144. margin = '0'
  145. paddingLeft = '1.2rem'
  146. }
  147. }
  148. // No language form for class or attribute on the component root.
  149. 'file-upload.over drop-zone' {
  150. borderColor = 'var(--color-accent, #000)'
  151. background = 'var(--color-surface, #f2f2f2)'
  152. }
  153. // No language form for class or attribute on the component root.
  154. 'file-upload.shown upload-status' { display = 'block' }
  155. // No language form for class or attribute on the component root.
  156. 'file-upload.done .state' { color = 'var(--color-success, #000)' }
  157. // No language form for class or attribute on the component root.
  158. 'file-upload.failed .state' { color = 'var(--color-danger, #000)' }
  159. // No language form for pseudo-element.
  160. 'file-upload ul.uploaded:empty::after' {
  161. content = '"Nothing yet."'
  162. marginLeft = '-1.2rem'
  163. color = 'var(--color-text-muted, #555)'
  164. }
  165. }

Branches

Latest commits

  • adfe2c4dAdd shuffle, repeat and repeatOne shared iconsmre
  • c96709c8media-player: skip = false and repeat = 'one' options (jukebox#19)mre
  • bd4aa808Record VL24 deployment and live verificationmre
  • e5b98ecfRun virtual-list gate before deploymentmre
  • 2e9b4a8evirtual-list: into main, with centerId and scrollTop/onScroll (jukebox#20)mre
  • 69954249Align all plugin sources with Hybriel day38bmre
  • f740f666Re-vendor components to Hybriel set day38bmre
  • 8176eb14Record CS23b deployment and live verificationmre
  • 84e3ddaaRemove blank line before pagination Style closing bracemre
  • a92298ddRecord exact CSS and same-date proof after specificity reviewmre
  • c71a0abePreserve consumer theme specificity with nested tag rulesmre
  • 64a2bc27Document CS23b style formatting proof and review statusmre
  • a72b57cficons: nest Style rules in the language's formmre
  • 927616e2pagination: nest Style rules in the language's formmre
  • aab8ad29code-editor: nest Style rules in the language's formmre
  • ba30727afile-upload: nest Style rules in the language's formmre
  • 42fefa4emodal: nest Style rules in the language's formmre
  • 6fd953e3range-picker: nest Style rules in the language's formmre
  • f661951ddate-picker: nest Style rules in the language's formmre
  • 363577e6media-player: nest Style rules in the language's formmre