mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Calendar Mode shows each card on the day it is due - the date only, no time - with its labels in their colours and its whole title over as many lines as it needs, and the cards of a day in the board's own order: by their list's place, then their place in the list. A month or a week, starting on the user's first day of the week, with Today, Previous and Next; the board Filter applies, as on Calendar, and a card opens beside the calendar. The grid and the day's order are models/lib/calendarMode.js. A board that saved its Board View order before this view existed still has the default order, with Calendar Mode in its place and the menu's separators: a saved order that lacks only views added later (VIEWS_ADDED_LATER) is the default order of its time. Any other saved order is the user's own, and the new view goes at its end, as before. Tests: tests/calendarMode.test.cjs (the month and week grid, the first day of the week, the day's order, cards without a due date, the wiring), tests/boardViewSettings.test.cjs (an order saved before Calendar Mode, and a custom one), tests/boardViewMenu.test.cjs and tests/playwright/specs/calendar-mode.e2e.js (the menu entry below Calendar, labels and order on today's cell, Week, opening a card). Thanks to tripledee and xet7 ! Fixes #3194.
98 lines
4.2 KiB
JavaScript
98 lines
4.2 KiB
JavaScript
// #3194: Calendar Mode (see calendarModeView.jade, models/lib/calendarMode.js).
|
|
import { ReactiveVar } from 'meteor/reactive-var';
|
|
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
|
|
import { TAPi18n } from '/imports/i18n';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { Utils } from '/client/lib/utils';
|
|
import { Filter } from '/client/lib/filter';
|
|
const { calendarModeDays, shiftAnchor, cardsByDueDay, RANGES } = require('/models/lib/calendarMode');
|
|
|
|
// The month or week a viewer chose is remembered in this browser only.
|
|
const RANGE_KEY = 'calendarModeRange';
|
|
const storedRange = () => {
|
|
try { const range = window.localStorage.getItem(RANGE_KEY); return RANGES.includes(range) ? range : 'month'; } catch (e) { return 'month'; }
|
|
};
|
|
|
|
const firstDayOfWeek = () => {
|
|
const user = ReactiveCache.getCurrentUser();
|
|
const day = user && typeof user.getStartDayOfWeek === 'function' ? user.getStartDayOfWeek() : 1;
|
|
return Number.isInteger(day) && day >= 0 && day <= 6 ? day : 1;
|
|
};
|
|
|
|
Template.calendarModeView.onCreated(function () {
|
|
this.anchor = new ReactiveVar(new Date());
|
|
this.range = new ReactiveVar(storedRange());
|
|
this.days = () => calendarModeDays({ anchor: this.anchor.get(), range: this.range.get(), firstDay: firstDayOfWeek() });
|
|
});
|
|
|
|
Template.calendarModeView.helpers({
|
|
isRange(range) {
|
|
return Template.instance().range.get() === range;
|
|
},
|
|
rangeTitle() {
|
|
const tpl = Template.instance();
|
|
const anchor = tpl.anchor.get();
|
|
const locale = TAPi18n.getLanguage();
|
|
try {
|
|
if (tpl.range.get() === 'week') {
|
|
const { weeks } = tpl.days();
|
|
const fmt = new Intl.DateTimeFormat(locale, { day: 'numeric', month: 'short', year: 'numeric' });
|
|
return `${fmt.format(weeks[0][0].date)} - ${fmt.format(weeks[0][6].date)}`;
|
|
}
|
|
return new Intl.DateTimeFormat(locale, { month: 'long', year: 'numeric' }).format(anchor);
|
|
} catch (e) {
|
|
return anchor.toDateString();
|
|
}
|
|
},
|
|
weekdayNames() {
|
|
const { weeks } = Template.instance().days();
|
|
const locale = TAPi18n.getLanguage();
|
|
return weeks[0].map(day => {
|
|
try { return new Intl.DateTimeFormat(locale, { weekday: 'short' }).format(day.date); } catch (e) { return ''; }
|
|
});
|
|
},
|
|
weeks() {
|
|
return Template.instance().days().weeks;
|
|
},
|
|
// The cards due on this day, in the board's order. Read once per render
|
|
// from the board's due cards in the shown range, with the board Filter.
|
|
cardsOn(key) {
|
|
const tpl = Template.instance();
|
|
const board = Utils.getCurrentBoard();
|
|
if (!board) return [];
|
|
const { start, end } = tpl.days();
|
|
const filter = Filter.isActive() ? Filter._getMongoSelector() : undefined;
|
|
const cards = board.cardsDueInBetween(start, end, filter);
|
|
const listSort = {};
|
|
ReactiveCache.getLists({ boardId: board._id }).forEach(list => { listSort[list._id] = list.sort; });
|
|
const byDay = cardsByDueDay(typeof cards.fetch === 'function' ? cards.fetch() : cards, listSort);
|
|
return byDay.get(key) || [];
|
|
},
|
|
cardUrl() {
|
|
const board = Utils.getCurrentBoard();
|
|
return board ? FlowRouter.path('card', { boardId: board._id, slug: board.slug, cardId: this._id }) : '#';
|
|
},
|
|
cardLabels() {
|
|
const board = Utils.getCurrentBoard();
|
|
const labels = (board && board.labels) || [];
|
|
return (this.labelIds || []).map(id => labels.find(label => label._id === id)).filter(Boolean);
|
|
},
|
|
});
|
|
|
|
Template.calendarModeView.events({
|
|
'click .js-calendar-mode-today'(event, tpl) { tpl.anchor.set(new Date()); },
|
|
'click .js-calendar-mode-prev'(event, tpl) { tpl.anchor.set(shiftAnchor(tpl.anchor.get(), tpl.range.get(), -1)); },
|
|
'click .js-calendar-mode-next'(event, tpl) { tpl.anchor.set(shiftAnchor(tpl.anchor.get(), tpl.range.get(), 1)); },
|
|
'click .js-calendar-mode-range'(event, tpl) {
|
|
const range = event.currentTarget.dataset.range;
|
|
if (!RANGES.includes(range)) return;
|
|
tpl.range.set(range);
|
|
try { window.localStorage.setItem(RANGE_KEY, range); } catch (e) { /* per-browser convenience only */ }
|
|
},
|
|
// A card opens in this tab, beside the calendar, as on the Calendar view.
|
|
'click .js-calendar-mode-card'(event) {
|
|
event.preventDefault();
|
|
FlowRouter.go(event.currentTarget.getAttribute('href'));
|
|
},
|
|
});
|