Skip to content

Commit

Permalink
Merge pull request #46 from League-of-Foundry-Developers/45-auto-open…
Browse files Browse the repository at this point in the history
…-document
  • Loading branch information
cswendrowski authored Jun 13, 2022
2 parents 334ab3e + 4811716 commit 14bdfa3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions foundryvtt-devMode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { setupJSONDiff } from './module/hooks/json-changes.mjs';
import { _devModeDisplayUsabilityErrors } from './module/patches/displayUsabilityErrors.mjs';
import setupDisableTemplateCache from './module/patches/getTemplate.mjs';
import { libWrapper } from './module/shim.mjs';
import autoOpenDocuments from "./module/hooks/auto-open-documents.mjs";

Handlebars.registerHelper('dev-concat', (...args) => {
DevMode.log(false, args);
Expand Down Expand Up @@ -68,4 +69,8 @@ Hooks.on('ready', () => {
inspectSystemTemplate();

setupApplicationHeaderPrintButton();

// If Vueport is enabled, it needs a little bit to be ready to render a sheet
if ( game.modules.get("vueport").active ) setTimeout(autoOpenDocuments, 1000);
else autoOpenDocuments();
});
4 changes: 4 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"failed": "Could not copy to Clipboard"
},
"configMenu": {
"AutoOpen": "Auto Open Documents",
"FormTitle": "Debug Flag Config",
"ConfigDebug": "CONFIG.debug Overrides",
"PackageSpecific": "Package Specific Debugging",
Expand All @@ -16,6 +17,9 @@
"DebugMode": "Enable Debug Mode",
"Hint": "Packages can register a debug mode setting which will appear below. Checking it or setting a log level will enable more detailed output in the console.",
"None": "None",
"autoOpen": {
"hint": "Register Documents that you want to automatically open Sheets for on World load here."
},
"performance": {
"run": "Run",
"hint": "This is a set of experimental tests for gauging how well your world/server performs. It can be useful to compare against different configurations on the same hardware (e.g. with and without a module on, etc).",
Expand Down
1 change: 1 addition & 0 deletions module/classes/DevMode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class DevMode {
jsonDiffSystem: 'json-diff-system',
jsonDiffModules: 'json-diff-modules',
inspectTemplate: 'inspect-system-template',
autoOpenDocuments: 'auto-open-documents'
};

static TEMPLATES = {
Expand Down
24 changes: 24 additions & 0 deletions module/classes/DevModeConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class DevModeConfig extends FormApplication {
return isObjectEmpty(settings) ? CONFIG.compatibility : settings;
}

get autoOpenDocuments() {
return game.settings.get(DevMode.MODULE_ID, DevMode.SETTINGS.autoOpenDocuments);
}

/**
* A helper to generate the names and hints of any debug key which has those defined
*
Expand Down Expand Up @@ -182,13 +186,18 @@ export class DevModeConfig extends FormApplication {
types[type] = `ACTOR.Type${type.capitalize()}`;
return types;
}, {}),
autoOpenDocuments: this.autoOpenDocuments,
documentsWithSheets: CONST.DOCUMENT_TYPES.filter(x => Object.values(CONFIG[x].sheetClasses)
.find(sc => !foundry.utils.isObjectEmpty(sc)))
.reduce((types, type) => { types[type] = type; return types; }, {}),
compatibilityWarningsData,
};

DevMode.log(false, data, {
debugOverrides: this.debugOverrides,
packageSpecificDebug: this.packageSpecificDebug,
compatibilityWarnings: this.compatibilityWarnings,
autoOpenDocuments: this.autoOpenDocuments,
});
return data;
}
Expand Down Expand Up @@ -216,6 +225,21 @@ export class DevModeConfig extends FormApplication {
return;
}
}

if (event.currentTarget?.dataset?.action === "addAutoOpen" || event.currentTarget?.dataset?.action === "deleteAutoOpen") {
event.preventDefault();
const isDeleting = event.currentTarget?.dataset?.action === "deleteAutoOpen";
const type = event.currentTarget.parentElement.querySelector('[name="autoOpen.type"]').value;
const id = event.currentTarget.parentElement.querySelector('[name="autoOpen.id"]').value;
const element = { type, id };
let autoOpen = [...this.autoOpenDocuments];
if ( isDeleting ) {
autoOpen = autoOpen.filter(x => !(x.type == element.type && x.id == element.id));
}
else autoOpen.push(element);
game.settings.set(DevMode.MODULE_ID, DevMode.SETTINGS.autoOpenDocuments, autoOpen);
this.render();
}
});
}

Expand Down
8 changes: 8 additions & 0 deletions module/classes/DevModeSettings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ export class DevModeSettings {
onChange: () => DevMode.setCompatibilityWarnings(),
});

// register the setting where we'll store all the documents to auto-open
game.settings.register(DevMode.MODULE_ID, DevMode.SETTINGS.autoOpenDocuments, {
default: [],
type: Object,
scope: 'client',
config: false,
});

this.booleanSettings.forEach(({ key, ...rest }) => {
game.settings.register(DevMode.MODULE_ID, key, {
name: `${DevMode.MODULE_ABBREV}.settings.${key}.Name`,
Expand Down
20 changes: 20 additions & 0 deletions module/hooks/auto-open-documents.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DevMode } from '../classes/DevMode.mjs';

/**
* Automatically open registered Documents
*
*/
export default function autoOpenDocuments() {
const registered = game.settings.get(DevMode.MODULE_ID, DevMode.SETTINGS.autoOpenDocuments);

for ( const document of registered ) {
const collectionName = foundry.documents["Base" + document.type].collectionName;
const collection = game[collectionName];
const toRender = collection.get(document.id);
if ( !toRender ) {
DevMode.log(false, `Could not find ${document.type} with ID ${document.id}`);
continue;
}
toRender.sheet.render(true);
}
}
28 changes: 28 additions & 0 deletions templates/settings.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
{{#if compatibilityWarningsData.enabled}}
<a class='item' data-tab='compatibility'>{{localize 'DEV.configMenu.CompatibilityWarnings'}}</a>
{{/if}}
<a class="item" data-tab='auto-open'>{{localize 'DEV.configMenu.AutoOpen'}}</a>
</nav>

<section class='tab' data-tab='config'>
Expand Down Expand Up @@ -135,6 +136,33 @@
</div>
</section>
{{/if}}

<section class='tab auto-open' data-tab='auto-open'>
<p class="notes">{{localize 'DEV.configMenu.autoOpen.hint'}}</p>

<div class="flexrow">
<b>{{localize "Type"}}</b>
<b>{{localize "Id"}}</b>
</div>

{{#each this.autoOpenDocuments as |autoopen a|}}
<div class='flexrow form-group add-auto-open'>
<select name='autoOpen.type' data-dtype='String' disabled>
<option value="{{autoopen.type}}" selected>{{autoopen.type}}</option>
</select>
<input name='autoOpen.id' data-dtype='String' value="{{autoopen.id}}" disabled />
<button class="delete-auto-open-action" data-action="deleteAutoOpen">Delete</button>
</div>
{{/each}}

<div class='flexrow form-group add-auto-open'>
<select name='autoOpen.type' data-dtype='String'>
{{selectOptions documentsWithSheets localize=true}}
</select>
<input name='autoOpen.id' data-dtype='String' />
<button class="add-auto-open-action" data-action="addAutoOpen">Add</button>
</div>
</section>
</div>

</form>

0 comments on commit 14bdfa3

Please sign in to comment.