-
Notifications
You must be signed in to change notification settings - Fork 6
/
Model.js
474 lines (408 loc) · 15.1 KB
/
Model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*!
* Copyright 2017 - Swiss Data Science Center (SDSC)
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
* Eidgenössische Technische Hochschule Zürich (ETHZ).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* renku-ui
*
* Model.js
*
* A schema is comprised of property definitions which are of the form:
* {[property name]: {initial: [initial value], mandatory: [true/false], ...}
*
*
* A model is an instance of the StateModel class, created from a schema definition.
* Instances of StateModel have bindings to the corresponding react state / redux store
* to facilitate state access, immutable updates, etc and ensure a structure of the state
* in accordance with the schema definition.
*
* Examples for how to use the Schema / StateModel classes can be found in ./Model.example.js
*/
// TODO: Maybe use [jsdoc](http://usejsdoc.org/) here?
import immutableUpdate from 'immutability-helper';
import { createStore } from "../utils/EnhancedState";
// import { Component } from 'react';
// // Todo: Resolve dependency from our custom store
// import { createStore } from '../utils/EnhancedState';
const PropertyName = {
SCHEMA: 'schema',
INITIAL: 'initial',
MANDATORY: 'mandatory'
};
// Named consts for the bindings to the store.
const StateKind = {
REDUX: 'redux_store',
REACT: 'react_State'
};
// We currently need only one action type. The information about which
// part of the state has to be modified is contained in the action payload.
const ActionType = {
UPDATE: 'update'
};
// Fields which are updating are set to this value.
const SpecialPropVal = {
UPDATING: 'is_updating'
};
const StatusHelper = {
isUpdating: (value) => {
return value === SpecialPropVal.UPDATING ? true : false;
}
}
class FieldSpec {
constructor(spec) {
Object.keys(spec).forEach((prop) => {
// We ignore properties which are not part of the known field specification properties.
if (Object.values(PropertyName).indexOf(prop) < 0) return;
// Handle arrays in Field spec definitions
if (prop === PropertyName.SCHEMA && spec[prop] instanceof Array) {
if (spec[prop][0] && !(spec[prop] instanceof Schema)) {
this[prop] = [new Schema(spec[prop][0])]
}
else {
this[prop] = []
}
}
// Sub-objects in field spec definitions are turned into schema definitions.
else if (prop === PropertyName.SCHEMA && !(spec[prop] instanceof Schema)) {
this[prop] = new Schema(spec[prop]);
}
else {
this[prop] = spec[prop];
}
});
}
}
class Schema {
constructor(obj) {
Object.keys(obj).forEach((prop) => {
this[prop] = new FieldSpec(obj[prop])
});
}
createEmpty(obj) { return createEmpty(this, obj) }
applyDefaults(obj) { return applyDefaults(this, obj)}
createInitialized() {
const emptyObject = this.createEmpty();
return this.applyDefaults(emptyObject);
}
validate(obj) { return validate(this, obj)}
reducer() {
return (state=this.createEmpty(), action) => modelUpdateReducer(state, action);
}
}
class ReduxStateModel {
constructor(owner, stateBinding, stateHolder, initialState) {
this.stateBinding = stateBinding; // We know stateBinding === StateKind.REDUX
this.reduxStore = stateHolder;
// Initialize state
const updateObj = updateObjectFromObject(initialState, this.reduxStore.getState());
this.immutableUpdate(updateObj, null);
}
getStateObject() { return this.reduxStore.getState(); }
immutableUpdate(updateObj, callback) {
this.reduxStore.dispatch({
type: ActionType.UPDATE,
payload: updateObj,
});
// We provide this just to keep the interface for the react state and the redux case similar.
if (callback) {
console.error('Unnecessary callback: The update of the REDUX store is synchronous.');
callback.call();
}
}
}
class ReactStateModel {
constructor(owner, stateBinding, stateHolder, initialState) {
this.owner = owner
this.stateBinding = stateBinding; // We know stateBinding === StateKind.REACT
this.reactComponent = stateHolder;
this.reactComponent.state = initialState;
}
getStateObject() {return this.reactComponent.state; }
immutableUpdate(updateObj, callback) {
this.reactComponent.setState((prevState) => immutableUpdate(prevState, updateObj), callback);
}
}
class StateModel {
constructor(schema, stateBinding, stateHolder, initialState) {
this.stateBinding = stateBinding;
this.schema = schema;
this._updatingPropVal = SpecialPropVal.UPDATING;
const initializedState = initialState ? initialState : schema.createInitialized();
if (stateBinding === StateKind.REDUX) {
if (!stateHolder) stateHolder = createStore(schema.reducer(), this.constructor.name)
this._stateModel = new ReduxStateModel(this, stateBinding, stateHolder, initializedState)
this.reduxStore = this._stateModel.reduxStore;
} else if (stateBinding === StateKind.REACT) {
this._stateModel = new ReactStateModel(this, stateBinding, stateHolder, initializedState)
this.reactComponent = this._stateModel.reactComponent;
} else {
throw new Error(`State binding ${stateBinding} not implemented`)
}
}
get(propertyAccessorString) {
const stateObject = this._stateModel.getStateObject();
if (!propertyAccessorString) {
return stateObject;
}
else {
return nestedPropertyAccess(propertyAccessorString, stateObject);
}
}
set(propertyAccessorString, value, callback) {
const updateObj = updateObjectFromString(propertyAccessorString, value);
this.immutableUpdate(updateObj, callback);
}
setUpdating(options){
const updateObj = updateObjectFromOptions(options);
this.immutableUpdate(updateObj);
}
setObject(obj, callback) {
const updateObj = updateObjectFromObject(obj, this.get());
this.immutableUpdate(updateObj, callback);
}
immutableUpdate(updateObj, callback) {
// TODO: Reconsider validation, for what and when it should be used.
// const validation = this.schema.validate(immutableUpdate(this.get(), updateObj));
// if (!validation.result) {
// let errorString = 'Skipping update to prevent invalid state:';
// validation.errors.forEach((error) => {
// errorString = errorString.concat(JSON.stringify(error));
// });
// throw(errorString);
// }
this._stateModel.immutableUpdate(updateObj, callback);
}
validate() {
return this.schema.validate(this.get())
}
mapStateToProps = _mapStateToProps.bind(this);
subModel = (path) => new SubModel(this, path);
}
// Note that the SubModel can not be implemented as a child class as an instance of a SubModel
// can not be created without an already existing instance of a Model.
class SubModel {
constructor(model, path) {
this._updatingPropVal = model._updatingPropVal;
this.reduxStore = model.reduxStore;
this.reactComponent = model.reactComponent;
this.stateBinding = model.stateBinding;
this.baseModel = model;
this.baseModelPath = path;
}
get(propAccessorString) {
const fullPropAccessorString = this.baseModelPath + (propAccessorString ? '.' + propAccessorString : '');
return this.baseModel.get(fullPropAccessorString);
}
set(propAccessorString, value) {
this.baseModel.set(this.baseModelPath + '.' + propAccessorString, value);
}
setObject(obj) {
const fullObj = {};
let leafObj = fullObj;
this.baseModelPath.split('.').forEach((prop) => {
leafObj[prop] = {};
leafObj = leafObj[prop];
});
Object.keys(obj).forEach((prop) => leafObj[prop] = obj[prop]);
this.baseModel.set(fullObj);
}
setUpdating(options) {
const fullOptions = {};
let leafOptions = fullOptions;
this.baseModelPath.split('.').forEach((prop) => {
leafOptions[prop] = {};
leafOptions = leafOptions[prop];
});
Object.keys(options).forEach((prop) => leafOptions[prop] = options[prop]);
this.baseModel.setUpdating(fullOptions);
}
isUpdating(propAccessorString) {
const fullPropAccessorString = this.baseModelPath + (propAccessorString ? '.' + propAccessorString : '');
return StatusHelper.isUpdating(this.baseModel.get(fullPropAccessorString));
}
mapStateToProps = _mapStateToProps.bind(this);
subModel = (path) => new SubModel(this.baseModel, this.baseModelPath + '.' + path);
}
// The following functions are not exported and probably never called directly, we use
// them to define Schema / StateModel object methods.
// Create an empty object according to the schema
// where all values are undefined
function createEmpty(schema, newObj={}) {
Object.keys(schema).forEach((prop) => {
if (schema[prop].hasOwnProperty(PropertyName.SCHEMA) && schema[prop][PropertyName.SCHEMA] instanceof Array) {
newObj[prop] = []
}
else if (schema[prop].hasOwnProperty(PropertyName.SCHEMA)) {
newObj[prop] = createEmpty(schema[prop][PropertyName.SCHEMA])
}
else {
newObj[prop] = undefined
}
});
return newObj;
}
// Apply the defaults defined in a schema to a generic object. We don't overwrite
// already existing values, defaults are only applied to undefined values.
function applyDefaults(schema, obj) {
Object.keys(schema).forEach((prop) => {
const initialValue = schema[prop][PropertyName.INITIAL];
if (initialValue !== undefined) {
if (initialValue instanceof Function) {
obj[prop] = initialValue()
}
else {
// TODO: Add proper check here to make sure only JSON-serializable initial
// TODO: values are accepted
obj[prop] = JSON.parse(JSON.stringify(initialValue));
}
}
// If the sub-schema is an array, we leave it empty, otherwise we apply the defaults to the sub-objects.
else if (schema[prop].hasOwnProperty(PropertyName.SCHEMA)
&& !(schema[prop][PropertyName.SCHEMA] instanceof Array)) {
schema[prop][PropertyName.SCHEMA].applyDefaults(obj[prop])
}
});
return obj;
}
// Validate a generic object against a schema.
function validate(schema, obj) {
if (!(obj instanceof Object)) {
throw new Error('Only objects should be passed to this routine')
}
let errors = [];
Object.keys(schema).forEach((prop) => {
let subErrors = [];
// schema[prop] conatains another schema but the corresponding obj property is NOT an object itself.
if (schema[prop].hasOwnProperty(PropertyName.SCHEMA) && !(obj[prop] instanceof Object)) {
subErrors = validateField(prop, schema[prop], obj[prop]);
}
// schema[prop] conatains another schema which is not an array
else if (schema[prop].hasOwnProperty(PropertyName.SCHEMA) &&
(schema[prop][PropertyName.SCHEMA] instanceof Schema)) {
subErrors = schema[prop][PropertyName.SCHEMA].validate(obj[prop]).errors;
}
// schema[prop] contains another schema which is an array
else if (
schema[prop].hasOwnProperty(PropertyName.SCHEMA)
&& (schema[prop][PropertyName.SCHEMA] instanceof Array)
&& (schema[prop][PropertyName.SCHEMA].length > 0)
) {
subErrors = obj[prop]
.map((el, i) => {
if (el instanceof Object) {
return schema[prop][PropertyName.SCHEMA][0].validate(el).errors
}
else {
return [{[prop]: `${prop}[${i}] must be an object`}];
}
})
.reduce((arr1, arr2) => arr1.concat(arr2));
}
// schema[prop] contains no schema
else {
subErrors = validateField(prop, schema[prop], obj[prop]);
}
errors = errors.concat(subErrors);
});
const result = errors.length === 0;
return {result, errors};
}
// Validate an individual field.
// TODO: Validation of mandatory sub-fields of non-mandatory fields seems to give unexpected results.
function validateField(fieldName, fieldSpec, fieldValue){
const errors = [];
if (fieldSpec[PropertyName.SCHEMA] instanceof Array && !(fieldValue instanceof Array)) {
errors.push({[fieldName]: `${fieldName} must be an array`})
}
else if (fieldSpec[PropertyName.SCHEMA] instanceof Object && !(fieldValue instanceof Object)) {
errors.push({[fieldName]: `${fieldName} must be an object`})
}
else if (fieldSpec[PropertyName.MANDATORY] && isEmpty(fieldValue)) {
errors.push({[fieldName]: `${fieldName} must be provided and non-empty`});
}
return errors;
}
// Check if a single value is "non-empty" (depending on type).
function isEmpty(value) {
if (value === undefined || value === null) return true;
if (value instanceof String || typeof value === 'string') return value === '';
if (value instanceof Number || typeof value === 'number') return false;
return false;
}
// Create a mongodb-style update object from a dot-separated property
// accessor string and the desired value.
function updateObjectFromString(propAccessorString, value) {
const updateObj = {};
let leafObj = updateObj;
propAccessorString.split('.').forEach((prop) => {
leafObj[prop] = {};
leafObj = leafObj[prop];
});
leafObj.$set = value;
return updateObj
}
// Create a mongodb-style update object from a plain
// object containing the desired (potentially nested) values.
function updateObjectFromObject(obj, currentObject){
let updateObj = {};
Object.keys(obj).forEach((prop) => {
if (obj[prop] instanceof Object && currentObject[prop]) {
updateObj[prop] = updateObjectFromObject(obj[prop], currentObject[prop])
}
else {
updateObj[prop] = {$set: obj[prop]}
}
});
return updateObj
}
// Create a mongodb-style update object from a plain
// object containing the information about which fields are updating.
function updateObjectFromOptions(options){
let updateObj = {};
Object.keys(options).forEach((prop) => {
if (options[prop] instanceof Object) {
updateObj[prop] = updateObjectFromOptions(options[prop])
}
else {
if (options[prop] === true) {
updateObj[prop] = {$set: SpecialPropVal.UPDATING}
}
}
});
return updateObj
}
// Translate nestedPropertyAccess('some.string.with.dots', obj)
// into obj[some][string][with][dots]
function nestedPropertyAccess(propAccessorString, obj) {
let leaf = obj;
propAccessorString.split('.').forEach((prop) => {
leaf = leaf[prop];
});
return leaf;
}
// A redux reducer that will handle immutability-helper
// updates.
function modelUpdateReducer(state, action) {
if (action.type === ActionType.UPDATE) {
return immutableUpdate(state, action.payload);
}
return state
}
// A default mapStateToProps. Note that this needs to be bound.
function _mapStateToProps(state, ownProps) {
return {...this.get(), ...ownProps};
}
export { Schema, StateModel, StateKind , SubModel, SpecialPropVal, StatusHelper }