2018-02-08 23:38:41 +01:00
var whiteboardId = getQueryVariable ( "whiteboardid" ) ;
2020-04-03 22:38:33 +02:00
var randomid = getQueryVariable ( "randomid" ) ;
if ( randomid && ! whiteboardId ) { //set random whiteboard on empty whiteboardid
whiteboardId = Array ( 2 ) . fill ( null ) . map ( ( ) => Math . random ( ) . toString ( 36 ) . substr ( 2 ) ) . join ( '' ) ;
const urlParams = new URLSearchParams ( window . location . search ) ;
urlParams . set ( 'whiteboardid' , whiteboardId ) ;
window . location . search = urlParams ;
}
2018-02-08 23:38:41 +01:00
whiteboardId = whiteboardId || "myNewWhiteboard" ;
2020-03-27 17:12:30 +01:00
whiteboardId = unescape ( encodeURIComponent ( whiteboardId ) ) . replace ( /[^a-zA-Z0-9 ]/g , "" ) ;
2018-02-08 23:38:41 +01:00
var myUsername = getQueryVariable ( "username" ) ;
2019-02-11 12:43:23 +01:00
var accessToken = getQueryVariable ( "accesstoken" ) ;
2018-09-08 19:15:23 +02:00
myUsername = myUsername || "unknown" + ( Math . random ( ) + "" ) . substring ( 2 , 6 ) ;
2019-02-11 12:43:23 +01:00
accessToken = accessToken || "" ;
2019-06-02 17:40:20 +02:00
var accessDenied = false ;
2018-02-08 17:45:07 +01:00
2020-03-28 19:41:47 +01:00
// Custom Html Title
var title = getQueryVariable ( "title" ) ;
2020-04-03 22:38:33 +02:00
if ( ! title === false ) {
2020-03-29 16:56:47 +02:00
document . title = decodeURIComponent ( title ) ;
2020-03-28 19:41:47 +01:00
}
2018-09-08 19:15:23 +02:00
var url = document . URL . substr ( 0 , document . URL . lastIndexOf ( '/' ) ) ;
2018-02-09 00:45:10 +01:00
var signaling _socket = null ;
var urlSplit = url . split ( "/" ) ;
var subdir = "" ;
2018-09-08 19:15:23 +02:00
for ( var i = 3 ; i < urlSplit . length ; i ++ ) {
subdir = subdir + '/' + urlSplit [ i ] ;
2018-02-09 00:45:10 +01:00
}
2018-09-08 19:15:23 +02:00
if ( subdir != "" ) {
signaling _socket = io ( "" , { "path" : subdir + "/socket.io" } ) ; //Connect even if we are in a subdir behind a reverse proxy
2018-02-09 00:45:10 +01:00
} else {
signaling _socket = io ( ) ;
}
signaling _socket . on ( 'connect' , function ( ) {
2018-09-08 19:15:23 +02:00
console . log ( "Websocket connected!" ) ;
2018-02-08 17:45:07 +01:00
signaling _socket . on ( 'drawToWhiteboard' , function ( content ) {
whiteboard . handleEventsAndData ( content , true ) ;
} ) ;
2018-02-08 23:38:41 +01:00
signaling _socket . on ( 'refreshUserBadges' , function ( ) {
whiteboard . refreshUserBadges ( ) ;
} ) ;
2018-02-09 02:04:50 +01:00
2019-02-11 12:43:23 +01:00
signaling _socket . on ( 'wrongAccessToken' , function ( ) {
2019-07-01 13:15:11 +02:00
if ( ! accessDenied ) {
2019-06-02 17:40:20 +02:00
accessDenied = true ;
showBasicAlert ( "Access denied! Wrong accessToken!" )
}
2019-02-11 12:43:23 +01:00
} ) ;
2019-05-07 08:36:42 +02:00
signaling _socket . on ( 'updateSmallestScreenResolution' , function ( widthHeight ) {
whiteboard . updateSmallestScreenResolution ( widthHeight [ "w" ] , widthHeight [ "h" ] ) ;
} ) ;
signaling _socket . emit ( 'joinWhiteboard' , { wid : whiteboardId , at : accessToken , windowWidthHeight : { w : $ ( window ) . width ( ) , h : $ ( window ) . height ( ) } } ) ;
2018-02-08 17:45:07 +01:00
} ) ;
2018-09-08 19:15:23 +02:00
$ ( document ) . ready ( function ( ) {
2019-07-01 13:15:11 +02:00
if ( getQueryVariable ( "webdav" ) == "true" ) {
$ ( "#uploadWebDavBtn" ) . show ( ) ;
}
2018-09-08 19:15:23 +02:00
whiteboard . loadWhiteboard ( "#whiteboardContainer" , { //Load the whiteboard
whiteboardId : whiteboardId ,
2020-03-20 17:29:20 +01:00
username : btoa ( myUsername ) ,
2018-09-08 19:15:23 +02:00
sendFunction : function ( content ) {
2019-02-11 12:43:23 +01:00
content [ "at" ] = accessToken ;
2018-09-08 19:15:23 +02:00
signaling _socket . emit ( 'drawToWhiteboard' , content ) ;
2018-02-08 17:45:07 +01:00
}
} ) ;
2019-01-11 11:24:04 +01:00
// request whiteboard from server
2019-04-05 14:56:09 +02:00
$ . get ( subdir + "/loadwhiteboard" , { wid : whiteboardId , at : accessToken } ) . done ( function ( data ) {
2018-02-08 17:45:07 +01:00
whiteboard . loadData ( data )
} ) ;
2018-02-08 20:04:13 +01:00
2019-05-07 08:36:42 +02:00
$ ( window ) . resize ( function ( ) {
signaling _socket . emit ( 'updateScreenResolution' , { at : accessToken , windowWidthHeight : { w : $ ( window ) . width ( ) , h : $ ( window ) . height ( ) } } ) ;
} )
2018-02-08 20:04:13 +01:00
/*----------------/
Whiteboard actions
/----------------*/
2019-02-22 13:14:34 +01:00
//Handle key actions
$ ( document ) . on ( "keydown" , function ( e ) {
2020-03-24 20:22:37 +01:00
if ( e . which == 16 ) {
2019-02-22 13:14:34 +01:00
whiteboard . pressedKeys [ "shift" ] = true ; //Used for straight lines...
}
//console.log(e.which);
} ) ;
$ ( document ) . on ( "keyup" , function ( e ) {
2020-03-24 20:22:37 +01:00
if ( e . which == 16 ) {
2019-02-22 13:14:34 +01:00
whiteboard . pressedKeys [ "shift" ] = false ;
}
} ) ;
2020-03-24 20:22:37 +01:00
var shortcutFunctions = {
clearWhiteboard : function ( ) { whiteboard . clearWhiteboard ( ) ; } ,
undoStep : function ( ) { whiteboard . undoWhiteboardClick ( ) ; } ,
2020-04-17 19:37:19 +02:00
redoStep : function ( ) { whiteboard . redoWhiteboardClick ( ) ; } ,
2020-03-24 20:22:37 +01:00
setTool _mouse : function ( ) { $ ( ".whiteboardTool[tool=mouse]" ) . click ( ) ; } ,
setTool _recSelect : function ( ) { $ ( ".whiteboardTool[tool=recSelect]" ) . click ( ) ; } ,
setTool _pen : function ( ) {
$ ( ".whiteboardTool[tool=pen]" ) . click ( ) ;
whiteboard . redrawMouseCursor ( ) ;
} ,
setTool _line : function ( ) { $ ( ".whiteboardTool[tool=line]" ) . click ( ) ; } ,
setTool _rect : function ( ) { $ ( ".whiteboardTool[tool=rect]" ) . click ( ) ; } ,
setTool _circle : function ( ) { $ ( ".whiteboardTool[tool=circle]" ) . click ( ) ; } ,
setTool _text : function ( ) { $ ( ".whiteboardTool[tool=text]" ) . click ( ) ; } ,
setTool _eraser : function ( ) {
$ ( ".whiteboardTool[tool=eraser]" ) . click ( ) ;
whiteboard . redrawMouseCursor ( ) ;
} ,
thickness _bigger : function ( ) {
var thickness = parseInt ( $ ( "#whiteboardThicknessSlider" ) . val ( ) ) + 1 ;
$ ( "#whiteboardThicknessSlider" ) . val ( thickness ) ;
whiteboard . setStrokeThickness ( thickness ) ;
whiteboard . redrawMouseCursor ( ) ;
} ,
thickness _smaller : function ( ) {
var thickness = parseInt ( $ ( "#whiteboardThicknessSlider" ) . val ( ) ) - 1 ;
$ ( "#whiteboardThicknessSlider" ) . val ( thickness ) ;
whiteboard . setStrokeThickness ( thickness ) ;
whiteboard . redrawMouseCursor ( ) ;
} ,
openColorPicker : function ( ) { $ ( "#whiteboardColorpicker" ) . click ( ) ; } ,
saveWhiteboardAsImage : function ( ) { $ ( "#saveAsImageBtn" ) . click ( ) ; } ,
2020-03-25 14:42:47 +01:00
saveWhiteboardAsJson : function ( ) { $ ( "#saveAsJSONBtn" ) . click ( ) ; } ,
2020-03-24 20:22:37 +01:00
uploadWhiteboardToWebDav : function ( ) { $ ( "#uploadWebDavBtn" ) . click ( ) ; } ,
uploadJsonToWhiteboard : function ( ) { $ ( "#uploadJsonBtn" ) . click ( ) ; } ,
shareWhiteboard : function ( ) { $ ( "#shareWhiteboardBtn" ) . click ( ) ; } ,
hideShowControls : function ( ) { $ ( "#minMaxBtn" ) . click ( ) ; } ,
setDrawColorBlack : function ( ) {
whiteboard . setDrawColor ( "black" ) ;
whiteboard . redrawMouseCursor ( ) ;
$ ( "#whiteboardColorpicker" ) . css ( { "background" : "black" } ) ;
} ,
setDrawColorRed : function ( ) {
whiteboard . setDrawColor ( "red" ) ;
whiteboard . redrawMouseCursor ( ) ;
$ ( "#whiteboardColorpicker" ) . css ( { "background" : "red" } ) ;
} ,
setDrawColorGreen : function ( ) {
whiteboard . setDrawColor ( "green" ) ;
whiteboard . redrawMouseCursor ( ) ;
$ ( "#whiteboardColorpicker" ) . css ( { "background" : "green" } ) ;
} ,
setDrawColorBlue : function ( ) {
whiteboard . setDrawColor ( "blue" ) ;
whiteboard . redrawMouseCursor ( ) ;
$ ( "#whiteboardColorpicker" ) . css ( { "background" : "blue" } ) ;
} ,
setDrawColorYellow : function ( ) {
whiteboard . setDrawColor ( "yellow" ) ;
whiteboard . redrawMouseCursor ( ) ;
$ ( "#whiteboardColorpicker" ) . css ( { "background" : "yellow" } ) ;
} ,
2020-03-25 14:42:47 +01:00
toggleLineRecCircle : function ( ) {
2020-03-24 20:22:37 +01:00
var activeTool = $ ( ".whiteboardTool.active" ) . attr ( "tool" ) ;
if ( activeTool == "line" ) {
$ ( ".whiteboardTool[tool=rect]" ) . click ( ) ;
} else if ( activeTool == "rect" ) {
$ ( ".whiteboardTool[tool=circle]" ) . click ( ) ;
} else {
$ ( ".whiteboardTool[tool=line]" ) . click ( ) ;
}
} ,
2020-03-25 14:42:47 +01:00
togglePenEraser : function ( ) {
var activeTool = $ ( ".whiteboardTool.active" ) . attr ( "tool" ) ;
if ( activeTool == "pen" ) {
$ ( ".whiteboardTool[tool=eraser]" ) . click ( ) ;
} else {
$ ( ".whiteboardTool[tool=pen]" ) . click ( ) ;
}
} ,
toggleMainColors : function ( ) {
var bgColor = $ ( "#whiteboardColorpicker" ) [ 0 ] . style . backgroundColor ;
if ( bgColor == "blue" ) {
shortcutFunctions . setDrawColorGreen ( ) ;
} else if ( bgColor == "green" ) {
shortcutFunctions . setDrawColorYellow ( ) ;
} else if ( bgColor == "yellow" ) {
shortcutFunctions . setDrawColorRed ( ) ;
} else if ( bgColor == "red" ) {
shortcutFunctions . setDrawColorBlack ( ) ;
} else {
shortcutFunctions . setDrawColorBlue ( ) ;
}
} ,
2020-03-24 20:22:37 +01:00
moveDraggableUp : function ( ) {
2020-04-03 22:38:33 +02:00
var elm = whiteboard . tool == "text" ? $ ( "#" + whiteboard . latestActiveTextBoxId ) : $ ( ".dragMe" ) [ 0 ] ;
2020-03-25 14:42:47 +01:00
var p = $ ( elm ) . position ( ) ;
$ ( elm ) . css ( { top : p . top - 5 , left : p . left } )
2020-03-24 20:22:37 +01:00
} ,
moveDraggableDown : function ( ) {
2020-04-03 22:38:33 +02:00
var elm = whiteboard . tool == "text" ? $ ( "#" + whiteboard . latestActiveTextBoxId ) : $ ( ".dragMe" ) [ 0 ] ;
2020-03-25 14:42:47 +01:00
var p = $ ( elm ) . position ( ) ;
$ ( elm ) . css ( { top : p . top + 5 , left : p . left } )
2020-03-24 20:22:37 +01:00
} ,
moveDraggableLeft : function ( ) {
2020-04-03 22:38:33 +02:00
var elm = whiteboard . tool == "text" ? $ ( "#" + whiteboard . latestActiveTextBoxId ) : $ ( ".dragMe" ) [ 0 ] ;
2020-03-25 14:42:47 +01:00
var p = $ ( elm ) . position ( ) ;
$ ( elm ) . css ( { top : p . top , left : p . left - 5 } )
2020-03-24 20:22:37 +01:00
} ,
moveDraggableRight : function ( ) {
2020-04-03 22:38:33 +02:00
var elm = whiteboard . tool == "text" ? $ ( "#" + whiteboard . latestActiveTextBoxId ) : $ ( ".dragMe" ) [ 0 ] ;
2020-03-25 14:42:47 +01:00
var p = $ ( elm ) . position ( ) ;
$ ( elm ) . css ( { top : p . top , left : p . left + 5 } )
2020-03-24 20:22:37 +01:00
} ,
dropDraggable : function ( ) {
2020-03-25 14:42:47 +01:00
$ ( $ ( ".dragMe" ) [ 0 ] ) . find ( '.addToCanvasBtn' ) . click ( ) ;
} ,
addToBackground : function ( ) {
$ ( $ ( ".dragMe" ) [ 0 ] ) . find ( '.addToBackgroundBtn' ) . click ( ) ;
2020-03-24 20:22:37 +01:00
} ,
cancelAllActions : function ( ) { whiteboard . escKeyAction ( ) ; } ,
deleteSelection : function ( ) { whiteboard . delKeyAction ( ) ; } ,
}
//Load keybindings from keybinds.js to given functions
for ( var i in keybinds ) {
if ( shortcutFunctions [ keybinds [ i ] ] ) {
keymage ( i , shortcutFunctions [ keybinds [ i ] ] , { preventDefault : true } ) ;
} else {
console . error ( "function you want to keybind on key:" , i , "named:" , keybinds [ i ] , "is not available!" )
}
}
2019-01-11 11:24:04 +01:00
// whiteboard clear button
2018-09-08 19:15:23 +02:00
$ ( "#whiteboardTrashBtn" ) . click ( function ( ) {
2019-02-11 14:09:21 +01:00
$ ( "#whiteboardTrashBtnConfirm" ) . show ( ) . focus ( ) ;
2020-03-20 17:29:20 +01:00
$ ( this ) . css ( { visibility : "hidden" } ) ;
2019-02-11 14:09:21 +01:00
} ) ;
2020-03-19 21:07:46 +01:00
$ ( "#whiteboardTrashBtnConfirm" ) . mouseout ( function ( ) {
2019-02-11 14:09:21 +01:00
$ ( this ) . hide ( ) ;
2020-03-20 17:29:20 +01:00
$ ( "#whiteboardTrashBtn" ) . css ( { visibility : "inherit" } ) ;
2019-02-11 14:09:21 +01:00
} ) ;
$ ( "#whiteboardTrashBtnConfirm" ) . click ( function ( ) {
$ ( this ) . hide ( ) ;
2020-03-20 17:29:20 +01:00
$ ( "#whiteboardTrashBtn" ) . css ( { visibility : "inherit" } ) ;
2018-09-08 19:15:23 +02:00
whiteboard . clearWhiteboard ( ) ;
2018-02-08 20:04:13 +01:00
} ) ;
2019-01-11 11:24:04 +01:00
// undo button
2018-09-08 19:15:23 +02:00
$ ( "#whiteboardUndoBtn" ) . click ( function ( ) {
whiteboard . undoWhiteboardClick ( ) ;
2018-02-08 20:04:13 +01:00
} ) ;
2018-09-08 19:15:23 +02:00
2020-04-17 19:47:19 +02:00
// redo button
$ ( "#whiteboardRedoBtn" ) . click ( function ( ) {
whiteboard . redoWhiteboardClick ( ) ;
} ) ;
2019-01-11 11:24:04 +01:00
// switch tool
2018-09-08 19:15:23 +02:00
$ ( ".whiteboardTool" ) . click ( function ( ) {
2018-02-08 20:04:13 +01:00
$ ( ".whiteboardTool" ) . removeClass ( "active" ) ;
$ ( this ) . addClass ( "active" ) ;
2019-02-05 13:51:59 +01:00
var activeTool = $ ( this ) . attr ( "tool" ) ;
whiteboard . setTool ( activeTool ) ;
2019-04-05 14:56:09 +02:00
if ( activeTool == "mouse" || activeTool == "recSelect" ) {
2019-02-05 13:51:59 +01:00
$ ( ".activeToolIcon" ) . empty ( ) ;
} else {
$ ( ".activeToolIcon" ) . html ( $ ( this ) . html ( ) ) ; //Set Active icon the same as the button icon
2019-04-05 14:56:09 +02:00
}
2018-02-08 20:04:13 +01:00
} ) ;
2019-01-11 11:24:04 +01:00
// upload image button
2018-09-08 19:15:23 +02:00
$ ( "#addImgToCanvasBtn" ) . click ( function ( ) {
2019-06-02 17:26:26 +02:00
showBasicAlert ( "Please drag the image into the browser." ) ;
2018-02-08 20:04:13 +01:00
} ) ;
2019-05-07 08:36:42 +02:00
// save image as png
2018-09-08 19:15:23 +02:00
$ ( "#saveAsImageBtn" ) . click ( function ( ) {
var imgData = whiteboard . getImageDataBase64 ( ) ;
2019-04-05 14:56:09 +02:00
var w = window . open ( 'about:blank' ) ; //Firefox will not allow downloads without extra window
setTimeout ( function ( ) { //FireFox seems to require a setTimeout for this to work.
var a = document . createElement ( 'a' ) ;
a . href = imgData ;
a . download = 'whiteboard.png' ;
w . document . body . appendChild ( a ) ;
a . click ( ) ;
w . document . body . removeChild ( a ) ;
setTimeout ( function ( ) { w . close ( ) ; } , 100 ) ;
} , 0 ) ;
2018-02-08 20:04:13 +01:00
} ) ;
2019-01-11 11:24:04 +01:00
// save image to json containing steps
2018-09-08 19:15:23 +02:00
$ ( "#saveAsJSONBtn" ) . click ( function ( ) {
var imgData = whiteboard . getImageDataJson ( ) ;
2019-04-05 14:56:09 +02:00
var w = window . open ( 'about:blank' ) ; //Firefox will not allow downloads without extra window
setTimeout ( function ( ) { //FireFox seems to require a setTimeout for this to work.
var a = document . createElement ( 'a' ) ;
a . href = window . URL . createObjectURL ( new Blob ( [ imgData ] , { type : 'text/json' } ) ) ;
a . download = 'whiteboard.json' ;
w . document . body . appendChild ( a ) ;
a . click ( ) ;
w . document . body . removeChild ( a ) ;
setTimeout ( function ( ) { w . close ( ) ; } , 100 ) ;
} , 0 ) ;
2018-09-08 19:15:23 +02:00
} ) ;
2018-02-08 23:25:57 +01:00
2019-07-01 13:15:11 +02:00
$ ( "#uploadWebDavBtn" ) . click ( function ( ) {
if ( $ ( ".webdavUploadBtn" ) . length > 0 ) {
return ;
}
2020-03-20 17:29:20 +01:00
2019-07-01 13:15:11 +02:00
var webdavserver = localStorage . getItem ( 'webdavserver' ) || ""
var webdavpath = localStorage . getItem ( 'webdavpath' ) || "/"
var webdavusername = localStorage . getItem ( 'webdavusername' ) || ""
2020-03-20 17:29:20 +01:00
var webdavpassword = localStorage . getItem ( 'webdavpassword' ) || ""
2019-07-01 13:15:11 +02:00
var webDavHtml = $ ( '<div>' +
'<table>' +
'<tr>' +
2019-07-01 13:24:02 +02:00
'<td>Server URL:</td>' +
2020-03-20 17:29:20 +01:00
'<td><input class="webdavserver" type="text" value="' + webdavserver + '" placeholder="https://yourserver.com/remote.php/webdav/"></td>' +
2019-07-01 13:15:11 +02:00
'<td></td>' +
'</tr>' +
'<tr>' +
'<td>Path:</td>' +
2020-03-20 17:29:20 +01:00
'<td><input class="webdavpath" type="text" placeholder="folder" value="' + webdavpath + '"></td>' +
2019-07-01 13:15:11 +02:00
'<td style="font-size: 0.7em;"><i>path always have to start & end with "/"</i></td>' +
'</tr>' +
'<tr>' +
'<td>Username:</td>' +
2020-03-20 17:29:20 +01:00
'<td><input class="webdavusername" type="text" value="' + webdavusername + '" placeholder="username"></td>' +
2019-07-01 13:15:11 +02:00
'<td style="font-size: 0.7em;"></td>' +
'</tr>' +
'<tr>' +
'<td>Password:</td>' +
2020-03-20 17:29:20 +01:00
'<td><input class="webdavpassword" type="password" value="' + webdavpassword + '" placeholder="password"></td>' +
2019-07-01 13:15:11 +02:00
'<td style="font-size: 0.7em;"></td>' +
'</tr>' +
'<tr>' +
'<td style="font-size: 0.7em;" colspan="3">Note: You have to generate and use app credentials if you have 2 Factor Auth activated on your dav/nextcloud server!</td>' +
'</tr>' +
'<tr>' +
'<td></td>' +
'<td colspan="2"><span class="loadingWebdavText" style="display:none;">Saving to webdav, please wait...</span><button class="modalBtn webdavUploadBtn"><i class="fas fa-upload"></i> Start Upload</button></td>' +
'</tr>' +
'</table>' +
'</div>' ) ;
webDavHtml . find ( ".webdavUploadBtn" ) . click ( function ( ) {
var webdavserver = webDavHtml . find ( ".webdavserver" ) . val ( ) ;
localStorage . setItem ( 'webdavserver' , webdavserver ) ;
var webdavpath = webDavHtml . find ( ".webdavpath" ) . val ( ) ;
localStorage . setItem ( 'webdavpath' , webdavpath ) ;
var webdavusername = webDavHtml . find ( ".webdavusername" ) . val ( ) ;
localStorage . setItem ( 'webdavusername' , webdavusername ) ;
var webdavpassword = webDavHtml . find ( ".webdavpassword" ) . val ( ) ;
localStorage . setItem ( 'webdavpassword' , webdavpassword ) ;
var base64data = whiteboard . getImageDataBase64 ( ) ;
var webdavaccess = {
webdavserver : webdavserver ,
webdavpath : webdavpath ,
webdavusername : webdavusername ,
webdavpassword : webdavpassword
}
webDavHtml . find ( ".loadingWebdavText" ) . show ( ) ;
webDavHtml . find ( ".webdavUploadBtn" ) . hide ( ) ;
saveWhiteboardToWebdav ( base64data , webdavaccess , function ( err ) {
if ( err ) {
webDavHtml . find ( ".loadingWebdavText" ) . hide ( ) ;
webDavHtml . find ( ".webdavUploadBtn" ) . show ( ) ;
} else {
webDavHtml . parents ( ".basicalert" ) . remove ( ) ;
}
} ) ;
} )
showBasicAlert ( webDavHtml , {
header : "Save to Webdav" ,
okBtnText : "cancel" ,
headercolor : "#0082c9"
} )
} ) ;
2019-01-11 11:24:04 +01:00
// upload json containing steps
2018-09-08 19:15:23 +02:00
$ ( "#uploadJsonBtn" ) . click ( function ( ) {
$ ( "#myFile" ) . click ( ) ;
2018-02-08 23:25:57 +01:00
} ) ;
2019-04-05 14:56:09 +02:00
2019-03-04 19:50:38 +01:00
$ ( "#shareWhiteboardBtn" ) . click ( function ( ) {
var url = window . location . href ;
2019-04-05 14:56:09 +02:00
var s = url . indexOf ( "&username=" ) !== - 1 ? "&username=" : "username=" ; //Remove username from url
2019-03-04 19:50:38 +01:00
var urlSlpit = url . split ( s ) ;
var urlStart = urlSlpit [ 0 ] ;
2019-04-05 14:56:09 +02:00
if ( urlSlpit . length > 1 ) {
2019-03-04 19:50:38 +01:00
var endSplit = urlSlpit [ 1 ] . split ( "&" ) ;
endSplit = endSplit . splice ( 1 , 1 ) ;
2019-04-05 14:56:09 +02:00
urlStart += "&" + endSplit . join ( "&" ) ;
2019-03-04 19:50:38 +01:00
}
$ ( "<textarea/>" ) . appendTo ( "body" ) . val ( urlStart ) . select ( ) . each ( function ( ) {
document . execCommand ( 'copy' ) ;
} ) . remove ( ) ;
2020-03-24 20:22:37 +01:00
showBasicAlert ( "Copied Whiteboard-URL to clipboard." , { hideAfter : 2 } )
2019-03-04 19:50:38 +01:00
} ) ;
2018-02-08 23:25:57 +01:00
2019-05-13 11:39:33 +02:00
var btnsMini = false ;
2019-06-02 16:49:48 +02:00
$ ( "#minMaxBtn" ) . click ( function ( ) {
if ( ! btnsMini ) {
2019-05-13 11:39:33 +02:00
$ ( "#toolbar" ) . find ( ".btn-group:not(.minGroup)" ) . hide ( ) ;
$ ( this ) . find ( "#minBtn" ) . hide ( ) ;
$ ( this ) . find ( "#maxBtn" ) . show ( ) ;
} else {
$ ( "#toolbar" ) . find ( ".btn-group" ) . show ( ) ;
$ ( this ) . find ( "#minBtn" ) . show ( ) ;
$ ( this ) . find ( "#maxBtn" ) . hide ( ) ;
}
btnsMini = ! btnsMini ;
} )
2019-01-11 11:24:04 +01:00
// load json to whiteboard
2018-09-08 19:15:23 +02:00
$ ( "#myFile" ) . on ( "change" , function ( ) {
var file = document . getElementById ( "myFile" ) . files [ 0 ] ;
var reader = new FileReader ( ) ;
reader . onload = function ( e ) {
try {
var j = JSON . parse ( e . target . result ) ;
whiteboard . loadJsonData ( j ) ;
} catch ( e ) {
2019-06-02 17:26:26 +02:00
showBasicAlert ( "File was not a valid JSON!" ) ;
2018-09-08 19:15:23 +02:00
}
} ;
reader . readAsText ( file ) ;
$ ( this ) . val ( "" ) ;
2018-02-08 23:25:57 +01:00
} ) ;
2018-02-08 20:04:13 +01:00
2019-01-11 11:24:04 +01:00
// On thickness slider change
2019-02-22 13:49:40 +01:00
$ ( "#whiteboardThicknessSlider" ) . on ( "input" , function ( ) {
whiteboard . setStrokeThickness ( $ ( this ) . val ( ) ) ;
2019-01-11 11:24:04 +01:00
} ) ;
// handle drag&drop
2018-02-08 20:04:13 +01:00
var dragCounter = 0 ;
2018-09-08 19:15:23 +02:00
$ ( '#whiteboardContainer' ) . on ( "dragenter" , function ( e ) {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
2018-02-08 20:04:13 +01:00
dragCounter ++ ;
whiteboard . dropIndicator . show ( ) ;
} ) ;
2018-09-08 19:15:23 +02:00
$ ( '#whiteboardContainer' ) . on ( "dragleave" , function ( e ) {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
dragCounter -- ;
if ( dragCounter === 0 ) {
whiteboard . dropIndicator . hide ( ) ;
}
} ) ;
2019-01-11 11:24:04 +01:00
$ ( '#whiteboardContainer' ) . on ( 'drop' , function ( e ) { //Handle drop
2018-09-08 19:15:23 +02:00
if ( e . originalEvent . dataTransfer ) {
if ( e . originalEvent . dataTransfer . files . length ) { //File from harddisc
2018-02-08 20:04:13 +01:00
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
var filename = e . originalEvent . dataTransfer . files [ 0 ] [ "name" ] ;
2018-09-08 19:15:23 +02:00
if ( isImageFileName ( filename ) ) {
var blob = e . originalEvent . dataTransfer . files [ 0 ] ;
var reader = new window . FileReader ( ) ;
reader . readAsDataURL ( blob ) ;
reader . onloadend = function ( ) {
base64data = reader . result ;
uploadImgAndAddToWhiteboard ( base64data ) ;
}
} else {
2019-06-05 00:55:48 +02:00
showBasicAlert ( "File must be an image!" ) ;
2018-09-08 19:15:23 +02:00
}
2018-02-08 20:04:13 +01:00
} else { //File from other browser
2019-07-01 13:15:11 +02:00
2018-09-08 19:15:23 +02:00
var fileUrl = e . originalEvent . dataTransfer . getData ( 'URL' ) ;
var imageUrl = e . originalEvent . dataTransfer . getData ( 'text/html' ) ;
var rex = /src="?([^"\s]+)"?\s*/ ;
var url = rex . exec ( imageUrl ) ;
if ( url && url . length > 1 ) {
url = url [ 1 ] ;
} else {
url = "" ;
}
2018-02-08 20:04:13 +01:00
2018-09-08 19:15:23 +02:00
isValidImageUrl ( fileUrl , function ( isImage ) {
if ( isImage && isImageFileName ( url ) ) {
whiteboard . addImgToCanvasByUrl ( fileUrl ) ;
} else {
isValidImageUrl ( url , function ( isImage ) {
if ( isImage ) {
2019-06-05 00:55:48 +02:00
if ( isImageFileName ( url ) || url . startsWith ( "http" ) ) {
2018-09-08 19:15:23 +02:00
whiteboard . addImgToCanvasByUrl ( url ) ;
} else {
2019-06-05 00:55:48 +02:00
uploadImgAndAddToWhiteboard ( url ) ; //Last option maybe its base64
2018-09-08 19:15:23 +02:00
}
} else {
2019-06-05 00:55:48 +02:00
showBasicAlert ( "Can only upload Imagedata!" ) ;
2018-09-08 19:15:23 +02:00
}
} ) ;
}
} ) ;
}
2018-02-08 20:04:13 +01:00
}
dragCounter = 0 ;
whiteboard . dropIndicator . hide ( ) ;
} ) ;
2018-09-08 19:15:23 +02:00
2018-02-08 20:04:13 +01:00
$ ( '#whiteboardColorpicker' ) . colorPicker ( {
2018-09-08 19:15:23 +02:00
renderCallback : function ( elm ) {
2019-02-22 13:49:40 +01:00
whiteboard . setDrawColor ( elm . val ( ) ) ;
2018-09-08 19:15:23 +02:00
}
2018-02-08 20:04:13 +01:00
} ) ;
} ) ;
2018-02-08 21:53:48 +01:00
//Prevent site from changing tab on drag&drop
2018-09-08 19:15:23 +02:00
window . addEventListener ( "dragover" , function ( e ) {
e = e || event ;
e . preventDefault ( ) ;
} , false ) ;
window . addEventListener ( "drop" , function ( e ) {
e = e || event ;
e . preventDefault ( ) ;
} , false ) ;
2018-02-08 21:53:48 +01:00
2018-02-08 23:16:28 +01:00
function uploadImgAndAddToWhiteboard ( base64data ) {
2018-09-08 19:15:23 +02:00
var date = ( + new Date ( ) ) ;
$ . ajax ( {
2018-02-08 23:16:28 +01:00
type : 'POST' ,
2018-09-08 19:15:23 +02:00
url : document . URL . substr ( 0 , document . URL . lastIndexOf ( '/' ) ) + '/upload' ,
data : {
2018-02-08 23:16:28 +01:00
'imagedata' : base64data ,
2018-09-08 19:15:23 +02:00
'whiteboardId' : whiteboardId ,
2019-02-11 12:43:23 +01:00
'date' : date ,
2019-04-05 14:56:09 +02:00
'at' : accessToken
2018-09-08 19:15:23 +02:00
} ,
success : function ( msg ) {
var filename = whiteboardId + "_" + date + ".png" ;
whiteboard . addImgToCanvasByUrl ( document . URL . substr ( 0 , document . URL . lastIndexOf ( '/' ) ) + "/uploads/" + filename ) ; //Add image to canvas
2018-02-08 23:16:28 +01:00
console . log ( "Image uploaded!" ) ;
} ,
2018-09-08 19:15:23 +02:00
error : function ( err ) {
2019-06-05 00:55:48 +02:00
showBasicAlert ( "Failed to upload frame: " + JSON . stringify ( err ) ) ;
2018-02-08 23:16:28 +01:00
}
} ) ;
}
2019-07-01 13:15:11 +02:00
function saveWhiteboardToWebdav ( base64data , webdavaccess , callback ) {
var date = ( + new Date ( ) ) ;
$ . ajax ( {
type : 'POST' ,
url : document . URL . substr ( 0 , document . URL . lastIndexOf ( '/' ) ) + '/upload' ,
data : {
'imagedata' : base64data ,
'whiteboardId' : whiteboardId ,
'date' : date ,
'at' : accessToken ,
'webdavaccess' : JSON . stringify ( webdavaccess )
} ,
success : function ( msg ) {
showBasicAlert ( "Whiteboard was saved to Webdav!" , {
headercolor : "#5c9e5c"
} ) ;
console . log ( "Image uploaded for webdav!" ) ;
callback ( ) ;
} ,
error : function ( err ) {
if ( err . status == 403 ) {
showBasicAlert ( "Could not connect to Webdav folder! Please check the credentials and paths and try again!" ) ;
} else {
showBasicAlert ( "Unknown Webdav error! " , err ) ;
}
callback ( err ) ;
}
} ) ;
}
2019-01-11 11:24:04 +01:00
// verify if filename refers to an image
2018-02-08 20:04:13 +01:00
function isImageFileName ( filename ) {
2019-01-11 11:24:04 +01:00
var extension = filename . split ( "." ) [ filename . split ( "." ) . length - 1 ] ;
2019-07-01 10:52:39 +02:00
var known _extensions = [ "png" , "jpg" , "jpeg" , "gif" , "tiff" , "bmp" , "webp" ] ;
2019-01-11 11:24:04 +01:00
return known _extensions . includes ( extension . toLowerCase ( ) ) ;
2018-02-08 20:04:13 +01:00
}
2019-01-11 11:24:04 +01:00
// verify if given url is url to an image
function isValidImageUrl ( url , callback ) {
2018-02-08 20:04:13 +01:00
var img = new Image ( ) ;
var timer = null ;
img . onerror = img . onabort = function ( ) {
clearTimeout ( timer ) ;
callback ( false ) ;
} ;
img . onload = function ( ) {
clearTimeout ( timer ) ;
callback ( true ) ;
} ;
timer = setTimeout ( function ( ) {
callback ( false ) ;
} , 2000 ) ;
img . src = url ;
}
2019-01-11 11:24:04 +01:00
// handle pasting from clipboard
window . addEventListener ( "paste" , function ( e ) {
2020-03-20 17:29:20 +01:00
if ( $ ( ".basicalert" ) . length > 0 ) {
2019-07-01 13:15:11 +02:00
return ;
}
2018-09-08 19:15:23 +02:00
if ( e . clipboardData ) {
var items = e . clipboardData . items ;
2019-06-02 17:26:26 +02:00
var imgItemFound = false ;
2018-09-08 19:15:23 +02:00
if ( items ) {
// Loop through all items, looking for any kind of image
for ( var i = 0 ; i < items . length ; i ++ ) {
if ( items [ i ] . type . indexOf ( "image" ) !== - 1 ) {
2019-06-02 17:26:26 +02:00
imgItemFound = true ;
2018-09-08 19:15:23 +02:00
// We need to represent the image as a file,
var blob = items [ i ] . getAsFile ( ) ;
var reader = new window . FileReader ( ) ;
reader . readAsDataURL ( blob ) ;
reader . onloadend = function ( ) {
console . log ( "Uploading image!" ) ;
base64data = reader . result ;
uploadImgAndAddToWhiteboard ( base64data ) ;
}
}
}
2018-02-08 20:04:13 +01:00
}
2019-06-02 17:26:26 +02:00
2019-07-01 13:15:11 +02:00
if ( ! imgItemFound && whiteboard . tool != "text" ) {
2019-06-05 23:30:46 +02:00
showBasicAlert ( "Please Drag&Drop the image into the Whiteboard. (Browsers don't allow copy+past from the filesystem directly)" ) ;
2019-06-02 17:26:26 +02:00
}
2018-09-08 19:15:23 +02:00
}
2018-02-08 23:38:41 +01:00
} ) ;
2019-07-01 13:15:11 +02:00
function showBasicAlert ( html , newOptions ) {
var options = {
header : "INFO MESSAGE" ,
okBtnText : "Ok" ,
2020-03-24 20:22:37 +01:00
headercolor : "#d25d5d" ,
hideAfter : false
2019-07-01 13:15:11 +02:00
}
if ( newOptions ) {
for ( var i in newOptions ) {
options [ i ] = newOptions [ i ] ;
}
}
var alertHtml = $ ( '<div class="basicalert" style="position:absolute; top:0px; left:0px; width:100%; top:70px; font-family: monospace;">' +
'<div style="width: 30%; margin: auto; background: #aaaaaa; border-radius: 5px; font-size: 1.2em; border: 1px solid gray;">' +
'<div style="border-bottom: 1px solid #676767; background: ' + options [ "headercolor" ] + '; padding-left: 5px; font-size: 0.8em;">' + options [ "header" ] + '</div>' +
'<div style="padding: 10px;" class="htmlcontent"></div>' +
'<div style="height: 20px; padding: 10px;"><button class="modalBtn okbtn" style="float: right;">' + options [ "okBtnText" ] + '</button></div>' +
2019-06-02 17:26:26 +02:00
'</div>' +
'</div>' ) ;
2019-07-01 13:15:11 +02:00
alertHtml . find ( ".htmlcontent" ) . append ( html ) ;
2019-06-02 17:26:26 +02:00
$ ( "body" ) . append ( alertHtml ) ;
2019-07-01 13:15:11 +02:00
alertHtml . find ( ".okbtn" ) . click ( function ( ) {
2019-06-02 17:26:26 +02:00
alertHtml . remove ( ) ;
} )
2020-03-24 20:22:37 +01:00
if ( options . hideAfter ) {
setTimeout ( function ( ) {
alertHtml . find ( ".okbtn" ) . click ( ) ;
} , 1000 * options . hideAfter )
}
2019-06-02 17:26:26 +02:00
}
2019-01-11 11:24:04 +01:00
// get 'GET' parameter by variable name
2018-02-08 23:38:41 +01:00
function getQueryVariable ( variable ) {
2018-09-08 19:15:23 +02:00
var query = window . location . search . substring ( 1 ) ;
var vars = query . split ( "&" ) ;
for ( var i = 0 ; i < vars . length ; i ++ ) {
var pair = vars [ i ] . split ( "=" ) ;
if ( pair [ 0 ] == variable ) {
return pair [ 1 ] ;
}
2018-02-08 23:38:41 +01:00
}
2018-09-08 19:15:23 +02:00
return false ;
2018-02-08 23:38:41 +01:00
}