form.component.ts 3.42 KB
Newer Older
1
import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef} from '@angular/core';
李维's avatar
李维 committed
2
import { defaultData } from '../../assets/default/formData/defaultData.js'
liujiangnan's avatar
liujiangnan committed
3 4 5 6

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
7
  styleUrls: ['./form.component.scss']
liujiangnan's avatar
liujiangnan committed
8 9
})

10
export class FormComponent implements OnInit, OnChanges, OnDestroy {
limingzhe's avatar
limingzhe committed
11

12 13 14 15
  _item: any;
  dataArray: Array<Object> = []; 
  hotZoneItemArr: Array<Object> = [];
  bgItem: Object;
李维's avatar
李维 committed
16
  bgImages: Object = {leftTop:"", middleTop:"", rightTop:"", leftMiddle:"", rightMiddle:"", leftBottom:"", middleBottom:"", rightBottom:""};
17
  KEY = 'DataKey_YM4-11';
limingzhe's avatar
limingzhe committed
18

19 20 21 22 23
  set item(item) {
    this._item = item;
  }
  get item() {
    return this._item;
limingzhe's avatar
limingzhe committed
24 25
  }

26 27 28
  @Output()
  update = new EventEmitter();
  constructor(private appRef: ApplicationRef) {
limingzhe's avatar
limingzhe committed
29

30
  }
liujiangnan's avatar
liujiangnan committed
31

32
  ngOnInit() {
limingzhe's avatar
limingzhe committed
33
    this.item = {};
34 35 36 37
    this.item.contentObj = {};
    const getData = (<any> window).courseware.getData;
    getData((data) => {
      // console.log("读取数据", data)
limingzhe's avatar
limingzhe committed
38 39
      if (data) {
        this.item = data;
40 41 42 43
      } else {
        this.item = {};
      }
      if ( !this.item.contentObj ) {
李维's avatar
李维 committed
44
        this.item.contentObj = {};
limingzhe's avatar
limingzhe committed
45 46 47
      }
      this.init();
      this.refresh();
48 49 50
    }, this.KEY);
  }
  ngOnChanges() {
limingzhe's avatar
limingzhe committed
51

52
  }
limingzhe's avatar
limingzhe committed
53

54
  ngOnDestroy() {
limingzhe's avatar
limingzhe committed
55 56
  }

57 58
  saveData(e){
    this.bgItem = e.bgItem;
李维's avatar
李维 committed
59
    // this.bgImages = e.bgImages
60 61 62 63 64 65
    this.hotZoneItemArr.length = 0;
    e.hotZoneItemArr.forEach(item => {
      this.hotZoneItemArr.push(item)
    });
    this.save();
  }
limingzhe's avatar
limingzhe committed
66

67
  init() {
李维's avatar
李维 committed
68 69 70 71 72 73
    if(typeof(this.item.contentObj.hotZoneItemArr) == undefined || typeof(this.item.contentObj.bgItem) == undefined || typeof(this.item.contentObj.bgImages) == undefined || this.item.contentObj.version != defaultData.version) {
      console.log("Init defalue data")
      this.item.contentObj = defaultData
      this.hotZoneItemArr = defaultData.hotZoneItemArr
      this.bgItem = defaultData.bgItem
      this.bgImages = defaultData.bgImages
74
    } else {
李维's avatar
李维 committed
75
      console.log("Data loaded")
76 77
      this.hotZoneItemArr = this.item.contentObj.hotZoneItemArr;
      this.bgItem = this.item.contentObj.bgItem;
李维's avatar
李维 committed
78 79
      this.bgImages = this.item.contentObj.bgImages;
    }
李维's avatar
李维 committed
80
  
limingzhe's avatar
limingzhe committed
81 82
  }

83 84 85 86 87 88
  cardItemData(){
    return {
      audio_url: "",
      image_url: "",
      hotZoneIndex: -1
    };
limingzhe's avatar
limingzhe committed
89 90
  }

91 92 93
  cardChoiceData(){
    return { isText: true, text: "", image_url: "" }
  }
limingzhe's avatar
limingzhe committed
94

95 96 97 98
  getDefaultPicArr() {
    let arr = []; 
    return arr;
  }
limingzhe's avatar
limingzhe committed
99

100
  initData() {
limingzhe's avatar
limingzhe committed
101

liujiangnan's avatar
liujiangnan committed
102 103
  }

limingzhe's avatar
limingzhe committed
104

105 106 107 108
  addChoice(questionIndex) { 
    // let item = this.cardChoiceData();  
    // this.dataArray[questionIndex].choice.incorrect.push(item);
    // this.saveItem();
liujiangnan's avatar
liujiangnan committed
109 110
  }

111 112 113 114 115 116 117 118 119
  onImageUploadSuccessByItem(e, item) {
    item.image_url = e.url
    this.save(); 
  }
 
  onAudioUploadSuccessByItem(e, item) { 
    item.audio_url = e.url; 
    this.save();
  }
limingzhe's avatar
limingzhe committed
120

121 122
  onTitleAudioUploadSuccess(e) { 
    this.item.contentObj.titleAudio_url = e.url;
limingzhe's avatar
limingzhe committed
123 124 125
    this.save();
  }

126 127 128 129 130
  addItem() { 
    let item = this.cardItemData();  
    this.dataArray.push(item);
    this.saveItem();
  }
limingzhe's avatar
limingzhe committed
131

132 133 134 135 136 137 138 139 140 141 142 143
  radioClick(it, radioValue) {
    it.radioValue = radioValue;
    this.saveItem();
  }

  clickCheckBox() {
    this.saveItem();
  }

  saveItem() {
    this.save();
  }
limingzhe's avatar
limingzhe committed
144

李维's avatar
李维 committed
145 146 147 148
  backgroundImageChanged(e) {
    this.saveItem()
  }

limingzhe's avatar
limingzhe committed
149
  save() {
150
    (<any> window).courseware.setData(this.item, null, this.KEY);
limingzhe's avatar
limingzhe committed
151
    this.refresh();
152
    // console.log("保存", this.item)
limingzhe's avatar
limingzhe committed
153 154 155 156 157 158
  }

  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
liujiangnan's avatar
liujiangnan committed
159 160
  }
}
limingzhe's avatar
limingzhe committed
161