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

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

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

李维's avatar
李维 committed
12 13
  _item: any;
  dataArray: Array<Object> = []; 
李维's avatar
李维 committed
14 15 16
  contentObj = {
    dataArray: []
  }
limingzhe's avatar
limingzhe committed
17

李维's avatar
李维 committed
18
  KEY = 'DataKey_East_L226';
limingzhe's avatar
limingzhe committed
19

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

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

李维's avatar
李维 committed
31
  }
liujiangnan's avatar
liujiangnan committed
32

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

李维's avatar
李维 committed
53
  }
limingzhe's avatar
limingzhe committed
54

李维's avatar
李维 committed
55
  ngOnDestroy() {
limingzhe's avatar
limingzhe committed
56 57
  }

李维's avatar
李维 committed
58 59 60
  saveData(e){
    this.save();
  }
limingzhe's avatar
limingzhe committed
61

李维's avatar
李维 committed
62 63 64 65 66 67 68
  init() {
    if (Object.keys(this.item.contentObj).length != 0 && this.item.contentObj.version && this.item.contentObj.version==defauleFormData.version) {
      // console.log("读取数据", this.item.contentObj)
      this.contentObj = this.item.contentObj;
      this.dataArray = this.item.contentObj.dataArray;
    } else {
      this.contentObj = defauleFormData;
李维's avatar
李维 committed
69
      this.dataArray = defauleFormData.dataArray
李维's avatar
李维 committed
70 71
      // console.log("使用默认数据", this.contentObj)
      this.item.contentObj = this.contentObj;
李维's avatar
李维 committed
72
      this.item.contentObj.dataArray = this.dataArray;
李维's avatar
李维 committed
73
    }
limingzhe's avatar
limingzhe committed
74 75
  }

李维's avatar
李维 committed
76 77 78 79 80 81 82
  cardItemData(){
    return {
      type: "Text",
      text: "",
      audio_url: "",
      image_url: ""
    };
limingzhe's avatar
limingzhe committed
83 84
  }

李维's avatar
李维 committed
85 86 87
  cardChoiceData(){
    return { isText: true, text: "", image_url: "" }
  }
limingzhe's avatar
limingzhe committed
88

李维's avatar
李维 committed
89 90 91 92
  getDefaultPicArr() {
    let arr = []; 
    return arr;
  }
limingzhe's avatar
limingzhe committed
93

李维's avatar
李维 committed
94
  initData() {
limingzhe's avatar
limingzhe committed
95

liujiangnan's avatar
liujiangnan committed
96 97
  }

limingzhe's avatar
limingzhe committed
98

李维's avatar
李维 committed
99 100 101 102 103
  addChoice(questionIndex) { 
    // let item = this.cardChoiceData();  
    // this.dataArray[questionIndex].choice.incorrect.push(item);
    // this.saveItem();
  }
limingzhe's avatar
limingzhe committed
104

李维's avatar
李维 committed
105 106 107
  onUploadSuccessByItem(e, item, key) { 
    item[key] = e.url; 
    this.save();
liujiangnan's avatar
liujiangnan committed
108 109
  }

李维's avatar
李维 committed
110 111 112 113 114 115 116 117 118
  onImageUploadSuccessByItem(e, item) {
    item.image_url = e.url
    this.save(); 
  }
 
  onAudioUploadSuccessByItem(e, item, key) { 
    item[key] = e.url; 
    this.save();
  }
limingzhe's avatar
limingzhe committed
119

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

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

李维's avatar
李维 committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  deleteItem(index){
    this.dataArray.splice(index,1)
    this.save()
  }

  radioClick(it, radioValue) {
    it.radioValue = radioValue;
    this.saveItem();
  }

  clickCheckBox() {
    this.saveItem();
  }

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

  save() {
李维's avatar
李维 committed
150
    (<any> window).courseware.setData(this.item, null, this.KEY);
limingzhe's avatar
limingzhe committed
151
    this.refresh();
李维's avatar
李维 committed
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