-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from foundryvtt/containers
[#729] Add container support to sidebar & compendiums
- Loading branch information
Showing
12 changed files
with
174 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export {default as ContainerSheet} from "./container-sheet.mjs"; | ||
export {default as ItemCompendium5e} from "./item-compendium.mjs"; | ||
export {default as ItemDirectory5e} from "./item-directory.mjs"; | ||
export {default as ItemSheet5e} from "./item-sheet.mjs"; | ||
|
||
export {default as AbilityUseDialog} from "./ability-use-dialog.mjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Item5e from "../../documents/item.mjs"; | ||
|
||
/** | ||
* Compendium with added support for item containers. | ||
*/ | ||
export default class ItemCompendium5e extends Compendium { | ||
|
||
/** @inheritdoc */ | ||
async _render(...args) { | ||
await super._render(...args); | ||
|
||
if ( this.collection.index ) { | ||
if ( !this.collection._reindexing ) this.collection._reindexing = this.collection.getIndex(); | ||
await this.collection._reindexing; | ||
items = this.collection.index; | ||
} | ||
for ( const item of items ) { | ||
if ( items.has(item.system.container) ) this._element[0].querySelector(`[data-entry-id="${item._id}"]`)?.remove(); | ||
} | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritdoc */ | ||
async _handleDroppedEntry(target, data) { | ||
// Obtain the dropped Document | ||
let item = await Item.fromDropData(data); | ||
if ( !item ) return; | ||
|
||
// Create item and its contents if it doesn't already exist here | ||
if ( !this._entryAlreadyExists(item) ) { | ||
const contents = await item.system.contents; | ||
if ( contents?.size ) { | ||
const toCreate = await Item5e.createWithContents([item], {transformAll: item => item.toCompendium(item)}); | ||
[item] = await Item5e.createDocuments(toCreate, {pack: this.collection.collection, keepId: true}); | ||
} | ||
} | ||
|
||
// Otherwise, if it is within a container, take it out | ||
else if ( item.system.container ) await item.update({"system.container": null}); | ||
|
||
// Let parent method perform sorting | ||
super._handleDroppedEntry(target, item.toDragData()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Item5e from "../../documents/item.mjs"; | ||
|
||
/** | ||
* Items sidebar with added support for item containers. | ||
*/ | ||
export default class ItemDirectory5e extends ItemDirectory { | ||
/** @inheritdoc */ | ||
async _handleDroppedEntry(target, data) { | ||
// Obtain the dropped Document | ||
let item = await this._getDroppedEntryFromData(data); | ||
if ( !item ) return; | ||
|
||
// Create item and its contents if it doesn't already exist here | ||
if ( !this._entryAlreadyExists(item) ) { | ||
const toCreate = await Item5e.createWithContents([item]); | ||
[item] = await Item5e.createDocuments(toCreate, {keepId: true}); | ||
} | ||
|
||
// Otherwise, if it is within a container, take it out | ||
else if ( item.system.container ) await item.update({"system.container": null}); | ||
|
||
// Let parent method perform sorting | ||
super._handleDroppedEntry(target, item.toDragData()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {default as Items5e} from "./items-collection.mjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Item5e from "../../documents/item.mjs"; | ||
|
||
/** | ||
* Custom items collection to hide items in containers automatically. | ||
*/ | ||
export default class Items5e extends Items { | ||
/** @override */ | ||
_getVisibleTreeContents(entry) { | ||
return this.contents.filter(c => c.visible && !this.has(c.system?.container)); | ||
} | ||
|
||
/* -------------------------------------------- */ | ||
|
||
/** @inheritdoc */ | ||
async importFromCompendium(pack, id, updateData={}, options={}) { | ||
const created = await super.importFromCompendium(pack, id, updateData, options); | ||
|
||
const item = await pack.getDocument(id); | ||
const contents = await item.system.contents; | ||
if ( contents ) { | ||
const toCreate = await Item5e.createWithContents(contents, { | ||
container: created, keepId: options.keepId, transformAll: item => this.fromCompendium(item, options) | ||
}); | ||
await Item5e.createDocuments(toCreate, {fromCompendium: true, keepId: true}); | ||
} | ||
|
||
return created; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters