form.component.ts 1.92 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']
})
liujiangnan's avatar
liujiangnan committed
10
export class FormComponent implements OnInit, OnChanges, OnDestroy {
范雪寒's avatar
范雪寒 committed
11 12

  // 储存数据用
liujiangnan's avatar
liujiangnan committed
13
  saveKey = "test_001";
范雪寒's avatar
范雪寒 committed
14
  // 储存对象
liujiangnan's avatar
liujiangnan committed
15 16 17 18 19 20 21 22 23
  item;

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

  }

  createShell() {
    this.item.wordList.push({
      word: '',
范雪寒's avatar
范雪寒 committed
24
      audio: '',
liujiangnan's avatar
liujiangnan committed
25 26
      backWord: '',
      backWordAudio: '',
范雪寒's avatar
范雪寒 committed
27 28 29 30
    });
    this.save();
  }

liujiangnan's avatar
liujiangnan committed
31 32
  removeShell(idx) {
    this.item.wordList.splice(idx, 1);
范雪寒's avatar
范雪寒 committed
33 34
    this.save();
  }
35

liujiangnan's avatar
liujiangnan committed
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 79 80 81
  ngOnInit() {

    this.item = {};

    // 获取存储的数据
    (<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;
82 83
    this.save();
  }
liujiangnan's avatar
liujiangnan committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

  onWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].audio = e.url;
    this.save();
  }

  onBackWordAudioUploadSuccess(e, idx) {
    this.item.wordList[idx].backWordAudio = e.url;
    this.save();
  }

  /**
   * 储存数据
   */
  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
114
}