2017-07-10 22:03:46 +02:00
import { BaseCommand } from './base-command.js' ;
import { app } from './app.js' ;
import { _ } from 'lib/locale.js' ;
2017-07-11 20:17:23 +02:00
import { BaseModel } from 'lib/base-model.js' ;
2017-07-10 22:03:46 +02:00
import { Folder } from 'lib/models/folder.js' ;
import { Note } from 'lib/models/note.js' ;
import { autocompleteItems } from './autocomplete.js' ;
class Command extends BaseCommand {
usage ( ) {
2017-07-18 20:49:47 +02:00
return 'mv <pattern> <destination>' ;
2017-07-10 22:03:46 +02:00
}
description ( ) {
2017-07-18 20:21:03 +02:00
return _ ( 'Moves the notes matching <pattern> to <destination>. If <pattern> is a note, it will be moved to the notebook <destination>. If <pattern> is a notebook, it will be renamed to <destination>.' ) ;
2017-07-10 22:03:46 +02:00
}
autocomplete ( ) {
return { data : autocompleteItems } ;
}
async action ( args ) {
2017-07-11 20:17:23 +02:00
const pattern = args [ 'pattern' ] ;
2017-07-17 21:19:01 +02:00
const destination = args [ 'destination' ] ;
2017-07-10 22:03:46 +02:00
2017-07-17 21:19:01 +02:00
const item = await app ( ) . guessTypeAndLoadItem ( pattern ) ;
2017-07-10 22:03:46 +02:00
2017-07-18 20:21:03 +02:00
if ( ! item ) throw new Error ( _ ( 'Cannot find "%s".' , pattern ) ) ;
2017-07-10 22:03:46 +02:00
2017-07-17 21:19:01 +02:00
if ( item . type _ == BaseModel . TYPE _FOLDER ) {
await Folder . save ( { id : item . id , title : destination } , { userSideValidation : true } ) ;
await app ( ) . refreshCurrentFolder ( ) ;
} else { // TYPE_NOTE
const folder = await Folder . loadByField ( 'title' , destination ) ;
2017-07-18 20:21:03 +02:00
if ( ! folder ) throw new Error ( _ ( 'Cannot find "%s".' , destination ) ) ;
2017-07-17 21:19:01 +02:00
const notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , pattern ) ;
2017-07-18 20:21:03 +02:00
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , pattern ) ) ;
2017-07-17 21:19:01 +02:00
for ( let i = 0 ; i < notes . length ; i ++ ) {
await Note . moveToFolder ( notes [ i ] . id , folder . id ) ;
}
2017-07-10 22:03:46 +02:00
}
}
}
module . exports = Command ;