import { Engine , ValueChangeHandler } from '@/Engine' ; import { ConfigAttrs } from '@/models/Config' ; import { Module } from '@/modules/Module' ; import { ModuleBar } from '@/modules/ModuleBar' ; import { ModuleGrid } from '@/modules/ModuleGrid' ; import { ModuleLabel } from '@/modules/ModuleLabel' ; import { ModuleLimit } from '@/modules/ModuleLimit' ; import { ModuleRail } from '@/modules/ModuleRail' ; import { ModuleSlider } from '@/modules/ModuleSlider' ; import { assert , isArray , isInstanceOf , isNumber , isPlainObject } from '@/validation/assert' ; export interface JsrConstructor { /** Configuration */ config : ConfigAttrs ; /** List of modules to use - without them, JSR shows nothing in HTML, but is still usable via API */ modules : Module [ ] ; } export type ChangeLimitCommand = { min ? : number ; max ? : number ; } ; export class JSR { private engine : Engine ; public constructor ( ctor ) { assert ( 'JSR.modules' , ctor . modules , isArray ( isInstanceOf ( Module ) ) ) ; this . engine = new Engine ( { config : ctor . config , modules : ctor . modules , } ) ; } param index param value public setRealValue ( index , value ) { this . engine . inputHandler . setRealValue ( index , value ) ; } param index param value public setRatioValue ( index , value ) { this . engine . inputHandler . setRatioValue ( index , value ) ; } param index returns public getRealValue ( index ) { return this . engine . stateProcessor . getState ( ) . values [ index ] . asReal ( ) ; } param index returns public getRatioValue ( index ) { return this . engine . stateProcessor . getState ( ) . values [ index ] . asRatio ( ) ; } param handler returns public onValueChange ( handler ) { return this . engine . addValueChangeHandler ( handler ) ; } param command public changeLimit ( command ) { assert ( 'limit object' , command , isPlainObject ) ; if ( command . min ) { assert ( 'limit.min' , command . min , isNumber ) ; } if ( command . max ) { assert ( 'limit.max' , command . max , isNumber ) ; } this . engine . changeLimit ( { min : command . min !== undefined ? this . engine . produceRealValue ( command . min ) : undefined , max : command . max !== undefined ? this . engine . produceRealValue ( command . max ) : undefined , } ) ; } /**
     * Enable JSR by allowing value changes, and removing `.is-disabled` class from the container.
     */ public enable ( ) { this . engine . enable ( ) ; } /**
     * Disable JSR by disallowing value changes, and adding `.is-disabled` class to the container.
     */ public disable ( ) { this . engine . disable ( ) ; } /**
     * Return whether JSR is enabled or disabled.
     */ public isEnabled ( ) { return this . engine . isEnabled ( ) ; } NOTE public destroy ( ) { this . engine . modules . forEach ( m => m . destroy ( ) ) ; } /** Base module used for creating user's own modules */ public static Module = Module ; /** Bar module */ public static Bar = ModuleBar ; /** Grid module */ public static Grid = ModuleGrid ; /** Label module */ public static Label = ModuleLabel ; /** Limit module */ public static Limit = ModuleLimit ; /** Rail module */ public static Rail = ModuleRail ; /** Slider module */ public static Slider = ModuleSlider ; }