Skip to content

Commit

Permalink
fix up getLabelMap
Browse files Browse the repository at this point in the history
  • Loading branch information
esheyw committed Jan 20, 2024
1 parent 85f4048 commit 555e209
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- add some debug to `applyOwnershipToFolderStructure()`
- fixed data path in `setInitiativeStatistic()`

## Release 0.1.4
## Release 0.2.0
- add `updateInitiativeSkillsDialog()` macro
- add `MHDialog` class
- add `MHDialog` class
- various tidyup
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ Supports passing a `validator` property along with the dialog data. This can eit
- An array of strings equating to the `name`s of form elements that are not allowed to be empty
- A single string `name` (gets put into an array and treated as above)
If passed either non-function option, the default validator will produce a banner if validation is failed: ![](https://i.imgur.com/EfbNTWE.png)

#### Static methods `MHLDialog.getFormData(html)` and `MHLDialog.getFormData(html)`
`getFormsData` takes in html (or jQuery), and, for each form in the data, runs that form through `new FormDataExtended`, and assigns the output to an object, with the key of the form's name, eg:
```js
Expand All @@ -140,3 +139,15 @@ If passed either non-function option, the default validator will produce a banne
```
If there is more than one form in `html`, and any forms lack a `name` attribute, will error. If there's only one form, if that form lacks a `name` attribute it will be default to just 'form'.
`getFormData` just calls `getFormsData` and returns the first form's data; the only difference between it and simple `(html) => new FormDataExtended(html).object` is `getFormsData`'s handling for multiple forms. Either function is suitable as a callback if you'd like to simple dump the form output and handle that separately (my preference over having all the logic in the callback).
#### Static method `getLabelMap(html)`
As above, takes html/jQuery, returns an object of all valid name/label pairs. That is, for:
- `<label>`s with valid `for` attributes that point to something that has an `id` matching the `for`, *and* a `name`, and
- `<label>`s containing a labelable element (`button`, `input`, `meter`, `output`, `progress`, `select`, `textarea`) that have a `name`,
it will produce an object like:
```js
{
"name1":"Label for input name1",
//etc
}
```
label text is acquired via `.innerText`, so may require trimming before use.
22 changes: 14 additions & 8 deletions scripts/classes/MHLDialog.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { COLOURS, fu } from "../constants.mjs";
import { COLOURS, LABELABLE_TAGS, fu } from "../constants.mjs";
import { MHLError, isEmpty, localizedBanner, mhlog } from "../helpers/errorHelpers.mjs";
import { localize } from "../helpers/stringHelpers.mjs";
const PREFIX = "MHL.Dialog";
Expand Down Expand Up @@ -124,13 +124,19 @@ export class MHLDialog extends Dialog {
const named = html.querySelectorAll("[name][id]");
if (!named.length) return {};
const namedIDs = Array.from(named).map((e) => e.getAttribute("id"));
const labels = html.querySelectorAll("[for]");
if (!labels.length) return {};
return Array.from(labels).reduce((acc, curr) => {
const forAttr = curr.getAttribute("for");
if (!namedIDs.includes(forAttr)) return acc;
acc[forAttr] = curr.innerText;
return acc;
const allLabels = Array.from(html.querySelectorAll('label'));
if (!allLabels.length) return {};
return allLabels.reduce((acc, curr) => {
const forAttr = curr.getAttribute("for");
if (forAttr) {
if (!namedIDs.includes(forAttr)) return acc;
acc[curr.getAttribute('name')] = curr.innerText;
} else {
const labelableChild = curr.querySelector(LABELABLE_TAGS.map(t=>`${t}[name]`).join(', '));
if (!labelableChild) return acc;
acc[labelableChild.getAttribute('name')] = curr.innerText;
}
return acc;
}, {});
}
}
22 changes: 16 additions & 6 deletions scripts/constants.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
export const MODULE = 'pf2e-macro-helper-library';
export const PHYSICAL_ITEM_TYPES = ["armor", "backpack", "book", "consumable", "equipment", "shield", "treasure", "weapon"];
export const MODULE = "pf2e-macro-helper-library";
export const PHYSICAL_ITEM_TYPES = [
"armor",
"backpack",
"book",
"consumable",
"equipment",
"shield",
"treasure",
"weapon",
];
export const fu = foundry.utils;
export const CONSOLE_TYPES = ["debug","info","warn","error"];
export const BANNER_TYPES = CONSOLE_TYPES.slice(1)
export const CONSOLE_TYPES = ["debug", "info", "warn", "error"];
export const BANNER_TYPES = CONSOLE_TYPES.slice(1);
export const COLOURS = {
error: 'var(--color-level-error, red)'
}
error: "var(--color-level-error, red)",
};
export const LABELABLE_TAGS = ["button", "input", "meter", "output", "progress", "select", "textarea"];

0 comments on commit 555e209

Please sign in to comment.