extqa.ts 3.21 KB
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { DataService } from '../../providers/data-service';
import { Qa } from '../../providers/define-qa';
import { Content } from 'ionic-angular';
import { ViewChild } from '@angular/core';
import { Observable } from 'rxjs/Rx';

/*
  Generated class for the Extqa page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-extqa',
  templateUrl: 'extqa.html'
})
export class ExtqaPage {
    @ViewChild(Content) content: Content;

    qaList: Qa[];
    qa1: Qa;
    menuId: number;
    unitId: number;
    index: number;
    title: string;
    buttonDisabled: boolean;
    ds: DataService;
    nv: NavController;
    selected: number;
    result: string;
    selectstate: Array<boolean> = new Array();
    subscription;
    screenHeight:number;
    positiveCnt: number;

  constructor(public navCtrl: NavController, public navParams: NavParams, private dataSevice: DataService) {
        this.menuId = navParams.get("menuId");
        this.unitId = navParams.get("unitId");
        this.title = navParams.get("title");

        this.index = 0;
        this.qaList = dataSevice.getExtqa(this.menuId, this.unitId);
        this.qa1 = this.qaList[this.index];
        this.buttonDisabled = null;
        this.ds = dataSevice;
        this.nv = navCtrl;
        this.selected = -1;
        this.positiveCnt = 0;
        for (var i = 0; i < this.qa1.choicesId.length; i++) {
            this.selectstate.push(false);
        }
  
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ExtqaPage');
  }

  itemSelected(id: number) {
      for (var i = 0; i < this.qa1.choicesId.length; i++) {
          this.selectstate[i] = false;
      }
      this.selectstate[id] = true;
      this.selected = id;
      this.content.scrollToBottom(300);
  }

  openAnswer() {
      if (this.buttonDisabled == null)
      {
          this.result = "解答";

          if (this.selected === this.qa1.answerId) {  //正解
              this.result = "正解";
              this.positiveCnt++;
          }
          else {
              this.result = "不正解";
          }

          this.buttonDisabled = true;
          if (this.index >= (this.qaList.length - 1))
          {
              var key: string;
              var value: string;
              key = this.qa1.menuId + ":" + this.qa1.unitId;
              value = this.positiveCnt + ":" + this.qaList.length;
              this.dataSevice.saveInfo(key, value);
          }

          var element = document.getElementsByClassName('question-card');
          this.screenHeight = element[0].clientHeight;
          this.subscription = Observable.timer(300)
              .subscribe(x => {
                  this.content.scrollToBottom(300);
              }
          );
      }
  }

  goToNext() {
      this.index++;
      this.qa1 = this.qaList[this.index];
      this.buttonDisabled = null;
      this.selected = -1;
      this.selectstate = new Array();
      for (var i = 0; i < this.qa1.choicesId.length; i++) {
          this.selectstate.push(false);
      }
      this.content.scrollToTop(300);
  }

  backToUnit() {
      this.nv.pop();
  }
}