form.component.ts 4.1 KB
Newer Older
liujiaxin's avatar
liujiaxin committed
1 2 3 4 5 6 7 8 9 10 11 12
import {
  Component,
  EventEmitter,
  Input,
  OnDestroy,
  OnChanges,
  OnInit,
  Output,
  ApplicationRef,
  ChangeDetectorRef,
  AfterViewInit
} from '@angular/core';
limingzhe's avatar
limingzhe committed
13
import { JsonPipe } from '@angular/common';
limingzhe's avatar
limingzhe committed
14

liujiangnan's avatar
liujiangnan committed
15 16 17 18

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
liujiaxin's avatar
liujiaxin committed
19
  styleUrls: ['./form.component.css']
liujiangnan's avatar
liujiangnan committed
20
})
liujiaxin's avatar
liujiaxin committed
21
export class FormComponent implements OnInit, OnChanges, OnDestroy, AfterViewInit {
liujiangnan's avatar
liujiangnan committed
22

limingzhe's avatar
limingzhe committed
23
  // 储存数据用
limingzhe's avatar
limingzhe committed
24
  saveKey = "JM_MATH01";
limingzhe's avatar
limingzhe committed
25 26
  // 储存对象
  item;
liujiaxin's avatar
liujiaxin committed
27 28
  // 公式键盘
  fboard = null;
limingzhe's avatar
limingzhe committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  customTypeGroupArr = [

    {
      name: '轮播图片',
      audio: true,
      pic: true,
      // rect: true,
      // isShowPos: true,
      // isCopy: true,

      // label: '比对',
    },
    {
      name: '选择区域',
      rect: true,
    },
    {
      name: '简答问题',
      pic: true,
    },
    {
      name: '简答填空区',
      rect: true,
      label: '答案',
    },
liujiaxin's avatar
liujiaxin committed
54 55
  ];
   testValue = '123';
limingzhe's avatar
limingzhe committed
56 57 58


  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef) {
liujiaxin's avatar
liujiaxin committed
59 60
    // @ts-ignore
    window.fff = this;
limingzhe's avatar
limingzhe committed
61
  }
limingzhe's avatar
limingzhe committed
62

limingzhe's avatar
limingzhe committed
63 64 65 66 67 68 69 70
  createShell() {
    this.item.wordList.push({
      word: '',
      audio: '',
      backWord: '',
      backWordAudio: '',
    });
    this.save();
limingzhe's avatar
limingzhe committed
71 72
  }

limingzhe's avatar
limingzhe committed
73 74 75 76
  removeShell(idx) {
    this.item.wordList.splice(idx, 1);
    this.save();
  }
limingzhe's avatar
limingzhe committed
77

liujiangnan's avatar
liujiangnan committed
78 79
  ngOnInit() {

limingzhe's avatar
limingzhe committed
80 81
    this.item = {};

limingzhe's avatar
limingzhe committed
82
    // 获取存储的数据
limingzhe's avatar
limingzhe committed
83
    (<any>window).courseware.getData((data) => {
limingzhe's avatar
limingzhe committed
84 85 86 87 88

      if (data) {
        this.item = data;
      }

limingzhe's avatar
limingzhe committed
89 90
      console.log('this.item: ', this.item);

limingzhe's avatar
limingzhe committed
91
      this.init();
liujiangnan's avatar
liujiangnan committed
92 93
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
limingzhe's avatar
limingzhe committed
94 95
      this.refresh();

limingzhe's avatar
limingzhe committed
96
    }, this.saveKey);
limingzhe's avatar
limingzhe committed
97
  }
liujiaxin's avatar
liujiaxin committed
98 99 100
  ngAfterViewInit() {


limingzhe's avatar
limingzhe committed
101

liujiaxin's avatar
liujiaxin committed
102
  }
limingzhe's avatar
limingzhe committed
103
  ngOnChanges() {
limingzhe's avatar
limingzhe committed
104 105
  }

limingzhe's avatar
limingzhe committed
106
  ngOnDestroy() {
limingzhe's avatar
limingzhe committed
107 108
  }

