gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmain0f6d1592Merge branch 'f18b-shell-session' into mainIdentmain/components/identityform.hl

8.0 KB

  1. // components/identityform.hl — THE IDENTITY FORM (ident#40.3), placed by components/home.hl while it is open: a new identity, an edit,
  2. // and right after the first login the default identity ("welcome": the names are optional, Skip closes it). The host decides what
  3. // the form shows and passes it in: the heading, whether it is the welcome form, which identity (editId, or welcomeId for the welcome
  4. // form; '' = a new one) and the starting values (the f… fields). This component owns the typing, the avatar picture and the save
  5. // (faces addIdentity and editIdentity, lib/identities.hl). Host assignments reset the starting values when the form opens.
  6. // The host shows the messages: $parent.status(message, notice);
  7. // $parent.formSaved(rows, text) takes the new list after a create (a new identity changes the other rows: the first gets its Delete button, and the order is the server's), $parent.rowSaved(row, text) the one edited row, $parent.closeForm() closes the form (Skip, Cancel).
  8. import { accountOfSession } from '../lib/accounts.hl'
  9. import { identityRows, identityRow, createIdentity, updateIdentity } from '../lib/identities.hl'
  10. import { leadLook } from './styles.hl'
  11. editHeading = ''
  12. welcome = false
  13. notWelcome = !welcome // `if (!welcome)` in a View renders nothing: an `if` takes a member
  14. editId = ''
  15. welcomeId = ''
  16. fIdentityName = ''
  17. fNickname = ''
  18. fFirstname = ''
  19. fLastname = ''
  20. fAvatar = ''
  21. avatarNote = ''
  22. hasAvatarPreview = false
  23. // the preview and the Remove button are hidden (the attribute) while there is no picture
  24. noAvatarPreview = !hasAvatarPreview
  25. Style {
  26. pLead = leadLook
  27. avatarField {
  28. display = 'flex'
  29. flexWrap = 'wrap'
  30. alignItems = 'center'
  31. gap = '.6rem'
  32. // real Chrome: a file input has an intrinsic width (its "No file chosen" text) that pushed the form to 413 px at 390: every item may shrink
  33. label {
  34. flex = '1 1 12rem'
  35. minWidth = '0'
  36. }
  37. }
  38. '#avatarpreview' {
  39. width = '48px'
  40. height = '48px'
  41. objectFit = 'cover'
  42. borderRadius = '50%'
  43. background = 'var(--color-background-sunken)'
  44. }
  45. '#identityform input[type=file]' {
  46. width = '100%'
  47. maxWidth = '100%'
  48. minWidth = '0'
  49. }
  50. '#avatarnote' {
  51. flexBasis = '100%'
  52. color = 'var(--color-danger)'
  53. fontSize = '.875rem'
  54. empty { display = 'none' }
  55. }
  56. }
  57. View {
  58. form { id = "identityform"
  59. on submit(e) {
  60. e.preventDefault()
  61. emit saveForm(e)
  62. }
  63. h1 { id = "formheading" editHeading }
  64. if (welcome) {
  65. p { id = "welcome" Style.pLead "Your account is ready and has a default identity. All of these fields are " strong { "optional" } " — fill in what you like, or skip it and do it later." }
  66. }
  67. label { "Identity name " fieldHint { "(shown when you pick an identity in an app; optional)" }
  68. input { id = "fidentityname" name = "identityName" maxlength = "60" autocomplete = "off" value = fIdentityName on input(e) { emit setF('identityName', e.target.value) } }
  69. }
  70. label { "Nickname " fieldHint { "(optional)" }
  71. input { id = "fnickname" name = "nickname" maxlength = "60" autocomplete = "nickname" value = fNickname on input(e) { emit setF('nickname', e.target.value) } }
  72. }
  73. label { "First name " fieldHint { "(optional)" }
  74. input { id = "ffirstname" name = "firstname" maxlength = "60" autocomplete = "given-name" value = fFirstname on input(e) { emit setF('firstname', e.target.value) } }
  75. }
  76. label { "Last name " fieldHint { "(optional)" }
  77. input { id = "flastname" name = "lastname" maxlength = "60" autocomplete = "family-name" value = fLastname on input(e) { emit setF('lastname', e.target.value) } }
  78. }
  79. avatarField {
  80. img { id = "avatarpreview" hidden = noAvatarPreview src = fAvatar alt = "" width = "48" height = "48" }
  81. label { "Avatar " fieldHint { "(optional; a picture apps can show, e.g. next to your comments)" }
  82. input { id = "favatarfile" type = "file" accept = "image/png,image/jpeg,image/webp,image/gif" on change(Hybrid e) { pickAvatar(e) } }
  83. }
  84. input { id = "favatar" name = "avatar" type = "hidden" value = fAvatar on input(Hybrid e) { emit setF('avatar', e.target.value) } }
  85. button { id = "avatarremove" type = "button" class = "quiet small" hidden = noAvatarPreview "Remove picture" on click(e) { emit setF('avatar', '') } }
  86. span { id = "avatarnote" avatarNote }
  87. }
  88. formButtons {
  89. button { id = "saveidentity" type = "submit" "Save" }
  90. if (welcome) { button { id = "skip" type = "button" class = "quiet" "Skip" on click(e) { $parent.closeForm() } } }
  91. if (notWelcome) { button { id = "cancel" type = "button" class = "quiet" "Cancel" on click(e) { $parent.closeForm() } } }
  92. }
  93. }
  94. }
  95. // Browser constructors are documented in COMPONENTS §6b. Only members carry form state.
  96. pickAvatar(Hybrid e) {
  97. input = e.target
  98. file = input.files[0]
  99. if (file == null) { return null }
  100. avatarNote = ''
  101. if (!['image/png', 'image/jpeg', 'image/webp', 'image/gif'].includes(file.type)) {
  102. avatarNote = 'That is not a picture (PNG, JPEG, WebP or GIF).'
  103. input.value = ''
  104. return null
  105. }
  106. if (file.size > 20 * 1024 * 1024) {
  107. avatarNote = 'That picture is too big (at most 20 MB).'
  108. input.value = ''
  109. return null
  110. }
  111. src = URL.createObjectURL(file)
  112. image = new Image()
  113. image.onerror = (Hybrid event) => {
  114. URL.revokeObjectURL(src)
  115. avatarNote = 'That picture could not be read.'
  116. input.value = ''
  117. }
  118. image.onload = (Hybrid event) => {
  119. URL.revokeObjectURL(src)
  120. side = Math.min(image.naturalWidth, image.naturalHeight)
  121. if (!side) { avatarNote = 'That picture could not be read.' return null }
  122. canvas = new OffscreenCanvas(128, 128)
  123. cx = canvas.getContext('2d')
  124. cx.drawImage(image, (image.naturalWidth - side) / 2, (image.naturalHeight - side) / 2, side, side, 0, 0, 128, 128)
  125. blob = canvas.convertToBlob({ type = 'image/webp' quality = 0.85 })
  126. reader = new FileReader()
  127. reader.onload = (Hybrid event) => {
  128. url = event.target.result
  129. if (!url.startsWith('data:image/webp') && url.length > 60000) {
  130. fallback = canvas.convertToBlob({ type = 'image/jpeg' quality = 0.85 })
  131. jpegReader = new FileReader()
  132. jpegReader.onload = (Hybrid event) => { acceptAvatar(event.target.result, input) }
  133. jpegReader.readAsDataURL(fallback)
  134. return null
  135. }
  136. acceptAvatar(url, input)
  137. }
  138. reader.readAsDataURL(blob)
  139. }
  140. image.src = src
  141. }
  142. acceptAvatar(String url, Hybrid input) {
  143. if (url.length > 60000) {
  144. avatarNote = 'That picture is too complex — try another one.'
  145. return null
  146. }
  147. fAvatar = url
  148. hasAvatarPreview = true
  149. input.value = ''
  150. }
  151. on setF(name, v) {
  152. if (name == 'identityName') { fIdentityName = v }
  153. if (name == 'nickname') { fNickname = v }
  154. if (name == 'firstname') { fFirstname = v }
  155. if (name == 'lastname') { fLastname = v }
  156. if (name == 'avatar') { fAvatar = v hasAvatarPreview = v.trim() != '' }
  157. }
  158. on saveForm(e) {
  159. $parent.status('', '')
  160. fields = { identityName = fIdentityName nickname = fNickname firstname = fFirstname lastname = fLastname avatar = fAvatar }
  161. let r = null
  162. if (editId == '' && welcome) {
  163. r = emit server editIdentity('' + welcomeId, fields)
  164. } else if (editId == '') {
  165. r = emit server addIdentity(fields)
  166. } else {
  167. r = emit server editIdentity('' + editId, fields)
  168. }
  169. if (r == null) {
  170. $parent.status('the server did not answer — try again', '')
  171. return null
  172. }
  173. if (r.error != null) {
  174. $parent.status(r.error, '')
  175. return null
  176. }
  177. if (r.row != null) {
  178. $parent.rowSaved(r.row, 'Identity saved.')
  179. return null
  180. }
  181. $parent.formSaved(r.identities, 'Identity created.')
  182. }
  183. // A face runs on a BLANK instance (its arguments and its imports only); the account comes from the session.
  184. on server addIdentity(fields, session) {
  185. let me = accountOfSession(&session)
  186. if (me == null) { return { error = 'you are not signed in' } }
  187. r = createIdentity(me.id, fields)
  188. if (r.error != null) { return r }
  189. return { identity = r.identity identities = identityRows(me.id) }
  190. }
  191. on server editIdentity(id, fields, session) {
  192. let me = accountOfSession(&session)
  193. if (me == null) { return { error = 'you are not signed in' } }
  194. r = updateIdentity(me.id, id, fields)
  195. if (r.error != null) { return r }
  196. return { identity = r.identity row = identityRow(me.id, r.identity) }
  197. }

Branches

Latest commits

  • 0f6d1592Merge branch 'f18b-shell-session' into mainIdent
  • 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
  • 5d1b3fedMerge branch 'v61-day61' into mainIdent
  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre