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-01-13 16:56:00 +00:00
return out + "ago" ;
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" ;
} ;
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
function downloadnotice ( ) {
alert ( "Your file is being prepared. This will take a minute, please be patient and do not leave this page. The download will start automatically." ) ;
} ;
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-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-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-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-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-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-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 ( ) ;
$ ( "#linkdevices" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkwifi" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linkusers" ) . css ( "font-weight" , "normal" ) ;
$ ( "#linknets" ) . css ( "font-weight" , "bold" ) ;
2019-02-02 13:24:15 +00:00
window . history . pushState ( "" , "" , "/networks/" ) ;
2019-02-02 13:20:40 +00:00
} ;
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-02 10:31:30 +00:00
else showdevices ( ) ;
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 ) ;
} ) ;