Skip to content

Commit

Permalink
Merge pull request #302 from DC23/276-option-to-use-dekads-10-days-pe…
Browse files Browse the repository at this point in the history
…riod-rather-than-weeks-7-days-period

Implemented support for variable length weeks with customisable names of the days.
  • Loading branch information
DC23 authored Dec 15, 2024
2 parents b54c0ed + 0fa6ce8 commit f8ef115
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 30 deletions.
14 changes: 12 additions & 2 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
"JDTIMEKEEPING.WeekName": "Week",

"JDTIMEKEEPING.Settings.WeekdayConfig.name": "Configure the Days of the Week",
"JDTIMEKEEPING.Settings.WeekdayConfig.label": "Rename Weekdays",
"JDTIMEKEEPING.Settings.WeekdayConfig.label": "Weekday Settings",
"JDTIMEKEEPING.Settings.RenameWeekdays": "Rename Weekdays",
"JDTIMEKEEPING.Settings.WeekdayConfig.hint": "",

"JDTIMEKEEPING.longTimeFormat": "{time}, {dayName} {weekName} {weekNumber}",
Expand All @@ -120,5 +121,14 @@
"JDTIMEKEEPING.Settings.UIFocusOpacity.name": "UI Transparency",
"JDTIMEKEEPING.Settings.UIFocusOpacity.hint": "Controls the UI transparency when in use. 0 is fully hidden, and 1 is fully visible.",
"JDTIMEKEEPING.Settings.UIBackgroundColor.name": "UI Background Color",
"JDTIMEKEEPING.Settings.UIBackgroundColor.hint": "Used on both the docked and floating panels"
"JDTIMEKEEPING.Settings.UIBackgroundColor.hint": "Used on both the docked and floating panels",
"JDTIMEKEEPING.Settings.DaysInWeek": "Number of Days in a Week",
"JDTIMEKEEPING.Settings.WeekName.label": "Name of the Week",
"JDTIMEKEEPING.EightDay": "Eightday",
"JDTIMEKEEPING.NineDay": "Nineday",
"JDTIMEKEEPING.TenDay": "Tenday",
"JDTIMEKEEPING.ElevenDay": "Elevenday",
"JDTIMEKEEPING.TwelveDay": "Twelveday",
"JDTIMEKEEPING.ThirteenDay": "Thirteenday",
"JDTIMEKEEPING.FourteenDay": "Fourteenday"
}
14 changes: 12 additions & 2 deletions lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
"JDTIMEKEEPING.WeekName": "Semaine",

"JDTIMEKEEPING.Settings.WeekdayConfig.name": "Configurer les jours de la semaine",
"JDTIMEKEEPING.Settings.WeekdayConfig.label": "Renommer les jours de la semaine",
"JDTIMEKEEPING.Settings.WeekdayConfig.label": "Weekday Settings",
"JDTIMEKEEPING.Settings.RenameWeekdays": "Renommer les jours de la semaine",
"JDTIMEKEEPING.Settings.WeekdayConfig.hint": "",

"JDTIMEKEEPING.longTimeFormat": "{dayName}, {time} | {weekName}: {weekNumber}",
Expand All @@ -120,5 +121,14 @@
"JDTIMEKEEPING.Settings.UIFocusOpacity.name": "UI Transparency",
"JDTIMEKEEPING.Settings.UIFocusOpacity.hint": "Controls the UI transparency when in use. 0 is fully hidden, and 1 is fully visible.",
"JDTIMEKEEPING.Settings.UIBackgroundColor.name": "UI Background Color",
"JDTIMEKEEPING.Settings.UIBackgroundColor.hint": "Used on both the docked and floating panels"
"JDTIMEKEEPING.Settings.UIBackgroundColor.hint": "Used on both the docked and floating panels",
"JDTIMEKEEPING.Settings.DaysInWeek": "Number of Days in a Week",
"JDTIMEKEEPING.Settings.WeekName.label": "Name of the Week",
"JDTIMEKEEPING.EightDay": "Eightday",
"JDTIMEKEEPING.NineDay": "Nineday",
"JDTIMEKEEPING.TenDay": "Tenday",
"JDTIMEKEEPING.ElevenDay": "Elevenday",
"JDTIMEKEEPING.TwelveDay": "Twelveday",
"JDTIMEKEEPING.ThirteenDay": "Thirteenday",
"JDTIMEKEEPING.FourteenDay": "Fourteenday"
}
14 changes: 12 additions & 2 deletions lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
"JDTIMEKEEPING.WeekName": "Settimana",

