-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.js
53 lines (47 loc) · 1.52 KB
/
board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const io = require('socket.io-client')
const Counter = require('./counter')
const Sticker = require('./sticker')
const socket = io(`/board`)
const state = Object.assign({
clients: 0,
stickers: {
temp: [],
saved: []
}
}, window.initialState)
const counter = new Counter('online')
document.body.appendChild(counter.render(state.clients))
socket.on('client:join', function () {
state.clients += 1
counter.render(state.clients)
})
socket.on('client:leave', function () {
state.clients -= 1
counter.render(state.clients)
})
socket.on('add', function (data) {
const sticker = new Sticker('sticker-' + data.id)
state.stickers.temp.push(Object.assign({ sticker }, data))
document.body.appendChild(sticker.render(data))
})
socket.on('move', function (data) {
const sticker = state.stickers.temp.find(sticker => sticker.id === data.id)
if (!sticker) return
sticker.sticker.render(data)
})
socket.on('remove', function (data) {
const sticker = document.getElementById(`sticker-${data.id}`)
if (!sticker) return
state.stickers.temp = state.stickers.temp.filter(sticker => sticker.id !== data.id)
sticker.parentNode.removeChild(sticker)
})
socket.on('save', function (data) {
const sticker = state.stickers.temp.find(sticker => sticker.id === data.id)
state.stickers.temp = state.stickers.temp.filter(sticker => sticker.id !== data.id)
state.stickers.saved.push(sticker)
sticker.sticker.render({ saved: true })
})
socket.on('new', function (data) {
const image = new window.Image(data.src)
console.log(image)
})