limingzhe's avatar
limingzhe committed
109 110 111 112 113 114 115 116
  init() {

    if (!this.item.sentenceArr) {
      this.item.sentenceArr = [];
    }
    if (!this.item.templateArr) {
     this.item.templateArr = [];
    }
liujiaxin's avatar
liujiaxin committed
117

limingzhe's avatar
limingzhe committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    if (!this.item.optionArr) {
      this.item.optionArr = [
        {id: 'A', optionArr: [{id: 'A'}, {id: 'B'}]},
        {id: 'B', optionArr: [{id: 'A'}, {id: 'B'}]},
        {id: 'C', optionArr: [{id: 'A'}, {id: 'B'}]},
        {id: 'D', optionArr: [{id: 'A'}, {id: 'B'}]},
      ];
    }

    if (!this.item.ques_type) {
      this.item.ques_type = 'single_choice'
    }
  }

  addSubTemplate() {
    this.item.templateArr.push({
    })

    this.save();
  }

  onDeleteTemplate(e, index) {
    this.item.templateArr[index] = {};
    this.item.templateArr.splice(index, 1);
    this.save();
  }

  onSaveTemplate(e, index) {

    this.item.templateArr[index] = e;
    this.save();
  }
limingzhe's avatar
limingzhe committed
150 151 152



limingzhe's avatar
limingzhe committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
  addBtnClick() {
    this.item.sentenceArr.push({});
    this.save();
  }

  deleteBtnClick(index) {
    this.item.sentenceArr.splice(index, 1);
    this.save();

  }

  onSaveCustomAction(e) {
    console.log('e:', e);
    this.item.customAction = e;
    this.save();
  }


  saveHotZone(group, e) {
    console.log('e: ', e);
    const {bgItem, hotZoneItemArr} = e;
liujiaxin's avatar
liujiaxin committed
174

limingzhe's avatar
limingzhe committed
175 176 177 178
    group.bgItem = bgItem;
    group.hotZoneItemArr = hotZoneItemArr;

    this.save();
liujiangnan's avatar
liujiangnan committed
179 180
  }

limingzhe's avatar
limingzhe committed
181

limingzhe's avatar
limingzhe committed
182 183 184 185
  /**
   * 储存图片数据
   * @param e
   */
limingzhe's avatar
limingzhe committed
186 187 188 189
  onImageUploadSuccess(e, key, item=null) {
    if (!item) {
      item = this.item;
    }
limingzhe's avatar
limingzhe committed
190

limingzhe's avatar
limingzhe committed
191 192
    item[key] = e.url;
    this.save();
liujiangnan's avatar
liujiangnan committed
193 194
  }

limingzhe's avatar
limingzhe committed
195 196 197 198
  /**
   * 储存音频数据
   * @param e
   */
limingzhe's avatar
limingzhe committed
199 200 201 202 203 204 205
  onAudioUploadSuccess(e, key, item=null) {
    if (item == null) {
      item = this.item;
    }
    item[key] = e.url;
    this.save();
  }
limingzhe's avatar
limingzhe committed
206

limingzhe's avatar
limingzhe committed
207 208
  onWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].audio = e.url;
limingzhe's avatar
limingzhe committed
209 210 211
    this.save();
  }

limingzhe's avatar
limingzhe committed
212 213 214 215
  onBackWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].backWordAudio = e.url;
    this.save();
  }
limingzhe's avatar
limingzhe committed
216

limingzhe's avatar
limingzhe committed
217 218 219 220 221 222 223 224 225
  onVideoUploadSuccess(e, item=null) {

    console.log(' in onVideoUploadSuccess')
    if (!item) {
      item = this.item;
    }
    item.video_url = e.url;
    this.save();
  }
limingzhe's avatar
limingzhe committed
226

limingzhe's avatar
limingzhe committed
227 228 229
  /**
   * 储存数据
   */
limingzhe's avatar
limingzhe committed
230
  save() {
limingzhe's avatar
limingzhe committed
231 232
    (<any>window).courseware.setData(this.item, null, this.saveKey);

limingzhe's avatar
limingzhe committed
233
    this.refresh();
limingzhe's avatar
limingzhe committed
234
    console.log('this.item = ' + JSON.stringify(this.item));
limingzhe's avatar
limingzhe committed
235 236
  }

limingzhe's avatar
limingzhe committed
237 238 239
  /**
   * 刷新 渲染页面
   */
limingzhe's avatar
limingzhe committed
240 241 242 243
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
liujiangnan's avatar
liujiangnan committed
244 245
  }

liujiaxin's avatar
liujiaxin committed
246
}