todo-webclient/client/todod.ls

145 lines
2.8 KiB
Plaintext

# todod module
# 1. create a socket
# 2. the socket parses and serialize data
# 3. the socket handles network errors
module.exports = {
create-socket: (socket-url) ->
self = {}
self.open-socket = ->
console.log "Opening socket to #{socket-url}"
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) ->
payload = JSON.parse(event.data).payload
self.on-message {data: payload}
self.reopen = ->
self.socket.close!
self.open-socket!
self.open-socket!
self.send = (opts) ->
console.log JSON.stringify {
mtype: 0,
payload: opts
}
self.socket.send JSON.stringify {
mtype: 0,
payload: opts
}
self.get-project = (project-id) ->
self.send JSON.stringify {
type: "project",
project: project-id
}
self.login = (login, password) ->
self.send JSON.stringify {
type: "login",
login: login
password: password
}
self.get-user = (uid) ->
self.send JSON.stringify {
type: "get-user"
uid: uid
}
self.new-project = (name) ->
self.send JSON.stringify {
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
self.send JSON.stringify payload
self.delete-task = (project-id, task-id) ->
self.send JSON.stringify {
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
self.send JSON.stringify payload
self.delete-column = (project-id, column-id) ->
self.send JSON.stringify {
type: "delete-column"
project: project-id
column: column-id
}
self.new-column = (project-id, name) ->
self.send JSON.stringify {
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
self.send JSON.stringify payload
self.edit-project = (project-id, options) ->
payload = {
type: "edit-project"
project: project-id
}
for key, value of options
payload[key] = value
self.send JSON.stringify payload
self
}