app.component.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Component, OnInit, OnChanges, ViewChild, TemplateRef } from '@angular/core';
  2. import { FormGroup } from '@angular/forms';
  3. import { DynaformService } from './dynaform';
  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. import * as test9 from './_mock/testfields.v9';
  13. import * as test10 from './_mock/testfields.v10';
  14. import * as test11 from './_mock/testfields.v11';
  15. import * as test12 from './_mock/testfields.v12';
  16. import * as test13 from './_mock/testfields.v13';
  17. import * as test14 from './_mock/testfields.v14';
  18. const testdata = [ test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14 ];
  19. const defatltTest = 14;
  20. @Component({
  21. selector: 'app-root',
  22. templateUrl: './app.component.html',
  23. styleUrls: ['./app.component.scss']
  24. })
  25. export class AppComponent implements OnInit, OnChanges {
  26. form: FormGroup;
  27. meta: StringMap<any>;
  28. hCssRed = 'color: white; background-color: maroon; font-weight: bold;';
  29. hCssGreen = 'color: white; background-color: green; font-weight: bold;';
  30. @ViewChild('testTemplate', { read: TemplateRef })
  31. tref: TemplateRef<any>;
  32. constructor(
  33. private dynaform: DynaformService
  34. ) {
  35. }
  36. ngOnInit() {
  37. // Optionally supply the test to run in the query string e.g. ?test=3
  38. const testcase = parseInt(new URLSearchParams(document.location.search).get('test'), 10) || defatltTest;
  39. const { model, meta } = testdata[testcase - 1];
  40. console.log('%c *** TEST DATA *** ', this.hCssRed);
  41. console.log('Model', model);
  42. console.log('Meta', meta);
  43. /*
  44. console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  45. // Test autoMeta
  46. const auto = this.dynaform.autoMeta(model);
  47. console.log(auto);
  48. // Test combine
  49. const m2 = this.dynaform.combineModelWithMeta(model, meta, true);
  50. console.log(m2);
  51. // Test building Field-Specific-Meta
  52. const fsm = this.dynaform.buildFieldSpecificMeta(m2);
  53. console.log(fsm);
  54. // Test building the FormGroup
  55. const fg = this.dynaform.buildFormGroup(fsm);
  56. console.log(fg);
  57. console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  58. */
  59. // Build the FormGroup and Modeled Metadata from the imported test data
  60. const dynaformdata = this.dynaform.build(model, meta, true);
  61. ({ form: this.form, meta: this.meta } = dynaformdata);
  62. console.log('%c *** Modeled MetaData *** ', this.hCssGreen);
  63. console.dir(this.meta);
  64. console.log('%c *** FormGroup *** ', this.hCssGreen);
  65. console.dir(this.form);
  66. // Registering callbacks
  67. this.dynaform.register({
  68. 'SAYHELLO': this.sayHello,
  69. 'SAYCHEESE': this.sayCheese
  70. }, this);
  71. }
  72. ngOnChanges() {
  73. console.log(this.form.errors);
  74. }
  75. handleCallback(fnId) {
  76. this.dynaform.call(fnId);
  77. }
  78. sayHello() {
  79. alert('HELLO');
  80. }
  81. sayCheese() {
  82. alert('CHEESE');
  83. }
  84. }