daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Oct 23rd, 2018 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, ViewChild, ViewContainerRef, ComponentFactoryResolver, ComponentRef, ComponentFactory } from '@angular/core';
  2. import { ReactiveFormsModule, FormsModule, Validators, FormBuilder, FormGroup, FormControl } from '@angular/forms';
  3. import * as json from '../assets/questions.json';
  4.  
  5. import { QuestionComponent } from './question/question.component';
  6. import { MainQuestionComponent } from './main-question/main-question.component';
  7.  
  8. @Component({
  9.   selector: 'app-root',
  10.   templateUrl: './app.component.html',
  11.   styleUrls: ['./app.component.css'],
  12.   providers: []
  13. })
  14. export class AppComponent implements OnInit {
  15.   title = 'Form Builder';
  16.  
  17.   questions : any;
  18.   main_index:number = 0;
  19.  
  20.   mainQuestionComponents = [];
  21.   questionComponents = [];
  22.  
  23.   mainQuestionsComponentRef: any;
  24.  
  25.   ngOnInit() {
  26.     this.questions = json.default;
  27.   }
  28.  
  29.   select_questions_by_level(main_index, parent_level) {
  30.     var possible_questions = [];
  31.  
  32.     for (let i = 0; i < this.questions[main_index].questions.length; i++) {
  33.       if (this.questions[main_index].questions[i].level == parent_level + 1)
  34.         possible_questions.push(this.questions[main_index].questions[i])
  35.     }
  36.  
  37.     if (this.questionComponents.length > 0) {
  38.       for (let i = 0; i < this.questionComponents.length; i++) {
  39.         for (let j = 0; j <= possible_questions.length - 1; j++){
  40.           if (possible_questions[j].content == this.questionComponents[i].instance.question) {
  41.             possible_questions.splice(j--,1);
  42.           }
  43.         }
  44.       }
  45.     }
  46.     return possible_questions;
  47.   }
  48.  
  49.   get_key(main_index, question) {
  50.     var key;
  51.  
  52.     for (let i = 0; i < this.questions[main_index].questions.length; i++) {
  53.       if (this.questions[main_index].questions[i].content == question)
  54.         key = i;
  55.     }
  56.     return key;
  57.   }
  58.  
  59.   get_sequence(main_index, parent, child) {
  60.     var previous_key = this.get_key(0, parent),
  61.     actual_key = this.get_key(0, child),
  62.     levels = [];
  63.  
  64.     for (let i = previous_key; i <= actual_key; i++) {
  65.       levels.push(this.questions[main_index].questions[i].level);
  66.     }
  67.  
  68.     if (levels[0] != levels[1])
  69.       return true;
  70.     return false;
  71.   }
  72.  
  73.   @ViewChild('questionsContainer', { read: ViewContainerRef }) entry: ViewContainerRef; constructor(private resolver: ComponentFactoryResolver) { }
  74.  
  75.   createMainQuestionComponent() {
  76.     const factory = this.resolver.resolveComponentFactory(MainQuestionComponent);
  77.     this.mainQuestionsComponentRef = this.entry.createComponent(factory);
  78.     this.mainQuestionComponents.push(this.mainQuestionsComponentRef);
  79.     this.mainQuestionsComponentRef.instance.main_question_index = this.main_index;
  80.     this.mainQuestionsComponentRef.instance.main_question_content = this.questions[this.main_index].main_question.content;
  81.     this.mainQuestionsComponentRef.instance.main_question_type = this.questions[this.main_index].main_question.type;
  82.     this.mainQuestionsComponentRef.instance.main_question_answer = null;
  83.     this.mainQuestionsComponentRef.instance.main_question_childrens_length = 0;
  84.     this.mainQuestionsComponentRef.instance.onMainQuestionAddSubInput.subscribe(msg => this.onMainQuestionAddSubInput(msg));
  85.     this.mainQuestionsComponentRef.instance.onMainQuestionDestroy.subscribe(msg => this.onMainQuestionDestroy(msg));
  86.   }
  87.  
  88.   createQuestionComponent(main_index, sub_index, last_answer, last_type, question, type, level) {
  89.     const factory = this.resolver.resolveComponentFactory(QuestionComponent);
  90.     this.questionsComponentRef = this.entry.createComponent(factory);
  91.     this.questionComponents.push(this.questionsComponentRef);
  92.     this.questionsComponentRef.instance.main_index = main_index;
  93.     this.questionsComponentRef.instance.question_index = sub_index;
  94.     this.questionsComponentRef.instance.last_answer = last_answer;
  95.     this.mainQuestionsComponentRef.instance.question_answer = null;
  96.     this.questionsComponentRef.instance.last_type = last_type;
  97.     this.questionsComponentRef.instance.question = question;
  98.     this.questionsComponentRef.instance.type = type;
  99.     this.questionsComponentRef.instance.level = level;
  100.     this.questionsComponentRef.instance.onQuestionAddSubInput.subscribe(msg => this.onQuestionAddSubInput(msg));
  101.     this.questionsComponentRef.instance.onQuestionDestroy.subscribe(msg => this.onQuestionDestroy(msg));
  102.   }
  103.  
  104.   addMainQuestion() {
  105.     if (this.main_index < this.questions.length) {
  106.       this.createMainQuestionComponent();
  107.       this.main_index++;
  108.     }
  109.   }
  110.  
  111.   onMainQuestionAddSubInput(main_index) {
  112.     var last_answer, last_type, question, type, level;
  113.     switch (this.mainQuestionComponents[main_index].instance.main_question_type) {
  114.       case 'select':
  115.           if (this.mainQuestionComponents[main_index].instance.main_question_answer == 'Yes') {
  116.           let possible = this.select_questions_by_level(main_index, 0);
  117.           if (possible.length > 0) {
  118.             let data = possible[0],
  119.             last_answer = this.mainQuestionComponents[main_index].instance.main_question_answer,
  120.             last_type = this.mainQuestionComponents[main_index].instance.main_question_type,
  121.             question = data.content,
  122.             type = data.type;
  123.            
  124.             this.createQuestionComponent(main_index, this.mainQuestionComponents[main_index].instance.main_question_childrens_length, last_answer, last_type, question, type, 1);
  125.             this.mainQuestionComponents[main_index].instance.main_question_childrens_length++;
  126.           }
  127.           }
  128.         break;
  129.       default:
  130.         let possible = this.select_questions_by_level(main_index, 0);
  131.         if (possible.length > 0) {
  132.           let data = possible[0],
  133.           last_answer = this.mainQuestionComponents[main_index].instance.main_question_answer,
  134.           last_type = this.mainQuestionComponents[main_index].instance.main_question_type,
  135.           question = data.content,
  136.           type = data.type;
  137.  
  138.           this.createQuestionComponent(main_index, this.mainQuestionComponents[main_index].instance.main_question_childrens_length, last_answer, last_type, question, type, 1);
  139.           this.mainQuestionComponents[main_index].instance.main_question_childrens_length++;
  140.         }
  141.         break;
  142.     }
  143.   }
  144.  
  145.   onQuestionAddSubInput(data) {
  146.     if (this.select_questions_by_level(data.main_index, data.level).length > 0) {
  147.       let possible = this.select_questions_by_level(data.main_index, data.level)[0],
  148.       last_object = null;
  149.       for (let i = 0; i < this.questionComponents.length; i++) {
  150.         if (this.questionComponents[i].instance.main_index == data.main_index && this.questionComponents[i].instance.question_index == data.question_index)
  151.           last_object = this.questionComponents[i];
  152.       }
  153.       if (this.get_sequence(data.main_index, last_object.instance.question, possible.content)) {
  154.         let last_answer = last_object.instance.question_answer,
  155.         last_type = last_object.instance.type,
  156.         question = possible.content,
  157.         type = possible.type,
  158.         level = possible.level;
  159.  
  160.         this.createQuestionComponent(data.main_index, this.mainQuestionComponents[data.main_index].instance.main_question_childrens_length, last_answer, last_type, question, type, level);
  161.         this.mainQuestionComponents[data.main_index].instance.main_question_childrens_length++;
  162.       }
  163.     }
  164.   }
  165.  
  166.   onMainQuestionDestroy(main_index) {
  167.     this.mainQuestionComponents[main_index].destroy();
  168.     for (let i = 0; i < this.questionComponents.length; i++) {
  169.       if (this.questionComponents[i].instance.main_index == main_index)
  170.         this.questionComponents[i].destroy();
  171.     }
  172.   }
  173.  
  174.   onQuestionDestroy(data) {
  175.     for (let i = 0; i < this.questionComponents.length; i++) {
  176.       if (this.questionComponents[i].instance.main_index == data.main_index && this.questionComponents[i].instance.question_index == data.question_index)
  177.         this.questionComponents[i].destroy();
  178.     }
  179.   }
  180. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top