File

src/app/pages/answers/answers.component.ts

Extends

BaseComponent

Implements

OnInit AfterViewInit OnChanges

Metadata

selector framadate-answers
styleUrls ./answers.component.scss
templateUrl ./answers.component.html

Index

Properties
Methods

Constructor

constructor(config: ConfigService, document: any, cd: ChangeDetectorRef)
Parameters :
Name Type Optional
config ConfigService No
document any No
cd ChangeDetectorRef No

Methods

addAnswer
addAnswer()
Returns : void
checkValidAnswers
checkValidAnswers()
Returns : void
focusOnAnswer
focusOnAnswer(i)
Parameters :
Name Optional
i No
Returns : void
navigateOrDelete
navigateOrDelete(event: KeyboardEvent, i)
Parameters :
Name Type Optional
event KeyboardEvent No
i No
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : void
ngOnChanges
ngOnChanges()
Returns : void
ngOnInit
ngOnInit()
Returns : void
showModalForPictureOfAnswer
showModalForPictureOfAnswer(answer)
Parameters :
Name Optional
answer No
Returns : void
trackFunction
trackFunction(index: number, item: any)
Parameters :
Name Type Optional
index number No
item any No
Returns : number
checkValidity
checkValidity()
Inherited from BaseComponent
Defined in BaseComponent:21
Returns : boolean
displayErrorMessage
displayErrorMessage()
Inherited from BaseComponent
Defined in BaseComponent:27
Returns : boolean
ngOnInit
ngOnInit()
Inherited from BaseComponent
Defined in BaseComponent:17
Returns : void

Properties

allAnswersAreValid
Default value : false
answerList
Type : []
Default value : []
Public config
Type : ConfigService
currentHeader
Type : any
Default value : ""
display
Type : boolean
Public config
Type : ConfigService
Inherited from BaseComponent
Defined in BaseComponent:14
import {AfterViewInit, ChangeDetectorRef, Component, Inject, OnChanges, OnInit} from '@angular/core';
import {BaseComponent} from '../base-page/base.component';
import {ConfigService} from '../../services/config.service';

import {DOCUMENT} from '@angular/common';

@Component({
    selector: 'framadate-answers',
    templateUrl: './answers.component.html',
    styleUrls: ['./answers.component.scss']
})
export class AnswersComponent extends BaseComponent implements OnInit, AfterViewInit, OnChanges {

    allAnswersAreValid = false;

    answerList = [];
    currentHeader: any = "";
    display: boolean;

    constructor(public config: ConfigService,
                @Inject(DOCUMENT) private document: any,
                private cd: ChangeDetectorRef) {
        super(config);
        this.answerList = this.config.answers;
    }

    // todo, manage validation of each page in a common way
    ngOnInit() {
    }

    ngOnChanges() {

        this.checkValidAnswers();
    }

    checkValidAnswers() {
        this.allAnswersAreValid = true;
        this.config.answers.forEach(answer => {
            if (!answer.text.length) {
                this.allAnswersAreValid = false;
                return;
            }
        });
    }

    ngAfterViewInit() {
        this.focusOnAnswer(0);
        this.checkValidAnswers();
    }

    trackFunction(index: number, item: any): number {
        return item.id;
    }

    addAnswer() {
        this.config.answers.push(
            {
                id: this.config.answers.length + 1,
                text: '',
                url: '',
                file: '',
                literal: '',
                date_object: null,
                timeList: []
            });
        this.cd.detectChanges(); // to refresh the view before focusing on the new input
        this.focusOnAnswer(this.config.answers.length - 1)
    }

    focusOnAnswer(i) {
        const AnswersDomToFocus = this.document.querySelectorAll('.answers .answer');
        const dom = AnswersDomToFocus[i];
        if (dom.focus) {
            dom.focus();
        }
        if (dom.select) {
            dom.select();
        }
    }

    navigateOrDelete(event: KeyboardEvent, i) {
        if (event.ctrlKey && event.key == "d") {
            this.config.answers.splice(i, 1)
        }
        if (event.key == "ArrowUp" && i > 0) {
            this.focusOnAnswer(i - 1);
        }
        if (event.key == "ArrowDown" && i < this.config.answers.length) {
            this.focusOnAnswer(i + 1);
        }
    }

    showModalForPictureOfAnswer(answer) {
        // TODO
        this.currentHeader = answer;
        this.display = true;
        // this.config.todo();
    }
}
<div class="answers" >
    <h1 i18n >
        Choisir les propositions
    </h1 >

    <p
        class="subtitle"
        i18n >
        Vous pouvez utiliser la syntaxe markdown, et naviguer entre les inputs avec les flèches du clavier.
    </p >

    <ol >
        <li
            #answers
            *ngFor="let answer of config.answers; index as i;trackBy trackFunction"
            class="answer-item" >
            <button
                class='btn btn--default'
                title='ajouter une image'
                (click)='showModalForPictureOfAnswer(answer)' >
                <i class='fa fa-image' ></i >
            </button >
            <label
                for='answer_{{answer.id}}_url'
                (click)='showModalForPictureOfAnswer(answer)' >

                <img
                    class='img-thumbnail'
                    src='{{answer.url}}'
                    alt='image {{answer.url}}' >
            </label >
            <p-dialog
                class='url-dialog'
                [(visible)]="display"
                [modal]='true' >
                <p-header >
                    {{answer.text}}
                </p-header >

                <form
                    action='#'
                    (submit)='display=false' >
                    <label for='answer_{{answer.id}}_url' >
                        Choisissez une URL pour illustrer le choix de réponse
                    </label >
                    <i class='fa fa-image' ></i >
                    <br >
                    <input
                        class='input is-block'
                        id='answer_{{answer.id}}_url'
                        type='text'
                        autofocus='autofocus'
                        name='answer-url'
                        [(ngModel)]='answer.url' >

                </form >

            </p-dialog >
            <input
                type="name"
                class="answer"
                id='answer_{{answer.id}}'
                [(ngModel)]="answer.text"
                (keyup.enter)="addAnswer()"
                (keyup)="navigateOrDelete($event,i)"
                required='required'
                placeholder="réponse"
            >
            <button
                class="btn btn--alert"
                (click)="config.answers.splice(i,1)" >X
            </button >
        </li >
    </ol >

    <button
        class="btn btn--primary btn--outline"
        (click)="addAnswer()"
        [ngClass]="{'btn--primary': allAnswersAreValid}"
        i18n
    >
        <i class='fa fa-plus' ></i >
        Ajouter une proposition
    </button >
    <br >
    <button
        [routerLink]="'/step/resume'"
        class="btn btn--full "
        i18n
        [ngClass]="{'btn--primary': allAnswersAreValid}"
        [disabled]='!allAnswersAreValid'
    >
        Voyons ce que ça donne
    </button >
    <br >
    <a
        [routerLink]="'/home'"
        class="prev"
        i18n >
        Retour
    </a >
</div >

./answers.component.scss

Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""