@tasknotes/calendar 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +163 -2
- package/dist/calendar.css +1345 -0
- package/dist/cjs/core.cjs +716 -0
- package/dist/cjs/index.cjs +4206 -0
- package/dist/cjs/react.cjs +4034 -0
- package/dist/esm/chunk-DVMZ5OXN.js +694 -0
- package/dist/esm/chunk-L4WKZ6WT.js +0 -0
- package/dist/esm/chunk-UWCSL5AN.js +3401 -0
- package/dist/esm/core.js +85 -0
- package/dist/esm/index.js +223 -0
- package/dist/esm/react.js +46 -0
- package/dist/types/core/date.d.cts +30 -0
- package/dist/types/core/date.d.ts +30 -0
- package/dist/types/core/index.d.cts +7 -0
- package/dist/types/core/index.d.ts +7 -0
- package/dist/types/core/layout.d.cts +82 -0
- package/dist/types/core/layout.d.ts +82 -0
- package/dist/types/core/order.d.cts +7 -0
- package/dist/types/core/order.d.ts +7 -0
- package/dist/types/core/plan.d.cts +42 -0
- package/dist/types/core/plan.d.ts +42 -0
- package/dist/types/core/store.d.cts +60 -0
- package/dist/types/core/store.d.ts +60 -0
- package/dist/types/core/types.d.cts +69 -0
- package/dist/types/core/types.d.ts +69 -0
- package/dist/types/core/zone.d.cts +43 -0
- package/dist/types/core/zone.d.ts +43 -0
- package/dist/types/dom/calendar.d.cts +206 -0
- package/dist/types/dom/calendar.d.ts +206 -0
- package/dist/types/dom/context.d.cts +107 -0
- package/dist/types/dom/context.d.ts +107 -0
- package/dist/types/dom/dom.d.cts +8 -0
- package/dist/types/dom/dom.d.ts +8 -0
- package/dist/types/dom/format.d.cts +30 -0
- package/dist/types/dom/format.d.ts +30 -0
- package/dist/types/dom/options.d.cts +401 -0
- package/dist/types/dom/options.d.ts +401 -0
- package/dist/types/dom/views/daygrid.d.cts +42 -0
- package/dist/types/dom/views/daygrid.d.ts +42 -0
- package/dist/types/dom/views/list.d.cts +27 -0
- package/dist/types/dom/views/list.d.ts +27 -0
- package/dist/types/dom/views/timegrid.d.cts +54 -0
- package/dist/types/dom/views/timegrid.d.ts +54 -0
- package/dist/types/dom/views/timeline.d.cts +83 -0
- package/dist/types/dom/views/timeline.d.ts +83 -0
- package/dist/types/dom/views/year.d.cts +26 -0
- package/dist/types/dom/views/year.d.ts +26 -0
- package/dist/types/index.d.cts +6 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/react/index.d.cts +20 -0
- package/dist/types/react/index.d.ts +20 -0
- package/dist/types/tasknotes/bases.d.cts +50 -0
- package/dist/types/tasknotes/bases.d.ts +50 -0
- package/package.json +108 -5
|
@@ -0,0 +1,4206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
BASES_CALENDAR_DEFAULTS: () => BASES_CALENDAR_DEFAULTS,
|
|
24
|
+
Calendar: () => Calendar,
|
|
25
|
+
DAY_MS: () => DAY_MS,
|
|
26
|
+
DEFAULT_TEXT: () => DEFAULT_TEXT,
|
|
27
|
+
Formatter: () => Formatter,
|
|
28
|
+
ItemStore: () => ItemStore,
|
|
29
|
+
LAYOUT_KEYS: () => LAYOUT_KEYS,
|
|
30
|
+
MINUTES_PER_DAY: () => MINUTES_PER_DAY,
|
|
31
|
+
TIMELINE_UNIT_DAYS: () => TIMELINE_UNIT_DAYS,
|
|
32
|
+
addMonths: () => addMonths,
|
|
33
|
+
basesLayoutOptions: () => basesLayoutOptions,
|
|
34
|
+
dayFromYMD: () => dayFromYMD,
|
|
35
|
+
dayLengthMinutes: () => dayLengthMinutes,
|
|
36
|
+
dayOf: () => dayOf,
|
|
37
|
+
daysInMonth: () => daysInMonth,
|
|
38
|
+
daysOfRange: () => daysOfRange,
|
|
39
|
+
defaultOptions: () => defaultOptions,
|
|
40
|
+
defaultOrder: () => defaultOrder,
|
|
41
|
+
dependencyConflicts: () => dependencyConflicts,
|
|
42
|
+
dependencySatisfied: () => dependencySatisfied,
|
|
43
|
+
dependencyType: () => dependencyType,
|
|
44
|
+
findFreeSlot: () => findFreeSlot,
|
|
45
|
+
formatDay: () => formatDay,
|
|
46
|
+
formatFloating: () => formatFloating,
|
|
47
|
+
formatMinutes: () => formatMinutes,
|
|
48
|
+
getTimeZone: () => getTimeZone,
|
|
49
|
+
isValidSlotTime: () => isValidSlotTime,
|
|
50
|
+
isValidTimeZone: () => isValidTimeZone,
|
|
51
|
+
isoWeekNumber: () => isoWeekNumber,
|
|
52
|
+
itemDependencies: () => itemDependencies,
|
|
53
|
+
layoutLane: () => layoutLane,
|
|
54
|
+
layoutRow: () => layoutRow,
|
|
55
|
+
layoutTimedDay: () => layoutTimedDay,
|
|
56
|
+
localeFirstDay: () => localeFirstDay,
|
|
57
|
+
packTimed: () => packTimed,
|
|
58
|
+
parseDateInput: () => parseDateInput,
|
|
59
|
+
parseDay: () => parseDay,
|
|
60
|
+
parseDurationMinutes: () => parseDurationMinutes,
|
|
61
|
+
parseOrder: () => parseOrder,
|
|
62
|
+
plannedMinutes: () => plannedMinutes,
|
|
63
|
+
segmentInWindow: () => segmentInWindow,
|
|
64
|
+
segmentOn: () => segmentOn,
|
|
65
|
+
startOfDayInstant: () => startOfDayInstant,
|
|
66
|
+
startOfMonth: () => startOfMonth,
|
|
67
|
+
startOfWeek: () => startOfWeek,
|
|
68
|
+
startOfYear: () => startOfYear,
|
|
69
|
+
toInstant: () => toInstant,
|
|
70
|
+
viewFromBases: () => viewFromBases,
|
|
71
|
+
viewToBases: () => viewToBases,
|
|
72
|
+
wallTime: () => wallTime,
|
|
73
|
+
weekdayOf: () => weekdayOf,
|
|
74
|
+
ymdOfDay: () => ymdOfDay
|
|
75
|
+
});
|
|
76
|
+
module.exports = __toCommonJS(src_exports);
|
|
77
|
+
|
|
78
|
+
// src/core/date.ts
|
|
79
|
+
var DAY_MS = 864e5;
|
|
80
|
+
var MINUTES_PER_DAY = 1440;
|
|
81
|
+
function dayFromYMD(year, month, day) {
|
|
82
|
+
const d = /* @__PURE__ */ new Date(0);
|
|
83
|
+
d.setUTCFullYear(year, month - 1, day);
|
|
84
|
+
d.setUTCHours(0, 0, 0, 0);
|
|
85
|
+
return Math.floor(d.getTime() / DAY_MS);
|
|
86
|
+
}
|
|
87
|
+
function ymdOfDay(day) {
|
|
88
|
+
const d = new Date(day * DAY_MS);
|
|
89
|
+
return { year: d.getUTCFullYear(), month: d.getUTCMonth() + 1, day: d.getUTCDate() };
|
|
90
|
+
}
|
|
91
|
+
function weekdayOf(day) {
|
|
92
|
+
return ((day + 4) % 7 + 7) % 7;
|
|
93
|
+
}
|
|
94
|
+
function startOfWeek(day, firstDay) {
|
|
95
|
+
return day - (weekdayOf(day) - firstDay + 7) % 7;
|
|
96
|
+
}
|
|
97
|
+
function startOfMonth(day) {
|
|
98
|
+
const { year, month } = ymdOfDay(day);
|
|
99
|
+
return dayFromYMD(year, month, 1);
|
|
100
|
+
}
|
|
101
|
+
function daysInMonth(year, month) {
|
|
102
|
+
return dayFromYMD(year, month + 1, 1) - dayFromYMD(year, month, 1);
|
|
103
|
+
}
|
|
104
|
+
function addMonths(day, months) {
|
|
105
|
+
const { year, month, day: dom } = ymdOfDay(day);
|
|
106
|
+
const total = year * 12 + (month - 1) + months;
|
|
107
|
+
const y = Math.floor(total / 12);
|
|
108
|
+
const m = total % 12 + 1;
|
|
109
|
+
return dayFromYMD(y, m, Math.min(dom, daysInMonth(y, m)));
|
|
110
|
+
}
|
|
111
|
+
function startOfYear(day) {
|
|
112
|
+
return dayFromYMD(ymdOfDay(day).year, 1, 1);
|
|
113
|
+
}
|
|
114
|
+
var pad2 = (n) => String(n).padStart(2, "0");
|
|
115
|
+
function formatDay(day) {
|
|
116
|
+
const { year, month, day: d } = ymdOfDay(day);
|
|
117
|
+
const y = year < 0 ? "-" + String(-year).padStart(6, "0") : String(year).padStart(4, "0");
|
|
118
|
+
return `${y}-${pad2(month)}-${pad2(d)}`;
|
|
119
|
+
}
|
|
120
|
+
var DATE_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
121
|
+
function parseDay(value) {
|
|
122
|
+
const m = DATE_RE.exec(value.trim());
|
|
123
|
+
if (!m) return null;
|
|
124
|
+
const year = Number(m[1]);
|
|
125
|
+
const month = Number(m[2]);
|
|
126
|
+
const d = Number(m[3]);
|
|
127
|
+
if (month < 1 || month > 12 || d < 1 || d > daysInMonth(year, month)) return null;
|
|
128
|
+
return dayFromYMD(year, month, d);
|
|
129
|
+
}
|
|
130
|
+
function isoWeekNumber(day) {
|
|
131
|
+
const thursday = day - (weekdayOf(day) + 6) % 7 + 3;
|
|
132
|
+
const jan1 = dayFromYMD(ymdOfDay(thursday).year, 1, 1);
|
|
133
|
+
return Math.floor((thursday - jan1) / 7) + 1;
|
|
134
|
+
}
|
|
135
|
+
function parseDurationMinutes(value, fallback) {
|
|
136
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
137
|
+
if (typeof value !== "string") return fallback;
|
|
138
|
+
const m = /^(-?\d{1,3}):(\d{2})(?::(\d{2}))?$/.exec(value.trim());
|
|
139
|
+
if (!m) return fallback;
|
|
140
|
+
const sign = m[1].startsWith("-") ? -1 : 1;
|
|
141
|
+
return sign * (Math.abs(Number(m[1])) * 60 + Number(m[2]) + Number(m[3] ?? 0) / 60);
|
|
142
|
+
}
|
|
143
|
+
function formatMinutes(minutes) {
|
|
144
|
+
const m = (Math.round(minutes) % MINUTES_PER_DAY + MINUTES_PER_DAY) % MINUTES_PER_DAY;
|
|
145
|
+
return `${pad2(Math.floor(m / 60))}:${pad2(m % 60)}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/core/zone.ts
|
|
149
|
+
var LocalZone = class {
|
|
150
|
+
id = "local";
|
|
151
|
+
offsetAt(ms) {
|
|
152
|
+
return -new Date(ms).getTimezoneOffset();
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
var IanaZone = class {
|
|
156
|
+
constructor(id) {
|
|
157
|
+
this.id = id;
|
|
158
|
+
this.fmt = new Intl.DateTimeFormat("en-US", {
|
|
159
|
+
timeZone: id,
|
|
160
|
+
hourCycle: "h23",
|
|
161
|
+
year: "numeric",
|
|
162
|
+
month: "numeric",
|
|
163
|
+
day: "numeric",
|
|
164
|
+
hour: "numeric",
|
|
165
|
+
minute: "numeric",
|
|
166
|
+
second: "numeric"
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
id;
|
|
170
|
+
fmt;
|
|
171
|
+
cache = /* @__PURE__ */ new Map();
|
|
172
|
+
offsetAt(ms) {
|
|
173
|
+
const bucket = Math.floor(ms / 9e5);
|
|
174
|
+
const hit = this.cache.get(bucket);
|
|
175
|
+
if (hit !== void 0) return hit;
|
|
176
|
+
const at = bucket * 9e5;
|
|
177
|
+
const p = {};
|
|
178
|
+
for (const part of this.fmt.formatToParts(new Date(at))) {
|
|
179
|
+
if (part.type !== "literal") p[part.type] = Number(part.value);
|
|
180
|
+
}
|
|
181
|
+
const wall = dayFromYMD(p.year, p.month, p.day) * DAY_MS + ((p.hour * 60 + p.minute) * 60 + p.second) * 1e3;
|
|
182
|
+
const offset = Math.round((wall - at) / 6e4);
|
|
183
|
+
if (this.cache.size > 5e3) this.cache.clear();
|
|
184
|
+
this.cache.set(bucket, offset);
|
|
185
|
+
return offset;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
var zones = /* @__PURE__ */ new Map();
|
|
189
|
+
function getTimeZone(id) {
|
|
190
|
+
const key = !id || id === "local" ? "local" : id;
|
|
191
|
+
let z = zones.get(key);
|
|
192
|
+
if (!z) {
|
|
193
|
+
z = key === "local" ? new LocalZone() : new IanaZone(key);
|
|
194
|
+
zones.set(key, z);
|
|
195
|
+
}
|
|
196
|
+
return z;
|
|
197
|
+
}
|
|
198
|
+
function isValidTimeZone(id) {
|
|
199
|
+
if (id === "local") return true;
|
|
200
|
+
try {
|
|
201
|
+
new Intl.DateTimeFormat("en-US", { timeZone: id });
|
|
202
|
+
return true;
|
|
203
|
+
} catch {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function wallTime(ms, zone) {
|
|
208
|
+
const local = ms + zone.offsetAt(ms) * 6e4;
|
|
209
|
+
const day = Math.floor(local / DAY_MS);
|
|
210
|
+
return { day, minute: Math.floor((local - day * DAY_MS) / 6e4) };
|
|
211
|
+
}
|
|
212
|
+
function dayOf(ms, zone) {
|
|
213
|
+
return wallTime(ms, zone).day;
|
|
214
|
+
}
|
|
215
|
+
function toInstant(day, minute, zone) {
|
|
216
|
+
const localTs = day * DAY_MS + minute * 6e4;
|
|
217
|
+
const o = zone.offsetAt(localTs);
|
|
218
|
+
let utc = localTs - o * 6e4;
|
|
219
|
+
const o2 = zone.offsetAt(utc);
|
|
220
|
+
if (o === o2) return utc;
|
|
221
|
+
utc -= (o2 - o) * 6e4;
|
|
222
|
+
const o3 = zone.offsetAt(utc);
|
|
223
|
+
if (o2 === o3) return utc;
|
|
224
|
+
return localTs - Math.min(o2, o3) * 6e4;
|
|
225
|
+
}
|
|
226
|
+
function startOfDayInstant(day, zone) {
|
|
227
|
+
return toInstant(day, 0, zone);
|
|
228
|
+
}
|
|
229
|
+
function dayLengthMinutes(day, zone) {
|
|
230
|
+
return Math.round((startOfDayInstant(day + 1, zone) - startOfDayInstant(day, zone)) / 6e4);
|
|
231
|
+
}
|
|
232
|
+
var FLOATING_RE = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?$/;
|
|
233
|
+
var DATE_ONLY_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
234
|
+
function parseDateInput(value, zone) {
|
|
235
|
+
if (value instanceof Date) {
|
|
236
|
+
const t2 = value.getTime();
|
|
237
|
+
return Number.isNaN(t2) ? null : { kind: "instant", ms: t2 };
|
|
238
|
+
}
|
|
239
|
+
if (typeof value === "number") return Number.isFinite(value) ? { kind: "instant", ms: value } : null;
|
|
240
|
+
const s = value.trim();
|
|
241
|
+
let m = DATE_ONLY_RE.exec(s);
|
|
242
|
+
if (m) return { kind: "day", day: dayFromYMD(Number(m[1]), Number(m[2]), Number(m[3])) };
|
|
243
|
+
m = FLOATING_RE.exec(s);
|
|
244
|
+
if (m) {
|
|
245
|
+
const day = dayFromYMD(Number(m[1]), Number(m[2]), Number(m[3]));
|
|
246
|
+
const minute = Number(m[4]) * 60 + Number(m[5]);
|
|
247
|
+
const extraMs = Number(m[6] ?? 0) * 1e3 + Number((m[7] ?? "0").padEnd(3, "0"));
|
|
248
|
+
return { kind: "instant", ms: toInstant(day, minute, zone) + extraMs };
|
|
249
|
+
}
|
|
250
|
+
const t = Date.parse(s);
|
|
251
|
+
return Number.isNaN(t) ? null : { kind: "instant", ms: t };
|
|
252
|
+
}
|
|
253
|
+
function formatFloating(ms, zone) {
|
|
254
|
+
const { day, minute } = wallTime(ms, zone);
|
|
255
|
+
const d = new Date(day * DAY_MS);
|
|
256
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
257
|
+
return `${d.getUTCFullYear()}-${pad(d.getUTCMonth() + 1)}-${pad(d.getUTCDate())}T${pad(Math.floor(minute / 60))}:${pad(minute % 60)}`;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// src/core/store.ts
|
|
261
|
+
var DEFAULTS = { defaultTimedMinutes: 60, defaultAllDayDays: 1 };
|
|
262
|
+
function signature(item) {
|
|
263
|
+
if (item.rev !== void 0) return `${item.id}#${String(item.rev)}`;
|
|
264
|
+
try {
|
|
265
|
+
return JSON.stringify(item, (_k, v) => v instanceof Date ? v.getTime() : v);
|
|
266
|
+
} catch {
|
|
267
|
+
return `${item.id}#${Math.random()}`;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
var ItemStore = class _ItemStore {
|
|
271
|
+
zone;
|
|
272
|
+
opts;
|
|
273
|
+
items = /* @__PURE__ */ new Map();
|
|
274
|
+
timedByDay = /* @__PURE__ */ new Map();
|
|
275
|
+
spanning = /* @__PURE__ */ new Set();
|
|
276
|
+
pending = _ItemStore.emptyChange();
|
|
277
|
+
/** Bumped on every change; cheap staleness check for caches. */
|
|
278
|
+
version = 0;
|
|
279
|
+
constructor(zone, opts = {}) {
|
|
280
|
+
this.zone = zone;
|
|
281
|
+
this.opts = { ...DEFAULTS, ...opts };
|
|
282
|
+
}
|
|
283
|
+
static emptyChange() {
|
|
284
|
+
return { added: [], updated: [], removed: [], days: /* @__PURE__ */ new Set(), spans: false };
|
|
285
|
+
}
|
|
286
|
+
get size() {
|
|
287
|
+
return this.items.size;
|
|
288
|
+
}
|
|
289
|
+
get timeZone() {
|
|
290
|
+
return this.zone;
|
|
291
|
+
}
|
|
292
|
+
/** Re-resolves every item; used when the zone or defaults change. */
|
|
293
|
+
reconfigure(zone, opts = {}) {
|
|
294
|
+
this.zone = zone;
|
|
295
|
+
this.opts = { ...this.opts, ...opts };
|
|
296
|
+
const all = [...this.items.values()].map((n) => n.item);
|
|
297
|
+
this.items.clear();
|
|
298
|
+
this.timedByDay.clear();
|
|
299
|
+
this.spanning.clear();
|
|
300
|
+
for (const it of all) this.insert(it, "added");
|
|
301
|
+
this.pending.spans = true;
|
|
302
|
+
}
|
|
303
|
+
normalize(item) {
|
|
304
|
+
const zone = this.zone;
|
|
305
|
+
const s = parseDateInput(item.start, zone);
|
|
306
|
+
if (!s) return null;
|
|
307
|
+
const e = item.end != null && item.end !== "" ? parseDateInput(item.end, zone) : null;
|
|
308
|
+
const allDay = item.allDay ?? s.kind === "day";
|
|
309
|
+
const sig = signature(item);
|
|
310
|
+
if (allDay) {
|
|
311
|
+
const startDay2 = s.kind === "day" ? s.day : dayOf(s.ms, zone);
|
|
312
|
+
let endDay2 = startDay2 + this.opts.defaultAllDayDays;
|
|
313
|
+
if (e) {
|
|
314
|
+
const ed = e.kind === "day" ? e.day : dayOf(e.ms - 1, zone) + 1;
|
|
315
|
+
if (ed > startDay2) endDay2 = ed;
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
id: item.id,
|
|
319
|
+
item,
|
|
320
|
+
allDay: true,
|
|
321
|
+
startDay: startDay2,
|
|
322
|
+
endDay: endDay2,
|
|
323
|
+
startMs: startOfDayInstant(startDay2, zone),
|
|
324
|
+
endMs: startOfDayInstant(endDay2, zone),
|
|
325
|
+
sig
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
const startMs = s.kind === "instant" ? s.ms : startOfDayInstant(s.day, zone);
|
|
329
|
+
let endMs = startMs + this.opts.defaultTimedMinutes * 6e4;
|
|
330
|
+
if (e) {
|
|
331
|
+
const em = e.kind === "instant" ? e.ms : startOfDayInstant(e.day, zone);
|
|
332
|
+
if (em > startMs) endMs = em;
|
|
333
|
+
}
|
|
334
|
+
const startDay = dayOf(startMs, zone);
|
|
335
|
+
const endDay = dayOf(endMs - 1, zone) + 1;
|
|
336
|
+
return { id: item.id, item, allDay: false, startMs, endMs, startDay, endDay, sig };
|
|
337
|
+
}
|
|
338
|
+
insert(item, kind) {
|
|
339
|
+
const n = this.normalize(item);
|
|
340
|
+
if (!n) return false;
|
|
341
|
+
this.items.set(n.id, n);
|
|
342
|
+
this.index(n);
|
|
343
|
+
this.pending[kind].push(n.id);
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
index(n) {
|
|
347
|
+
if (n.item.display === "none") return;
|
|
348
|
+
if (n.allDay) {
|
|
349
|
+
this.spanning.add(n.id);
|
|
350
|
+
this.pending.spans = true;
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (n.endDay - n.startDay > 1) {
|
|
354
|
+
this.spanning.add(n.id);
|
|
355
|
+
this.pending.spans = true;
|
|
356
|
+
}
|
|
357
|
+
for (let d = n.startDay; d < n.endDay; d++) {
|
|
358
|
+
let set = this.timedByDay.get(d);
|
|
359
|
+
if (!set) this.timedByDay.set(d, set = /* @__PURE__ */ new Set());
|
|
360
|
+
set.add(n.id);
|
|
361
|
+
this.pending.days.add(d);
|
|
362
|
+
}
|
|
363
|
+
this.pending.spans = true;
|
|
364
|
+
}
|
|
365
|
+
unindex(n) {
|
|
366
|
+
if (this.spanning.delete(n.id)) this.pending.spans = true;
|
|
367
|
+
if (!n.allDay) {
|
|
368
|
+
for (let d = n.startDay; d < n.endDay; d++) {
|
|
369
|
+
this.timedByDay.get(d)?.delete(n.id);
|
|
370
|
+
this.pending.days.add(d);
|
|
371
|
+
}
|
|
372
|
+
this.pending.spans = true;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/** Replaces the whole item set, touching only items whose signature changed. */
|
|
376
|
+
setAll(items) {
|
|
377
|
+
const seen = /* @__PURE__ */ new Set();
|
|
378
|
+
for (const item of items) {
|
|
379
|
+
if (seen.has(item.id)) continue;
|
|
380
|
+
seen.add(item.id);
|
|
381
|
+
this.upsertOne(item);
|
|
382
|
+
}
|
|
383
|
+
for (const [id, n] of this.items) {
|
|
384
|
+
if (!seen.has(id)) {
|
|
385
|
+
this.unindex(n);
|
|
386
|
+
this.items.delete(id);
|
|
387
|
+
this.pending.removed.push(id);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return this.flush();
|
|
391
|
+
}
|
|
392
|
+
/** Upserts items without removing others. */
|
|
393
|
+
upsert(items) {
|
|
394
|
+
for (const item of items) this.upsertOne(item);
|
|
395
|
+
return this.flush();
|
|
396
|
+
}
|
|
397
|
+
remove(ids) {
|
|
398
|
+
for (const id of ids) {
|
|
399
|
+
const n = this.items.get(id);
|
|
400
|
+
if (!n) continue;
|
|
401
|
+
this.unindex(n);
|
|
402
|
+
this.items.delete(id);
|
|
403
|
+
this.pending.removed.push(id);
|
|
404
|
+
}
|
|
405
|
+
return this.flush();
|
|
406
|
+
}
|
|
407
|
+
upsertOne(item) {
|
|
408
|
+
const prev = this.items.get(item.id);
|
|
409
|
+
if (prev) {
|
|
410
|
+
if (prev.sig === signature(item)) {
|
|
411
|
+
if (prev.item !== item) this.items.set(item.id, { ...prev, item });
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
this.unindex(prev);
|
|
415
|
+
this.items.delete(item.id);
|
|
416
|
+
if (!this.insert(item, "updated")) this.pending.removed.push(item.id);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
this.insert(item, "added");
|
|
420
|
+
}
|
|
421
|
+
flush() {
|
|
422
|
+
const out = this.pending;
|
|
423
|
+
this.pending = _ItemStore.emptyChange();
|
|
424
|
+
if (out.added.length || out.updated.length || out.removed.length || out.spans || out.days.size) this.version++;
|
|
425
|
+
return out;
|
|
426
|
+
}
|
|
427
|
+
get(id) {
|
|
428
|
+
return this.items.get(id);
|
|
429
|
+
}
|
|
430
|
+
all() {
|
|
431
|
+
return this.items.values();
|
|
432
|
+
}
|
|
433
|
+
/** Timed items touching `day`. */
|
|
434
|
+
timedOn(day) {
|
|
435
|
+
const set = this.timedByDay.get(day);
|
|
436
|
+
if (!set) return [];
|
|
437
|
+
const out = [];
|
|
438
|
+
for (const id of set) {
|
|
439
|
+
const n = this.items.get(id);
|
|
440
|
+
if (n) out.push(n);
|
|
441
|
+
}
|
|
442
|
+
return out;
|
|
443
|
+
}
|
|
444
|
+
/** All-day items (and, if asked, multi-day timed items) overlapping [startDay, endDay). */
|
|
445
|
+
spanningIn(startDay, endDay, includeTimedMultiDay) {
|
|
446
|
+
const out = [];
|
|
447
|
+
for (const id of this.spanning) {
|
|
448
|
+
const n = this.items.get(id);
|
|
449
|
+
if (!n || n.startDay >= endDay || n.endDay <= startDay) continue;
|
|
450
|
+
if (!n.allDay && !includeTimedMultiDay) continue;
|
|
451
|
+
out.push(n);
|
|
452
|
+
}
|
|
453
|
+
return out;
|
|
454
|
+
}
|
|
455
|
+
/** Every visible item overlapping [startDay, endDay). */
|
|
456
|
+
inRange(startDay, endDay) {
|
|
457
|
+
const seen = /* @__PURE__ */ new Set();
|
|
458
|
+
const out = [];
|
|
459
|
+
for (const n of this.spanningIn(startDay, endDay, true)) {
|
|
460
|
+
seen.add(n.id);
|
|
461
|
+
out.push(n);
|
|
462
|
+
}
|
|
463
|
+
for (let d = startDay; d < endDay; d++) {
|
|
464
|
+
for (const n of this.timedOn(d)) {
|
|
465
|
+
if (seen.has(n.id)) continue;
|
|
466
|
+
seen.add(n.id);
|
|
467
|
+
out.push(n);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return out;
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
function daysOfRange(startMs, endMs, zone) {
|
|
474
|
+
return [dayOf(startMs, zone), dayOf(Math.max(startMs, endMs - 1), zone) + 1];
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/core/layout.ts
|
|
478
|
+
function segmentOn(n, day, zone) {
|
|
479
|
+
return segmentInWindow(n, day, 0, MINUTES_PER_DAY, zone);
|
|
480
|
+
}
|
|
481
|
+
function segmentInWindow(n, day, minMinute, maxMinute, zone) {
|
|
482
|
+
const winStart = toInstant(day, minMinute, zone);
|
|
483
|
+
const winEnd = toInstant(day, maxMinute, zone);
|
|
484
|
+
const s = Math.max(n.startMs, winStart);
|
|
485
|
+
const e = Math.min(n.endMs, winEnd);
|
|
486
|
+
const zeroLength = n.startMs === n.endMs && n.startMs >= winStart && n.startMs < winEnd;
|
|
487
|
+
if (e <= s && !zeroLength) return null;
|
|
488
|
+
const rel = (ms) => {
|
|
489
|
+
const w = wallTime(ms, zone);
|
|
490
|
+
return (w.day - day) * MINUTES_PER_DAY + w.minute;
|
|
491
|
+
};
|
|
492
|
+
const startMin = s === winStart ? minMinute : rel(s);
|
|
493
|
+
let endMin = e === winEnd ? maxMinute : rel(e);
|
|
494
|
+
if (endMin < startMin) endMin = Math.min(maxMinute, startMin + Math.round((e - s) / 6e4));
|
|
495
|
+
return {
|
|
496
|
+
item: n,
|
|
497
|
+
day,
|
|
498
|
+
startMin,
|
|
499
|
+
endMin,
|
|
500
|
+
isStart: n.startMs >= winStart,
|
|
501
|
+
isEnd: n.endMs <= winEnd
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
var defaultOrder = (a, b) => a.startMs - b.startMs || b.endMs - b.startMs - (a.endMs - a.startMs) || Number(b.allDay) - Number(a.allDay) || a.item.title.localeCompare(b.item.title);
|
|
505
|
+
function packTimed(segs, minMinutes, order = defaultOrder) {
|
|
506
|
+
const sorted = segs.map((s) => ({ ...s, end: Math.max(s.endMin, s.startMin + minMinutes) })).sort((a, b) => a.startMin - b.startMin || b.end - a.end || order(a.item, b.item));
|
|
507
|
+
const out = [];
|
|
508
|
+
let cluster = [];
|
|
509
|
+
let colEnds = [];
|
|
510
|
+
let clusterEnd = -Infinity;
|
|
511
|
+
let clusterIndex = 0;
|
|
512
|
+
const flush = () => {
|
|
513
|
+
if (cluster.length) clusterIndex++;
|
|
514
|
+
const cols = colEnds.length;
|
|
515
|
+
for (const p of cluster) {
|
|
516
|
+
p.cols = cols;
|
|
517
|
+
let span = 1;
|
|
518
|
+
while (p.col + span < cols && !cluster.some((o) => o.col === p.col + span && o.startMin < p.end && p.startMin < o.end)) span++;
|
|
519
|
+
p.span = span;
|
|
520
|
+
}
|
|
521
|
+
cluster = [];
|
|
522
|
+
colEnds = [];
|
|
523
|
+
};
|
|
524
|
+
for (const s of sorted) {
|
|
525
|
+
if (s.startMin >= clusterEnd) {
|
|
526
|
+
flush();
|
|
527
|
+
clusterEnd = -Infinity;
|
|
528
|
+
}
|
|
529
|
+
let col = colEnds.findIndex((end) => end <= s.startMin);
|
|
530
|
+
if (col < 0) {
|
|
531
|
+
col = colEnds.length;
|
|
532
|
+
colEnds.push(0);
|
|
533
|
+
}
|
|
534
|
+
colEnds[col] = s.end;
|
|
535
|
+
const p = { ...s, col, cols: 1, span: 1, cluster: clusterIndex };
|
|
536
|
+
cluster.push(p);
|
|
537
|
+
out.push(p);
|
|
538
|
+
clusterEnd = Math.max(clusterEnd, s.end);
|
|
539
|
+
}
|
|
540
|
+
flush();
|
|
541
|
+
for (const p of out) delete p.end;
|
|
542
|
+
return out;
|
|
543
|
+
}
|
|
544
|
+
function layoutTimedDay(items, day, zone, minMinutes, order, window = [0, MINUTES_PER_DAY]) {
|
|
545
|
+
const plan = [];
|
|
546
|
+
const actual = [];
|
|
547
|
+
const background = [];
|
|
548
|
+
for (const n of items) {
|
|
549
|
+
const seg = segmentInWindow(n, day, window[0], window[1], zone);
|
|
550
|
+
if (!seg) continue;
|
|
551
|
+
if (n.item.display === "background") background.push(seg);
|
|
552
|
+
else if (n.item.lane === "actual") actual.push(seg);
|
|
553
|
+
else plan.push(seg);
|
|
554
|
+
}
|
|
555
|
+
return { day, plan: packTimed(plan, minMinutes, order), actual: packTimed(actual, minMinutes, order), background };
|
|
556
|
+
}
|
|
557
|
+
function layoutRow(items, days, order = defaultOrder) {
|
|
558
|
+
const first = days[0];
|
|
559
|
+
const last = days[days.length - 1];
|
|
560
|
+
const countByCol = new Array(days.length).fill(0);
|
|
561
|
+
if (first === void 0 || last === void 0) return { placements: [], countByCol, levels: 0 };
|
|
562
|
+
const colOf = (d) => {
|
|
563
|
+
let lo = 0;
|
|
564
|
+
let hi = days.length;
|
|
565
|
+
while (lo < hi) {
|
|
566
|
+
const mid = lo + hi >> 1;
|
|
567
|
+
if (days[mid] < d) lo = mid + 1;
|
|
568
|
+
else hi = mid;
|
|
569
|
+
}
|
|
570
|
+
return lo;
|
|
571
|
+
};
|
|
572
|
+
const sorted = items.filter((n) => n.startDay <= last && n.endDay > first).sort((a, b) => {
|
|
573
|
+
const sa = Math.max(a.startDay, first);
|
|
574
|
+
const sb = Math.max(b.startDay, first);
|
|
575
|
+
return sa - sb || b.endDay - b.startDay - (a.endDay - a.startDay) || order(a, b);
|
|
576
|
+
});
|
|
577
|
+
const levelEnds = [];
|
|
578
|
+
const placements = [];
|
|
579
|
+
for (const n of sorted) {
|
|
580
|
+
const startCol = colOf(n.startDay);
|
|
581
|
+
const endCol = colOf(n.endDay);
|
|
582
|
+
if (endCol <= startCol) continue;
|
|
583
|
+
let level = 0;
|
|
584
|
+
for (; ; level++) {
|
|
585
|
+
const occ = levelEnds[level] ??= [];
|
|
586
|
+
let free = true;
|
|
587
|
+
for (let c = startCol; c < endCol; c++) if (occ[c]) free = false;
|
|
588
|
+
if (free) {
|
|
589
|
+
for (let c = startCol; c < endCol; c++) occ[c] = 1;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
for (let c = startCol; c < endCol; c++) countByCol[c]++;
|
|
594
|
+
placements.push({
|
|
595
|
+
item: n,
|
|
596
|
+
startCol,
|
|
597
|
+
endCol,
|
|
598
|
+
level,
|
|
599
|
+
isStart: n.startDay >= days[startCol],
|
|
600
|
+
isEnd: n.endDay <= days[endCol - 1] + 1
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
return { placements, countByCol, levels: levelEnds.length };
|
|
604
|
+
}
|
|
605
|
+
function layoutLane(items, windowStart, windowEnd) {
|
|
606
|
+
const sorted = items.filter((n) => n.endMs > windowStart && n.startMs < windowEnd).sort((a, b) => a.startMs - b.startMs || b.endMs - a.endMs);
|
|
607
|
+
const levelEnds = [];
|
|
608
|
+
const bars = [];
|
|
609
|
+
for (const n of sorted) {
|
|
610
|
+
let level = levelEnds.findIndex((end) => end <= n.startMs);
|
|
611
|
+
if (level < 0) {
|
|
612
|
+
level = levelEnds.length;
|
|
613
|
+
levelEnds.push(0);
|
|
614
|
+
}
|
|
615
|
+
levelEnds[level] = Math.max(n.endMs, n.startMs + 1);
|
|
616
|
+
bars.push({
|
|
617
|
+
item: n,
|
|
618
|
+
startMs: Math.max(n.startMs, windowStart),
|
|
619
|
+
endMs: Math.min(n.endMs, windowEnd),
|
|
620
|
+
clippedStart: n.startMs < windowStart,
|
|
621
|
+
clippedEnd: n.endMs > windowEnd,
|
|
622
|
+
level
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
return { bars, levels: levelEnds.length };
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// src/core/order.ts
|
|
629
|
+
var BUILTIN = {
|
|
630
|
+
start: (n) => n.startMs,
|
|
631
|
+
end: (n) => n.endMs,
|
|
632
|
+
duration: (n) => n.endMs - n.startMs,
|
|
633
|
+
allDay: (n) => n.allDay ? 0 : 1,
|
|
634
|
+
title: (n) => n.item.title,
|
|
635
|
+
id: (n) => n.id
|
|
636
|
+
};
|
|
637
|
+
function compare(a, b) {
|
|
638
|
+
if (a === b) return 0;
|
|
639
|
+
if (a == null) return 1;
|
|
640
|
+
if (b == null) return -1;
|
|
641
|
+
if (typeof a === "number" && typeof b === "number") return a - b;
|
|
642
|
+
const str = (v) => typeof v === "string" ? v : JSON.stringify(v) ?? "";
|
|
643
|
+
return str(a).localeCompare(str(b));
|
|
644
|
+
}
|
|
645
|
+
function parseOrder(spec) {
|
|
646
|
+
if (typeof spec === "function") return spec;
|
|
647
|
+
const parts = (spec ?? "start,-duration,allDay,title").split(",").map((p) => p.trim()).filter(Boolean).map((p) => {
|
|
648
|
+
const desc = p.startsWith("-");
|
|
649
|
+
const name = desc ? p.slice(1) : p;
|
|
650
|
+
const field = BUILTIN[name] ?? ((n) => {
|
|
651
|
+
const own = n.item[name];
|
|
652
|
+
if (own !== void 0) return own;
|
|
653
|
+
const data = n.item.data;
|
|
654
|
+
return data && typeof data === "object" ? data[name] : void 0;
|
|
655
|
+
});
|
|
656
|
+
return { field, sign: desc ? -1 : 1 };
|
|
657
|
+
});
|
|
658
|
+
return (a, b) => {
|
|
659
|
+
for (const { field, sign } of parts) {
|
|
660
|
+
const c = compare(field(a), field(b));
|
|
661
|
+
if (c) return c * sign;
|
|
662
|
+
}
|
|
663
|
+
return 0;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/core/plan.ts
|
|
668
|
+
function plannedMinutes(store, days) {
|
|
669
|
+
const zone = store.timeZone;
|
|
670
|
+
const out = /* @__PURE__ */ new Map();
|
|
671
|
+
for (const day of days) {
|
|
672
|
+
const start = toInstant(day, 0, zone);
|
|
673
|
+
const end = toInstant(day + 1, 0, zone);
|
|
674
|
+
let total = 0;
|
|
675
|
+
for (const n of store.timedOn(day)) {
|
|
676
|
+
if (n.item.display === "background" || n.item.lane === "actual") continue;
|
|
677
|
+
total += Math.max(0, Math.min(n.endMs, end) - Math.max(n.startMs, start)) / 6e4;
|
|
678
|
+
}
|
|
679
|
+
out.set(day, total);
|
|
680
|
+
}
|
|
681
|
+
return out;
|
|
682
|
+
}
|
|
683
|
+
function itemDependencies(item) {
|
|
684
|
+
return (item.dependsOn ?? []).map((d) => typeof d === "string" ? { id: d, type: "FS" } : { id: d.id, type: d.type ?? "FS" });
|
|
685
|
+
}
|
|
686
|
+
function dependencyType(fromEdge, toEdge) {
|
|
687
|
+
return `${fromEdge === "start" ? "S" : "F"}${toEdge === "start" ? "S" : "F"}`;
|
|
688
|
+
}
|
|
689
|
+
function dependencySatisfied(type, pred, succ) {
|
|
690
|
+
const from = type[0] === "S" ? pred.startMs : pred.endMs;
|
|
691
|
+
const to = type[1] === "S" ? succ.startMs : succ.endMs;
|
|
692
|
+
return from <= to;
|
|
693
|
+
}
|
|
694
|
+
function dependencyConflicts(store) {
|
|
695
|
+
const out = /* @__PURE__ */ new Map();
|
|
696
|
+
for (const n of store.all()) {
|
|
697
|
+
if (!n.item.dependsOn?.length) continue;
|
|
698
|
+
for (const dep of itemDependencies(n.item)) {
|
|
699
|
+
const d = store.get(dep.id);
|
|
700
|
+
if (!d || dependencySatisfied(dep.type, d, n)) continue;
|
|
701
|
+
const list = out.get(n.id) ?? [];
|
|
702
|
+
list.push(dep.id);
|
|
703
|
+
out.set(n.id, list);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return out;
|
|
707
|
+
}
|
|
708
|
+
function findFreeSlot(store, q) {
|
|
709
|
+
const zone = store.timeZone;
|
|
710
|
+
const snap = (q.snapMinutes ?? 15) * 6e4;
|
|
711
|
+
const need = q.durationMinutes * 6e4;
|
|
712
|
+
for (let day = q.fromDay; day < q.toDay; day++) {
|
|
713
|
+
if (q.skipDay?.(day)) continue;
|
|
714
|
+
const winStart = toInstant(day, q.window[0], zone);
|
|
715
|
+
const winEnd = toInstant(day, q.window[1], zone);
|
|
716
|
+
const busy = store.timedOn(day).filter((n) => n.item.display !== "background" && n.item.lane !== "actual" && !(q.ignore?.(n) ?? false)).map((n) => [n.startMs, n.endMs]).sort((a, b) => a[0] - b[0]);
|
|
717
|
+
let cursor = Math.max(winStart, q.notBefore ?? -Infinity);
|
|
718
|
+
cursor = Math.ceil(cursor / snap) * snap;
|
|
719
|
+
for (const [s, e] of busy) {
|
|
720
|
+
if (e <= cursor) continue;
|
|
721
|
+
if (s - cursor >= need && cursor + need <= winEnd) return { startMs: cursor, endMs: cursor + need };
|
|
722
|
+
cursor = Math.max(cursor, Math.ceil(e / snap) * snap);
|
|
723
|
+
if (cursor >= winEnd) break;
|
|
724
|
+
}
|
|
725
|
+
if (cursor + need <= winEnd) return { startMs: cursor, endMs: cursor + need };
|
|
726
|
+
}
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// src/dom/context.ts
|
|
731
|
+
function focusKey(el) {
|
|
732
|
+
const cls = [...el.classList].filter((c) => c.startsWith("tnc-"));
|
|
733
|
+
return cls.length ? cls.map((c) => `.${CSS.escape(c)}`).join("") : "";
|
|
734
|
+
}
|
|
735
|
+
var ItemPool = class {
|
|
736
|
+
constructor(ctx, container) {
|
|
737
|
+
this.ctx = ctx;
|
|
738
|
+
this.container = container;
|
|
739
|
+
}
|
|
740
|
+
ctx;
|
|
741
|
+
container;
|
|
742
|
+
recs = /* @__PURE__ */ new Map();
|
|
743
|
+
used = /* @__PURE__ */ new Set();
|
|
744
|
+
begin() {
|
|
745
|
+
this.used = /* @__PURE__ */ new Set();
|
|
746
|
+
}
|
|
747
|
+
place(key, meta) {
|
|
748
|
+
const sig = `${meta.n.sig}|${meta.variant}|${meta.isStart ? 1 : 0}${meta.isEnd ? 1 : 0}|${meta.mirror ? 1 : 0}`;
|
|
749
|
+
let rec = this.recs.get(key);
|
|
750
|
+
let refocus = null;
|
|
751
|
+
if (rec && rec.sig !== sig) {
|
|
752
|
+
const active = this.ctx.doc.activeElement;
|
|
753
|
+
if (active && rec.el.contains(active)) refocus = active === rec.el ? "" : focusKey(active);
|
|
754
|
+
this.ctx.destroyItemEl(rec.el);
|
|
755
|
+
rec = void 0;
|
|
756
|
+
}
|
|
757
|
+
if (!rec) {
|
|
758
|
+
rec = { el: this.ctx.createItemEl(meta), sig };
|
|
759
|
+
this.recs.set(key, rec);
|
|
760
|
+
this.container.append(rec.el);
|
|
761
|
+
if (refocus !== null) (refocus ? rec.el.querySelector(refocus) : rec.el)?.focus({ preventScroll: true });
|
|
762
|
+
} else {
|
|
763
|
+
this.ctx.meta.set(rec.el, meta);
|
|
764
|
+
this.ctx.refreshItemEl(rec.el, meta);
|
|
765
|
+
if (rec.el.parentNode !== this.container) this.container.append(rec.el);
|
|
766
|
+
}
|
|
767
|
+
this.used.add(key);
|
|
768
|
+
return rec.el;
|
|
769
|
+
}
|
|
770
|
+
end() {
|
|
771
|
+
for (const [key, rec] of this.recs) {
|
|
772
|
+
if (this.used.has(key)) continue;
|
|
773
|
+
this.ctx.destroyItemEl(rec.el);
|
|
774
|
+
this.recs.delete(key);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
clear() {
|
|
778
|
+
this.begin();
|
|
779
|
+
this.end();
|
|
780
|
+
}
|
|
781
|
+
elementsFor(id) {
|
|
782
|
+
const out = [];
|
|
783
|
+
for (const rec of this.recs.values()) if (this.ctx.meta.get(rec.el)?.n.id === id) out.push(rec.el);
|
|
784
|
+
return out;
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
// src/dom/dom.ts
|
|
789
|
+
function h(doc, tag, cls, attrs, ...children) {
|
|
790
|
+
const el = doc.createElement(tag);
|
|
791
|
+
if (cls) el.className = cls;
|
|
792
|
+
if (attrs) setAttrs(el, attrs);
|
|
793
|
+
for (const c of children) if (c !== null && c !== void 0 && c !== false) el.append(c);
|
|
794
|
+
return el;
|
|
795
|
+
}
|
|
796
|
+
function setAttrs(el, attrs) {
|
|
797
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
798
|
+
if (v === null || v === void 0 || v === false) el.removeAttribute(k);
|
|
799
|
+
else el.setAttribute(k, v === true ? "" : String(v));
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function classList(value) {
|
|
803
|
+
if (!value) return [];
|
|
804
|
+
return (Array.isArray(value) ? value : value.split(/\s+/)).filter(Boolean);
|
|
805
|
+
}
|
|
806
|
+
function setStyle(el, props) {
|
|
807
|
+
for (const [k, v] of Object.entries(props)) {
|
|
808
|
+
if (v === null) el.style.removeProperty(k);
|
|
809
|
+
else el.style.setProperty(k, typeof v === "number" ? `${v}px` : v);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
function pct(n) {
|
|
813
|
+
return `${Math.round(n * 1e4) / 100}%`;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// src/dom/format.ts
|
|
817
|
+
var Formatter = class {
|
|
818
|
+
cache = /* @__PURE__ */ new Map();
|
|
819
|
+
locale;
|
|
820
|
+
hour12;
|
|
821
|
+
constructor(locale, hour12) {
|
|
822
|
+
this.locale = locale;
|
|
823
|
+
const probe = new Intl.DateTimeFormat(locale, { hour: "numeric" }).resolvedOptions();
|
|
824
|
+
this.hour12 = hour12 ?? (probe.hour12 === true || probe.hourCycle === "h12" || probe.hourCycle === "h11");
|
|
825
|
+
}
|
|
826
|
+
fmt(key, opts) {
|
|
827
|
+
let f = this.cache.get(key);
|
|
828
|
+
if (!f) {
|
|
829
|
+
f = new Intl.DateTimeFormat(this.locale, { timeZone: "UTC", ...opts });
|
|
830
|
+
this.cache.set(key, f);
|
|
831
|
+
}
|
|
832
|
+
return f;
|
|
833
|
+
}
|
|
834
|
+
d(day) {
|
|
835
|
+
return new Date(day * DAY_MS);
|
|
836
|
+
}
|
|
837
|
+
weekdayShort(day) {
|
|
838
|
+
return this.fmt("wds", { weekday: "short" }).format(this.d(day));
|
|
839
|
+
}
|
|
840
|
+
weekdayLong(day) {
|
|
841
|
+
return this.fmt("wdl", { weekday: "long" }).format(this.d(day));
|
|
842
|
+
}
|
|
843
|
+
weekdayNarrow(day) {
|
|
844
|
+
return this.fmt("wdn", { weekday: "narrow" }).format(this.d(day));
|
|
845
|
+
}
|
|
846
|
+
dayNumber(day) {
|
|
847
|
+
return this.fmt("dn", { day: "numeric" }).format(this.d(day));
|
|
848
|
+
}
|
|
849
|
+
monthShort(day) {
|
|
850
|
+
return this.fmt("ms", { month: "short" }).format(this.d(day));
|
|
851
|
+
}
|
|
852
|
+
monthLong(day) {
|
|
853
|
+
return this.fmt("ml", { month: "long" }).format(this.d(day));
|
|
854
|
+
}
|
|
855
|
+
monthYear(day) {
|
|
856
|
+
return this.fmt("my", { month: "long", year: "numeric" }).format(this.d(day));
|
|
857
|
+
}
|
|
858
|
+
year(day) {
|
|
859
|
+
return this.fmt("y", { year: "numeric" }).format(this.d(day));
|
|
860
|
+
}
|
|
861
|
+
fullDate(day) {
|
|
862
|
+
return this.fmt("fd", { year: "numeric", month: "long", day: "numeric" }).format(this.d(day));
|
|
863
|
+
}
|
|
864
|
+
longDate(day) {
|
|
865
|
+
return this.fmt("ld", { weekday: "long", year: "numeric", month: "long", day: "numeric" }).format(this.d(day));
|
|
866
|
+
}
|
|
867
|
+
listDay(day) {
|
|
868
|
+
return this.fmt("lst", { weekday: "short", month: "short", day: "numeric" }).format(this.d(day));
|
|
869
|
+
}
|
|
870
|
+
/** "Oct 19 – 25, 2026" style. */
|
|
871
|
+
range(startDay, lastDay) {
|
|
872
|
+
if (startDay === lastDay) return this.fullDate(startDay);
|
|
873
|
+
const f = this.fmt("rng", { year: "numeric", month: "short", day: "numeric" });
|
|
874
|
+
try {
|
|
875
|
+
return f.formatRange(this.d(startDay), this.d(lastDay));
|
|
876
|
+
} catch {
|
|
877
|
+
return `${f.format(this.d(startDay))} \u2013 ${f.format(this.d(lastDay))}`;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
/** Wall-clock minutes to a label. `compact` drops ":00" in 12-hour formats ("9am"). */
|
|
881
|
+
time(minute, compact = false) {
|
|
882
|
+
const m = (Math.round(minute) % 1440 + 1440) % 1440;
|
|
883
|
+
const date = new Date(Date.UTC(2e3, 0, 1, Math.floor(m / 60), m % 60));
|
|
884
|
+
if (this.hour12) {
|
|
885
|
+
const parts = this.fmt("t12", { hour: "numeric", minute: "2-digit", hour12: true }).formatToParts(date);
|
|
886
|
+
const english = (this.fmt("t12", {}).resolvedOptions().locale ?? "en").startsWith("en");
|
|
887
|
+
const out = [];
|
|
888
|
+
parts.forEach((p, i) => {
|
|
889
|
+
const prev = parts[i - 1];
|
|
890
|
+
if (compact && p.type === "minute" && p.value === "00") {
|
|
891
|
+
if (prev?.type === "literal") out.pop();
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
if (compact && english && p.type === "dayPeriod") {
|
|
895
|
+
if (prev?.type === "literal" && /^\s+$/.test(prev.value)) out.pop();
|
|
896
|
+
out.push(p.value.toLowerCase().replace(/\./g, ""));
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
out.push(p.value);
|
|
900
|
+
});
|
|
901
|
+
return out.join("").trim();
|
|
902
|
+
}
|
|
903
|
+
return this.fmt("t24", { hour: "2-digit", minute: "2-digit", hourCycle: "h23" }).format(date);
|
|
904
|
+
}
|
|
905
|
+
/** Hour labels for the gutter. */
|
|
906
|
+
slotLabel(minute) {
|
|
907
|
+
return this.time(minute, true);
|
|
908
|
+
}
|
|
909
|
+
isoDate(day) {
|
|
910
|
+
const { year, month, day: d } = ymdOfDay(day);
|
|
911
|
+
return `${year}-${String(month).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
function localeFirstDay(locale) {
|
|
915
|
+
try {
|
|
916
|
+
const loc = new Intl.Locale(locale ?? new Intl.DateTimeFormat().resolvedOptions().locale);
|
|
917
|
+
const info = loc.getWeekInfo?.() ?? loc.weekInfo;
|
|
918
|
+
if (info) return info.firstDay % 7;
|
|
919
|
+
} catch {
|
|
920
|
+
}
|
|
921
|
+
return 0;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
// src/dom/options.ts
|
|
925
|
+
var TIMELINE_UNIT_DAYS = {
|
|
926
|
+
hour: 1 / 24,
|
|
927
|
+
day: 1,
|
|
928
|
+
week: 7,
|
|
929
|
+
month: 30.44,
|
|
930
|
+
quarter: 91.31
|
|
931
|
+
};
|
|
932
|
+
var DEFAULT_TEXT = {
|
|
933
|
+
today: "Today",
|
|
934
|
+
prev: "Previous",
|
|
935
|
+
next: "Next",
|
|
936
|
+
allDay: "All day",
|
|
937
|
+
noItems: "Nothing scheduled",
|
|
938
|
+
more: (n) => `+${n} more`,
|
|
939
|
+
itemCount: (n) => n === 1 ? "1 item" : `${n} items`,
|
|
940
|
+
weekNumber: (n) => `W${n}`,
|
|
941
|
+
views: { week: "Week", day: "Day", days: "Days", month: "Month", list: "List", year: "Year", timeline: "Timeline" },
|
|
942
|
+
viewHints: {}
|
|
943
|
+
};
|
|
944
|
+
function defaultOptions() {
|
|
945
|
+
return {
|
|
946
|
+
view: "week",
|
|
947
|
+
date: Date.now(),
|
|
948
|
+
dayCount: 3,
|
|
949
|
+
listDayCount: 7,
|
|
950
|
+
listStart: "date",
|
|
951
|
+
monthDisplay: "full",
|
|
952
|
+
timeZone: "local",
|
|
953
|
+
locale: void 0,
|
|
954
|
+
firstDay: void 0,
|
|
955
|
+
hour12: void 0,
|
|
956
|
+
weekNumbers: false,
|
|
957
|
+
weekends: true,
|
|
958
|
+
hiddenDays: [],
|
|
959
|
+
allDaySlot: true,
|
|
960
|
+
timeGrid: true,
|
|
961
|
+
todayHighlight: true,
|
|
962
|
+
todayWidth: 1,
|
|
963
|
+
slotMinTime: "00:00",
|
|
964
|
+
slotMaxTime: "24:00",
|
|
965
|
+
slotDuration: "00:30",
|
|
966
|
+
snapDuration: void 0,
|
|
967
|
+
scrollTime: "08:00",
|
|
968
|
+
slotHeight: 24,
|
|
969
|
+
expandRows: false,
|
|
970
|
+
nowIndicator: true,
|
|
971
|
+
selectMirror: true,
|
|
972
|
+
slotEventOverlap: true,
|
|
973
|
+
eventMinHeight: 15,
|
|
974
|
+
eventMaxStack: 0,
|
|
975
|
+
dayMaxEvents: true,
|
|
976
|
+
dayMaxEventRows: 0,
|
|
977
|
+
eventOrder: "start,-duration,allDay,title",
|
|
978
|
+
height: "fill",
|
|
979
|
+
editable: true,
|
|
980
|
+
selectable: true,
|
|
981
|
+
navLinks: true,
|
|
982
|
+
resizableFromStart: false,
|
|
983
|
+
longPressDelay: 350,
|
|
984
|
+
defaultTimedMinutes: 60,
|
|
985
|
+
defaultAllDayDays: 1,
|
|
986
|
+
capacityMinutes: null,
|
|
987
|
+
planning: false,
|
|
988
|
+
toolbar: true,
|
|
989
|
+
text: {},
|
|
990
|
+
timeline: null,
|
|
991
|
+
acceptNativeDrop: null,
|
|
992
|
+
selected: null
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
var LAYOUT_KEYS = /* @__PURE__ */ new Set([
|
|
996
|
+
"view",
|
|
997
|
+
"dayCount",
|
|
998
|
+
"listDayCount",
|
|
999
|
+
"listStart",
|
|
1000
|
+
"monthDisplay",
|
|
1001
|
+
"timeZone",
|
|
1002
|
+
"locale",
|
|
1003
|
+
"firstDay",
|
|
1004
|
+
"hour12",
|
|
1005
|
+
"weekNumbers",
|
|
1006
|
+
"weekends",
|
|
1007
|
+
"hiddenDays",
|
|
1008
|
+
"allDaySlot",
|
|
1009
|
+
"timeGrid",
|
|
1010
|
+
"todayHighlight",
|
|
1011
|
+
"todayWidth",
|
|
1012
|
+
"slotMinTime",
|
|
1013
|
+
"slotMaxTime",
|
|
1014
|
+
"slotDuration",
|
|
1015
|
+
"snapDuration",
|
|
1016
|
+
"slotHeight",
|
|
1017
|
+
"expandRows",
|
|
1018
|
+
"nowIndicator",
|
|
1019
|
+
"slotEventOverlap",
|
|
1020
|
+
"eventMinHeight",
|
|
1021
|
+
"eventMaxStack",
|
|
1022
|
+
"dayMaxEvents",
|
|
1023
|
+
"dayMaxEventRows",
|
|
1024
|
+
"eventOrder",
|
|
1025
|
+
"height",
|
|
1026
|
+
"capacityMinutes",
|
|
1027
|
+
"planning",
|
|
1028
|
+
"toolbar",
|
|
1029
|
+
"text",
|
|
1030
|
+
"timeline",
|
|
1031
|
+
"editable",
|
|
1032
|
+
"itemContent",
|
|
1033
|
+
"itemClassNames",
|
|
1034
|
+
"listItemContent",
|
|
1035
|
+
"dayHeaderContent",
|
|
1036
|
+
"dayClassNames"
|
|
1037
|
+
]);
|
|
1038
|
+
|
|
1039
|
+
// src/dom/views/daygrid.ts
|
|
1040
|
+
var BAR_GAP = 2;
|
|
1041
|
+
var CELL_PAD = 6;
|
|
1042
|
+
function px(cs, name, fallback) {
|
|
1043
|
+
const v = parseFloat(cs.getPropertyValue(name));
|
|
1044
|
+
return Number.isFinite(v) ? v : fallback;
|
|
1045
|
+
}
|
|
1046
|
+
var DayGridView = class {
|
|
1047
|
+
constructor(ctx) {
|
|
1048
|
+
this.ctx = ctx;
|
|
1049
|
+
const { doc, opts, days } = ctx;
|
|
1050
|
+
this.dotsMode = opts.monthDisplay === "dots";
|
|
1051
|
+
this.el = h(doc, "div", this.dotsMode ? "tnc-dg tnc-dg--dots" : "tnc-dg");
|
|
1052
|
+
this.scroller = h(doc, "div", "tnc-dg-scroll");
|
|
1053
|
+
this.grid = h(doc, "div", "tnc-dg-grid", { role: "grid", "aria-label": ctx.view.title });
|
|
1054
|
+
const perWeek = this.daysPerWeek();
|
|
1055
|
+
const cols = `${opts.weekNumbers ? "var(--tnc-weeknum-w) " : ""}repeat(${perWeek}, minmax(0, 1fr))`;
|
|
1056
|
+
this.el.style.setProperty("--tnc-cols", cols);
|
|
1057
|
+
const head = h(doc, "div", "tnc-dg-head", { role: "row" });
|
|
1058
|
+
if (opts.weekNumbers) head.append(h(doc, "div", "tnc-weeknum-head"));
|
|
1059
|
+
for (const d of days.slice(0, perWeek)) {
|
|
1060
|
+
const arg = ctx.dayArg(d);
|
|
1061
|
+
const cell = h(doc, "div", "tnc-dg-dow", { role: "columnheader", "aria-label": ctx.fmt.weekdayLong(d) });
|
|
1062
|
+
const custom = opts.dayHeaderContent?.(arg);
|
|
1063
|
+
cell.append(custom ?? ctx.fmt.weekdayShort(d));
|
|
1064
|
+
head.append(cell);
|
|
1065
|
+
opts.dayHeaderDidMount?.({ ...arg, el: cell });
|
|
1066
|
+
}
|
|
1067
|
+
this.grid.append(head);
|
|
1068
|
+
const anchorMonth = ymdOfDay(ctx.view.anchor).month;
|
|
1069
|
+
for (let i = 0; i < days.length; i += perWeek) {
|
|
1070
|
+
const wdays = days.slice(i, i + perWeek);
|
|
1071
|
+
const row = h(doc, "div", "tnc-dg-week", { role: "rowgroup" });
|
|
1072
|
+
const dates = h(doc, "div", "tnc-dg-dates", { role: "row" });
|
|
1073
|
+
if (opts.weekNumbers) dates.append(h(doc, "div", "tnc-weeknum", { role: "rowheader" }, ctx.text.weekNumber(isoWeekNumber(wdays[0]))));
|
|
1074
|
+
const cells = wdays.map((d) => {
|
|
1075
|
+
const arg = ctx.dayArg(d);
|
|
1076
|
+
const cls = ["tnc-dg-day", ...ctx.dayClasses(d)];
|
|
1077
|
+
if (ymdOfDay(d).month !== anchorMonth) cls.push("is-other-month");
|
|
1078
|
+
const cell = h(doc, "div", cls.join(" "), { role: "gridcell", "aria-label": ctx.fmt.longDate(d), "data-date": arg.dateStr });
|
|
1079
|
+
if (this.dotsMode) cell.tabIndex = 0;
|
|
1080
|
+
const num2 = ctx.fmt.dayNumber(d);
|
|
1081
|
+
const label = ymdOfDay(d).day === 1 ? `${ctx.fmt.monthShort(d)} ${num2}` : num2;
|
|
1082
|
+
cell.append(
|
|
1083
|
+
h(
|
|
1084
|
+
doc,
|
|
1085
|
+
"div",
|
|
1086
|
+
"tnc-dg-top",
|
|
1087
|
+
null,
|
|
1088
|
+
opts.navLinks ? ctx.navLink(d, label, "tnc-dg-num") : h(doc, "span", "tnc-dg-num", null, label)
|
|
1089
|
+
)
|
|
1090
|
+
);
|
|
1091
|
+
dates.append(cell);
|
|
1092
|
+
return cell;
|
|
1093
|
+
});
|
|
1094
|
+
const layer = h(doc, "div", "tnc-dg-layer", { role: "row" });
|
|
1095
|
+
const content = h(doc, "div", "tnc-dg-content", { role: "gridcell", "aria-colspan": String(perWeek), "aria-label": ctx.fmt.range(wdays[0], wdays[wdays.length - 1]) });
|
|
1096
|
+
const sel = h(doc, "div", "tnc-sel-layer");
|
|
1097
|
+
const more = h(doc, "div", "tnc-more-layer");
|
|
1098
|
+
content.append(sel, more);
|
|
1099
|
+
layer.append(content);
|
|
1100
|
+
row.append(dates, layer);
|
|
1101
|
+
this.grid.append(row);
|
|
1102
|
+
const dots = this.dotsMode ? cells.map((cell) => {
|
|
1103
|
+
const box = h(doc, "div", "tnc-dg-dots", { "aria-hidden": "true" });
|
|
1104
|
+
if (opts.dayMaxEvents === false) box.classList.add("is-unlimited");
|
|
1105
|
+
const count = h(doc, "span", "tnc-dg-dots-more");
|
|
1106
|
+
cell.append(box);
|
|
1107
|
+
return { box, pool: new ItemPool(ctx, box), count };
|
|
1108
|
+
}) : null;
|
|
1109
|
+
this.weeks.push({ days: wdays, row, cells, layer, sel, more, pool: new ItemPool(ctx, content), dots });
|
|
1110
|
+
}
|
|
1111
|
+
this.scroller.append(this.grid);
|
|
1112
|
+
this.el.append(this.scroller);
|
|
1113
|
+
this.el.classList.toggle("tnc-dg--auto", opts.height === "auto");
|
|
1114
|
+
const RO = ctx.win.ResizeObserver;
|
|
1115
|
+
if (RO && (opts.dayMaxEvents === true || this.dotsMode && opts.dayMaxEvents !== false)) {
|
|
1116
|
+
this.resizeObs = new RO(() => {
|
|
1117
|
+
if (this.sizeKey() !== this.lastHeights) this.render(null);
|
|
1118
|
+
});
|
|
1119
|
+
this.resizeObs.observe(this.grid);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
ctx;
|
|
1123
|
+
el;
|
|
1124
|
+
scroller;
|
|
1125
|
+
grid;
|
|
1126
|
+
weeks = [];
|
|
1127
|
+
resizeObs = null;
|
|
1128
|
+
barPx = 20;
|
|
1129
|
+
morePx = 20;
|
|
1130
|
+
topPx = 24;
|
|
1131
|
+
lastHeights = "";
|
|
1132
|
+
dotsMode;
|
|
1133
|
+
daysPerWeek() {
|
|
1134
|
+
const { days } = this.ctx;
|
|
1135
|
+
const first = days[0];
|
|
1136
|
+
if (first === void 0) return 7;
|
|
1137
|
+
let n = 0;
|
|
1138
|
+
while (n < days.length && days[n] < first + 7) n++;
|
|
1139
|
+
return n || 7;
|
|
1140
|
+
}
|
|
1141
|
+
sizeKey() {
|
|
1142
|
+
return `${this.grid.clientWidth}|${this.weeks.map((w) => w.row.clientHeight).join(",")}`;
|
|
1143
|
+
}
|
|
1144
|
+
render(_change) {
|
|
1145
|
+
const { ctx } = this;
|
|
1146
|
+
const cs = ctx.win.getComputedStyle(this.el);
|
|
1147
|
+
this.barPx = px(cs, "--tnc-bar-h", 20);
|
|
1148
|
+
this.topPx = this.weeks[0]?.cells[0]?.querySelector(".tnc-dg-top")?.offsetHeight || px(cs, "--tnc-dg-top-h", 24);
|
|
1149
|
+
this.morePx = this.measureMore(cs);
|
|
1150
|
+
this.lastHeights = this.sizeKey();
|
|
1151
|
+
for (const w of this.weeks) {
|
|
1152
|
+
if (w.dots) this.renderDots(w);
|
|
1153
|
+
else this.renderWeek(w);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
/** Height of a "+N more" link as the host styles it (`--tnc-more-h`, or a taller min-height). */
|
|
1157
|
+
measureMore(cs) {
|
|
1158
|
+
const fallback = px(cs, "--tnc-more-h", this.barPx);
|
|
1159
|
+
const layer = this.weeks[0]?.more;
|
|
1160
|
+
if (this.dotsMode || !layer) return fallback;
|
|
1161
|
+
const probe = h(this.ctx.doc, "button", "tnc-more", { type: "button", tabindex: "-1", "aria-hidden": "true" }, "+0");
|
|
1162
|
+
probe.style.visibility = "hidden";
|
|
1163
|
+
layer.append(probe);
|
|
1164
|
+
const measured = probe.offsetHeight;
|
|
1165
|
+
probe.remove();
|
|
1166
|
+
return Math.max(fallback, measured);
|
|
1167
|
+
}
|
|
1168
|
+
limitFor(w) {
|
|
1169
|
+
const { dayMaxEvents, dayMaxEventRows, height } = this.ctx.opts;
|
|
1170
|
+
const rows = (n) => ({ fit: n, withMore: n - 1 });
|
|
1171
|
+
if (dayMaxEventRows > 0) return rows(dayMaxEventRows);
|
|
1172
|
+
if (typeof dayMaxEvents === "number" && dayMaxEvents > 0) return rows(dayMaxEvents + 1);
|
|
1173
|
+
if (dayMaxEvents !== true) return null;
|
|
1174
|
+
if (height === "auto") return rows(5);
|
|
1175
|
+
const step = this.barPx + BAR_GAP;
|
|
1176
|
+
const avail = w.row.clientHeight - this.topPx - CELL_PAD;
|
|
1177
|
+
return {
|
|
1178
|
+
fit: Math.max(1, Math.floor((avail + BAR_GAP) / step)),
|
|
1179
|
+
withMore: Math.max(0, Math.floor((avail - this.morePx) / step))
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
itemsFor(w) {
|
|
1183
|
+
const { store } = this.ctx;
|
|
1184
|
+
const first = w.days[0];
|
|
1185
|
+
const last = w.days[w.days.length - 1];
|
|
1186
|
+
const out = store.spanningIn(first, last + 1, true).filter((n) => n.item.display !== "background");
|
|
1187
|
+
const seen = new Set(out.map((n) => n.id));
|
|
1188
|
+
for (const d of w.days) {
|
|
1189
|
+
for (const n of store.timedOn(d)) {
|
|
1190
|
+
if (seen.has(n.id) || n.item.display === "background") continue;
|
|
1191
|
+
seen.add(n.id);
|
|
1192
|
+
out.push(n);
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
return out;
|
|
1196
|
+
}
|
|
1197
|
+
renderWeek(w) {
|
|
1198
|
+
const { ctx } = this;
|
|
1199
|
+
const row = layoutRow(this.itemsFor(w), w.days, ctx.order);
|
|
1200
|
+
const limit = this.limitFor(w);
|
|
1201
|
+
const visible = (col) => !limit ? Infinity : (row.countByCol[col] ?? 0) > limit.fit ? limit.withMore : limit.fit;
|
|
1202
|
+
const n = w.days.length;
|
|
1203
|
+
const hidden = new Array(n).fill([]).map(() => []);
|
|
1204
|
+
w.pool.begin();
|
|
1205
|
+
let maxLevel = -1;
|
|
1206
|
+
for (const p of row.placements) {
|
|
1207
|
+
let level = p.level;
|
|
1208
|
+
let room = Infinity;
|
|
1209
|
+
for (let c = p.startCol; c < p.endCol; c++) room = Math.min(room, visible(c));
|
|
1210
|
+
if (level >= room) {
|
|
1211
|
+
if (p.item.id !== ctx.draggingId) {
|
|
1212
|
+
for (let c = p.startCol; c < p.endCol; c++) hidden[c].push(p.item);
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
level = Math.max(0, room - 1);
|
|
1216
|
+
}
|
|
1217
|
+
maxLevel = Math.max(maxLevel, level);
|
|
1218
|
+
const single = !p.item.allDay && p.item.endDay - p.item.startDay <= 1;
|
|
1219
|
+
const el = w.pool.place(p.item.id, {
|
|
1220
|
+
n: p.item,
|
|
1221
|
+
day: w.days[p.startCol],
|
|
1222
|
+
isStart: p.isStart,
|
|
1223
|
+
isEnd: p.isEnd,
|
|
1224
|
+
variant: single ? "dot" : "bar",
|
|
1225
|
+
mirror: false
|
|
1226
|
+
});
|
|
1227
|
+
setStyle(el, {
|
|
1228
|
+
left: `calc(${pct(p.startCol / n)} + 2px)`,
|
|
1229
|
+
width: `calc(${pct((p.endCol - p.startCol) / n)} - 4px)`,
|
|
1230
|
+
top: `calc(var(--tnc-dg-top-h) + ${level} * (var(--tnc-bar-h) + ${BAR_GAP}px))`
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
w.pool.end();
|
|
1234
|
+
w.more.replaceChildren();
|
|
1235
|
+
hidden.forEach((items, col) => {
|
|
1236
|
+
if (!items.length) return;
|
|
1237
|
+
const day = w.days[col];
|
|
1238
|
+
const link = h(ctx.doc, "button", "tnc-more", { type: "button", "aria-haspopup": "dialog" }, ctx.text.more(items.length));
|
|
1239
|
+
setStyle(link, {
|
|
1240
|
+
left: `calc(${pct(col / n)} + 2px)`,
|
|
1241
|
+
width: `calc(${pct(1 / n)} - 4px)`,
|
|
1242
|
+
top: `calc(var(--tnc-dg-top-h) + ${visible(col)} * (var(--tnc-bar-h) + ${BAR_GAP}px))`
|
|
1243
|
+
});
|
|
1244
|
+
link.addEventListener("click", (ev) => ctx.openMore(day, link, ev));
|
|
1245
|
+
w.more.append(link);
|
|
1246
|
+
});
|
|
1247
|
+
const step = this.barPx + BAR_GAP;
|
|
1248
|
+
let need = maxLevel >= 0 ? (maxLevel + 1) * step - BAR_GAP : 0;
|
|
1249
|
+
hidden.forEach((items, col) => {
|
|
1250
|
+
if (items.length) need = Math.max(need, visible(col) * step + this.morePx);
|
|
1251
|
+
});
|
|
1252
|
+
w.row.style.setProperty("--tnc-levels", String(maxLevel + 1));
|
|
1253
|
+
w.row.style.setProperty("--tnc-dg-need", `${this.topPx + need + CELL_PAD}px`);
|
|
1254
|
+
}
|
|
1255
|
+
/** Items touching one day, all-day first, as a dot cell lists them. */
|
|
1256
|
+
itemsOn(day) {
|
|
1257
|
+
const { store } = this.ctx;
|
|
1258
|
+
const out = store.spanningIn(day, day + 1, true);
|
|
1259
|
+
const seen = new Set(out.map((n) => n.id));
|
|
1260
|
+
for (const n of store.timedOn(day)) if (!seen.has(n.id)) out.push(n);
|
|
1261
|
+
return out.filter((n) => n.item.display !== "background" && n.item.display !== "none").sort((a, b) => Number(b.allDay) - Number(a.allDay) || this.ctx.order(a, b));
|
|
1262
|
+
}
|
|
1263
|
+
/** `monthDisplay: "dots"`: each cell wraps one dot per item, then "+N" for the rest. */
|
|
1264
|
+
renderDots(w) {
|
|
1265
|
+
const { ctx } = this;
|
|
1266
|
+
const { dayMaxEvents } = ctx.opts;
|
|
1267
|
+
const first = w.dots[0].box;
|
|
1268
|
+
const cs = ctx.win.getComputedStyle(first);
|
|
1269
|
+
const size = px(cs, "--tnc-dot-size", 8);
|
|
1270
|
+
const gap = px(cs, "--tnc-dot-gap", 3);
|
|
1271
|
+
const step = size + gap;
|
|
1272
|
+
const perRow = Math.max(1, Math.floor((first.clientWidth + gap) / step));
|
|
1273
|
+
const rows = Math.max(1, Math.floor((first.clientHeight + gap) / step));
|
|
1274
|
+
const fits = dayMaxEvents === false ? Infinity : perRow * rows;
|
|
1275
|
+
const cap = typeof dayMaxEvents === "number" && dayMaxEvents > 0 ? Math.min(dayMaxEvents, fits) : fits;
|
|
1276
|
+
w.row.style.setProperty("--tnc-levels", "0");
|
|
1277
|
+
w.row.style.removeProperty("--tnc-dg-need");
|
|
1278
|
+
w.days.forEach((day, i) => {
|
|
1279
|
+
const cell = w.dots[i];
|
|
1280
|
+
const items = this.itemsOn(day);
|
|
1281
|
+
const shown = items.length <= cap ? items.length : cap >= fits ? Math.max(0, cap - 2) : cap;
|
|
1282
|
+
cell.pool.begin();
|
|
1283
|
+
const els = items.slice(0, shown).map((n) => cell.pool.place(n.id, { n, day, isStart: n.startDay >= day, isEnd: n.endDay <= day + 1, variant: "indicator", mirror: false }));
|
|
1284
|
+
cell.pool.end();
|
|
1285
|
+
els.forEach((el, k) => {
|
|
1286
|
+
if (cell.box.children[k] !== el) cell.box.insertBefore(el, cell.box.children[k] ?? null);
|
|
1287
|
+
});
|
|
1288
|
+
const rest = items.length - shown;
|
|
1289
|
+
cell.count.textContent = rest > 0 ? `+${rest}` : "";
|
|
1290
|
+
if (rest > 0) cell.box.append(cell.count);
|
|
1291
|
+
else cell.count.remove();
|
|
1292
|
+
const label = ctx.fmt.longDate(day);
|
|
1293
|
+
w.cells[i].setAttribute("aria-label", items.length ? `${label}, ${ctx.text.itemCount(items.length)}` : label);
|
|
1294
|
+
});
|
|
1295
|
+
}
|
|
1296
|
+
showSelection(sel) {
|
|
1297
|
+
for (const w of this.weeks) {
|
|
1298
|
+
w.sel.replaceChildren();
|
|
1299
|
+
if (!sel) continue;
|
|
1300
|
+
const n = w.days.length;
|
|
1301
|
+
const startCol = w.days.findIndex((d) => d >= sel.startDay);
|
|
1302
|
+
if (startCol < 0) continue;
|
|
1303
|
+
let endCol = w.days.findIndex((d) => d >= sel.endDay);
|
|
1304
|
+
if (endCol < 0) endCol = n;
|
|
1305
|
+
if (endCol <= startCol) continue;
|
|
1306
|
+
const box = h(this.ctx.doc, "div", "tnc-sel");
|
|
1307
|
+
setStyle(box, { left: pct(startCol / n), width: pct((endCol - startCol) / n), top: "0", bottom: "0" });
|
|
1308
|
+
w.sel.append(box);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
hit(x, y) {
|
|
1312
|
+
for (const w of this.weeks) {
|
|
1313
|
+
const r = w.row.getBoundingClientRect();
|
|
1314
|
+
if (y < r.top || y >= r.bottom) continue;
|
|
1315
|
+
for (let i = 0; i < w.cells.length; i++) {
|
|
1316
|
+
const cr = w.cells[i].getBoundingClientRect();
|
|
1317
|
+
if (x >= cr.left && x < cr.right) return { kind: "day", day: w.days[i] };
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
return null;
|
|
1321
|
+
}
|
|
1322
|
+
getScroll() {
|
|
1323
|
+
return { top: this.scroller.scrollTop, left: 0 };
|
|
1324
|
+
}
|
|
1325
|
+
setScroll(s) {
|
|
1326
|
+
this.scroller.scrollTop = s.top;
|
|
1327
|
+
}
|
|
1328
|
+
elementsFor(id) {
|
|
1329
|
+
return this.weeks.flatMap((w) => [...w.pool.elementsFor(id), ...(w.dots ?? []).flatMap((c) => c.pool.elementsFor(id))]);
|
|
1330
|
+
}
|
|
1331
|
+
destroy() {
|
|
1332
|
+
this.resizeObs?.disconnect();
|
|
1333
|
+
for (const w of this.weeks) {
|
|
1334
|
+
w.pool.clear();
|
|
1335
|
+
for (const c of w.dots ?? []) c.pool.clear();
|
|
1336
|
+
}
|
|
1337
|
+
this.el.remove();
|
|
1338
|
+
}
|
|
1339
|
+
};
|
|
1340
|
+
|
|
1341
|
+
// src/dom/views/list.ts
|
|
1342
|
+
var ListView = class {
|
|
1343
|
+
constructor(ctx) {
|
|
1344
|
+
this.ctx = ctx;
|
|
1345
|
+
this.el = h(ctx.doc, "div", "tnc-list");
|
|
1346
|
+
this.scroller = h(ctx.doc, "div", "tnc-list-scroll");
|
|
1347
|
+
this.body = h(ctx.doc, "div", "tnc-list-body", { role: "list", "aria-label": ctx.view.title });
|
|
1348
|
+
this.scroller.append(this.body);
|
|
1349
|
+
this.el.append(this.scroller);
|
|
1350
|
+
}
|
|
1351
|
+
ctx;
|
|
1352
|
+
el;
|
|
1353
|
+
scroller;
|
|
1354
|
+
body;
|
|
1355
|
+
pools = /* @__PURE__ */ new Map();
|
|
1356
|
+
sections = /* @__PURE__ */ new Map();
|
|
1357
|
+
itemsOn(day) {
|
|
1358
|
+
const { store, opts } = this.ctx;
|
|
1359
|
+
const out = store.spanningIn(day, day + 1, true);
|
|
1360
|
+
const seen = new Set(out.map((n) => n.id));
|
|
1361
|
+
for (const n of store.timedOn(day)) if (!seen.has(n.id)) out.push(n);
|
|
1362
|
+
return out.filter((n) => n.item.display !== "background" && n.item.display !== "none" && (opts.allDaySlot || !n.allDay)).sort((a, b) => Number(b.allDay) - Number(a.allDay) || this.ctx.order(a, b));
|
|
1363
|
+
}
|
|
1364
|
+
render(_change) {
|
|
1365
|
+
const { ctx } = this;
|
|
1366
|
+
const { doc, days } = ctx;
|
|
1367
|
+
const keep = /* @__PURE__ */ new Set();
|
|
1368
|
+
let prev = null;
|
|
1369
|
+
for (const day of days) {
|
|
1370
|
+
const items = this.itemsOn(day);
|
|
1371
|
+
if (!items.length) continue;
|
|
1372
|
+
keep.add(day);
|
|
1373
|
+
let section = this.sections.get(day);
|
|
1374
|
+
let pool = this.pools.get(day);
|
|
1375
|
+
if (!section || !pool) {
|
|
1376
|
+
const arg = ctx.dayArg(day);
|
|
1377
|
+
const heading = h(doc, "div", ["tnc-list-day", ...ctx.dayClasses(day)].join(" "), { "data-date": arg.dateStr });
|
|
1378
|
+
const custom = ctx.opts.dayHeaderContent?.(arg);
|
|
1379
|
+
const label = custom ?? ctx.fmt.longDate(day);
|
|
1380
|
+
heading.append(ctx.opts.navLinks ? ctx.navLink(day, label, "tnc-list-day-link") : label);
|
|
1381
|
+
const rows2 = h(doc, "div", "tnc-list-rows", { role: "group" });
|
|
1382
|
+
section = h(doc, "div", "tnc-list-section", { role: "listitem", "aria-label": ctx.fmt.longDate(day), "data-date": arg.dateStr }, heading, rows2);
|
|
1383
|
+
ctx.opts.dayHeaderDidMount?.({ ...arg, el: heading });
|
|
1384
|
+
pool = new ItemPool(ctx, rows2);
|
|
1385
|
+
this.sections.set(day, section);
|
|
1386
|
+
this.pools.set(day, pool);
|
|
1387
|
+
}
|
|
1388
|
+
pool.begin();
|
|
1389
|
+
for (const n of items) {
|
|
1390
|
+
pool.place(n.id, {
|
|
1391
|
+
n,
|
|
1392
|
+
day,
|
|
1393
|
+
isStart: n.startDay === day,
|
|
1394
|
+
isEnd: n.endDay === day + 1,
|
|
1395
|
+
variant: "list",
|
|
1396
|
+
mirror: false
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
pool.end();
|
|
1400
|
+
const rows = pool.container;
|
|
1401
|
+
for (const n of items) {
|
|
1402
|
+
const el = pool.elementsFor(n.id)[0];
|
|
1403
|
+
if (el) rows.append(el);
|
|
1404
|
+
}
|
|
1405
|
+
if (prev ? prev.nextSibling !== section : this.body.firstChild !== section) {
|
|
1406
|
+
if (prev) prev.after(section);
|
|
1407
|
+
else this.body.prepend(section);
|
|
1408
|
+
}
|
|
1409
|
+
prev = section;
|
|
1410
|
+
}
|
|
1411
|
+
for (const [day, section] of this.sections) {
|
|
1412
|
+
if (keep.has(day)) continue;
|
|
1413
|
+
this.pools.get(day)?.clear();
|
|
1414
|
+
this.pools.delete(day);
|
|
1415
|
+
section.remove();
|
|
1416
|
+
this.sections.delete(day);
|
|
1417
|
+
}
|
|
1418
|
+
let empty = this.body.querySelector(":scope > .tnc-list-empty");
|
|
1419
|
+
if (!keep.size) {
|
|
1420
|
+
if (!empty) {
|
|
1421
|
+
empty = h(doc, "div", "tnc-list-empty", { role: "listitem" }, ctx.text.noItems);
|
|
1422
|
+
this.body.append(empty);
|
|
1423
|
+
}
|
|
1424
|
+
} else empty?.remove();
|
|
1425
|
+
}
|
|
1426
|
+
showSelection(_sel) {
|
|
1427
|
+
}
|
|
1428
|
+
hit(_x, _y) {
|
|
1429
|
+
return null;
|
|
1430
|
+
}
|
|
1431
|
+
getScroll() {
|
|
1432
|
+
return { top: this.scroller.scrollTop, left: 0 };
|
|
1433
|
+
}
|
|
1434
|
+
setScroll(s) {
|
|
1435
|
+
this.scroller.scrollTop = s.top;
|
|
1436
|
+
}
|
|
1437
|
+
elementsFor(id) {
|
|
1438
|
+
return [...this.pools.values()].flatMap((p) => p.elementsFor(id));
|
|
1439
|
+
}
|
|
1440
|
+
destroy() {
|
|
1441
|
+
for (const p of this.pools.values()) p.clear();
|
|
1442
|
+
this.el.remove();
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
|
|
1446
|
+
// src/dom/views/timegrid.ts
|
|
1447
|
+
var BAR_GAP2 = 2;
|
|
1448
|
+
var TimeGridView = class {
|
|
1449
|
+
constructor(ctx) {
|
|
1450
|
+
this.ctx = ctx;
|
|
1451
|
+
const { doc, opts } = ctx;
|
|
1452
|
+
const showGrid = opts.timeGrid;
|
|
1453
|
+
this.el = h(doc, "div", "tnc-tg", { role: "table", "aria-label": ctx.view.title });
|
|
1454
|
+
this.el.classList.toggle("tnc-tg--no-grid", !showGrid);
|
|
1455
|
+
this.scroller = h(doc, "div", "tnc-tg-scroll");
|
|
1456
|
+
this.top = h(doc, "div", "tnc-tg-top");
|
|
1457
|
+
this.headRow = h(doc, "div", "tnc-tg-head tnc-tg-cols", { role: "row" });
|
|
1458
|
+
this.top.append(this.headRow);
|
|
1459
|
+
if (opts.allDaySlot || !showGrid) {
|
|
1460
|
+
this.allDayRow = h(doc, "div", "tnc-tg-allday tnc-tg-cols", { role: "row" });
|
|
1461
|
+
const label = h(doc, "div", "tnc-gutter-label", { role: "rowheader" }, showGrid ? ctx.text.allDay : "");
|
|
1462
|
+
this.allDayLayer = h(doc, "div", "tnc-allday-layer", { role: "cell", "aria-colspan": String(ctx.days.length) });
|
|
1463
|
+
this.allDaySel = h(doc, "div", "tnc-sel-layer");
|
|
1464
|
+
this.allDayMore = h(doc, "div", "tnc-more-layer");
|
|
1465
|
+
this.allDayLayer.append(this.allDaySel, this.allDayMore);
|
|
1466
|
+
this.allDayRow.append(label, this.allDayLayer);
|
|
1467
|
+
this.allDayPool = new ItemPool(ctx, this.allDayLayer);
|
|
1468
|
+
this.top.append(this.allDayRow);
|
|
1469
|
+
} else {
|
|
1470
|
+
this.allDayRow = this.allDayLayer = this.allDaySel = this.allDayMore = null;
|
|
1471
|
+
this.allDayPool = null;
|
|
1472
|
+
}
|
|
1473
|
+
this.scroller.append(this.top);
|
|
1474
|
+
if (showGrid) {
|
|
1475
|
+
this.body = h(doc, "div", "tnc-tg-body tnc-tg-cols", { role: "row" });
|
|
1476
|
+
this.gutter = h(doc, "div", "tnc-gutter", { "aria-hidden": "true" });
|
|
1477
|
+
this.body.append(this.gutter);
|
|
1478
|
+
this.nowGutter = h(doc, "div", "tnc-now-label");
|
|
1479
|
+
this.scroller.append(this.body);
|
|
1480
|
+
} else {
|
|
1481
|
+
this.body = this.gutter = null;
|
|
1482
|
+
}
|
|
1483
|
+
this.el.append(this.scroller);
|
|
1484
|
+
this.build();
|
|
1485
|
+
if (showGrid && opts.height === "fill") {
|
|
1486
|
+
const RO = ctx.win.ResizeObserver;
|
|
1487
|
+
if (RO) {
|
|
1488
|
+
this.resizeObs = new RO(() => this.fitRows());
|
|
1489
|
+
this.resizeObs.observe(this.scroller);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
ctx;
|
|
1494
|
+
el;
|
|
1495
|
+
scroller;
|
|
1496
|
+
top;
|
|
1497
|
+
headRow;
|
|
1498
|
+
allDayRow;
|
|
1499
|
+
allDayLayer;
|
|
1500
|
+
allDaySel;
|
|
1501
|
+
allDayMore;
|
|
1502
|
+
allDayPool;
|
|
1503
|
+
body;
|
|
1504
|
+
gutter;
|
|
1505
|
+
nowGutter = null;
|
|
1506
|
+
cols = [];
|
|
1507
|
+
weights = [];
|
|
1508
|
+
ppm = 1;
|
|
1509
|
+
resizeObs = null;
|
|
1510
|
+
get minutes() {
|
|
1511
|
+
return this.ctx.slot.max - this.ctx.slot.min;
|
|
1512
|
+
}
|
|
1513
|
+
/* ------------------------------------------------------------ structure */
|
|
1514
|
+
build() {
|
|
1515
|
+
const { ctx } = this;
|
|
1516
|
+
const { doc, opts, days, today } = ctx;
|
|
1517
|
+
this.weights = days.map((d) => d === today && opts.todayWidth > 1 && days.length > 1 ? opts.todayWidth : 1);
|
|
1518
|
+
const tpl = `var(--tnc-gutter-w) ${this.weights.map((w) => `minmax(0, ${w}fr)`).join(" ")}`;
|
|
1519
|
+
this.el.style.setProperty("--tnc-cols", tpl);
|
|
1520
|
+
const gutterHead = h(doc, "div", "tnc-gutter-head", { role: "columnheader" });
|
|
1521
|
+
if (opts.weekNumbers && days[0] !== void 0) gutterHead.textContent = ctx.text.weekNumber(isoWeekNumber(days[0]));
|
|
1522
|
+
this.headRow.replaceChildren(gutterHead);
|
|
1523
|
+
const capacity = opts.capacityMinutes;
|
|
1524
|
+
this.cols = days.map((day) => {
|
|
1525
|
+
const arg = ctx.dayArg(day);
|
|
1526
|
+
const head = h(doc, "div", ["tnc-dh", ...ctx.dayClasses(day)].join(" "), {
|
|
1527
|
+
role: "columnheader",
|
|
1528
|
+
"data-date": arg.dateStr,
|
|
1529
|
+
"aria-label": ctx.fmt.longDate(day)
|
|
1530
|
+
});
|
|
1531
|
+
const custom = opts.dayHeaderContent?.(arg);
|
|
1532
|
+
if (custom != null) head.append(custom);
|
|
1533
|
+
else {
|
|
1534
|
+
const label = h(
|
|
1535
|
+
doc,
|
|
1536
|
+
"span",
|
|
1537
|
+
"tnc-dh-label",
|
|
1538
|
+
null,
|
|
1539
|
+
h(doc, "span", "tnc-dh-dow", null, ctx.fmt.weekdayShort(day)),
|
|
1540
|
+
h(doc, "span", "tnc-dh-num", null, ctx.fmt.dayNumber(day))
|
|
1541
|
+
);
|
|
1542
|
+
head.append(opts.navLinks ? ctx.navLink(day, label, "tnc-dh-link") : label);
|
|
1543
|
+
}
|
|
1544
|
+
let cap = null;
|
|
1545
|
+
if (capacity !== null) {
|
|
1546
|
+
cap = h(doc, "div", "tnc-cap", null, h(doc, "i"), h(doc, "span", "tnc-cap-txt"));
|
|
1547
|
+
head.append(cap);
|
|
1548
|
+
}
|
|
1549
|
+
this.headRow.append(head);
|
|
1550
|
+
opts.dayHeaderDidMount?.({ ...arg, el: head });
|
|
1551
|
+
const col = h(doc, "div", ["tnc-col", ...ctx.dayClasses(day)].join(" "), { role: "cell", "aria-label": ctx.fmt.longDate(day), "data-date": arg.dateStr });
|
|
1552
|
+
const bgLayer = h(doc, "div", "tnc-col-bg");
|
|
1553
|
+
const planLayer = h(doc, "div", "tnc-col-plan");
|
|
1554
|
+
const actualLane = h(doc, "div", "tnc-col-actual");
|
|
1555
|
+
const more = h(doc, "div", "tnc-more-layer");
|
|
1556
|
+
const sel = h(doc, "div", "tnc-sel-layer");
|
|
1557
|
+
col.append(bgLayer, sel, planLayer, actualLane, more);
|
|
1558
|
+
let now = null;
|
|
1559
|
+
if (opts.nowIndicator && day === today) {
|
|
1560
|
+
now = h(doc, "div", "tnc-now", { "aria-hidden": "true" });
|
|
1561
|
+
col.append(now);
|
|
1562
|
+
}
|
|
1563
|
+
this.body?.append(col);
|
|
1564
|
+
return {
|
|
1565
|
+
day,
|
|
1566
|
+
head,
|
|
1567
|
+
col,
|
|
1568
|
+
plan: new ItemPool(ctx, planLayer),
|
|
1569
|
+
actual: new ItemPool(ctx, actualLane),
|
|
1570
|
+
bg: new ItemPool(ctx, bgLayer),
|
|
1571
|
+
actualLane,
|
|
1572
|
+
more,
|
|
1573
|
+
sel,
|
|
1574
|
+
now,
|
|
1575
|
+
cap
|
|
1576
|
+
};
|
|
1577
|
+
});
|
|
1578
|
+
if (this.gutter) this.buildGutter();
|
|
1579
|
+
this.applyScale();
|
|
1580
|
+
}
|
|
1581
|
+
buildGutter() {
|
|
1582
|
+
const { ctx } = this;
|
|
1583
|
+
const gutter = this.gutter;
|
|
1584
|
+
gutter.replaceChildren();
|
|
1585
|
+
const { min, max, duration } = ctx.slot;
|
|
1586
|
+
const interval = duration >= 60 ? duration : 60;
|
|
1587
|
+
const first = Math.ceil(min / interval) * interval;
|
|
1588
|
+
for (let m = first; m < max; m += interval) {
|
|
1589
|
+
if (m === min) continue;
|
|
1590
|
+
const label = h(ctx.doc, "div", "tnc-slot-label", null, ctx.fmt.slotLabel(m));
|
|
1591
|
+
label.style.top = pct((m - min) / this.minutes);
|
|
1592
|
+
gutter.append(label);
|
|
1593
|
+
}
|
|
1594
|
+
if (this.nowGutter) gutter.append(this.nowGutter);
|
|
1595
|
+
}
|
|
1596
|
+
applyScale() {
|
|
1597
|
+
const { opts, slot } = this.ctx;
|
|
1598
|
+
this.ppm = opts.slotHeight / slot.duration;
|
|
1599
|
+
this.fitRows(false);
|
|
1600
|
+
this.el.style.setProperty("--tnc-slot-px", `${slot.duration * this.ppm}px`);
|
|
1601
|
+
this.el.style.setProperty("--tnc-hour-px", `${60 * this.ppm}px`);
|
|
1602
|
+
this.el.style.setProperty("--tnc-grid-offset", `${-(slot.min % 60 * this.ppm)}px`);
|
|
1603
|
+
if (this.body) this.body.style.height = `${this.minutes * this.ppm}px`;
|
|
1604
|
+
}
|
|
1605
|
+
/** In fill mode with expandRows, stretches slots so the grid fills the height. */
|
|
1606
|
+
fitRows(rerender = true) {
|
|
1607
|
+
const { opts, slot } = this.ctx;
|
|
1608
|
+
if (!this.body || opts.height !== "fill" || !opts.expandRows) return;
|
|
1609
|
+
const avail = this.scroller.clientHeight - this.top.offsetHeight;
|
|
1610
|
+
const base = opts.slotHeight / slot.duration;
|
|
1611
|
+
const next = avail > 0 ? Math.max(base, avail / this.minutes) : base;
|
|
1612
|
+
if (Math.abs(next - this.ppm) < 1e-3) return;
|
|
1613
|
+
this.ppm = next;
|
|
1614
|
+
this.body.style.height = `${this.minutes * this.ppm}px`;
|
|
1615
|
+
this.el.style.setProperty("--tnc-slot-px", `${slot.duration * this.ppm}px`);
|
|
1616
|
+
this.el.style.setProperty("--tnc-hour-px", `${60 * this.ppm}px`);
|
|
1617
|
+
if (rerender) this.render(null);
|
|
1618
|
+
}
|
|
1619
|
+
/* --------------------------------------------------------------- render */
|
|
1620
|
+
render(change) {
|
|
1621
|
+
this.renderAllDay();
|
|
1622
|
+
if (!this.body) return;
|
|
1623
|
+
const dirty = change ? change.days : null;
|
|
1624
|
+
for (const c of this.cols) {
|
|
1625
|
+
if (dirty && !dirty.has(c.day) && !(this.ctx.slot.max > MINUTES_PER_DAY && dirty.has(c.day + 1))) continue;
|
|
1626
|
+
this.renderColumn(c);
|
|
1627
|
+
}
|
|
1628
|
+
this.renderCapacity();
|
|
1629
|
+
this.tick();
|
|
1630
|
+
}
|
|
1631
|
+
renderCapacity() {
|
|
1632
|
+
const cap = this.ctx.opts.capacityMinutes;
|
|
1633
|
+
if (cap === null) return;
|
|
1634
|
+
const planned = plannedMinutes(this.ctx.store, this.cols.map((c) => c.day));
|
|
1635
|
+
for (const c of this.cols) {
|
|
1636
|
+
if (!c.cap) continue;
|
|
1637
|
+
const limit = typeof cap === "function" ? cap(c.day) : cap;
|
|
1638
|
+
const used = planned.get(c.day) ?? 0;
|
|
1639
|
+
const ratio = limit > 0 ? used / limit : 0;
|
|
1640
|
+
const bar = c.cap.firstElementChild;
|
|
1641
|
+
bar.style.width = pct(Math.min(1, ratio));
|
|
1642
|
+
c.cap.classList.toggle("is-warn", ratio > 0.85 && ratio <= 1);
|
|
1643
|
+
c.cap.classList.toggle("is-over", ratio > 1);
|
|
1644
|
+
const hours = (m) => `${Math.round(m / 60 * 10) / 10}h`;
|
|
1645
|
+
c.cap.lastElementChild.textContent = limit > 0 ? `${hours(used)} / ${hours(limit)}` : hours(used);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
renderAllDay() {
|
|
1649
|
+
const { ctx } = this;
|
|
1650
|
+
if (!this.allDayLayer || !this.allDayPool || !this.allDayMore) return;
|
|
1651
|
+
const { days, store, opts } = ctx;
|
|
1652
|
+
const first = days[0];
|
|
1653
|
+
const last = days[days.length - 1];
|
|
1654
|
+
if (first === void 0 || last === void 0) return;
|
|
1655
|
+
const items = opts.timeGrid ? store.spanningIn(first, last + 1, false) : store.inRange(first, last + 1).filter((n) => n.item.display !== "background");
|
|
1656
|
+
const row = layoutRow(items.filter((n) => n.item.display !== "background"), days, ctx.order);
|
|
1657
|
+
const limit = this.allDayLimit();
|
|
1658
|
+
const visibleLevels = (col) => limit && (row.countByCol[col] ?? 0) > limit ? limit - 1 : limit || Infinity;
|
|
1659
|
+
const offsets = this.colOffsets();
|
|
1660
|
+
const pool = this.allDayPool;
|
|
1661
|
+
pool.begin();
|
|
1662
|
+
const hidden = new Array(days.length).fill(0);
|
|
1663
|
+
let maxLevel = -1;
|
|
1664
|
+
for (const p of row.placements) {
|
|
1665
|
+
let level = p.level;
|
|
1666
|
+
let room = Infinity;
|
|
1667
|
+
for (let c = p.startCol; c < p.endCol; c++) room = Math.min(room, visibleLevels(c));
|
|
1668
|
+
if (level >= room) {
|
|
1669
|
+
if (p.item.id !== ctx.draggingId) {
|
|
1670
|
+
for (let c = p.startCol; c < p.endCol; c++) hidden[c]++;
|
|
1671
|
+
continue;
|
|
1672
|
+
}
|
|
1673
|
+
level = Math.max(0, room - 1);
|
|
1674
|
+
}
|
|
1675
|
+
maxLevel = Math.max(maxLevel, level);
|
|
1676
|
+
const el = pool.place(p.item.id, {
|
|
1677
|
+
n: p.item,
|
|
1678
|
+
day: days[p.startCol],
|
|
1679
|
+
isStart: p.isStart,
|
|
1680
|
+
isEnd: p.isEnd,
|
|
1681
|
+
variant: "bar",
|
|
1682
|
+
mirror: false
|
|
1683
|
+
});
|
|
1684
|
+
setStyle(el, {
|
|
1685
|
+
left: `calc(${pct(offsets[p.startCol])} + 2px)`,
|
|
1686
|
+
width: `calc(${pct(offsets[p.endCol] - offsets[p.startCol])} - 4px)`,
|
|
1687
|
+
top: `calc(${level} * (var(--tnc-bar-h) + ${BAR_GAP2}px) + 3px)`
|
|
1688
|
+
});
|
|
1689
|
+
}
|
|
1690
|
+
pool.end();
|
|
1691
|
+
this.allDayMore.replaceChildren();
|
|
1692
|
+
let anyMore = false;
|
|
1693
|
+
hidden.forEach((count, col) => {
|
|
1694
|
+
if (!count) return;
|
|
1695
|
+
anyMore = true;
|
|
1696
|
+
const day = days[col];
|
|
1697
|
+
const link = h(ctx.doc, "button", "tnc-more", { type: "button", "aria-haspopup": "dialog" }, ctx.text.more(count));
|
|
1698
|
+
setStyle(link, {
|
|
1699
|
+
left: `calc(${pct(offsets[col])} + 2px)`,
|
|
1700
|
+
width: `calc(${pct(offsets[col + 1] - offsets[col])} - 4px)`,
|
|
1701
|
+
top: `calc(${visibleLevels(col)} * (var(--tnc-bar-h) + ${BAR_GAP2}px) + 3px)`
|
|
1702
|
+
});
|
|
1703
|
+
link.addEventListener("click", (ev) => ctx.openMore(day, link, ev));
|
|
1704
|
+
this.allDayMore.append(link);
|
|
1705
|
+
});
|
|
1706
|
+
const bars = `calc(${Math.max(1, maxLevel + 1)} * (var(--tnc-bar-h) + ${BAR_GAP2}px) + 6px)`;
|
|
1707
|
+
if (!anyMore) {
|
|
1708
|
+
this.allDayLayer.style.height = bars;
|
|
1709
|
+
return;
|
|
1710
|
+
}
|
|
1711
|
+
const moreH = this.allDayMore.querySelector(".tnc-more")?.offsetHeight ?? 0;
|
|
1712
|
+
const moreTop = Math.max(...hidden.map((count, col) => count ? visibleLevels(col) : 0));
|
|
1713
|
+
this.allDayLayer.style.height = `max(${bars}, calc(${moreTop} * (var(--tnc-bar-h) + ${BAR_GAP2}px) + ${moreH}px + 6px))`;
|
|
1714
|
+
}
|
|
1715
|
+
allDayLimit() {
|
|
1716
|
+
const { dayMaxEvents, dayMaxEventRows } = this.ctx.opts;
|
|
1717
|
+
if (dayMaxEventRows > 0) return dayMaxEventRows;
|
|
1718
|
+
if (typeof dayMaxEvents === "number" && dayMaxEvents > 0) return dayMaxEvents + 1;
|
|
1719
|
+
if (dayMaxEvents === true && this.ctx.opts.timeGrid) return 4;
|
|
1720
|
+
return 0;
|
|
1721
|
+
}
|
|
1722
|
+
/** Fractional left edge of each column (length n + 1). */
|
|
1723
|
+
colOffsets() {
|
|
1724
|
+
const total = this.weights.reduce((a, b) => a + b, 0) || 1;
|
|
1725
|
+
const out = [0];
|
|
1726
|
+
let acc = 0;
|
|
1727
|
+
for (const w of this.weights) out.push((acc += w) / total);
|
|
1728
|
+
return out;
|
|
1729
|
+
}
|
|
1730
|
+
renderColumn(c) {
|
|
1731
|
+
const { ctx } = this;
|
|
1732
|
+
const { store, opts, slot, zone } = ctx;
|
|
1733
|
+
const items = store.timedOn(c.day);
|
|
1734
|
+
if (slot.max > MINUTES_PER_DAY) {
|
|
1735
|
+
const seen = new Set(items.map((n) => n.id));
|
|
1736
|
+
for (const n of store.timedOn(c.day + 1)) if (!seen.has(n.id)) items.push(n);
|
|
1737
|
+
}
|
|
1738
|
+
const minMinutes = opts.eventMinHeight / this.ppm;
|
|
1739
|
+
const layout = layoutTimedDay(items, c.day, zone, minMinutes, ctx.order, [slot.min, slot.max]);
|
|
1740
|
+
const total = this.minutes;
|
|
1741
|
+
const top = (m) => pct((m - slot.min) / total);
|
|
1742
|
+
const height = (s) => pct((s.endMin - s.startMin) / total);
|
|
1743
|
+
c.bg.begin();
|
|
1744
|
+
for (const s of layout.background) {
|
|
1745
|
+
const el = c.bg.place(s.item.id, this.meta(s, true));
|
|
1746
|
+
setStyle(el, { top: top(s.startMin), height: height(s) });
|
|
1747
|
+
}
|
|
1748
|
+
c.bg.end();
|
|
1749
|
+
const hasActual = layout.actual.length > 0;
|
|
1750
|
+
c.col.classList.toggle("has-actual", hasActual);
|
|
1751
|
+
this.placeLane(c.plan, layout.plan, c, top, height, true);
|
|
1752
|
+
this.placeLane(c.actual, layout.actual, c, top, height, false);
|
|
1753
|
+
}
|
|
1754
|
+
placeLane(pool, placements, c, top, height, withMore) {
|
|
1755
|
+
const { ctx } = this;
|
|
1756
|
+
const { opts } = ctx;
|
|
1757
|
+
const maxStack = opts.eventMaxStack;
|
|
1758
|
+
const hiddenByCluster = /* @__PURE__ */ new Map();
|
|
1759
|
+
pool.begin();
|
|
1760
|
+
for (const p of placements) {
|
|
1761
|
+
if (withMore && maxStack > 0 && p.cols > maxStack && p.col >= maxStack - 1) {
|
|
1762
|
+
const rec = hiddenByCluster.get(p.cluster) ?? { count: 0, start: p.startMin, end: p.endMin, items: [] };
|
|
1763
|
+
rec.count++;
|
|
1764
|
+
rec.start = Math.min(rec.start, p.startMin);
|
|
1765
|
+
rec.end = Math.max(rec.end, p.endMin);
|
|
1766
|
+
rec.items.push(p);
|
|
1767
|
+
hiddenByCluster.set(p.cluster, rec);
|
|
1768
|
+
continue;
|
|
1769
|
+
}
|
|
1770
|
+
const cols = withMore && maxStack > 0 ? Math.min(p.cols, maxStack) : p.cols;
|
|
1771
|
+
const span = Math.min(p.span, cols - p.col);
|
|
1772
|
+
const left = p.col / cols;
|
|
1773
|
+
let width = span / cols;
|
|
1774
|
+
if (opts.slotEventOverlap && cols > 1 && p.col + span < cols) width = Math.min(1 - left, width * 1.7);
|
|
1775
|
+
const el = pool.place(p.item.id, this.meta(p, false));
|
|
1776
|
+
const short = (p.endMin - p.startMin) * this.ppm < 34;
|
|
1777
|
+
el.classList.toggle("is-short", short);
|
|
1778
|
+
setStyle(el, {
|
|
1779
|
+
top: top(p.startMin),
|
|
1780
|
+
height: `max(${height(p)}, ${opts.eventMinHeight}px)`,
|
|
1781
|
+
left: pct(left),
|
|
1782
|
+
width: `calc(${pct(width)} - 1px)`,
|
|
1783
|
+
"z-index": String(p.col + 1)
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
pool.end();
|
|
1787
|
+
if (!withMore) return;
|
|
1788
|
+
c.more.replaceChildren();
|
|
1789
|
+
for (const rec of hiddenByCluster.values()) {
|
|
1790
|
+
const link = h(ctx.doc, "button", "tnc-more tnc-more--stack", { type: "button", "aria-haspopup": "dialog" }, `+${rec.count}`);
|
|
1791
|
+
const cols = maxStack;
|
|
1792
|
+
setStyle(link, {
|
|
1793
|
+
top: top(rec.start),
|
|
1794
|
+
left: pct((cols - 1) / cols),
|
|
1795
|
+
width: `calc(${pct(1 / cols)} - 1px)`
|
|
1796
|
+
});
|
|
1797
|
+
link.addEventListener("click", (ev) => ctx.openMore(c.day, link, ev));
|
|
1798
|
+
c.more.append(link);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
meta(s, bg) {
|
|
1802
|
+
return {
|
|
1803
|
+
n: s.item,
|
|
1804
|
+
day: s.day,
|
|
1805
|
+
isStart: s.isStart,
|
|
1806
|
+
isEnd: s.isEnd,
|
|
1807
|
+
variant: bg ? "bar" : "timed",
|
|
1808
|
+
mirror: false
|
|
1809
|
+
};
|
|
1810
|
+
}
|
|
1811
|
+
/* ------------------------------------------------------------ selection */
|
|
1812
|
+
showSelection(sel) {
|
|
1813
|
+
const { ctx } = this;
|
|
1814
|
+
const offsets = this.colOffsets();
|
|
1815
|
+
if (this.allDaySel) {
|
|
1816
|
+
this.allDaySel.replaceChildren();
|
|
1817
|
+
if (sel?.allDay) {
|
|
1818
|
+
const startCol = this.cols.findIndex((c) => c.day >= sel.startDay);
|
|
1819
|
+
let endCol = this.cols.findIndex((c) => c.day >= sel.endDay);
|
|
1820
|
+
if (endCol < 0) endCol = this.cols.length;
|
|
1821
|
+
if (startCol >= 0 && endCol > startCol) {
|
|
1822
|
+
const box = h(ctx.doc, "div", "tnc-sel");
|
|
1823
|
+
setStyle(box, {
|
|
1824
|
+
left: pct(offsets[startCol]),
|
|
1825
|
+
width: pct(offsets[endCol] - offsets[startCol]),
|
|
1826
|
+
top: "0",
|
|
1827
|
+
bottom: "0"
|
|
1828
|
+
});
|
|
1829
|
+
this.allDaySel.append(box);
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
for (const c of this.cols) {
|
|
1834
|
+
c.sel.replaceChildren();
|
|
1835
|
+
if (!sel || sel.allDay) continue;
|
|
1836
|
+
const winStart = toInstant(c.day, ctx.slot.min, ctx.zone);
|
|
1837
|
+
const winEnd = toInstant(c.day, ctx.slot.max, ctx.zone);
|
|
1838
|
+
const s = Math.max(sel.startMs, winStart);
|
|
1839
|
+
const e = Math.min(sel.endMs, winEnd);
|
|
1840
|
+
if (e <= s) continue;
|
|
1841
|
+
const rel = (ms) => {
|
|
1842
|
+
const w = wallTime(ms, ctx.zone);
|
|
1843
|
+
return (w.day - c.day) * MINUTES_PER_DAY + w.minute;
|
|
1844
|
+
};
|
|
1845
|
+
const sm = s === winStart ? ctx.slot.min : rel(s);
|
|
1846
|
+
const em = e === winEnd ? ctx.slot.max : rel(e);
|
|
1847
|
+
const box = h(ctx.doc, "div", "tnc-sel");
|
|
1848
|
+
if (ctx.opts.selectMirror) {
|
|
1849
|
+
box.classList.add("tnc-sel--mirror");
|
|
1850
|
+
box.textContent = `${ctx.fmt.time(sm)} \u2013 ${ctx.fmt.time(em)}`;
|
|
1851
|
+
}
|
|
1852
|
+
setStyle(box, { top: pct((sm - ctx.slot.min) / this.minutes), height: pct((em - sm) / this.minutes) });
|
|
1853
|
+
c.sel.append(box);
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
1856
|
+
/* ------------------------------------------------------------- hit test */
|
|
1857
|
+
hit(x, y) {
|
|
1858
|
+
const colIndex = (cells) => {
|
|
1859
|
+
for (let i2 = 0; i2 < cells.length; i2++) {
|
|
1860
|
+
const r2 = cells[i2].getBoundingClientRect();
|
|
1861
|
+
if (x >= r2.left && x < r2.right) return i2;
|
|
1862
|
+
}
|
|
1863
|
+
const firstR = cells[0]?.getBoundingClientRect();
|
|
1864
|
+
const lastR = cells[cells.length - 1]?.getBoundingClientRect();
|
|
1865
|
+
if (firstR && x < firstR.left) return 0;
|
|
1866
|
+
if (lastR && x >= lastR.right) return cells.length - 1;
|
|
1867
|
+
return -1;
|
|
1868
|
+
};
|
|
1869
|
+
if (this.allDayRow) {
|
|
1870
|
+
const r2 = this.allDayRow.getBoundingClientRect();
|
|
1871
|
+
if (y >= r2.top && y < r2.bottom || !this.body && y >= r2.top) {
|
|
1872
|
+
const i2 = colIndex(this.cols.map((c2) => c2.head));
|
|
1873
|
+
return i2 >= 0 ? { kind: "allDay", day: this.cols[i2].day } : null;
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
if (!this.body) return null;
|
|
1877
|
+
const i = colIndex(this.cols.map((c2) => c2.col));
|
|
1878
|
+
if (i < 0) return null;
|
|
1879
|
+
const c = this.cols[i];
|
|
1880
|
+
const r = c.col.getBoundingClientRect();
|
|
1881
|
+
const scrollerR = this.scroller.getBoundingClientRect();
|
|
1882
|
+
const topR = this.top.getBoundingClientRect();
|
|
1883
|
+
if (y < Math.max(topR.bottom, scrollerR.top)) {
|
|
1884
|
+
if (y < topR.top) return null;
|
|
1885
|
+
return this.allDayRow ? { kind: "allDay", day: c.day } : null;
|
|
1886
|
+
}
|
|
1887
|
+
const ratio = Math.min(1, Math.max(0, (y - r.top) / r.height));
|
|
1888
|
+
return { kind: "time", day: c.day, minute: this.ctx.slot.min + ratio * this.minutes };
|
|
1889
|
+
}
|
|
1890
|
+
/* ----------------------------------------------------------------- misc */
|
|
1891
|
+
tick() {
|
|
1892
|
+
const { ctx } = this;
|
|
1893
|
+
const nowMs = ctx.now();
|
|
1894
|
+
const w = wallTime(nowMs, ctx.zone);
|
|
1895
|
+
for (const c of this.cols) {
|
|
1896
|
+
if (!c.now) continue;
|
|
1897
|
+
const minute = (w.day - c.day) * MINUTES_PER_DAY + w.minute;
|
|
1898
|
+
const visible = minute >= ctx.slot.min && minute <= ctx.slot.max;
|
|
1899
|
+
c.now.hidden = !visible;
|
|
1900
|
+
if (visible) c.now.style.top = pct((minute - ctx.slot.min) / this.minutes);
|
|
1901
|
+
if (this.nowGutter) {
|
|
1902
|
+
this.nowGutter.hidden = !visible;
|
|
1903
|
+
if (visible) this.nowGutter.style.top = pct((minute - ctx.slot.min) / this.minutes);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
resize() {
|
|
1908
|
+
this.fitRows();
|
|
1909
|
+
}
|
|
1910
|
+
scrollToTime(minute) {
|
|
1911
|
+
if (!this.body) return;
|
|
1912
|
+
const y = (minute - this.ctx.slot.min) * this.ppm;
|
|
1913
|
+
this.scroller.scrollTop = Math.max(0, y);
|
|
1914
|
+
}
|
|
1915
|
+
getScroll() {
|
|
1916
|
+
return { top: this.scroller.scrollTop, left: this.scroller.scrollLeft };
|
|
1917
|
+
}
|
|
1918
|
+
setScroll(s) {
|
|
1919
|
+
this.scroller.scrollTop = s.top;
|
|
1920
|
+
this.scroller.scrollLeft = s.left;
|
|
1921
|
+
}
|
|
1922
|
+
elementsFor(id) {
|
|
1923
|
+
const out = this.allDayPool?.elementsFor(id) ?? [];
|
|
1924
|
+
for (const c of this.cols) out.push(...c.plan.elementsFor(id), ...c.actual.elementsFor(id), ...c.bg.elementsFor(id));
|
|
1925
|
+
return out;
|
|
1926
|
+
}
|
|
1927
|
+
destroy() {
|
|
1928
|
+
this.resizeObs?.disconnect();
|
|
1929
|
+
this.allDayPool?.clear();
|
|
1930
|
+
for (const c of this.cols) {
|
|
1931
|
+
c.plan.clear();
|
|
1932
|
+
c.actual.clear();
|
|
1933
|
+
c.bg.clear();
|
|
1934
|
+
}
|
|
1935
|
+
this.el.remove();
|
|
1936
|
+
}
|
|
1937
|
+
};
|
|
1938
|
+
|
|
1939
|
+
// src/dom/views/timeline.ts
|
|
1940
|
+
var SVG = "http://www.w3.org/2000/svg";
|
|
1941
|
+
var COMPACT_PX = 28;
|
|
1942
|
+
var NARROW_PX = 20;
|
|
1943
|
+
var MILESTONE_R = 7;
|
|
1944
|
+
function sameRow(a, b) {
|
|
1945
|
+
return a === b || a.id === b.id && a.title === b.title && a.depth === b.depth && a.group === b.group && a.color === b.color && a.collapsed === b.collapsed;
|
|
1946
|
+
}
|
|
1947
|
+
var TimelineView = class {
|
|
1948
|
+
constructor(ctx) {
|
|
1949
|
+
this.ctx = ctx;
|
|
1950
|
+
const { doc } = ctx;
|
|
1951
|
+
const given = this.tl.scale ?? {};
|
|
1952
|
+
const unit = given.unit ?? "day";
|
|
1953
|
+
this.dayWidth = given.dayWidth ?? (given.unitWidth ?? 36) / TIMELINE_UNIT_DAYS[unit];
|
|
1954
|
+
this.scale = { snapMinutes: MINUTES_PER_DAY, ...given, unit, unitWidth: this.dayWidth * TIMELINE_UNIT_DAYS[unit] };
|
|
1955
|
+
this.startDay = ctx.view.range.startDay;
|
|
1956
|
+
this.endDay = ctx.view.range.endDay;
|
|
1957
|
+
this.labelWidth = this.tl.labelWidth ?? 240;
|
|
1958
|
+
this.rowHeight = this.tl.rowHeight ?? 32;
|
|
1959
|
+
this.el = h(doc, "div", "tnc-tl");
|
|
1960
|
+
this.el.style.setProperty("--tnc-tl-label-w", `${this.labelWidth}px`);
|
|
1961
|
+
this.el.style.setProperty("--tnc-tl-row-h", `${this.rowHeight}px`);
|
|
1962
|
+
this.el.style.setProperty("--tnc-tl-day-w", `${this.dayWidth}px`);
|
|
1963
|
+
this.scroller = h(doc, "div", "tnc-tl-scroll");
|
|
1964
|
+
this.canvas = h(doc, "div", "tnc-tl-canvas", { role: "table", "aria-label": ctx.view.title });
|
|
1965
|
+
this.canvas.style.width = `${this.labelWidth + this.trackWidth()}px`;
|
|
1966
|
+
this.head = h(doc, "div", "tnc-tl-head");
|
|
1967
|
+
this.ticks = h(doc, "div", "tnc-tl-ticks");
|
|
1968
|
+
this.rowsEl = h(doc, "div", "tnc-tl-rows", { role: "rowgroup" });
|
|
1969
|
+
this.svg = doc.createElementNS(SVG, "svg");
|
|
1970
|
+
this.svg.classList.add("tnc-tl-deps");
|
|
1971
|
+
this.svg.setAttribute("aria-hidden", "true");
|
|
1972
|
+
this.todayLine = h(doc, "div", "tnc-tl-today", { "aria-hidden": "true" });
|
|
1973
|
+
this.todayTag = this.tl.todayLabel ? h(doc, "div", "tnc-tl-today-label", { "aria-hidden": "true" }, this.tl.todayLabel) : null;
|
|
1974
|
+
this.selLayer = h(doc, "div", "tnc-tl-sel");
|
|
1975
|
+
this.canvas.append(this.head, this.rowsEl);
|
|
1976
|
+
this.scroller.append(this.canvas);
|
|
1977
|
+
this.el.append(this.scroller);
|
|
1978
|
+
this.buildHead();
|
|
1979
|
+
this.el.addEventListener("pointerdown", this.onLinkDown, true);
|
|
1980
|
+
}
|
|
1981
|
+
ctx;
|
|
1982
|
+
el;
|
|
1983
|
+
scroller;
|
|
1984
|
+
canvas;
|
|
1985
|
+
head;
|
|
1986
|
+
ticks;
|
|
1987
|
+
rowsEl;
|
|
1988
|
+
svg;
|
|
1989
|
+
todayLine;
|
|
1990
|
+
todayTag;
|
|
1991
|
+
selLayer;
|
|
1992
|
+
rows = [];
|
|
1993
|
+
scale;
|
|
1994
|
+
dayWidth;
|
|
1995
|
+
startDay;
|
|
1996
|
+
endDay;
|
|
1997
|
+
labelWidth;
|
|
1998
|
+
rowHeight;
|
|
1999
|
+
link = null;
|
|
2000
|
+
get tl() {
|
|
2001
|
+
return this.ctx.opts.timeline ?? { rows: [], rowOf: () => null };
|
|
2002
|
+
}
|
|
2003
|
+
/* ------------------------------------------------------------- geometry */
|
|
2004
|
+
trackWidth() {
|
|
2005
|
+
return (this.endDay - this.startDay) * this.dayWidth;
|
|
2006
|
+
}
|
|
2007
|
+
/** X offset (within the track) of an instant. */
|
|
2008
|
+
x(ms) {
|
|
2009
|
+
const w = wallTime(ms, this.ctx.zone);
|
|
2010
|
+
return (w.day - this.startDay + w.minute / MINUTES_PER_DAY) * this.dayWidth;
|
|
2011
|
+
}
|
|
2012
|
+
xDay(day) {
|
|
2013
|
+
return (day - this.startDay) * this.dayWidth;
|
|
2014
|
+
}
|
|
2015
|
+
/** Instant at a track x offset. */
|
|
2016
|
+
msAt(x) {
|
|
2017
|
+
const civil = this.startDay * MINUTES_PER_DAY + x / this.dayWidth * MINUTES_PER_DAY;
|
|
2018
|
+
const day = Math.floor(civil / MINUTES_PER_DAY);
|
|
2019
|
+
return toInstant(day, civil - day * MINUTES_PER_DAY, this.ctx.zone);
|
|
2020
|
+
}
|
|
2021
|
+
/** Viewport x (from the scroller's left edge) where the track starts showing, and its visible width. */
|
|
2022
|
+
trackViewport() {
|
|
2023
|
+
return { left: this.labelWidth, width: Math.max(0, this.scroller.clientWidth - this.labelWidth) };
|
|
2024
|
+
}
|
|
2025
|
+
/** Instant under a viewport x, measured from the scroller's left edge. */
|
|
2026
|
+
instantAt(viewportX) {
|
|
2027
|
+
return this.msAt(this.scroller.scrollLeft + viewportX - this.labelWidth);
|
|
2028
|
+
}
|
|
2029
|
+
/** Viewport x of an instant at the current scroll. */
|
|
2030
|
+
viewportXOf(ms) {
|
|
2031
|
+
return this.labelWidth + this.x(ms) - this.scroller.scrollLeft;
|
|
2032
|
+
}
|
|
2033
|
+
/** Scrolls so `ms` sits at `viewportX`. */
|
|
2034
|
+
placeInstant(ms, viewportX, smooth = false) {
|
|
2035
|
+
this.scrollLeftTo(this.labelWidth + this.x(ms) - viewportX, smooth);
|
|
2036
|
+
}
|
|
2037
|
+
scrollToInstant(ms, align, smooth) {
|
|
2038
|
+
const vp = this.trackViewport();
|
|
2039
|
+
const vx = vp.left + (align === "start" ? 0 : align === "end" ? vp.width : vp.width / 2);
|
|
2040
|
+
this.placeInstant(ms, vx, smooth);
|
|
2041
|
+
}
|
|
2042
|
+
/** Viewport x of the middle of the visible track. */
|
|
2043
|
+
viewportCentre() {
|
|
2044
|
+
const vp = this.trackViewport();
|
|
2045
|
+
return vp.left + vp.width / 2;
|
|
2046
|
+
}
|
|
2047
|
+
/** The scroller's left edge in client coordinates. */
|
|
2048
|
+
viewportLeft() {
|
|
2049
|
+
return this.scroller.getBoundingClientRect().left;
|
|
2050
|
+
}
|
|
2051
|
+
scrollLeftTo(left, smooth) {
|
|
2052
|
+
const max = Math.max(0, this.scroller.scrollWidth - this.scroller.clientWidth);
|
|
2053
|
+
const target = Math.max(0, Math.min(max || left, left));
|
|
2054
|
+
if (typeof this.scroller.scrollTo === "function") this.scroller.scrollTo({ left: target, behavior: smooth ? "smooth" : "auto" });
|
|
2055
|
+
else this.scroller.scrollLeft = target;
|
|
2056
|
+
}
|
|
2057
|
+
buildHead() {
|
|
2058
|
+
const { doc, fmt, today } = this.ctx;
|
|
2059
|
+
const top = h(doc, "div", "tnc-tl-tier tnc-tl-tier--major");
|
|
2060
|
+
const bottom = h(doc, "div", "tnc-tl-tier tnc-tl-tier--minor");
|
|
2061
|
+
const corner = h(doc, "div", "tnc-tl-corner");
|
|
2062
|
+
const ticks = this.ticks;
|
|
2063
|
+
ticks.append(top, bottom);
|
|
2064
|
+
if (this.todayTag) ticks.append(this.todayTag);
|
|
2065
|
+
this.head.append(corner, ticks);
|
|
2066
|
+
const unit = this.scale.unit;
|
|
2067
|
+
const add = (tier, x0, x1, label, cls = "") => {
|
|
2068
|
+
const t = h(doc, "div", `tnc-tl-tick ${cls}`, null, h(doc, "span", null, null, label));
|
|
2069
|
+
setStyle(t, { left: x0, width: Math.max(0, x1 - x0) });
|
|
2070
|
+
tier.append(t);
|
|
2071
|
+
};
|
|
2072
|
+
const s = this.startDay;
|
|
2073
|
+
const e = this.endDay;
|
|
2074
|
+
if (unit === "hour") {
|
|
2075
|
+
for (let d = s; d < e; d++) add(top, this.xDay(d), this.xDay(d + 1), fmt.longDate(d), d === today ? "is-today" : "");
|
|
2076
|
+
} else if (unit === "month" || unit === "quarter") {
|
|
2077
|
+
for (let d = startOfYear(s); d < e; d = startOfYear(addMonths(d, 12))) {
|
|
2078
|
+
add(top, this.xDay(Math.max(d, s)), this.xDay(Math.min(startOfYear(addMonths(d, 12)), e)), fmt.year(d));
|
|
2079
|
+
}
|
|
2080
|
+
} else {
|
|
2081
|
+
for (let d = startOfMonth(s); d < e; d = addMonths(d, 1)) {
|
|
2082
|
+
add(top, this.xDay(Math.max(d, s)), this.xDay(Math.min(addMonths(d, 1), e)), fmt.monthYear(d));
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
if (unit === "hour") {
|
|
2086
|
+
const step = this.scale.unitWidth >= 28 ? 1 : this.scale.unitWidth >= 14 ? 2 : 6;
|
|
2087
|
+
for (let d = s; d < e; d++) {
|
|
2088
|
+
for (let hr = 0; hr < 24; hr += step) {
|
|
2089
|
+
const x0 = this.xDay(d) + hr / 24 * this.dayWidth;
|
|
2090
|
+
add(bottom, x0, x0 + step / 24 * this.dayWidth, fmt.slotLabel(hr * 60));
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
} else if (unit === "day") {
|
|
2094
|
+
for (let d = s; d < e; d++) {
|
|
2095
|
+
const narrow = this.dayWidth < 26;
|
|
2096
|
+
add(bottom, this.xDay(d), this.xDay(d + 1), narrow ? fmt.dayNumber(d) : `${fmt.weekdayNarrow(d)} ${fmt.dayNumber(d)}`, [
|
|
2097
|
+
d === today ? "is-today" : "",
|
|
2098
|
+
...this.ctx.dayClasses(d).filter((c) => c === "is-weekend")
|
|
2099
|
+
].join(" "));
|
|
2100
|
+
}
|
|
2101
|
+
} else if (unit === "week") {
|
|
2102
|
+
for (let d = startOfWeek(s, this.ctx.firstDay); d < e; d += 7) {
|
|
2103
|
+
add(bottom, this.xDay(Math.max(d, s)), this.xDay(Math.min(d + 7, e)), `${fmt.monthShort(d)} ${fmt.dayNumber(d)}`);
|
|
2104
|
+
}
|
|
2105
|
+
} else {
|
|
2106
|
+
const step = unit === "quarter" ? 3 : 1;
|
|
2107
|
+
for (let d = startOfMonth(s); d < e; d = addMonths(d, step)) {
|
|
2108
|
+
const label = unit === "quarter" ? `Q${Math.floor((ymdOfDay(d).month - 1) / 3) + 1}` : fmt.monthShort(d);
|
|
2109
|
+
add(bottom, this.xDay(Math.max(d, s)), this.xDay(Math.min(addMonths(d, step), e)), label);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
const colPx = unit === "hour" ? this.dayWidth / 24 : unit === "day" ? this.dayWidth : this.dayWidth * 7;
|
|
2113
|
+
this.el.style.setProperty("--tnc-tl-col-w", `${colPx}px`);
|
|
2114
|
+
this.el.style.setProperty("--tnc-tl-day-px", `${this.dayWidth}px`);
|
|
2115
|
+
}
|
|
2116
|
+
/* --------------------------------------------------------------- render */
|
|
2117
|
+
mountRow(row) {
|
|
2118
|
+
const { ctx } = this;
|
|
2119
|
+
const { doc } = ctx;
|
|
2120
|
+
const el = h(doc, "div", `tnc-tl-row${row.group ? " is-group" : ""}`, { role: "row", "data-row": row.id });
|
|
2121
|
+
const label = h(doc, "div", "tnc-tl-label", { role: "rowheader" });
|
|
2122
|
+
label.style.paddingLeft = `${10 + (row.depth ?? 0) * 16}px`;
|
|
2123
|
+
if (row.color) label.style.setProperty("--tnc-c", row.color);
|
|
2124
|
+
const tl = this.tl;
|
|
2125
|
+
if (tl.rowLabelDidMount) tl.rowLabelDidMount({ row, el: label });
|
|
2126
|
+
else if (tl.rowContent) tl.rowContent(row, label);
|
|
2127
|
+
else label.append(h(doc, "span", "tnc-tl-label-text", null, row.title));
|
|
2128
|
+
if (tl.onRowClick) label.addEventListener("click", (ev) => this.tl.onRowClick?.(row, ev));
|
|
2129
|
+
const track = h(doc, "div", "tnc-tl-track", { role: "cell" });
|
|
2130
|
+
el.append(label, track);
|
|
2131
|
+
return { row, el, label, track, pool: row.group ? null : new ItemPool(ctx, track), top: 0, height: this.rowHeight };
|
|
2132
|
+
}
|
|
2133
|
+
unmountRow(rec) {
|
|
2134
|
+
rec.pool?.clear();
|
|
2135
|
+
this.tl.rowLabelWillUnmount?.({ row: rec.row, el: rec.label });
|
|
2136
|
+
rec.el.remove();
|
|
2137
|
+
}
|
|
2138
|
+
render(_change) {
|
|
2139
|
+
const { ctx } = this;
|
|
2140
|
+
const { store } = ctx;
|
|
2141
|
+
const byRow = /* @__PURE__ */ new Map();
|
|
2142
|
+
for (const n of store.inRange(this.startDay, this.endDay)) {
|
|
2143
|
+
if (n.item.display === "none") continue;
|
|
2144
|
+
const rowId = ctx.dragRow?.id === n.id ? ctx.dragRow.rowId : this.tl.rowOf(n.item);
|
|
2145
|
+
if (rowId == null) continue;
|
|
2146
|
+
const list = byRow.get(rowId) ?? [];
|
|
2147
|
+
list.push(n);
|
|
2148
|
+
byRow.set(rowId, list);
|
|
2149
|
+
}
|
|
2150
|
+
const old = new Map(this.rows.map((r) => [r.row.id, r]));
|
|
2151
|
+
const next = [];
|
|
2152
|
+
let y = 0;
|
|
2153
|
+
for (const row of this.tl.rows) {
|
|
2154
|
+
let rec = old.get(row.id);
|
|
2155
|
+
if (rec && !sameRow(rec.row, row)) {
|
|
2156
|
+
this.unmountRow(rec);
|
|
2157
|
+
rec = void 0;
|
|
2158
|
+
}
|
|
2159
|
+
rec ??= this.mountRow(row);
|
|
2160
|
+
old.delete(row.id);
|
|
2161
|
+
const items = byRow.get(row.id) ?? [];
|
|
2162
|
+
const lane = layoutLane(items, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY);
|
|
2163
|
+
const levels = Math.max(1, lane.levels);
|
|
2164
|
+
rec.height = this.rowHeight * levels;
|
|
2165
|
+
rec.el.style.height = `${rec.height}px`;
|
|
2166
|
+
rec.top = y;
|
|
2167
|
+
y += rec.height;
|
|
2168
|
+
if (rec.pool) {
|
|
2169
|
+
rec.pool.begin();
|
|
2170
|
+
for (const bar of lane.bars) this.placeBar(rec.pool, bar.item, bar.level);
|
|
2171
|
+
rec.pool.end();
|
|
2172
|
+
}
|
|
2173
|
+
next.push(rec);
|
|
2174
|
+
}
|
|
2175
|
+
for (const rec of old.values()) this.unmountRow(rec);
|
|
2176
|
+
let prev = null;
|
|
2177
|
+
for (const rec of next) {
|
|
2178
|
+
if (prev ? prev.nextSibling !== rec.el : this.rowsEl.firstChild !== rec.el) {
|
|
2179
|
+
if (prev) prev.after(rec.el);
|
|
2180
|
+
else this.rowsEl.prepend(rec.el);
|
|
2181
|
+
}
|
|
2182
|
+
prev = rec.el;
|
|
2183
|
+
}
|
|
2184
|
+
this.rows = next;
|
|
2185
|
+
this.rowsEl.style.height = `${y}px`;
|
|
2186
|
+
this.rowsEl.append(this.svg, this.selLayer, this.todayLine);
|
|
2187
|
+
this.drawDependencies();
|
|
2188
|
+
this.tick();
|
|
2189
|
+
}
|
|
2190
|
+
placeBar(pool, n, level) {
|
|
2191
|
+
const { ctx } = this;
|
|
2192
|
+
const milestone = n.item.kind === "milestone";
|
|
2193
|
+
const el = pool.place(n.id, { n, day: n.startDay, isStart: true, isEnd: true, variant: "timeline", mirror: false });
|
|
2194
|
+
const x0 = n.allDay ? this.xDay(n.startDay) : this.x(n.startMs);
|
|
2195
|
+
const x1 = n.allDay ? this.xDay(n.endDay) : this.x(n.endMs);
|
|
2196
|
+
el.classList.toggle("is-milestone", milestone);
|
|
2197
|
+
el.classList.toggle("is-conflict", ctx.conflicts.has(n.id));
|
|
2198
|
+
if (milestone) {
|
|
2199
|
+
const cx = n.allDay ? this.xDay(n.startDay) + this.dayWidth / 2 : x0;
|
|
2200
|
+
setStyle(el, { left: cx, width: null, top: level * this.rowHeight, "--tnc-grip": null });
|
|
2201
|
+
return;
|
|
2202
|
+
}
|
|
2203
|
+
const width = Math.max(4, x1 - x0);
|
|
2204
|
+
el.classList.toggle("is-compact", width < COMPACT_PX);
|
|
2205
|
+
el.classList.toggle("is-narrow", width < NARROW_PX);
|
|
2206
|
+
const grip = Math.min(8, Math.floor((width - 12) / 2));
|
|
2207
|
+
setStyle(el, { left: x0, width, top: level * this.rowHeight, "--tnc-grip": width < NARROW_PX ? null : `${grip}px` });
|
|
2208
|
+
}
|
|
2209
|
+
rowOfItem(id) {
|
|
2210
|
+
for (const r of this.rows) if (r.pool?.elementsFor(id).length) return r;
|
|
2211
|
+
return void 0;
|
|
2212
|
+
}
|
|
2213
|
+
/** Track x of a bar's start or finish edge. */
|
|
2214
|
+
edgeX(el, edge) {
|
|
2215
|
+
if (el.classList.contains("is-milestone")) return el.offsetLeft + (edge === "start" ? -MILESTONE_R : MILESTONE_R);
|
|
2216
|
+
return edge === "start" ? el.offsetLeft : el.offsetLeft + el.offsetWidth;
|
|
2217
|
+
}
|
|
2218
|
+
drawDependencies() {
|
|
2219
|
+
const { ctx } = this;
|
|
2220
|
+
this.svg.replaceChildren();
|
|
2221
|
+
if (!this.tl.dependencies) return;
|
|
2222
|
+
this.svg.setAttribute("width", String(this.trackWidth()));
|
|
2223
|
+
this.svg.setAttribute("height", this.rowsEl.style.height.replace("px", "") || "0");
|
|
2224
|
+
this.svg.style.left = `${this.labelWidth}px`;
|
|
2225
|
+
for (const n of ctx.store.all()) {
|
|
2226
|
+
if (!n.item.dependsOn?.length) continue;
|
|
2227
|
+
const toRow = this.rowOfItem(n.id);
|
|
2228
|
+
const toEl = toRow?.pool?.elementsFor(n.id)[0];
|
|
2229
|
+
if (!toRow || !toEl) continue;
|
|
2230
|
+
for (const dep of itemDependencies(n.item)) {
|
|
2231
|
+
const fromRow = this.rowOfItem(dep.id);
|
|
2232
|
+
const fromEl = fromRow?.pool?.elementsFor(dep.id)[0];
|
|
2233
|
+
if (!fromRow || !fromEl) continue;
|
|
2234
|
+
const path = this.ctx.doc.createElementNS(SVG, "path");
|
|
2235
|
+
path.setAttribute(
|
|
2236
|
+
"d",
|
|
2237
|
+
dependencyPath(
|
|
2238
|
+
dep.type,
|
|
2239
|
+
this.edgeX(fromEl, dep.type[0] === "S" ? "start" : "finish"),
|
|
2240
|
+
fromRow.top + fromEl.offsetTop + fromEl.offsetHeight / 2,
|
|
2241
|
+
this.edgeX(toEl, dep.type[1] === "S" ? "start" : "finish"),
|
|
2242
|
+
toRow.top + toEl.offsetTop + toEl.offsetHeight / 2
|
|
2243
|
+
)
|
|
2244
|
+
);
|
|
2245
|
+
path.setAttribute("marker-end", "url(#tnc-arrow)");
|
|
2246
|
+
path.dataset.from = dep.id;
|
|
2247
|
+
path.dataset.to = n.id;
|
|
2248
|
+
path.dataset.type = dep.type;
|
|
2249
|
+
if (ctx.conflicts.get(n.id)?.includes(dep.id)) path.classList.add("is-conflict");
|
|
2250
|
+
this.svg.append(path);
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
const defs = this.ctx.doc.createElementNS(SVG, "defs");
|
|
2254
|
+
defs.innerHTML = '<marker id="tnc-arrow" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0 L8,4 L0,8 z" fill="currentColor"/></marker>';
|
|
2255
|
+
this.svg.prepend(defs);
|
|
2256
|
+
}
|
|
2257
|
+
/* ---------------------------------------------------------------- links */
|
|
2258
|
+
linkTargetAt(clientX, clientY, from) {
|
|
2259
|
+
const under = this.ctx.doc.elementFromPoint(clientX, clientY);
|
|
2260
|
+
const el = under?.closest(".tnc-item--timeline");
|
|
2261
|
+
if (!el || !this.el.contains(el)) return null;
|
|
2262
|
+
const meta = this.ctx.meta.get(el);
|
|
2263
|
+
if (!meta || meta.mirror || meta.n.id === from.id) return null;
|
|
2264
|
+
const port = under?.closest(".tnc-port") ?? null;
|
|
2265
|
+
let edge;
|
|
2266
|
+
if (port) edge = port.dataset.edge === "start" ? "start" : "finish";
|
|
2267
|
+
else if (el.classList.contains("is-milestone")) edge = "start";
|
|
2268
|
+
else {
|
|
2269
|
+
const r = el.getBoundingClientRect();
|
|
2270
|
+
edge = clientX < r.left + r.width / 2 ? "start" : "finish";
|
|
2271
|
+
}
|
|
2272
|
+
return { n: meta.n, el, edge, port };
|
|
2273
|
+
}
|
|
2274
|
+
/** Centre of a target edge's port, in track coordinates. */
|
|
2275
|
+
targetPoint(t) {
|
|
2276
|
+
const port = t.port ?? t.el.querySelector(`.tnc-port[data-edge="${t.edge}"]`);
|
|
2277
|
+
const rowsR = this.rowsEl.getBoundingClientRect();
|
|
2278
|
+
const r = (port ?? t.el).getBoundingClientRect();
|
|
2279
|
+
return { x: r.left + r.width / 2 - rowsR.left - this.labelWidth, y: r.top + r.height / 2 - rowsR.top };
|
|
2280
|
+
}
|
|
2281
|
+
linkArg(from, fromEdge, t) {
|
|
2282
|
+
return { from: from.item, to: t.n.item, fromEdge, toEdge: t.edge, type: dependencyType(fromEdge, t.edge) };
|
|
2283
|
+
}
|
|
2284
|
+
linkState(s, phase, linked = false) {
|
|
2285
|
+
const t = s.target;
|
|
2286
|
+
return {
|
|
2287
|
+
phase,
|
|
2288
|
+
from: s.from.item,
|
|
2289
|
+
fromEdge: s.fromEdge,
|
|
2290
|
+
to: t?.n.item ?? null,
|
|
2291
|
+
toEdge: t?.edge ?? null,
|
|
2292
|
+
type: t ? dependencyType(s.fromEdge, t.edge) : null,
|
|
2293
|
+
valid: s.valid,
|
|
2294
|
+
reason: s.reason,
|
|
2295
|
+
linked
|
|
2296
|
+
};
|
|
2297
|
+
}
|
|
2298
|
+
markTarget(t, on, invalid = false) {
|
|
2299
|
+
if (!t) return;
|
|
2300
|
+
t.el.classList.toggle("is-link-target", on);
|
|
2301
|
+
t.el.classList.toggle("is-link-invalid", on && invalid);
|
|
2302
|
+
for (const p of t.el.querySelectorAll(".tnc-port")) {
|
|
2303
|
+
const hit = on && p.dataset.edge === t.edge;
|
|
2304
|
+
p.classList.toggle("is-target", hit);
|
|
2305
|
+
p.classList.toggle("is-invalid", hit && invalid);
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
onLinkDown = (ev) => {
|
|
2309
|
+
const port = ev.target.closest?.(".tnc-port");
|
|
2310
|
+
if (!port || !this.tl.onLink || ev.button !== 0 || this.link) return;
|
|
2311
|
+
const barEl = port.closest(".tnc-item");
|
|
2312
|
+
const meta = barEl ? this.ctx.meta.get(barEl) : void 0;
|
|
2313
|
+
if (!meta) return;
|
|
2314
|
+
ev.stopPropagation();
|
|
2315
|
+
ev.preventDefault();
|
|
2316
|
+
const rowsR = this.rowsEl.getBoundingClientRect();
|
|
2317
|
+
const pr = port.getBoundingClientRect();
|
|
2318
|
+
const x = pr.left + pr.width / 2 - rowsR.left - this.labelWidth;
|
|
2319
|
+
const y = pr.top + pr.height / 2 - rowsR.top;
|
|
2320
|
+
const path = this.ctx.doc.createElementNS(SVG, "path");
|
|
2321
|
+
path.classList.add("is-linking");
|
|
2322
|
+
path.setAttribute("d", `M${x},${y}`);
|
|
2323
|
+
this.svg.append(path);
|
|
2324
|
+
const doc = this.ctx.doc;
|
|
2325
|
+
const move = (e) => {
|
|
2326
|
+
const s = this.link;
|
|
2327
|
+
if (!s || e.pointerId !== s.pointerId) return;
|
|
2328
|
+
e.preventDefault();
|
|
2329
|
+
const t = this.linkTargetAt(e.clientX, e.clientY, s.from);
|
|
2330
|
+
const r = this.rowsEl.getBoundingClientRect();
|
|
2331
|
+
const end = t ? this.targetPoint(t) : { x: e.clientX - r.left - this.labelWidth, y: e.clientY - r.top };
|
|
2332
|
+
s.path.setAttribute("d", `M${s.x},${s.y} L${end.x},${end.y}`);
|
|
2333
|
+
if (t?.n.id === s.target?.n.id && t?.edge === s.target?.edge) return;
|
|
2334
|
+
this.markTarget(s.target, false);
|
|
2335
|
+
s.target = t;
|
|
2336
|
+
s.valid = true;
|
|
2337
|
+
s.reason = null;
|
|
2338
|
+
if (t) {
|
|
2339
|
+
const verdict = this.tl.canLink?.(this.linkArg(s.from, s.fromEdge, t)) ?? true;
|
|
2340
|
+
s.valid = verdict === true;
|
|
2341
|
+
s.reason = typeof verdict === "string" ? verdict : null;
|
|
2342
|
+
}
|
|
2343
|
+
s.path.classList.toggle("is-invalid", !s.valid);
|
|
2344
|
+
this.markTarget(t, true, !s.valid);
|
|
2345
|
+
this.tl.onLinkDrag?.(this.linkState(s, "over"));
|
|
2346
|
+
};
|
|
2347
|
+
const finish = (e) => {
|
|
2348
|
+
const s = this.link;
|
|
2349
|
+
if (!s) return;
|
|
2350
|
+
if (e) move(e);
|
|
2351
|
+
this.endLink(e !== null);
|
|
2352
|
+
};
|
|
2353
|
+
const up = (e) => {
|
|
2354
|
+
if (this.link && e.pointerId === this.link.pointerId) finish(e);
|
|
2355
|
+
};
|
|
2356
|
+
const cancel = (e) => {
|
|
2357
|
+
if (this.link && e.pointerId === this.link.pointerId) finish(null);
|
|
2358
|
+
};
|
|
2359
|
+
const key = (e) => {
|
|
2360
|
+
if (e.key !== "Escape") return;
|
|
2361
|
+
e.preventDefault();
|
|
2362
|
+
e.stopPropagation();
|
|
2363
|
+
finish(null);
|
|
2364
|
+
};
|
|
2365
|
+
doc.addEventListener("pointermove", move);
|
|
2366
|
+
doc.addEventListener("pointerup", up);
|
|
2367
|
+
doc.addEventListener("pointercancel", cancel);
|
|
2368
|
+
doc.addEventListener("keydown", key, true);
|
|
2369
|
+
this.link = {
|
|
2370
|
+
from: meta.n,
|
|
2371
|
+
fromEdge: port.dataset.edge === "start" ? "start" : "finish",
|
|
2372
|
+
path,
|
|
2373
|
+
x,
|
|
2374
|
+
y,
|
|
2375
|
+
pointerId: ev.pointerId,
|
|
2376
|
+
target: null,
|
|
2377
|
+
valid: true,
|
|
2378
|
+
reason: null,
|
|
2379
|
+
cleanup: () => {
|
|
2380
|
+
doc.removeEventListener("pointermove", move);
|
|
2381
|
+
doc.removeEventListener("pointerup", up);
|
|
2382
|
+
doc.removeEventListener("pointercancel", cancel);
|
|
2383
|
+
doc.removeEventListener("keydown", key, true);
|
|
2384
|
+
}
|
|
2385
|
+
};
|
|
2386
|
+
this.el.classList.add("is-linking");
|
|
2387
|
+
this.tl.onLinkDrag?.(this.linkState(this.link, "start"));
|
|
2388
|
+
};
|
|
2389
|
+
endLink(commit) {
|
|
2390
|
+
const s = this.link;
|
|
2391
|
+
if (!s) return;
|
|
2392
|
+
this.link = null;
|
|
2393
|
+
s.cleanup();
|
|
2394
|
+
s.path.remove();
|
|
2395
|
+
this.markTarget(s.target, false);
|
|
2396
|
+
this.el.classList.remove("is-linking");
|
|
2397
|
+
if (commit) this.ctx.suppressNextClick();
|
|
2398
|
+
else s.target = null;
|
|
2399
|
+
const linked = commit && !!s.target && s.valid;
|
|
2400
|
+
if (linked && s.target) this.tl.onLink?.(this.linkArg(s.from, s.fromEdge, s.target));
|
|
2401
|
+
this.tl.onLinkDrag?.(this.linkState(s, "end", linked));
|
|
2402
|
+
}
|
|
2403
|
+
/* ------------------------------------------------------------ selection */
|
|
2404
|
+
showSelection(sel) {
|
|
2405
|
+
this.selLayer.replaceChildren();
|
|
2406
|
+
if (!sel) return;
|
|
2407
|
+
const row = sel.rowId ? this.rows.find((r) => r.row.id === sel.rowId) : void 0;
|
|
2408
|
+
if (sel.rowId && !row) return;
|
|
2409
|
+
const x0 = sel.allDay ? this.xDay(sel.startDay) : this.x(sel.startMs);
|
|
2410
|
+
const x1 = sel.allDay ? this.xDay(sel.endDay) : this.x(sel.endMs);
|
|
2411
|
+
const box = h(this.ctx.doc, "div", `tnc-sel${row ? "" : " is-column"}`);
|
|
2412
|
+
const height = row ? row.height : parseFloat(this.rowsEl.style.height) || 0;
|
|
2413
|
+
setStyle(box, { left: this.labelWidth + x0, width: Math.max(2, x1 - x0), top: row ? row.top : 0, height });
|
|
2414
|
+
this.selLayer.append(box);
|
|
2415
|
+
}
|
|
2416
|
+
/* ------------------------------------------------------------- hit test */
|
|
2417
|
+
hit(x, y) {
|
|
2418
|
+
const vr = this.scroller.getBoundingClientRect();
|
|
2419
|
+
const right = vr.left + (this.scroller.clientWidth || vr.width);
|
|
2420
|
+
const bottom = vr.top + (this.scroller.clientHeight || vr.height);
|
|
2421
|
+
if (x < vr.left || x >= right || y < vr.top || y >= bottom) return null;
|
|
2422
|
+
const r = this.rowsEl.getBoundingClientRect();
|
|
2423
|
+
const px2 = x - r.left - this.labelWidth;
|
|
2424
|
+
const days = px2 / this.dayWidth;
|
|
2425
|
+
const day = this.startDay + Math.floor(days);
|
|
2426
|
+
const minute = (days - Math.floor(days)) * MINUTES_PER_DAY;
|
|
2427
|
+
const inLabel = x < vr.left + this.labelWidth;
|
|
2428
|
+
const inHead = y < this.head.getBoundingClientRect().bottom;
|
|
2429
|
+
const rel = y - r.top;
|
|
2430
|
+
const row = inHead ? void 0 : this.rows.find((rec) => rel >= rec.top && rel < rec.top + rec.height);
|
|
2431
|
+
let area;
|
|
2432
|
+
if (inLabel) area = "label";
|
|
2433
|
+
else if (inHead) area = px2 < this.trackWidth() ? "header" : "outside";
|
|
2434
|
+
else area = row && px2 >= 0 && px2 < this.trackWidth() ? "track" : "outside";
|
|
2435
|
+
return { kind: "timeline", rowId: row?.row.id ?? null, day, minute, area, overTrack: area === "track" && !!row && !row.row.group };
|
|
2436
|
+
}
|
|
2437
|
+
tick() {
|
|
2438
|
+
const x = this.x(this.ctx.now());
|
|
2439
|
+
const inRange = x >= 0 && x <= this.trackWidth();
|
|
2440
|
+
this.todayLine.hidden = !inRange;
|
|
2441
|
+
this.todayLine.style.left = `${this.labelWidth + x}px`;
|
|
2442
|
+
if (this.todayTag) {
|
|
2443
|
+
this.todayTag.hidden = !inRange;
|
|
2444
|
+
this.todayTag.style.left = `${x}px`;
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
/** Scrolls so `day` sits a little in from the left edge. */
|
|
2448
|
+
scrollToDay(day) {
|
|
2449
|
+
this.scroller.scrollLeft = Math.max(0, this.xDay(day) - this.dayWidth * 2);
|
|
2450
|
+
}
|
|
2451
|
+
getScroll() {
|
|
2452
|
+
return { top: this.scroller.scrollTop, left: this.scroller.scrollLeft };
|
|
2453
|
+
}
|
|
2454
|
+
setScroll(s) {
|
|
2455
|
+
this.scroller.scrollTop = s.top;
|
|
2456
|
+
this.scroller.scrollLeft = s.left;
|
|
2457
|
+
}
|
|
2458
|
+
elementsFor(id) {
|
|
2459
|
+
return this.rows.flatMap((r) => r.pool?.elementsFor(id) ?? []);
|
|
2460
|
+
}
|
|
2461
|
+
destroy() {
|
|
2462
|
+
this.endLink(false);
|
|
2463
|
+
this.el.removeEventListener("pointerdown", this.onLinkDown, true);
|
|
2464
|
+
for (const r of this.rows) this.unmountRow(r);
|
|
2465
|
+
this.rows = [];
|
|
2466
|
+
this.el.remove();
|
|
2467
|
+
}
|
|
2468
|
+
};
|
|
2469
|
+
function dependencyPath(type, x1, y1, x2, y2) {
|
|
2470
|
+
const gap = 10;
|
|
2471
|
+
const out = type[0] === "S" ? -1 : 1;
|
|
2472
|
+
const side = type[1] === "S" ? -1 : 1;
|
|
2473
|
+
const xa = x1 + out * gap;
|
|
2474
|
+
const xb = x2 + side * gap;
|
|
2475
|
+
const xEnd = x2 + side * 2;
|
|
2476
|
+
if (out === side) {
|
|
2477
|
+
const xe = out < 0 ? Math.min(xa, xb) : Math.max(xa, xb);
|
|
2478
|
+
return `M${x1},${y1} H${xe} V${y2} H${xEnd}`;
|
|
2479
|
+
}
|
|
2480
|
+
if (out > 0 ? xa <= xb : xa >= xb) {
|
|
2481
|
+
const mid = (x1 + x2) / 2;
|
|
2482
|
+
return `M${x1},${y1} H${mid} V${y2} H${xEnd}`;
|
|
2483
|
+
}
|
|
2484
|
+
return `M${x1},${y1} H${xa} V${(y1 + y2) / 2} H${xb} V${y2} H${xEnd}`;
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
// src/dom/views/year.ts
|
|
2488
|
+
var YearView = class {
|
|
2489
|
+
constructor(ctx) {
|
|
2490
|
+
this.ctx = ctx;
|
|
2491
|
+
const { doc, fmt, opts } = ctx;
|
|
2492
|
+
this.el = h(doc, "div", "tnc-year");
|
|
2493
|
+
this.scroller = h(doc, "div", "tnc-year-scroll");
|
|
2494
|
+
const grid = h(doc, "div", "tnc-year-grid");
|
|
2495
|
+
let month = ctx.view.range.startDay;
|
|
2496
|
+
for (let i = 0; i < 12; i++) {
|
|
2497
|
+
const { year, month: m } = ymdOfDay(month);
|
|
2498
|
+
const box = h(doc, "section", "tnc-ym", { "aria-label": fmt.monthYear(month) });
|
|
2499
|
+
const title = opts.navLinks ? h(doc, "button", "tnc-ym-title", { type: "button", "data-month": `${year}-${m}` }, fmt.monthLong(month)) : h(doc, "div", "tnc-ym-title", null, fmt.monthLong(month));
|
|
2500
|
+
const first = month;
|
|
2501
|
+
if (opts.navLinks) title.addEventListener("click", (ev) => this.ctx.navigate(first, ev, "month"));
|
|
2502
|
+
box.append(title);
|
|
2503
|
+
const days = h(doc, "div", "tnc-ym-days", { role: "group", "aria-label": fmt.monthYear(month) });
|
|
2504
|
+
const start = startOfWeek(month, ctx.firstDay);
|
|
2505
|
+
for (let d = start; d < start + 7; d++) days.append(h(doc, "div", "tnc-ym-dow", { "aria-hidden": "true" }, fmt.weekdayNarrow(d)));
|
|
2506
|
+
const len = daysInMonth(year, m);
|
|
2507
|
+
for (let d = start; d < month; d++) days.append(h(doc, "div", "tnc-ym-pad"));
|
|
2508
|
+
for (let d = month; d < month + len; d++) {
|
|
2509
|
+
const arg = ctx.dayArg(d);
|
|
2510
|
+
const cell = h(doc, "button", ["tnc-ym-day", ...ctx.dayClasses(d)].join(" "), {
|
|
2511
|
+
type: "button",
|
|
2512
|
+
"data-date": arg.dateStr,
|
|
2513
|
+
"aria-label": fmt.longDate(d)
|
|
2514
|
+
});
|
|
2515
|
+
const dots = h(doc, "span", "tnc-ym-dots", { "aria-hidden": "true" });
|
|
2516
|
+
cell.append(h(doc, "span", "tnc-ym-num", null, fmt.dayNumber(d)), dots);
|
|
2517
|
+
days.append(cell);
|
|
2518
|
+
this.cells.push({ day: d, el: cell, dots });
|
|
2519
|
+
}
|
|
2520
|
+
box.append(days);
|
|
2521
|
+
grid.append(box);
|
|
2522
|
+
month = addMonths(month, 1);
|
|
2523
|
+
}
|
|
2524
|
+
this.scroller.append(grid);
|
|
2525
|
+
this.el.append(this.scroller);
|
|
2526
|
+
}
|
|
2527
|
+
ctx;
|
|
2528
|
+
el;
|
|
2529
|
+
scroller;
|
|
2530
|
+
cells = [];
|
|
2531
|
+
render(_change) {
|
|
2532
|
+
const { store } = this.ctx;
|
|
2533
|
+
const doc = this.ctx.doc;
|
|
2534
|
+
for (const c of this.cells) {
|
|
2535
|
+
const items = store.inRange(c.day, c.day + 1).filter((n) => n.item.display !== "background").sort(this.ctx.order);
|
|
2536
|
+
c.el.dataset.count = String(items.length);
|
|
2537
|
+
c.el.classList.toggle("has-items", items.length > 0);
|
|
2538
|
+
c.el.style.setProperty("--tnc-heat", String(Math.min(1, items.length / 6)));
|
|
2539
|
+
c.dots.replaceChildren(
|
|
2540
|
+
...items.slice(0, 3).map((n) => {
|
|
2541
|
+
const dot = h(doc, "i");
|
|
2542
|
+
const color = n.item.color ?? n.item.borderColor ?? n.item.backgroundColor;
|
|
2543
|
+
if (color) dot.style.setProperty("--tnc-c", color);
|
|
2544
|
+
return dot;
|
|
2545
|
+
})
|
|
2546
|
+
);
|
|
2547
|
+
const label = this.ctx.fmt.longDate(c.day);
|
|
2548
|
+
c.el.setAttribute("aria-label", items.length ? `${label}, ${this.ctx.text.itemCount(items.length)}` : label);
|
|
2549
|
+
c.el.title = items.length ? `${label}
|
|
2550
|
+
${items.slice(0, 8).map((n) => `\u2022 ${n.item.title}`).join("\n")}${items.length > 8 ? `
|
|
2551
|
+
${this.ctx.text.more(items.length - 8)}` : ""}` : label;
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
showSelection(_sel) {
|
|
2555
|
+
}
|
|
2556
|
+
hit(x, y) {
|
|
2557
|
+
const el = this.ctx.doc.elementFromPoint(x, y)?.closest(".tnc-ym-day");
|
|
2558
|
+
const c = el ? this.cells.find((cell) => cell.el === el) : void 0;
|
|
2559
|
+
return c ? { kind: "day", day: c.day } : null;
|
|
2560
|
+
}
|
|
2561
|
+
dayAt(el) {
|
|
2562
|
+
const btn = el.closest(".tnc-ym-day");
|
|
2563
|
+
return this.cells.find((c) => c.el === btn)?.day ?? null;
|
|
2564
|
+
}
|
|
2565
|
+
getScroll() {
|
|
2566
|
+
return { top: this.scroller.scrollTop, left: 0 };
|
|
2567
|
+
}
|
|
2568
|
+
setScroll(s) {
|
|
2569
|
+
this.scroller.scrollTop = s.top;
|
|
2570
|
+
}
|
|
2571
|
+
elementsFor(_id) {
|
|
2572
|
+
return [];
|
|
2573
|
+
}
|
|
2574
|
+
destroy() {
|
|
2575
|
+
this.el.remove();
|
|
2576
|
+
}
|
|
2577
|
+
};
|
|
2578
|
+
|
|
2579
|
+
// src/dom/calendar.ts
|
|
2580
|
+
var EXTERNAL_ID = "__tnc_external__";
|
|
2581
|
+
function sameOption(a, b, depth = 0) {
|
|
2582
|
+
if (a === b) return true;
|
|
2583
|
+
if (typeof a === "function" && typeof b === "function") return true;
|
|
2584
|
+
if (depth > 4 || !a || !b || typeof a !== "object" || typeof b !== "object") return false;
|
|
2585
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
2586
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
|
|
2587
|
+
return a.every((v, i) => sameOption(v, b[i], depth + 1));
|
|
2588
|
+
}
|
|
2589
|
+
if (Object.getPrototypeOf(a) !== Object.prototype || Object.getPrototypeOf(b) !== Object.prototype) return false;
|
|
2590
|
+
const ka = Object.keys(a);
|
|
2591
|
+
const kb = Object.keys(b);
|
|
2592
|
+
if (ka.length !== kb.length) return false;
|
|
2593
|
+
return ka.every((k) => sameOption(a[k], b[k], depth + 1));
|
|
2594
|
+
}
|
|
2595
|
+
function timelineSansRows(t) {
|
|
2596
|
+
return t ? { ...t, rows: null } : t;
|
|
2597
|
+
}
|
|
2598
|
+
function sameProposal(a, b) {
|
|
2599
|
+
return !!a && !!b && a.startMs === b.startMs && a.endMs === b.endMs && a.allDay === b.allDay && a.rowId === b.rowId;
|
|
2600
|
+
}
|
|
2601
|
+
var Calendar = class {
|
|
2602
|
+
constructor(host, options = {}, items = []) {
|
|
2603
|
+
this.host = host;
|
|
2604
|
+
this.doc = host.ownerDocument;
|
|
2605
|
+
this.win = this.doc.defaultView ?? globalThis;
|
|
2606
|
+
this.opts = { ...defaultOptions(), ...options };
|
|
2607
|
+
this.zone = getTimeZone(this.opts.timeZone);
|
|
2608
|
+
this.fmt = new Formatter(this.opts.locale, this.opts.hour12);
|
|
2609
|
+
this.text = this.resolveText();
|
|
2610
|
+
this.order = parseOrder(this.opts.eventOrder);
|
|
2611
|
+
this.store = new ItemStore(this.zone, {
|
|
2612
|
+
defaultTimedMinutes: this.opts.defaultTimedMinutes,
|
|
2613
|
+
defaultAllDayDays: this.opts.defaultAllDayDays
|
|
2614
|
+
});
|
|
2615
|
+
this.store.setAll(items);
|
|
2616
|
+
this.conflicts = dependencyConflicts(this.store);
|
|
2617
|
+
this.anchor = this.toDay(this.opts.date) ?? this.todayDay();
|
|
2618
|
+
this.selectedIds = this.selectedSet();
|
|
2619
|
+
this.root = h(this.doc, "div", "tnc", { "data-tnc": "" });
|
|
2620
|
+
this.toolbarEl = h(this.doc, "div", "tnc-toolbar", { role: "toolbar" });
|
|
2621
|
+
this.viewHost = h(this.doc, "div", "tnc-view");
|
|
2622
|
+
this.root.append(this.toolbarEl, this.viewHost);
|
|
2623
|
+
host.append(this.root);
|
|
2624
|
+
this.bindEvents();
|
|
2625
|
+
this.rebuild(true);
|
|
2626
|
+
this.scheduleTick();
|
|
2627
|
+
const RO = this.win.ResizeObserver;
|
|
2628
|
+
if (RO) {
|
|
2629
|
+
this.resizeObs = new RO(() => this.current?.resize?.());
|
|
2630
|
+
this.resizeObs.observe(this.root);
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
host;
|
|
2634
|
+
doc;
|
|
2635
|
+
win;
|
|
2636
|
+
root;
|
|
2637
|
+
store;
|
|
2638
|
+
meta = /* @__PURE__ */ new WeakMap();
|
|
2639
|
+
opts;
|
|
2640
|
+
zone;
|
|
2641
|
+
fmt;
|
|
2642
|
+
text;
|
|
2643
|
+
order;
|
|
2644
|
+
view;
|
|
2645
|
+
firstDay = 0;
|
|
2646
|
+
days = [];
|
|
2647
|
+
today = 0;
|
|
2648
|
+
conflicts = /* @__PURE__ */ new Map();
|
|
2649
|
+
slot = { min: 0, max: MINUTES_PER_DAY, duration: 30, snap: 30, scroll: 480 };
|
|
2650
|
+
draggingId = null;
|
|
2651
|
+
dragRow = null;
|
|
2652
|
+
toolbarEl;
|
|
2653
|
+
viewHost;
|
|
2654
|
+
current = null;
|
|
2655
|
+
anchor = 0;
|
|
2656
|
+
session = null;
|
|
2657
|
+
selection = null;
|
|
2658
|
+
suppressClick = false;
|
|
2659
|
+
selectedIds = /* @__PURE__ */ new Set();
|
|
2660
|
+
popover = null;
|
|
2661
|
+
timer = null;
|
|
2662
|
+
resizeObs = null;
|
|
2663
|
+
destroyed = false;
|
|
2664
|
+
pendingChange = null;
|
|
2665
|
+
frame = null;
|
|
2666
|
+
/* ================================================================ public */
|
|
2667
|
+
/** Replaces all items, re-rendering only what changed. */
|
|
2668
|
+
setItems(items) {
|
|
2669
|
+
const drag = this.session?.active && this.session.origin ? this.store.get(this.session.origin.id) : void 0;
|
|
2670
|
+
const change = this.store.setAll(items);
|
|
2671
|
+
if (drag && this.session?.origin) {
|
|
2672
|
+
const fresh = this.store.get(drag.id);
|
|
2673
|
+
if (fresh) this.session.origin = fresh;
|
|
2674
|
+
this.merge(change, this.store.upsert([drag.item]));
|
|
2675
|
+
}
|
|
2676
|
+
this.applyChange(change);
|
|
2677
|
+
}
|
|
2678
|
+
upsertItems(items) {
|
|
2679
|
+
this.applyChange(this.store.upsert(items));
|
|
2680
|
+
}
|
|
2681
|
+
removeItems(ids) {
|
|
2682
|
+
this.applyChange(this.store.remove(ids));
|
|
2683
|
+
}
|
|
2684
|
+
getItem(id) {
|
|
2685
|
+
return this.store.get(id)?.item;
|
|
2686
|
+
}
|
|
2687
|
+
/**
|
|
2688
|
+
* Updates options. Only what the change affects is rebuilt.
|
|
2689
|
+
*
|
|
2690
|
+
* In the timeline, a new `date`, range or scale keeps what is on screen
|
|
2691
|
+
* instead of jumping back to the anchor date: a scale change keeps the
|
|
2692
|
+
* instant at the middle of the visible track, other changes keep the left
|
|
2693
|
+
* edge. Pass `anchor` to pin a different instant, e.g. the one under the
|
|
2694
|
+
* pointer when zooming with the wheel.
|
|
2695
|
+
*/
|
|
2696
|
+
setOptions(next, how = {}) {
|
|
2697
|
+
const prev = this.opts;
|
|
2698
|
+
this.opts = { ...prev, ...next };
|
|
2699
|
+
const changed = Object.keys(next).filter(
|
|
2700
|
+
(k) => !sameOption(next[k], prev[k])
|
|
2701
|
+
);
|
|
2702
|
+
if (!changed.length) return;
|
|
2703
|
+
const has = (k) => changed.includes(k);
|
|
2704
|
+
if (has("selected")) this.applySelected();
|
|
2705
|
+
const rest = changed.filter((k) => k !== "selected");
|
|
2706
|
+
if (!rest.length) return;
|
|
2707
|
+
if (rest.length === 1 && has("timeline") && this.current instanceof TimelineView && sameOption(timelineSansRows(this.opts.timeline), timelineSansRows(prev.timeline))) {
|
|
2708
|
+
this.current.render(null);
|
|
2709
|
+
if (this.selection) this.current.showSelection(this.selection);
|
|
2710
|
+
return;
|
|
2711
|
+
}
|
|
2712
|
+
if (has("timeZone") || has("defaultTimedMinutes") || has("defaultAllDayDays")) {
|
|
2713
|
+
this.zone = getTimeZone(this.opts.timeZone);
|
|
2714
|
+
this.store.reconfigure(this.zone, {
|
|
2715
|
+
defaultTimedMinutes: this.opts.defaultTimedMinutes,
|
|
2716
|
+
defaultAllDayDays: this.opts.defaultAllDayDays
|
|
2717
|
+
});
|
|
2718
|
+
}
|
|
2719
|
+
if (has("locale") || has("hour12")) this.fmt = new Formatter(this.opts.locale, this.opts.hour12);
|
|
2720
|
+
if (has("text") || has("locale")) this.text = this.resolveText();
|
|
2721
|
+
if (has("eventOrder")) this.order = parseOrder(this.opts.eventOrder);
|
|
2722
|
+
if (has("date")) this.anchor = this.toDay(this.opts.date) ?? this.anchor;
|
|
2723
|
+
const needsRebuild = changed.some((k) => LAYOUT_KEYS.has(k)) || has("date");
|
|
2724
|
+
if (!needsRebuild) return;
|
|
2725
|
+
const keep = !has("view") && this.opts.view === "timeline" ? this.timelineKeep(prev, how.anchor) : null;
|
|
2726
|
+
if (keep) this.rebuild(false, keep);
|
|
2727
|
+
else this.rebuild(has("view") || has("date"));
|
|
2728
|
+
}
|
|
2729
|
+
/** What a timeline rebuild should keep on screen. */
|
|
2730
|
+
timelineKeep(prev, anchor) {
|
|
2731
|
+
const view = this.current;
|
|
2732
|
+
if (!(view instanceof TimelineView)) return null;
|
|
2733
|
+
const top = view.getScroll().top;
|
|
2734
|
+
if (anchor) {
|
|
2735
|
+
const date = anchor.date !== void 0 ? this.toMs(anchor.date) : null;
|
|
2736
|
+
if (anchor.clientX !== void 0) {
|
|
2737
|
+
const vx2 = anchor.clientX - view.viewportLeft();
|
|
2738
|
+
return { ms: date ?? view.instantAt(vx2), vx: vx2, top };
|
|
2739
|
+
}
|
|
2740
|
+
if (date !== null) return { ms: date, vx: view.viewportXOf(date), top };
|
|
2741
|
+
}
|
|
2742
|
+
const before = prev.timeline?.scale;
|
|
2743
|
+
const after = this.opts.timeline?.scale;
|
|
2744
|
+
const rescaled = !sameOption(before, after) || prev.timeline?.labelWidth !== this.opts.timeline?.labelWidth;
|
|
2745
|
+
const vx = rescaled ? view.viewportCentre() : this.opts.timeline?.labelWidth ?? 240;
|
|
2746
|
+
return { ms: view.instantAt(vx), vx, top };
|
|
2747
|
+
}
|
|
2748
|
+
/** Re-renders everything, e.g. after render hooks start returning different content. */
|
|
2749
|
+
rerender() {
|
|
2750
|
+
this.rebuild(false);
|
|
2751
|
+
}
|
|
2752
|
+
getOptions() {
|
|
2753
|
+
return this.opts;
|
|
2754
|
+
}
|
|
2755
|
+
changeView(view, date) {
|
|
2756
|
+
if (date !== void 0) this.anchor = this.toDay(date) ?? this.anchor;
|
|
2757
|
+
if (view === this.opts.view && date === void 0) return;
|
|
2758
|
+
this.opts = { ...this.opts, view };
|
|
2759
|
+
this.rebuild(true);
|
|
2760
|
+
this.opts.onViewChange?.(this.view);
|
|
2761
|
+
}
|
|
2762
|
+
gotoDate(date) {
|
|
2763
|
+
const day = this.toDay(date);
|
|
2764
|
+
if (day === null) return;
|
|
2765
|
+
this.anchor = day;
|
|
2766
|
+
this.rebuild(true);
|
|
2767
|
+
}
|
|
2768
|
+
prev() {
|
|
2769
|
+
this.anchor = this.step(-1);
|
|
2770
|
+
this.rebuild(true);
|
|
2771
|
+
}
|
|
2772
|
+
next() {
|
|
2773
|
+
this.anchor = this.step(1);
|
|
2774
|
+
this.rebuild(true);
|
|
2775
|
+
}
|
|
2776
|
+
goToday() {
|
|
2777
|
+
this.anchor = this.todayDay();
|
|
2778
|
+
this.rebuild(true);
|
|
2779
|
+
}
|
|
2780
|
+
/** The anchor date, as local midnight of that civil day in the calendar zone. */
|
|
2781
|
+
getDate() {
|
|
2782
|
+
return new Date(toInstant(this.anchor, 0, this.zone));
|
|
2783
|
+
}
|
|
2784
|
+
getView() {
|
|
2785
|
+
return this.view;
|
|
2786
|
+
}
|
|
2787
|
+
/** Clears the current selection. */
|
|
2788
|
+
unselect() {
|
|
2789
|
+
if (!this.selection) return;
|
|
2790
|
+
this.selection = null;
|
|
2791
|
+
this.current?.showSelection(null);
|
|
2792
|
+
this.opts.onUnselect?.();
|
|
2793
|
+
}
|
|
2794
|
+
/** Re-measures (e.g. after the host was shown or resized by a parent). */
|
|
2795
|
+
updateSize() {
|
|
2796
|
+
this.current?.resize?.();
|
|
2797
|
+
}
|
|
2798
|
+
getScroll() {
|
|
2799
|
+
return this.current?.getScroll() ?? { top: 0, left: 0 };
|
|
2800
|
+
}
|
|
2801
|
+
setScroll(s) {
|
|
2802
|
+
this.current?.setScroll(s);
|
|
2803
|
+
}
|
|
2804
|
+
scrollToTime(time) {
|
|
2805
|
+
this.current?.scrollToTime?.(parseDurationMinutes(time, this.slot.scroll));
|
|
2806
|
+
}
|
|
2807
|
+
/**
|
|
2808
|
+
* Scrolls a date (or instant) into view. In the timeline `align` places it
|
|
2809
|
+
* at the start, middle or end of the visible track; other views move to
|
|
2810
|
+
* the date if it is outside the range, and time grids scroll to its time.
|
|
2811
|
+
*/
|
|
2812
|
+
scrollToDate(date, opts = {}) {
|
|
2813
|
+
const p = parseDateInput(date, this.zone);
|
|
2814
|
+
if (!p) return;
|
|
2815
|
+
const day = p.kind === "day" ? p.day : dayOf(p.ms, this.zone);
|
|
2816
|
+
const ms = p.kind === "day" ? toInstant(p.day, 0, this.zone) : p.ms;
|
|
2817
|
+
const { startDay, endDay } = this.view.range;
|
|
2818
|
+
if (day < startDay || day >= endDay) {
|
|
2819
|
+
this.anchor = day;
|
|
2820
|
+
this.rebuild(true);
|
|
2821
|
+
}
|
|
2822
|
+
const view = this.current;
|
|
2823
|
+
if (view instanceof TimelineView) view.scrollToInstant(ms, opts.align ?? "center", opts.smooth ?? false);
|
|
2824
|
+
else if (view instanceof TimeGridView && p.kind !== "day") view.scrollToTime(wallTime(ms, this.zone).minute);
|
|
2825
|
+
}
|
|
2826
|
+
/** What is under a client point: a time slot, a day, or a timeline row and date. Null outside the view. */
|
|
2827
|
+
hitTest(clientX, clientY) {
|
|
2828
|
+
return this.current?.hit(clientX, clientY) ?? null;
|
|
2829
|
+
}
|
|
2830
|
+
/** Item elements currently rendered for an id. */
|
|
2831
|
+
elementsFor(id) {
|
|
2832
|
+
return this.current?.elementsFor(id) ?? [];
|
|
2833
|
+
}
|
|
2834
|
+
/**
|
|
2835
|
+
* Starts dragging something from outside the calendar (a task tray, an
|
|
2836
|
+
* unscheduled list). Call from a pointerdown handler; the calendar follows
|
|
2837
|
+
* the pointer and fires `onExternalDrop` on release.
|
|
2838
|
+
*/
|
|
2839
|
+
beginExternalDrag(ev, source) {
|
|
2840
|
+
this.endSession(null, true);
|
|
2841
|
+
this.session = {
|
|
2842
|
+
mode: "external",
|
|
2843
|
+
pointerId: ev.pointerId,
|
|
2844
|
+
pointerType: ev.pointerType,
|
|
2845
|
+
x0: ev.clientX,
|
|
2846
|
+
y0: ev.clientY,
|
|
2847
|
+
active: true,
|
|
2848
|
+
origin: null,
|
|
2849
|
+
grab: null,
|
|
2850
|
+
proposal: null,
|
|
2851
|
+
denied: false,
|
|
2852
|
+
timer: null,
|
|
2853
|
+
lastEvent: ev,
|
|
2854
|
+
source
|
|
2855
|
+
};
|
|
2856
|
+
this.root.classList.add("tnc-dragging", "tnc-dragging-external");
|
|
2857
|
+
this.listenDoc(true);
|
|
2858
|
+
}
|
|
2859
|
+
destroy() {
|
|
2860
|
+
if (this.destroyed) return;
|
|
2861
|
+
this.destroyed = true;
|
|
2862
|
+
this.endSession(null, true);
|
|
2863
|
+
this.closeMore();
|
|
2864
|
+
if (this.timer !== null) this.win.clearTimeout(this.timer);
|
|
2865
|
+
if (this.frame !== null) this.win.cancelAnimationFrame(this.frame);
|
|
2866
|
+
this.resizeObs?.disconnect();
|
|
2867
|
+
this.unbindEvents();
|
|
2868
|
+
this.current?.destroy();
|
|
2869
|
+
this.current = null;
|
|
2870
|
+
this.root.remove();
|
|
2871
|
+
}
|
|
2872
|
+
/* ========================================================== view context */
|
|
2873
|
+
now() {
|
|
2874
|
+
return Date.now();
|
|
2875
|
+
}
|
|
2876
|
+
suppressNextClick() {
|
|
2877
|
+
this.suppressClick = true;
|
|
2878
|
+
this.win.setTimeout(() => this.suppressClick = false, 0);
|
|
2879
|
+
}
|
|
2880
|
+
dayArg(day) {
|
|
2881
|
+
return {
|
|
2882
|
+
day,
|
|
2883
|
+
date: new Date(toInstant(day, 0, this.zone)),
|
|
2884
|
+
dateStr: formatDay(day),
|
|
2885
|
+
view: this.view,
|
|
2886
|
+
isToday: day === this.today,
|
|
2887
|
+
isPast: day < this.today
|
|
2888
|
+
};
|
|
2889
|
+
}
|
|
2890
|
+
dayClasses(day) {
|
|
2891
|
+
const out = [];
|
|
2892
|
+
const wd = weekdayOf(day);
|
|
2893
|
+
if (wd === 0 || wd === 6) out.push("is-weekend");
|
|
2894
|
+
if (day === this.today && this.opts.todayHighlight) out.push("is-today");
|
|
2895
|
+
if (day < this.today) out.push("is-past");
|
|
2896
|
+
out.push(...classList(this.opts.dayClassNames?.(this.dayArg(day))));
|
|
2897
|
+
return out;
|
|
2898
|
+
}
|
|
2899
|
+
isEditable(n, what) {
|
|
2900
|
+
if (n.id === EXTERNAL_ID || this.opts.view === "list" || this.opts.view === "year") return false;
|
|
2901
|
+
const it = n.item;
|
|
2902
|
+
const base = it.editable ?? this.opts.editable;
|
|
2903
|
+
const specific = what === "start" ? it.startEditable : it.durationEditable;
|
|
2904
|
+
return specific ?? base;
|
|
2905
|
+
}
|
|
2906
|
+
navLink(day, label, cls) {
|
|
2907
|
+
const btn = h(this.doc, "button", `tnc-navlink ${cls}`, { type: "button", "data-nav-date": formatDay(day) }, label);
|
|
2908
|
+
btn.addEventListener("click", (ev) => {
|
|
2909
|
+
ev.stopPropagation();
|
|
2910
|
+
this.navigate(day, ev);
|
|
2911
|
+
});
|
|
2912
|
+
btn.addEventListener("pointerdown", (ev) => ev.stopPropagation());
|
|
2913
|
+
return btn;
|
|
2914
|
+
}
|
|
2915
|
+
navigate(day, jsEvent, view = "day") {
|
|
2916
|
+
if (this.opts.onNavLink) {
|
|
2917
|
+
this.opts.onNavLink({ ...this.dayArg(day), target: view, jsEvent });
|
|
2918
|
+
return;
|
|
2919
|
+
}
|
|
2920
|
+
this.changeView(view, formatDay(day));
|
|
2921
|
+
}
|
|
2922
|
+
createItemEl(meta) {
|
|
2923
|
+
const { n, variant } = meta;
|
|
2924
|
+
const it = n.item;
|
|
2925
|
+
const el = h(this.doc, "div", "tnc-item");
|
|
2926
|
+
el.classList.add(`tnc-item--${variant}`);
|
|
2927
|
+
if (it.kind) el.classList.add(`tnc-k-${it.kind.replace(/[^\w-]/g, "-")}`);
|
|
2928
|
+
if (it.completed) el.classList.add("is-completed");
|
|
2929
|
+
if (it.display === "background") el.classList.add("is-background");
|
|
2930
|
+
if (it.lane === "actual") el.classList.add("is-actual");
|
|
2931
|
+
if (!meta.isStart) el.classList.add("is-cont-start");
|
|
2932
|
+
if (!meta.isEnd) el.classList.add("is-cont-end");
|
|
2933
|
+
if (n.allDay) el.classList.add("is-all-day");
|
|
2934
|
+
if (meta.mirror || n.id === EXTERNAL_ID) el.classList.add("is-mirror");
|
|
2935
|
+
if (this.conflicts.has(n.id)) el.classList.add("is-conflict");
|
|
2936
|
+
const indicator = variant === "indicator";
|
|
2937
|
+
if (!meta.mirror && !indicator && (this.isEditable(n, "start") || this.isEditable(n, "duration"))) el.classList.add("is-draggable");
|
|
2938
|
+
if (this.draggingId === n.id) el.classList.add("is-dragging");
|
|
2939
|
+
if (this.selectedIds.has(n.id) && !meta.mirror) el.classList.add("is-selected");
|
|
2940
|
+
el.classList.add(...classList(it.className));
|
|
2941
|
+
const arg = this.itemArg(el, meta);
|
|
2942
|
+
el.classList.add(...classList(this.opts.itemClassNames?.(arg)));
|
|
2943
|
+
el.dataset.id = n.id;
|
|
2944
|
+
if (it.kind) el.dataset.kind = it.kind;
|
|
2945
|
+
const color = it.color ?? it.borderColor;
|
|
2946
|
+
if (color) el.style.setProperty("--tnc-c", color);
|
|
2947
|
+
if (it.backgroundColor) el.style.setProperty("--tnc-bg", it.backgroundColor);
|
|
2948
|
+
if (it.borderColor) el.style.setProperty("--tnc-border", it.borderColor);
|
|
2949
|
+
if (it.textColor) el.style.setProperty("--tnc-fg", it.textColor);
|
|
2950
|
+
if (indicator) el.setAttribute("aria-hidden", "true");
|
|
2951
|
+
else if (it.display !== "background") {
|
|
2952
|
+
el.tabIndex = 0;
|
|
2953
|
+
el.setAttribute("role", "button");
|
|
2954
|
+
el.setAttribute("aria-label", this.ariaLabel(n));
|
|
2955
|
+
}
|
|
2956
|
+
this.meta.set(el, meta);
|
|
2957
|
+
const custom = variant === "list" ? this.opts.listItemContent?.(arg) : null;
|
|
2958
|
+
const content = indicator ? null : custom ?? this.opts.itemContent?.(arg);
|
|
2959
|
+
if (indicator) {
|
|
2960
|
+
} else if (content != null) {
|
|
2961
|
+
const wrap = h(this.doc, "div", "tnc-item-main");
|
|
2962
|
+
wrap.append(content);
|
|
2963
|
+
el.append(wrap);
|
|
2964
|
+
} else {
|
|
2965
|
+
el.append(this.defaultContent(meta));
|
|
2966
|
+
}
|
|
2967
|
+
if (variant !== "list" && !indicator && it.display !== "background") this.addHandles(el, meta);
|
|
2968
|
+
this.opts.itemDidMount?.(arg);
|
|
2969
|
+
return el;
|
|
2970
|
+
}
|
|
2971
|
+
refreshItemEl(el, meta) {
|
|
2972
|
+
const time = el.querySelector(":scope .tnc-item-time");
|
|
2973
|
+
if (time) this.fillTime(time, meta.n, meta.variant, meta.day);
|
|
2974
|
+
el.classList.toggle("is-dragging", this.draggingId === meta.n.id);
|
|
2975
|
+
el.classList.toggle("is-conflict", this.conflicts.has(meta.n.id));
|
|
2976
|
+
el.classList.toggle("is-selected", this.selectedIds.has(meta.n.id) && !meta.mirror);
|
|
2977
|
+
}
|
|
2978
|
+
selectedSet() {
|
|
2979
|
+
const s = this.opts.selected;
|
|
2980
|
+
return new Set(s == null ? [] : typeof s === "string" ? [s] : s);
|
|
2981
|
+
}
|
|
2982
|
+
applySelected() {
|
|
2983
|
+
this.selectedIds = this.selectedSet();
|
|
2984
|
+
for (const el of this.viewHost.querySelectorAll(".tnc-item")) {
|
|
2985
|
+
const meta = this.meta.get(el);
|
|
2986
|
+
if (meta) el.classList.toggle("is-selected", this.selectedIds.has(meta.n.id) && !meta.mirror);
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
destroyItemEl(el) {
|
|
2990
|
+
const meta = this.meta.get(el);
|
|
2991
|
+
if (meta) this.opts.itemWillUnmount?.(this.itemArg(el, meta));
|
|
2992
|
+
el.remove();
|
|
2993
|
+
}
|
|
2994
|
+
openMore(day, anchor, jsEvent) {
|
|
2995
|
+
jsEvent.stopPropagation();
|
|
2996
|
+
const items = this.store.inRange(day, day + 1).filter((n) => n.item.display !== "background" && (n.allDay || this.opts.view === "month" || !this.opts.timeGrid || this.opts.eventMaxStack > 0)).sort((a, b) => Number(b.allDay) - Number(a.allDay) || this.order(a, b));
|
|
2997
|
+
const shown = new Set(
|
|
2998
|
+
[...this.viewHost.querySelectorAll(".tnc-item")].filter((el2) => this.meta.get(el2)?.day === day || (this.meta.get(el2)?.n.startDay ?? Infinity) <= day).map((el2) => el2.dataset.id ?? "")
|
|
2999
|
+
);
|
|
3000
|
+
const res = this.opts.onMoreClick?.({
|
|
3001
|
+
day,
|
|
3002
|
+
date: new Date(toInstant(day, 0, this.zone)),
|
|
3003
|
+
items: items.map((n) => n.item),
|
|
3004
|
+
hidden: items.filter((n) => !shown.has(n.id)).map((n) => n.item),
|
|
3005
|
+
jsEvent,
|
|
3006
|
+
el: anchor,
|
|
3007
|
+
view: this.view
|
|
3008
|
+
});
|
|
3009
|
+
if (res === false) return;
|
|
3010
|
+
this.closeMore();
|
|
3011
|
+
const el = h(this.doc, "div", "tnc-popover", { role: "dialog", "aria-label": this.fmt.longDate(day), tabindex: "-1" });
|
|
3012
|
+
const close = h(this.doc, "button", "tnc-popover-close", { type: "button", "aria-label": "Close" }, "\xD7");
|
|
3013
|
+
close.addEventListener("click", () => this.closeMore(true));
|
|
3014
|
+
const title = this.opts.navLinks ? this.navLink(day, this.fmt.longDate(day), "tnc-popover-title") : h(this.doc, "div", "tnc-popover-title", null, this.fmt.longDate(day));
|
|
3015
|
+
const body = h(this.doc, "div", "tnc-popover-body");
|
|
3016
|
+
el.append(h(this.doc, "div", "tnc-popover-head", null, title, close), body);
|
|
3017
|
+
this.root.append(el);
|
|
3018
|
+
const pool = new ItemPool(this, body);
|
|
3019
|
+
pool.begin();
|
|
3020
|
+
for (const n of items) {
|
|
3021
|
+
pool.place(n.id, { n, day, isStart: n.startDay >= day, isEnd: n.endDay <= day + 1, variant: n.allDay ? "bar" : "dot", mirror: false });
|
|
3022
|
+
}
|
|
3023
|
+
pool.end();
|
|
3024
|
+
this.popover = { el, pool, anchor, day };
|
|
3025
|
+
const rr = this.root.getBoundingClientRect();
|
|
3026
|
+
const ar = anchor.getBoundingClientRect();
|
|
3027
|
+
const w = Math.max(220, ar.width + 24);
|
|
3028
|
+
let left = ar.left - rr.left - 12;
|
|
3029
|
+
left = Math.max(4, Math.min(left, rr.width - w - 4));
|
|
3030
|
+
let top = ar.top - rr.top - 40;
|
|
3031
|
+
const height = el.offsetHeight;
|
|
3032
|
+
top = Math.max(4, Math.min(top, rr.height - height - 4));
|
|
3033
|
+
setStyle(el, { left, top, width: w });
|
|
3034
|
+
el.focus();
|
|
3035
|
+
}
|
|
3036
|
+
/* =============================================================== render */
|
|
3037
|
+
resolveText() {
|
|
3038
|
+
const t = this.opts.text;
|
|
3039
|
+
return {
|
|
3040
|
+
...DEFAULT_TEXT,
|
|
3041
|
+
...t,
|
|
3042
|
+
views: { ...DEFAULT_TEXT.views, ...t.views },
|
|
3043
|
+
viewHints: { ...DEFAULT_TEXT.viewHints, ...t.viewHints }
|
|
3044
|
+
};
|
|
3045
|
+
}
|
|
3046
|
+
todayDay() {
|
|
3047
|
+
return dayOf(this.now(), this.zone);
|
|
3048
|
+
}
|
|
3049
|
+
toDay(date) {
|
|
3050
|
+
if (date === void 0 || date === null) return null;
|
|
3051
|
+
const p = parseDateInput(date, this.zone);
|
|
3052
|
+
if (!p) return null;
|
|
3053
|
+
return p.kind === "day" ? p.day : dayOf(p.ms, this.zone);
|
|
3054
|
+
}
|
|
3055
|
+
toMs(date) {
|
|
3056
|
+
const p = parseDateInput(date, this.zone);
|
|
3057
|
+
if (!p) return null;
|
|
3058
|
+
return p.kind === "day" ? toInstant(p.day, 0, this.zone) : p.ms;
|
|
3059
|
+
}
|
|
3060
|
+
step(dir) {
|
|
3061
|
+
const { view, dayCount, listDayCount } = this.opts;
|
|
3062
|
+
switch (view) {
|
|
3063
|
+
case "day":
|
|
3064
|
+
return this.anchor + dir;
|
|
3065
|
+
case "days":
|
|
3066
|
+
return this.anchor + dir * Math.max(1, dayCount);
|
|
3067
|
+
case "week":
|
|
3068
|
+
return this.anchor + dir * 7;
|
|
3069
|
+
case "list": {
|
|
3070
|
+
const n = Math.max(1, listDayCount);
|
|
3071
|
+
return this.anchor + dir * (this.opts.listStart === "week" ? Math.ceil(n / 7) * 7 : n);
|
|
3072
|
+
}
|
|
3073
|
+
case "month":
|
|
3074
|
+
return addMonths(startOfMonth(this.anchor), dir);
|
|
3075
|
+
case "year":
|
|
3076
|
+
return addMonths(startOfYear(this.anchor), 12 * dir);
|
|
3077
|
+
case "timeline": {
|
|
3078
|
+
const len = this.opts.timeline?.days ?? 120;
|
|
3079
|
+
return this.anchor + dir * Math.max(7, Math.round(len / 3));
|
|
3080
|
+
}
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
computeRange() {
|
|
3084
|
+
const { view, dayCount, listDayCount } = this.opts;
|
|
3085
|
+
const a = this.anchor;
|
|
3086
|
+
switch (view) {
|
|
3087
|
+
case "day":
|
|
3088
|
+
return { startDay: a, endDay: a + 1 };
|
|
3089
|
+
case "days":
|
|
3090
|
+
return { startDay: a, endDay: a + Math.max(1, dayCount) };
|
|
3091
|
+
case "week": {
|
|
3092
|
+
const s = startOfWeek(a, this.firstDay);
|
|
3093
|
+
return { startDay: s, endDay: s + 7 };
|
|
3094
|
+
}
|
|
3095
|
+
case "list": {
|
|
3096
|
+
const s = this.opts.listStart === "week" ? startOfWeek(a, this.firstDay) : a;
|
|
3097
|
+
return { startDay: s, endDay: s + Math.max(1, listDayCount) };
|
|
3098
|
+
}
|
|
3099
|
+
case "month": {
|
|
3100
|
+
const s = startOfWeek(startOfMonth(a), this.firstDay);
|
|
3101
|
+
return { startDay: s, endDay: s + 42 };
|
|
3102
|
+
}
|
|
3103
|
+
case "year": {
|
|
3104
|
+
const s = startOfYear(a);
|
|
3105
|
+
return { startDay: s, endDay: addMonths(s, 12) };
|
|
3106
|
+
}
|
|
3107
|
+
case "timeline": {
|
|
3108
|
+
const lead = this.opts.timeline?.leadDays ?? 14;
|
|
3109
|
+
const len = this.opts.timeline?.days ?? 120;
|
|
3110
|
+
return { startDay: a - lead, endDay: a - lead + len };
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
}
|
|
3114
|
+
computeTitle(startDay, endDay) {
|
|
3115
|
+
const { view } = this.opts;
|
|
3116
|
+
if (view === "month") return this.fmt.monthYear(this.anchor);
|
|
3117
|
+
if (view === "year") return this.fmt.year(this.anchor);
|
|
3118
|
+
if (view === "day") return this.fmt.fullDate(this.anchor);
|
|
3119
|
+
const visible = this.days;
|
|
3120
|
+
const first = visible[0] ?? startDay;
|
|
3121
|
+
const last = visible[visible.length - 1] ?? endDay - 1;
|
|
3122
|
+
return this.fmt.range(first, last);
|
|
3123
|
+
}
|
|
3124
|
+
rebuild(navigated, keep) {
|
|
3125
|
+
const scroll = navigated || keep ? null : this.current?.getScroll();
|
|
3126
|
+
this.closeMore();
|
|
3127
|
+
this.unselectSilently();
|
|
3128
|
+
this.firstDay = this.opts.firstDay ?? localeFirstDay(this.opts.locale);
|
|
3129
|
+
this.today = this.todayDay();
|
|
3130
|
+
const min = parseDurationMinutes(this.opts.slotMinTime, 0);
|
|
3131
|
+
const max = Math.max(min + 30, parseDurationMinutes(this.opts.slotMaxTime, MINUTES_PER_DAY));
|
|
3132
|
+
const duration = Math.max(1, parseDurationMinutes(this.opts.slotDuration, 30));
|
|
3133
|
+
const snap = Math.max(1, parseDurationMinutes(this.opts.snapDuration ?? null, duration));
|
|
3134
|
+
this.slot = { min, max, duration, snap, scroll: parseDurationMinutes(this.opts.scrollTime, 480) };
|
|
3135
|
+
const { startDay, endDay } = this.computeRange();
|
|
3136
|
+
const hidden = new Set(this.opts.hiddenDays);
|
|
3137
|
+
if (!this.opts.weekends) {
|
|
3138
|
+
hidden.add(0);
|
|
3139
|
+
hidden.add(6);
|
|
3140
|
+
}
|
|
3141
|
+
const days = [];
|
|
3142
|
+
const listLike = this.opts.view === "year" || this.opts.view === "timeline" || this.opts.view === "day";
|
|
3143
|
+
for (let d = startDay; d < endDay; d++) if (listLike || !hidden.has(weekdayOf(d))) days.push(d);
|
|
3144
|
+
this.days = days.length ? days : [startDay];
|
|
3145
|
+
const range = {
|
|
3146
|
+
startDay,
|
|
3147
|
+
endDay,
|
|
3148
|
+
start: new Date(toInstant(startDay, 0, this.zone)),
|
|
3149
|
+
end: new Date(toInstant(endDay, 0, this.zone))
|
|
3150
|
+
};
|
|
3151
|
+
const prevView = this.view;
|
|
3152
|
+
this.view = { type: this.opts.view, range, anchor: this.anchor, title: "" };
|
|
3153
|
+
this.view.title = this.computeTitle(startDay, endDay);
|
|
3154
|
+
this.conflicts = dependencyConflicts(this.store);
|
|
3155
|
+
this.root.className = "tnc";
|
|
3156
|
+
this.root.classList.add(`tnc--${this.opts.view}`, `tnc--height-${this.opts.height}`);
|
|
3157
|
+
if (!this.opts.timeGrid) this.root.classList.add("tnc--no-time-grid");
|
|
3158
|
+
this.renderToolbar();
|
|
3159
|
+
this.current?.destroy();
|
|
3160
|
+
this.current = this.createView();
|
|
3161
|
+
this.viewHost.replaceChildren(this.current.el);
|
|
3162
|
+
this.current.render(null);
|
|
3163
|
+
if (keep && this.current instanceof TimelineView) {
|
|
3164
|
+
this.current.setScroll({ top: keep.top, left: 0 });
|
|
3165
|
+
this.current.placeInstant(keep.ms, keep.vx);
|
|
3166
|
+
} else if (scroll) this.current.setScroll(scroll);
|
|
3167
|
+
else if (this.current instanceof TimeGridView) this.current.scrollToTime(this.slot.scroll);
|
|
3168
|
+
else if (this.current instanceof TimelineView) this.current.scrollToDay(this.anchor);
|
|
3169
|
+
const rangeChanged = !prevView || prevView.type !== this.view.type || prevView.range.startDay !== startDay || prevView.range.endDay !== endDay;
|
|
3170
|
+
if (rangeChanged) this.opts.onRangeChange?.(this.view);
|
|
3171
|
+
}
|
|
3172
|
+
createView() {
|
|
3173
|
+
switch (this.opts.view) {
|
|
3174
|
+
case "month":
|
|
3175
|
+
return new DayGridView(this);
|
|
3176
|
+
case "list":
|
|
3177
|
+
return new ListView(this);
|
|
3178
|
+
case "year":
|
|
3179
|
+
return new YearView(this);
|
|
3180
|
+
case "timeline":
|
|
3181
|
+
return new TimelineView(this);
|
|
3182
|
+
default:
|
|
3183
|
+
return new TimeGridView(this);
|
|
3184
|
+
}
|
|
3185
|
+
}
|
|
3186
|
+
renderToolbar() {
|
|
3187
|
+
const tb = this.opts.toolbar;
|
|
3188
|
+
this.toolbarEl.replaceChildren();
|
|
3189
|
+
this.toolbarEl.hidden = !tb;
|
|
3190
|
+
if (!tb) return;
|
|
3191
|
+
const cfg = tb === true ? {} : tb;
|
|
3192
|
+
const views = cfg.views ?? ["month", "week", "days", "day", "list", "year"];
|
|
3193
|
+
const btn = (label, hint, cls, onClick, pressed) => {
|
|
3194
|
+
const b = h(this.doc, "button", `tnc-btn ${cls}`, { type: "button", title: hint, "aria-label": hint }, label);
|
|
3195
|
+
if (pressed !== void 0) b.setAttribute("aria-pressed", String(pressed));
|
|
3196
|
+
b.addEventListener("click", onClick);
|
|
3197
|
+
return b;
|
|
3198
|
+
};
|
|
3199
|
+
const extras = (cfg.extraButtons ?? []).map((e) => {
|
|
3200
|
+
const b = btn(e.text, e.hint ?? e.text, `tnc-btn-extra tnc-btn-${e.id}`, (ev) => e.onClick(ev));
|
|
3201
|
+
if (e.icon) b.replaceChildren(e.icon());
|
|
3202
|
+
return b;
|
|
3203
|
+
});
|
|
3204
|
+
const nav = h(
|
|
3205
|
+
this.doc,
|
|
3206
|
+
"div",
|
|
3207
|
+
"tnc-toolbar-group tnc-toolbar-nav",
|
|
3208
|
+
null,
|
|
3209
|
+
btn("\u2039", this.text.prev, "tnc-btn-prev", () => this.prev()),
|
|
3210
|
+
btn(this.text.today, this.text.today, "tnc-btn-today", () => this.goToday()),
|
|
3211
|
+
btn("\u203A", this.text.next, "tnc-btn-next", () => this.next())
|
|
3212
|
+
);
|
|
3213
|
+
const title = h(this.doc, "h2", "tnc-title", { "aria-live": "polite" }, this.view.title);
|
|
3214
|
+
const viewGroup = h(this.doc, "div", "tnc-toolbar-group tnc-toolbar-views", { role: "group" });
|
|
3215
|
+
for (const v of views) {
|
|
3216
|
+
const label = v === "days" ? this.text.views.days ?? `${this.opts.dayCount} days` : this.text.views[v] ?? v;
|
|
3217
|
+
viewGroup.append(btn(label, this.text.viewHints[v] ?? label, `tnc-btn-view tnc-btn-view-${v}`, () => this.changeView(v), v === this.opts.view));
|
|
3218
|
+
}
|
|
3219
|
+
const start = h(this.doc, "div", "tnc-toolbar-start");
|
|
3220
|
+
const end = h(this.doc, "div", "tnc-toolbar-end");
|
|
3221
|
+
if (cfg.extraPlacement === "start") start.append(...extras);
|
|
3222
|
+
start.append(nav, title);
|
|
3223
|
+
end.append(viewGroup);
|
|
3224
|
+
if (cfg.extraPlacement !== "start") end.append(...extras);
|
|
3225
|
+
this.toolbarEl.append(start, end);
|
|
3226
|
+
}
|
|
3227
|
+
applyChange(change) {
|
|
3228
|
+
if (!change.added.length && !change.updated.length && !change.removed.length && !change.days.size && !change.spans) return;
|
|
3229
|
+
this.conflicts = dependencyConflicts(this.store);
|
|
3230
|
+
this.pendingChange = this.pendingChange ? this.merge(this.pendingChange, change) : change;
|
|
3231
|
+
if (this.session?.active) {
|
|
3232
|
+
this.flush();
|
|
3233
|
+
return;
|
|
3234
|
+
}
|
|
3235
|
+
if (this.frame === null) this.frame = this.win.requestAnimationFrame(() => this.flush());
|
|
3236
|
+
}
|
|
3237
|
+
/** Renders pending changes now. Called automatically on the next frame. */
|
|
3238
|
+
flush() {
|
|
3239
|
+
if (this.frame !== null) {
|
|
3240
|
+
this.win.cancelAnimationFrame(this.frame);
|
|
3241
|
+
this.frame = null;
|
|
3242
|
+
}
|
|
3243
|
+
const change = this.pendingChange;
|
|
3244
|
+
this.pendingChange = null;
|
|
3245
|
+
if (!change || !this.current) return;
|
|
3246
|
+
this.current.render(change);
|
|
3247
|
+
if (this.selection) this.current.showSelection(this.selection);
|
|
3248
|
+
if (this.popover) this.refreshMore();
|
|
3249
|
+
}
|
|
3250
|
+
merge(a, b) {
|
|
3251
|
+
for (const d of b.days) a.days.add(d);
|
|
3252
|
+
a.added.push(...b.added);
|
|
3253
|
+
a.updated.push(...b.updated);
|
|
3254
|
+
a.removed.push(...b.removed);
|
|
3255
|
+
a.spans ||= b.spans;
|
|
3256
|
+
return a;
|
|
3257
|
+
}
|
|
3258
|
+
scheduleTick() {
|
|
3259
|
+
const delay = 6e4 - this.now() % 6e4 + 50;
|
|
3260
|
+
this.timer = this.win.setTimeout(() => {
|
|
3261
|
+
if (this.destroyed) return;
|
|
3262
|
+
if (this.todayDay() !== this.today) this.rebuild(false);
|
|
3263
|
+
else this.current?.tick?.();
|
|
3264
|
+
this.scheduleTick();
|
|
3265
|
+
}, delay);
|
|
3266
|
+
}
|
|
3267
|
+
/* ========================================================= item content */
|
|
3268
|
+
itemArg(el, meta) {
|
|
3269
|
+
const item = this.store.get(meta.n.id)?.item ?? meta.n.item;
|
|
3270
|
+
const { start, end } = this.timeLabel(meta.n, meta.variant, meta.day);
|
|
3271
|
+
return {
|
|
3272
|
+
item,
|
|
3273
|
+
el,
|
|
3274
|
+
view: this.view,
|
|
3275
|
+
day: meta.day,
|
|
3276
|
+
isStart: meta.isStart,
|
|
3277
|
+
isEnd: meta.isEnd,
|
|
3278
|
+
isMirror: meta.mirror,
|
|
3279
|
+
variant: meta.variant,
|
|
3280
|
+
timeText: meta.n.allDay ? "" : end ? `${start} \u2013 ${end}` : start
|
|
3281
|
+
};
|
|
3282
|
+
}
|
|
3283
|
+
/** Start and (for timed and list items) end labels; the end is hidden on short items by CSS. */
|
|
3284
|
+
timeLabel(n, variant, day) {
|
|
3285
|
+
if (n.allDay) return { start: variant === "list" ? this.text.allDay : "", end: "" };
|
|
3286
|
+
const s = wallTime(n.startMs, this.zone);
|
|
3287
|
+
const start = this.fmt.time(s.minute, true);
|
|
3288
|
+
if (variant === "dot" || variant === "bar" || variant === "indicator") return { start: s.day === day ? start : "", end: "" };
|
|
3289
|
+
if (n.startMs === n.endMs) return { start, end: "" };
|
|
3290
|
+
return { start, end: this.fmt.time(wallTime(n.endMs, this.zone).minute, true) };
|
|
3291
|
+
}
|
|
3292
|
+
fillTime(el, n, variant, day) {
|
|
3293
|
+
const { start, end } = this.timeLabel(n, variant, day);
|
|
3294
|
+
el.textContent = start;
|
|
3295
|
+
if (end) el.append(h(this.doc, "span", "tnc-item-time-end", null, ` \u2013 ${end}`));
|
|
3296
|
+
return start !== "";
|
|
3297
|
+
}
|
|
3298
|
+
ariaLabel(n) {
|
|
3299
|
+
if (n.allDay) {
|
|
3300
|
+
const last = n.endDay - 1;
|
|
3301
|
+
return `${n.item.title}, ${last > n.startDay ? this.fmt.range(n.startDay, last) : this.fmt.longDate(n.startDay)}`;
|
|
3302
|
+
}
|
|
3303
|
+
const s = wallTime(n.startMs, this.zone);
|
|
3304
|
+
const e = wallTime(n.endMs, this.zone);
|
|
3305
|
+
return `${n.item.title}, ${this.fmt.longDate(s.day)} ${this.fmt.time(s.minute)} \u2013 ${this.fmt.time(e.minute)}`;
|
|
3306
|
+
}
|
|
3307
|
+
defaultContent(meta) {
|
|
3308
|
+
const { n, variant, day } = meta;
|
|
3309
|
+
const doc = this.doc;
|
|
3310
|
+
const main = h(doc, "div", "tnc-item-main");
|
|
3311
|
+
const title = h(doc, "span", "tnc-item-title", null, n.item.title);
|
|
3312
|
+
const time = h(doc, "span", "tnc-item-time");
|
|
3313
|
+
const label = this.fillTime(time, n, variant, day);
|
|
3314
|
+
switch (variant) {
|
|
3315
|
+
case "timed":
|
|
3316
|
+
main.append(title, time);
|
|
3317
|
+
break;
|
|
3318
|
+
case "dot":
|
|
3319
|
+
main.append(h(doc, "span", "tnc-item-dot", { "aria-hidden": "true" }), time, title);
|
|
3320
|
+
break;
|
|
3321
|
+
case "list":
|
|
3322
|
+
main.append(time, h(doc, "span", "tnc-item-dot", { "aria-hidden": "true" }), title);
|
|
3323
|
+
break;
|
|
3324
|
+
default:
|
|
3325
|
+
if (label) main.append(time);
|
|
3326
|
+
main.append(title);
|
|
3327
|
+
}
|
|
3328
|
+
return main;
|
|
3329
|
+
}
|
|
3330
|
+
addHandles(el, meta) {
|
|
3331
|
+
const { n, variant } = meta;
|
|
3332
|
+
const ports = variant === "timeline" && !meta.mirror && n.id !== EXTERNAL_ID && !!this.opts.timeline?.onLink;
|
|
3333
|
+
if (variant === "dot" || !this.isEditable(n, "duration")) {
|
|
3334
|
+
if (ports) this.addPorts(el, n);
|
|
3335
|
+
return;
|
|
3336
|
+
}
|
|
3337
|
+
const vertical = variant === "timed";
|
|
3338
|
+
const milestone = variant === "timeline" && n.item.kind === "milestone";
|
|
3339
|
+
const handleLabel = variant === "timeline" ? this.opts.timeline?.resizeHandleLabel : void 0;
|
|
3340
|
+
const handle = (edge) => {
|
|
3341
|
+
const label = handleLabel?.({ item: n.item, edge: edge === "start" ? "start" : "finish" });
|
|
3342
|
+
const attrs = label ? { role: "button", tabindex: "0", "aria-label": label } : { "aria-hidden": "true" };
|
|
3343
|
+
el.append(h(this.doc, "div", `tnc-resize tnc-resize-${edge} ${vertical ? "is-v" : "is-h"}`, attrs));
|
|
3344
|
+
};
|
|
3345
|
+
if (meta.isStart && (this.opts.resizableFromStart || variant === "timeline") && !milestone) handle("start");
|
|
3346
|
+
if (meta.isEnd && !milestone) handle("end");
|
|
3347
|
+
if (ports) this.addPorts(el, n);
|
|
3348
|
+
}
|
|
3349
|
+
/** Start and finish link handles (a milestone has one). */
|
|
3350
|
+
addPorts(el, n) {
|
|
3351
|
+
const edges = n.item.kind === "milestone" ? ["finish"] : ["start", "finish"];
|
|
3352
|
+
for (const edge of edges) {
|
|
3353
|
+
el.append(h(this.doc, "span", `tnc-port tnc-port-${edge}`, { "aria-hidden": "true", "data-edge": edge, title: "Drag to link" }));
|
|
3354
|
+
}
|
|
3355
|
+
}
|
|
3356
|
+
/* ============================================================== popover */
|
|
3357
|
+
refreshMore() {
|
|
3358
|
+
const p = this.popover;
|
|
3359
|
+
if (!p) return;
|
|
3360
|
+
const items = this.store.inRange(p.day, p.day + 1).filter((n) => n.item.display !== "background").sort((a, b) => Number(b.allDay) - Number(a.allDay) || this.order(a, b));
|
|
3361
|
+
p.pool.begin();
|
|
3362
|
+
for (const n of items) {
|
|
3363
|
+
p.pool.place(n.id, { n, day: p.day, isStart: n.startDay >= p.day, isEnd: n.endDay <= p.day + 1, variant: n.allDay ? "bar" : "dot", mirror: false });
|
|
3364
|
+
}
|
|
3365
|
+
p.pool.end();
|
|
3366
|
+
}
|
|
3367
|
+
closeMore(restoreFocus = false) {
|
|
3368
|
+
const p = this.popover;
|
|
3369
|
+
if (!p) return;
|
|
3370
|
+
this.popover = null;
|
|
3371
|
+
p.pool.clear();
|
|
3372
|
+
p.el.remove();
|
|
3373
|
+
if (restoreFocus && p.anchor.isConnected) p.anchor.focus();
|
|
3374
|
+
}
|
|
3375
|
+
/* =========================================================== interaction */
|
|
3376
|
+
bindEvents() {
|
|
3377
|
+
const r = this.root;
|
|
3378
|
+
r.addEventListener("pointerdown", this.onPointerDown);
|
|
3379
|
+
r.addEventListener("click", this.onClickCapture, true);
|
|
3380
|
+
r.addEventListener("click", this.onClick);
|
|
3381
|
+
r.addEventListener("dblclick", this.onDblClick);
|
|
3382
|
+
r.addEventListener("contextmenu", this.onContextMenu);
|
|
3383
|
+
r.addEventListener("mouseover", this.onMouseOver);
|
|
3384
|
+
r.addEventListener("mouseout", this.onMouseOut);
|
|
3385
|
+
r.addEventListener("keydown", this.onKeyDown);
|
|
3386
|
+
r.addEventListener("dragover", this.onNativeDragOver);
|
|
3387
|
+
r.addEventListener("dragleave", this.onNativeDragLeave);
|
|
3388
|
+
r.addEventListener("drop", this.onNativeDrop);
|
|
3389
|
+
r.addEventListener("touchmove", this.onTouchMove, { passive: false });
|
|
3390
|
+
this.doc.addEventListener("pointerdown", this.onDocPointerDown, true);
|
|
3391
|
+
}
|
|
3392
|
+
unbindEvents() {
|
|
3393
|
+
const r = this.root;
|
|
3394
|
+
r.removeEventListener("pointerdown", this.onPointerDown);
|
|
3395
|
+
r.removeEventListener("click", this.onClickCapture, true);
|
|
3396
|
+
r.removeEventListener("click", this.onClick);
|
|
3397
|
+
r.removeEventListener("dblclick", this.onDblClick);
|
|
3398
|
+
r.removeEventListener("contextmenu", this.onContextMenu);
|
|
3399
|
+
r.removeEventListener("mouseover", this.onMouseOver);
|
|
3400
|
+
r.removeEventListener("mouseout", this.onMouseOut);
|
|
3401
|
+
r.removeEventListener("keydown", this.onKeyDown);
|
|
3402
|
+
r.removeEventListener("dragover", this.onNativeDragOver);
|
|
3403
|
+
r.removeEventListener("dragleave", this.onNativeDragLeave);
|
|
3404
|
+
r.removeEventListener("drop", this.onNativeDrop);
|
|
3405
|
+
r.removeEventListener("touchmove", this.onTouchMove);
|
|
3406
|
+
this.doc.removeEventListener("pointerdown", this.onDocPointerDown, true);
|
|
3407
|
+
this.listenDoc(false);
|
|
3408
|
+
}
|
|
3409
|
+
listenDoc(on) {
|
|
3410
|
+
const d = this.doc;
|
|
3411
|
+
const fn = on ? d.addEventListener.bind(d) : d.removeEventListener.bind(d);
|
|
3412
|
+
fn("pointermove", this.onPointerMove);
|
|
3413
|
+
fn("pointerup", this.onPointerUp);
|
|
3414
|
+
fn("pointercancel", this.onPointerCancel);
|
|
3415
|
+
fn("keydown", this.onDocKeyDown, true);
|
|
3416
|
+
}
|
|
3417
|
+
itemElFrom(target) {
|
|
3418
|
+
const el = target?.closest?.(".tnc-item");
|
|
3419
|
+
return el && this.root.contains(el) && this.meta.has(el) ? el : null;
|
|
3420
|
+
}
|
|
3421
|
+
onDocPointerDown = (ev) => {
|
|
3422
|
+
const t = ev.target;
|
|
3423
|
+
if (this.popover && !this.popover.el.contains(t) && !this.popover.anchor.contains(t)) this.closeMore();
|
|
3424
|
+
if (this.selection && !this.root.contains(t)) this.unselect();
|
|
3425
|
+
};
|
|
3426
|
+
onPointerDown = (ev) => {
|
|
3427
|
+
if (ev.button !== 0 || this.session) return;
|
|
3428
|
+
const target = ev.target;
|
|
3429
|
+
if (target.closest("button, a, input, select, textarea, .tnc-toolbar, .tnc-port, [data-tnc-no-drag]")) {
|
|
3430
|
+
if (!target.closest(".tnc-item")) return;
|
|
3431
|
+
if (target.closest(".tnc-port, [data-tnc-no-drag], input, select, textarea")) return;
|
|
3432
|
+
}
|
|
3433
|
+
const itemEl = this.itemElFrom(target);
|
|
3434
|
+
let mode = null;
|
|
3435
|
+
let origin = null;
|
|
3436
|
+
if (itemEl) {
|
|
3437
|
+
const meta = this.meta.get(itemEl);
|
|
3438
|
+
origin = this.store.get(meta.n.id) ?? null;
|
|
3439
|
+
if (!origin || origin.id === EXTERNAL_ID) return;
|
|
3440
|
+
if (target.closest(".tnc-resize-end")) mode = this.isEditable(origin, "duration") ? "resize-end" : null;
|
|
3441
|
+
else if (target.closest(".tnc-resize-start")) mode = this.isEditable(origin, "duration") ? "resize-start" : null;
|
|
3442
|
+
else mode = this.isEditable(origin, "start") ? "move" : null;
|
|
3443
|
+
if (!mode) return;
|
|
3444
|
+
} else {
|
|
3445
|
+
if (!this.opts.selectable || !this.current) return;
|
|
3446
|
+
if (target.closest(".tnc-popover, .tnc-more")) return;
|
|
3447
|
+
const hit = this.current.hit(ev.clientX, ev.clientY);
|
|
3448
|
+
if (!hit || hit.kind === "timeline" && !hit.overTrack) return;
|
|
3449
|
+
mode = "select";
|
|
3450
|
+
}
|
|
3451
|
+
const touch = ev.pointerType === "touch";
|
|
3452
|
+
const s = {
|
|
3453
|
+
mode,
|
|
3454
|
+
pointerId: ev.pointerId,
|
|
3455
|
+
pointerType: ev.pointerType,
|
|
3456
|
+
x0: ev.clientX,
|
|
3457
|
+
y0: ev.clientY,
|
|
3458
|
+
active: false,
|
|
3459
|
+
origin,
|
|
3460
|
+
grab: this.current?.hit(ev.clientX, ev.clientY) ?? null,
|
|
3461
|
+
proposal: null,
|
|
3462
|
+
denied: false,
|
|
3463
|
+
timer: null,
|
|
3464
|
+
lastEvent: ev
|
|
3465
|
+
};
|
|
3466
|
+
this.session = s;
|
|
3467
|
+
if (touch && itemEl?.classList.contains("is-draggable")) {
|
|
3468
|
+
const el = itemEl.closest(".tnc-tg-scroll, .tnc-dg-scroll, .tnc-tl-scroll, .tnc-list-scroll, .tnc-year-scroll");
|
|
3469
|
+
if (el) s.pan = { el, left: el.scrollLeft, top: el.scrollTop, active: false };
|
|
3470
|
+
}
|
|
3471
|
+
if (touch) {
|
|
3472
|
+
s.timer = this.win.setTimeout(() => {
|
|
3473
|
+
if (this.session !== s) return;
|
|
3474
|
+
this.activate(s);
|
|
3475
|
+
navigator.vibrate?.(10);
|
|
3476
|
+
}, this.opts.longPressDelay);
|
|
3477
|
+
} else if (mode !== "select") {
|
|
3478
|
+
ev.preventDefault();
|
|
3479
|
+
}
|
|
3480
|
+
this.listenDoc(true);
|
|
3481
|
+
};
|
|
3482
|
+
activate(s) {
|
|
3483
|
+
s.active = true;
|
|
3484
|
+
if (s.timer !== null) this.win.clearTimeout(s.timer);
|
|
3485
|
+
s.timer = null;
|
|
3486
|
+
if (s.origin) {
|
|
3487
|
+
this.draggingId = s.origin.id;
|
|
3488
|
+
for (const el of this.elementsFor(s.origin.id)) el.classList.add("is-dragging");
|
|
3489
|
+
}
|
|
3490
|
+
this.closeMore();
|
|
3491
|
+
if (s.mode !== "select") this.unselectSilently();
|
|
3492
|
+
this.root.classList.add("tnc-dragging", `tnc-dragging-${s.mode}`);
|
|
3493
|
+
if (s.lastEvent) this.track(s, s.lastEvent);
|
|
3494
|
+
}
|
|
3495
|
+
onTouchMove = (ev) => {
|
|
3496
|
+
if (this.session?.active) ev.preventDefault();
|
|
3497
|
+
};
|
|
3498
|
+
onPointerMove = (ev) => {
|
|
3499
|
+
const s = this.session;
|
|
3500
|
+
if (!s || ev.pointerId !== s.pointerId) return;
|
|
3501
|
+
s.lastEvent = ev;
|
|
3502
|
+
if (!s.active) {
|
|
3503
|
+
const dist = Math.hypot(ev.clientX - s.x0, ev.clientY - s.y0);
|
|
3504
|
+
if (s.pointerType === "touch") {
|
|
3505
|
+
const pan = s.pan;
|
|
3506
|
+
if (pan && (pan.active || dist > 8)) {
|
|
3507
|
+
if (!pan.active && s.timer !== null) this.win.clearTimeout(s.timer);
|
|
3508
|
+
s.timer = null;
|
|
3509
|
+
pan.active = true;
|
|
3510
|
+
pan.el.scrollLeft = pan.left - (ev.clientX - s.x0);
|
|
3511
|
+
pan.el.scrollTop = pan.top - (ev.clientY - s.y0);
|
|
3512
|
+
} else if (dist > 8) this.endSession(null, true);
|
|
3513
|
+
return;
|
|
3514
|
+
}
|
|
3515
|
+
if (dist < 4) return;
|
|
3516
|
+
this.activate(s);
|
|
3517
|
+
return;
|
|
3518
|
+
}
|
|
3519
|
+
ev.preventDefault();
|
|
3520
|
+
this.track(s, ev);
|
|
3521
|
+
};
|
|
3522
|
+
onPointerUp = (ev) => {
|
|
3523
|
+
const s = this.session;
|
|
3524
|
+
if (!s || ev.pointerId !== s.pointerId) return;
|
|
3525
|
+
if (!s.active) {
|
|
3526
|
+
this.endSession(null, true);
|
|
3527
|
+
if (s.pan?.active) {
|
|
3528
|
+
this.suppressNextClick();
|
|
3529
|
+
return;
|
|
3530
|
+
}
|
|
3531
|
+
if (s.mode === "select" && s.grab) this.clickSlot(s.grab, ev, s.pointerType === "touch");
|
|
3532
|
+
return;
|
|
3533
|
+
}
|
|
3534
|
+
this.suppressNextClick();
|
|
3535
|
+
this.endSession(ev, false);
|
|
3536
|
+
};
|
|
3537
|
+
onPointerCancel = (ev) => {
|
|
3538
|
+
if (this.session && ev.pointerId === this.session.pointerId) this.endSession(null, true);
|
|
3539
|
+
};
|
|
3540
|
+
onDocKeyDown = (ev) => {
|
|
3541
|
+
if (ev.key === "Escape" && this.session) {
|
|
3542
|
+
ev.preventDefault();
|
|
3543
|
+
ev.stopPropagation();
|
|
3544
|
+
this.endSession(null, true);
|
|
3545
|
+
}
|
|
3546
|
+
};
|
|
3547
|
+
/** Proposal for the current pointer position. */
|
|
3548
|
+
track(s, ev) {
|
|
3549
|
+
const hit = this.current?.hit(ev.clientX, ev.clientY) ?? null;
|
|
3550
|
+
if (s.mode === "external") {
|
|
3551
|
+
this.trackExternal(s, hit);
|
|
3552
|
+
return;
|
|
3553
|
+
}
|
|
3554
|
+
if (!s.grab) s.grab = hit;
|
|
3555
|
+
if (!hit || !s.grab) return;
|
|
3556
|
+
if (s.mode === "select") {
|
|
3557
|
+
const sel = this.selectionFor(s.grab, hit);
|
|
3558
|
+
if (!sel) return;
|
|
3559
|
+
this.selection = sel;
|
|
3560
|
+
this.current?.showSelection(sel);
|
|
3561
|
+
return;
|
|
3562
|
+
}
|
|
3563
|
+
const origin = s.origin;
|
|
3564
|
+
const p = s.mode === "move" ? this.moveProposal(origin, s.grab, hit) : this.resizeProposal(origin, hit, s.mode === "resize-start");
|
|
3565
|
+
if (!p) return;
|
|
3566
|
+
const allowed = this.opts.canDrop?.({ item: origin.item, start: new Date(p.startMs), end: new Date(p.endMs), allDay: p.allDay, rowId: p.rowId }) ?? true;
|
|
3567
|
+
s.denied = !allowed;
|
|
3568
|
+
this.root.classList.toggle("tnc-drop-denied", !allowed);
|
|
3569
|
+
if (!allowed || sameProposal(p, s.proposal)) return;
|
|
3570
|
+
s.proposal = p;
|
|
3571
|
+
this.dragRow = p.rowId !== void 0 ? { id: origin.id, rowId: p.rowId } : null;
|
|
3572
|
+
this.applyChange(this.store.upsert([this.withProposal(origin, p)]));
|
|
3573
|
+
}
|
|
3574
|
+
trackExternal(s, raw) {
|
|
3575
|
+
const src = s.source;
|
|
3576
|
+
const hit = raw?.kind === "timeline" && raw.area !== "track" && raw.area !== "header" ? null : raw;
|
|
3577
|
+
s.hit = hit;
|
|
3578
|
+
if (!hit) {
|
|
3579
|
+
if (s.proposal) {
|
|
3580
|
+
s.proposal = null;
|
|
3581
|
+
this.applyChange(this.store.remove([EXTERNAL_ID]));
|
|
3582
|
+
}
|
|
3583
|
+
this.unselectSilently();
|
|
3584
|
+
return;
|
|
3585
|
+
}
|
|
3586
|
+
const dur = src.durationMinutes ?? this.opts.defaultTimedMinutes;
|
|
3587
|
+
let p;
|
|
3588
|
+
if (hit.kind === "time" || hit.kind === "timeline" && this.timelineSnap() < MINUTES_PER_DAY) {
|
|
3589
|
+
const snap = hit.kind === "time" ? this.slot.snap : this.timelineSnap();
|
|
3590
|
+
const startCivil = Math.floor((hit.day * MINUTES_PER_DAY + hit.minute) / snap) * snap;
|
|
3591
|
+
const startMs = this.civilToMs(startCivil);
|
|
3592
|
+
const endMs = this.civilToMs(startCivil + dur);
|
|
3593
|
+
p = { startMs, endMs, allDay: false, startDay: dayOf(startMs, this.zone), endDay: dayOf(endMs - 1, this.zone) + 1 };
|
|
3594
|
+
} else {
|
|
3595
|
+
const day = hit.day;
|
|
3596
|
+
p = this.dayProposal(day, day + 1);
|
|
3597
|
+
}
|
|
3598
|
+
if (hit.kind === "timeline" && hit.overTrack && hit.rowId !== null) p.rowId = hit.rowId;
|
|
3599
|
+
const allowed = this.opts.canDrop?.({ item: this.externalItem(src, p), start: new Date(p.startMs), end: new Date(p.endMs), allDay: p.allDay, rowId: p.rowId }) ?? true;
|
|
3600
|
+
s.denied = !allowed;
|
|
3601
|
+
this.root.classList.toggle("tnc-drop-denied", !allowed);
|
|
3602
|
+
if (sameProposal(p, s.proposal)) return;
|
|
3603
|
+
s.proposal = p;
|
|
3604
|
+
if (hit.kind === "timeline" && p.rowId === void 0) {
|
|
3605
|
+
this.selection = { ...p };
|
|
3606
|
+
this.current?.showSelection(this.selection);
|
|
3607
|
+
} else this.unselectSilently();
|
|
3608
|
+
this.dragRow = p.rowId !== void 0 ? { id: EXTERNAL_ID, rowId: p.rowId } : null;
|
|
3609
|
+
this.applyChange(this.store.upsert([this.externalItem(src, p)]));
|
|
3610
|
+
}
|
|
3611
|
+
externalItem(src, p) {
|
|
3612
|
+
return {
|
|
3613
|
+
id: EXTERNAL_ID,
|
|
3614
|
+
title: src.title,
|
|
3615
|
+
kind: src.kind,
|
|
3616
|
+
color: src.color,
|
|
3617
|
+
start: p.allDay ? formatDay(p.startDay) : p.startMs,
|
|
3618
|
+
end: p.allDay ? formatDay(p.endDay) : p.endMs,
|
|
3619
|
+
allDay: p.allDay,
|
|
3620
|
+
editable: false,
|
|
3621
|
+
data: src.data,
|
|
3622
|
+
rev: `${p.startMs}:${p.endMs}:${p.allDay ? 1 : 0}:${p.rowId ?? ""}`
|
|
3623
|
+
};
|
|
3624
|
+
}
|
|
3625
|
+
timelineSnap() {
|
|
3626
|
+
return this.opts.timeline?.scale?.snapMinutes ?? MINUTES_PER_DAY;
|
|
3627
|
+
}
|
|
3628
|
+
civil(ms) {
|
|
3629
|
+
const w = wallTime(ms, this.zone);
|
|
3630
|
+
return w.day * MINUTES_PER_DAY + w.minute;
|
|
3631
|
+
}
|
|
3632
|
+
civilToMs(c) {
|
|
3633
|
+
const day = Math.floor(c / MINUTES_PER_DAY);
|
|
3634
|
+
return toInstant(day, c - day * MINUTES_PER_DAY, this.zone);
|
|
3635
|
+
}
|
|
3636
|
+
hitCivil(hit) {
|
|
3637
|
+
return hit.kind === "time" || hit.kind === "timeline" ? hit.day * MINUTES_PER_DAY + hit.minute : hit.day * MINUTES_PER_DAY;
|
|
3638
|
+
}
|
|
3639
|
+
dayProposal(startDay, endDay) {
|
|
3640
|
+
return {
|
|
3641
|
+
startMs: toInstant(startDay, 0, this.zone),
|
|
3642
|
+
endMs: toInstant(endDay, 0, this.zone),
|
|
3643
|
+
allDay: true,
|
|
3644
|
+
startDay,
|
|
3645
|
+
endDay
|
|
3646
|
+
};
|
|
3647
|
+
}
|
|
3648
|
+
timedProposal(startMs, endMs) {
|
|
3649
|
+
return { startMs, endMs, allDay: false, startDay: dayOf(startMs, this.zone), endDay: dayOf(Math.max(startMs, endMs - 1), this.zone) + 1 };
|
|
3650
|
+
}
|
|
3651
|
+
moveProposal(o, grab, hit) {
|
|
3652
|
+
const days = o.endDay - o.startDay;
|
|
3653
|
+
if (hit.kind === "timeline") {
|
|
3654
|
+
const snap2 = o.allDay ? Math.max(MINUTES_PER_DAY, this.timelineSnap()) : this.timelineSnap();
|
|
3655
|
+
const delta2 = Math.round((this.hitCivil(hit) - this.hitCivil(grab)) / snap2) * snap2;
|
|
3656
|
+
let p;
|
|
3657
|
+
if (o.allDay) {
|
|
3658
|
+
const dd = Math.round(delta2 / MINUTES_PER_DAY);
|
|
3659
|
+
p = this.dayProposal(o.startDay + dd, o.endDay + dd);
|
|
3660
|
+
} else {
|
|
3661
|
+
const startMs2 = this.civilToMs(this.civil(o.startMs) + delta2);
|
|
3662
|
+
p = this.timedProposal(startMs2, startMs2 + (o.endMs - o.startMs));
|
|
3663
|
+
}
|
|
3664
|
+
p.rowId = (hit.overTrack ? hit.rowId : null) ?? this.dragRow?.rowId ?? this.opts.timeline?.rowOf(o.item) ?? void 0;
|
|
3665
|
+
return p;
|
|
3666
|
+
}
|
|
3667
|
+
if (hit.kind === "allDay") {
|
|
3668
|
+
if (o.allDay) {
|
|
3669
|
+
const dd = hit.day - (grab.kind === "allDay" || grab.kind === "day" ? grab.day : hit.day);
|
|
3670
|
+
return this.dayProposal(o.startDay + dd, o.endDay + dd);
|
|
3671
|
+
}
|
|
3672
|
+
return this.dayProposal(hit.day, hit.day + this.opts.defaultAllDayDays);
|
|
3673
|
+
}
|
|
3674
|
+
if (hit.kind === "day") {
|
|
3675
|
+
const dd = hit.day - (grab.kind === "day" ? grab.day : hit.day);
|
|
3676
|
+
if (o.allDay) return this.dayProposal(o.startDay + dd, o.startDay + dd + days);
|
|
3677
|
+
const startMs2 = this.civilToMs(this.civil(o.startMs) + dd * MINUTES_PER_DAY);
|
|
3678
|
+
const endMs = this.civilToMs(this.civil(o.endMs) + dd * MINUTES_PER_DAY);
|
|
3679
|
+
return this.timedProposal(startMs2, endMs);
|
|
3680
|
+
}
|
|
3681
|
+
const snap = this.slot.snap;
|
|
3682
|
+
if (o.allDay || grab.kind !== "time") {
|
|
3683
|
+
const c = Math.floor((hit.day * MINUTES_PER_DAY + hit.minute) / snap) * snap;
|
|
3684
|
+
const startMs2 = this.civilToMs(c);
|
|
3685
|
+
return this.timedProposal(startMs2, this.civilToMs(c + this.opts.defaultTimedMinutes));
|
|
3686
|
+
}
|
|
3687
|
+
const delta = Math.round((this.hitCivil(hit) - this.hitCivil(grab)) / snap) * snap;
|
|
3688
|
+
const startCivil = this.civil(o.startMs) + delta;
|
|
3689
|
+
const startMs = this.civilToMs(startCivil);
|
|
3690
|
+
return this.timedProposal(startMs, startMs + (o.endMs - o.startMs));
|
|
3691
|
+
}
|
|
3692
|
+
resizeProposal(o, hit, fromStart) {
|
|
3693
|
+
if (o.allDay || hit.kind === "allDay" || hit.kind === "day") {
|
|
3694
|
+
if (hit.kind === "time") return null;
|
|
3695
|
+
const pointerDay = hit.kind === "timeline" ? Math.round(this.hitCivil(hit) / MINUTES_PER_DAY) : fromStart ? hit.day : hit.day + 1;
|
|
3696
|
+
if (!o.allDay) {
|
|
3697
|
+
const end = wallTime(o.endMs - 1, this.zone);
|
|
3698
|
+
const dd = (fromStart ? 0 : pointerDay - 1) - end.day;
|
|
3699
|
+
if (fromStart) return null;
|
|
3700
|
+
const endMs = Math.max(o.startMs + 6e4 * this.slot.snap, this.civilToMs(this.civil(o.endMs) + dd * MINUTES_PER_DAY));
|
|
3701
|
+
return this.timedProposal(o.startMs, endMs);
|
|
3702
|
+
}
|
|
3703
|
+
const p2 = fromStart ? this.dayProposal(Math.min(o.endDay - 1, pointerDay), o.endDay) : this.dayProposal(o.startDay, Math.max(o.startDay + 1, pointerDay));
|
|
3704
|
+
if (hit.kind === "timeline") p2.rowId = this.opts.timeline?.rowOf(o.item) ?? void 0;
|
|
3705
|
+
return p2;
|
|
3706
|
+
}
|
|
3707
|
+
const snap = hit.kind === "timeline" ? this.timelineSnap() : this.slot.snap;
|
|
3708
|
+
const c = Math.round(this.hitCivil(hit) / snap) * snap;
|
|
3709
|
+
const ms = this.civilToMs(c);
|
|
3710
|
+
const min = snap * 6e4;
|
|
3711
|
+
const p = fromStart ? this.timedProposal(Math.min(ms, o.endMs - min), o.endMs) : this.timedProposal(o.startMs, Math.max(ms, o.startMs + min));
|
|
3712
|
+
if (hit.kind === "timeline") p.rowId = this.opts.timeline?.rowOf(o.item) ?? void 0;
|
|
3713
|
+
return p;
|
|
3714
|
+
}
|
|
3715
|
+
withProposal(o, p) {
|
|
3716
|
+
return {
|
|
3717
|
+
...o.item,
|
|
3718
|
+
start: p.allDay ? formatDay(p.startDay) : p.startMs,
|
|
3719
|
+
end: p.allDay ? formatDay(p.endDay) : p.endMs,
|
|
3720
|
+
allDay: p.allDay,
|
|
3721
|
+
// Unique per proposal, so the store redraws the item wherever the pointer takes it.
|
|
3722
|
+
rev: `drag:${p.startMs}:${p.endMs}:${p.allDay ? 1 : 0}:${p.rowId ?? ""}:${o.sig}`
|
|
3723
|
+
};
|
|
3724
|
+
}
|
|
3725
|
+
selectionFor(a, b) {
|
|
3726
|
+
if (a.kind !== b.kind) {
|
|
3727
|
+
if (!(a.kind === "allDay" && b.kind === "time") && !(a.kind === "time" && b.kind === "allDay")) return null;
|
|
3728
|
+
b = a.kind === "allDay" ? { kind: "allDay", day: b.day } : { kind: "time", day: b.day, minute: a.minute };
|
|
3729
|
+
}
|
|
3730
|
+
if (a.kind === "time" || a.kind === "timeline" && this.timelineSnap() < MINUTES_PER_DAY) {
|
|
3731
|
+
const snap = a.kind === "time" ? this.slot.snap : this.timelineSnap();
|
|
3732
|
+
const ca = Math.floor(this.hitCivil(a) / snap) * snap;
|
|
3733
|
+
const cb = Math.floor(this.hitCivil(b) / snap) * snap;
|
|
3734
|
+
const startMs = this.civilToMs(Math.min(ca, cb));
|
|
3735
|
+
const endMs = this.civilToMs(Math.max(ca, cb) + snap);
|
|
3736
|
+
const sel2 = { ...this.timedProposal(startMs, endMs) };
|
|
3737
|
+
if (a.kind === "timeline" && a.overTrack && a.rowId !== null) sel2.rowId = a.rowId;
|
|
3738
|
+
return sel2;
|
|
3739
|
+
}
|
|
3740
|
+
const da = a.day;
|
|
3741
|
+
const db = b.day;
|
|
3742
|
+
const sel = this.dayProposal(Math.min(da, db), Math.max(da, db) + 1);
|
|
3743
|
+
if (a.kind === "timeline" && a.overTrack && a.rowId !== null) sel.rowId = a.rowId;
|
|
3744
|
+
return sel;
|
|
3745
|
+
}
|
|
3746
|
+
clickSlot(hit, ev, touch) {
|
|
3747
|
+
if (hit.kind === "timeline" && !hit.overTrack) return;
|
|
3748
|
+
const day = hit.day;
|
|
3749
|
+
if (this.opts.onDateClick) {
|
|
3750
|
+
let minute = null;
|
|
3751
|
+
if (hit.kind === "time") minute = Math.floor(hit.minute / this.slot.duration) * this.slot.duration;
|
|
3752
|
+
else if (hit.kind === "timeline" && this.timelineSnap() < MINUTES_PER_DAY) minute = Math.floor(hit.minute / this.timelineSnap()) * this.timelineSnap();
|
|
3753
|
+
const civil = day * MINUTES_PER_DAY + (minute ?? 0);
|
|
3754
|
+
const at = Math.floor(civil / MINUTES_PER_DAY);
|
|
3755
|
+
const startStr = minute === null ? formatDay(day) : formatFloating(this.civilToMs(civil), this.zone);
|
|
3756
|
+
this.opts.onDateClick({
|
|
3757
|
+
...this.dayArg(minute === null ? day : at),
|
|
3758
|
+
minute: minute === null ? null : civil - at * MINUTES_PER_DAY,
|
|
3759
|
+
allDay: minute === null,
|
|
3760
|
+
startStr,
|
|
3761
|
+
jsEvent: ev
|
|
3762
|
+
});
|
|
3763
|
+
}
|
|
3764
|
+
if (touch || !this.opts.selectable) return;
|
|
3765
|
+
const sel = hit.kind === "time" ? (() => {
|
|
3766
|
+
const c = Math.floor(this.hitCivil(hit) / this.slot.duration) * this.slot.duration;
|
|
3767
|
+
return this.timedProposal(this.civilToMs(c), this.civilToMs(c + this.slot.duration));
|
|
3768
|
+
})() : this.selectionFor(hit, hit);
|
|
3769
|
+
if (!sel) return;
|
|
3770
|
+
this.selection = sel;
|
|
3771
|
+
this.current?.showSelection(sel);
|
|
3772
|
+
this.fireSelect(sel, ev);
|
|
3773
|
+
}
|
|
3774
|
+
fireSelect(sel, ev) {
|
|
3775
|
+
this.opts.onSelect?.({
|
|
3776
|
+
start: new Date(sel.startMs),
|
|
3777
|
+
end: new Date(sel.endMs),
|
|
3778
|
+
allDay: sel.allDay,
|
|
3779
|
+
startStr: sel.allDay ? formatDay(sel.startDay) : formatFloating(sel.startMs, this.zone),
|
|
3780
|
+
endStr: sel.allDay ? formatDay(sel.endDay) : formatFloating(sel.endMs, this.zone),
|
|
3781
|
+
jsEvent: ev,
|
|
3782
|
+
view: this.view,
|
|
3783
|
+
rowId: sel.rowId
|
|
3784
|
+
});
|
|
3785
|
+
}
|
|
3786
|
+
unselectSilently() {
|
|
3787
|
+
this.selection = null;
|
|
3788
|
+
this.current?.showSelection(null);
|
|
3789
|
+
}
|
|
3790
|
+
/** Ends the current pointer session, committing unless `cancel`. */
|
|
3791
|
+
endSession(ev, cancel) {
|
|
3792
|
+
const s = this.session;
|
|
3793
|
+
if (!s) return;
|
|
3794
|
+
this.session = null;
|
|
3795
|
+
if (s.timer !== null) this.win.clearTimeout(s.timer);
|
|
3796
|
+
this.listenDoc(false);
|
|
3797
|
+
this.root.classList.remove(
|
|
3798
|
+
"tnc-dragging",
|
|
3799
|
+
"tnc-drop-denied",
|
|
3800
|
+
"tnc-dragging-move",
|
|
3801
|
+
"tnc-dragging-resize-end",
|
|
3802
|
+
"tnc-dragging-resize-start",
|
|
3803
|
+
"tnc-dragging-select",
|
|
3804
|
+
"tnc-dragging-external"
|
|
3805
|
+
);
|
|
3806
|
+
const draggedId = this.draggingId;
|
|
3807
|
+
this.draggingId = null;
|
|
3808
|
+
this.dragRow = null;
|
|
3809
|
+
if (draggedId) for (const el of this.elementsFor(draggedId)) el.classList.remove("is-dragging");
|
|
3810
|
+
if (!s.active) return;
|
|
3811
|
+
if (s.mode === "select") {
|
|
3812
|
+
if (cancel) this.unselectSilently();
|
|
3813
|
+
else if (this.selection) this.fireSelect(this.selection, ev);
|
|
3814
|
+
return;
|
|
3815
|
+
}
|
|
3816
|
+
if (s.mode === "external") {
|
|
3817
|
+
const p2 = s.proposal;
|
|
3818
|
+
if (p2) this.applyChange(this.store.remove([EXTERNAL_ID]));
|
|
3819
|
+
this.unselectSilently();
|
|
3820
|
+
const dropped = !cancel && !!p2 && !s.denied && !!ev;
|
|
3821
|
+
const hit = s.hit ?? null;
|
|
3822
|
+
const ended = () => {
|
|
3823
|
+
if (s.source) this.opts.onExternalDragEnd?.({ source: s.source, dropped, hit, jsEvent: ev });
|
|
3824
|
+
};
|
|
3825
|
+
if (!dropped || !p2 || !ev) {
|
|
3826
|
+
ended();
|
|
3827
|
+
return;
|
|
3828
|
+
}
|
|
3829
|
+
this.flush();
|
|
3830
|
+
this.opts.onExternalDrop?.({
|
|
3831
|
+
start: new Date(p2.startMs),
|
|
3832
|
+
end: new Date(p2.endMs),
|
|
3833
|
+
allDay: p2.allDay,
|
|
3834
|
+
startStr: p2.allDay ? formatDay(p2.startDay) : formatFloating(p2.startMs, this.zone),
|
|
3835
|
+
endStr: p2.allDay ? formatDay(p2.endDay) : formatFloating(p2.endMs, this.zone),
|
|
3836
|
+
jsEvent: ev,
|
|
3837
|
+
view: this.view,
|
|
3838
|
+
rowId: p2.rowId,
|
|
3839
|
+
source: s.source ?? null,
|
|
3840
|
+
hit,
|
|
3841
|
+
dataTransfer: null
|
|
3842
|
+
});
|
|
3843
|
+
ended();
|
|
3844
|
+
return;
|
|
3845
|
+
}
|
|
3846
|
+
const origin = s.origin;
|
|
3847
|
+
const p = s.proposal;
|
|
3848
|
+
const revert = () => {
|
|
3849
|
+
const cur = this.store.get(origin.id);
|
|
3850
|
+
if (cur && cur.sig !== origin.sig) this.applyChange(this.store.upsert([origin.item]));
|
|
3851
|
+
this.flush();
|
|
3852
|
+
};
|
|
3853
|
+
if (cancel || s.denied || !p) {
|
|
3854
|
+
revert();
|
|
3855
|
+
return;
|
|
3856
|
+
}
|
|
3857
|
+
const changed = p.startMs !== origin.startMs || p.endMs !== origin.endMs || p.allDay !== origin.allDay || p.rowId !== void 0 && p.rowId !== this.opts.timeline?.rowOf(origin.item);
|
|
3858
|
+
if (!changed) {
|
|
3859
|
+
revert();
|
|
3860
|
+
return;
|
|
3861
|
+
}
|
|
3862
|
+
this.flush();
|
|
3863
|
+
this.commit(s.mode === "move" ? "drop" : "resize", origin, p, ev, revert);
|
|
3864
|
+
}
|
|
3865
|
+
commit(kind, origin, p, ev, revert) {
|
|
3866
|
+
const arg = {
|
|
3867
|
+
item: origin.item,
|
|
3868
|
+
start: new Date(p.startMs),
|
|
3869
|
+
end: new Date(p.endMs),
|
|
3870
|
+
allDay: p.allDay,
|
|
3871
|
+
startStr: p.allDay ? formatDay(p.startDay) : formatFloating(p.startMs, this.zone),
|
|
3872
|
+
endStr: p.allDay ? formatDay(p.endDay) : formatFloating(p.endMs, this.zone),
|
|
3873
|
+
oldStart: new Date(origin.startMs),
|
|
3874
|
+
oldEnd: new Date(origin.endMs),
|
|
3875
|
+
oldAllDay: origin.allDay,
|
|
3876
|
+
delta: p.startMs - origin.startMs,
|
|
3877
|
+
rowId: p.rowId,
|
|
3878
|
+
jsEvent: ev,
|
|
3879
|
+
source: ev && "key" in ev ? "keyboard" : "pointer",
|
|
3880
|
+
view: this.view,
|
|
3881
|
+
revert
|
|
3882
|
+
};
|
|
3883
|
+
const handler = kind === "drop" ? this.opts.onItemDrop : this.opts.onItemResize;
|
|
3884
|
+
try {
|
|
3885
|
+
const res = handler?.(arg);
|
|
3886
|
+
if (res && typeof res.then === "function") {
|
|
3887
|
+
Promise.resolve(res).catch(() => revert());
|
|
3888
|
+
}
|
|
3889
|
+
} catch (err) {
|
|
3890
|
+
revert();
|
|
3891
|
+
throw err;
|
|
3892
|
+
}
|
|
3893
|
+
}
|
|
3894
|
+
/* --------------------------------------------------------- click / keys */
|
|
3895
|
+
/** Runs in the capture phase so the click ending a drag never reaches content inside the calendar, React portals included. */
|
|
3896
|
+
onClickCapture = (ev) => {
|
|
3897
|
+
if (!this.suppressClick) return;
|
|
3898
|
+
this.suppressClick = false;
|
|
3899
|
+
ev.stopPropagation();
|
|
3900
|
+
ev.preventDefault();
|
|
3901
|
+
};
|
|
3902
|
+
onClick = (ev) => {
|
|
3903
|
+
const el = this.itemElFrom(ev.target);
|
|
3904
|
+
if (!el || ev.target.closest(".tnc-resize, .tnc-port")) return;
|
|
3905
|
+
const meta = this.meta.get(el);
|
|
3906
|
+
if (meta.n.id === EXTERNAL_ID) return;
|
|
3907
|
+
this.opts.onItemClick?.({ ...this.itemArg(el, meta), jsEvent: ev });
|
|
3908
|
+
};
|
|
3909
|
+
onDblClick = (ev) => {
|
|
3910
|
+
const el = this.itemElFrom(ev.target);
|
|
3911
|
+
if (!el) return;
|
|
3912
|
+
const meta = this.meta.get(el);
|
|
3913
|
+
this.opts.onItemDoubleClick?.({ ...this.itemArg(el, meta), jsEvent: ev });
|
|
3914
|
+
};
|
|
3915
|
+
onContextMenu = (ev) => {
|
|
3916
|
+
const el = this.itemElFrom(ev.target);
|
|
3917
|
+
if (!el) return;
|
|
3918
|
+
if (this.session?.active) {
|
|
3919
|
+
ev.preventDefault();
|
|
3920
|
+
return;
|
|
3921
|
+
}
|
|
3922
|
+
if (!this.opts.onItemContextMenu) return;
|
|
3923
|
+
ev.preventDefault();
|
|
3924
|
+
const meta = this.meta.get(el);
|
|
3925
|
+
this.opts.onItemContextMenu({ ...this.itemArg(el, meta), jsEvent: ev });
|
|
3926
|
+
};
|
|
3927
|
+
hoverId = null;
|
|
3928
|
+
onMouseOver = (ev) => {
|
|
3929
|
+
const el = this.itemElFrom(ev.target);
|
|
3930
|
+
const id = el ? this.meta.get(el).n.id : null;
|
|
3931
|
+
if (id === this.hoverId) return;
|
|
3932
|
+
this.leaveHover(ev);
|
|
3933
|
+
if (!el || !id || this.session?.active) return;
|
|
3934
|
+
this.hoverId = id;
|
|
3935
|
+
for (const other of this.elementsFor(id)) other.classList.add("is-hover");
|
|
3936
|
+
this.opts.onItemHover?.({ ...this.itemArg(el, this.meta.get(el)), jsEvent: ev, entering: true });
|
|
3937
|
+
};
|
|
3938
|
+
onMouseOut = (ev) => {
|
|
3939
|
+
const to = ev.relatedTarget;
|
|
3940
|
+
const toEl = to ? this.itemElFrom(to) : null;
|
|
3941
|
+
if (toEl && this.meta.get(toEl)?.n.id === this.hoverId) return;
|
|
3942
|
+
this.leaveHover(ev);
|
|
3943
|
+
};
|
|
3944
|
+
leaveHover(ev) {
|
|
3945
|
+
const id = this.hoverId;
|
|
3946
|
+
if (!id) return;
|
|
3947
|
+
this.hoverId = null;
|
|
3948
|
+
const els = this.elementsFor(id);
|
|
3949
|
+
for (const el2 of els) el2.classList.remove("is-hover");
|
|
3950
|
+
const el = els[0];
|
|
3951
|
+
const meta = el ? this.meta.get(el) : void 0;
|
|
3952
|
+
if (el && meta) this.opts.onItemHover?.({ ...this.itemArg(el, meta), jsEvent: ev, entering: false });
|
|
3953
|
+
}
|
|
3954
|
+
onKeyDown = (ev) => {
|
|
3955
|
+
if (ev.key === "Escape" && this.popover) {
|
|
3956
|
+
ev.preventDefault();
|
|
3957
|
+
ev.stopPropagation();
|
|
3958
|
+
this.closeMore(true);
|
|
3959
|
+
return;
|
|
3960
|
+
}
|
|
3961
|
+
const target = ev.target;
|
|
3962
|
+
if ((ev.key === "Enter" || ev.key === " ") && target.matches(".tnc-ym-day, .tnc-dg-day[tabindex]")) {
|
|
3963
|
+
const day = parseDay(target.dataset.date ?? "");
|
|
3964
|
+
if (day !== null) {
|
|
3965
|
+
ev.preventDefault();
|
|
3966
|
+
this.clickSlot({ kind: "day", day }, ev, true);
|
|
3967
|
+
}
|
|
3968
|
+
return;
|
|
3969
|
+
}
|
|
3970
|
+
const el = this.itemElFrom(ev.target);
|
|
3971
|
+
if (!el) return;
|
|
3972
|
+
const grip = ev.target.closest?.(".tnc-resize[tabindex]");
|
|
3973
|
+
if (grip && el.contains(grip) && (ev.key === "ArrowLeft" || ev.key === "ArrowRight") && !ev.ctrlKey && !ev.metaKey) {
|
|
3974
|
+
ev.preventDefault();
|
|
3975
|
+
this.nudge(this.meta.get(el).n.id, ev, grip.classList.contains("tnc-resize-start") ? "start" : "end");
|
|
3976
|
+
return;
|
|
3977
|
+
}
|
|
3978
|
+
if (ev.target !== el) return;
|
|
3979
|
+
const meta = this.meta.get(el);
|
|
3980
|
+
if (ev.key === "Enter" || ev.key === " ") {
|
|
3981
|
+
ev.preventDefault();
|
|
3982
|
+
this.opts.onItemClick?.({ ...this.itemArg(el, meta), jsEvent: ev });
|
|
3983
|
+
return;
|
|
3984
|
+
}
|
|
3985
|
+
if (ev.key === "ContextMenu" || ev.key === "F10" && ev.shiftKey) {
|
|
3986
|
+
ev.preventDefault();
|
|
3987
|
+
this.opts.onItemContextMenu?.({ ...this.itemArg(el, meta), jsEvent: ev });
|
|
3988
|
+
return;
|
|
3989
|
+
}
|
|
3990
|
+
if (ev.altKey && ev.key.startsWith("Arrow")) {
|
|
3991
|
+
ev.preventDefault();
|
|
3992
|
+
this.nudge(meta.n.id, ev);
|
|
3993
|
+
}
|
|
3994
|
+
};
|
|
3995
|
+
/** Alt+Arrow moves an item; Alt+Shift+Arrow changes its end; arrows on a focused grip move that edge. */
|
|
3996
|
+
nudge(id, ev, edge = null) {
|
|
3997
|
+
const o = this.store.get(id);
|
|
3998
|
+
if (!o) return;
|
|
3999
|
+
const resize = edge !== null || ev.shiftKey;
|
|
4000
|
+
const fromStart = edge === "start";
|
|
4001
|
+
if (!this.isEditable(o, resize ? "duration" : "start")) return;
|
|
4002
|
+
const view = this.opts.view;
|
|
4003
|
+
const timeStep = view === "timeline" ? this.timelineSnap() : this.slot.snap;
|
|
4004
|
+
let dMin = 0;
|
|
4005
|
+
switch (ev.key) {
|
|
4006
|
+
case "ArrowLeft":
|
|
4007
|
+
dMin = view === "timeline" ? -Math.max(timeStep, o.allDay ? MINUTES_PER_DAY : 0) : -MINUTES_PER_DAY;
|
|
4008
|
+
break;
|
|
4009
|
+
case "ArrowRight":
|
|
4010
|
+
dMin = view === "timeline" ? Math.max(timeStep, o.allDay ? MINUTES_PER_DAY : 0) : MINUTES_PER_DAY;
|
|
4011
|
+
break;
|
|
4012
|
+
case "ArrowUp":
|
|
4013
|
+
dMin = view === "month" ? -7 * MINUTES_PER_DAY : o.allDay ? 0 : -timeStep;
|
|
4014
|
+
break;
|
|
4015
|
+
case "ArrowDown":
|
|
4016
|
+
dMin = view === "month" ? 7 * MINUTES_PER_DAY : o.allDay ? 0 : timeStep;
|
|
4017
|
+
break;
|
|
4018
|
+
}
|
|
4019
|
+
if (!dMin) return;
|
|
4020
|
+
let p;
|
|
4021
|
+
if (o.allDay) {
|
|
4022
|
+
const dd = Math.round(dMin / MINUTES_PER_DAY);
|
|
4023
|
+
if (!dd) return;
|
|
4024
|
+
if (fromStart) p = this.dayProposal(Math.min(o.endDay - 1, o.startDay + dd), o.endDay);
|
|
4025
|
+
else if (resize) p = this.dayProposal(o.startDay, Math.max(o.startDay + 1, o.endDay + dd));
|
|
4026
|
+
else p = this.dayProposal(o.startDay + dd, o.endDay + dd);
|
|
4027
|
+
} else if (fromStart) {
|
|
4028
|
+
p = this.timedProposal(Math.min(o.endMs - timeStep * 6e4, this.civilToMs(this.civil(o.startMs) + dMin)), o.endMs);
|
|
4029
|
+
} else if (resize) {
|
|
4030
|
+
p = this.timedProposal(o.startMs, Math.max(o.startMs + timeStep * 6e4, this.civilToMs(this.civil(o.endMs) + dMin)));
|
|
4031
|
+
} else {
|
|
4032
|
+
const startMs = this.civilToMs(this.civil(o.startMs) + dMin);
|
|
4033
|
+
p = this.timedProposal(startMs, startMs + (o.endMs - o.startMs));
|
|
4034
|
+
}
|
|
4035
|
+
const allowed = this.opts.canDrop?.({ item: o.item, start: new Date(p.startMs), end: new Date(p.endMs), allDay: p.allDay }) ?? true;
|
|
4036
|
+
if (!allowed) return;
|
|
4037
|
+
const revert = () => {
|
|
4038
|
+
this.applyChange(this.store.upsert([o.item]));
|
|
4039
|
+
this.flush();
|
|
4040
|
+
};
|
|
4041
|
+
if (p.startMs === o.startMs && p.endMs === o.endMs) return;
|
|
4042
|
+
this.applyChange(this.store.upsert([this.withProposal(o, p)]));
|
|
4043
|
+
this.flush();
|
|
4044
|
+
const next = this.elementsFor(id)[0];
|
|
4045
|
+
const grip = edge ? next?.querySelector(`.tnc-resize-${edge}[tabindex]`) : null;
|
|
4046
|
+
(grip ?? next)?.focus();
|
|
4047
|
+
this.commit(resize ? "resize" : "drop", o, p, ev, revert);
|
|
4048
|
+
}
|
|
4049
|
+
/* ---------------------------------------------------------- native drop */
|
|
4050
|
+
onNativeDragOver = (ev) => {
|
|
4051
|
+
const dt = ev.dataTransfer;
|
|
4052
|
+
if (!dt || !this.opts.acceptNativeDrop?.(dt) || !this.current) return;
|
|
4053
|
+
ev.preventDefault();
|
|
4054
|
+
dt.dropEffect = "copy";
|
|
4055
|
+
const hit = this.current.hit(ev.clientX, ev.clientY);
|
|
4056
|
+
if (!hit || hit.kind === "timeline" && hit.area !== "track" && hit.area !== "header") return;
|
|
4057
|
+
const sel = this.selectionFor(hit, hit);
|
|
4058
|
+
if (!sel) return;
|
|
4059
|
+
this.selection = sel;
|
|
4060
|
+
this.current.showSelection(sel);
|
|
4061
|
+
};
|
|
4062
|
+
onNativeDragLeave = (ev) => {
|
|
4063
|
+
if (ev.relatedTarget && this.root.contains(ev.relatedTarget)) return;
|
|
4064
|
+
this.unselectSilently();
|
|
4065
|
+
};
|
|
4066
|
+
onNativeDrop = (ev) => {
|
|
4067
|
+
const dt = ev.dataTransfer;
|
|
4068
|
+
if (!dt || !this.opts.acceptNativeDrop?.(dt) || !this.current) return;
|
|
4069
|
+
ev.preventDefault();
|
|
4070
|
+
const hit = this.current.hit(ev.clientX, ev.clientY);
|
|
4071
|
+
this.unselectSilently();
|
|
4072
|
+
if (!hit || hit.kind === "timeline" && hit.area !== "track" && hit.area !== "header") return;
|
|
4073
|
+
const sel = this.selectionFor(hit, hit);
|
|
4074
|
+
if (!sel) return;
|
|
4075
|
+
this.opts.onExternalDrop?.({
|
|
4076
|
+
start: new Date(sel.startMs),
|
|
4077
|
+
end: new Date(sel.endMs),
|
|
4078
|
+
allDay: sel.allDay,
|
|
4079
|
+
startStr: sel.allDay ? formatDay(sel.startDay) : formatFloating(sel.startMs, this.zone),
|
|
4080
|
+
endStr: sel.allDay ? formatDay(sel.endDay) : formatFloating(sel.endMs, this.zone),
|
|
4081
|
+
jsEvent: null,
|
|
4082
|
+
view: this.view,
|
|
4083
|
+
rowId: sel.rowId,
|
|
4084
|
+
source: null,
|
|
4085
|
+
hit,
|
|
4086
|
+
dataTransfer: dt
|
|
4087
|
+
});
|
|
4088
|
+
};
|
|
4089
|
+
};
|
|
4090
|
+
|
|
4091
|
+
// src/tasknotes/bases.ts
|
|
4092
|
+
var FROM_BASES = {
|
|
4093
|
+
dayGridMonth: "month",
|
|
4094
|
+
timeGridWeek: "week",
|
|
4095
|
+
timeGridCustom: "days",
|
|
4096
|
+
timeGridDay: "day",
|
|
4097
|
+
listWeek: "list",
|
|
4098
|
+
multiMonthYear: "year"
|
|
4099
|
+
};
|
|
4100
|
+
var TO_BASES = {
|
|
4101
|
+
month: "dayGridMonth",
|
|
4102
|
+
week: "timeGridWeek",
|
|
4103
|
+
days: "timeGridCustom",
|
|
4104
|
+
day: "timeGridDay",
|
|
4105
|
+
list: "listWeek",
|
|
4106
|
+
year: "multiMonthYear"
|
|
4107
|
+
};
|
|
4108
|
+
function viewFromBases(name, fallback = "month") {
|
|
4109
|
+
return name && FROM_BASES[name] || fallback;
|
|
4110
|
+
}
|
|
4111
|
+
function viewToBases(view) {
|
|
4112
|
+
return TO_BASES[view] ?? null;
|
|
4113
|
+
}
|
|
4114
|
+
var BASES_CALENDAR_DEFAULTS = {
|
|
4115
|
+
calendarView: "dayGridMonth",
|
|
4116
|
+
customDayCount: 3,
|
|
4117
|
+
listDayCount: 7,
|
|
4118
|
+
heightMode: "fill",
|
|
4119
|
+
firstDay: 1,
|
|
4120
|
+
timeFormat: "24",
|
|
4121
|
+
weekNumbers: false,
|
|
4122
|
+
showWeekends: true,
|
|
4123
|
+
showAllDaySlot: true,
|
|
4124
|
+
showTodayHighlight: true,
|
|
4125
|
+
todayColumnWidthMultiplier: 1,
|
|
4126
|
+
slotMinTime: "00:00:00",
|
|
4127
|
+
slotMaxTime: "24:00:00",
|
|
4128
|
+
slotDuration: "00:30:00",
|
|
4129
|
+
snapDuration: "00:30:00",
|
|
4130
|
+
scrollTime: "08:00:00",
|
|
4131
|
+
nowIndicator: true,
|
|
4132
|
+
showTimeGrid: true,
|
|
4133
|
+
selectMirror: true,
|
|
4134
|
+
slotEventOverlap: true,
|
|
4135
|
+
eventMinHeight: 15,
|
|
4136
|
+
eventMaxStack: 0,
|
|
4137
|
+
dayMaxEvents: 0,
|
|
4138
|
+
dayMaxEventRows: 0,
|
|
4139
|
+
locale: ""
|
|
4140
|
+
};
|
|
4141
|
+
var num = (v, fallback) => {
|
|
4142
|
+
const n = typeof v === "string" ? Number(v) : v;
|
|
4143
|
+
return typeof n === "number" && Number.isFinite(n) ? n : fallback;
|
|
4144
|
+
};
|
|
4145
|
+
var clamp = (n, lo, hi) => Math.min(hi, Math.max(lo, n));
|
|
4146
|
+
function isValidSlotTime(value, maxHours = 23) {
|
|
4147
|
+
const m = /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/.exec(value.trim());
|
|
4148
|
+
if (!m) return false;
|
|
4149
|
+
const hh = Number(m[1]);
|
|
4150
|
+
const mm = Number(m[2]);
|
|
4151
|
+
const ss = Number(m[3] ?? 0);
|
|
4152
|
+
if (mm > 59 || ss > 59 || hh > maxHours) return false;
|
|
4153
|
+
if (hh === 48 && (mm || ss)) return false;
|
|
4154
|
+
return true;
|
|
4155
|
+
}
|
|
4156
|
+
function basesLayoutOptions(s, context = {}) {
|
|
4157
|
+
const d = BASES_CALENDAR_DEFAULTS;
|
|
4158
|
+
const pick = (k) => s[k] ?? d[k];
|
|
4159
|
+
const view = viewFromBases(pick("calendarView"));
|
|
4160
|
+
const showTimeGrid = pick("showTimeGrid") !== false;
|
|
4161
|
+
const time = (k, max = 23) => {
|
|
4162
|
+
const v = s[k];
|
|
4163
|
+
return typeof v === "string" && isValidSlotTime(v, max) ? v : void 0;
|
|
4164
|
+
};
|
|
4165
|
+
let today = num(s.todayColumnWidthMultiplier, 1);
|
|
4166
|
+
today = today < 1 || today > 5 ? 1 : Math.round(today * 2) / 2;
|
|
4167
|
+
const rawMax = s.dayMaxEvents ?? d.dayMaxEvents;
|
|
4168
|
+
let dayMaxEvents = typeof rawMax === "boolean" ? rawMax : num(rawMax, 0) === 0 ? true : clamp(num(rawMax, 0), 1, 20);
|
|
4169
|
+
if (!showTimeGrid && dayMaxEvents === true && view !== "month") dayMaxEvents = false;
|
|
4170
|
+
const rawRows = s.dayMaxEventRows ?? d.dayMaxEventRows;
|
|
4171
|
+
const dayMaxEventRows = typeof rawRows === "boolean" ? 0 : clamp(num(rawRows, 0), 0, 10);
|
|
4172
|
+
const slotDuration = time("slotDuration") ?? d.slotDuration;
|
|
4173
|
+
const locale = (s.locale ?? "").trim();
|
|
4174
|
+
const out = {
|
|
4175
|
+
view,
|
|
4176
|
+
dayCount: clamp(Math.round(num(s.customDayCount, d.customDayCount)), 1, 14),
|
|
4177
|
+
listDayCount: clamp(Math.round(num(s.listDayCount, d.listDayCount)), 1, 365),
|
|
4178
|
+
// The plugin overrode FullCalendar's listWeek with `duration: { days: listDayCount }`,
|
|
4179
|
+
// which aligns to the day (not the week), so saved list views start at the date.
|
|
4180
|
+
listStart: "date",
|
|
4181
|
+
height: context.embedded || s.heightMode === "auto" ? "auto" : "fill",
|
|
4182
|
+
expandRows: !(context.embedded || s.heightMode === "auto"),
|
|
4183
|
+
firstDay: clamp(Math.round(num(s.firstDay, 1)), 0, 6),
|
|
4184
|
+
hour12: (s.timeFormat ?? d.timeFormat) === "12",
|
|
4185
|
+
weekNumbers: pick("weekNumbers") === true,
|
|
4186
|
+
weekends: pick("showWeekends") !== false,
|
|
4187
|
+
allDaySlot: pick("showAllDaySlot") !== false,
|
|
4188
|
+
todayHighlight: pick("showTodayHighlight") !== false,
|
|
4189
|
+
todayWidth: view === "week" || view === "days" ? today : 1,
|
|
4190
|
+
slotMinTime: time("slotMinTime") ?? d.slotMinTime,
|
|
4191
|
+
slotMaxTime: time("slotMaxTime", 48) ?? d.slotMaxTime,
|
|
4192
|
+
slotDuration,
|
|
4193
|
+
snapDuration: time("snapDuration") ?? slotDuration,
|
|
4194
|
+
scrollTime: time("scrollTime", 48) ?? d.scrollTime,
|
|
4195
|
+
nowIndicator: pick("nowIndicator") !== false,
|
|
4196
|
+
timeGrid: showTimeGrid,
|
|
4197
|
+
selectMirror: pick("selectMirror") !== false,
|
|
4198
|
+
slotEventOverlap: pick("slotEventOverlap") !== false,
|
|
4199
|
+
eventMinHeight: clamp(num(s.eventMinHeight, d.eventMinHeight), 15, 100),
|
|
4200
|
+
eventMaxStack: clamp(Math.round(num(s.eventMaxStack, 0)), 0, 10),
|
|
4201
|
+
dayMaxEvents,
|
|
4202
|
+
dayMaxEventRows
|
|
4203
|
+
};
|
|
4204
|
+
if (locale) out.locale = locale;
|
|
4205
|
+
return out;
|
|
4206
|
+
}
|