| 1 |
/*! |
| 2 |
FullCalendar Luxon 3 Plugin v6.1.11 |
| 3 |
Docs & License: https://fullcalendar.io/docs/luxon |
| 4 |
(c) 2023 Adam Shaw |
| 5 |
*/ |
| 6 |
FullCalendar.Luxon3 = (function (exports, core, luxon, internal) { |
| 7 |
'use strict'; |
| 8 |
|
| 9 |
function toLuxonDateTime(date, calendar) { |
| 10 |
if (!(calendar instanceof internal.CalendarImpl)) { |
| 11 |
throw new Error('must supply a CalendarApi instance'); |
| 12 |
} |
| 13 |
let { dateEnv } = calendar.getCurrentData(); |
| 14 |
return luxon.DateTime.fromJSDate(date, { |
| 15 |
zone: dateEnv.timeZone, |
| 16 |
locale: dateEnv.locale.codes[0], |
| 17 |
}); |
| 18 |
} |
| 19 |
function toLuxonDuration(duration, calendar) { |
| 20 |
if (!(calendar instanceof internal.CalendarImpl)) { |
| 21 |
throw new Error('must supply a CalendarApi instance'); |
| 22 |
} |
| 23 |
let { dateEnv } = calendar.getCurrentData(); |
| 24 |
return luxon.Duration.fromObject(duration, { |
| 25 |
locale: dateEnv.locale.codes[0], |
| 26 |
}); |
| 27 |
} |
| 28 |
// Internal Utils |
| 29 |
function luxonToArray(datetime) { |
| 30 |
return [ |
| 31 |
datetime.year, |
| 32 |
datetime.month - 1, |
| 33 |
datetime.day, |
| 34 |
datetime.hour, |
| 35 |
datetime.minute, |
| 36 |
datetime.second, |
| 37 |
datetime.millisecond, |
| 38 |
]; |
| 39 |
} |
| 40 |
function arrayToLuxon(arr, timeZone, locale) { |
| 41 |
return luxon.DateTime.fromObject({ |
| 42 |
year: arr[0], |
| 43 |
month: arr[1] + 1, |
| 44 |
day: arr[2], |
| 45 |
hour: arr[3], |
| 46 |
minute: arr[4], |
| 47 |
second: arr[5], |
| 48 |
millisecond: arr[6], |
| 49 |
}, { |
| 50 |
locale, |
| 51 |
zone: timeZone, |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
class LuxonNamedTimeZone extends internal.NamedTimeZoneImpl { |
| 56 |
offsetForArray(a) { |
| 57 |
return arrayToLuxon(a, this.timeZoneName).offset; |
| 58 |
} |
| 59 |
timestampToArray(ms) { |
| 60 |
return luxonToArray(luxon.DateTime.fromMillis(ms, { |
| 61 |
zone: this.timeZoneName, |
| 62 |
})); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
function formatWithCmdStr(cmdStr, arg) { |
| 67 |
let cmd = parseCmdStr(cmdStr); |
| 68 |
if (arg.end) { |
| 69 |
let start = arrayToLuxon(arg.start.array, arg.timeZone, arg.localeCodes[0]); |
| 70 |
let end = arrayToLuxon(arg.end.array, arg.timeZone, arg.localeCodes[0]); |
| 71 |
return formatRange(cmd, start.toFormat.bind(start), end.toFormat.bind(end), arg.defaultSeparator); |
| 72 |
} |
| 73 |
return arrayToLuxon(arg.date.array, arg.timeZone, arg.localeCodes[0]).toFormat(cmd.whole); |
| 74 |
} |
| 75 |
function parseCmdStr(cmdStr) { |
| 76 |
let parts = cmdStr.match(/^(.*?)\{(.*)\}(.*)$/); // TODO: lookbehinds for escape characters |
| 77 |
if (parts) { |
| 78 |
let middle = parseCmdStr(parts[2]); |
| 79 |
return { |
| 80 |
head: parts[1], |
| 81 |
middle, |
| 82 |
tail: parts[3], |
| 83 |
whole: parts[1] + middle.whole + parts[3], |
| 84 |
}; |
| 85 |
} |
| 86 |
return { |
| 87 |
head: null, |
| 88 |
middle: null, |
| 89 |
tail: null, |
| 90 |
whole: cmdStr, |
| 91 |
}; |
| 92 |
} |
| 93 |
function formatRange(cmd, formatStart, formatEnd, separator) { |
| 94 |
if (cmd.middle) { |
| 95 |
let startHead = formatStart(cmd.head); |
| 96 |
let startMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator); |
| 97 |
let startTail = formatStart(cmd.tail); |
| 98 |
let endHead = formatEnd(cmd.head); |
| 99 |
let endMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator); |
| 100 |
let endTail = formatEnd(cmd.tail); |
| 101 |
if (startHead === endHead && startTail === endTail) { |
| 102 |
return startHead + |
| 103 |
(startMiddle === endMiddle ? startMiddle : startMiddle + separator + endMiddle) + |
| 104 |
startTail; |
| 105 |
} |
| 106 |
} |
| 107 |
let startWhole = formatStart(cmd.whole); |
| 108 |
let endWhole = formatEnd(cmd.whole); |
| 109 |
if (startWhole === endWhole) { |
| 110 |
return startWhole; |
| 111 |
} |
| 112 |
return startWhole + separator + endWhole; |
| 113 |
} |
| 114 |
|
| 115 |
var plugin = core.createPlugin({ |
| 116 |
name: '@fullcalendar/luxon3', |
| 117 |
cmdFormatter: formatWithCmdStr, |
| 118 |
namedTimeZonedImpl: LuxonNamedTimeZone, |
| 119 |
}); |
| 120 |
|
| 121 |
core.globalPlugins.push(plugin); |
| 122 |
|
| 123 |
exports["default"] = plugin; |
| 124 |
exports.toLuxonDateTime = toLuxonDateTime; |
| 125 |
exports.toLuxonDuration = toLuxonDuration; |
| 126 |
|
| 127 |
Object.defineProperty(exports, '__esModule', { value: true }); |
| 128 |
|
| 129 |
return exports; |
| 130 |
|
| 131 |
})({}, FullCalendar, luxon, FullCalendar.Internal); |