|
| 1 | +/* |
| 2 | + The MIT License |
| 3 | + |
| 4 | + Copyright (c) 2017-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 | + |
| 26 | +import AJV, { ErrorObject } from 'ajv'; |
| 27 | +import { JsonSchema, UISchemaElement } from '../models'; |
| 28 | +import { generateDefaultUISchema, generateJsonSchema } from '../generators'; |
| 29 | + |
| 30 | +import { RankedTester } from '../testers'; |
| 31 | +import { UISchemaTester, ValidationMode } from '../reducers'; |
| 32 | + |
| 33 | +export const INIT: 'jsonforms/INIT' = 'jsonforms/INIT'; |
| 34 | +export const UPDATE_CORE: 'jsonforms/UPDATE_CORE' = `jsonforms/UPDATE_CORE`; |
| 35 | +export const SET_AJV: 'jsonforms/SET_AJV' = 'jsonforms/SET_AJV'; |
| 36 | +export const UPDATE_DATA: 'jsonforms/UPDATE' = 'jsonforms/UPDATE'; |
| 37 | +export const UPDATE_ERRORS: 'jsonforms/UPDATE_ERRORS' = |
| 38 | + 'jsonforms/UPDATE_ERRORS'; |
| 39 | +export const VALIDATE: 'jsonforms/VALIDATE' = 'jsonforms/VALIDATE'; |
| 40 | +export const ADD_RENDERER: 'jsonforms/ADD_RENDERER' = 'jsonforms/ADD_RENDERER'; |
| 41 | +export const REMOVE_RENDERER: 'jsonforms/REMOVE_RENDERER' = |
| 42 | + 'jsonforms/REMOVE_RENDERER'; |
| 43 | +export const ADD_CELL: 'jsonforms/ADD_CELL' = 'jsonforms/ADD_CELL'; |
| 44 | +export const REMOVE_CELL: 'jsonforms/REMOVE_CELL' = 'jsonforms/REMOVE_CELL'; |
| 45 | +export const SET_CONFIG: 'jsonforms/SET_CONFIG' = 'jsonforms/SET_CONFIG'; |
| 46 | +export const ADD_UI_SCHEMA: 'jsonforms/ADD_UI_SCHEMA' = `jsonforms/ADD_UI_SCHEMA`; |
| 47 | +export const REMOVE_UI_SCHEMA: 'jsonforms/REMOVE_UI_SCHEMA' = `jsonforms/REMOVE_UI_SCHEMA`; |
| 48 | +export const SET_SCHEMA: 'jsonforms/SET_SCHEMA' = `jsonforms/SET_SCHEMA`; |
| 49 | +export const SET_UISCHEMA: 'jsonforms/SET_UISCHEMA' = `jsonforms/SET_UISCHEMA`; |
| 50 | +export const SET_VALIDATION_MODE: 'jsonforms/SET_VALIDATION_MODE' = |
| 51 | + 'jsonforms/SET_VALIDATION_MODE'; |
| 52 | + |
| 53 | +export const SET_LOCALE: 'jsonforms/SET_LOCALE' = `jsonforms/SET_LOCALE`; |
| 54 | +export const SET_LOCALIZED_SCHEMAS: 'jsonforms/SET_LOCALIZED_SCHEMAS' = |
| 55 | + 'jsonforms/SET_LOCALIZED_SCHEMAS'; |
| 56 | +export const SET_LOCALIZED_UISCHEMAS: 'jsonforms/SET_LOCALIZED_UISCHEMAS' = |
| 57 | + 'jsonforms/SET_LOCALIZED_UISCHEMAS'; |
| 58 | + |
| 59 | +export const ADD_DEFAULT_DATA: 'jsonforms/ADD_DEFAULT_DATA' = `jsonforms/ADD_DEFAULT_DATA`; |
| 60 | +export const REMOVE_DEFAULT_DATA: 'jsonforms/REMOVE_DEFAULT_DATA' = `jsonforms/REMOVE_DEFAULT_DATA`; |
| 61 | + |
| 62 | +export type CoreActions = |
| 63 | + | InitAction |
| 64 | + | UpdateCoreAction |
| 65 | + | UpdateAction |
| 66 | + | UpdateErrorsAction |
| 67 | + | SetAjvAction |
| 68 | + | SetSchemaAction |
| 69 | + | SetUISchemaAction |
| 70 | + | SetValidationModeAction; |
| 71 | + |
| 72 | +export interface UpdateAction { |
| 73 | + type: 'jsonforms/UPDATE'; |
| 74 | + path: string; |
| 75 | + updater(existingData?: any): any; |
| 76 | +} |
| 77 | + |
| 78 | +export interface UpdateErrorsAction { |
| 79 | + type: 'jsonforms/UPDATE_ERRORS'; |
| 80 | + errors: ErrorObject[]; |
| 81 | +} |
| 82 | + |
| 83 | +export interface InitAction { |
| 84 | + type: 'jsonforms/INIT'; |
| 85 | + data: any; |
| 86 | + schema: JsonSchema; |
| 87 | + uischema: UISchemaElement; |
| 88 | + options?: InitActionOptions | AJV.Ajv; |
| 89 | +} |
| 90 | + |
| 91 | +export interface UpdateCoreAction { |
| 92 | + type: 'jsonforms/UPDATE_CORE'; |
| 93 | + data?: any; |
| 94 | + schema?: JsonSchema; |
| 95 | + uischema?: UISchemaElement; |
| 96 | + options?: InitActionOptions | AJV.Ajv; |
| 97 | +} |
| 98 | + |
| 99 | +export interface InitActionOptions { |
| 100 | + ajv?: AJV.Ajv; |
| 101 | + validationMode?: ValidationMode; |
| 102 | +} |
| 103 | + |
| 104 | +export interface SetValidationModeAction { |
| 105 | + type: 'jsonforms/SET_VALIDATION_MODE' |
| 106 | + validationMode: ValidationMode |
| 107 | +} |
| 108 | + |
| 109 | +export const init = ( |
| 110 | + data: any, |
| 111 | + schema: JsonSchema = generateJsonSchema(data), |
| 112 | + uischema?: UISchemaElement, |
| 113 | + options?: InitActionOptions | AJV.Ajv |
| 114 | +) => ({ |
| 115 | + type: INIT, |
| 116 | + data, |
| 117 | + schema, |
| 118 | + uischema: |
| 119 | + typeof uischema === 'object' ? uischema : generateDefaultUISchema(schema), |
| 120 | + options |
| 121 | +}); |
| 122 | + |
| 123 | +export const updateCore = ( |
| 124 | + data: any, |
| 125 | + schema: JsonSchema, |
| 126 | + uischema?: UISchemaElement, |
| 127 | + options?: AJV.Ajv | InitActionOptions |
| 128 | +): UpdateCoreAction => ({ |
| 129 | + type: UPDATE_CORE, |
| 130 | + data, |
| 131 | + schema, |
| 132 | + uischema, |
| 133 | + options |
| 134 | +}); |
| 135 | + |
| 136 | +export interface RegisterDefaultDataAction { |
| 137 | + type: 'jsonforms/ADD_DEFAULT_DATA'; |
| 138 | + schemaPath: string; |
| 139 | + data: any; |
| 140 | +} |
| 141 | + |
| 142 | +export const registerDefaultData = (schemaPath: string, data: any) => ({ |
| 143 | + type: ADD_DEFAULT_DATA, |
| 144 | + schemaPath, |
| 145 | + data |
| 146 | +}); |
| 147 | + |
| 148 | +export interface UnregisterDefaultDataAction { |
| 149 | + type: 'jsonforms/REMOVE_DEFAULT_DATA'; |
| 150 | + schemaPath: string; |
| 151 | +} |
| 152 | + |
| 153 | +export const unregisterDefaultData = (schemaPath: string) => ({ |
| 154 | + type: REMOVE_DEFAULT_DATA, |
| 155 | + schemaPath |
| 156 | +}); |
| 157 | + |
| 158 | +export interface SetAjvAction { |
| 159 | + type: 'jsonforms/SET_AJV'; |
| 160 | + ajv: AJV.Ajv; |
| 161 | +} |
| 162 | + |
| 163 | +export const setAjv = (ajv: AJV.Ajv) => ({ |
| 164 | + type: SET_AJV, |
| 165 | + ajv |
| 166 | +}); |
| 167 | + |
| 168 | +export const update = ( |
| 169 | + path: string, |
| 170 | + updater: (existingData: any) => any |
| 171 | +): UpdateAction => ({ |
| 172 | + type: UPDATE_DATA, |
| 173 | + path, |
| 174 | + updater |
| 175 | +}); |
| 176 | + |
| 177 | +export const updateErrors = (errors: ErrorObject[]): UpdateErrorsAction => ({ |
| 178 | + type: UPDATE_ERRORS, |
| 179 | + errors |
| 180 | +}); |
| 181 | + |
| 182 | +export interface AddRendererAction { |
| 183 | + type: 'jsonforms/ADD_RENDERER'; |
| 184 | + tester: RankedTester; |
| 185 | + renderer: any; |
| 186 | +} |
| 187 | + |
| 188 | +export const registerRenderer = (tester: RankedTester, renderer: any) => ({ |
| 189 | + type: ADD_RENDERER, |
| 190 | + tester, |
| 191 | + renderer |
| 192 | +}); |
| 193 | + |
| 194 | +export interface AddCellRendererAction { |
| 195 | + type: 'jsonforms/ADD_CELL'; |
| 196 | + tester: RankedTester; |
| 197 | + cell: any; |
| 198 | +} |
| 199 | + |
| 200 | +export const registerCell = (tester: RankedTester, cell: any) => ({ |
| 201 | + type: ADD_CELL, |
| 202 | + tester, |
| 203 | + cell |
| 204 | +}); |
| 205 | + |
| 206 | +export interface RemoveCellRendererAction { |
| 207 | + type: 'jsonforms/REMOVE_CELL'; |
| 208 | + tester: RankedTester; |
| 209 | + cell: any; |
| 210 | +} |
| 211 | + |
| 212 | +export const unregisterCell = (tester: RankedTester, cell: any) => ({ |
| 213 | + type: REMOVE_CELL, |
| 214 | + tester, |
| 215 | + cell |
| 216 | +}); |
| 217 | + |
| 218 | +export interface RemoveRendererAction { |
| 219 | + type: 'jsonforms/REMOVE_RENDERER'; |
| 220 | + tester: RankedTester; |
| 221 | + renderer: any; |
| 222 | +} |
| 223 | + |
| 224 | +export const unregisterRenderer = (tester: RankedTester, renderer: any) => ({ |
| 225 | + type: REMOVE_RENDERER, |
| 226 | + tester, |
| 227 | + renderer |
| 228 | +}); |
| 229 | + |
| 230 | +export interface SetConfigAction { |
| 231 | + type: 'jsonforms/SET_CONFIG'; |
| 232 | + config: any; |
| 233 | +} |
| 234 | + |
| 235 | +export const setConfig = (config: any): SetConfigAction => ({ |
| 236 | + type: SET_CONFIG, |
| 237 | + config |
| 238 | +}); |
| 239 | + |
| 240 | +export const setValidationMode = (validationMode: ValidationMode): SetValidationModeAction => ({ |
| 241 | + type: SET_VALIDATION_MODE, |
| 242 | + validationMode |
| 243 | +}) |
| 244 | + |
| 245 | +export type UISchemaActions = AddUISchemaAction | RemoveUISchemaAction; |
| 246 | + |
| 247 | +export interface AddUISchemaAction { |
| 248 | + type: 'jsonforms/ADD_UI_SCHEMA'; |
| 249 | + tester: UISchemaTester; |
| 250 | + uischema: UISchemaElement; |
| 251 | +} |
| 252 | + |
| 253 | +export const registerUISchema = ( |
| 254 | + tester: UISchemaTester, |
| 255 | + uischema: UISchemaElement |
| 256 | +): AddUISchemaAction => { |
| 257 | + return { |
| 258 | + type: ADD_UI_SCHEMA, |
| 259 | + tester, |
| 260 | + uischema |
| 261 | + }; |
| 262 | +}; |
| 263 | + |
| 264 | +export interface RemoveUISchemaAction { |
| 265 | + type: 'jsonforms/REMOVE_UI_SCHEMA'; |
| 266 | + tester: UISchemaTester; |
| 267 | +} |
| 268 | + |
| 269 | +export const unregisterUISchema = ( |
| 270 | + tester: UISchemaTester |
| 271 | +): RemoveUISchemaAction => { |
| 272 | + return { |
| 273 | + type: REMOVE_UI_SCHEMA, |
| 274 | + tester |
| 275 | + }; |
| 276 | +}; |
| 277 | + |
| 278 | +export type LocaleActions = |
| 279 | + | SetLocaleAction |
| 280 | + | SetLocalizedSchemasAction |
| 281 | + | SetLocalizedUISchemasAction; |
| 282 | + |
| 283 | +export interface SetLocaleAction { |
| 284 | + type: 'jsonforms/SET_LOCALE'; |
| 285 | + locale: string; |
| 286 | +} |
| 287 | + |
| 288 | +export const setLocale = (locale: string): SetLocaleAction => ({ |
| 289 | + type: SET_LOCALE, |
| 290 | + locale |
| 291 | +}); |
| 292 | + |
| 293 | +export interface SetLocalizedSchemasAction { |
| 294 | + type: 'jsonforms/SET_LOCALIZED_SCHEMAS'; |
| 295 | + localizedSchemas: Map<string, JsonSchema>; |
| 296 | +} |
| 297 | + |
| 298 | +export const setLocalizedSchemas = ( |
| 299 | + localizedSchemas: Map<string, JsonSchema> |
| 300 | +): SetLocalizedSchemasAction => ({ |
| 301 | + type: SET_LOCALIZED_SCHEMAS, |
| 302 | + localizedSchemas |
| 303 | +}); |
| 304 | + |
| 305 | +export interface SetSchemaAction { |
| 306 | + type: 'jsonforms/SET_SCHEMA'; |
| 307 | + schema: JsonSchema; |
| 308 | +} |
| 309 | + |
| 310 | +export const setSchema = (schema: JsonSchema): SetSchemaAction => ({ |
| 311 | + type: SET_SCHEMA, |
| 312 | + schema |
| 313 | +}); |
| 314 | + |
| 315 | +export interface SetLocalizedUISchemasAction { |
| 316 | + type: 'jsonforms/SET_LOCALIZED_UISCHEMAS'; |
| 317 | + localizedUISchemas: Map<string, UISchemaElement>; |
| 318 | +} |
| 319 | + |
| 320 | +export const setLocalizedUISchemas = ( |
| 321 | + localizedUISchemas: Map<string, UISchemaElement> |
| 322 | +): SetLocalizedUISchemasAction => ({ |
| 323 | + type: SET_LOCALIZED_UISCHEMAS, |
| 324 | + localizedUISchemas |
| 325 | +}); |
| 326 | + |
| 327 | +export interface SetUISchemaAction { |
| 328 | + type: 'jsonforms/SET_UISCHEMA'; |
| 329 | + uischema: UISchemaElement; |
| 330 | +} |
| 331 | + |
| 332 | +export const setUISchema = (uischema: UISchemaElement): SetUISchemaAction => ({ |
| 333 | + type: SET_UISCHEMA, |
| 334 | + uischema |
| 335 | +}); |
0 commit comments