gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitba30727aba30727afile-upload: nest Style rules in the language's formmreba30727a/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

  • 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
  • 0b3fd8c9comments: nest Style rules in the language's formmre
  • 67e043c8plugins/web/README.md from hybriel cb9e480dmre
  • 519b56e9styles.hl: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 29f63d01md-editor: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 869f49c7files-upload: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • f60dab20month-view: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 5b3c349aRe-vendor to Hybriel set day35 (cb9e480d): plugins core and web sources, README vendoring notemre
  • 3449b117components: a startup error prints its message before exit 1 (hybriel#247)mre
  • 59704ce1Merge branch 'n5b-pipes'mre
  • 23b9164aMdEditor: a clipboard whose text is only the names or file:// paths of its files (a file manager copy) is about the files, as before; any other text wins (tickets#49); gate casesmre
  • b0f1ec28MdEditor: a clipboard with text always pastes the text, also when a picture is on it too (a spreadsheet or office app), files on or off; a picture alone still goes to the files; real Ctrl+V gate tests/md-paste-files.mjs (tickets#49)mre
  • ad4e929bMdEditor: pipe rows without a delimiter row are a table (first row = header, short rows padded, written back without an invented | --- | line) (notes#5)mre
  • 4bd11128MdEditor: the Table button's empty table is written as a real pipe table (empty cells), so it survives the source switch, a save and a reload (notes#7)mre
  • 7437e32bMdEditor draws its surface at once when claimed from server HTML: 1 ms mount animation, the first editor mounts all editors of the pagemre
  • 5de0264cRe-vendor onto Hybriel master acaceaf3 (set-day27b): binary, lib and plugins; exit 1 on a startup fault; .gitignore binmre