-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract monthNavigator custom element
- Loading branch information
1 parent
ef5a35a
commit 3fbaa00
Showing
10 changed files
with
218 additions
and
150 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 was deleted.
Oops, something went wrong.
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,85 @@ | ||
// @ts-check | ||
import { onParsed, waitForElement } from '../lib/customElements'; | ||
|
||
export default class extends HTMLElement { | ||
/** @type {HTMLInputElement} */ | ||
#month; | ||
|
||
/** @type {HTMLInputElement} */ | ||
#year; | ||
|
||
connectedCallback() { | ||
onParsed(() => { | ||
const parent = /** @type {HTMLElement} */ (this.parentElement); | ||
|
||
const findFields = /** @type {Promise<[HTMLInputElement, HTMLInputElement]>} */ (Promise.all( | ||
[ | ||
waitForElement(this.dataset.monthTarget || '', parent, { | ||
timeout: 3000 | ||
}), | ||
waitForElement(this.dataset.yearTarget || '', parent, { | ||
timeout: 3000 | ||
}) | ||
] | ||
)); | ||
|
||
findFields.then(([month, year]) => { | ||
// Assume month is 1 (January) to 12 (December) | ||
this.#month = month; | ||
this.#year = year; | ||
|
||
const template = /** @type {HTMLTemplateElement} */ (this.querySelector( | ||
'template' | ||
)); | ||
|
||
this.appendChild(document.importNode(template.content, true)); | ||
|
||
const previousBtn = /** @type {HTMLButtonElement} */ (this.querySelector( | ||
'button[data-previous]' | ||
)); | ||
|
||
const todayBtn = /** @type {HTMLButtonElement} */ (this.querySelector( | ||
'button[data-today]' | ||
)); | ||
|
||
const nextBtn = /** @type {HTMLButtonElement} */ (this.querySelector( | ||
'button[data-next]' | ||
)); | ||
|
||
previousBtn.addEventListener('click', () => { | ||
this._updateMonth(+month.value - 1); | ||
}); | ||
|
||
todayBtn.addEventListener('click', () => { | ||
const today = new Date(); | ||
month.value = (today.getMonth() + 1).toString(); | ||
year.value = today.getFullYear().toString(); | ||
// Trigger change on either field, but only once | ||
year.dispatchEvent(new Event('change')); | ||
}); | ||
|
||
nextBtn.addEventListener('click', () => { | ||
this._updateMonth(+month.value + 1); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {number} month | ||
*/ | ||
_updateMonth(month) { | ||
if (month < 1) { | ||
month += 12; | ||
this.#year.value = (+this.#year.value - 1).toString(); | ||
} | ||
|
||
if (month > 12) { | ||
month -= 12; | ||
this.#year.value = (+this.#year.value + 1).toString(); | ||
} | ||
|
||
this.#month.value = month.toString(); | ||
this.#month.dispatchEvent(new Event('change')); | ||
} | ||
} |
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,20 @@ | ||
{% import 'macros/icons.njk' as icons %} | ||
|
||
{% macro month_navigator(monthTarget, yearTarget) %} | ||
<pc-month-navigator data-month-target="{{ monthTarget }}" data-year-target="{{ yearTarget }}"> | ||
<template> | ||
<div class="pc-cluster" style="--cluster-align: stretch"> | ||
{# TODO: translations #} | ||
<button type="button" data-previous class="pc-btn pc-btn--muted" title="Mois précédent"> | ||
{{ '<' }} | ||
</button> | ||
<button type="button" data-today class="pc-btn pc-btn--muted" title="Aujourd'hui"> | ||
{{ icons.calendar() }} | ||
</button> | ||
<button type="button" data-next class="pc-btn pc-btn--muted" title="Mois suivant"> | ||
{{ '>' }} | ||
</button> | ||
</div> | ||
</template> | ||
</pc-month-navigator> | ||
{% endmacro %} |
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,44 +1,40 @@ | ||
{% from 'macros/month_navigator.njk' import month_navigator %} | ||
|
||
{% macro filters_form() %} | ||
<pc-faircalendar-filters-form> | ||
<form | ||
method="GET" | ||
action="{{ path('faircalendar_index') }}" | ||
class="pc-cluster pc-gap" | ||
style="--cluster-align: flex-end" | ||
> | ||
<template id="faircalendar-filters-form-navigation"> | ||
<div class="pc-cluster" style="--cluster-align: stretch"> | ||
<button id="previousBtn" type="button" class="pc-btn pc-btn--muted" title="Mois précédent">{{ '<' }}</button> | ||
<button id="todayBtn" type="button" class="pc-btn pc-btn--muted" title="Aujourd'hui"> | ||
{{ icons.calendar() }} | ||
</button> | ||
<button id="nextBtn" type="button" class="pc-btn pc-btn--muted" title="Mois suivant">{{ '>' }}</button> | ||
</div> | ||
</template> | ||
<div class="pc-cluster pc-gap" style="--cluster-align: flex-end"> | ||
{{ month_navigator(monthTarget='#month', yearTarget='#year') }} | ||
|
||
<div class="pc-select-group pc-m" style="--m: 0"> | ||
<label class="pc-label" for="month">{{ 'faircalendar-filters-month-title'|trans }}</label> | ||
<select name="month" id="month"> | ||
{% for month in range(12) %} | ||
<option value="{{ month }}" {% if month == currentMonth %}selected{% endif %}>{{ month|longMonth|capitalize }}</option> | ||
{% endfor %} | ||
</select> | ||
</div> | ||
<div class="pc-input-group pc-m" style="--m: 0"> | ||
<label class="pc-label" for="year">{{ 'faircalendar-filters-year-title'|trans }}</label> | ||
<input name="year" id="year" type="number" value="{{ currentYear }}"> | ||
</div> | ||
<div class="pc-select-group pc-m" style="--m: 0"> | ||
<label class="pc-label" for="userId">{{ 'faircalendar-filters-userId-title'|trans }}</label> | ||
<select name="userId" id="userId"> | ||
{% for user in users %} | ||
<option value="{{ user.id }}" {% if user.id == userId %}selected{% endif %}>{{ user|fullName }}</option> | ||
{% endfor %} | ||
</select> | ||
</div> | ||
<button type="submit" class="pc-btn pc-btn--secondary"> | ||
{{ 'common-form-update'|trans }} | ||
</button> | ||
</form> | ||
</pc-faircalendar-filters-form> | ||
<pc-auto-form> | ||
<form | ||
method="GET" | ||
action="{{ path('faircalendar_index') }}" | ||
class="pc-cluster pc-gap" | ||
style="--cluster-align: flex-end" | ||
> | ||
<div class="pc-select-group pc-m" style="--m: 0"> | ||
<label class="pc-label" for="month">{{ 'faircalendar-filters-month-title'|trans }}</label> | ||
<select name="month" id="month"> | ||
{% for month in range(1, 13) %} | ||
<option value="{{ month }}" {% if month == currentMonth %}selected{% endif %}>{{ (month - 1)|longMonth|capitalize }}</option> | ||
{% endfor %} | ||
</select> | ||
</div> | ||
<div class="pc-input-group pc-m" style="--m: 0"> | ||
<label class="pc-label" for="year">{{ 'faircalendar-filters-year-title'|trans }}</label> | ||
<input name="year" id="year" type="number" value="{{ currentYear }}"> | ||
</div> | ||
<div class="pc-select-group pc-m" style="--m: 0"> | ||
<label class="pc-label" for="userId">{{ 'faircalendar-filters-userId-title'|trans }}</label> | ||
<select name="userId" id="userId"> | ||
{% for user in users %} | ||
<option value="{{ user.id }}" {% if user.id == userId %}selected{% endif %}>{{ user|fullName }}</option> | ||
{% endfor %} | ||
</select> | ||
</div> | ||
<button type="submit" class="pc-btn pc-btn--secondary"> | ||
{{ 'common-form-update'|trans }} | ||
</button> | ||
</form> | ||
</pc-auto-form> | ||
</div> | ||
{% endmacro %} |
Oops, something went wrong.