import { CoreOptions } from '@chartwerk/core'; import { BarOptions, BarPodType, DiscreteConfig, NonDiscreteConfig, SizeType } from '../types'; import * as _ from 'lodash'; const DEFAULT_MIN_BAR_WIDTH = 4; //px const BAR_SERIE_DEFAULTS = { type: { [BarPodType.DISCRETE]: { enable: false, step: undefined, innerSize: undefined, groupSize: { width: { value: 50, type: (SizeType.PERCENT as SizeType.PERCENT) }, max: undefined, min: DEFAULT_MIN_BAR_WIDTH, } }, [BarPodType.NON_DISCRETE]: { enable: false, barWidth: { estimated: { value: undefined, type: (SizeType.UNIT as SizeType.UNIT) }, max: undefined, min: DEFAULT_MIN_BAR_WIDTH, } }, }, }; export class BarConfig extends CoreOptions { constructor(options: BarOptions) { super(options, BAR_SERIE_DEFAULTS); this.validateOptions(); } get barOptions(): any { return {}; } get dicreteConfig(): DiscreteConfig { if(!this._options.type[BarPodType.DISCRETE]?.enable) { throw new Error(`Can't get discrete config for non_dicreste type`); } return this._options.type[BarPodType.DISCRETE]; } get nonDicreteConfig(): NonDiscreteConfig { if(!this._options.type[BarPodType.NON_DISCRETE]?.enable) { throw new Error(`Can't get non_discrete config for dicreste type`); } return this._options.type[BarPodType.NON_DISCRETE]; } get barType(): BarPodType { if(this._options.type[BarPodType.DISCRETE]?.enable === true) { return BarPodType.DISCRETE; } if(this._options.type[BarPodType.NON_DISCRETE]?.enable === true) { return BarPodType.NON_DISCRETE; } throw new Error(`Unknown Ber Pod Type: ${this._options.type}`); } // event callbacks callbackContextMenu(data: any): void { if(_.has(this._options.eventsCallbacks, 'contextMenu')) { this._options.eventsCallbacks.contextMenu(data); } } get contextMenu(): (evt: any) => void { return this._options.eventsCallbacks.contextMenu; } validateOptions(): void { if( this._options.type[BarPodType.DISCRETE]?.enable === true && this._options.type[BarPodType.NON_DISCRETE]?.enable === true ) { throw new Error(`Bar Options is not valid: only one BarPodType should be enabled`); } if( this._options.type[BarPodType.DISCRETE]?.enable === false && this._options.type[BarPodType.NON_DISCRETE]?.enable === false ) { // @ts-ignore this._options.type[BarPodType.DISCRETE]?.enable = true; console.warn(`Bar Options: no type has been provided -> BarPodType.DISCRETE type will be used af default`); } } validateDiscreteOptions(): void { const groupType = this._options.type[BarPodType.DISCRETE].groupSize?.width?.type; if( groupType !== undefined && groupType !== SizeType.PERCENT && groupType !== SizeType.PX ) { throw new Error(`Bar Options is not valid: groupSize.width.type should be "px" on "percent": ${groupType}`); } } }