2022-11-04 20:41:16 +01:00
|
|
|
import { SYSTEM_RDD } from "./constants.js";
|
|
|
|
import { Grammar } from "./grammar.js";
|
2023-01-10 22:17:20 +01:00
|
|
|
import { HtmlUtility } from "./html-utility.js";
|
2023-01-07 20:06:04 +01:00
|
|
|
import { RdDTimestamp } from "./rdd-timestamp.js";
|
2022-11-04 20:41:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
const LATEST_USED_JOURNAL_ID = "chronologie-dernier-journal";
|
|
|
|
|
|
|
|
export class DialogChronologie extends Dialog {
|
|
|
|
|
2022-11-05 18:06:30 +01:00
|
|
|
static init() {
|
2022-11-04 20:41:16 +01:00
|
|
|
game.settings.register(SYSTEM_RDD, LATEST_USED_JOURNAL_ID, {
|
|
|
|
name: "Dernier article de journal utilisé pour enregistrer la chronologie",
|
|
|
|
scope: "client",
|
|
|
|
config: false,
|
|
|
|
default: "",
|
|
|
|
type: String
|
|
|
|
});
|
|
|
|
}
|
|
|
|
static async create() {
|
|
|
|
const dialogData = {
|
|
|
|
auteur: game.user.name,
|
|
|
|
isGM: game.user.isGM,
|
|
|
|
information: "",
|
|
|
|
journalId: game.settings.get(SYSTEM_RDD, LATEST_USED_JOURNAL_ID),
|
|
|
|
journaux: game.journal.filter(it => it.testUserPermission(game.user, CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER)),
|
2023-01-07 20:06:04 +01:00
|
|
|
timestamp: game.system.rdd.calendrier.timestamp,
|
2022-11-04 20:41:16 +01:00
|
|
|
dateReel: DialogChronologie.getCurrentDateTime()
|
|
|
|
};
|
|
|
|
const html = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/dialog-chronologie.html", dialogData);
|
2023-01-10 22:17:20 +01:00
|
|
|
const dialog = new DialogChronologie(html, dialogData);
|
2022-11-13 21:57:18 +01:00
|
|
|
dialog.render(true);
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 22:17:20 +01:00
|
|
|
static getCurrentDateTime() {
|
|
|
|
return new Date().toLocaleString("sv-SE", {
|
|
|
|
year: "numeric",
|
|
|
|
month: "2-digit",
|
|
|
|
day: "2-digit",
|
|
|
|
hour: "2-digit",
|
|
|
|
minute: "2-digit"
|
|
|
|
}).replace(" ", "T");
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(html, dialogData) {
|
2022-11-04 20:41:16 +01:00
|
|
|
const options = {
|
|
|
|
classes: ["DialogChronologie"],
|
|
|
|
width: 500,
|
2022-11-17 01:23:53 +01:00
|
|
|
height: 'fit-content',
|
2022-11-04 20:41:16 +01:00
|
|
|
'z-index': 99999
|
|
|
|
};
|
2023-01-10 22:17:20 +01:00
|
|
|
const timeData = dialogData.timestamp.toCalendrier()
|
2022-11-04 20:41:16 +01:00
|
|
|
const conf = {
|
2023-01-10 22:17:20 +01:00
|
|
|
title: `Chronologie - ${timeData.jourDuMois} ${timeData.mois.label} - Heure ${timeData.heure.label}`,
|
2022-11-04 20:41:16 +01:00
|
|
|
content: html,
|
|
|
|
buttons: {
|
2022-11-17 01:21:13 +01:00
|
|
|
}
|
2022-11-04 20:41:16 +01:00
|
|
|
};
|
|
|
|
super(conf, options);
|
2023-01-10 22:17:20 +01:00
|
|
|
this.dialogData = dialogData;
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
activateListeners(html) {
|
2022-12-09 02:00:31 +01:00
|
|
|
this.html = html;
|
2023-01-10 22:17:20 +01:00
|
|
|
super.activateListeners(html);
|
|
|
|
this.showChronologiePreset(!game.journal.get(this.dialogData.journalId).canUserModify(game.user))
|
|
|
|
|
|
|
|
this.html.find("a.chronologie-preset-show").click(event => this.showChronologiePreset(true));
|
|
|
|
this.html.find("a.chronologie-preset-hide").click(event => this.showChronologiePreset(false));
|
2023-01-10 22:45:23 +01:00
|
|
|
this.html.find("button.chronologie-ajouter").click(event => this.ajouter());
|
2023-01-10 22:17:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
showChronologiePreset(showPreset) {
|
|
|
|
HtmlUtility.showControlWhen(this.html.find(".chronologie-preset-show"), !showPreset);
|
|
|
|
HtmlUtility.showControlWhen(this.html.find(".chronologie-preset-hide"), showPreset);
|
|
|
|
HtmlUtility.showControlWhen(this.html.find(".chronologie-preset"), showPreset);
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async ajouter() {
|
|
|
|
await this.forceValidation();
|
|
|
|
const { journalId, journalEntry } = this.findJournal();
|
2023-01-10 22:45:23 +01:00
|
|
|
if (journalEntry?.canUserModify(game.user)) {
|
2023-01-10 22:45:56 +01:00
|
|
|
const journalParameters = this.extractJournalParameters();
|
|
|
|
|
|
|
|
const jour = journalParameters.dateRdD.jour;
|
|
|
|
const mois = journalParameters.dateRdD.mois.label;
|
|
|
|
const annee = journalParameters.dateRdD.annee;
|
|
|
|
const section = `${jour} ${mois} ${annee}`
|
|
|
|
const content = await this.prepareChronologieEntry(journalParameters);
|
2023-01-10 22:45:23 +01:00
|
|
|
// ajouter à la page ou créer une page
|
2023-01-10 22:45:56 +01:00
|
|
|
this.addContentToJournal(journalEntry, section, content);
|
2023-01-10 22:45:23 +01:00
|
|
|
this.storeLatestUsedJournalEntry(journalId);
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const journal = this.html.find("form.rdddialogchrono select[name='journalId']").val();
|
|
|
|
ui.notifications.warn(`Le journal ${journal} n'est pas accessible`);
|
|
|
|
}
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async forceValidation() {
|
2022-12-09 02:00:31 +01:00
|
|
|
await this.html.find("form.rdddialogchrono :input").change();
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
findJournal() {
|
2022-12-09 02:00:31 +01:00
|
|
|
const journalId = this.html.find("form.rdddialogchrono :input[name='journalId']").val();
|
2022-11-04 20:41:16 +01:00
|
|
|
const journalEntry = game.journal.get(journalId);
|
|
|
|
return { journalId, journalEntry };
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:45:56 +01:00
|
|
|
async prepareChronologieEntry(journalParameters) {
|
|
|
|
return await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/chronologie-entry.html", journalParameters);
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extractJournalParameters() {
|
|
|
|
return {
|
2022-12-09 02:00:31 +01:00
|
|
|
auteur: this.html.find("form.rdddialogchrono :input[name='auteur']").val(),
|
|
|
|
information: this.html.find("form.rdddialogchrono :input[name='information']").val(),
|
2022-11-04 20:41:16 +01:00
|
|
|
dateRdD: {
|
2023-01-07 20:06:04 +01:00
|
|
|
jour: this.html.find("form.rdddialogchrono :input[name='chronologie.jourDuMois']").val(),
|
|
|
|
mois: RdDTimestamp.definition(this.html.find("form.rdddialogchrono :input[name='chronologie.mois']").val()),
|
|
|
|
annee: this.html.find("form.rdddialogchrono :input[name='chronologie.annee']").val(),
|
|
|
|
heure: RdDTimestamp.definition(this.html.find("form.rdddialogchrono :input[name='chronologie.heure']").val()),
|
|
|
|
minute: this.html.find("form.rdddialogchrono :input[name='chronologie.minute']").val(),
|
2022-11-04 20:41:16 +01:00
|
|
|
},
|
2022-12-09 02:00:31 +01:00
|
|
|
dateReel: this.html.find("form.rdddialogchrono :input[name='dateReel']").val().replace('T', ' ')
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:45:56 +01:00
|
|
|
addContentToJournal(journalEntry, section, content) {
|
|
|
|
let page = journalEntry.pages.find(p => p.type == 'text' && Grammar.equalsInsensitive(p.name, section));
|
2022-11-04 20:41:16 +01:00
|
|
|
if (page) {
|
2023-01-10 22:45:56 +01:00
|
|
|
page.update({ 'text.content': page.text.content + '\n' + content });
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
else {
|
2023-01-10 22:45:56 +01:00
|
|
|
journalEntry.createEmbeddedDocuments('JournalEntryPage', [this.newPageChronologie(section, content)]);
|
2022-11-04 20:41:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 22:45:56 +01:00
|
|
|
newPageChronologie(section, content) {
|
2022-11-04 20:41:16 +01:00
|
|
|
return new JournalEntryPage({
|
2023-01-10 22:45:56 +01:00
|
|
|
name: section,
|
2022-11-04 20:41:16 +01:00
|
|
|
type: 'text',
|
|
|
|
title: { show: true, level: 1 },
|
|
|
|
text: { content: content, format: 1 }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
storeLatestUsedJournalEntry(journalId) {
|
|
|
|
game.settings.set(SYSTEM_RDD, LATEST_USED_JOURNAL_ID, journalId);
|
|
|
|
}
|
|
|
|
}
|