Nest render
function in separate class
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
babdc6104e
commit
5ff474a7ea
|
@ -1,6 +1,6 @@
|
||||||
import { App } from "./Components.js";
|
import { App } from "./Components.js";
|
||||||
import { State } from "./State.js";
|
import { State } from "./State.js";
|
||||||
import { render } from "./SuiWeb.js";
|
import { SuiWeb } from "./SuiWeb.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a game.
|
* Represents a game.
|
||||||
|
@ -171,7 +171,7 @@ export class Game
|
||||||
{
|
{
|
||||||
let container = document.getElementById(this.id);
|
let container = document.getElementById(this.id);
|
||||||
container.innerHTML = "";
|
container.innerHTML = "";
|
||||||
render([App, this], container);
|
SuiWeb.render([App, this], container);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,51 +1,57 @@
|
||||||
/**
|
/**
|
||||||
* Renders the specified {@link data `data`} and appends it to the specified {@link element `element`}.
|
* Provides component for rendering elements written in SJDON notation.
|
||||||
*
|
|
||||||
* @param {NodeDescriptor} data
|
|
||||||
* The node to render written in SJDON notation.
|
|
||||||
*
|
|
||||||
* @param {HTMLElement} element
|
|
||||||
* The element to add the rendered node to.
|
|
||||||
*/
|
*/
|
||||||
export function render(data, element)
|
export class SuiWeb
|
||||||
{
|
{
|
||||||
if (Array.isArray(data))
|
/**
|
||||||
|
* Renders the specified {@link data `data`} and appends it to the specified {@link element `element`}.
|
||||||
|
*
|
||||||
|
* @param {NodeDescriptor} data
|
||||||
|
* The node to render written in SJDON notation.
|
||||||
|
*
|
||||||
|
* @param {HTMLElement} element
|
||||||
|
* The element to add the rendered node to.
|
||||||
|
*/
|
||||||
|
static render(data, element)
|
||||||
{
|
{
|
||||||
let descriptor = data[0];
|
if (Array.isArray(data))
|
||||||
let args = data.slice(1);
|
|
||||||
|
|
||||||
if (typeof descriptor === "function")
|
|
||||||
{
|
{
|
||||||
render(descriptor(...args), element);
|
let descriptor = data[0];
|
||||||
}
|
let args = data.slice(1);
|
||||||
else if (typeof descriptor === "string")
|
|
||||||
{
|
|
||||||
let result = element.ownerDocument.createElement(descriptor);
|
|
||||||
element.appendChild(result);
|
|
||||||
|
|
||||||
for (let arg of args)
|
if (typeof descriptor === "function")
|
||||||
{
|
{
|
||||||
if (typeof arg === "object" && !Array.isArray(arg))
|
SuiWeb.render(descriptor(...args), element);
|
||||||
|
}
|
||||||
|
else if (typeof descriptor === "string")
|
||||||
|
{
|
||||||
|
let result = element.ownerDocument.createElement(descriptor);
|
||||||
|
element.appendChild(result);
|
||||||
|
|
||||||
|
for (let arg of args)
|
||||||
{
|
{
|
||||||
Object.assign(result, arg);
|
if (typeof arg === "object" && !Array.isArray(arg))
|
||||||
}
|
{
|
||||||
else
|
Object.assign(result, arg);
|
||||||
{
|
}
|
||||||
render(arg, result);
|
else
|
||||||
|
{
|
||||||
|
SuiWeb.render(arg, result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new SyntaxError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (typeof data === "string")
|
||||||
|
{
|
||||||
|
element.appendChild(element.ownerDocument.createTextNode(data));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new SyntaxError();
|
throw new SyntaxError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (typeof data === "string")
|
|
||||||
{
|
|
||||||
element.appendChild(element.ownerDocument.createTextNode(data));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new SyntaxError();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue