2024-02-16 01:31:49 -05:00
|
|
|
import Component from "@glimmer/component";
|
2024-02-26 17:07:32 -05:00
|
|
|
import { cached, tracked } from "@glimmer/tracking";
|
2024-02-16 01:31:49 -05:00
|
|
|
import { fn } from "@ember/helper";
|
|
|
|
import { on } from "@ember/modifier";
|
|
|
|
import { action } from "@ember/object";
|
2024-02-20 05:43:18 -05:00
|
|
|
import DButton from "discourse/components/d-button";
|
|
|
|
import I18n from "discourse-i18n";
|
2024-02-26 17:07:32 -05:00
|
|
|
import FieldInput from "./field";
|
2024-02-16 01:31:49 -05:00
|
|
|
|
|
|
|
class Node {
|
2024-02-26 17:07:32 -05:00
|
|
|
@tracked text;
|
|
|
|
object;
|
|
|
|
schema;
|
|
|
|
index;
|
2024-02-16 01:31:49 -05:00
|
|
|
active = false;
|
|
|
|
trees = [];
|
|
|
|
|
2024-02-26 17:07:32 -05:00
|
|
|
constructor({ text, index, object, schema }) {
|
2024-02-16 01:31:49 -05:00
|
|
|
this.text = text;
|
|
|
|
this.index = index;
|
2024-02-26 17:07:32 -05:00
|
|
|
this.object = object;
|
|
|
|
this.schema = schema;
|
2024-02-16 01:31:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Tree {
|
|
|
|
propertyName = null;
|
|
|
|
nodes = [];
|
|
|
|
}
|
|
|
|
|
2024-02-26 17:07:32 -05:00
|
|
|
export default class SchemaThemeSettingEditor extends Component {
|
2024-02-16 01:31:49 -05:00
|
|
|
@tracked activeIndex = 0;
|
2024-02-20 05:43:18 -05:00
|
|
|
@tracked backButtonText;
|
2024-02-16 01:31:49 -05:00
|
|
|
history = [];
|
|
|
|
|
2024-02-26 17:07:32 -05:00
|
|
|
@cached
|
2024-02-16 01:31:49 -05:00
|
|
|
get tree() {
|
|
|
|
let schema = this.args.schema;
|
|
|
|
let data = this.args.data;
|
|
|
|
|
|
|
|
for (const point of this.history) {
|
2024-02-20 05:43:18 -05:00
|
|
|
data = data[point.node.index][point.propertyName];
|
|
|
|
schema = schema.properties[point.propertyName].schema;
|
2024-02-16 01:31:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const tree = new Tree();
|
|
|
|
|
2024-02-26 17:07:32 -05:00
|
|
|
data.forEach((object, index) => {
|
|
|
|
const node = new Node({
|
|
|
|
index,
|
|
|
|
schema,
|
|
|
|
object,
|
|
|
|
text: object[schema.identifier],
|
|
|
|
});
|
2024-02-16 01:31:49 -05:00
|
|
|
if (index === this.activeIndex) {
|
|
|
|
node.active = true;
|
2024-02-26 17:07:32 -05:00
|
|
|
const childObjectsProperties = this.findChildObjectsProperties(
|
|
|
|
schema.properties
|
|
|
|
);
|
2024-02-16 01:31:49 -05:00
|
|
|
for (const childObjectsProperty of childObjectsProperties) {
|
|
|
|
const subtree = new Tree();
|
|
|
|
subtree.propertyName = childObjectsProperty.name;
|
|
|
|
data[index][childObjectsProperty.name].forEach(
|
|
|
|
(childObj, childIndex) => {
|
|
|
|
subtree.nodes.push(
|
|
|
|
new Node({
|
2024-02-26 17:07:32 -05:00
|
|
|
text: childObj[childObjectsProperty.schema.identifier],
|
2024-02-16 01:31:49 -05:00
|
|
|
index: childIndex,
|
2024-02-26 17:07:32 -05:00
|
|
|
object: childObj,
|
|
|
|
schema: childObjectsProperty.schema,
|
2024-02-16 01:31:49 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
node.trees.push(subtree);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tree.nodes.push(node);
|
|
|
|
});
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2024-02-26 17:07:32 -05:00
|
|
|
@cached
|
|
|
|
get activeNode() {
|
|
|
|
return this.tree.nodes.find((node, index) => {
|
|
|
|
return index === this.activeIndex;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get fields() {
|
|
|
|
const node = this.activeNode;
|
|
|
|
const list = [];
|
|
|
|
for (const [name, spec] of Object.entries(node.schema.properties)) {
|
|
|
|
if (spec.type === "objects") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
list.push({
|
|
|
|
name,
|
2024-02-29 03:11:32 -05:00
|
|
|
spec,
|
2024-02-26 17:07:32 -05:00
|
|
|
value: node.object[name],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:31:49 -05:00
|
|
|
findChildObjectsProperties(properties) {
|
|
|
|
const list = [];
|
|
|
|
for (const [name, spec] of Object.entries(properties)) {
|
|
|
|
if (spec.type === "objects") {
|
|
|
|
list.push({
|
|
|
|
name,
|
2024-02-26 17:07:32 -05:00
|
|
|
schema: spec.schema,
|
2024-02-16 01:31:49 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
onClick(node) {
|
|
|
|
this.activeIndex = node.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2024-02-20 05:43:18 -05:00
|
|
|
onChildClick(node, tree, parentNode) {
|
|
|
|
this.history.push({
|
|
|
|
propertyName: tree.propertyName,
|
|
|
|
node: parentNode,
|
|
|
|
});
|
|
|
|
this.backButtonText = I18n.t("admin.customize.theme.schema.back_button", {
|
|
|
|
name: parentNode.text,
|
|
|
|
});
|
2024-02-16 01:31:49 -05:00
|
|
|
this.activeIndex = node.index;
|
|
|
|
}
|
|
|
|
|
2024-02-20 05:43:18 -05:00
|
|
|
@action
|
|
|
|
backButtonClick() {
|
|
|
|
const historyPoint = this.history.pop();
|
|
|
|
this.activeIndex = historyPoint.node.index;
|
|
|
|
if (this.history.length > 0) {
|
|
|
|
this.backButtonText = I18n.t("admin.customize.theme.schema.back_button", {
|
|
|
|
name: this.history[this.history.length - 1].node.text,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.backButtonText = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 17:07:32 -05:00
|
|
|
@action
|
|
|
|
inputFieldChanged(field, newVal) {
|
|
|
|
if (field.name === this.activeNode.schema.identifier) {
|
|
|
|
this.activeNode.text = newVal;
|
|
|
|
}
|
|
|
|
this.activeNode.object[field.name] = newVal;
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:31:49 -05:00
|
|
|
<template>
|
|
|
|
<div class="schema-editor-navigation">
|
2024-02-20 05:43:18 -05:00
|
|
|
{{#if this.backButtonText}}
|
|
|
|
<DButton
|
|
|
|
@action={{this.backButtonClick}}
|
|
|
|
@icon="chevron-left"
|
|
|
|
@translatedLabel={{this.backButtonText}}
|
|
|
|
class="back-button"
|
|
|
|
/>
|
|
|
|
{{/if}}
|
2024-02-16 01:31:49 -05:00
|
|
|
<ul class="tree">
|
|
|
|
{{#each this.tree.nodes as |node|}}
|
|
|
|
<div class="item-container">
|
|
|
|
<li
|
|
|
|
role="link"
|
|
|
|
class="parent node{{if node.active ' active'}}"
|
|
|
|
{{on "click" (fn this.onClick node)}}
|
|
|
|
>
|
|
|
|
{{node.text}}
|
|
|
|
</li>
|
|
|
|
{{#each node.trees as |nestedTree|}}
|
|
|
|
<ul>
|
|
|
|
{{#each nestedTree.nodes as |childNode|}}
|
|
|
|
<li
|
|
|
|
role="link"
|
|
|
|
class="child node"
|
2024-02-20 05:43:18 -05:00
|
|
|
{{on
|
|
|
|
"click"
|
|
|
|
(fn this.onChildClick childNode nestedTree node)
|
|
|
|
}}
|
2024-02-16 01:31:49 -05:00
|
|
|
>{{childNode.text}}</li>
|
|
|
|
{{/each}}
|
|
|
|
</ul>
|
|
|
|
{{/each}}
|
|
|
|
</div>
|
|
|
|
{{/each}}
|
|
|
|
</ul>
|
2024-02-26 17:07:32 -05:00
|
|
|
{{#each this.fields as |field|}}
|
|
|
|
<FieldInput
|
|
|
|
@name={{field.name}}
|
|
|
|
@value={{field.value}}
|
2024-02-29 03:11:32 -05:00
|
|
|
@spec={{field.spec}}
|
2024-02-26 17:07:32 -05:00
|
|
|
@onValueChange={{fn this.inputFieldChanged field}}
|
|
|
|
/>
|
|
|
|
{{/each}}
|
2024-02-16 01:31:49 -05:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
}
|