Skip to content

Commit 23725bd

Browse files
authored
Merge pull request #2 from judy-n/v1
please no conflicts
2 parents dc852d9 + dbbb463 commit 23725bd

File tree

5 files changed

+98
-29
lines changed

5 files changed

+98
-29
lines changed

client/static/js/actions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ const updateNote = (note) => {
4545
return instance.patch('/note', {note})
4646
.then(res => res.data.note)
4747
.catch(err => console.error)
48+
}
49+
50+
const updateNotePos = (note_id, pos) => {
51+
return instance.patch(`/note/${note_id}/position`, {pos})
52+
.then(res => res.data.pos)
53+
.catch(err => console.error)
4854
}

client/static/js/board.js

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const socket = io()
44
const board_id = window.location.href.split('/')[3]
5-
const tempQuadrant = {important: 0, urgent: 0}
5+
const defaultPos = {left: 0, top: 0}
66

77
document.addEventListener('DOMContentLoaded', () => {
88
const stickyArea = document.querySelector(
@@ -16,26 +16,48 @@ document.addEventListener('DOMContentLoaded', () => {
1616
const stickyTitleInput = document.querySelector('#stickytitle');
1717
const stickyTextInput = document.querySelector('#stickytext');
1818

19+
// helpers
20+
const getBoardBounds = () => {
21+
const topMin = document.querySelector('.h-titles').clientHeight
22+
const leftMin = document.querySelector('.v-titles').clientWidth
23+
const topMax = document.querySelector('.q-container').clientHeight + topMin
24+
const leftMax = document.querySelector('.q-container').clientWidth + leftMin
25+
return { topMin, leftMin, topMax, leftMax }
26+
}
27+
28+
const normalize = ({x, y}) => {
29+
const { topMin, leftMin, topMax, leftMax } = getBoardBounds()
30+
const left = x * (leftMax - leftMin) + leftMin
31+
const top = y * (topMax - topMin) + topMin
32+
return { left, top }
33+
}
34+
1935
// socket.io functions
2036
const sendCreateNote = () => {
2137
// create a note and send it
2238
// here we have access to title and text field
2339
const title = stickyTitleInput.value
2440
const text = stickyTextInput.value
25-
const note = { title, text, tempQuadrant }
41+
const pos = { left: Math.random(), top: Math.random() }
42+
const note = { title, text, pos }
2643
console.log('n', board_id, note)
2744
createNote(board_id, note).then(newNote => {
2845
socket.emit('note created', {note: newNote, board_id})
29-
createSticky(newNote._id)
46+
createSticky(newNote._id, pos)
3047
}).catch(e => console.log('an unknown error occurred'))
3148
}
32-
const sendUpdateNote = (_id, title, text) => {
33-
const note = { _id, title, text, tempQuadrant }
49+
const sendUpdateNote = (_id, title, text, pos) => {
50+
const note = { _id, title, text, tempQuadrant: defaultPos }
3451
updateNote(note).then(resNote => {
3552
socket.emit('note update', {note: resNote, board_id})
3653
console.log('up conf', resNote)
3754
}).catch(e => console.log('an error occurred'))
3855
}
56+
const sendMoveNote = (note_id, pos) => {
57+
updateNotePos(note_id, pos).then(_resPos => {
58+
socket.emit('note move', {note_id, pos, board_id})
59+
}).catch(e => console.error('an unknown error has occurred'))
60+
}
3961
const sendDeleteNote = (note_id) => {
4062
deleteNote(note_id).then(note => {
4163
socket.emit('note delete', {note_id, board_id})
@@ -44,7 +66,7 @@ document.addEventListener('DOMContentLoaded', () => {
4466

4567
const receiveCreatedNote = ({note, io_board_id}) => {
4668
if (io_board_id === board_id) {
47-
loadSticky(note._id, note.title, note.text)
69+
loadSticky(note._id, note.title, note.text, note.pos)
4870
}
4971
}
5072
const receiveUpdatedNote = ({note, io_board_id}) => {
@@ -54,6 +76,14 @@ document.addEventListener('DOMContentLoaded', () => {
5476
noteEL.querySelector('p').innerText = note.text
5577
}
5678
}
79+
const receiveMoveNote = ({note_id, pos, io_board_id}) => {
80+
if (io_board_id === board_id) {
81+
const noteEl = document.querySelector(`.sticky[id="${note_id}"]`)
82+
const { left, top } = normalize({x: pos.left, y: pos.top})
83+
noteEl.style.left = `${left}px`
84+
noteEl.style.top = `${top}px`
85+
}
86+
}
5787
const receiveDeleteNote = ({note_id, io_board_id}) => {
5888
if (io_board_id === board_id) {
5989
const el = document.querySelector(`.sticky[id="${note_id}"]`)
@@ -69,6 +99,10 @@ document.addEventListener('DOMContentLoaded', () => {
6999
receiveUpdatedNote({note, io_board_id})
70100
})
71101

102+
socket.on('receive move', ({note_id, pos, io_board_id}) => {
103+
receiveMoveNote({note_id, pos, io_board_id})
104+
})
105+
72106
socket.on('receive delete', ({note_id, io_board_id}) => {
73107
receiveDeleteNote({note_id, io_board_id})
74108
})
@@ -107,7 +141,6 @@ document.addEventListener('DOMContentLoaded', () => {
107141
const text = sticky.querySelector('.input-p').value
108142
try {
109143
await sendUpdateNote(id, title, text)
110-
console.log('up sent', id, title, text)
111144
} catch(e) {
112145
console.log('an error occured')
113146
return
@@ -142,12 +175,11 @@ document.addEventListener('DOMContentLoaded', () => {
142175
if (!isDragging) return;
143176

144177
// console.log(lastOffsetX);
145-
146178
dragTarget.style.left = e.clientX - lastOffsetX + 'px';
147179
dragTarget.style.top = e.clientY - lastOffsetY + 'px';
148180
}
149181

150-
function createSticky(note_id) {
182+
function createSticky(note_id, pos) {
151183
const newSticky = document.createElement('div');
152184
newSticky.setAttribute('id', note_id)
153185
newSticky.addEventListener('dblclick', e => console.log(e))
@@ -164,11 +196,11 @@ document.addEventListener('DOMContentLoaded', () => {
164196
newSticky.innerHTML = html;
165197
// newSticky.style.backgroundColor = randomColor();
166198
stickyArea.append(newSticky);
167-
positionSticky(newSticky);
199+
positionSticky(newSticky, pos);
168200
applyDeleteListener();
169201
clearStickyForm();
170202
}
171-
function loadSticky(note_id, title, text) {
203+
function loadSticky(note_id, title, text, pos) {
172204
const newSticky = document.createElement('div');
173205
newSticky.setAttribute('id', note_id)
174206
const html = `<h3>${title.replace(
@@ -184,25 +216,18 @@ document.addEventListener('DOMContentLoaded', () => {
184216
newSticky.innerHTML = html;
185217
// newSticky.style.backgroundColor = randomColor();
186218
stickyArea.append(newSticky);
187-
positionSticky(newSticky);
219+
positionSticky(newSticky, pos)
188220
applyDeleteListener();
189221
clearStickyForm();
190222
}
191223
function clearStickyForm() {
192224
stickyTitleInput.value = '';
193225
stickyTextInput.value = '';
194226
}
195-
function positionSticky(sticky) {
196-
sticky.style.left =
197-
window.innerWidth / 2 -
198-
sticky.clientWidth / 2 +
199-
(-100 + Math.round(Math.random() * 50)) +
200-
'px';
201-
sticky.style.top =
202-
window.innerHeight / 2 -
203-
sticky.clientHeight / 2 +
204-
(-100 + Math.round(Math.random() * 50)) +
205-
'px';
227+
function positionSticky(sticky, pos) {
228+
const {left, top} = normalize({x: pos.left, y: pos.top})
229+
sticky.style.left = `${left}px`
230+
sticky.style.top = `${top}px`
206231
}
207232

208233
function stripHtml(text) {
@@ -241,7 +266,25 @@ document.addEventListener('DOMContentLoaded', () => {
241266
}
242267
});
243268
window.addEventListener('mousemove', drag);
244-
window.addEventListener('mouseup', () => (isDragging = false));
269+
window.addEventListener('mouseup', async (e) => {
270+
// some useful values
271+
isDragging = false
272+
if (e.target.classList.contains('drag')) {
273+
const id = e.target.getAttribute('id')
274+
const {left, top} = e.target.style
275+
const { topMin, leftMin, topMax, leftMax } = getBoardBounds()
276+
// x is left 0to1, y is top 0to1
277+
const x = (parseFloat(left)-leftMin)/(leftMax-leftMin)
278+
const y = (parseFloat(top)-topMin)/(topMax-topMin)
279+
try {
280+
await sendMoveNote(id, {left: x, top: y})
281+
} catch(e) {
282+
console.error('an unknown error has occurred')
283+
} finally {
284+
console.log('sent', id, x, y)
285+
}
286+
}
287+
});
245288

246289
createStickyButton.addEventListener('click', sendCreateNote);
247290
applyDeleteListener();
@@ -250,7 +293,7 @@ document.addEventListener('DOMContentLoaded', () => {
250293
getBoard(board_id).then(board => {
251294
if (board) {
252295
board.notes.forEach(note => {
253-
loadSticky(note._id, note.title, note.text)
296+
loadSticky(note._id, note.title, note.text, note.pos)
254297
})
255298
} else {
256299
// window.location.href = "/undefined"

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ io.on('connection', socket => {
4343
socket.broadcast.emit('receive update', {note, io_board_id: board_id})
4444
})
4545

46+
socket.on('note move', ({note_id, pos, board_id}) => {
47+
socket.broadcast.emit('receive move', {note_id, pos, io_board_id: board_id})
48+
})
49+
4650
socket.on('note delete', ({note_id, board_id}) => {
4751
socket.broadcast.emit('receive delete', {note_id, io_board_id: board_id})
4852
})

mongo.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class Board {
1919
}
2020

2121
class Note {
22-
constructor({title, text, quadrant}) {
22+
constructor({title, text, pos}) {
2323
this._id = new ObjectId();
2424
this.title = title;
2525
this.text = text;
26-
this.quadrant = quadrant;
26+
this.pos = pos;
2727
}
2828
}
2929

@@ -73,7 +73,6 @@ async function readNote(id){
7373
id = new ObjectId(id)
7474
const res = await client.db("FourQuadrant").collection("Notes").findOne({_id: id})
7575
if (res) {
76-
console.log(res)
7776
return res
7877
}
7978
else {
@@ -96,12 +95,16 @@ async function removeNote(note){
9695
return
9796
}
9897

99-
/* Changes the Note's text to <text>*/
10098
async function updateNote(note){
10199
note._id = new ObjectId(note._id)
102100
const res = await client.db("FourQuadrant").collection("Notes").updateOne({_id: note._id}, {$set: note});
103101
}
104102

103+
async function updateNotePos(note_id, pos){
104+
note_id = new ObjectId(note_id)
105+
const res = await client.db("FourQuadrant").collection("Notes").updateOne({_id: note_id}, {$set: {pos}}, {returnNewDocument:true})
106+
}
107+
105108
module.exports = {
106109
Board,
107110
Note,
@@ -113,4 +116,5 @@ module.exports = {
113116
removeBoard,
114117
removeNote,
115118
updateNote,
119+
updateNotePos,
116120
}

router.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,16 @@ router.patch('/note', async (req, res, next) => {
139139
}
140140
})
141141

142+
router.patch('/note/:note_id/position', idChecker, async (req, res, next) => {
143+
const { pos } = req.body
144+
try {
145+
await mongo.updateNotePos(req.params.note_id, pos)
146+
res.send({pos})
147+
} catch (e) {
148+
handleError(e, res)
149+
console.log('error', e)
150+
next()
151+
}
152+
})
153+
142154
module.exports = router

0 commit comments

Comments
 (0)