import { Parser } from '@/parsers/Parser' ; import { ScalarParser } from '@/parsers/ScalarParser' ; import { Value } from '@/lib/Value' ; import { BooleanField , ParserOptions , UnknowParser , ScalarDescriptor } from '../../types' ; export class BooleanParser extends ScalarParser < boolean , BooleanField > { constructor ( options , parent ? ) { const schema = options . schema ; const kind = options . kind || ScalarParser . getKind ( schema , parent ) || 'boolean' ; const type = ScalarParser . getType ( kind ) || 'checkbox' ; super ( kind , type , options , parent ) ; } isEmpty ( data = this . model ) { return typeof data !== 'boolean' ; } parseField ( ) { super . parseField ( ) ; if ( this . field . attrs . type !== 'radio' ) { Object . defineProperty ( this . field . attrs , 'checked' , { enumerable : true , get : ( ) => ! ! this . model } ) ; } this . field . toggle = ( ) => { this . field . setValue ( ! this . model ) ; } ; } parseValue ( checked ) { return Value . boolean ( checked ) ; } } Parser . register ( 'boolean' , BooleanParser ) ;