Add pre-selection of calendars #20

Merged
tomate merged 4 commits from refs/pull/20/head into master 2022-03-29 20:33:07 +02:00
4 changed files with 57 additions and 9 deletions
Showing only changes of commit 8dff9ca919 - Show all commits

View file

@ -10,7 +10,7 @@
import frLocale from '@fullcalendar/core/locales/fr';
import EventModal from './EventModal.svelte';
import FilterBar from './FilterBar.svelte';
import { mkSource, calendarTree } from './calendar';
import { mkSource, calendarTree, initialCalendars, getSubCalendars } from './calendar';
import { debounce } from 'lodash';
import Help from './Help.svelte';
@ -30,6 +30,8 @@
return time.toLocaleTimeString();
})();
const params = new URL(document.location).searchParams.getAll('c');
let calendar;
let options = writable({
@ -71,7 +73,23 @@
progressiveEventRendering: true
});
const flatten = d => {
let array = [];
if (Array.isArray(d)) {
d.forEach(a => (array = array.concat(flatten(a))));
} else {
for (const [n, s] of Object.entries(d)) {
array = array.concat(n, flatten(s));
}
}
return array;
};
let selectedCalendars = [];
const initial =
params.length > 0
? flatten(params.map(cal => getSubCalendars(cal))).concat(params)
: initialCalendars;
const updateEvents = debounce(calendars => {
options.update(opts => ({
@ -88,7 +106,7 @@
<Help />
<FilterBar {calendarTree} bind:selected={selectedCalendars} />
<FilterBar {calendarTree} bind:selected={selectedCalendars} {initial} />
<FullCalendar bind:this={calendar} options={$options} />

View file

@ -5,15 +5,17 @@
export let calendarTree = [];
export let selected = null;
export let initial = [];
let open = false;
let icon;
let generalToggle = writable(true);
const toggle = () => (open = !open);
$: icon = () => ($generalToggle ? 'toggle-on' : 'toggle-off');
let subSelections = Array.from({ length: Object.keys(calendarTree).length }, _ => []);
$: selected = subSelections.flat();
generalToggle.subscribe(v => (icon = v ? 'toggle-on' : 'toggle-off'));
</script>
<Button on:click={toggle} class="mb-3 col-6 mx-auto">
@ -29,7 +31,7 @@
header="Sélection des calendriers"
>
<span class="fs-5 toggle" on:click={() => ($generalToggle = !$generalToggle)}>
<Icon name={icon()} />
<Icon name={icon} />
<span class="ms-2">Tous les calendriers</span>
</span>
<hr class="my-3" />
@ -40,6 +42,7 @@
children={subtrees}
bind:selected={subSelections[i]}
bind:generalToggle
{initial}
/>
{/each}
</Offcanvas>

View file

@ -11,11 +11,15 @@
export let item = null;
export let level = 1;
export let generalToggle = undefined;
export let initial;
export let children = [];
export let selected = [];
export let filtering = createTriState(UNCHECKED);
let subfiltering = createTriStates(UNCHECKED, Object.entries(children).length);
const initialState = initial.includes(item) ? CHECKED : UNCHECKED;
export let filtering = createTriState(initialState);
let subfiltering = createTriStates(initialState, Object.entries(children).length);
let subselected = Array.from({ length: Object.entries(children).length }, _ => []);
let collapsed = Object.entries(children).length ? false : undefined;
@ -75,7 +79,7 @@
if (generalToggle) {
generalToggle.subscribe(value => {
setTimeout(() => (value ? filtering.setChecked() : filtering.setUnchecked()), 0);
value ? filtering.setChecked() : filtering.setUnchecked();
dispatch('change', { value: $filtering });
});
}
@ -85,6 +89,8 @@
return source ? source.export_url : undefined;
})();
initial.includes(item) ? filtering.setChecked() : filtering.setUnchecked();
$: selected =
$filtering === CHECKED ? [item, ...subselected.flat()] : subselected.flat();
$: subHidden = () => (collapsed ? 'd-none' : '');
@ -111,6 +117,7 @@
filtering={subfiltering.storeAt(i)}
bind:selected={subselected[i]}
level={level + 1}
{initial}
/>
{:else}
<svelte:self
@ -119,6 +126,7 @@
filtering={subfiltering.storeAt(i)}
bind:selected={subselected[i]}
level={level + 1}
{initial}
/>
{/if}
{/each}

View file

@ -26,7 +26,8 @@ const calendars = {
w442JdS5AaQ6czrP: {
cloud: clouds.ELEVES_ENS,
name: "Écriv'ENS",
color: null
color: null,
initial: false
},
fRtjDkjrZyn6fxd8: {
cloud: clouds.ELEVES_ENS,
@ -50,6 +51,12 @@ const calendarsByName = Object.fromEntries(
Object.entries(calendars).map(([id, { name }]) => [name, id])
)
export const initialCalendars = Array.from(
Object.entries(calendars).map(([_, { name, initial }]) => [name, initial ?? true])
)
.filter(cal => cal[1])
.map(cal => cal[0])
export const calendarTree = {
'Clubs COF': {
'Club réseau': {},
@ -68,6 +75,18 @@ export const calendarTree = {
'K-Fêt': {}
}
export function getSubCalendars(name, tree = calendarTree) {
let ret
for (const [cal, subTree] of Object.entries(tree)) {
if (cal === name) {
ret = subTree
} else {
ret = ret || getSubCalendars(name, subTree)
}
}
return ret
}
const calendarIds = Object.keys(calendars)
function mkCalendarUrl(id, { cloud }, extra = {}) {