Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion js/5etools-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ function d20plusImporter () {

// init list library
const importList = new List("import-list", {
valueNames: options.listIndex || ["name"],
valueNames: options.listIndex || ["name", "source"],
});

// reset the UI and add handlers
Expand All @@ -730,6 +730,13 @@ function d20plusImporter () {
d20plus.importer._importSelectPublished(importList);
});

$("#importlist-filter").bind("click", () => {
d20plus.importer._importFilterList(importList);
});
$("#importlist-reset").bind("click", () => {
d20plus.importer._importResetList(importList);
});

if (options.listIndexConverter) {
const $iptFilter = $(`#import-list-filter`).show();
$(`#import-list-filter-help`).show();
Expand Down Expand Up @@ -1213,6 +1220,60 @@ function d20plusImporter () {
});
};

d20plus.importer._importFilterList = function (importList) {
const $winFilterList = $("#d20plus-import-filter-list");
$winFilterList.dialog("open");
const $btnImport = $winFilterList.find(".btn");
const $winText = $winFilterList.find(".table-import-textarea");

$btnImport.on("click", () => {

function filterList () {
const toSearch = $winText.val().toLowerCase().replaceAll(`"`, "").split("\n");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$winText.val().toLowerCase().replaceAll(", "").split("\n"); ->

$winText.val().toLowerCase().replaceAll(`"`, "").split("\n").map(it => it.trim()).filter(Boolean);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have quotes in the input, we should respect them; e.g. "Namedcreature, Bringer of Fun","QWE" should be parsed as name=Namedcreature, Bringer of Fun source=QWE; currently this parses it as name=Namedcreature source=Bringer of Fun?

some generic CSV parsing ought to do the trick; should be simple enough to put in a utility function for it and call it good

const firstLine = toSearch[0];
const filterUnofficial = !d20plus.cfg.getOrDefault("import", "allSourcesIncludeUnofficial");

// If no search terms are entered, reset the filter
if (toSearch.length == 1 && firstLine == '') {
importList.filter();
return;
}

const searchDict = {};
toSearch.forEach(it => {
const items = it.split(",");
// If additional filters are added, they can go here
searchDict[items[0].trim()] = {
"name": items[0].trim(),
"source": items.length > 1 ? items[1].trim() : null,
// Some categories list their source in this format
"altsource": items.length > 1 ? `src[${items[1].trim()}]`: null,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make everything uniformly use one style, then remove this workaround

}
})

// Filters to match names and sources on the list
x = importList
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

junk line(?)

importList.filter(it => {
const name = it._values.name.toLowerCase();
const source = it._values.source.toLowerCase();

if (!(name in searchDict)) return false;
if (!searchDict[name].source) return true;
if (searchDict[name].source === source || searchDict[name].altsource === source) return true;
return false;
});
}

filterList();

$winFilterList.dialog("close");
}).appendTo($winFilterList);
};

d20plus.importer._importResetList = function (importList) {
importList.filter();
};

d20plus.importer.CharacterAttributesProxy = class {
constructor (character) {
this.character = character;
Expand Down
34 changes: 34 additions & 0 deletions js/5etools-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ const d20plusTemplate = function () {
$body.append(d20plus.template5e.importDialogHtml);
$body.append(d20plus.template5e.importListHTML);
$body.append(d20plus.template5e.importListPropsHTML);
$body.append(d20plus.template5e.importFilterList);
$("#d20plus-import").dialog({
autoOpen: false,
resizable: false,
Expand All @@ -223,6 +224,12 @@ const d20plusTemplate = function () {
width: 300,
height: 600,
});
$("#d20plus-import-filter-list").dialog({
autoOpen: false,
resizable: true,
width: 640,
height: 540,
});

// add class subclasses to the subclasses dropdown(s)
d20plus.template5e._populateDropdown("#button-subclasses-select", "#import-subclasses-url", CLASS_DATA_DIR, classDataUrls, "", ["class"]);
Expand Down Expand Up @@ -321,6 +328,9 @@ ${finalText}
<button type="button" id="importlist-deselectvis" class="btn" style="margin: 0 2px;"><span>Deselect Visible</span></button>
<span style="width:1px;background: #bbb;height: 26px;margin: 2px;"></span>
<button type="button" id="importlist-selectall-published" class="btn" style="margin: 0 2px;"><span>Select All Published</span></button>
<span style="width:1px;background: #bbb;height: 26px;margin: 2px;"></span>
<button type="button" id="importlist-filter" class="btn" style="margin: 0 2px;"><span>Filter By List</span></button>
<button type="button" id="importlist-reset" class="btn" style="margin: 0 2px;"><span>Reset Filters</span></button>
</p>
<p>
<span id="import-list">
Expand Down Expand Up @@ -351,6 +361,30 @@ ${finalText}
<br>
<button type="button" id="save-import-props" class="btn" role="button" aria-disabled="false">Save</button>
</p>
</div>`;

d20plus.template5e.importFilterList = `<div id="d20plus-import-filter-list" title="List Items to Filter For">
<p>
Write down a vertical list of the items you want to filter and then <b>Filter List > Select Visible > Import</b>
<span title="If an item is in more than one source (like those that get reprinted or appear in UAs), you will get multiple results out of the filter for it unless you indicate a source." style="cursor: help;">[!]</span>.
</p>
<p>
You can either manually write down your list, or you can go to the corresponding page in <b>5etools > make your list > Table View > Copy CSV to Clipboard > Paste it here > Filter List</b>
<span title="Don't worry if there is more info that is needed, it'll get discarded, as well as any failed match. Caps are optional as well." style="cursor: help;">[?]</span>.
</p>
<textarea placeholder=
'List 1 item per line. There are 3 ways of listing things (you can combine them):

Fireball
Fireball, PHB
"Fireball","PHB"

etc.
(Source is optional, as you can see in the 1st example, but it is recommended)
(the 2nd and 3rd example show the 2 ways to indicate source)'
class="table-import-textarea">
</textarea>
<button class="btn">Filter List</button>
</div>`;

d20plus.template5e.importDialogHtml = `<div id="d20plus-import" title="Importing">
Expand Down