todod/client/index.ls

620 lines
13 KiB
Plaintext
Raw Normal View History

2019-07-06 02:35:50 +02:00
maquette = require "maquette"
nmd = require "nano-markdown"
{create-projector, h} = maquette
projector = create-projector!
bulma = require "./bulma.ls"
get-previous = (collection, element) ->
var previous
for item in collection
if item == element
return previous
previous = item
get-next = (collection, element) ->
var found-element
for item in collection
if found-element
return item
if item == element
found-element := true
Task = (self, project) ->
self.render = ->
author = model.users[self.author]
if typeof(author) != "object" and author != "request sent"
model.users[self.author] = "request sent"
# FIXME: This should go directly to authd.
2019-07-09 03:26:08 +02:00
socket.get-user self.author
2019-07-06 02:35:50 +02:00
assigned_to = model.users[self.assigned_to]
if self.assigned_to and typeof(assigned_to) != "object" and assigned_to != "request sent"
2019-07-06 02:35:50 +02:00
model.users[self.assigned_to] = "request sent"
# FIXME: This should go directly to authd.
2019-07-09 03:26:08 +02:00
socket.get-user self.assigned_to
2019-07-06 02:35:50 +02:00
is-selected = model.selected == self.id
2019-07-08 06:31:13 +02:00
h (\div.card.is- + (self.color || "dark")), {
2019-07-06 02:35:50 +02:00
key: self.id
classes: {
"is-selected": is-selected
}
onclick: ->
model.selected := self.id
} [
h \div.card-content [
h \div.media [
h \div.media-left [
h \img.image.is-48x48.avatar {
alt: "user image"
src: if typeof(assigned_to) == "object"
assigned_to.avatar
else
"https://bulma.io/images/placeholders/96x96.png"
}
]
h \div.media-content [
if model.editing == self.id + ".title"
h \input.input {
value: self.title
onchange: (e) ->
model.editing := undefined
2019-07-09 03:26:08 +02:00
socket.edit-task project.id, self.id, {
2019-07-06 02:35:50 +02:00
title: e.target.value
}
} [ self.title ]
else
h \a [
bulma.title 4 self.title
]
2019-07-06 02:35:50 +02:00
if typeof(model.users[self.assigned_to]) == "object"
user = model.users[self.assigned_to]
h \div.subtitle.is-6 [
"@" + (user.full_name || user.login)
]
]
if ! is-selected && self.description != ""
h \div.media-right {key: "description-icon"} [
h \span.icon.is-size-1 [ "🗎" ]
]
2019-07-06 02:35:50 +02:00
if is-selected
h \div.media-right {key: "edit"} [
h \a.small {
2019-07-06 02:35:50 +02:00
onclick: ->
if model.editing == self.id + ".title"
model.editing := undefined
else
model.editing := self.id + ".title"
} [
"Edit"
]
]
if is-selected
h \div.media-right {key: "delete"} [
h \a.small {
2019-07-06 02:35:50 +02:00
onclick: ->
model.editing := self.id + ".delete"
} [
"Delete"
]
]
]
if is-selected
h \div.content {
key: self.description
2019-07-06 02:35:50 +02:00
after-create: (dom) ->
dom.innerHTML = nmd self.description
} [
if model.editing == self.id + ".description"
h \form.form [
h \textarea.textarea {
2019-07-08 06:47:18 +02:00
value: model.editing-data
2019-07-06 02:35:50 +02:00
oninput: (e) ->
model.editing-data := e.target.value
}
h \div.button.is-fullwidth {
onclick: ->
2019-07-09 03:26:08 +02:00
socket.edit-task project.id, self.id, {
2019-07-06 02:35:50 +02:00
description: model.editing-data
}
model.editing-data := undefined
model.editing := undefined
} [ "Update" ]
]
]
if is-selected
h \span.button.is-small {
onclick: ->
2019-07-08 06:47:18 +02:00
model.editing-data := self.description
2019-07-06 02:35:50 +02:00
model.editing := self.id + ".description"
} [
"edit"
]
]
if is-selected
h \div.card-footer {key: "assign"} [
if model.editing == self.id + ".assigned_to"
h \div.card-footer-item {
key: "assign.clicked"
} [
h \input.input {
onchange: (e) ->
model.editing := undefined
2019-07-09 03:26:08 +02:00
socket.edit-task project.id, self.id, {
2019-07-06 02:35:50 +02:00
assigned_to: Number e.target.value
}
}
]
else
h \a.card-footer-item {
2019-07-06 02:35:50 +02:00
key: "assign"
onclick: ->
model.editing := self.id + ".assigned_to"
} [ "Assign" ]
]
2019-07-08 06:31:13 +02:00
if is-selected
h \div.card-footer {key: "color"} [
if model.editing == self.id + ".color"
h \div.card-footer-item {
key: "color.clicked"
} [
h \input.input {
onchange: (e) ->
model.editing := undefined
2019-07-09 03:26:08 +02:00
socket.edit-task project.id, self.id, {
2019-07-08 06:31:13 +02:00
color: e.target.value
}
}
]
else
h \a.card-footer-item {
2019-07-08 06:31:13 +02:00
key: "assign"
onclick: ->
model.editing := self.id + ".color"
} [ "Change Color" ]
]
2019-07-06 02:35:50 +02:00
if is-selected
h \div.card-footer {key: "move"} [
h \a.card-footer-item {
2019-07-06 02:35:50 +02:00
key: "⇐"
onclick: ->
2019-07-09 03:26:08 +02:00
socket.edit-task project.id, self.id, {
2019-07-06 02:35:50 +02:00
column: get-previous project.columns.map((.id)), self.column
}
} [ "⇐" ]
if model.editing == self.id + ".delete"
h \a.card-footer-item {
2019-07-06 02:35:50 +02:00
key: "delete"
} [
h \div.button.is-danger {
onclick: ->
2019-07-09 03:26:08 +02:00
socket.delete-task project.id, self.id
2019-07-06 02:35:50 +02:00
} [ "Delete! For real!" ]
]
h \a.card-footer-item {
2019-07-06 02:35:50 +02:00
key: "⇒"
onclick: ->
2019-07-09 03:26:08 +02:00
socket.edit-task project.id, self.id, {
2019-07-06 02:35:50 +02:00
column: get-next project.columns.map((.id)), self.column
}
} [ "⇒" ]
]
]
self
Project = (self) ->
self.tasks = self.tasks.map (e) -> Task e, self
self.render-column = (column) ->
2019-07-08 05:32:51 +02:00
h \div.column.is-3 {
2019-07-06 02:35:50 +02:00
key: column.id
} [
h \div.card.is-column-header {
key: column.id
} [
h \div.card-header [
if model.editing == column.id + ".title"
h \input.input {
type: "text",
value: column.name
onchange: (e) ->
console.log "onchange??"
model.editing := undefined
2019-07-09 03:26:08 +02:00
socket.edit-column self.id, column.id, {
2019-07-06 02:35:50 +02:00
name: e.target.value
}
}
else
h \div.card-header-title [
bulma.title 3 column.name
]
h \a.card-header-icon {
2019-07-06 02:35:50 +02:00
key: "edit"
onclick: ->
if model.editing == column.id + ".title"
model.editing := undefined
else
model.editing := column.id + ".title"
} [
"Edit"
]
if self.tasks.filter((.column == column.id)).length == 0
h \a.card-header-icon {
2019-07-06 02:35:50 +02:00
key: "delete"
onclick: ->
model.editing := column.id + ".delete"
} [
"Delete"
]
]
if model.editing == column.id + ".delete"
h \div.card-content [
h \div.button.is-fullwidth.is-danger {
onclick: ->
2019-07-09 03:26:08 +02:00
socket.delete-column self.id, column.id
2019-07-06 02:35:50 +02:00
} [ "Delete me!"]
]
]
for task in self.tasks
continue if task.column != column.id
task.render!
h \div.button.is-fullwidth {
onclick: ->
2019-07-09 03:26:08 +02:00
socket.new-task self.id, column.id, {
2019-07-06 02:35:50 +02:00
title: "General Kenobi…"
description: ""
}
} [ "New task" ]
]
self.render = ->
h \div.project {
key: self.id
} [
h \div.hero.is-dark { key: "title" } [
h \div.hero-body [
# FIXME: Consider using a .level for this.
h \div.is-pulled-right {
onclick: ->
model.editing := self.id + ".name"
} [
"Edit"
]
if model.editing == self.id + ".name"
h \input.input {
onchange: (e) ->
model.editing := undefined
2019-07-09 03:26:08 +02:00
socket.edit-project self.id, {
2019-07-06 02:35:50 +02:00
name: e.target.value
}
value: self.name
}
else
h \div.title [ self.name ]
]
]
h \div.columns [
for dom in self.columns.map((column) -> self.render-column(column))
dom
h \div.column.is-2 {
key: "new-column"
} [
h \div.button.is-fullwidth {
onclick: ->
2019-07-09 03:26:08 +02:00
socket.new-column self.id, "Hello, there!"
2019-07-06 02:35:50 +02:00
} [ "New Column" ]
]
]
]
self
model = {
current-view: "login"
editing: undefined
selected: undefined
users: {}
projects: {}
projects-list: []
}
protocol = if location.protocol == 'https' then 'wss' else 'ws'
2019-09-30 21:11:42 +02:00
port = 9999
socket-url = protocol + '://' + location.hostname + \: + port + "/kanban.JSON"
console.log socket-url
2019-07-09 03:26:08 +02:00
KanbanSocket = (socket-url) ->
self = {}
self.open-socket = ->
2019-09-30 21:11:42 +02:00
console.log "Opening socket to #{socket-url}"
2019-07-09 03:26:08 +02:00
self.socket := new WebSocket socket-url
self.socket.onerror = (event) ->
console.log "WebSocket error.", event
model.state := "websocket-error"
self.socket.close!
projector.schedule-render!
self.socket.onclose = (event) ->
# Exporting the error in case the UI is able to deal with it.
model.previous-state := model.state
model.state := "websocket-error"
model.websocket-error := event.reason
projector.schedule-render!
self.socket.onmessage = (event) ->
2019-09-30 21:11:42 +02:00
payload = JSON.parse(event.data).payload
self.on-message {data: payload}
2019-07-09 03:26:08 +02:00
2019-07-10 13:13:52 +02:00
self.reopen = ->
self.socket.close!
self.open-socket!
2019-07-09 03:26:08 +02:00
self.open-socket!
self.send = (opts) ->
2019-09-30 21:11:42 +02:00
console.log JSON.stringify {
mtype: 0,
payload: opts
}
2019-07-09 03:26:08 +02:00
self.socket.send JSON.stringify {
2019-09-30 21:11:42 +02:00
mtype: 0,
payload: opts
}
self.get-project = (project-id) ->
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "project",
project: project-id
}
self.login = (login, password) ->
2019-09-30 21:11:42 +02:00
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "login",
login: login
password: password
}
self.get-user = (uid) ->
2019-09-30 21:11:42 +02:00
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "get-user"
uid: uid
}
self.new-project = (name) ->
2019-09-30 21:11:42 +02:00
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "new-project"
name: name
}
self.edit-task = (project-id, task-id, options) ->
payload = {
type: "edit-task"
project: project-id
task: task-id
}
for key, value of options
payload[key] = value
2019-09-30 21:11:42 +02:00
self.send JSON.stringify payload
2019-07-09 03:26:08 +02:00
self.delete-task = (project-id, task-id) ->
2019-09-30 21:11:42 +02:00
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "delete-task"
project: project-id
task: task-id
}
self.edit-column = (project-id, column-id, options) ->
payload = {
type: "edit-column"
project: project-id
column: column-id
}
for key, value of options
payload[key] = value
2019-09-30 21:11:42 +02:00
self.send JSON.stringify payload
2019-07-09 03:26:08 +02:00
self.delete-column = (project-id, column-id) ->
2019-09-30 21:11:42 +02:00
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "delete-column"
project: project-id
column: column-id
}
self.new-column = (project-id, name) ->
2019-09-30 21:11:42 +02:00
self.send JSON.stringify {
2019-07-09 03:26:08 +02:00
type: "new-column"
project: project-id
name: name
}
self.new-task = (project-id, column-id, options) ->
payload = {
type: "new-task"
project: project-id
column: column-id
}
for key, value of options
payload[key] = value
2019-09-30 21:11:42 +02:00
self.send JSON.stringify payload
2019-07-09 03:26:08 +02:00
self.edit-project = (project-id, options) ->
payload = {
type: "edit-project"
project: project-id
}
for key, value of options
payload[key] = value
2019-09-30 21:11:42 +02:00
self.send JSON.stringify payload
2019-07-06 02:35:50 +02:00
2019-07-09 03:26:08 +02:00
self
2019-07-06 02:35:50 +02:00
2019-07-09 03:26:08 +02:00
socket = KanbanSocket socket-url
2019-07-06 02:35:50 +02:00
2019-09-30 21:11:42 +02:00
socket.on-close = ->
model.current-view := "login"
socket.reopen!
2019-07-06 02:35:50 +02:00
2019-09-30 21:11:42 +02:00
socket.on-message = (event) ->
2019-07-06 02:35:50 +02:00
message = JSON.parse event.data
switch message.type
when "login"
model.current-view := "projects-list"
when "list-projects"
for project in message.projects
model.projects[project.id] = Project project
model.projects-list := message.projects
when "project"
model.projects[message.project.id] = Project message.project
when "user"
2019-10-26 18:56:52 +02:00
if message.user
model.users[message.user.uid] := message.user
when "login-error"
model.login-error = message.error
2019-07-06 02:35:50 +02:00
else
console.log "RECEIVED UNKNOWN MESSAGE TYPE: #{message.type}"
projector.schedule-render!
console.log message
renderer = ->
2019-07-10 13:13:52 +02:00
render-navbar = ->
h \div.navbar [
h \div.navbar-start [
h \a.navbar-item.is-size-1 {
onclick: ->
model.viewed-project := undefined
model.current-view := "projects-list"
} [ "⌂" ]
]
h \div.navbar-end [
h \a.navbar-item {
onclick: ->
model.current-view := "login"
socket.reopen!
} [ "Logout" ]
]
]
2019-07-06 02:35:50 +02:00
h \div.section [
switch model.current-view
when "login"
h \div.container [
h \div.box [
if model.login-error
h \div.notification.is-danger [
model.login-error
]
2019-07-06 02:35:50 +02:00
h \form [
bulma.field [
bulma.label "Login"
bulma.input {
oninput: (e) ->
model.login = e.target.value
name: \login
id: \login-input
}
]
bulma.field [
bulma.label "Password"
bulma.input {
oninput: (e) ->
model.password = e.target.value
name: \password
type: \password
id: \password-input
}
]
h \button.button.is-fullwidth.is-primary {
onclick: (e) ->
e.prevent-default!
model.login-error = undefined
2019-07-09 03:26:08 +02:00
socket.login model.login, model.password
2019-07-06 02:35:50 +02:00
} [ "Letsu go!" ]
]
]
]
when "project"
h \div [
2019-07-10 13:13:52 +02:00
render-navbar!
2019-07-06 02:35:50 +02:00
if model.projects[model.viewed-project]
model.projects[model.viewed-project].render!
]
when "projects-list"
h \div#projects-list [
2019-07-10 13:13:52 +02:00
render-navbar!
h \div.section (model.projects-list || []).map (project) ->
h \a.box {
2019-07-06 02:35:50 +02:00
key: project.id
onclick: ->
model.current-view := "project"
model.viewed-project := project.id
2019-07-09 03:26:08 +02:00
socket.get-project project.id
2019-07-06 02:35:50 +02:00
} [
bulma.title 3 project.name
]
h \div.button.is-primary.is-large.is-fullwidth {
onclick: ->
2019-07-09 03:26:08 +02:00
socket.new-project "Hello, there!"
2019-07-06 02:35:50 +02:00
} [ "New project! (random atm)" ]
]
else
h \div.notification.is-error [
"Wait, what? Internal error!"
]
]
document.add-event-listener 'DOMContentLoaded' ->
projector.append document.body, renderer