framadate-responsive/src/app/pages/answers/answers.component.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-08-10 21:00:05 +02:00
import {ChangeDetectorRef, Component, Inject, OnInit} from '@angular/core';
2019-08-10 17:41:01 +02:00
import {BaseComponent} from '../base-page/base.component';
import {ConfigService} from '../../config.service';
2019-08-10 21:00:05 +02:00
import {DOCUMENT} from '@angular/common';
2019-08-10 17:41:01 +02:00
@Component({
selector: 'framadate-answers',
templateUrl: './answers.component.html',
styleUrls: ['./answers.component.scss']
})
export class AnswersComponent extends BaseComponent implements OnInit {
private answerList = [];
2019-08-10 18:14:00 +02:00
2019-08-11 16:21:49 +02:00
constructor(public config: ConfigService,
2019-08-10 21:00:05 +02:00
@Inject(DOCUMENT) private document: any,
private cd: ChangeDetectorRef) {
2019-08-10 17:41:01 +02:00
super(config);
2019-08-10 18:14:00 +02:00
this.answerList = this.config.answers;
2019-08-10 17:41:01 +02:00
}
// todo, manage validation of each page in a common way
ngOnInit() {
}
trackFunction(index: number, item: any): number {
return item.id;
}
addAnswer() {
this.config.answers.push(
{
id: this.config.answers.length + 1,
text: ''
});
this.cd.detectChanges(); // to refresh the view before focusing on the new input
2019-08-10 21:00:05 +02:00
const AnswersDomToFocus = this.document.querySelectorAll('.answers .answer');
const dom = AnswersDomToFocus[AnswersDomToFocus.length - 1];
if (dom.focus) {
dom.focus();
}
}
2019-08-10 18:14:00 +02:00
2019-08-10 17:41:01 +02:00
// add a new answer on the press of ENTER in an input
addWhenEnterKey(event) {
2019-08-10 18:14:00 +02:00
console.log('event', event);
2019-08-10 17:41:01 +02:00
if (event.keyCode === 13) {
this.addAnswer();
2019-08-10 17:41:01 +02:00
}
2019-08-10 18:14:00 +02:00
return;
2019-08-10 17:41:01 +02:00
}
}