import { Backend } from '../base-backend' ; import { Record } from '../datatypes/record' ; import { SaveQuery , ChildQueryDescriber } from '../query/query' ; import { Lookup } from '../query/lookups' ; import { BaseSorter } from './sorters/base' ; export class BaseFieldEvent { } export class FromDBConvertingError extends Error { constructor ( fieldType , value ) { super ( `Can't convert '${ value }' to JS internal type. Field '${ fieldType }'.` ) ; } } export class NotImplementedError extends Error { constructor ( fieldType , functionName ) { super ( `Function '${ functionName }' not implement for field '${ fieldType }'.` ) ; } } export interface BaseFieldParams { name : string ; emptyValue ? : any ; verboseName ? : string ; helpText ? : string ; sorter ? : BaseSorter ; } export class BaseField implements BaseFieldParams { internalName : string ; name : string ; emptyValue : any ; verboseName : string ; helpText : string ; sorter : BaseSorter ; backend : Backend ; constructor ( backend , params ) { for ( let key in params ) { this [ key ] = params [ key ] ; } if ( ! this . verboseName ) this . verboseName = this . name . charAt ( 0 ) . toUpperCase ( ) + this . name . slice ( 1 ) ; this . backend = backend ; } fromRAWtoInternal ( value , record ? ) { return value ; } fromInternalToRAW ( value ) { return value ; } constructDefaultValue ( record ) { return undefined ; } checkValue ( value ) { return true ; } compareValues ( value , value2 ) { return value === value2 ; } cloneValue ( asNew , value ) { return value ; } getEmptyValue ( ) { return this . emptyValue ; } prepareValue ( record , oldValue , newValue ) { } destroyValue ( record ) { } } export interface BaseDbFieldParams extends BaseFieldParams { maxLength ? : number ; null ? : boolean ; blank ? : boolean ; choices ? : [ any , string ] [ ] ; default ? : any ; } export abstract class BaseDbField extends BaseField implements BaseDbFieldParams { maxLength : number ; null : boolean ; blank : boolean ; choices : [ string | number , string ] [ ] ; default : any ; lookups : typeof Lookup [ ] = [ ] ; constructor ( backend , params ) { super ( backend , params ) ; } constructDefaultValue ( record ) { if ( typeof this . default == "function" ) return this . default ( ) ; else if ( this . default !== undefined ) return this . default ; else if ( this . null ) return null ; throw new Error ( `Can't get default value for field ${ this . name }` ) ; } getLookup ( name ) { for ( let lookupClass of this . lookups ) { if ( lookupClass . prototype . lookupName == name ) return lookupClass ; } return null ; } getEmptyValue ( ) { var val = super . getEmptyValue ( ) ; if ( val !== undefined ) return val ; else if ( this . null ) return null ; else throw new Error ( "Error while attempting to get empty value for field '" + this . verboseName + "'." ) ; } } export interface BaseVirtualFieldParams extends BaseFieldParams { } export class BaseVirtualField extends BaseField implements BaseVirtualField { backend : Backend ; constructor ( backend , params ) { super ( backend , params ) ; } dataChanged ( fields = null , record , emitChange ) { } getSaveQueries ( value ) { return [ ] ; } }