2017-10-11 18:27:42 +02:00
/ * *
* EGroupware - VFS SELECT Widget UI
*
2021-06-07 17:33:53 +02:00
* @ link https : //www.egroupware.org
2017-10-11 18:27:42 +02:00
* @ package et2 _vfsSelect
* @ author Hadi Nategh < hn @ egroupware . org >
2021-06-07 17:33:53 +02:00
* @ copyright ( c ) 2013 - 2021 by Hadi Nategh < hn @ egroupware . org >
2017-10-11 18:27:42 +02:00
* @ license http : //opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* /
2020-02-26 12:19:17 +01:00
/ * e g w : u s e s
/ a p i / j s / j s a p i / e g w _ a p p . j s
* /
2021-06-07 17:33:53 +02:00
import { EgwApp } from "../jsapi/egw_app" ;
2021-06-08 14:11:59 +02:00
import { egw } from "../jsapi/egw_global" ;
2021-06-07 17:33:53 +02:00
import { et2 _dialog } from "./et2_widget_dialog" ;
import { et2 _file } from "./et2_widget_file" ;
import { et2 _textbox } from "./et2_widget_textbox" ;
import { et2 _checkbox } from "./et2_widget_checkbox" ;
2017-10-11 18:27:42 +02:00
/ * *
* UI for VFS Select widget
*
* /
2021-06-07 17:33:53 +02:00
export class vfsSelectUI extends EgwApp {
2020-02-26 12:19:17 +01:00
/ * *
* Constructor
*
* /
2021-06-07 17:33:53 +02:00
constructor ( ) {
2020-02-26 12:19:17 +01:00
// call parent
2021-06-07 17:33:53 +02:00
super ( 'vfsSelectUI' ) ;
this . egw . langRequireApp ( this . egw . window , 'filemanager' ) ;
2020-02-26 12:19:17 +01:00
}
/ * *
* Destructor
* /
2021-06-07 17:33:53 +02:00
destroy ( _app ) {
2020-02-26 12:19:17 +01:00
delete this . path _widget ;
delete this . vfsSelectWidget ;
// call parent
2021-06-07 17:33:53 +02:00
super . destroy ( _app ) ;
}
2020-02-26 12:19:17 +01:00
/ * *
* This function is called when the etemplate2 object is loaded
* and ready . If you must store a reference to the et2 object ,
* make sure to clean it up in destroy ( ) .
*
* @ param et2 etemplate2 Newly ready object
* @ param { string } name template name
* /
2021-06-07 17:33:53 +02:00
et2 _ready ( et2 , name ) {
2020-02-26 12:19:17 +01:00
this . path _widget = this . et2 . getWidgetById ( 'path' ) ;
this . dirContent = this . et2 . getArrayMgr ( 'content' ) . data . dir ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Get directory of a path
*
* @ param { string } _path
* @ returns string
* /
2021-06-07 17:33:53 +02:00
dirname ( _path ) {
let parts = _path . split ( '/' ) ;
2020-02-26 12:19:17 +01:00
parts . pop ( ) ;
return parts . join ( '/' ) || '/' ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Get name of a path
*
* @ param { string } _path
* @ returns string
* /
2021-06-07 17:33:53 +02:00
basename ( _path ) {
2020-02-26 12:19:17 +01:00
return _path . split ( '/' ) . pop ( ) ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Get current working directory
*
* @ return string
* /
2021-06-07 17:33:53 +02:00
get _path ( ) {
2020-02-26 12:19:17 +01:00
return this . path _widget . get _value ( ) ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Send names of uploaded files ( again ) to server ,
* to process them : either copy to vfs or ask overwrite / rename
*
* @ param { event } _event
* /
2021-06-07 17:33:53 +02:00
storeFile ( _event ) {
let path = this . get _path ( ) ;
2020-02-26 12:19:17 +01:00
if ( ! jQuery . isEmptyObject ( _event . data . getValue ( ) ) ) {
2021-06-07 17:33:53 +02:00
let widget = _event . data ;
2020-02-26 12:19:17 +01:00
egw ( window ) . json ( 'EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_storeFile' , [ widget . getValue ( ) , path ] , this . _storeFile _callback , this , true , this ) . sendRequest ( true ) ;
widget . set _value ( '' ) ;
}
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Callback for server response to storeFile request :
* - display message and refresh list
* - ask use to confirm overwritting existing files or rename upload
*
* @ param { object } _data values for attributes msg , files , ...
* /
2021-06-07 17:33:53 +02:00
_storeFile _callback ( _data ) {
2020-02-26 12:19:17 +01:00
if ( _data . msg || _data . uploaded )
egw ( window ) . message ( _data . msg ) ;
2021-06-07 17:33:53 +02:00
let that = this ;
for ( let file in _data . uploaded ) {
2020-02-26 12:19:17 +01:00
if ( _data . uploaded [ file ] . confirm && ! _data . uploaded [ file ] . confirmed ) {
2021-06-07 17:33:53 +02:00
let buttons = [
2020-02-26 12:19:17 +01:00
{ text : this . egw . lang ( "Yes" ) , id : "overwrite" , class : "ui-priority-primary" , "default" : true , image : 'check' } ,
{ text : this . egw . lang ( "Rename" ) , id : "rename" , image : 'edit' } ,
{ text : this . egw . lang ( "Cancel" ) , id : "cancel" }
] ;
if ( _data . uploaded [ file ] . confirm === "is_dir" )
buttons . shift ( ) ;
2021-06-07 17:33:53 +02:00
let dialog = et2 _dialog . show _prompt ( function ( _button _id , _value ) {
let uploaded = { } ;
2020-02-26 12:19:17 +01:00
uploaded [ this . my _data . file ] = this . my _data . data ;
switch ( _button _id ) {
case "overwrite" :
uploaded [ this . my _data . file ] . confirmed = true ;
// fall through
case "rename" :
uploaded [ this . my _data . file ] . name = _value ;
delete uploaded [ this . my _data . file ] . confirm ;
// send overwrite-confirmation and/or rename request to server
egw . json ( 'EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_vfsSelect_storeFile' , [ uploaded , this . my _data . path ] , that . _storeFile _callback , that , true , that ) . sendRequest ( ) ;
return ;
case "cancel" :
// Remove that file from every file widget...
that . et2 . iterateOver ( function ( _widget ) {
_widget . remove _file ( this . my _data . data . name ) ;
2021-06-07 17:33:53 +02:00
} , this , et2 _file ) ;
2020-02-26 12:19:17 +01:00
}
} , _data . uploaded [ file ] . confirm === "is_dir" ?
this . egw . lang ( "There's already a directory with that name!" ) :
this . egw . lang ( 'Do you want to overwrite existing file %1 in directory %2?' , _data . uploaded [ file ] . name , _data . path ) , this . egw . lang ( 'File %1 already exists' , _data . uploaded [ file ] . name ) , _data . uploaded [ file ] . name , buttons , file ) ;
// setting required data for callback in as my_data
dialog . my _data = {
file : file ,
path : _data . path ,
data : _data . uploaded [ file ] ,
} ;
}
else {
this . submit ( ) ;
}
}
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Prompt user for directory to create
*
* @ param { egwAction | undefined | jQuery . Event } action Action , event or undefined if called directly
* @ param { egwActionObject [ ] | undefined } selected Selected row , or undefined if called directly
* /
2021-06-07 17:33:53 +02:00
createdir ( action , selected ) {
let self = this ;
et2 _dialog . show _prompt ( function ( button , dir ) {
2020-02-26 12:19:17 +01:00
if ( button && dir ) {
2021-06-07 17:33:53 +02:00
let path = self . get _path ( ) ;
self . egw . json ( 'EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_create_dir' , [ dir , path ] , function ( msg ) {
2020-02-26 12:19:17 +01:00
self . egw . message ( msg ) ;
2021-06-07 17:33:53 +02:00
self . change _dir ( ( path == '/' ? '' : path ) + '/' + dir ) ;
2020-02-26 12:19:17 +01:00
} ) . sendRequest ( false ) ;
}
} , this . egw . lang ( 'New directory' ) , this . egw . lang ( 'Create directory' ) ) ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Change directory
*
* @ param { string } _dir directory to change to incl . '..' for one up
* /
2021-06-07 17:33:53 +02:00
change _dir ( _dir ) {
2020-02-26 12:19:17 +01:00
if ( _dir == '..' ) {
_dir = this . dirname ( this . get _path ( ) ) ;
}
this . path _widget . set _value ( _dir ) ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Row or filename in select - file dialog clicked
*
* @ param { jQuery . event } event
* @ param { et2 _widget } widget
* /
2021-06-07 17:33:53 +02:00
select _clicked ( event , widget ) {
2020-02-26 12:19:17 +01:00
if ( ! widget || typeof widget . value != 'object' ) {
}
else if ( widget . value . is _dir ) // true for "httpd/unix-directory" and "egw/*"
{
2021-06-07 17:33:53 +02:00
let path = null ;
2020-02-26 12:19:17 +01:00
// Cannot do this, there are multiple widgets named path
// widget.getRoot().getWidgetById("path");
widget . getRoot ( ) . iterateOver ( function ( widget ) {
if ( widget . id == "path" )
2021-06-07 17:33:53 +02:00
path = widget ;
} , null , et2 _textbox ) ;
if ( path ) {
path . set _value ( widget . value . path ) ;
2020-02-26 12:19:17 +01:00
}
}
else if ( this . et2 && this . et2 . getArrayMgr ( 'content' ) . getEntry ( 'mode' ) != 'open-multiple' ) {
2020-05-29 19:21:17 +02:00
this . et2 . setValueById ( 'name' , widget . value . name ) ;
2020-02-26 12:19:17 +01:00
}
else {
2021-06-07 17:33:53 +02:00
let file = widget . value . name ;
2020-02-26 12:19:17 +01:00
widget . getParent ( ) . iterateOver ( function ( widget ) {
2021-06-07 17:33:53 +02:00
if ( widget . options . selected _value == file ) {
widget . set _value ( widget . get _value ( ) == file ? widget . options . unselected _value : file ) ;
2020-02-26 12:19:17 +01:00
}
2021-06-07 17:33:53 +02:00
} , null , et2 _checkbox ) ;
2020-02-26 12:19:17 +01:00
}
// Stop event or it will toggle back off
event . preventDefault ( ) ;
event . stopPropagation ( ) ;
return false ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Handles action and offer it to the submit
*
* @ param { string } action action name
* @ param { object } widget widget which action was called from
* /
2021-06-07 17:33:53 +02:00
do _action ( action , widget ) {
2020-02-26 12:19:17 +01:00
if ( ! action )
return ;
2021-06-07 17:33:53 +02:00
let field = '' , value = '' ;
2020-02-26 12:19:17 +01:00
switch ( action ) {
case 'path' :
field = 'path' ;
value = widget . getValue ( ) ;
break ;
case 'home' :
field = 'action' ;
value = 'home' ;
break ;
case 'app' :
field = 'app' ;
value = widget . getValue ( ) ;
break ;
case 'mime' :
field = 'mime' ;
value = widget . getValue ( ) ;
break ;
}
this . submit ( field , value ) ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* Sumbits content value after modification
*
* @ param { string } _field content field to be modified
* @ param { any } _val value of field
* @ param { function } _callback
* /
2021-06-07 17:33:53 +02:00
submit ( _field , _val , _callback ) {
let arrMgrs = this . et2 . getArrayMgrs ( ) ;
2020-02-26 12:19:17 +01:00
if ( _field ) {
arrMgrs . content . data [ _field ] = _val ;
jQuery . extend ( arrMgrs . content . data , arrMgrs . modifications . data ) ;
this . et2 . setArrayMgrs ( arrMgrs ) ;
}
// preserve value of the name
if ( arrMgrs && this . et2 . getWidgetById ( 'name' ) ) {
arrMgrs . content . data [ 'name' ] = this . et2 . getWidgetById ( 'name' ) . get _value ( ) ;
}
this . vfsSelectWidget . _content ( arrMgrs . content . data , _callback ) ;
2021-06-07 17:33:53 +02:00
}
2020-02-26 12:19:17 +01:00
/ * *
* search through dir content and set its content base on searched query
* @ returns
* /
2021-06-07 17:33:53 +02:00
search ( _widget ) {
let dir = this . et2 . getWidgetById ( 'dir' ) ;
let query = _widget . get _value ( ) ;
2020-02-26 12:19:17 +01:00
if ( query == "" ) {
dir . set _value ( { content : this . dirContent } ) ;
return ;
}
2021-06-07 17:33:53 +02:00
let self = this ;
let searchQuery = function ( _query ) {
let result = { } ;
let reg = RegExp ( _query , 'ig' ) ;
let key = 0 ;
for ( let i in self . dirContent ) {
2020-02-26 12:19:17 +01:00
if ( typeof self . dirContent [ i ] [ 'name' ] != 'undefined' && self . dirContent [ i ] [ 'name' ] . match ( reg ) ) {
result [ key ] = self . dirContent [ i ] ;
key ++ ;
}
else if ( typeof self . dirContent [ i ] [ 'name' ] == 'undefined' && isNaN ( i ) ) {
result [ i ] = self . dirContent [ i ] ;
}
}
return result ;
} ;
dir . set _value ( { content : searchQuery ( query ) } ) ;
2021-06-07 17:33:53 +02:00
}
}
2020-02-26 12:19:17 +01:00
app . classes . vfsSelectUI = vfsSelectUI ;
//# sourceMappingURL=vfsSelectUI.js.map