form.component.ts 2 KB
Newer Older
范雪寒's avatar
范雪寒 committed
1 2 3 4 5 6 7 8 9
import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { JsonPipe } from '@angular/common';


@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
李维's avatar
李维 committed
10
export class FormComponent implements OnInit, OnChanges, OnDestroy {
范雪寒's avatar
范雪寒 committed
11 12

  // 储存数据用
李维's avatar
李维 committed
13
  saveKey = "test_001";
范雪寒's avatar
范雪寒 committed
14
  // 储存对象
李维's avatar
李维 committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  item;

  constructor(private appRef: ApplicationRef, private changeDetectorRef: ChangeDetectorRef) {

  }

  ngOnInit() {

    this.item = {
      audioUrl_loading: '',
      audioUrl_start: '',
      audioUrl_start_guessing: '',
      audioUrl_waiting_guessing: '',
      audioUrl_tip1: '',
      audioUrl_tip2: '',
      audioUrl_tip3: '',
      audioUrl_tip_wrong1: '',
      audioUrl_tip_wrong2: '',
      audioUrl_lose: '',
      audioUrl_win: '',
    };

    // 获取存储的数据
    (<any>window).courseware.getData((data) => {

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

      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);
  }

  ngOnChanges() {
  }

  ngOnDestroy() {
  }

  init() {

  }


  /**
   * 储存图片数据
   * @param e
   */
  onImageUploadSuccess(e, key) {

    this.item[key] = e.url;
    this.save();
  }

  /**
   * 储存音频数据
   * @param e
   */
  onAudioUploadSuccess(e, key) {
    this.item[key] = e.url;
范雪寒's avatar
范雪寒 committed
79 80 81
    this.save();
  }

李维's avatar
李维 committed
82 83
  onWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].audio = e.url;
范雪寒's avatar
范雪寒 committed
84 85
    this.save();
  }
86

李维's avatar
李维 committed
87 88
  onBackWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].backWordAudio = e.url;
89 90
    this.save();
  }
李维's avatar
李维 committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  /**
   * 储存数据
   */
  save() {
    (<any>window).courseware.setData(this.item, null, this.saveKey);

    this.refresh();
    console.log('this.item = ' + JSON.stringify(this.item));
  }

  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }

范雪寒's avatar
范雪寒 committed
111
}