2020-01-02 22:20:37 +00:00
// This is free software, licensed under the Apache License, Version 2.0
'use strict' ;
2020-04-03 08:00:06 +00:00
'require view' ;
2020-01-02 22:20:37 +00:00
'require fs' ;
'require ui' ;
'require uci' ;
'require form' ;
'require tools.widgets as widgets' ;
2020-04-24 16:43:03 +00:00
var isReadonlyView = ! L . hasViewPermission ( ) || null ;
2020-04-03 08:00:06 +00:00
return view . extend ( {
2020-01-02 22:20:37 +00:00
handleDeleteModal : function ( m , iface , ev ) {
L . showModal ( _ ( 'Delete interface <em>%h</em>' ) . format ( iface ) , [
E ( 'p' , _ ( 'The interface will be removed from the database permanently. This cannot be undone.' ) ) ,
E ( 'div' , { 'class' : 'right' } , [
E ( 'div' , {
'class' : 'btn' ,
'click' : L . hideModal
} , _ ( 'Cancel' ) ) ,
' ' ,
E ( 'div' , {
'class' : 'btn cbi-button-negative' ,
'click' : ui . createHandlerFn ( this , 'handleDelete' , m , iface )
} , _ ( 'Delete' ) )
] )
] ) ;
} ,
handleDelete : function ( m , iface , ev ) {
return fs . exec ( '/usr/bin/vnstat' , [ '--remove' , '-i' , iface , '--force' ] )
. then ( L . bind ( m . render , m ) )
. catch ( function ( e ) {
ui . addNotification ( null , E ( 'p' , e . message ) ) ;
} )
. finally ( L . hideModal ) ;
} ,
render : function ( ) {
var m , s , o ;
m = new form . Map ( 'vnstat' , _ ( 'vnStat' ) , _ ( 'vnStat is a network traffic monitor for Linux that keeps a log of network traffic for the selected interface(s).' ) ) ;
s = m . section ( form . TypedSection , 'vnstat' , _ ( 'Interfaces' ) ) ;
s . anonymous = true ;
s . addremove = false ;
o = s . option ( widgets . DeviceSelect , 'interface' , _ ( 'Monitor interfaces' ) , _ ( 'The selected interfaces are automatically added to the vnStat database upon startup.' ) ) ;
o . rmempty = true ;
o . multiple = true ;
o . noaliases = true ;
o . nobridges = false ;
o . noinactive = false ;
o . nocreate = false ;
o = s . option ( form . DummyValue , '_database' ) ;
o . load = function ( section _id ) {
return fs . exec ( '/usr/bin/vnstat' , [ '--json' , 'f' , '1' ] ) . then ( L . bind ( function ( result ) {
var databaseInterfaces = [ ] ;
if ( result . code == 0 ) {
var vnstatData = JSON . parse ( result . stdout ) ;
for ( var i = 0 ; i < vnstatData . interfaces . length ; i ++ ) {
databaseInterfaces . push ( vnstatData . interfaces [ i ] . name ) ;
}
}
var configInterfaces = uci . get _first ( 'vnstat' , 'vnstat' , 'interface' ) || [ ] ;
this . interfaces = databaseInterfaces . filter ( function ( iface ) {
return configInterfaces . indexOf ( iface ) == - 1 ;
} ) ;
} , this ) ) ;
} ;
o . render = L . bind ( function ( view , section _id ) {
2020-11-27 19:54:18 +00:00
var table = E ( 'table' , { 'class' : 'table' } , [
E ( 'tr' , { 'class' : 'tr table-titles' } , [
E ( 'th' , { 'class' : 'th' } , _ ( 'Interface' ) ) ,
E ( 'th' , { 'class' : 'th right' } , _ ( 'Delete' ) )
2020-01-02 22:20:37 +00:00
] )
] ) ;
var rows = [ ] ;
for ( var i = 0 ; i < this . interfaces . length ; i ++ ) {
rows . push ( [
this . interfaces [ i ] ,
E ( 'button' , {
'class' : 'btn cbi-button-remove' ,
2020-04-24 16:43:03 +00:00
'click' : ui . createHandlerFn ( view , 'handleDeleteModal' , m , this . interfaces [ i ] ) ,
'disabled' : isReadonlyView
2020-01-02 22:20:37 +00:00
} , [ _ ( 'Delete…' ) ] )
] ) ;
}
cbi _update _table ( table , rows , E ( 'em' , _ ( 'No unconfigured interfaces found in database.' ) ) ) ;
return E ( [ ] , [
E ( 'h3' , _ ( 'Unconfigured interfaces' ) ) ,
E ( 'div' , { 'class' : 'cbi-section-descr' } ,
_ ( 'These interfaces are present in the vnStat database, but are not configured above.' ) ) ,
table
] ) ;
} , o , this ) ;
return m . render ( ) ;
}
} ) ;