2020-10-07 12:42:39 +00:00
'use strict' ;
'require view' ;
'require rpc' ;
'require ui' ;
'require uci' ;
'require fs' ;
return view . extend ( {
translateTable : {
NO _BOARD _NAME : function ( args ) { return _ ( 'Unable to find Device Board Name.' ) } ,
2021-04-07 03:59:42 +00:00
NO _DUAL _FLAG : function ( args ) { return _ ( 'Unable to find Dual Boot Flag Partition.' ) } ,
NO _DUAL _FLAG _BLOCK : function ( args ) { return _ ( 'The Dual Boot Flag Partition: %s is not a block device.' ) . format ( args [ 0 ] ) } ,
2020-10-07 12:42:39 +00:00
ERR _SET _DUAL _FLAG : function ( args ) { return _ ( 'Unable to set Dual Boot Flag Partition entry for partition: %s.' ) . format ( args [ 0 ] ) } ,
NO _FIRM _ENV : function ( args ) { return _ ( 'Unable to obtain firmware environment variable: %s.' ) . format ( args [ 0 ] ) } ,
ERR _SET _ENV : function ( args ) { return _ ( 'Unable to set firmware environment variable: %s to %s.' ) . format ( args [ 0 ] , args [ 1 ] ) }
} ,
callReboot : rpc . declare ( {
object : 'system' ,
method : 'reboot' ,
expect : { result : 0 }
} ) ,
callObtainDeviceInfo : rpc . declare ( {
object : 'luci.advanced_reboot' ,
method : 'obtain_device_info' ,
expect : { }
} ) ,
callTogglePartition : rpc . declare ( {
object : 'luci.advanced_reboot' ,
method : 'toggle_boot_partition' ,
expect : { }
} ) ,
callPowerOff : function ( ) {
return fs . exec ( '/sbin/poweroff' ) . then ( function ( ) {
ui . showModal ( _ ( 'Shutting down...' ) , [
E ( 'p' , { 'class' : 'spinning' } , _ ( 'The system is shutting down now.<br /> DO NOT POWER OFF THE DEVICE!<br /> It might be necessary to renew the address of your computer to reach the device again, depending on your settings.' ) )
] ) ;
} )
} ,
handlePowerOff : function ( ) {
ui . showModal ( _ ( 'Power Off Device' ) , [
E ( 'p' , _ ( " WARNING : Power off might result in a reboot on a device which doesn ' t support power off . < br / > < br / > \
Click \ "Proceed\" below to power off your device." ) ) ,
E ( 'div' , { 'class' : 'right' } , [
E ( 'button' , {
'class' : 'btn' ,
'click' : ui . hideModal
} , _ ( 'Cancel' ) ) , ' ' ,
E ( 'button' , {
2021-02-08 17:37:13 +00:00
'class' : 'btn cbi-button cbi-button-positive important' ,
2020-10-07 12:42:39 +00:00
'click' : L . bind ( this . callPowerOff , this )
} , _ ( 'Proceed' ) )
] )
] ) ;
} ,
handleReboot : function ( ev ) {
return this . callReboot ( ) . then ( function ( res ) {
if ( res != 0 ) {
ui . addNotification ( null , E ( 'p' , _ ( 'The reboot command failed with code %d' ) . format ( res ) ) ) ;
L . raise ( 'Error' , 'Reboot failed' ) ;
}
ui . showModal ( _ ( 'Rebooting…' ) , [
E ( 'p' , { 'class' : 'spinning' } , _ ( 'Waiting for device...' ) )
] ) ;
window . setTimeout ( function ( ) {
ui . showModal ( _ ( 'Rebooting…' ) , [
E ( 'p' , { 'class' : 'spinning alert-message warning' } ,
_ ( 'Device unreachable! Still waiting for device...' ) )
] ) ;
} , 150000 ) ;
ui . awaitReconnect ( ) ;
} )
. catch ( function ( e ) { ui . addNotification ( null , E ( 'p' , e . message ) ) } ) ;
} ,
handleTogglePartition : function ( ev ) {
return this . callTogglePartition ( ) . then ( L . bind ( function ( res ) {
if ( res . error ) {
ui . hideModal ( )
return ui . addNotification ( null , E ( 'p' , this . translateTable [ res . error ] ( res . args ) ) ) ;
}
return this . callReboot ( ) . then ( function ( res ) {
if ( res != 0 ) {
ui . addNotification ( null , E ( 'p' , _ ( 'The reboot command failed with code %d' ) . format ( res ) ) ) ;
L . raise ( 'Error' , 'Reboot failed' ) ;
}
ui . showModal ( _ ( 'Rebooting…' ) , [
E ( 'p' , { 'class' : 'spinning' } , _ ( 'The system is rebooting to an alternative partition now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings.' ) )
] ) ;
window . setTimeout ( function ( ) {
ui . showModal ( _ ( 'Rebooting…' ) , [
E ( 'p' , { 'class' : 'spinning alert-message warning' } ,
_ ( 'Device unreachable! Still waiting for device...' ) )
] ) ;
} , 150000 ) ;
ui . awaitReconnect ( ) ;
} )
. catch ( function ( e ) { ui . addNotification ( null , E ( 'p' , e . message ) ) } ) ;
} , this ) ) ;
} ,
handleAlternativeReboot : function ( ev ) {
return Promise . all ( [
L . resolveDefault ( fs . stat ( '/usr/sbin/fw_printenv' ) , null ) ,
L . resolveDefault ( fs . stat ( '/usr/sbin/fw_setenv' ) , null ) ,
] ) . then ( L . bind ( function ( data ) {
if ( ! data [ 0 ] || ! data [ 1 ] ) {
return ui . addNotification ( null , E ( 'p' , _ ( 'No access to fw_printenv or fw_printenv!' ) ) ) ;
}
ui . showModal ( _ ( 'Reboot Device to an Alternative Partition' ) + " - " + _ ( "Confirm" ) , [
E ( 'p' , _ ( " WARNING : An alternative partition might have its own settings and completely different firmware . < br / > < br / > \
As your network configuration and WiFi SSID / password on alternative partition might be different , \
you might have to adjust your computer settings to be able to access your device once it reboots . < br / > < br / > \
Please also be aware that alternative partition firmware might not provide an easy way to switch active partition \
and boot back to the currently active partition . < br / > < br / > \
Click \ "Proceed\" below to reboot device to an alternative partition." ) ) ,
E ( 'div' , { 'class' : 'right' } , [
E ( 'button' , {
'class' : 'btn' ,
'click' : ui . hideModal
} , _ ( 'Cancel' ) ) , ' ' ,
E ( 'button' , {
2021-02-08 17:37:13 +00:00
'class' : 'btn cbi-button cbi-button-positive important' ,
2020-10-07 12:42:39 +00:00
'click' : L . bind ( this . handleTogglePartition , this )
} , _ ( 'Proceed' ) )
] )
] ) ;
} , this ) )
} ,
parsePartitions : function ( partitions ) {
var res = [ ] ;
partitions . forEach ( L . bind ( function ( partition ) {
var func , text ;
if ( partition . state == 'Current' ) {
func = 'handleReboot' ;
text = _ ( 'Reboot to current partition' ) ;
} else {
func = 'handleAlternativeReboot' ;
text = _ ( 'Reboot to alternative partition...' ) ;
}
res . push ( [
2021-01-06 16:56:10 +00:00
( partition . number + 0x100 ) . toString ( 16 ) . substr ( - 2 ) . toUpperCase ( ) ,
2020-10-07 12:42:39 +00:00
_ ( partition . state ) ,
partition . os . replace ( "Unknown" , _ ( "Unknown" ) ) . replace ( "Compressed" , _ ( "Compressed" ) ) ,
E ( 'button' , {
2021-02-08 17:37:13 +00:00
'class' : 'btn cbi-button cbi-button-apply important' ,
2020-10-07 12:42:39 +00:00
'click' : ui . createHandlerFn ( this , func )
} , text )
] )
} , this ) ) ;
return res ;
} ,
load : function ( ) {
return Promise . all ( [
uci . changes ( ) ,
L . resolveDefault ( fs . stat ( '/sbin/poweroff' ) , null ) ,
this . callObtainDeviceInfo ( )
] ) ;
} ,
render : function ( data ) {
var changes = data [ 0 ] ,
poweroff _supported = data [ 1 ] != null ? true : false ,
device _info = data [ 2 ] ;
var body = E ( [
E ( 'h2' , _ ( 'Advanced Reboot' ) )
] ) ;
for ( var config in ( changes || { } ) ) {
body . appendChild ( E ( 'p' , { 'class' : 'alert-message warning' } ,
_ ( 'Warning: There are unsaved changes that will get lost on reboot!' ) ) ) ;
break ;
}
if ( device _info . error )
body . appendChild ( E ( 'p' , { 'class' : 'alert-message warning' } , _ ( "ERROR: " ) + this . translateTable [ device _info . error ] ( ) ) ) ;
2021-10-06 21:43:38 +00:00
body . appendChild ( E ( 'h3' , ( device _info . device _name || '' ) + _ ( ' Partitions' ) ) ) ;
2020-10-07 12:42:39 +00:00
if ( device _info . device _name ) {
2020-11-27 19:54:18 +00:00
var partitions _table = E ( 'table' , { 'class' : 'table' } , [
E ( 'tr' , { 'class' : 'tr table-titles' } , [
E ( 'th' , { 'class' : 'th' } , [ _ ( 'Partition' ) ] ) ,
E ( 'th' , { 'class' : 'th' } , [ _ ( 'Status' ) ] ) ,
E ( 'th' , { 'class' : 'th' } , [ _ ( 'Firmware' ) ] ) ,
E ( 'th' , { 'class' : 'th' } , [ _ ( 'Reboot' ) ] )
2020-10-07 12:42:39 +00:00
] )
] ) ;
cbi _update _table ( partitions _table , this . parsePartitions ( device _info . partitions ) ) ;
body . appendChild ( partitions _table ) ;
} else {
body . appendChild ( E ( 'p' , { 'class' : 'alert-message warning' } ,
device _info . rom _board _name ? _ ( "Warning: Device (%s) is unknown or isn't a dual-partition device!" ) . format ( device _info . rom _board _name )
: _ ( 'Warning: Unable to obtain device information!' )
) ) ;
}
body . appendChild ( E ( 'hr' ) ) ;
body . appendChild (
poweroff _supported ? E ( 'button' , {
2021-02-08 17:37:13 +00:00
'class' : 'btn cbi-button cbi-button-apply important' ,
2020-10-07 12:42:39 +00:00
'click' : ui . createHandlerFn ( this , 'handlePowerOff' )
} , _ ( 'Perform power off...' ) )
: E ( 'p' , { 'class' : 'alert-message warning' } ,
_ ( 'Warning: This system does not support powering off!' ) )
) ;
return body ;
} ,
handleSaveApply : null ,
handleSave : null ,
handleReset : null
} ) ;