暂无描述

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. FullCalendar Luxon Plugin v4.3.0
  3. Docs & License: https://fullcalendar.io/
  4. (c) 2019 Adam Shaw
  5. */
  6. import { DateTime, Duration } from 'luxon';
  7. import { createPlugin, Calendar, NamedTimeZoneImpl } from '@fullcalendar/core';
  8. /*! *****************************************************************************
  9. Copyright (c) Microsoft Corporation. All rights reserved.
  10. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  11. this file except in compliance with the License. You may obtain a copy of the
  12. License at http://www.apache.org/licenses/LICENSE-2.0
  13. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  15. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  16. MERCHANTABLITY OR NON-INFRINGEMENT.
  17. See the Apache Version 2.0 License for specific language governing permissions
  18. and limitations under the License.
  19. ***************************************************************************** */
  20. /* global Reflect, Promise */
  21. var extendStatics = function(d, b) {
  22. extendStatics = Object.setPrototypeOf ||
  23. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  24. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  25. return extendStatics(d, b);
  26. };
  27. function __extends(d, b) {
  28. extendStatics(d, b);
  29. function __() { this.constructor = d; }
  30. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  31. }
  32. var __assign = function() {
  33. __assign = Object.assign || function __assign(t) {
  34. for (var s, i = 1, n = arguments.length; i < n; i++) {
  35. s = arguments[i];
  36. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  37. }
  38. return t;
  39. };
  40. return __assign.apply(this, arguments);
  41. };
  42. function toDateTime(date, calendar) {
  43. if (!(calendar instanceof Calendar)) {
  44. throw new Error('must supply a Calendar instance');
  45. }
  46. return DateTime.fromJSDate(date, {
  47. zone: calendar.dateEnv.timeZone,
  48. locale: calendar.dateEnv.locale.codes[0]
  49. });
  50. }
  51. function toDuration(duration, calendar) {
  52. if (!(calendar instanceof Calendar)) {
  53. throw new Error('must supply a Calendar instance');
  54. }
  55. return Duration.fromObject(__assign({}, duration, { locale: calendar.dateEnv.locale.codes[0] }));
  56. }
  57. var LuxonNamedTimeZone = /** @class */ (function (_super) {
  58. __extends(LuxonNamedTimeZone, _super);
  59. function LuxonNamedTimeZone() {
  60. return _super !== null && _super.apply(this, arguments) || this;
  61. }
  62. LuxonNamedTimeZone.prototype.offsetForArray = function (a) {
  63. return arrayToLuxon(a, this.timeZoneName).offset;
  64. };
  65. LuxonNamedTimeZone.prototype.timestampToArray = function (ms) {
  66. return luxonToArray(DateTime.fromMillis(ms, {
  67. zone: this.timeZoneName
  68. }));
  69. };
  70. return LuxonNamedTimeZone;
  71. }(NamedTimeZoneImpl));
  72. function formatWithCmdStr(cmdStr, arg) {
  73. var cmd = parseCmdStr(cmdStr);
  74. if (arg.end) {
  75. var start = arrayToLuxon(arg.start.array, arg.timeZone, arg.localeCodes[0]);
  76. var end = arrayToLuxon(arg.end.array, arg.timeZone, arg.localeCodes[0]);
  77. return formatRange(cmd, start.toFormat.bind(start), end.toFormat.bind(end), arg.separator);
  78. }
  79. return arrayToLuxon(arg.date.array, arg.timeZone, arg.localeCodes[0]).toFormat(cmd.whole);
  80. }
  81. var main = createPlugin({
  82. cmdFormatter: formatWithCmdStr,
  83. namedTimeZonedImpl: LuxonNamedTimeZone
  84. });
  85. function luxonToArray(datetime) {
  86. return [
  87. datetime.year,
  88. datetime.month - 1,
  89. datetime.day,
  90. datetime.hour,
  91. datetime.minute,
  92. datetime.second,
  93. datetime.millisecond
  94. ];
  95. }
  96. function arrayToLuxon(arr, timeZone, locale) {
  97. return DateTime.fromObject({
  98. zone: timeZone,
  99. locale: locale,
  100. year: arr[0],
  101. month: arr[1] + 1,
  102. day: arr[2],
  103. hour: arr[3],
  104. minute: arr[4],
  105. second: arr[5],
  106. millisecond: arr[6]
  107. });
  108. }
  109. function parseCmdStr(cmdStr) {
  110. var parts = cmdStr.match(/^(.*?)\{(.*)\}(.*)$/); // TODO: lookbehinds for escape characters
  111. if (parts) {
  112. var middle = parseCmdStr(parts[2]);
  113. return {
  114. head: parts[1],
  115. middle: middle,
  116. tail: parts[3],
  117. whole: parts[1] + middle.whole + parts[3]
  118. };
  119. }
  120. else {
  121. return {
  122. head: null,
  123. middle: null,
  124. tail: null,
  125. whole: cmdStr
  126. };
  127. }
  128. }
  129. function formatRange(cmd, formatStart, formatEnd, separator) {
  130. if (cmd.middle) {
  131. var startHead = formatStart(cmd.head);
  132. var startMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator);
  133. var startTail = formatStart(cmd.tail);
  134. var endHead = formatEnd(cmd.head);
  135. var endMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator);
  136. var endTail = formatEnd(cmd.tail);
  137. if (startHead === endHead && startTail === endTail) {
  138. return startHead +
  139. (startMiddle === endMiddle ? startMiddle : startMiddle + separator + endMiddle) +
  140. startTail;
  141. }
  142. }
  143. var startWhole = formatStart(cmd.whole);
  144. var endWhole = formatEnd(cmd.whole);
  145. if (startWhole === endWhole) {
  146. return startWhole;
  147. }
  148. else {
  149. return startWhole + separator + endWhole;
  150. }
  151. }
  152. export default main;
  153. export { toDateTime, toDuration };