Skip to content

Commit 916dba8

Browse files
committed
[WIP] Initial commit
It builds, but that is about it. Totally untested. Just wanted to share.
1 parent 63c8783 commit 916dba8

File tree

12 files changed

+575
-2
lines changed

12 files changed

+575
-2
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"es2015"
4+
]
5+
}

.jshintrc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"undef": true,
3+
"unused": true,
4+
"browser": true,
5+
"esnext": true,
6+
"globals": {
7+
"console":false,
8+
"assertEquals": false,
9+
"jstestdriver": false,
10+
"assertTrue": false,
11+
"assertFalse": false,
12+
"describe": false,
13+
"it":false,
14+
"expect": false,
15+
"sinon":false,
16+
"beforeEach": false,
17+
"afterEach": false,
18+
"module": false,
19+
"inject": false,
20+
"chai": false,
21+
"should": false,
22+
"Jed": false,
23+
"tws": false
24+
}
25+
}

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
# json-schema-form
2-
Core library
1+
# json-schema-form-core
2+
Core library that doesn't depend on a framework.
3+
4+
5+
Work-In-Progress!

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
export * from './lib/sfPath.js';
3+
export * from './lib/select.js';

lib/canonicalTitleMap.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Takes a titleMap in either object or list format and returns one in
2+
// in the list format.
3+
export default function(titleMap, originalEnum) {
4+
if (!Array.isArray(titleMap)) {
5+
const canonical = [];
6+
if (originalEnum) {
7+
originalEnum.forEach((value) => {
8+
canonical.push({name: titleMap[value], value});
9+
});
10+
} else {
11+
Object.keys(titleMap).forEach((value) => {
12+
canonical.push({name: titleMap[value], value});
13+
});
14+
}
15+
return canonical;
16+
}
17+
return titleMap;
18+
}

lib/merge.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {stringify, parse} from './sfPath';
2+
import canonicalTitleMap from './canonicalTitleMap';
3+
4+
5+
//export function merge(schema, form, schemaDefaultTypes, ignore, options, readonly, asyncTemplates) {
6+
export function merge(lookup, form, options, readonly, asyncTemplates) {
7+
form = form || [];
8+
options = options || {};
9+
10+
//ok let's merge!
11+
//We look at the supplied form and extend it with schema standards
12+
return form.map((obj) => {
13+
14+
//handle the shortcut with just a name
15+
if (typeof obj === 'string') {
16+
obj = {key: obj};
17+
}
18+
19+
if (obj.key) {
20+
if (typeof obj.key === 'string') {
21+
obj.key = parse(obj.key);
22+
}
23+
}
24+
25+
//If it has a titleMap make sure it's a list
26+
if (obj.titleMap) {
27+
obj.titleMap = canonicalTitleMap(obj.titleMap);
28+
}
29+
30+
//extend with std form from schema.
31+
if (obj.key) {
32+
const strid = stringify(obj.key);
33+
if (lookup[strid]) {
34+
const schemaDefaults = lookup[strid];
35+
if (schemaDefaults) {
36+
Object.keys(schemaDefaults).forEach((attr) => {
37+
if (obj[attr] === undefined) {
38+
obj[attr] = schemaDefaults[attr];
39+
}
40+
});
41+
}
42+
}
43+
}
44+
45+
// Are we inheriting readonly?
46+
if (readonly === true) { // Inheriting false is not cool.
47+
obj.readonly = true;
48+
}
49+
50+
//if it's a type with items, merge 'em!
51+
if (obj.items) {
52+
obj.items = merge(lookup, obj.items, options, obj.readonly, asyncTemplates);
53+
}
54+
55+
//if its has tabs, merge them also!
56+
if (obj.tabs) {
57+
obj.tabs.forEach((tab) => {
58+
if (tab.items) {
59+
tab.items = merge(lookup, tab.items, options, obj.readonly, asyncTemplates);
60+
}
61+
});
62+
}
63+
64+
// Special case: checkbox
65+
// Since have to ternary state we need a default
66+
if (obj.type === 'checkbox' && obj.schema['default'] === undefined) {
67+
obj.schema['default'] = false;
68+
}
69+
70+
// Special case: template type with tempplateUrl that's needs to be loaded before rendering
71+
// TODO: this is not a clean solution. Maybe something cleaner can be made when $ref support
72+
// is introduced since we need to go async then anyway
73+
if (asyncTemplates && obj.type === 'template' && !obj.template && obj.templateUrl) {
74+
asyncTemplates.push(obj);
75+
}
76+
77+
return obj;
78+
});
79+
}

0 commit comments

Comments
 (0)