main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. FullCalendar Moment Plugin v4.3.0
  3. Docs & License: https://fullcalendar.io/
  4. (c) 2019 Adam Shaw
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('moment'), require('@fullcalendar/core')) :
  8. typeof define === 'function' && define.amd ? define(['exports', 'moment', '@fullcalendar/core'], factory) :
  9. (global = global || self, factory(global.FullCalendarMoment = {}, global.moment, global.FullCalendar));
  10. }(this, function (exports, momentNs, core) {
  11. 'use strict';
  12. var moment = momentNs; // the directly callable function
  13. function toMoment(date, calendar) {
  14. if (!(calendar instanceof core.Calendar)) {
  15. throw new Error('must supply a Calendar instance');
  16. }
  17. return convertToMoment(date, calendar.dateEnv.timeZone, null, calendar.dateEnv.locale.codes[0]);
  18. }
  19. function toDuration(fcDuration) {
  20. return moment.duration(fcDuration); // moment accepts all the props that fc.Duration already has!
  21. }
  22. function formatWithCmdStr(cmdStr, arg) {
  23. var cmd = parseCmdStr(cmdStr);
  24. if (arg.end) {
  25. var startMom = convertToMoment(arg.start.array, arg.timeZone, arg.start.timeZoneOffset, arg.localeCodes[0]);
  26. var endMom = convertToMoment(arg.end.array, arg.timeZone, arg.end.timeZoneOffset, arg.localeCodes[0]);
  27. return formatRange(cmd, createMomentFormatFunc(startMom), createMomentFormatFunc(endMom), arg.separator);
  28. }
  29. return convertToMoment(arg.date.array, arg.timeZone, arg.date.timeZoneOffset, arg.localeCodes[0]).format(cmd.whole); // TODO: test for this
  30. }
  31. var main = core.createPlugin({
  32. cmdFormatter: formatWithCmdStr
  33. });
  34. function createMomentFormatFunc(mom) {
  35. return function (cmdStr) {
  36. return cmdStr ? mom.format(cmdStr) : ''; // because calling with blank string results in ISO8601 :(
  37. };
  38. }
  39. function convertToMoment(input, timeZone, timeZoneOffset, locale) {
  40. var mom;
  41. if (timeZone === 'local') {
  42. mom = moment(input);
  43. } else if (timeZone === 'UTC') {
  44. mom = moment.utc(input);
  45. } else if (moment.tz) {
  46. mom = moment.tz(input, timeZone);
  47. } else {
  48. mom = moment.utc(input);
  49. if (timeZoneOffset != null) {
  50. mom.utcOffset(timeZoneOffset);
  51. }
  52. }
  53. mom.locale(locale);
  54. return mom;
  55. }
  56. function parseCmdStr(cmdStr) {
  57. var parts = cmdStr.match(/^(.*?)\{(.*)\}(.*)$/); // TODO: lookbehinds for escape characters
  58. if (parts) {
  59. var middle = parseCmdStr(parts[2]);
  60. return {
  61. head: parts[1],
  62. middle: middle,
  63. tail: parts[3],
  64. whole: parts[1] + middle.whole + parts[3]
  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. } else {
  94. return startWhole + separator + endWhole;
  95. }
  96. }
  97. exports.default = main;
  98. exports.toDuration = toDuration;
  99. exports.toMoment = toMoment;
  100. Object.defineProperty(exports, '__esModule', {value: true});
  101. }));