Keine Beschreibung

main.js 6.5KB

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