Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, ComponentRef, ComponentFactory } from '@angular/core';
- import { ReactiveFormsModule, FormsModule, Validators, FormBuilder, FormGroup, FormControl } from '@angular/forms';
- import * as json from '../assets/questions.json';
- import { QuestionComponent } from './question/question.component';
- import { MainQuestionComponent } from './main-question/main-question.component';
- @Component({
- selector: 'app-root',
- templateUrl: './app.component.html',
- styleUrls: ['./app.component.css'],
- providers: []
- })
- export class AppComponent implements OnInit {
- title = 'Form Builder';
- questions : any;
- main_index:number = 0;
- mainQuestionComponents = [];
- questionComponents = [];
- mainQuestionsComponentRef: any;
- ngOnInit() {
- this.questions = json.default;
- }
- select_questions_by_level(main_index, parent_level) {
- var possible_questions = [];
- for (let i = 0; i < this.questions[main_index].questions.length; i++) {
- if (this.questions[main_index].questions[i].level == parent_level + 1)
- possible_questions.push(this.questions[main_index].questions[i])
- }
- if (this.questionComponents.length > 0) {
- for (let i = 0; i < this.questionComponents.length; i++) {
- for (let j = 0; j <= possible_questions.length - 1; j++){
- if (possible_questions[j].content == this.questionComponents[i].instance.question) {
- possible_questions.splice(j--,1);
- }
- }
- }
- }
- return possible_questions;
- }
- get_key(main_index, question) {
- var key;
- for (let i = 0; i < this.questions[main_index].questions.length; i++) {
- if (this.questions[main_index].questions[i].content == question)
- key = i;
- }
- return key;
- }
- get_sequence(main_index, parent, child) {
- var previous_key = this.get_key(0, parent),
- actual_key = this.get_key(0, child),
- levels = [];
- for (let i = previous_key; i <= actual_key; i++) {
- levels.push(this.questions[main_index].questions[i].level);
- }
- if (levels[0] != levels[1])
- return true;
- return false;
- }
- @ViewChild('questionsContainer', { read: ViewContainerRef }) entry: ViewContainerRef; constructor(private resolver: ComponentFactoryResolver) { }
- createMainQuestionComponent() {
- const factory = this.resolver.resolveComponentFactory(MainQuestionComponent);
- this.mainQuestionsComponentRef = this.entry.createComponent(factory);
- this.mainQuestionComponents.push(this.mainQuestionsComponentRef);
- this.mainQuestionsComponentRef.instance.main_question_index = this.main_index;
- this.mainQuestionsComponentRef.instance.main_question_content = this.questions[this.main_index].main_question.content;
- this.mainQuestionsComponentRef.instance.main_question_type = this.questions[this.main_index].main_question.type;
- this.mainQuestionsComponentRef.instance.main_question_answer = null;
- this.mainQuestionsComponentRef.instance.main_question_childrens_length = 0;
- this.mainQuestionsComponentRef.instance.onMainQuestionAddSubInput.subscribe(msg => this.onMainQuestionAddSubInput(msg));
- this.mainQuestionsComponentRef.instance.onMainQuestionDestroy.subscribe(msg => this.onMainQuestionDestroy(msg));
- }
- createQuestionComponent(main_index, sub_index, last_answer, last_type, question, type, level) {
- const factory = this.resolver.resolveComponentFactory(QuestionComponent);
- this.questionsComponentRef = this.entry.createComponent(factory);
- this.questionComponents.push(this.questionsComponentRef);
- this.questionsComponentRef.instance.main_index = main_index;
- this.questionsComponentRef.instance.question_index = sub_index;
- this.questionsComponentRef.instance.last_answer = last_answer;
- this.mainQuestionsComponentRef.instance.question_answer = null;
- this.questionsComponentRef.instance.last_type = last_type;
- this.questionsComponentRef.instance.question = question;
- this.questionsComponentRef.instance.type = type;
- this.questionsComponentRef.instance.level = level;
- this.questionsComponentRef.instance.onQuestionAddSubInput.subscribe(msg => this.onQuestionAddSubInput(msg));
- this.questionsComponentRef.instance.onQuestionDestroy.subscribe(msg => this.onQuestionDestroy(msg));
- }
- addMainQuestion() {
- if (this.main_index < this.questions.length) {
- this.createMainQuestionComponent();
- this.main_index++;
- }
- }
- onMainQuestionAddSubInput(main_index) {
- var last_answer, last_type, question, type, level;
- switch (this.mainQuestionComponents[main_index].instance.main_question_type) {
- case 'select':
- if (this.mainQuestionComponents[main_index].instance.main_question_answer == 'Yes') {
- let possible = this.select_questions_by_level(main_index, 0);
- if (possible.length > 0) {
- let data = possible[0],
- last_answer = this.mainQuestionComponents[main_index].instance.main_question_answer,
- last_type = this.mainQuestionComponents[main_index].instance.main_question_type,
- question = data.content,
- type = data.type;
- this.createQuestionComponent(main_index, this.mainQuestionComponents[main_index].instance.main_question_childrens_length, last_answer, last_type, question, type, 1);
- this.mainQuestionComponents[main_index].instance.main_question_childrens_length++;
- }
- }
- break;
- default:
- let possible = this.select_questions_by_level(main_index, 0);
- if (possible.length > 0) {
- let data = possible[0],
- last_answer = this.mainQuestionComponents[main_index].instance.main_question_answer,
- last_type = this.mainQuestionComponents[main_index].instance.main_question_type,
- question = data.content,
- type = data.type;
- this.createQuestionComponent(main_index, this.mainQuestionComponents[main_index].instance.main_question_childrens_length, last_answer, last_type, question, type, 1);
- this.mainQuestionComponents[main_index].instance.main_question_childrens_length++;
- }
- break;
- }
- }
- onQuestionAddSubInput(data) {
- if (this.select_questions_by_level(data.main_index, data.level).length > 0) {
- let possible = this.select_questions_by_level(data.main_index, data.level)[0],
- last_object = null;
- for (let i = 0; i < this.questionComponents.length; i++) {
- if (this.questionComponents[i].instance.main_index == data.main_index && this.questionComponents[i].instance.question_index == data.question_index)
- last_object = this.questionComponents[i];
- }
- if (this.get_sequence(data.main_index, last_object.instance.question, possible.content)) {
- let last_answer = last_object.instance.question_answer,
- last_type = last_object.instance.type,
- question = possible.content,
- type = possible.type,
- level = possible.level;
- this.createQuestionComponent(data.main_index, this.mainQuestionComponents[data.main_index].instance.main_question_childrens_length, last_answer, last_type, question, type, level);
- this.mainQuestionComponents[data.main_index].instance.main_question_childrens_length++;
- }
- }
- }
- onMainQuestionDestroy(main_index) {
- this.mainQuestionComponents[main_index].destroy();
- for (let i = 0; i < this.questionComponents.length; i++) {
- if (this.questionComponents[i].instance.main_index == main_index)
- this.questionComponents[i].destroy();
- }
- }
- onQuestionDestroy(data) {
- for (let i = 0; i < this.questionComponents.length; i++) {
- if (this.questionComponents[i].instance.main_index == data.main_index && this.questionComponents[i].instance.question_index == data.question_index)
- this.questionComponents[i].destroy();
- }
- }
- }
Add Comment
Please, Sign In to add comment