22
33const socket = io ( )
44const board_id = window . location . href . split ( '/' ) [ 3 ]
5- const tempQuadrant = { important : 0 , urgent : 0 }
5+ const defaultPos = { left : 0 , top : 0 }
66
77document . 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"
0 commit comments