@@ -45,18 +45,29 @@ qx.Class.define("osparc.file.FileLabelWithActions", {
4545 this . getChildControl ( "selected-label" ) ;
4646
4747 const downloadBtn = this . getChildControl ( "download-button" ) ;
48- downloadBtn . addListener ( "execute" , ( ) => this . __retrieveURLAndDownload ( ) , this ) ;
48+ downloadBtn . addListener ( "execute" , ( ) => this . __retrieveURLAndDownloadSelected ( ) , this ) ;
4949
5050 const deleteBtn = this . getChildControl ( "delete-button" ) ;
51- deleteBtn . addListener ( "execute" , ( ) => this . __deleteFile ( ) , this ) ;
51+ deleteBtn . addListener ( "execute" , ( ) => this . __deleteSelected ( ) , this ) ;
5252 } ,
5353
5454 events : {
5555 "fileDeleted" : "qx.event.type.Data"
5656 } ,
5757
58+ properties : {
59+ multiSelect : {
60+ check : "Boolean" ,
61+ init : false ,
62+ nullable : false ,
63+ event : "changeMultiSelect" ,
64+ apply : "__enableMultiSelection" ,
65+ } ,
66+ } ,
67+
5868 members : {
5969 __selection : null ,
70+ __multiSelection : null ,
6071
6172 _createChildControlImpl : function ( id ) {
6273 let control ;
@@ -88,26 +99,59 @@ qx.Class.define("osparc.file.FileLabelWithActions", {
8899 return control || this . base ( arguments , id ) ;
89100 } ,
90101
102+ __enableMultiSelection : function ( ) {
103+ this . resetItemSelected ( ) ;
104+ this . __multiSelection = [ ] ;
105+ } ,
106+
91107 setItemSelected : function ( selectedItem ) {
92- const isFile = osparc . file . FilesTree . isFile ( selectedItem ) ;
93- this . getChildControl ( "download-button" ) . setEnabled ( isFile ) ;
94- this . getChildControl ( "delete-button" ) . setEnabled ( isFile ) ;
95- const selectedLabel = this . getChildControl ( "selected-label" ) ;
96- if ( isFile ) {
97- this . __selection = selectedItem ;
98- selectedLabel . set ( {
99- value : selectedItem . getLabel ( ) ,
100- toolTipText : selectedItem . getFileId ( )
101- } ) ;
108+ if ( selectedItem ) {
109+ const isFile = osparc . file . FilesTree . isFile ( selectedItem ) ;
110+ this . getChildControl ( "download-button" ) . setEnabled ( isFile ) ;
111+ this . getChildControl ( "delete-button" ) . setEnabled ( isFile ) ;
112+ const selectedLabel = this . getChildControl ( "selected-label" ) ;
113+ if ( isFile ) {
114+ this . __selection = selectedItem ;
115+ selectedLabel . set ( {
116+ value : selectedItem . getLabel ( ) ,
117+ toolTipText : selectedItem . getFileId ( )
118+ } ) ;
119+ } else {
120+ this . __selection = null ;
121+ selectedLabel . set ( {
122+ value : "" ,
123+ toolTipText : ""
124+ } ) ;
125+ }
102126 } else {
103- this . __selection = null ;
104- selectedLabel . set ( {
105- value : "" ,
106- toolTipText : ""
107- } ) ;
127+ this . resetItemSelected ( ) ;
128+ }
129+ } ,
130+
131+ setMultiItemSelected : function ( multiSelectionData ) {
132+ this . __multiSelection = multiSelectionData ;
133+ if ( multiSelectionData && multiSelectionData . length ) {
134+ if ( multiSelectionData . length === 1 ) {
135+ this . setItemSelected ( multiSelectionData [ 0 ] ) ;
136+ } else {
137+ const selectedLabel = this . getChildControl ( "selected-label" ) ;
138+ selectedLabel . set ( {
139+ value : multiSelectionData . length + " files"
140+ } ) ;
141+ }
142+ } else {
143+ this . resetItemSelected ( ) ;
108144 }
109145 } ,
110146
147+ resetItemSelected : function ( ) {
148+ this . __selection = null ;
149+ this . __multiSelection = [ ] ;
150+ this . getChildControl ( "download-button" ) . setEnabled ( false ) ;
151+ this . getChildControl ( "delete-button" ) . setEnabled ( false ) ;
152+ this . getChildControl ( "selected-label" ) . resetValue ( ) ;
153+ } ,
154+
111155 getItemSelected : function ( ) {
112156 const selectedItem = this . __selection ;
113157 if ( selectedItem && osparc . file . FilesTree . isFile ( selectedItem ) ) {
@@ -116,40 +160,71 @@ qx.Class.define("osparc.file.FileLabelWithActions", {
116160 return null ;
117161 } ,
118162
119- // Request to the server and download
120- __retrieveURLAndDownload : function ( ) {
121- let selection = this . getItemSelected ( ) ;
122- if ( selection ) {
123- const fileId = selection . getFileId ( ) ;
124- const locationId = selection . getLocation ( ) ;
125- osparc . utils . Utils . retrieveURLAndDownload ( locationId , fileId )
126- . then ( data => {
127- if ( data ) {
128- osparc . DownloadLinkTracker . getInstance ( ) . downloadLinkUnattended ( data . link , data . fileName ) ;
163+ __retrieveURLAndDownloadSelected : function ( ) {
164+ if ( this . isMultiSelect ( ) ) {
165+ this . __multiSelection . forEach ( selection => {
166+ this . __retrieveURLAndDownloadFile ( selection ) ;
167+ } ) ;
168+ } else {
169+ const selection = this . getItemSelected ( ) ;
170+ if ( selection ) {
171+ this . __retrieveURLAndDownloadFile ( selection ) ;
172+ }
173+ }
174+ } ,
175+
176+ __deleteSelected : function ( ) {
177+ if ( this . isMultiSelect ( ) ) {
178+ const requests = [ ] ;
179+ this . __multiSelection . forEach ( selection => {
180+ const request = this . __deleteFile ( selection ) ;
181+ if ( request ) {
182+ requests . push ( request ) ;
183+ }
184+ } ) ;
185+ Promise . all ( requests )
186+ . then ( datas => {
187+ if ( datas . length ) {
188+ this . fireDataEvent ( "fileDeleted" , datas [ 0 ] ) ;
189+ osparc . FlashMessenger . getInstance ( ) . logAs ( this . tr ( "Files successfully deleted" ) , "ERROR" ) ;
129190 }
130191 } ) ;
192+ requests
193+ } else {
194+ const selection = this . getItemSelected ( ) ;
195+ if ( selection ) {
196+ const request = this . __deleteFile ( selection ) ;
197+ if ( request ) {
198+ request
199+ . then ( data => {
200+ this . fireDataEvent ( "fileDeleted" , data ) ;
201+ osparc . FlashMessenger . getInstance ( ) . logAs ( this . tr ( "File successfully deleted" ) , "ERROR" ) ;
202+ } ) ;
203+ }
204+ }
131205 }
132206 } ,
133207
134- __deleteFile : function ( ) {
135- let selection = this . getItemSelected ( ) ;
136- if ( selection ) {
137- console . log ( "Delete " , selection ) ;
138- const fileId = selection . getFileId ( ) ;
139- const locationId = selection . getLocation ( ) ;
140- if ( locationId !== 0 && locationId !== "0" ) {
141- osparc . FlashMessenger . getInstance ( ) . logAs ( this . tr ( "Only files in simcore.s3 can be deleted" ) ) ;
142- return false ;
143- }
144- const dataStore = osparc . store . Data . getInstance ( ) ;
145- dataStore . addListenerOnce ( "deleteFile" , e => {
146- if ( e ) {
147- this . fireDataEvent ( "fileDeleted" , e . getData ( ) ) ;
208+ __retrieveURLAndDownloadFile : function ( file ) {
209+ const fileId = file . getFileId ( ) ;
210+ const locationId = file . getLocation ( ) ;
211+ osparc . utils . Utils . retrieveURLAndDownload ( locationId , fileId )
212+ . then ( data => {
213+ if ( data ) {
214+ osparc . DownloadLinkTracker . getInstance ( ) . downloadLinkUnattended ( data . link , data . fileName ) ;
148215 }
149- } , this ) ;
150- return dataStore . deleteFile ( locationId , fileId ) ;
216+ } ) ;
217+ } ,
218+
219+ __deleteFile : function ( file ) {
220+ const fileId = file . getFileId ( ) ;
221+ const locationId = file . getLocation ( ) ;
222+ if ( locationId !== 0 && locationId !== "0" ) {
223+ osparc . FlashMessenger . getInstance ( ) . logAs ( this . tr ( "Only files in simcore.s3 can be deleted" ) ) ;
224+ return null ;
151225 }
152- return false ;
153- }
226+ const dataStore = osparc . store . Data . getInstance ( ) ;
227+ return dataStore . deleteFile ( locationId , fileId ) ;
228+ } ,
154229 }
155230} ) ;
0 commit comments