2017-07-26 23:07:27 +02:00
import { BaseCommand } from './base-command.js' ;
import { app } from './app.js' ;
import { _ } from 'lib/locale.js' ;
import { BaseModel } from 'lib/base-model.js' ;
import { Folder } from 'lib/models/folder.js' ;
import { Note } from 'lib/models/note.js' ;
import { time } from 'lib/time-utils.js' ;
import { autocompleteItems } from './autocomplete.js' ;
class Command extends BaseCommand {
usage ( ) {
2017-07-28 00:59:34 +02:00
return _ ( 'todo <action> <pattern>' ) ;
2017-07-26 23:07:27 +02:00
}
description ( ) {
2017-07-26 23:27:03 +02:00
return _ ( '<action> can either be "toggle" or "clear". Use "toggle" to toggle the given todo between completed and uncompleted state (If the target is a regular note it will be converted to a todo). Use "clear" to convert the todo back to a regular note.' ) ;
2017-07-26 23:07:27 +02:00
}
autocomplete ( ) {
return { data : autocompleteItems } ;
}
async action ( args ) {
const action = args . action ;
const pattern = args . pattern ;
const notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , pattern ) ;
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , pattern ) ) ;
for ( let i = 0 ; i < notes . length ; i ++ ) {
const note = notes [ i ] ;
let toSave = {
id : note . id ,
} ;
if ( action == 'toggle' ) {
toSave . todo _completed = note . todo _completed ? 0 : time . unixMs ( ) ;
if ( ! note . is _todo ) toSave . is _todo = 1 ;
} else if ( action == 'clear' ) {
toSave . is _todo = 0 ;
}
await Note . save ( toSave ) ;
}
}
}
module . exports = Command ;