generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ad260b
commit f111976
Showing
19 changed files
with
470 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ComponentBase from '../../scripts/component-base.js'; | ||
|
||
export default class Example extends ComponentBase { | ||
ready() { | ||
console.log('Example ready'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import ComponentBase from '../../scripts/component-base.js'; | ||
import { flat, getBaseUrl } from '../../scripts/libs.js'; | ||
import { generalManipulation, renderVirtualDom } from '../../scripts/render/dom.js'; | ||
|
||
export default class Form extends ComponentBase { | ||
static observedAttributes = ['schema']; | ||
|
||
dom = []; | ||
|
||
resolver = null; | ||
|
||
loading = new Promise((resolve) => { | ||
this.resolver = resolve; | ||
}); | ||
|
||
async connected() { | ||
await this.loading; | ||
this.append(...renderVirtualDom(generalManipulation(this.dom))); | ||
} | ||
|
||
get prefix() { | ||
return `${getBaseUrl()}/schemas/`; | ||
} | ||
|
||
get sufix() { | ||
return '.schema.json'; | ||
} | ||
|
||
async applySchema(schema) { | ||
console.log(schema); | ||
const n = flat(schema); | ||
const path = Object.keys(n).reduce((acc, key) => `${acc}${key}-${n[key]}`, ''); | ||
const json = await this.parseSchema(await this.loadSchema(path)); | ||
this.dom = this.schemaToDom(json); | ||
this.resolver(this.dom); | ||
} | ||
|
||
schemaToDom(schema) { | ||
return schema.map((s) => { | ||
const { key, type, title, required, id, label, placeholder, helpText, pattern } = s; | ||
return { | ||
tag: 'raqn-input', | ||
class: ['form-group'], | ||
attributes: { | ||
key, | ||
type, | ||
title, | ||
required, | ||
id, | ||
label, | ||
placeholder, | ||
helpText, | ||
pattern, | ||
}, | ||
children: [ | ||
{ | ||
tag: 'label', | ||
class: ['form-label'], | ||
attributes: { | ||
for: id, | ||
}, | ||
children: [ | ||
{ | ||
class: ['form-label-text'], | ||
tag: 'span', | ||
text: label, | ||
}, | ||
], | ||
}, | ||
{ | ||
tag: 'input', | ||
class: ['form-control'], | ||
children: [], | ||
attributes: { | ||
type, | ||
name: key, | ||
id, | ||
placeholder, | ||
pattern, | ||
required, | ||
title, | ||
}, | ||
}, | ||
{ | ||
class: ['form-text'], | ||
tag: 'small', | ||
text: helpText, | ||
children: [], | ||
}, | ||
], | ||
}; | ||
}); | ||
} | ||
|
||
async loadSchema(ref) { | ||
if (ref.includes('http')) { | ||
return (await fetch(ref)).json(); | ||
} | ||
return (await fetch(`${this.prefix}${ref}${this.sufix}`)).json(); | ||
} | ||
|
||
extractName(name) { | ||
return name.replace(this.prefix, '').replace(this.sufix, '').toLowerCase(); | ||
} | ||
|
||
async parseSchema(data) { | ||
const { $id, required, properties } = data; | ||
return Object.keys(properties).reduce(async (acc, key) => { | ||
const fullfill = await acc; | ||
const { type, title, description, $ref, pattern } = properties[key]; | ||
if ($ref) { | ||
const options = await this.parseSchema(await this.loadSchema($ref)); | ||
|
||
fullfill.push(...options); | ||
return fullfill; | ||
} | ||
const id = `${this.extractName($id)}-${key}`; | ||
fullfill.push({ | ||
key, | ||
type, | ||
title, | ||
description, | ||
required: required.includes(key), | ||
id: `${id}`, | ||
i18n: `i18n-${id}`, | ||
label: `i18n-${id}-label`, | ||
placeholder: `i18n-${id}-placeholder`, | ||
helpText: `i18n-${id}-helpText`, | ||
pattern, | ||
}); | ||
return fullfill; | ||
}, []); | ||
} | ||
|
||
// parse json schema and load any external $ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:host { | ||
background-color: red; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import ComponentBase from '../../../scripts/component-base.js'; | ||
|
||
export default class Input extends ComponentBase { | ||
static observedAttributes = ['schema']; | ||
|
||
connected() { | ||
console.log('init'); | ||
} | ||
|
||
// parse json schema and load any external $ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"$id": "http://localhost:3000/schemas/address.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"description": "An address similar to http://microformats.org/wiki/h-card", | ||
"type": "object", | ||
"properties": { | ||
"postOfficeBox": { | ||
"type": "string" | ||
}, | ||
"extendedAddress": { | ||
"type": "string" | ||
}, | ||
"streetAddress": { | ||
"type": "string" | ||
}, | ||
"locality": { | ||
"type": "string" | ||
}, | ||
"region": { | ||
"type": "string" | ||
}, | ||
"postalCode": { | ||
"type": "string" | ||
}, | ||
"countryName": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["locality", "region", "countryName"], | ||
"dependentRequired": { | ||
"postOfficeBox": ["streetAddress"], | ||
"extendedAddress": ["streetAddress"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"$id": "http://localhost:3000/schemas/blog-post.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"description": "A representation of a blog post", | ||
"type": "object", | ||
"required": ["title", "content", "author"], | ||
"properties": { | ||
"title": { | ||
"type": "string" | ||
}, | ||
"content": { | ||
"type": "string" | ||
}, | ||
"publishedDate": { | ||
"type": "string", | ||
"format": "date-time" | ||
}, | ||
"author": { | ||
"$ref": "http://localhost:3000/schemas/user-profile.schema.json" | ||
}, | ||
"tags": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"$id": "http://localhost:3000/schemas/ecommerce.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$defs": { | ||
"product": { | ||
"$anchor": "ProductSchema", | ||
"type": "object", | ||
"properties": { | ||
"name": { "type": "string" }, | ||
"price": { "type": "number", "minimum": 0 } | ||
} | ||
}, | ||
"order": { | ||
"$anchor": "OrderSchema", | ||
"type": "object", | ||
"properties": { | ||
"orderId": { "type": "string" }, | ||
"items": { | ||
"type": "array", | ||
"items": { "$ref": "#ProductSchema" } | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"$id": "http://localhost:3000/schemas/geographical-location.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "Longitude and Latitude Values", | ||
"description": "A geographical coordinate.", | ||
"required": ["latitude", "longitude"], | ||
"type": "object", | ||
"properties": { | ||
"latitude": { | ||
"type": "number", | ||
"minimum": -90, | ||
"maximum": 90 | ||
}, | ||
"longitude": { | ||
"type": "number", | ||
"minimum": -180, | ||
"maximum": 180 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"$id": "http://localhost:3000/schemas/health-record.schema.json", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"description": "Schema for representing a health record", | ||
"type": "object", | ||
"required": ["patientName", "dateOfBirth", "bloodType"], | ||
"properties": { | ||
"patientName": { | ||
"type": "string" | ||
}, | ||
"dateOfBirth": { | ||
"type": "string", | ||
"format": "date" | ||
}, | ||
"bloodType": { | ||
"type": "string" | ||
}, | ||
"allergies": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"conditions": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"medications": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"emergencyContact": { | ||
"$ref": "http://localhost:3000/schemas/user-profile.schema.json" | ||
} | ||
} | ||
} |
Oops, something went wrong.