Skip to content
Snippets Groups Projects
checkbox.ts 2.48 KiB
Newer Older
import { Type } from '@angular/core';
  InputElement, InputElementProperties, UIElementType
} from 'common/models/elements/element';
import { ElementComponent } from 'common/directives/element-component.directive';
import { CheckboxComponent } from 'common/components/input-elements/checkbox.component';
import { AnswerScheme, AnswerSchemeValue } from 'common/models/elements/answer-scheme-interfaces';
  BasicStyles, PropertyGroupGenerators, PropertyGroupValidators
} from 'common/models/elements/property-group-interfaces';
rhenck's avatar
rhenck committed
import { environment } from 'common/environment';
import { InstantiationEror } from 'common/util/errors';
export class CheckboxElement extends InputElement implements CheckboxProperties {
  type: UIElementType = 'checkbox';
  crossOutChecked: boolean = false;
  styling: BasicStyles;

rhenck's avatar
rhenck committed
  constructor(element?: CheckboxProperties) {
rhenck's avatar
rhenck committed
    if (element && isValid(element)) {
      this.crossOutChecked = element.crossOutChecked;
      this.styling = { ...element.styling };
rhenck's avatar
rhenck committed
    } else {
      if (environment.strictInstantiation) {
        throw new InstantiationEror('Error at Checkbox instantiation', element);
      }
      if (element?.crossOutChecked !== undefined) this.crossOutChecked = element.crossOutChecked;
rhenck's avatar
rhenck committed
      this.dimensions = PropertyGroupGenerators.generateDimensionProps({
        width: 215,
        ...element?.dimensions
      });
      this.styling = PropertyGroupGenerators.generateBasicStyleProps(element?.styling);
    }
  getDuplicate(): CheckboxElement {
    return new CheckboxElement(this);
  }

  hasAnswerScheme(): boolean {
    return Boolean(this.getAnswerScheme);
  getAnswerScheme(): AnswerScheme {
    return {
      id: this.id,
      type: 'boolean',
      format: '',
      multiple: false,
      nullable: false,
      values: this.getAnswerSchemeValues(),
  private getAnswerSchemeValues(): AnswerSchemeValue[] {
    return [
      { value: 'true', label: `Angekreuzt: ${this.label}` },
      { value: 'false', label: `Nicht Angekreuzt: ${this.label}` }
    ];
  }

  getElementComponent(): Type<ElementComponent> {
    return CheckboxComponent;
  }
}

export interface CheckboxProperties extends InputElementProperties {
rhenck's avatar
rhenck committed

function isValid(blueprint?: CheckboxProperties): boolean {
  if (!blueprint) return false;
  return blueprint.crossOutChecked !== undefined &&
rhenck's avatar
rhenck committed
    PropertyGroupValidators.isValidBasicStyles(blueprint.styling);
}