app.component.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Component, OnInit, OnChanges, ViewChild, TemplateRef } from '@angular/core';
  2. import { FormGroup } from '@angular/forms';
  3. import { DynaformService } from './dynaform/services/dynaform.service';
  4. import * as test1 from './_mock/testfields.v1';
  5. import * as test2 from './_mock/testfields.v2';
  6. import * as test3 from './_mock/testfields.v3';
  7. import * as test4 from './_mock/testfields.v4';
  8. import * as test5 from './_mock/testfields.v5';
  9. import * as test6 from './_mock/testfields.v6';
  10. import * as test7 from './_mock/testfields.v7';
  11. import * as test8 from './_mock/testfields.v8';
  12. const testdata = [ test1, test2, test3, test4, test5, test6, test7, test8 ];
  13. const defatltTest = 8;
  14. @Component({
  15. selector: 'app-root',
  16. templateUrl: './app.component.html',
  17. styleUrls: ['./app.component.scss']
  18. })
  19. export class AppComponent implements OnInit, OnChanges {
  20. form: FormGroup;
  21. meta: StringMap;
  22. hCssRed = 'color: white; background-color: maroon; font-weight: bold;';
  23. hCssGreen = 'color: white; background-color: green; font-weight: bold;';
  24. @ViewChild('testTemplate', { read: TemplateRef })
  25. tref: TemplateRef<any>;
  26. constructor(
  27. private dynaform: DynaformService
  28. ) {
  29. }
  30. ngOnInit() {
  31. // Optionally supply the test to run in the query string e.g. ?test=3
  32. const testcase = parseInt(new URLSearchParams(document.location.search).get('test'), 10) || defatltTest;
  33. const { model, meta } = testdata[testcase - 1];
  34. console.log('%c *** TEST DATA *** ', this.hCssRed);
  35. console.log('Model', model);
  36. console.log('Meta', meta);
  37. // Test autoMeta
  38. const auto = this.dynaform.autoMeta(model);
  39. console.log(auto);
  40. // Test combine
  41. const m2 = this.dynaform.combineModelWithMeta(model, meta);
  42. console.log(m2);
  43. // Test building Field-Specific-Meta
  44. const fsm = this.dynaform.buildFieldSpecificMeta(m2);
  45. console.log(fsm);
  46. // Test building the FormGroup
  47. const fg = this.dynaform.buildFormGroup(fsm);
  48. console.log(fg);
  49. // Build the FormGroup and Modeled Metadata from the imported test data
  50. const dynaformdata = this.dynaform.build(model, meta);
  51. ({ form: this.form, meta: this.meta } = dynaformdata);
  52. console.log('%c *** Modeled MetaData *** ', this.hCssGreen);
  53. console.dir(this.meta);
  54. console.log('%c *** FormGroup *** ', this.hCssGreen);
  55. console.dir(this.form);
  56. }
  57. ngOnChanges() {
  58. console.log(this.form.errors);
  59. }
  60. }