11import { JsonFormsElement } from '@jsonforms/webcomponent' ;
22import { ExampleDescription } from './example' ;
3- import { JsonFormsInit , jsonformsReducer } from '@jsonforms/core' ;
3+ import { jsonformsReducer , RankedTester } from '@jsonforms/core' ;
44import { combineReducers , createStore , Reducer } from 'redux' ;
55import { i18nReducer , translateProps } from '@jsonforms/i18n' ;
66
@@ -11,97 +11,108 @@ const knownExamples: {[key: string]: ExampleDescription} = {};
1111
1212export interface AdditionalStoreParams {
1313 name : string ;
14- reducer : Reducer < any > ;
14+ reducer ? : Reducer < any > ;
1515 state : any ;
1616}
1717
1818export const registerExamples = ( examples : ExampleDescription [ ] ) : void => {
1919 examples . forEach ( example => knownExamples [ example . name ] = example ) ;
2020} ;
2121
22- export const changeExample = ( selectedExample : string , ...additionalStoreParams : AdditionalStoreParams [ ] ) => {
23- let body = document . getElementById ( viewDivId ) ;
24- if ( body . firstChild !== null && body . firstChild . childNodes . length !== 0 ) {
25- body . removeChild ( body . firstChild ) ;
26- }
27- const example : ExampleDescription = knownExamples [ selectedExample ] ;
28- if ( example . setupCallback !== undefined ) {
29- const div = document . createElement ( 'div' ) ;
30- example . setupCallback ( div ) ;
31- body . appendChild ( div ) ;
32- body = div ;
33- }
22+ export const changeExample =
23+ ( selectedExample : string ,
24+ renderers : { tester : RankedTester , renderer : any } [ ] ,
25+ fields : { tester : RankedTester , field : any } [ ] ,
26+ ...additionalStoreParams : AdditionalStoreParams [ ] ) => {
27+ let body = document . getElementById ( viewDivId ) ;
28+ if ( body . firstChild !== null && body . firstChild . childNodes . length !== 0 ) {
29+ body . removeChild ( body . firstChild ) ;
30+ }
31+ const example : ExampleDescription = knownExamples [ selectedExample ] ;
32+ if ( example . setupCallback !== undefined ) {
33+ const div = document . createElement ( 'div' ) ;
34+ example . setupCallback ( div ) ;
35+ body . appendChild ( div ) ;
36+ body = div ;
37+ }
38+
39+ const jsonForms = document . createElement ( 'json-forms' ) as JsonFormsElement ;
40+ const additionalReducers = additionalStoreParams . reduce (
41+ ( acc , x ) => {
42+ if ( x . reducer ) {
43+ acc [ x . name ] = x . reducer ;
44+ }
45+ return acc ;
46+ } ,
47+ { }
48+ ) ;
49+ const additionalInitState = additionalStoreParams . reduce (
50+ ( acc , x ) => {
51+ acc [ x . name ] = x . state ;
3452
35- const jsonForms = document . createElement ( 'json-forms' ) as JsonFormsElement ;
36- const additionalReducers = additionalStoreParams . reduce (
37- ( acc , x ) => {
38- acc [ x . name ] = x . reducer ;
39- return acc ;
40- } ,
41- { }
42- ) ;
43- const additionalInitState = additionalStoreParams . reduce (
44- ( acc , x ) => {
45- acc [ x . name ] = x . state ;
46- return acc ;
47- } ,
48- { }
49- ) ;
53+ return acc ;
54+ } ,
55+ { }
56+ ) ;
5057
51- jsonForms . store = createStore (
52- combineReducers ( {
58+ jsonForms . store = createStore (
59+ combineReducers ( {
5360 jsonforms : jsonformsReducer (
5461 {
5562 i18n : i18nReducer ,
5663 ...additionalReducers
5764 } ,
5865 )
5966 }
60- ) ,
61- {
62- jsonforms : {
63- core : {
64- data : example . data ,
65- schema : example . schema ,
66- uischema : example . uiSchema
67- } ,
68- i18n : {
69- translations : example . translations ,
70- locale : example . locale || navigator . language
71- } ,
72- transformProps : [ translateProps ] ,
73- renderers : JsonFormsInit . renderers ,
74- fields : JsonFormsInit . fields ,
75- ...additionalInitState
67+ ) ,
68+ {
69+ jsonforms : {
70+ core : {
71+ data : example . data ,
72+ schema : example . schema ,
73+ uischema : example . uiSchema
74+ } ,
75+ renderers,
76+ fields,
77+ i18n : {
78+ translations : example . translations ,
79+ locale : example . locale || navigator . language
80+ } ,
81+ transformProps : [ translateProps ] ,
82+ ...additionalInitState
83+ }
7684 }
77- }
78- ) ;
85+ ) ;
7986
80- body . appendChild ( jsonForms ) ;
81- } ;
87+ body . appendChild ( jsonForms ) ;
88+ } ;
8289
83- export const createExampleSelection = ( ...additionalStoreParams : AdditionalStoreParams [ ] ) : HTMLSelectElement => {
90+ export const createExampleSelection = (
91+ renderers : { tester : RankedTester , renderer : any } [ ] ,
92+ fields : { tester : RankedTester , field : any } [ ] ,
93+ ...additionalStoreParams : AdditionalStoreParams [ ]
94+ ) : HTMLSelectElement => {
8495
85- const examplesDiv = document . getElementById ( exampleDivId ) ;
86- const labelExample = document . createElement ( 'label' ) ;
87- labelExample . textContent = 'Example:' ;
88- labelExample . htmlFor = 'example_select' ;
89- examplesDiv . appendChild ( labelExample ) ;
90- const select = document . createElement ( 'select' ) ;
91- select . id = 'example_select' ;
92- Object . keys ( knownExamples ) . forEach ( key => {
93- const example = knownExamples [ key ] ;
94- const option = document . createElement ( 'option' ) ;
95- option . value = example . name ;
96- option . label = example . label ;
97- option . text = example . label ;
98- select . appendChild ( option ) ;
99- } ) ;
100- select . onchange = ( ) => {
101- changeExample ( select . value , ...additionalStoreParams ) ;
102- } ;
103- examplesDiv . appendChild ( select ) ;
104- changeExample ( select . value , ...additionalStoreParams ) ;
96+ const examplesDiv = document . getElementById ( exampleDivId ) ;
97+ const labelExample = document . createElement ( 'label' ) ;
98+ labelExample . textContent = 'Example:' ;
99+ labelExample . htmlFor = 'example_select' ;
100+ examplesDiv . appendChild ( labelExample ) ;
101+ const select = document . createElement ( 'select' ) ;
102+ select . id = 'example_select' ;
103+ Object . keys ( knownExamples ) . forEach ( key => {
104+ const example = knownExamples [ key ] ;
105+ const option = document . createElement ( 'option' ) ;
106+ option . value = example . name ;
107+ option . label = example . label ;
108+ option . text = example . label ;
109+ select . appendChild ( option ) ;
110+ } ) ;
111+ select . onchange = ( ) => {
112+ changeExample ( select . value , renderers , fields , ...additionalStoreParams ) ;
113+ } ;
114+ examplesDiv . appendChild ( select ) ;
115+ changeExample ( select . value , renderers , fields , ...additionalStoreParams ) ;
105116
106- return select ;
107- } ;
117+ return select ;
118+ } ;
0 commit comments