form.component.ts 3.14 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
      this.save()
李维's avatar
李维 committed
50 51 52
    }, this.KEY);
  }
  ngOnChanges() {
limingzhe's avatar
limingzhe committed
53

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

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

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

李维's avatar
李维 committed
63 64 65 66 67 68 69
  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
70
      this.dataArray = defauleFormData.dataArray
李维's avatar
李维 committed
71 72
      // console.log("使用默认数据", this.contentObj)
      this.item.contentObj = this.contentObj;
李维's avatar
李维 committed
73
      this.item.contentObj.dataArray = this.dataArray;
李维's avatar
李维 committed
74
    }
limingzhe's avatar
limingzhe committed
75 76
  }

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

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

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

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

liujiangnan's avatar
liujiangnan committed
97 98
  }

limingzhe's avatar
limingzhe committed
99

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

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

李维's avatar
李维 committed
111 112 113 114 115 116 117 118 119
  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
120

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

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

李维's avatar
李维 committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  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
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