"JDTIMEKEEPING.Settings.WeekdayConfig.name": "Configura i Giorni della Settimana",
"JDTIMEKEEPING.Settings.WeekdayConfig.label": "Rinomina i Giorni della Settimana",
"JDTIMEKEEPING.Settings.WeekdayConfig.label": "Weekday Settings",
"JDTIMEKEEPING.Settings.RenameWeekdays": "Rinomina i Giorni della Settimana",
"JDTIMEKEEPING.Settings.WeekdayConfig.hint": "Imposta i nomi dei giorni della settimana",

"JDTIMEKEEPING.longTimeFormat": "{time}, {dayName}, {weekName} {weekNumber}",
Expand All @@ -120,5 +121,14 @@
"JDTIMEKEEPING.Settings.UIFocusOpacity.name": "UI Transparency",
"JDTIMEKEEPING.Settings.UIFocusOpacity.hint": "Controls the UI transparency when in use. 0 is fully hidden, and 1 is fully visible.",
"JDTIMEKEEPING.Settings.UIBackgroundColor.name": "UI Background Color",
"JDTIMEKEEPING.Settings.UIBackgroundColor.hint": "Used on both the docked and floating panels"
"JDTIMEKEEPING.Settings.UIBackgroundColor.hint": "Used on both the docked and floating panels",
"JDTIMEKEEPING.Settings.DaysInWeek": "Number of Days in a Week",
"JDTIMEKEEPING.Settings.WeekName.label": "Name of the Week",
"JDTIMEKEEPING.EightDay": "Eightday",
"JDTIMEKEEPING.NineDay": "Nineday",
"JDTIMEKEEPING.TenDay": "Tenday",
"JDTIMEKEEPING.ElevenDay": "Elevenday",
"JDTIMEKEEPING.TwelveDay": "Twelveday",
"JDTIMEKEEPING.ThirteenDay": "Thirteenday",
"JDTIMEKEEPING.FourteenDay": "Fourteenday"
}
5 changes: 4 additions & 1 deletion src/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export class Constants {
static shiftsPerDay = 4
static minutesPerShift = 6 * 60 // hoursPerShift * minutesPerHour
static hoursPerShift = 6
static daysPerWeek = 7

static get daysPerWeek() {
return game.settings.get(MODULE_ID, SETTINGS.DAYS_PER_WEEK)
}

static get minutesPerTurn () {
return game.settings.get(MODULE_ID, SETTINGS.SMALL_TIME_DELTA)
Expand Down
5 changes: 2 additions & 3 deletions src/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class Helpers {

/**
* Gets a localised weekday name
* @param {number} dayIndex the 0-based day index, range [0..6]
* @param {number} dayIndex the 0-based day index, range [0..14)
* @returns {string} the localized name of the corresponding day of the week.
* Weeks start on Monday.
*/
Expand All @@ -152,7 +152,6 @@ export class Helpers {
* Returns the current world setting for the word used for the name of a week
*/
static get weekName () {
const weekSettings = game.settings.get(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS)
return weekSettings.weekname
return game.settings.get(MODULE_ID, SETTINGS.WEEK_NAME)
}
}
2 changes: 2 additions & 0 deletions src/settings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const SETTINGS = {
SHIFT_SETTINGS: 'shiftSettings',
SHIFT_MENU: 'shiftMenu',
WEEKDAY_SETTINGS: 'weekdaySettings',
DAYS_PER_WEEK: 'daysPerWeek',
WEEK_NAME: 'weekName',
WEEKDAY_MENU: 'weekdayMenu',
SHOW_LONG_FORMAT_TIME: 'showDayInExactTime',
DISPLAY_24_HOUR_TIME: 'display24HourTime',
Expand Down
128 changes: 111 additions & 17 deletions src/weekdaysettings.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import { Helpers } from './helpers.mjs'
import { MODULE_ID, SETTINGS } from './settings.mjs'

const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'WeekName']
const weekdays = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
'EightDay',
'NineDay',
'TenDay',
'ElevenDay',
'TwelveDay',
'ThirteenDay',
'FourteenDay',
]

const MIN_DAYS_PER_WEEK = 5
const MAX_DAYS_PER_WEEK = 14
const DEFAULT_DAYS_PER_WEEK = 7

export function registerWeekdaySettings () {
// The settings menu
Expand All @@ -19,13 +38,28 @@ export function registerWeekdaySettings () {
defaults[v.toLowerCase()] = game.i18n.localize(`JDTIMEKEEPING.${v}`)
})

// the settings object
defaults.daysPerWeek = DEFAULT_DAYS_PER_WEEK

// the settings objects
game.settings.register(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS, {
scope: 'world',
config: false,
type: Object,
default: defaults,
requiresReload: true,
})

game.settings.register(MODULE_ID, SETTINGS.DAYS_PER_WEEK, {
scope: 'world',
config: false,
type: Number,
default: DEFAULT_DAYS_PER_WEEK,
})

game.settings.register(MODULE_ID, SETTINGS.WEEK_NAME, {
scope: 'world',
config: false,
type: String,
default: game.i18n.localize('JDTIMEKEEPING.WeekName'),
})
}

Expand All @@ -34,46 +68,99 @@ class WeekdaySettings extends FormApplication {
return foundry.utils.mergeObject(super.defaultOptions, {
popOut: true,
width: 400,
resizable: true,
template: `modules/${MODULE_ID}/templates/weekdaysettings.hbs`,
id: SETTINGS.WEEKDAY_MENU,
title: 'JDTIMEKEEPING.Settings.WeekdayConfig.name',
})
}

getData () {
const initialValues = game.settings.get(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS)
const initialDayNames = game.settings.get(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS)
const daysPerWeek = game.settings.get(MODULE_ID, SETTINGS.DAYS_PER_WEEK)
const weekName = game.settings.get(MODULE_ID, SETTINGS.WEEK_NAME)

const data = {}

// build the list of week days
data.weekdays = {}
weekdays.forEach((v, i) => {
data[i] = {
const value = game.i18n.localize(`JDTIMEKEEPING.${v}`)
data.weekdays[i] = {
id: `${v.toLowerCase()}`,
label: game.i18n.localize(`JDTIMEKEEPING.${v}`),
value: initialValues[v.toLowerCase()],
label: value,
// initial value if we have one, or the default localised string if we don't have a setting value
value: initialDayNames[v.toLowerCase()] ?? value,
class: i >= daysPerWeek ? 'hidden' : '', // hide if not in use
}
})

// data.weekday = {
// id: 'weekName',
// label: game.i18n.localize('JDTIMEKEEPING.WeekName'),
// value: initialValues['weekName'],
// }
// The number of days per week
data.daysPerWeek = {
id: 'daysPerWeek',
label: game.i18n.localize('JDTIMEKEEPING.Settings.DaysInWeek'),
value: daysPerWeek,
min: MIN_DAYS_PER_WEEK,
max: MAX_DAYS_PER_WEEK,
}

// the name of the week itself
data.weekName = {
id: 'weekName',
label: game.i18n.localize('JDTIMEKEEPING.Settings.WeekName.label'),
value: weekName,
}

return data
}

_updateObject (event, formData) {
async _updateObject (event, formData) {
const data = foundry.utils.expandObject(formData)
const current = game.settings.get(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS)
var reload = false

const daysPerWeek = await game.settings.get(MODULE_ID, SETTINGS.DAYS_PER_WEEK)
if (daysPerWeek != data.daysPerWeek) {
game.settings.set(MODULE_ID, SETTINGS.DAYS_PER_WEEK, data.daysPerWeek)
reload = true
}
delete data.daysPerWeek

const weekName = await game.settings.get(MODULE_ID, SETTINGS.WEEK_NAME)
if (weekName != data.weekName) {
game.settings.set(MODULE_ID, SETTINGS.WEEK_NAME, data.weekName)
reload = true
}
delete data.weekName

const current = game.settings.get(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS)
if (!Helpers.objectsShallowEqual(data, current)) {
game.settings.set(MODULE_ID, SETTINGS.WEEKDAY_SETTINGS, data)
// The days of the week don't need a reload at the moment.
// SettingsConfig.reloadConfirm({ world: true })
reload = true
}

if (reload) SettingsConfig.reloadConfirm({ world: true })
}

activateListeners (html) {
super.activateListeners(html)
html.on('click', '[data-action=reset]', this._handleResetButtonClicked)
html.on('click', '[data-action=reset]', this._handleResetButtonClicked.bind(this))
html.on('change', '[name=daysPerWeek]', this._handleDaysPerWeekChanged.bind(this))
}

_handleDaysPerWeekChanged (event) {
const daysPerWeek = Number.parseInt(event.currentTarget.value)
this.#updateDayElements(event.delegateTarget, daysPerWeek)
}

#updateDayElements (delegateTarget, daysPerWeek) {
weekdays.forEach((name, i) => {
let element = $(delegateTarget).find(`[name=${name.toLowerCase()}]`).parent()
if (element && element.length) {
element = element[0]
if (i >= daysPerWeek) element.style.display = 'none'
else element.style.display = 'flex'
}
})
}

async _handleResetButtonClicked (event) {
Expand All @@ -84,6 +171,13 @@ class WeekdaySettings extends FormApplication {
element[0].value = game.i18n.localize(`JDTIMEKEEPING.${id}`)
}
})

$(event.delegateTarget).find('[name=daysPerWeek]')[0].value = DEFAULT_DAYS_PER_WEEK
$(event.delegateTarget).find('[name=weekName]')[0].value =
game.i18n.localize('JDTIMEKEEPING.WeekName')

this.#updateDayElements(event.delegateTarget, DEFAULT_DAYS_PER_WEEK)

ui.notifications.notify(game.i18n.localize('SETTINGS.ResetInfo'))
}
}
15 changes: 12 additions & 3 deletions templates/weekdaysettings.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
{{#*inline "dayPartial"}}
<div class="form-row flexrow">
<div class="form-row flexrow {{class}}">
<label for="{{id}}">{{label}}:</label>
<input name="{{id}}" type="text" required=true value="{{value}}" />
</div>
{{/inline}}

<form class="form-group flexcol">

<div class="form-row flexrow">
<label for="{{daysPerWeek.id}}">{{daysPerWeek.label}}:</label>
<input name="{{daysPerWeek.id}}" type="number" min="{{daysPerWeek.min}}" max="{{daysPerWeek.max}}" required=true
value="{{daysPerWeek.value}}" />
</div>

{{> dayPartial weekName}}

<fieldset>
<legend>{{localize "JDTIMEKEEPING.Settings.WeekdayConfig.label"}}:</legend>
{{#each this}}
<legend>{{localize "JDTIMEKEEPING.Settings.RenameWeekdays"}}:</legend>
{{#each this.weekdays}}
{{> dayPartial}}
{{/each}}
</fieldset>
Expand Down

0 comments on commit f8ef115

Please sign in to comment.