Recherche dans l'inventaire

Par nom ou par type, et dans les contenants
This commit is contained in:
2023-02-05 20:26:44 +01:00
parent 703ab2579d
commit fbcc167272
9 changed files with 86 additions and 50 deletions

View File

@ -66,9 +66,9 @@ export class RdDActorSheet extends RdDBaseActorSheet {
formData.calc.fatigue = RdDUtility.calculFatigueHtml(formData.system.sante.fatigue.value, formData.system.sante.endurance.max);
formData.competences.forEach(item => {
item.system.isVisible = this.options.recherche
? RdDItemCompetence.nomContientTexte(item, this.options.recherche.text)
: (!this.options.showCompNiveauBase || !RdDItemCompetence.isNiveauBase(item));
item.system.isHidden = this.options.recherche
? !item.isNomLike(this.options.recherche.text)
: (this.options.showCompNiveauBase && RdDItemCompetence.isNiveauBase(item));
RdDItemCompetence.levelUp(item, formData.system.compteurs.experience.value);
});
@ -354,29 +354,6 @@ export class RdDActorSheet extends RdDBaseActorSheet {
this.render(true);
});
this.html.find('.recherche')
.each((index, field) => {
if (this.options.recherche) {
field.focus();
field.setSelectionRange(this.options.recherche.start, this.options.recherche.end);
}
})
.keyup(async event => {
const nouvelleRecherche = this._optionRecherche(event.currentTarget);
if (this.options.recherche?.text != nouvelleRecherche?.text) {
this.options.recherche = nouvelleRecherche;
if (this.timerRecherche) {
clearTimeout(this.timerRecherche);
}
this.timerRecherche = setTimeout(() => {
this.timerRecherche = undefined;
this.render(true);
}, 500);
}
})
.change(async event =>
this.options.recherche = this._optionRecherche(event.currentTarget)
);
this.html.find('.vue-detaillee').click(async event => {
this.options.vueDetaillee = !this.options.vueDetaillee;
this.render(true);
@ -486,16 +463,6 @@ export class RdDActorSheet extends RdDBaseActorSheet {
async createEmptyTache() {
await this.actor.createItem('tache', 'Nouvelle tache');
}
_optionRecherche(target) {
if (!target.value?.length) {
return undefined;
}
return {
text: target.value,
start: target.selectionStart,
end: target.selectionEnd,
};
}
_getEventArmeCombat(event) {
const li = this.html.find(event.currentTarget)?.parents(".item");

View File

@ -52,10 +52,30 @@ export class RdDBaseActorSheet extends ActorSheet {
this.objetVersConteneur = RdDUtility.buildArbreDeConteneurs(formData.conteneurs, formData.objets);
formData.conteneurs = RdDUtility.conteneursRacine(formData.conteneurs);
this._appliquerRechercheObjets(formData.objets, formData.conteneurs, this.options.recherche);
return formData;
}
_appliquerRechercheObjets(objets, conteneurs, recherche) {
if (recherche) {
this._setConteneursVisibles(objets, conteneurs);
}
}
_setConteneursVisibles(objets, conteneurs) {
const recherche = this.options.recherche;
const allVisible = objets.filter(it => it.isNomTypeLike(recherche.text)).map(it => it.id);
let addVisible = conteneurs.filter(it => it.isNomTypeLike(recherche.text)).map(it => it.id)
do {
allVisible.push(...addVisible)
const parentsIds = conteneurs.filter(it => it.system.contenu.find(id => allVisible.includes(id))).map(it => it.id)
addVisible = parentsIds.filter(id => !allVisible.includes(id))
}
while (addVisible.length > 0)
objets.forEach(it => it.system.isHidden = !allVisible.includes(it.id))
conteneurs.forEach(it => it.system.isHidden = !allVisible.includes(it.id))
}
/* -------------------------------------------- */
static filterItemsPerTypeForSheet(formData, itemTypes) {
formData.recettescuisine = Misc.arrayOrEmpty(itemTypes['recettecuisine']);
@ -140,6 +160,38 @@ export class RdDBaseActorSheet extends ActorSheet {
this.html.find('.monnaie-moins').click(async event => {
this.actor.monnaieIncDec(this.getItemId(event), -1);
});
this.html.find('.recherche')
.each((index, field) => {
this._rechercheSelectArea(field);
})
.keyup(async event => {
this._rechercherKeyup(event);
})
.change(async event =>
this.options.recherche = this._optionRecherche(event.currentTarget)
);
}
_rechercherKeyup(event) {
const currentTarget = event.currentTarget;
const nouvelleRecherche = this._optionRecherche(currentTarget);
if (this.options.recherche?.text != nouvelleRecherche?.text) {
this.options.recherche = nouvelleRecherche;
if (this.timerRecherche) {
clearTimeout(this.timerRecherche);
}
this.timerRecherche = setTimeout(() => {
this.timerRecherche = undefined;
this.render(true);
}, 500);
}
}
_rechercheSelectArea(field) {
if (this.options.recherche) {
field.focus();
field.setSelectionRange(this.options.recherche.start, this.options.recherche.end);
}
}
getItemId(event) {
@ -150,6 +202,16 @@ export class RdDBaseActorSheet extends ActorSheet {
return RdDSheetUtility.getItem(event, this.actor);
}
_optionRecherche(target) {
if (!target.value?.length) {
return undefined;
}
return {
text: target.value,
start: target.selectionStart,
end: target.selectionEnd,
};
}
/* -------------------------------------------- */
_getHeaderButtons() {
let buttons = super._getHeaderButtons();

View File

@ -190,15 +190,6 @@ export class RdDItemCompetence extends Item {
}
}
/* -------------------------------------------- */
static isVisible(item) {
return Number(item.system.niveau) != RdDItemCompetence.getNiveauBase(item.system.categorie);
}
static nomContientTexte(item, texte) {
return Grammar.toLowerCaseNoAccent(item.name).includes(Grammar.toLowerCaseNoAccent(texte))
}
/* -------------------------------------------- */
static isNiveauBase(item) {
return Number(item.system.niveau) == RdDItemCompetence.getNiveauBase(item.system.categorie);
@ -279,7 +270,7 @@ export class RdDItemCompetence extends Item {
/* -------------------------------------------- */
static triVisible(competences) {
return competences.filter(it => it.system.isVisible)
return competences.filter(it => !it.system.isHidden)
.sort((a, b) => RdDItemCompetence.compare(a, b))
}

View File

@ -304,6 +304,13 @@ export class RdDItem extends Item {
return this.parent?.type == 'commerce';
}
isNomLike(texte) {
return Grammar.includesLowerCaseNoAccent(this.name, texte)
}
isNomTypeLike(texte) {
return this.isNomLike(texte) || Grammar.includesLowerCaseNoAccent(Misc.typeName(this.type, 'Item'), texte)
}
getQuantite() {
return this.isService() ? undefined : Math.round(this.system.quantite ?? 0)
}