json-renderers 1.13.6 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +241 -76
- package/docs/dist/min.js +1063 -0
- package/docs/dist/v13/min.js +796 -702
- package/docs/dist/v14/min.js +1063 -0
- package/docs/index.html +725 -8
- package/docs/select.html +139 -0
- package/docs/table.html +172 -0
- package/package.json +18 -7
- package/src/index.js +2 -2
- package/src/v13/buildSpec/index.js +7 -6
- package/src/v13/index.js +42 -19
- package/src/v14/buildSpec/index.js +39 -0
- package/src/v14/common/deriveColumnsFromData.js +20 -0
- package/src/v14/index.js +81 -0
- package/src/v14/meta.js +6 -0
- package/src/v14/registerGlobal.js +14 -0
package/README.md
CHANGED
|
@@ -1,128 +1,293 @@
|
|
|
1
|
-
#
|
|
1
|
+
# json-renderers
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Browser DOM Component Renderer for JSON-Driven UIs**
|
|
4
|
+
> Consumes declarative component specifications from `json-renderers-build` and mounts real DOM elements into target containers using `@keshavsoft/json-to-tag`.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](package.json)
|
|
8
|
+
[](#the-story-of-json-renderers)
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## The Story of `json-renderers`
|
|
13
|
+
|
|
14
|
+
`json-renderers` is the **browser runtime layer** in the KeshavSoft declarative UI ecosystem.
|
|
15
|
+
|
|
16
|
+
It is not the data-shaping layer, and it is not the final DOM magic by itself. Its job is to coordinate the flow:
|
|
17
|
+
|
|
18
|
+
- raw or transformed data enters the system
|
|
19
|
+
- `json-renderers-build` prepares a UI blueprint/spec
|
|
20
|
+
- `@keshavsoft/json-to-tag` converts that blueprint into real DOM nodes
|
|
21
|
+
- `json-renderers` mounts the result into the target browser container
|
|
8
22
|
|
|
9
|
-
-
|
|
10
|
-
- Built on top of `json-to-spec` and `@keshavsoft/json-to-tag`
|
|
11
|
-
- Supports renderers such as:
|
|
12
|
-
- `datalist`
|
|
13
|
-
- `select`
|
|
14
|
-
- Legacy renderer layers for table/navTabs/form patterns are present in older source folders
|
|
15
|
-
- Simple browser-based demos and sample apps included in the repo
|
|
23
|
+
This is the crucial boundary: `json-renderers` should never be described as the repo that directly writes the browser DOM itself. It is the orchestrator. The actual DOM hook is `@keshavsoft/json-to-tag`, and the real responsibility for constructing complex UI structures is delegated to the JSON-driven spec that `json-renderers-build` creates.
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
For a table, select, datalist, or a hybrid form-table element, the runtime does not hand-code a DOM tree. Instead, it asks the builder for a JSON description of the desired component, then passes that description to `json-to-tag` to turn it into actual browser elements.
|
|
26
|
+
|
|
27
|
+
In other words, `json-renderers` is the orchestration shell around a larger pipeline, not the only engine in the stack.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## The Simple Pipeline
|
|
18
32
|
|
|
19
33
|
```text
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
├── samples/ # runnable browser examples
|
|
25
|
-
├── examples/ # additional demo usage
|
|
26
|
-
├── docs/ # docs/static assets
|
|
27
|
-
├── json-to-spec/ # bundled spec engine / related tooling
|
|
28
|
-
├── package.json # package metadata and scripts
|
|
29
|
-
├── vite.config.js # Vite config for local dev/build
|
|
30
|
-
├── LICENSE # package license (if present in your checkout)
|
|
31
|
-
└── README.md # project documentation
|
|
34
|
+
JSON transform / normalize
|
|
35
|
+
-> UI spec from json-renderers-build
|
|
36
|
+
-> DOM nodes from json-to-tag
|
|
37
|
+
-> rendered browser output from json-renderers
|
|
32
38
|
```
|
|
33
39
|
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## The 3-Tier Declarative Pipeline & The "In-Between" Story
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
46
|
+
│ 1. Application Layer: Raw Data & Configurations │
|
|
47
|
+
│ data: [{ id: 101, name: "Alpha" }], columns: ["Name"] │
|
|
48
|
+
└───────────────────────────────────┬────────────────────────────────────┘
|
|
49
|
+
│ Calls: render({ type, data, columns, targetHtmlId })
|
|
50
|
+
▼
|
|
51
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
52
|
+
│ 2. Spec Compiler: json-renderers-build (Headless AST Builder) │
|
|
53
|
+
│ • Skeletons for table, select, selectOptionsOnly │
|
|
54
|
+
│ • Compiled via json-to-spec engine │
|
|
55
|
+
│ • Output: Deterministic JSON-to-DOM AST (specAsJsonToDom) │
|
|
56
|
+
└───────────────────────────────────┬────────────────────────────────────┘
|
|
57
|
+
│ In-Between Handoff: specAsJsonToDom
|
|
58
|
+
▼
|
|
59
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
60
|
+
│ 3. DOM Mounting Runtime: json-renderers (THIS REPO) │
|
|
61
|
+
│ • Passes spec to @keshavsoft/json-to-tag │
|
|
62
|
+
│ • Creates real HTML elements (HTMLTableElement, HTMLSelectElement) │
|
|
63
|
+
│ • Resolves document.getElementById(targetHtmlId) │
|
|
64
|
+
│ • Handles append / prepend / replacement mount strategies │
|
|
65
|
+
└───────────────────────────────────┬────────────────────────────────────┘
|
|
66
|
+
│ Mounts into DOM
|
|
67
|
+
▼
|
|
68
|
+
┌────────────────────────────────────────────────────────────────────────┐
|
|
69
|
+
│ 4. Live Browser DOM Container │
|
|
70
|
+
│ <div id="targetHtmlId"> <table>...</table> </div> │
|
|
71
|
+
└────────────────────────────────────────────────────────────────────────┘
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### The "In-Between": How the Stack Connects
|
|
75
|
+
|
|
76
|
+
When you call `render({ type, targetHtmlId, data, columns, appendPosition })`:
|
|
77
|
+
|
|
78
|
+
1. **Data shaping (optional upstream layer):** `@keshavsoft/json-transformer` or a similar normalizer may reshape incoming business JSON into a clean model.
|
|
79
|
+
2. **Spec Request:** `json-renderers` calls `json-renderers-build({ type, data, columns })`.
|
|
80
|
+
3. **Headless AST:** `json-renderers-build` merges the inputs with its internal component blueprints (`skeleton.json`) via `json-to-spec` and returns a pure specification tree (`specAsJsonToDom`).
|
|
81
|
+
4. **DOM Instantiation:** The specification is passed to `@keshavsoft/json-to-tag(jsonToSend)`, creating real browser `Node` or `NodeList` instances.
|
|
82
|
+
5. **DOM Mounting:** `json-renderers` locates `document.getElementById(targetHtmlId)` and appends or prepends the final content into the page.
|
|
83
|
+
|
|
84
|
+
This matters because the true DOM magic happens in `json-to-tag`, while `json-renderers` simply makes that runtime flow usable from an application.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Features
|
|
89
|
+
|
|
90
|
+
- **End-to-End JSON to DOM:** One function call turns your business data into fully rendered, styled UI controls.
|
|
91
|
+
- **Built on `json-renderers-build` (v13):**
|
|
92
|
+
- **`table`:** Complete responsive table with Bootstrap classes (`table table-hover table-striped mb-0`), headers, and rows.
|
|
93
|
+
- **`select`:** Standalone `<select id="LedgerName">` populated with dynamic options.
|
|
94
|
+
- **`selectOptionsOnly`:** Option fragments injected directly into pre-existing `<select>` elements.
|
|
95
|
+
- **Flexible Mounting Modes:** Replace container content (default) or `prepend` to existing content.
|
|
96
|
+
- **Global & ESM Distribution:** Usable via npm or directly in the browser via CDN script (`window.ks.jsonRenderers`).
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
34
100
|
## Installation
|
|
35
101
|
|
|
36
|
-
|
|
102
|
+
### NPM
|
|
37
103
|
|
|
38
104
|
```bash
|
|
39
105
|
npm install json-renderers
|
|
40
106
|
```
|
|
41
107
|
|
|
42
|
-
|
|
108
|
+
### Browser (CDN / ES Module)
|
|
43
109
|
|
|
44
|
-
```
|
|
45
|
-
|
|
110
|
+
```html
|
|
111
|
+
<!-- Load ES module bundle directly from CDN -->
|
|
112
|
+
<script type="module" src="https://cdn.jsdelivr.net/gh/keshavsoft/json-renderers@main/docs/dist/v13/min.js"></script>
|
|
46
113
|
```
|
|
47
114
|
|
|
48
|
-
|
|
115
|
+
When loaded via `<script type="module">`, it automatically registers globally on:
|
|
116
|
+
```javascript
|
|
117
|
+
window.ks.jsonRenderers = {
|
|
118
|
+
meta: {
|
|
119
|
+
version: "v13.1.0",
|
|
120
|
+
description: "build table from store data and render to DOM uses json-to-spec, json-to-dom under the hood"
|
|
121
|
+
},
|
|
122
|
+
renderToDom: [Function: render]
|
|
123
|
+
};
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Quick Start & Usage
|
|
49
129
|
|
|
50
|
-
###
|
|
130
|
+
### 1. Rendering a Table into the DOM (`table`)
|
|
51
131
|
|
|
52
|
-
|
|
132
|
+
HTML:
|
|
133
|
+
```html
|
|
134
|
+
<div id="table-container"></div>
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
JavaScript:
|
|
138
|
+
```javascript
|
|
53
139
|
import render from "json-renderers";
|
|
54
140
|
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
"Crown Logistics"
|
|
60
|
-
]
|
|
61
|
-
};
|
|
141
|
+
const records = [
|
|
142
|
+
{ id: 101, name: "Alpha Enterprise", city: "Hyderabad" },
|
|
143
|
+
{ id: 102, name: "Beta Logistics", city: "Bengaluru" }
|
|
144
|
+
];
|
|
62
145
|
|
|
63
146
|
render({
|
|
64
|
-
type: "
|
|
65
|
-
|
|
66
|
-
|
|
147
|
+
type: "table",
|
|
148
|
+
targetHtmlId: "table-container",
|
|
149
|
+
data: records,
|
|
150
|
+
columns: ["Name", "City"]
|
|
67
151
|
});
|
|
68
152
|
```
|
|
69
153
|
|
|
70
|
-
|
|
154
|
+
*Result:* The table is rendered directly into `#table-container` with clean Bootstrap table classes.
|
|
71
155
|
|
|
72
|
-
|
|
73
|
-
import render from "json-renderers";
|
|
156
|
+
---
|
|
74
157
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
158
|
+
### 2. Rendering a Select Dropdown (`select`)
|
|
159
|
+
|
|
160
|
+
HTML:
|
|
161
|
+
```html
|
|
162
|
+
<div id="dropdown-container"></div>
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
JavaScript:
|
|
166
|
+
```javascript
|
|
167
|
+
import render from "json-renderers";
|
|
82
168
|
|
|
83
169
|
render({
|
|
84
|
-
type: "
|
|
85
|
-
|
|
86
|
-
|
|
170
|
+
type: "select",
|
|
171
|
+
targetHtmlId: "dropdown-container",
|
|
172
|
+
data: ["Account Receivable", "Account Payable", "Sales Revenue"]
|
|
87
173
|
});
|
|
88
174
|
```
|
|
89
175
|
|
|
90
|
-
|
|
176
|
+
*Result:* Generates `<select id="LedgerName">` with three `<option>` children and mounts it into `#dropdown-container`.
|
|
91
177
|
|
|
92
|
-
|
|
178
|
+
---
|
|
93
179
|
|
|
94
|
-
|
|
180
|
+
### 3. Populating an Existing Select Element (`selectOptionsOnly`)
|
|
95
181
|
|
|
96
|
-
|
|
97
|
-
|
|
182
|
+
When your page already contains a `<select>` element and you only want to dynamically populate its options:
|
|
183
|
+
|
|
184
|
+
HTML:
|
|
185
|
+
```html
|
|
186
|
+
<select id="user-role-select" class="form-select">
|
|
187
|
+
<option value="" disabled selected>Select Role...</option>
|
|
188
|
+
</select>
|
|
98
189
|
```
|
|
99
190
|
|
|
100
|
-
|
|
191
|
+
JavaScript:
|
|
192
|
+
```javascript
|
|
193
|
+
import render from "json-renderers";
|
|
101
194
|
|
|
102
|
-
|
|
103
|
-
|
|
195
|
+
render({
|
|
196
|
+
type: "selectOptionsOnly",
|
|
197
|
+
targetHtmlId: "user-role-select",
|
|
198
|
+
data: ["Administrator", "Editor", "Viewer"]
|
|
199
|
+
});
|
|
104
200
|
```
|
|
105
201
|
|
|
106
|
-
|
|
202
|
+
*Result:* The `<option>` elements are appended directly into `#user-role-select`.
|
|
107
203
|
|
|
108
|
-
|
|
204
|
+
---
|
|
109
205
|
|
|
110
|
-
|
|
111
|
-
- `examples/`
|
|
112
|
-
- `docs/`
|
|
206
|
+
## API Reference
|
|
113
207
|
|
|
114
|
-
|
|
208
|
+
### `render(options)` / `default export`
|
|
115
209
|
|
|
116
|
-
|
|
210
|
+
The entry point exported by `json-renderers` accepts a single configuration object:
|
|
117
211
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
212
|
+
```javascript
|
|
213
|
+
render({
|
|
214
|
+
type = "table",
|
|
215
|
+
targetHtmlId,
|
|
216
|
+
data,
|
|
217
|
+
columns,
|
|
218
|
+
appendPosition,
|
|
219
|
+
showLog = false
|
|
220
|
+
})
|
|
221
|
+
```
|
|
121
222
|
|
|
122
|
-
|
|
223
|
+
| Parameter | Type | Default | Description |
|
|
224
|
+
|---|---|---|---|
|
|
225
|
+
| `type` | `string` | `"table"` | The component renderer to invoke: `"table"`, `"select"`, or `"selectOptionsOnly"`. |
|
|
226
|
+
| `targetHtmlId` | `string` | `undefined` | The ID of the DOM element (`document.getElementById(targetHtmlId)`) to mount into. |
|
|
227
|
+
| `data` | `Array` | `[]` | Data array. For `table`: array of row objects. For `select` / `selectOptionsOnly`: array of strings. |
|
|
228
|
+
| `columns` | `Array` | `undefined` | Column headers for `table`. Accepts an array of strings (e.g. `["Name"]`) or objects (e.g. `[{ title: "Name" }]`). |
|
|
229
|
+
| `appendPosition` | `string` | `undefined` | If set to `"prepend"`, prepends to the container. Otherwise replaces (`container.innerHTML = ""`). |
|
|
230
|
+
| `showLog` | `boolean` | `false` | Enables debug console logging during render execution. |
|
|
231
|
+
|
|
232
|
+
**Returns:** `HTMLElement` — The mounted DOM container.
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
## Comparison: `json-renderers` vs `json-renderers-build`
|
|
123
237
|
|
|
124
|
-
|
|
238
|
+
| Aspect | `json-renderers-build` | `json-renderers` (THIS REPO) |
|
|
239
|
+
|---|---|---|
|
|
240
|
+
| **Role** | Specification Builder / Compiler | DOM Mounting Runtime |
|
|
241
|
+
| **Output** | Declarative JSON AST (`specAsJsonToDom`) | Real HTML DOM Elements inserted into page |
|
|
242
|
+
| **DOM Dependency** | Zero (100% Headless & Isomorphic) | Browser DOM (`document.getElementById`) |
|
|
243
|
+
| **Environments** | Browser, Node.js, SSR, Web Workers | Browser runtime |
|
|
244
|
+
| **Core Dependency** | `json-to-spec` | `json-renderers-build`, `@keshavsoft/json-to-tag` |
|
|
245
|
+
| **Typical Caller** | `json-renderers`, Custom build pipelines | Web Applications, Dashboards, UI Views |
|
|
125
246
|
|
|
126
|
-
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Project Structure
|
|
250
|
+
|
|
251
|
+
```text
|
|
252
|
+
json-renderers/
|
|
253
|
+
├── docs/ # Documentation portal & distribution
|
|
254
|
+
│ ├── dist/ # Production bundles (Vite build)
|
|
255
|
+
│ │ ├── min.js # Latest bundle
|
|
256
|
+
│ │ └── v13/min.js # v13 production bundle
|
|
257
|
+
│ └── index.html # Interactive Documentation & Live DOM Workbench
|
|
258
|
+
├── samples/ # Runnable browser verification samples
|
|
259
|
+
│ ├── table/ # Table DOM mounting sample
|
|
260
|
+
│ ├── select/ # Select dropdown DOM mounting sample
|
|
261
|
+
│ └── selectOptionsOnly/ # Existing select options population sample
|
|
262
|
+
├── src/ # Source code
|
|
263
|
+
│ ├── index.js # Entry router (exports ./v13/index.js)
|
|
264
|
+
│ └── v13/ # CURRENT: Runtime DOM renderer
|
|
265
|
+
│ ├── buildSpec/ # In-between handoff: calls json-renderers-build & json-to-tag
|
|
266
|
+
│ ├── meta.js # Version & metadata descriptor
|
|
267
|
+
│ ├── registerGlobal.js # Global window.ks namespace attachment
|
|
268
|
+
│ └── index.js # DOM resolver and mounter
|
|
269
|
+
├── package.json # Package definition & scripts
|
|
270
|
+
├── vite.config.js # Vite build configuration
|
|
271
|
+
└── README.md # Complete repository guide
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Development & Build
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
# Install dependencies
|
|
280
|
+
npm install
|
|
281
|
+
|
|
282
|
+
# Start Vite development server
|
|
283
|
+
npm run dev
|
|
284
|
+
|
|
285
|
+
# Build production bundle (into docs/dist/v13/min.js and docs/dist/min.js)
|
|
286
|
+
npm run build
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## License
|
|
127
292
|
|
|
128
|
-
KeshavSoft
|
|
293
|
+
MIT © [KeshavSoft](https://github.com/keshavsoft)
|