Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit 7d3ab4f

Browse files
author
Morten Christensen
committed
Added utils/constants + renaming (breaking change)
1 parent d72a51c commit 7d3ab4f

File tree

11 files changed

+183
-74
lines changed

11 files changed

+183
-74
lines changed

.vscode/launch.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
12+
"stopOnEntry": false,
13+
"args": ["--no-timeouts", "--colors", "dist/test/index.js"],
14+
"cwd": "${workspaceRoot}",
15+
"runtimeExecutable": null,
16+
"env": {
17+
"NODE_ENV": "testing"
18+
}
19+
},
20+
{
21+
"type": "node",
22+
"request": "attach",
23+
"name": "Attach to Process",
24+
"port": 5858
25+
}
26+
]
27+
}

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# json-schema-js-gui-model
22
Handy framework-agnostic gui model and associated translator that can be used as a basis when constructing
33
dynamic javascript UI forms (in any web framework) from json-schemas. For details, refer to the declared gui
4-
model [here](src/lib/gui-model.ts) and the translator [here](src/lib/gui-model.mapper.ts).
4+
model [here](src/lib/gui-model.ts) and the translator declared at the bottom of this [file](src/lib/gui-model.mapper.ts).
55

6-
Clients of this library are themselves responsible for constructing a GUI using
7-
the gui model provided by this library. Such UI code will be different depending
8-
on the exact web framework used and this out of scope of this more
6+
Clients of this library are themselves responsible for constructing a UI form
7+
dynamically using the gui model provided by this library. Such UI code will be
8+
different depending on the exact web framework used and this out of scope of this more
99
fundamental and general project.
1010

