@@ -64,6 +64,7 @@ export class FSCore {
6464
6565 updating = false ;
6666 renaming = '' ;
67+ isRenamedItemNew = false ;
6768
6869 setLabelProvider ( labelProvider : FSLabelProviderContribution ) {
6970 this . _labelProvider = labelProvider ;
@@ -995,15 +996,78 @@ export class FSCore {
995996 throw createError ( FSError . unknown ) ;
996997 }
997998 }
998-
999+
1000+ public async acceptName ( node : FSNode , name : string ) : Promise < boolean > {
1001+ return this . isRenamedItemNew
1002+ ? this . tryCreate ( node , name )
1003+ : this . tryRename ( node , name ) ;
1004+ }
1005+
1006+ public async cancelName ( node : FSNode ) : Promise < void > {
1007+ this . setRename ( ) ;
1008+ }
1009+
1010+ public validateName ( node : FSItemNode , newName : string , failsOnSameName = false ) : string {
1011+ newName = newName . trim ( ) ;
1012+ if ( ! node ) {
1013+ return 'No node to rename' ;
1014+ }
1015+ if ( newName === '' ) {
1016+ return 'Empty name' ;
1017+ }
1018+ // TODO: valid name
1019+ const collection = node . parent as FSCollectionNode ;
1020+ if ( newName === node . nodeName ) {
1021+ if ( failsOnSameName ) {
1022+ return 'Same name' ;
1023+ } else {
1024+ return '' ;
1025+ }
1026+ }
1027+ if ( this . fileExists ( newName , collection ) ) {
1028+ return 'Item already exists' ;
1029+ }
1030+ return '' ;
1031+ }
1032+
1033+ public async tryCreate ( node : FSNode , name : string ) : Promise < boolean > {
1034+ if ( FSNode . isItem ( node ) ) {
1035+ try {
1036+ if ( FSNode . isDocument ( node ) ) {
1037+ // const result = await this.rename(node, name);
1038+ // this.setRename();
1039+ // return result;
1040+ } else if ( FSNode . isCollection ( node ) ) {
1041+ const parent = ( node . parent as FSCollectionNode ) ;
1042+ const uri = parent . uri + TRAILING_SYMBOL + name ;
1043+ const collection = await FSApi . newCollection ( node . connectionNode . connection , uri ) ;
1044+ node . nodeName = name ;
1045+ node . collection = collection ;
1046+ // (node as any)['id'] = this.itemID(parent.connectionNode.connection, collection),
1047+ node . link = FS_RESOURCE_SCHEME + ':' + collection . name ,
1048+ node . uri = uri ;
1049+ this . isRenamedItemNew = false ;
1050+ this . setRename ( ) ;
1051+ return true ;
1052+ }
1053+ } catch ( e ) {
1054+ this . setRename ( ) ;
1055+ }
1056+ return false ;
1057+ } else {
1058+ this . setRename ( ) ;
1059+ throw createError ( FSError . unknown ) ;
1060+ }
1061+ }
1062+
9991063 public async tryRename ( node : FSNode , name : string ) : Promise < boolean > {
10001064 if ( node . nodeName === name ) {
1001- this . setRename ( node , false ) ;
1065+ this . setRename ( ) ;
10021066 return false ;
10031067 }
10041068 if ( FSNode . isItem ( node ) ) {
10051069 const result = await this . rename ( node , name ) ;
1006- this . setRename ( node , false ) ;
1070+ this . setRename ( ) ;
10071071 return result ;
10081072 } else {
10091073 throw createError ( FSError . unknown ) ;
@@ -1275,26 +1339,45 @@ export class FSCore {
12751339 }
12761340 const collection = this . node as FSCollectionNode ;
12771341 const validator = ( input : string ) => input !== '' && ! this . fileExists ( input ) ;
1278- const dialog = new SingleTextInputDialog ( {
1279- initialValue : this . newName ( validator ) ,
1280- title : 'New ' + ( isCollection ? 'collection' : 'document' ) ,
1281- confirmButtonLabel : 'Create' ,
1282- validate : validator ,
1283- } ) ;
1284- let name = await dialog . open ( ) ;
1285- if ( name ) {
1286- this . nextName ( name ) ;
1287- name = collection . uri + '/' + name ;
1288- if ( isCollection ) {
1289- const result = await FSApi . newCollection ( collection . connectionNode . connection , name ) ;
1290- if ( result ) {
1291- this . addCollection ( collection , result ) ;
1292- }
1293- } else {
1294- this . createDocument ( collection , name ) ;
1295- }
1342+ // const dialog = new SingleTextInputDialog({
1343+ // initialValue: this.newName(validator),
1344+ // title: 'New ' + (isCollection ? 'collection' : 'document'),
1345+ // confirmButtonLabel: 'Create',
1346+ // validate: validator,
1347+ // });
1348+ // let name = await dialog.open();
1349+ // if (name) {
1350+ // this.nextName(name);
1351+ // name = collection.uri + '/' + name;
1352+ // if (isCollection) {
1353+ // const result = await FSApi.newCollection(collection.connectionNode.connection, name);
1354+ // if (result) {
1355+ // this.addCollection(collection, result);
1356+ // }
1357+ // } else {
1358+ // this.createDocument(collection, name);
1359+ // }
1360+ // }
1361+ // return false;
1362+ const initialName = this . newName ( validator ) ;
1363+ this . nextName ( initialName ) ;
1364+ this . isRenamedItemNew = true ;
1365+ let item : FSItemNode ;
1366+ if ( isCollection ) {
1367+ item = await this . addCollection ( collection , {
1368+ name : collection . uri + TRAILING_SYMBOL + initialName ,
1369+ owner : 'admin' ,
1370+ group : 'dba' ,
1371+ acl : [ ] ,
1372+ documents : [ ] ,
1373+ created : new Date ( ) ,
1374+ collections : [ ]
1375+ } ) ;
1376+ } else {
1377+ item = await this . createDocument ( collection , name ) ;
12961378 }
1297- return false ;
1379+ this . setRename ( item ) ;
1380+ return true ;
12981381 }
12991382
13001383 public async uploadItem ( ) : Promise < boolean > {
@@ -1375,38 +1458,28 @@ export class FSCore {
13751458 return node ? node . id === this . renaming : this . renaming !== '' ;
13761459 }
13771460
1378- public async setRename ( node : FSNode , value = true ) {
1379- this . renaming = value ? node . id : '' ;
1380- await this . refresh ( ) ;
1381- if ( ! value ) {
1382- console . dir ( this . widgetManager ) ;
1461+ public async setRename ( node ?: FSNode ) {
1462+ this . updating = true ;
1463+ const renamingNode = this . getNode ( this . renaming ) ;
1464+ this . renaming = node ? node . id : '' ;
1465+ if ( ! node ) {
1466+ if ( this . isRenamedItemNew && renamingNode ) {
1467+ this . removeNode ( renamingNode ) ;
1468+ }
13831469 const serversWidget = await this . widgetManager . getWidget ( FS_CONNECTIONS_WIDGET_FACTORY_ID ) ;
13841470 if ( serversWidget ) {
13851471 serversWidget . activate ( ) ;
13861472 }
13871473 }
1388- }
1389-
1390- public validateName ( node : FSItemNode , input : string ) : string {
1391- input = input . trim ( ) ;
1392- if ( ! node ) {
1393- return 'No node to rename' ;
1394- }
1395- if ( input === '' ) {
1396- return 'Empty name' ;
1397- }
1398- // TODO: valid name
1399- const collection = node . parent as FSCollectionNode ;
1400- if ( this . fileExists ( input , collection ) ) {
1401- return 'Item already exists' ;
1402- }
1403- return '' ;
1474+ this . updating = false ;
1475+ await this . refresh ( ) ;
14041476 }
14051477
14061478 public async renameItem ( ) : Promise < void > {
14071479 if ( FSNode . isItem ( this . node ) ) {
14081480 // const collection = this.node.parent as FSCollectionNode;
14091481 // const validator = (input: string) => input === (this.node && this.node.nodeName) || input !== '' && !this.fileExists(input, collection);
1482+ this . isRenamedItemNew = false ;
14101483 this . setRename ( this . node ) ;
14111484 }
14121485 }
0 commit comments