|
| 1 | +/* |
| 2 | + The MIT License |
| 3 | +
|
| 4 | + Copyright (c) 2019 EclipseSource Munich |
| 5 | + https://github.com/eclipsesource/jsonforms |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in |
| 15 | + all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + THE SOFTWARE. |
| 24 | +*/ |
| 25 | +import { |
| 26 | + Actions, |
| 27 | + composeWithUi, |
| 28 | + computeLabel, |
| 29 | + ControlElement, |
| 30 | + isPlainLabel, |
| 31 | + JsonFormsState, |
| 32 | + JsonSchema, |
| 33 | + OwnPropsOfControl, |
| 34 | + StatePropsOfControl |
| 35 | +} from '@jsonforms/core'; |
| 36 | +import { Input, OnDestroy, OnInit } from '@angular/core'; |
| 37 | +import { NgRedux } from '@angular-redux/store'; |
| 38 | +import { |
| 39 | + AbstractControl, |
| 40 | + FormControl, |
| 41 | + ValidationErrors, |
| 42 | + ValidatorFn |
| 43 | +} from '@angular/forms'; |
| 44 | +import { Subscription } from 'rxjs'; |
| 45 | + |
| 46 | +import { JsonFormsBaseRenderer } from './base.renderer'; |
| 47 | + |
| 48 | +export abstract class JsonFormsAbstractControl< |
| 49 | + Props extends StatePropsOfControl |
| 50 | +> extends JsonFormsBaseRenderer<ControlElement> implements OnInit, OnDestroy { |
| 51 | + @Input() id: string; |
| 52 | + @Input() disabled: boolean; |
| 53 | + @Input() visible: boolean; |
| 54 | + |
| 55 | + form: FormControl; |
| 56 | + subscription: Subscription; |
| 57 | + data: any; |
| 58 | + label: string; |
| 59 | + description: string; |
| 60 | + error: string | null; |
| 61 | + scopedSchema: JsonSchema; |
| 62 | + rootSchema: JsonSchema; |
| 63 | + enabled: boolean; |
| 64 | + hidden: boolean; |
| 65 | + |
| 66 | + constructor(protected ngRedux: NgRedux<JsonFormsState>) { |
| 67 | + super(); |
| 68 | + this.form = new FormControl( |
| 69 | + { |
| 70 | + value: '', |
| 71 | + disabled: true |
| 72 | + }, |
| 73 | + { |
| 74 | + updateOn: 'change', |
| 75 | + validators: this.validator.bind(this) |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + getEventValue = (event: any) => event.value; |
| 81 | + |
| 82 | + onChange(ev: any) { |
| 83 | + const path = composeWithUi(this.uischema, this.path); |
| 84 | + this.ngRedux.dispatch(Actions.update(path, () => this.getEventValue(ev))); |
| 85 | + this.triggerValidation(); |
| 86 | + } |
| 87 | + |
| 88 | + ngOnInit() { |
| 89 | + this.subscription = this.ngRedux |
| 90 | + .select() |
| 91 | + .subscribe((state: JsonFormsState) => { |
| 92 | + const props = this.mapToProps(state); |
| 93 | + const { |
| 94 | + data, |
| 95 | + enabled, |
| 96 | + errors, |
| 97 | + label, |
| 98 | + required, |
| 99 | + schema, |
| 100 | + rootSchema, |
| 101 | + visible |
| 102 | + } = props; |
| 103 | + this.label = computeLabel( |
| 104 | + isPlainLabel(label) ? label : label.default, |
| 105 | + required |
| 106 | + ); |
| 107 | + this.data = data; |
| 108 | + this.error = errors ? errors.join('\n') : null; |
| 109 | + this.enabled = enabled; |
| 110 | + this.enabled ? this.form.enable() : this.form.disable(); |
| 111 | + this.hidden = !visible; |
| 112 | + this.scopedSchema = schema; |
| 113 | + this.rootSchema = rootSchema; |
| 114 | + this.description = |
| 115 | + this.scopedSchema !== undefined ? this.scopedSchema.description : ''; |
| 116 | + this.id = props.id; |
| 117 | + this.form.setValue(data); |
| 118 | + this.mapAdditionalProps(props); |
| 119 | + }); |
| 120 | + this.triggerValidation(); |
| 121 | + } |
| 122 | + |
| 123 | + validator: ValidatorFn = (_c: AbstractControl): ValidationErrors | null => { |
| 124 | + return this.error ? { error: this.error } : null; |
| 125 | + }; |
| 126 | + |
| 127 | + // @ts-ignore |
| 128 | + mapAdditionalProps(props: Props) { |
| 129 | + // do nothing by default |
| 130 | + } |
| 131 | + |
| 132 | + ngOnDestroy() { |
| 133 | + if (this.subscription) { |
| 134 | + this.subscription.unsubscribe(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + protected getOwnProps(): OwnPropsOfControl { |
| 139 | + const props: OwnPropsOfControl = { |
| 140 | + uischema: this.uischema, |
| 141 | + schema: this.schema, |
| 142 | + path: this.path, |
| 143 | + id: this.id |
| 144 | + }; |
| 145 | + if (this.disabled !== undefined) { |
| 146 | + props.enabled = !this.disabled; |
| 147 | + } |
| 148 | + if (this.visible !== undefined) { |
| 149 | + props.visible = this.visible; |
| 150 | + } |
| 151 | + return props; |
| 152 | + } |
| 153 | + |
| 154 | + protected abstract mapToProps(state: JsonFormsState): Props; |
| 155 | + |
| 156 | + protected triggerValidation() { |
| 157 | + // these cause the correct update of the error underline, seems to be |
| 158 | + // related to ionic-team/ionic#11640 |
| 159 | + this.form.markAsTouched(); |
| 160 | + this.form.updateValueAndValidity(); |
| 161 | + } |
| 162 | +} |
0 commit comments