Begin working on converting the list of text into an actual graph

This commit is contained in:
Oliver-Akins 2025-05-24 16:45:43 -06:00
parent c7197641b6
commit cb519ad721
4 changed files with 108 additions and 46 deletions

View file

@ -1,9 +1,13 @@
import { filePath } from "../consts.mjs";
import { Logger } from "../utils/Logger.mjs";
import { PrivacyMode } from "../utils/privacy.mjs";
const { HandlebarsApplicationMixin } = foundry.applications.api;
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
const { AbstractSidebarTab } = foundry.applications.sidebar;
const { getType } = foundry.utils;
export class StatSidebar extends HandlebarsApplicationMixin(AbstractSidebarTab) {
// #region Options
/** @override */
static DEFAULT_OPTIONS = {
classes: [
@ -14,9 +18,7 @@ export class StatSidebar extends HandlebarsApplicationMixin(AbstractSidebarTab)
title: `SIDEBAR.TabSettings`,
},
actions: {
openStats: this.#openStats,
manageTables: this.#manageTables,
createTable: this.#createTable,
openApp: this.#openApp,
},
};
@ -25,23 +27,74 @@ export class StatSidebar extends HandlebarsApplicationMixin(AbstractSidebarTab)
/** @override */
static PARTS = {
stats: {
template: filePath(`templates/Apps/StatSidebar/main.hbs`),
root: true,
// stats: {
// template: filePath(`templates/Apps/StatSidebar/main.hbs`),
// root: true,
// },
summaryText: {
template: filePath(`templates/Apps/StatSidebar/.hbs`),
},
summaryGraph: {
template: filePath(`templates/Apps/StatSidebar/.hbs`),
},
appControls: {
template: filePath(`templates/Apps/StatSidebar/controlSection.hbs`),
},
};
// #endregion Options
async _prepareContext(options) {
// #region Lifecycle
async render(options, _options) {
const { userUpdated = null } = (getType(options) === `Object` ? options : _options) ?? {};
if (userUpdated && userUpdated !== game.user.id) {
// TODO: Update the data in the graph
return;
};
return super.render(options, _options);
};
async _onFirstRender(context, options) {
await super._onFirstRender(context, options);
CONFIG.stats.db.addApp(this);
};
// #endregion Lifecycle
// #region Data Prep
async _preparePartContext(context, options) {
const ctx = await super._prepareContext(options);
this.#prepareApps(ctx);
return ctx;
};
async #prepareSummary(ctx) {
const db = CONFIG.stats.db;
ctx.tableCount = (await db.getTables()).length;
const tables = await db.getTables();
ctx.tableCount = tables.length;
ctx.rowCount = {
total: 0,
public: 0,
self: 0,
private: 0,
gm: 0,
};
for (const table of tables) {
const rows = await db.getRows(table.name, [game.user.id], Object.values(PrivacyMode));
for (const row of rows[game.user.id] ?? []) {
ctx.rowCount[row.privacy]++;
ctx.rowCount.total++;
};
};
};
async #prepareApps(ctx) {
const controls = {
openStats: { label: `View Stats`, action: `openStats` },
createTable: { label: `Create New Table`, action: `createTable` },
manageTables: { label: `Manage Tables`, action: `manageTables` },
// manageData: { label: `Manage Data`, action: `` },
openStats: { label: `View Stats`, action: `openApp`, appKey: `viewer` },
createTable: { label: `Create New Table`, action: `openApp`, appKey: `creator` },
manageTables: { label: `Manage Tables`, action: `openApp`, appKey: `tableManager` },
// manageData: { label: `Manage Data`, action: `openApp`, appKey: `rowManager` },
};
if (!game.user.isGM) {
@ -55,27 +108,21 @@ export class StatSidebar extends HandlebarsApplicationMixin(AbstractSidebarTab)
// delete controls.manageData;
// };
Hooks.callAll(`${__ID__}.getStatsSidebarControls`, controls);
Hooks.callAll(`${__ID__}.getStatsSidebarApps`, controls);
ctx.controls = Object.values(controls);
return ctx;
};
// #endregion Data Prep
/** @this {StatSidebar} */
static async #openStats() {
const app = new CONFIG.stats.viewer;
app.render({ force: true });
};
/** @this {StatSidebar} */
static async #manageTables() {
const app = new CONFIG.stats.manager;
app.render({ force: true });
};
/** @this {StatSidebar} */
static async #createTable() {
const app = new CONFIG.stats.creator;
// #region Actions
static async #openApp(target) {
const { appKey } = target.dataset;
const cls = CONFIG.stats[appKey];
if (!(cls.prototype instanceof ApplicationV2)) {
Logger.error(`Cannot create an app from`, cls);
return;
};
const app = new cls();
app.render({ force: true });
};
// #endregion Actions
};

View file

@ -38,7 +38,8 @@ Hooks.on(`init`, () => {
db: UserFlagDatabase,
viewer: StatsViewer,
creator: TableCreator,
manager: TableManager,
tableManager: TableManager,
rowManager: null,
};
if (import.meta.env.DEV) {

View file

@ -0,0 +1,12 @@
<section>
<h4 class="divider">{{ sectionTitle }}</h4>
{{#each controls as | btn |}}
<button
type="button"
data-action="{{ btn.action }}"
data-app-key="{{ btn.appKey }}"
>
{{ btn.label }}
</button>
{{/each}}
</section>

View file

@ -4,28 +4,30 @@
<span>Count of Tables</span>
<span>{{ tableCount }}</span>
</div>
{{!-- <div class="info">
<div class="info">
<span>Your Total Rows</span>
<span>{{ userRowCount }}</span>
<span>{{ rowCount.total }}</span>
</div>
<div class="info">
<span>Your Public Rows</span>
<span>{{ rowCount.public }}</span>
</div>
<div class="info">
<span>Your Self Rows</span>
<span>{{ rowCount.self }}</span>
</div>
<div class="info">
<span>Your Private Rows</span>
<span>{{ userPrivateRowCount }}</span>
<span>{{ rowCount.private }}</span>
</div>
<div class="info">
<span>Your Blind Rows</span>
<span>{{ rowCount.gm }}</span>
</div>
{{!-- <div class="info">
<span>Global Row Count</span>
<span>{{ globalRowCount }}</span>
</div> --}}
</section>
<section>
<h4 class="divider">Controls</h4>
{{#each controls as | btn |}}
<button
type="button"
data-action="{{ btn.action }}"
>
{{ btn.label }}
</button>
{{/each}}
</section>