Skip to content
Snippets Groups Projects
element.factory.ts 6.37 KiB
Newer Older
import { ComponentFactory, ComponentFactoryResolver } from '@angular/core';
rhenck's avatar
rhenck committed
import { UIElement } from '../models/uI-element';
import { TextElement } from '../ui-elements/text/text-element';
import { ButtonElement } from '../ui-elements/button/button-element';
import { TextFieldElement } from '../ui-elements/text-field/text-field-element';
import { TextAreaElement } from '../ui-elements/text-area/text-area-element';
import { CheckboxElement } from '../ui-elements/checkbox/checkbox-element';
import { DropdownElement } from '../ui-elements/dropdown/dropdown-element';
import { RadioButtonGroupElement } from '../ui-elements/radio/radio-button-group-element';
import { ImageElement } from '../ui-elements/image/image-element';
import { AudioElement } from '../ui-elements/audio/audio-element';
import { VideoElement } from '../ui-elements/video/video-element';
import { TextComponent } from '../ui-elements/text/text.component';
import { ButtonComponent } from '../ui-elements/button/button.component';
import { TextFieldComponent } from '../ui-elements/text-field/text-field.component';
import { TextAreaComponent } from '../ui-elements/text-area/text-area.component';
import { CheckboxComponent } from '../ui-elements/checkbox/checkbox.component';
import { DropdownComponent } from '../ui-elements/dropdown/dropdown.component';
import { RadioButtonGroupComponent } from '../ui-elements/radio/radio-button-group.component';
import { ImageComponent } from '../ui-elements/image/image.component';
import { AudioComponent } from '../ui-elements/audio/audio.component';
import { VideoComponent } from '../ui-elements/video/video.component';
import { LikertElement } from '../ui-elements/likert/likert-element';
import { LikertComponent } from '../ui-elements/likert/likert.component';
import { RadioGroupImagesComponent } from '../ui-elements/radio-with-images/radio-group-images.component';
import { RadioGroupImagesElement } from '../ui-elements/radio-with-images/radio-group-images';
import { DropListComponent } from '../ui-elements/drop-list/drop-list.component';
import { DropListElement } from '../ui-elements/drop-list/drop-list';
import { ClozeComponent } from '../ui-elements/cloze/cloze.component';
import { ClozeElement } from '../ui-elements/cloze/cloze-element';
mechtelm's avatar
mechtelm committed
import { SliderElement } from '../ui-elements/slider/slider-element';
import { SpellCorrectElement } from '../ui-elements/spell-correct/spell-correct-element';
import { SliderComponent } from '../ui-elements/slider/slider.component';
import { SpellCorrectComponent } from '../ui-elements/spell-correct/spell-correct.component';
rhenck's avatar
rhenck committed
import { FrameComponent } from '../ui-elements/frame/frame.component';
import { FrameElement } from '../ui-elements/frame/frame-element';
export function createElement(elementModel: Partial<UIElement>): UIElement {
  let newElement: UIElement;
  switch (elementModel.type) {
    case 'text':
rhenck's avatar
rhenck committed
      newElement = new TextElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new ButtonElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new TextFieldElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new TextAreaElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new CheckboxElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new DropdownElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new RadioButtonGroupElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new ImageElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new AudioElement(elementModel);
rhenck's avatar
rhenck committed
      newElement = new VideoElement(elementModel);
    case 'likert':
rhenck's avatar
rhenck committed
      newElement = new LikertElement(elementModel);
      break;
    case 'radio-group-images':
      newElement = new RadioGroupImagesElement(elementModel);
      break;
rhenck's avatar
rhenck committed
    case 'drop-list':
      newElement = new DropListElement(elementModel);
      break;
rhenck's avatar
rhenck committed
    case 'cloze':
      newElement = new ClozeElement(elementModel);
      break;
mechtelm's avatar
mechtelm committed
    case 'slider':
      newElement = new SliderElement(elementModel);
      break;
    case 'spell-correct':
      newElement = new SpellCorrectElement(elementModel);
      break;
rhenck's avatar
rhenck committed
    case 'frame':
      newElement = new FrameElement(elementModel);
      break;
    default:
      throw new Error(`ElementType ${elementModel.type} not found!`);
  }
  console.log('newElement', newElement);
  return newElement;
}

export function getComponentFactory(
  elementType: string, componentFactoryResolver: ComponentFactoryResolver
): ComponentFactory<any> {
  // TODO: Find better solution than any
  switch (elementType) {
    case 'text':
      return componentFactoryResolver.resolveComponentFactory(TextComponent);
    case 'button':
      return componentFactoryResolver.resolveComponentFactory(ButtonComponent);
    case 'text-field':
      return componentFactoryResolver.resolveComponentFactory(TextFieldComponent);
    case 'text-area':
      return componentFactoryResolver.resolveComponentFactory(TextAreaComponent);
    case 'checkbox':
      return componentFactoryResolver.resolveComponentFactory(CheckboxComponent);
    case 'dropdown':
      return componentFactoryResolver.resolveComponentFactory(DropdownComponent);
    case 'radio':
      return componentFactoryResolver.resolveComponentFactory(RadioButtonGroupComponent);
    case 'image':
      return componentFactoryResolver.resolveComponentFactory(ImageComponent);
    case 'audio':
      return componentFactoryResolver.resolveComponentFactory(AudioComponent);
    case 'video':
      return componentFactoryResolver.resolveComponentFactory(VideoComponent);
rhenck's avatar
rhenck committed
    case 'likert':
      return componentFactoryResolver.resolveComponentFactory(LikertComponent);
    case 'radio-group-images':
      return componentFactoryResolver.resolveComponentFactory(RadioGroupImagesComponent);
rhenck's avatar
rhenck committed
    case 'drop-list':
      return componentFactoryResolver.resolveComponentFactory(DropListComponent);
rhenck's avatar
rhenck committed
    case 'cloze':
      return componentFactoryResolver.resolveComponentFactory(ClozeComponent);
mechtelm's avatar
mechtelm committed
    case 'slider':
      return componentFactoryResolver.resolveComponentFactory(SliderComponent);
    case 'spell-correct':
      return componentFactoryResolver.resolveComponentFactory(SpellCorrectComponent);
rhenck's avatar
rhenck committed
    case 'frame':
      return componentFactoryResolver.resolveComponentFactory(FrameComponent);
    default:
      throw new Error('unknown element');
  }
}