提交 | 用户 | 时间
|
1ac2bc
|
1 |
/*! |
懒 |
2 |
FullCalendar Moment Plugin v4.3.0 |
|
3 |
Docs & License: https://fullcalendar.io/ |
|
4 |
(c) 2019 Adam Shaw |
|
5 |
*/ |
|
6 |
|
|
7 |
(function (global, factory) { |
|
8 |
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('moment'), require('@fullcalendar/core')) : |
|
9 |
typeof define === 'function' && define.amd ? define(['exports', 'moment', '@fullcalendar/core'], factory) : |
|
10 |
(global = global || self, factory(global.FullCalendarMoment = {}, global.moment, global.FullCalendar)); |
|
11 |
}(this, function (exports, momentNs, core) { 'use strict'; |
|
12 |
|
|
13 |
var moment = momentNs; // the directly callable function |
|
14 |
function toMoment(date, calendar) { |
|
15 |
if (!(calendar instanceof core.Calendar)) { |
|
16 |
throw new Error('must supply a Calendar instance'); |
|
17 |
} |
|
18 |
return convertToMoment(date, calendar.dateEnv.timeZone, null, calendar.dateEnv.locale.codes[0]); |
|
19 |
} |
|
20 |
function toDuration(fcDuration) { |
|
21 |
return moment.duration(fcDuration); // moment accepts all the props that fc.Duration already has! |
|
22 |
} |
|
23 |
function formatWithCmdStr(cmdStr, arg) { |
|
24 |
var cmd = parseCmdStr(cmdStr); |
|
25 |
if (arg.end) { |
|
26 |
var startMom = convertToMoment(arg.start.array, arg.timeZone, arg.start.timeZoneOffset, arg.localeCodes[0]); |
|
27 |
var endMom = convertToMoment(arg.end.array, arg.timeZone, arg.end.timeZoneOffset, arg.localeCodes[0]); |
|
28 |
return formatRange(cmd, createMomentFormatFunc(startMom), createMomentFormatFunc(endMom), arg.separator); |
|
29 |
} |
|
30 |
return convertToMoment(arg.date.array, arg.timeZone, arg.date.timeZoneOffset, arg.localeCodes[0]).format(cmd.whole); // TODO: test for this |
|
31 |
} |
|
32 |
var main = core.createPlugin({ |
|
33 |
cmdFormatter: formatWithCmdStr |
|
34 |
}); |
|
35 |
function createMomentFormatFunc(mom) { |
|
36 |
return function (cmdStr) { |
|
37 |
return cmdStr ? mom.format(cmdStr) : ''; // because calling with blank string results in ISO8601 :( |
|
38 |
}; |
|
39 |
} |
|
40 |
function convertToMoment(input, timeZone, timeZoneOffset, locale) { |
|
41 |
var mom; |
|
42 |
if (timeZone === 'local') { |
|
43 |
mom = moment(input); |
|
44 |
} |
|
45 |
else if (timeZone === 'UTC') { |
|
46 |
mom = moment.utc(input); |
|
47 |
} |
|
48 |
else if (moment.tz) { |
|
49 |
mom = moment.tz(input, timeZone); |
|
50 |
} |
|
51 |
else { |
|
52 |
mom = moment.utc(input); |
|
53 |
if (timeZoneOffset != null) { |
|
54 |
mom.utcOffset(timeZoneOffset); |
|
55 |
} |
|
56 |
} |
|
57 |
mom.locale(locale); |
|
58 |
return mom; |
|
59 |
} |
|
60 |
function parseCmdStr(cmdStr) { |
|
61 |
var parts = cmdStr.match(/^(.*?)\{(.*)\}(.*)$/); // TODO: lookbehinds for escape characters |
|
62 |
if (parts) { |
|
63 |
var middle = parseCmdStr(parts[2]); |
|
64 |
return { |
|
65 |
head: parts[1], |
|
66 |
middle: middle, |
|
67 |
tail: parts[3], |
|
68 |
whole: parts[1] + middle.whole + parts[3] |
|
69 |
}; |
|
70 |
} |
|
71 |
else { |
|
72 |
return { |
|
73 |
head: null, |
|
74 |
middle: null, |
|
75 |
tail: null, |
|
76 |
whole: cmdStr |
|
77 |
}; |
|
78 |
} |
|
79 |
} |
|
80 |
function formatRange(cmd, formatStart, formatEnd, separator) { |
|
81 |
if (cmd.middle) { |
|
82 |
var startHead = formatStart(cmd.head); |
|
83 |
var startMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator); |
|
84 |
var startTail = formatStart(cmd.tail); |
|
85 |
var endHead = formatEnd(cmd.head); |
|
86 |
var endMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator); |
|
87 |
var endTail = formatEnd(cmd.tail); |
|
88 |
if (startHead === endHead && startTail === endTail) { |
|
89 |
return startHead + |
|
90 |
(startMiddle === endMiddle ? startMiddle : startMiddle + separator + endMiddle) + |
|
91 |
startTail; |
|
92 |
} |
|
93 |
} |
|
94 |
var startWhole = formatStart(cmd.whole); |
|
95 |
var endWhole = formatEnd(cmd.whole); |
|
96 |
if (startWhole === endWhole) { |
|
97 |
return startWhole; |
|
98 |
} |
|
99 |
else { |
|
100 |
return startWhole + separator + endWhole; |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
exports.default = main; |
|
105 |
exports.toDuration = toDuration; |
|
106 |
exports.toMoment = toMoment; |
|
107 |
|
|
108 |
Object.defineProperty(exports, '__esModule', { value: true }); |
|
109 |
|
|
110 |
})); |