2017-11-03 02:09:34 +02:00
const { BaseCommand } = require ( './base-command.js' ) ;
const { app } = require ( './app.js' ) ;
const { _ } = require ( 'lib/locale.js' ) ;
2017-12-14 20:12:14 +02:00
const Tag = require ( 'lib/models/Tag.js' ) ;
const BaseModel = require ( 'lib/BaseModel.js' ) ;
2018-08-13 23:28:19 +02:00
const { time } = require ( 'lib/time-utils.js' ) ;
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage ( ) {
2017-08-04 18:02:43 +02:00
return 'tag <tag-command> [tag] [note]' ;
2017-07-10 22:03:46 +02:00
}
description ( ) {
2018-08-13 23:28:19 +02:00
return _ ( '<tag-command> can be "add", "remove" or "list" to assign or remove [tag] from [note], or to list the notes associated with [tag]. The command `tag list` can be used to list all the tags (use -l for long option).' ) ;
2017-07-10 22:03:46 +02:00
}
2018-08-13 23:28:19 +02:00
2018-09-03 22:18:15 +02:00
options ( ) {
return [
[ '-l, --long' , _ ( 'Use long list format. Format is ID, NOTE_COUNT (for notebook), DATE, TODO_CHECKED (for to-dos), TITLE' ) ] ,
] ;
}
2018-08-13 23:28:19 +02:00
2017-07-10 22:03:46 +02:00
async action ( args ) {
let tag = null ;
2018-08-13 23:28:19 +02:00
let options = args . options ;
2017-07-10 22:03:46 +02:00
if ( args . tag ) tag = await app ( ) . loadItem ( BaseModel . TYPE _TAG , args . tag ) ;
2017-07-11 20:17:23 +02:00
let notes = [ ] ;
if ( args . note ) {
notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , args . note ) ;
}
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
const command = args [ 'tag-command' ] ;
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
if ( command == 'remove' && ! tag ) throw new Error ( _ ( 'Cannot find "%s".' , args . tag ) ) ;
if ( command == 'add' ) {
2017-07-18 20:21:03 +02:00
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , args . note ) ) ;
2017-11-12 01:13:14 +02:00
if ( ! tag ) tag = await Tag . save ( { title : args . tag } , { userSideValidation : true } ) ;
2017-07-11 20:17:23 +02:00
for ( let i = 0 ; i < notes . length ; i ++ ) {
await Tag . addNote ( tag . id , notes [ i ] . id ) ;
}
2017-08-04 18:02:43 +02:00
} else if ( command == 'remove' ) {
2017-07-18 20:21:03 +02:00
if ( ! tag ) throw new Error ( _ ( 'Cannot find "%s".' , args . tag ) ) ;
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , args . note ) ) ;
2017-07-11 20:17:23 +02:00
for ( let i = 0 ; i < notes . length ; i ++ ) {
await Tag . removeNote ( tag . id , notes [ i ] . id ) ;
}
2017-08-04 18:02:43 +02:00
} else if ( command == 'list' ) {
2017-07-10 22:03:46 +02:00
if ( tag ) {
let notes = await Tag . notes ( tag . id ) ;
2018-09-03 22:18:15 +02:00
notes . map ( ( note ) => {
let line = '' ;
2018-09-05 12:46:49 +02:00
if ( options . long ) {
line += BaseModel . shortId ( note . id ) ;
line += ' ' ;
line += time . formatMsToLocal ( note . user _updated _time ) ;
line += ' ' ;
}
if ( note . is _todo ) {
line += '[' ;
if ( note . todo _completed ) {
line += 'X' ;
} else {
line += ' ' ;
}
line += '] ' ;
} else {
line += ' ' ;
}
line += note . title ;
this . stdout ( line ) ;
2018-09-03 22:18:15 +02:00
} ) ;
2017-07-10 22:03:46 +02:00
} else {
let tags = await Tag . all ( ) ;
2017-10-07 18:30:27 +02:00
tags . map ( ( tag ) => { this . stdout ( tag . title ) ; } ) ;
2017-07-10 22:03:46 +02:00
}
} else {
2017-08-04 18:02:43 +02:00
throw new Error ( _ ( 'Invalid command: "%s"' , command ) ) ;
2017-07-10 22:03:46 +02:00
}
}
}
module . exports = Command ;