-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f1f692
commit b900c25
Showing
3 changed files
with
99 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,96 @@ | ||
'use strict'; | ||
|
||
import { | ||
addEventObserver, | ||
createElement, | ||
pathQuerySelector, | ||
} from '../common/helpers'; | ||
import { createElement } from '../common/helpers'; | ||
|
||
/** | ||
* Add the equipment autocomplete. | ||
* Create a new Equipment Sheet object. | ||
* | ||
* @param {object} props | ||
* @param {HTMLElement} props.equipmentsContainer - The equipments container in the right side. | ||
* @param {Equipment[]} props.equipments - All available equipments. | ||
* @class | ||
*/ | ||
function loadEquipmentAutoComplete({ equipmentsContainer, equipments }) { | ||
if (!equipmentsContainer.querySelector('#equipment-list')) { | ||
equipmentsContainer.append( | ||
createElement('datalist', { | ||
id: 'equipment-list', | ||
append: equipments.map((equipment) => | ||
createElement('option', { value: equipment.name }), | ||
), | ||
}), | ||
export class EquipmentSheet { | ||
/** | ||
* @constructs | ||
* @param {Object} props | ||
* @param {EnhancedHTMLElement} props.iframe | ||
* @param {EquipmentData[]} props.equipments | ||
* @param {Object} props.character | ||
*/ | ||
constructor({ iframe, equipments, character }) { | ||
/** @type {EnhancedHTMLElement} */ | ||
this.iframe = iframe; | ||
/** @type {EquipmentData[]} */ | ||
this.equipments = equipments; | ||
/** @type {Equipment[]} */ | ||
this.equipmentsList = equipments.reduce( | ||
(acc, folder) => [...acc, ...folder.items], | ||
[], | ||
); | ||
/** @type {Object} */ | ||
this.character = character; | ||
/** | ||
* @type {EnhancedHTMLElement|null} | ||
* @private | ||
*/ | ||
this._equipmentsContainer = null; | ||
} | ||
|
||
/** @type {EnhancedHTMLElement|null} */ | ||
get equipmentsContainer() { | ||
if (this._equipmentsContainer === null) { | ||
const path = [ | ||
'div.sheet-right-container', | ||
'div.sheet-equipment-container', | ||
'div[data-groupname="repeating_equipment"]', | ||
]; | ||
this._equipmentsContainer = this.iframe.getElement(path); | ||
} | ||
return this._equipmentsContainer; | ||
} | ||
for (const input of equipmentsContainer.querySelectorAll( | ||
'input[name="attr_equipname"]:not([list="equipment-list"])', | ||
)) { | ||
input.setAttribute('list', 'equipment-list'); | ||
input.autocomplete = 'off'; | ||
const updateSpacesValue = () => { | ||
const equipment = equipments.find( | ||
(equipment) => equipment.name === input.value, | ||
|
||
/** Load the sheet equipment improvements. */ | ||
load() { | ||
this.loadAutoComplete(); | ||
} | ||
|
||
/** Load the sheet equipment auto complete. */ | ||
loadAutoComplete() { | ||
if (!this.equipmentsContainer.querySelector('#equipment-list')) { | ||
this.equipmentsContainer.append( | ||
createElement('datalist', { | ||
id: 'equipment-list', | ||
append: this.equipmentsList.map((equipment) => | ||
createElement('option', { value: equipment.name }), | ||
), | ||
}), | ||
); | ||
if (equipment) { | ||
const value = | ||
parseFloat(equipment.spaces.replace(',', '.').trim()) || 0; | ||
const spacesInput = input.parentNode.querySelector( | ||
'input[name="attr_equipweight"],input[name="attr_equipslot"]', | ||
} | ||
const selector = | ||
'input[name="attr_equipname"]:not([list="equipment-list"])'; | ||
for (const input of this.equipmentsContainer.getAllElements(selector)) { | ||
input.setAttribute('list', 'equipment-list'); | ||
input.autocomplete = 'off'; | ||
const repRow = input.parentNode.parentNode; | ||
const id = repRow.getAttribute('data-reprowid'); | ||
const updateSpacesValue = () => { | ||
const equipment = this.equipmentsList.find( | ||
(equipment) => equipment.name === input.value, | ||
); | ||
spacesInput.focus(); | ||
spacesInput.value = value; | ||
input.focus(); | ||
} | ||
}; | ||
addEventObserver({ | ||
el: input, | ||
eventName: 'input', | ||
eventHandler: updateSpacesValue, | ||
}); | ||
addEventObserver({ | ||
el: input, | ||
eventName: 'change', | ||
eventHandler: updateSpacesValue, | ||
}); | ||
if (equipment) { | ||
const prefix = `repeating_equipment_${id}`; | ||
const rawValue = equipment.spaces.replace(',', '.').trim(); | ||
const value = parseFloat(rawValue) || 0; | ||
const spacesInput = input.parentNode.querySelector( | ||
'input[name="attr_equipweight"],input[name="attr_equipslot"]', | ||
); | ||
const attrName = spacesInput.getAttribute('name').split('_')[1]; | ||
this.character.updateAttributes(prefix, { | ||
equipname: equipment.name, | ||
[attrName]: value, | ||
}); | ||
} | ||
}; | ||
input.addEventObserver('input', updateSpacesValue); | ||
input.addEventObserver('change', updateSpacesValue); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Load the equipment related enhancements. | ||
* | ||
* @param {object} props | ||
* @param {HTMLDocument} props.iframe - The character sheet iframe document. | ||
* @param {T20Data} props.data - The Tormenta20 data. | ||
*/ | ||
export function loadEquipmentEnhancement({ iframe, data }) { | ||
const equipmentsContainer = pathQuerySelector({ | ||
root: iframe, | ||
path: [ | ||
'div.sheet-right-container', | ||
'div.sheet-equipment-container', | ||
'div[data-groupname="repeating_equipment"]', | ||
], | ||
}); | ||
loadEquipmentAutoComplete({ | ||
equipmentsContainer, | ||
equipments: data.equipments.reduce( | ||
(acc, folder) => [...acc, ...folder.items], | ||
[], | ||
), | ||
}); | ||
} |