1111
This library is on purpose keept small with no runtime-dependencies. It can be used from both nodejs v6+
@@ -118,7 +118,7 @@ in realtime at each keypress if necessary.
118118
"kind": "group",
119119
"name": "",
120120
"controlType": "group",
121-
"settingsObjectPath": "",
121+
"dataObjectPath": "",
122122
"label": "",
123123
"tooltip": "",
124124
"isRoot": true,
@@ -128,7 +128,7 @@ in realtime at each keypress if necessary.
128128
"kind": "group",
129129
"name": "authentication",
130130
"controlType": "group",
131-
"settingsObjectPath": "authentication",
131+
"dataObjectPath": "authentication",
132132
"label": "Authentication",
133133
"tooltip": "an authentication description here",
134134
"isRoot": false,
@@ -140,7 +140,7 @@ in realtime at each keypress if necessary.
140140
"controlType": "input",
141141
"label": "User",
142142
"tooltip": "a username",
143-
"settingsObjectPath": "authentication.user",
143+
"dataObjectPath": "authentication.user",
144144
"defaultValue": "",
145145
"required": true,
146146
"type": "string",
@@ -152,7 +152,7 @@ in realtime at each keypress if necessary.
152152
"controlType": "input",
153153
"label": "Password",
154154
"tooltip": "a password",
155-
"settingsObjectPath": "authentication.password",
155+
"dataObjectPath": "authentication.password",
156156
"defaultValue": "",
157157
"required": true,
158158
"type": "string",
@@ -164,7 +164,7 @@ in realtime at each keypress if necessary.
164164
"controlType": "input",
165165
"label": "scheme",
166166
"tooltip": "",
167-
"settingsObjectPath": "authentication.scheme",
167+
"dataObjectPath": "authentication.scheme",
168168
"defaultValue": "basic",
169169
"required": false,
170170
"type": "string",
@@ -176,7 +176,7 @@ in realtime at each keypress if necessary.
176176
"controlType": "yesno",
177177
"label": "preemptive",
178178
"tooltip": "",
179-
"settingsObjectPath": "authentication.preemptive",
179+
"dataObjectPath": "authentication.preemptive",
180180
"defaultValue": true,
181181
"required": false,
182182
"type": "boolean",
@@ -188,7 +188,7 @@ in realtime at each keypress if necessary.
188188
"kind": "group",
189189
"name": "server",
190190
"controlType": "group",
191-
"settingsObjectPath": "server",
191+
"dataObjectPath": "server",
192192
"label": "Server",
193193
"tooltip": "",
194194
"isRoot": false,
@@ -200,7 +200,7 @@ in realtime at each keypress if necessary.
200200
"controlType": "input",
201201
"label": "host",
202202
"tooltip": "",
203-
"settingsObjectPath": "server.host",
203+
"dataObjectPath": "server.host",
204204
"defaultValue": "",
205205
"required": false,
206206
"type": "string",
@@ -212,7 +212,7 @@ in realtime at each keypress if necessary.
212212
"controlType": "input",
213213
"label": "port",
214214
"tooltip": "",
215-
"settingsObjectPath": "server.port",
215+
"dataObjectPath": "server.port",
216216
"defaultValue": 80,
217217
"required": false,
218218
"type": "integer",
@@ -224,7 +224,7 @@ in realtime at each keypress if necessary.
224224
"controlType": "dropdown",
225225
"label": "protocol",
226226
"tooltip": "",
227-
"settingsObjectPath": "server.protocol",
227+
"dataObjectPath": "server.protocol",
228228
"defaultValue": "http",
229229
"values": [
230230
"http",

docs/example.gui.model.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"kind": "group",
33
"name": "",
44
"controlType": "group",
5-
"settingsObjectPath": "",
5+
"dataObjectPath": "",
66
"label": "",
77
"tooltip": "",
88
"isRoot": true,
@@ -12,7 +12,7 @@
1212
"kind": "group",
1313
"name": "authentication",
1414
"controlType": "group",
15-
"settingsObjectPath": "authentication",
15+
"dataObjectPath": "authentication",
1616
"label": "Authentication",
1717
"tooltip": "an authentication description here",
1818
"isRoot": false,
@@ -24,7 +24,7 @@
2424
"controlType": "input",
2525
"label": "User",
2626
"tooltip": "a username",
27-
"settingsObjectPath": "authentication.user",
27+
"dataObjectPath": "authentication.user",
2828
"defaultValue": "",
2929
"required": true,
3030
"type": "string",
@@ -36,7 +36,7 @@
3636
"controlType": "input",
3737
"label": "Password",
3838
"tooltip": "a password",
39-
"settingsObjectPath": "authentication.password",
39+
"dataObjectPath": "authentication.password",
4040
"defaultValue": "",
4141
"required": true,
4242
"type": "string",
@@ -48,7 +48,7 @@
4848
"controlType": "input",
4949
"label": "scheme",
5050
"tooltip": "",
51-
"settingsObjectPath": "authentication.scheme",
51+
"dataObjectPath": "authentication.scheme",
5252
"defaultValue": "basic",
5353
"required": false,
5454
"type": "string",
@@ -60,7 +60,7 @@
6060
"controlType": "yesno",
6161
"label": "preemptive",
6262
"tooltip": "",
63-
"settingsObjectPath": "authentication.preemptive",
63+
"dataObjectPath": "authentication.preemptive",
6464
"defaultValue": true,
6565
"required": false,
6666
"type": "boolean",
@@ -72,7 +72,7 @@
7272
"kind": "group",
7373
"name": "server",
7474
"controlType": "group",
75-
"settingsObjectPath": "server",
75+
"dataObjectPath": "server",
7676
"label": "Server",
7777
"tooltip": "",
7878
"isRoot": false,
@@ -84,7 +84,7 @@
8484
"controlType": "input",
8585
"label": "host",
8686
"tooltip": "",
87-
"settingsObjectPath": "server.host",
87+
"dataObjectPath": "server.host",
8888
"defaultValue": "",
8989
"required": false,
9090
"type": "string",
@@ -96,7 +96,7 @@
9696
"controlType": "input",
9797
"label": "port",
9898
"tooltip": "",
99-
"settingsObjectPath": "server.port",
99+
"dataObjectPath": "server.port",
100100
"defaultValue": 80,
101101
"required": false,
102102
"type": "integer",
@@ -108,7 +108,7 @@
108108
"controlType": "dropdown",
109109
"label": "protocol",
110110
"tooltip": "",
111-
"settingsObjectPath": "server.protocol",
111+
"dataObjectPath": "server.protocol",
112112
"defaultValue": "http",
113113
"values": [
114114
"http",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-js-gui-model",
3-
"version": "0.1.4",
3+
"version": "1.0.0",
44
"description": "Handy gui model and associated translator that is useful when constructing javascript UI forms from json-schemas",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export * from './lib/gui-model';
2+
export * from './lib/constants';
3+
export * from './lib/util';
24
export { JsonSchema } from './dependencies/json-schema';
35
export { GuiModelMapper } from './lib/gui-model.mapper';
46

src/lib/constants.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { GuiModel, Group } from './gui-model';
2+
import { JsonSchema } from '../dependencies/json-schema';
3+
4+
export let EMPTY_SCHEMA: JsonSchema = Object.freeze({});
5+
6+
export let EMPTY_GUI_MODEL: GuiModel = Object.freeze<GuiModel>({
7+
kind: 'group',
8+
name: '',
9+
controlType: 'group',
10+
label: '',
11+
tooltip: '',
12+
dataObjectPath: '',
13+
isRoot: true,
14+
required: true,
15+
elements: [],
16+
errors: []
17+
});
18+
19+
export let EMPTY_GUI_MODEL_GROUP: Group = Object.freeze<Group>({
20+
kind: 'group',
21+
name: '',
22+
controlType: 'group',
23+
label: '',
24+
tooltip: '',
25+
dataObjectPath: '',
26+
isRoot: false,
27+
required: true,
28+
elements: []
29+
});
30+

src/lib/gui-model.mapper.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import { GuiModel, Group, GuiElement, SubDataType,
66

77
/**
88
* Process a json schema node. This can either be the root or an object inside it when called recursively from within each property.
9-
* @param settingsKeyPath The corresponding object path in the settings object for the schema element. Used by clients of gui model.
9+
* @param dataKeyPath The corresponding object path in the schema instance (data) object for the schema element. Used by clients of gui model.
1010
* @param schemaPath The path of the element inside the schema itself. Used for error reporting.
1111
* @param accumulatedErrors A mutable(!) array where any errors during processing are appended.
1212
*/
13-
function processProperties(obj: JsonSchema, settingsKeyPath: string, schemaPath: string, accumulatedErrors: TranslationError[]): GuiElement[] {
13+
function processProperties(obj: JsonSchema, dataKeyPath: string, schemaPath: string, accumulatedErrors: TranslationError[]): GuiElement[] {
1414
let result: GuiElement[] = [];
1515

1616
let properties = obj.properties || {};
1717
let requiredKeys = new Set(obj.required || []);
1818

1919
for (let key in properties) {
2020
if (properties.hasOwnProperty(key)) {
21-
validate(key, 'key', 'string', (v) => isString(v), settingsKeyPath, accumulatedErrors);
21+
validate(key, 'key', 'string', (v) => isString(v), dataKeyPath, accumulatedErrors);
2222

23-
let settingsPropertyKeyPath = (settingsKeyPath === '') ? key : settingsKeyPath + '.' + key;
23+
let settingsPropertyKeyPath = (dataKeyPath === '') ? key : dataKeyPath + '.' + key;
2424
let schemaPropertyPath = (schemaPath === '') ? 'properties.' + key : schemaPath + '.' + 'properties.' + key;
2525

2626
let requiredItem = requiredKeys.has(key);
@@ -145,7 +145,7 @@ function createNumberField(key: string, objectPath: string, label: string, toolt
145145
controlType: values && values.length > 0 ? 'dropdown' : 'input',
146146
label: label,
147147
tooltip: tooltip,
148-
settingsObjectPath: objectPath,
148+
dataObjectPath: objectPath,
149149
defaultValue: defaultValue,
150150
required: required,
151151
type: 'number',
@@ -165,7 +165,7 @@ function createIntegerField(key: string, objectPath: string, label: string, tool
165165
controlType: values && values.length > 0 ? 'dropdown' : 'input',
166166
label: label,
167167
tooltip: tooltip,
168-
settingsObjectPath: objectPath,
168+
dataObjectPath: objectPath,
169169
defaultValue: defaultValue,
170170
values: values ? Object.freeze(values) : values,
171171
required: required,
@@ -185,7 +185,7 @@ function createBooleanField(key: string, objectPath: string, label: string, tool
185185
controlType: 'yesno',
186186
label: label,
187187
tooltip: tooltip,
188-
settingsObjectPath: objectPath,
188+
dataObjectPath: objectPath,
189189
defaultValue: defaultValue,
190190
values: values ? Object.freeze(values) : values,
191191
required: required,
@@ -205,7 +205,7 @@ function createStringField(key: string, objectPath: string, label: string, toolt
205205
controlType: values && values.length > 0 ? 'dropdown' : 'input',
206206
label: label,
207207
tooltip: tooltip,
208-
settingsObjectPath: objectPath,
208+
dataObjectPath: objectPath,
209209
defaultValue: defaultValue,
210210
values: values ? Object.freeze(values) : values,
211211
required: required,
@@ -222,7 +222,7 @@ function createGroupProperty(key: string, objectPath: string, label: string, too
222222
kind: 'group',
223223
name: key,
224224
controlType: 'group',
225-
settingsObjectPath: objectPath,
225+
dataObjectPath: objectPath,
226226
label: label,
227227
tooltip: tooltip,
228228
isRoot: false,
@@ -301,7 +301,7 @@ export class GuiModelMapper {
301301
kind: 'group',
302302
name: '',
303303
controlType: 'group',
304-
settingsObjectPath: '',
304+
dataObjectPath: '',
305305
label: '',
306306
tooltip: '',
307307
isRoot: true,

src/lib/gui-model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface GuiElementBase {
5858
* Lodash.get method or mariocasciaro's object-path (https://github.com/mariocasciaro/object-path).
5959
* This string is unique for all elements in a model and may thus be used as a key if needed.
6060
*/
61-
readonly settingsObjectPath: string;
61+
readonly dataObjectPath: string;
6262
readonly required: boolean;
6363
}
6464

src/lib/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { GuiElement } from './gui-model';
2+
3+
/**
4+
* Calculate the max nesting level of a GuiElement.
5+
*/
6+
export function maxGuiElementDepth(element: GuiElement): number {
7+
if (element.kind === 'group') {
8+
let depths: number[] = element.elements.map(e => maxGuiElementDepth(e));
9+
return 1 + (depths.length === 0 ? 0 : Math.max.apply(null, depths));
10+
} else {
11+
return 1;
12+
}
13+
}
14+

0 commit comments

Comments
 (0)