todo-webclient/client/project-creation-modal.ls

224 lines
5.0 KiB
Plaintext
Raw Normal View History

2019-12-06 21:07:39 +01:00
h = require 'maquette' .h
2019-12-06 21:07:39 +01:00
Modal = require './modal.ls'
UUID = require "uuid/v4"
bulma = require "./bulma.ls"
2019-12-06 21:07:39 +01:00
2019-12-12 02:30:46 +01:00
{field, control, label, button, tag, input, select} = bulma
2019-12-08 02:17:55 +01:00
col-to-lines = (column, self) ->
2019-12-12 02:30:46 +01:00
field \.has-addons {
key: "column.#{column.id}"
2019-12-08 02:17:55 +01:00
} [
2019-12-12 02:30:46 +01:00
control \.is-expanded [
input {
2019-12-08 02:17:55 +01:00
key: "input" + column.id
value: column.title
oninput: (e) ->
self.extra_properties.columns.find((.id == column.id)).title := e.target.value
2019-12-12 02:30:46 +01:00
}
2019-12-08 02:17:55 +01:00
]
2019-12-12 02:30:46 +01:00
control [
button \.is-danger.is-outlined {
key: "button" + column.id
onclick: ->
self.extra_properties.columns := self.extra_properties.columns.filter((.id != column.id))
} [ "DELETE" ]
]
2019-12-08 02:17:55 +01:00
]
user-form-selection = (self, user) ->
h \option {
2019-12-10 05:59:52 +01:00
value: user.uid
} [ user.login ]
2019-12-10 05:59:52 +01:00
permission-groups =
2019-12-11 00:10:58 +01:00
"read"
"post"
"edit"
"admin"
permissions-add = (self, permission, user-id) ->
2019-12-11 00:10:58 +01:00
perm-list = self.permissions[permission]
if perm-list
is-already-there = perm-list.find (e) -> (""+ e) == ("" + user-id)
if is-already-there
console.log "user #{user-id} already in #{perm-list}"
else
2019-12-11 00:10:58 +01:00
perm-list ++= [ parseInt(user-id) ]
self.permissions[permission] := perm-list
# console.log "adding user #{user-id} to #{perm-list[0]}: #{perm-list}"
else
console.log "Cannot find #{permission} permissions, creating it"
2019-12-11 00:10:58 +01:00
self.permissions[permission] := user-id
permission-to-form-selection = (self, permission) ->
h \option {
value: permission
} [ permission ]
2019-12-08 02:17:55 +01:00
2019-12-10 05:59:52 +01:00
ProjectCreationModal = (project, todod-ws, users) ->
# work on a copy of the columns
# in case of cancelled modifications, only the copies are changed
columns-copy = []
for col in project.extra_properties.columns
new-col = {}
for k,v of col
new-col[k] = v
columns-copy ++= [ new-col ]
2019-12-06 21:07:39 +01:00
self = {
title: project.title || ""
2019-12-11 00:10:58 +01:00
permissions: project.permissions || {admin: [], edit: [], post: [], read: []}
2019-12-10 05:59:52 +01:00
tmp:
new-user-permission:
id: void
permission: permission-groups[0]
new-column-input:
title: "New column !"
users: users || []
2019-12-06 21:07:39 +01:00
extra_properties:
columns: columns-copy
2019-12-06 21:07:39 +01:00
}
modal = Modal {
+visible
content-render: (self) ->
h \div.form [
2019-12-08 02:17:55 +01:00
2019-12-12 02:30:46 +01:00
field [
label "Project title"
2019-12-08 02:17:55 +01:00
2019-12-12 02:30:46 +01:00
input {
2019-12-08 02:17:55 +01:00
value: self.title
oninput: (e) ->
self.title := e.target.value
}
]
# h \hr []
# bulma.field [
# bulma.label "Adding a user"
# bulma.input {
# value: self.new-user
# oninput: (e) ->
# self.new-user := e.target.value
# name: \new-user
# id: \user-add
# }
# ]
2019-12-06 21:07:39 +01:00
2019-12-08 02:17:55 +01:00
h \hr []
2019-12-12 02:30:46 +01:00
field [
label "Permissions"
h \table.table.is-fullwidth.is-striped [
h \tbody [
for permission, uids of self.permissions
for uid in uids
h \tr {key: uid.to-string!} [
h \td [
uid.to-string!
]
h \td.is-narrow [
permission
]
]
]
]
2019-12-07 18:15:57 +01:00
]
2019-12-08 02:17:55 +01:00
h \hr []
2019-12-12 02:30:46 +01:00
# FIXME: This is supposed to go in a .field, right?
label "Adding new user"
2019-12-12 02:30:46 +01:00
field \.has-addons { key: \new-user } [
control [
select {
onchange: (e) ->
2019-12-10 05:59:52 +01:00
self.tmp.new-user-permission.permission := e.target.value
} permission-groups.map (permission) -> permission-to-form-selection self, permission
]
2019-12-12 02:30:46 +01:00
control \.is-expanded [
select \.is-fullwidth {
onchange: (e) ->
2019-12-10 05:59:52 +01:00
self.tmp.new-user-permission.uid := e.target.value
} [
user-form-selection self, { login: "Choose a user", uid: "-" }
for user-id, user of self.tmp.users
user-form-selection self, user
]
]
2019-12-12 02:30:46 +01:00
control [
button \.is-success.is-outlined {
onclick: ->
if self.tmp.new-user-permission.uid == void || self.tmp.new-user-permission.uid == "-"
console.log "adding an user permission on the kanban: failed, no user selected"
else
# TODO:
# adding the permissions in self.permissions
# then editing the project in the db via todod-ws
permissions-add self, self.tmp.new-user-permission.permission, self.tmp.new-user-permission.uid
} [ "+" ]
]
]
h \hr []
2019-12-12 02:30:46 +01:00
label [ "Columns" ]
2019-12-08 02:17:55 +01:00
for dom in (self.extra_properties.columns.map (column) -> col-to-lines column, self)
dom
h \hr []
2019-12-12 02:30:46 +01:00
field \.has-addons { key: \new-column } [
control \.is-expanded [
input {
2019-12-10 05:59:52 +01:00
value: self.tmp.new-column-input.title
2019-12-08 04:08:21 +01:00
oninput: (e) ->
2019-12-10 05:59:52 +01:00
self.tmp.new-column-input.title := e.target.value
2019-12-12 02:30:46 +01:00
}
2019-12-08 04:08:21 +01:00
]
2019-12-12 21:49:15 +01:00
control [
button \.is-success.is-outlined {
onclick: ->
new-col = {
id: UUID!
title: self.tmp.new-column-input.title
}
self.extra_properties.columns ++= [ new-col ]
self.tmp.new-column-input.title := "New column !"
} [ "+" ]
]
2019-12-08 04:08:21 +01:00
]
2019-12-06 21:07:39 +01:00
]
2019-12-06 21:07:39 +01:00
on-validation: ->
2019-12-10 05:59:52 +01:00
tmp = delete self.tmp
2019-12-06 21:07:39 +01:00
if project.id
todod-ws.edit-list project.id, self
else
todod-ws.add-list self.title, self
2019-12-10 05:59:52 +01:00
self.tmp := tmp
}, self
2019-12-06 21:07:39 +01:00
self.render = ->
modal.render!
self
module.exports = ProjectCreationModal