2019-02-09 16:24:41 +00:00
function urlStatus ( url , cb ) {
$ . ajax ( {
url : url ,
dataType : 'text' ,
type : 'GET' ,
complete : function ( xhr ) {
if ( typeof cb === 'function' )
cb . apply ( this , [ xhr . status ] ) ;
}
} ) ;
}
function checkDownload ( url ) {
urlStatus ( url , function ( status ) {
if ( status === 200 ) {
2019-02-13 19:10:36 +00:00
var working = false ;
2019-02-09 16:24:41 +00:00
document . getElementById ( "download" ) . src = url ;
}
else {
2019-02-13 19:10:36 +00:00
var working = true ;
2019-02-09 16:24:41 +00:00
checkDownload ( url ) ;
}
} ) ;
}
2018-11-28 15:38:35 +00:00
function timeSince ( obj ) {
2019-01-13 16:56:00 +00:00
last = new Date ( Number ( obj ) ) ;
2019-01-13 16:28:52 +00:00
var seconds = Math . floor ( ( new Date ( ) - last ) / 1000 ) ;
interval = Math . floor ( seconds / 3600 ) ;
out = "" ;
2018-11-28 15:38:35 +00:00
2019-01-16 12:26:35 +00:00
if ( interval > 24 ) return "at " + last . toUTCString ( ) . substr ( 4 ) ;
2019-01-13 16:28:52 +00:00
if ( interval > 1 ) out = interval + " hours " ;
2018-11-28 15:38:35 +00:00
2019-01-13 16:28:52 +00:00
interval = Math . floor ( seconds / 60 ) ;
2018-11-28 15:38:35 +00:00
2019-01-13 16:28:52 +00:00
if ( seconds < 120 ) out = seconds + " seconds "
else if ( interval > 1 && interval < 120 ) out = out + interval + " minutes " ;
2018-11-28 15:38:35 +00:00
2019-02-11 14:09:56 +00:00
if ( out ) return out + "ago" ;
return "" ;
2018-11-28 15:38:35 +00:00
}
function styleStatus ( msg , device ) {
device _status = $ ( "#" + device + "-indicator" ) ;
device _ip = $ ( "#" + device + "-ip" ) ;
2018-12-15 11:35:00 +00:00
device _network = $ ( "#" + device + "-network" ) ;
device _name = $ ( "#" + device + "-name" ) ;
device _id = $ ( "#" + device + "-id" ) ;
2018-11-28 15:38:35 +00:00
2019-01-13 17:14:15 +00:00
device _status . css ( "color" , msg . status == 1 ? "green" : ( msg . status == 2 ? "yellow" : ( msg . status == 0 ? "red" : "grey" ) ) ) ;
2019-01-13 17:28:59 +00:00
device _status . prop ( "title" , msg . status == 1 ? "Online and in VPN mode" : ( msg . status == 2 ? "Online and in local mode" : ( msg . status == 0 ? "Offline" : "No information available" ) ) ) ;
2018-11-28 15:38:35 +00:00
if ( msg . hasOwnProperty ( "ip" ) ) {
device _ip . text ( msg . ip + ( msg . status == 1 ? "" : " (" + timeSince ( msg . time ) + ")" ) ) ;
} ;
2018-12-15 11:35:00 +00:00
if ( msg . hasOwnProperty ( "network" ) ) {
2019-01-17 08:47:41 +00:00
device _network . text ( msg . network . intip + ( msg . network . name ? ( " (" + msg . network . name + ")" ) : "" ) ) ;
2018-12-15 11:35:00 +00:00
} ;
if ( msg . hasOwnProperty ( "reboot" ) ) {
device _id . css ( "font-style" , msg . reboot == 1 ? "italic" : "normal" )
} ;
2018-12-26 01:24:36 +00:00
if ( msg . hasOwnProperty ( "update" ) ) {
device _id . css ( "font-weight" , msg . update == 1 ? "bold" : "normal" )
} ;
2018-12-15 11:35:00 +00:00
if ( msg . hasOwnProperty ( "name" ) ) {
device _name . text ( msg . name ) ;
} ;
2018-11-28 15:38:35 +00:00
} ;
function updateStatus ( device _id ) {
$ . getJSON ( "/devices/" + device _id + "/ping/" , function ( json ) { styleStatus ( json , device _id ) ; } ) ;
} ;
2018-11-28 21:35:57 +00:00
function askdelete ( device _id ) {
2019-02-02 12:31:05 +00:00
if ( confirm ( "Are you sure you want to delete this Device?" ) ) window . location . href = "/devices/" + device _id + "/delete" ;
2018-11-28 21:35:57 +00:00
} ;
2019-01-06 18:15:13 +00:00
function askdeletewifi ( wifi _id ) {
if ( confirm ( "Are you sure you want to delete this WiFi?" ) ) window . location . href = "/wifi/" + wifi _id + "/delete" ;
} ;
2019-02-02 12:31:05 +00:00
function askdeleteuser ( user _id ) {
if ( confirm ( "Are you sure you want to delete this User?" ) ) window . location . href = "/users/" + user _id + "/delete" ;
} ;
2019-02-02 15:01:18 +00:00
function askdeletenet ( network _id ) {
if ( confirm ( "Are you sure you want to delete this Network?" ) ) window . location . href = "/networks/" + network _id + "/delete" ;
} ;
2019-02-03 09:51:02 +00:00
function askdeleteorga ( organization _id ) {
if ( confirm ( "Are you sure you want to delete this Organization? Any associated Networks, WiFis, Devices and Users will also be deleted irreversibly!" ) ) window . location . href = "/organizations/" + organization _id + "/delete" ;
} ;
2018-11-28 21:35:57 +00:00
function askreboot ( device _id ) {
2019-02-02 12:31:05 +00:00
if ( confirm ( "Are you sure you want to reboot this Device?" ) ) window . location . href = "/devices/" + device _id + "/reboot" ;
2018-11-28 21:35:57 +00:00
} ;
2018-12-15 11:35:00 +00:00
2019-02-09 16:24:41 +00:00
function downloadnotice ( device _id ) {
2019-02-13 19:10:36 +00:00
if ( ! working ) {
alert ( "Your file is being prepared. This might take a minute or two, the download will start automatically when the file is ready. You may close this page and click the download button again later to pick the file up – we will store it for you." ) ;
checkDownload ( '/devices/' + device _id + '/download/' ) ;
} ;
2018-12-15 11:35:00 +00:00
} ;
2019-01-13 10:48:05 +00:00
function showdevices ( ) {
$ ( "#devicespart" ) . show ( ) ;
$ ( "#wifipart" ) . hide ( ) ;
2019-01-13 12:55:53 +00:00
$ ( "#userpart" ) . hide ( ) ;
2019-02-02 13:20:40 +00:00
$ ( "#netpart" ) . hide ( ) ;
2019-02-03 09:51:02 +00:00
$ ( "#orgapart" ) . hide ( ) ;
2019-01-16 08:27:33 +00:00
$ ( "#linkdevices" ) . css ( "font-weight" , "bold" ) ;
$ ( "#linkwifi" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkusers" ) . css ( "font-weight" , "normal" ) ;
2019-02-02 13:20:40 +00:00
$ ( "#linknets" ) . css ( "font-weight" , "normal" ) ;
2019-02-03 09:51:02 +00:00
$ ( "#linkorgas" ) . css ( "font-weight" , "normal" ) ;
2019-02-02 12:14:12 +00:00
window . history . pushState ( "" , "" , "/devices/" ) ;
2019-01-13 10:48:05 +00:00
} ;
function showwifi ( ) {
$ ( "#devicespart" ) . hide ( ) ;
$ ( "#wifipart" ) . show ( ) ;
2019-01-13 12:55:53 +00:00
$ ( "#userpart" ) . hide ( ) ;
2019-02-02 13:20:40 +00:00
$ ( "#netpart" ) . hide ( ) ;
2019-02-03 09:51:02 +00:00
$ ( "#orgapart" ) . hide ( ) ;
2019-01-16 08:27:33 +00:00
$ ( "#linkdevices" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkwifi" ) . css ( "font-weight" , "bold" ) ;
$ ( "#linkusers" ) . css ( "font-weight" , "normal" ) ;
2019-02-02 13:20:40 +00:00
$ ( "#linknets" ) . css ( "font-weight" , "normal" ) ;
2019-02-03 09:51:02 +00:00
$ ( "#linkorgas" ) . css ( "font-weight" , "normal" ) ;
2019-02-02 12:14:12 +00:00
window . history . pushState ( "" , "" , "/wifi/" ) ;
2019-01-13 12:55:53 +00:00
} ;
function showusers ( ) {
2019-01-13 16:28:52 +00:00
$ ( "#devicespart" ) . hide ( ) ;
$ ( "#wifipart" ) . hide ( ) ;
$ ( "#userpart" ) . show ( ) ;
2019-02-02 13:20:40 +00:00
$ ( "#netpart" ) . hide ( ) ;
2019-02-03 09:51:02 +00:00
$ ( "#orgapart" ) . hide ( ) ;
2019-01-16 08:27:33 +00:00
$ ( "#linkdevices" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkwifi" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkusers" ) . css ( "font-weight" , "bold" ) ;
2019-02-02 13:20:40 +00:00
$ ( "#linknets" ) . css ( "font-weight" , "normal" ) ;
2019-02-03 09:51:02 +00:00
$ ( "#linkorgas" ) . css ( "font-weight" , "normal" ) ;
2019-02-02 12:14:12 +00:00
window . history . pushState ( "" , "" , "/users/" ) ;
2019-01-13 10:48:05 +00:00
} ;
2019-01-13 16:28:52 +00:00
2019-02-02 13:20:40 +00:00
function shownets ( ) {
$ ( "#devicespart" ) . hide ( ) ;
$ ( "#wifipart" ) . hide ( ) ;
$ ( "#userpart" ) . hide ( ) ;
$ ( "#netpart" ) . show ( ) ;
2019-02-03 09:51:02 +00:00
$ ( "#orgapart" ) . hide ( ) ;
2019-02-02 13:20:40 +00:00
$ ( "#linkdevices" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkwifi" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkusers" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linknets" ) . css ( "font-weight" , "bold" ) ;
2019-02-03 09:51:02 +00:00
$ ( "#linkorgas" ) . css ( "font-weight" , "normal" ) ;
2019-02-02 13:20:40 +00:00
2019-02-02 13:24:15 +00:00
window . history . pushState ( "" , "" , "/networks/" ) ;
2019-02-02 13:20:40 +00:00
} ;
2019-02-03 09:51:02 +00:00
function showorgas ( ) {
$ ( "#devicespart" ) . hide ( ) ;
$ ( "#wifipart" ) . hide ( ) ;
$ ( "#userpart" ) . hide ( ) ;
$ ( "#netpart" ) . hide ( ) ;
$ ( "#orgapart" ) . show ( ) ;
$ ( "#linkdevices" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkwifi" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkusers" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linknets" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkorgas" ) . css ( "font-weight" , "bold" ) ;
window . history . pushState ( "" , "" , "/organizations/" ) ;
} ;
2019-02-19 13:19:43 +00:00
function filtertables ( ) {
orga _id = $ ( "#orgaselect option:selected" ) . val ( ) ;
$ ( "tbody tr" ) . each ( function ( i , row )
{
2019-02-19 14:01:18 +00:00
if ( $ ( row ) . attr ( 'class' ) == "orgarow" || $ ( row ) . data ( 'orga' ) && $ . inArray ( parseInt ( orga _id , "10" ) , $ ( row ) . data ( 'orga' ) ) !== - 1 ) $ ( row ) . show ( ) ;
2019-02-19 13:19:43 +00:00
else $ ( row ) . hide ( ) ;
} ) ;
} ;
2019-02-02 10:31:30 +00:00
var page = document . location . pathname . substring ( 1 , document . location . pathname . lastIndexOf ( '/' ) ) ;
if ( page == "users" ) showusers ( ) ;
else if ( page == "wifi" ) showwifi ( ) ;
2019-02-02 13:20:40 +00:00
else if ( page == "networks" ) shownets ( ) ;
2019-02-03 09:51:02 +00:00
else if ( page == "organizations" ) showorgas ( ) ;
2019-02-02 10:31:30 +00:00
else showdevices ( ) ;
2019-01-13 16:28:52 +00:00
2019-02-19 13:19:43 +00:00
filtertables ( ) ;
2019-01-13 16:28:52 +00:00
$ ( "div[id$='-indicator']" ) . each ( function ( ) {
device _id = this . id . split ( "-" ) [ 0 ] ;
updateStatus ( device _id ) ;
setInterval ( updateStatus , 10000 , device _id ) ;
} ) ;
2019-02-19 13:19:43 +00:00
$ ( "#orgaselect" ) . on ( 'change' , function ( ) {
filtertables ( ) ;
} ) ;