Geen omschrijving

main.esm.js 3.3KB

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