-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathapplyTransforms.js
99 lines (90 loc) · 3.14 KB
/
applyTransforms.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
import Injector from 'lib/Injector';
import Validator from 'lib/Validator';
import classnames from 'classnames';
import { findField } from 'lib/schemaFieldValues';
import fieldHolder from 'components/FieldHolder/FieldHolder';
const togglePristineState = (field, isPristine = false) => {
// set pristine and dirty classes if they're defined
const classes = (field.extraClass)
? field.extraClass.split(' ').reduce((prev, className) => ({
...prev,
[className]: true,
}), {})
: {};
if (typeof field.data.pristineClass === 'string') {
classes[field.data.pristineClass] = isPristine;
}
if (typeof field.data.dirtyClass === 'string') {
classes[field.data.dirtyClass] = !isPristine;
}
// custom titles and icons to replace the default
const customTitle = isPristine ? field.data.pristineTitle : field.data.dirtyTitle;
const customIcon = isPristine ? field.data.pristineIcon : field.data.dirtyIcon;
return {
...field,
title: customTitle || field.title,
icon: customIcon || field.icon,
extraClass: classnames(classes),
};
};
const applyTransforms = () => {
Injector.transform(
'field-holders',
(updater) => {
const fields = [
'FieldGroup',
];
fields.forEach((field) => updater.component('FieldGroup', fieldHolder, `${field}Holder`));
}
);
Injector.transform(
'form-action-changed',
(updater) => {
updater.form.alterSchema('*', (form) => {
form.mutateField('action_save', (field) => {
const isPristine = form.isPristine();
return togglePristineState(field, isPristine);
});
form.mutateField('action_publish', (field) => {
const isPristine = field.data.isPublished && !field.data.isModified && form.isPristine();
return togglePristineState(field, isPristine);
});
return form.getState();
});
});
Injector.transform(
'schema-validation',
(updater) => {
updater.form.addValidation(
'*',
(values, Validation, schema) => {
// Hardcoded exclusion for elemental inline forms
// This is done so that client-side validation doesn't run so that all validation is done
// on the server. This is done so that invalid, closed elements will popup open and
// also trigger a toast notification.
// Note that this has no effect on non-inline forms as they do not use form schema
if (schema.name.indexOf('ElementForm_') === 0) {
return Validation.getState();
}
const validator = new Validator(values);
const errorMap = Object.keys(values).reduce((curr, key) => {
const field = findField(schema.fields, key);
if (!field) {
return curr;
}
const { valid, errors } = validator.validateFieldSchema(field);
if (valid) {
return curr;
}
return {
...curr,
[key]: errors
};
}, {});
Validation.addErrors(errorMap);
return Validation.getState();
}
);
});
};
export default applyTransforms;