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

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

李维's avatar
李维 committed
9
export class FormComponent implements OnInit, OnChanges, OnDestroy {
limingzhe's avatar
limingzhe committed
10

李维's avatar
李维 committed
11 12 13 14
  _item: any;
  dataArray: Array<Object> = []; 
  hotZoneItemArr: Array<Object> = [];
  bgItem: Object;
limingzhe's avatar
limingzhe committed
15

李维's avatar
李维 committed
16
  KEY = 'DataKey_PU03';
limingzhe's avatar
limingzhe committed
17

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

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

李维's avatar
李维 committed
29
  }
liujiangnan's avatar
liujiangnan committed
30

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

李维's avatar
李维 committed
51
  }
limingzhe's avatar
limingzhe committed
52

李维's avatar
李维 committed
53
  ngOnDestroy() {
limingzhe's avatar
limingzhe committed
54 55
  }

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

李维's avatar
李维 committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  init() {
    if (this.item.contentObj.dataArray) {
      this.dataArray = this.item.contentObj.dataArray;
    } else {
      this.dataArray = this.getDefaultPicArr();
      this.item.contentObj.dataArray = this.dataArray;
    }

    if (this.item.contentObj.hotZoneItemArr) {
      this.hotZoneItemArr = this.item.contentObj.hotZoneItemArr;
    } else {
      this.hotZoneItemArr = this.getDefaultPicArr();
      this.item.contentObj.hotZoneItemArr = this.hotZoneItemArr;
    }

    if (this.item.contentObj.bgItem) {
      this.bgItem = this.item.contentObj.bgItem;
    } else {
      this.bgItem = {}
      this.item.contentObj.bgItem = this.bgItem;
    }
limingzhe's avatar
limingzhe committed
86 87
  }

李维's avatar
李维 committed
88 89 90 91 92 93
  cardItemData(){
    return {
      audio_url: "",
      image_url: "",
      hotZoneIndex: -1
    };
limingzhe's avatar
limingzhe committed
94 95
  }

李维's avatar
李维 committed
96 97 98
  cardChoiceData(){
    return { isText: true, text: "", image_url: "" }
  }
limingzhe's avatar
limingzhe committed
99

李维's avatar
李维 committed
100 101 102 103
  getDefaultPicArr() {
    let arr = []; 
    return arr;
  }
limingzhe's avatar
limingzhe committed
104

李维's avatar
李维 committed
105
  initData() {
limingzhe's avatar
limingzhe committed
106

liujiangnan's avatar
liujiangnan committed
107 108
  }

limingzhe's avatar
limingzhe committed
109

李维's avatar
李维 committed
110 111 112 113
  addChoice(questionIndex) { 
    // let item = this.cardChoiceData();  
    // this.dataArray[questionIndex].choice.incorrect.push(item);
    // this.saveItem();
liujiangnan's avatar
liujiangnan committed
114 115
  }

李维's avatar
李维 committed
116 117 118 119 120 121 122 123 124
  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
125

李维's avatar
李维 committed
126 127
  onTitleAudioUploadSuccess(e) { 
    this.item.contentObj.titleAudio_url = e.url;
limingzhe's avatar
limingzhe committed
128 129 130
    this.save();
  }

李维's avatar
李维 committed
131 132 133 134 135
  addItem() { 
    let item = this.cardItemData();  
    this.dataArray.push(item);
    this.saveItem();
  }
limingzhe's avatar
limingzhe committed
136

李维's avatar
李维 committed
137 138 139 140 141 142 143 144 145 146 147 148
  radioClick(it, radioValue) {
    it.radioValue = radioValue;
    this.saveItem();
  }

  clickCheckBox() {
    this.saveItem();
  }

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

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

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