resources-4c8610b1.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. 'use strict';
  7. const index = require('./index-a0010f96.js');
  8. const locale = require('./locale-678ce361.js');
  9. /**
  10. * Check if date is within a min and max
  11. *
  12. * @param date
  13. * @param min
  14. * @param max
  15. */
  16. function inRange(date, min, max) {
  17. const time = date.getTime();
  18. const afterMin = !(min instanceof Date) || time >= min.getTime();
  19. const beforeMax = !(max instanceof Date) || time <= max.getTime();
  20. return afterMin && beforeMax;
  21. }
  22. /**
  23. * Ensures date is within range,
  24. * returns min or max if out of bounds
  25. *
  26. * @param date
  27. * @param min
  28. * @param max
  29. */
  30. function dateFromRange(date, min, max) {
  31. if (!(date instanceof Date)) {
  32. return null;
  33. }
  34. const time = date.getTime();
  35. const beforeMin = min instanceof Date && time < min.getTime();
  36. const afterMax = max instanceof Date && time > max.getTime();
  37. if (beforeMin) {
  38. return min;
  39. }
  40. if (afterMax) {
  41. return max;
  42. }
  43. return date;
  44. }
  45. /**
  46. * Parse an iso8601 string (YYYY-mm-dd) into a valid date.
  47. * TODO: handle time when time of day UI is added
  48. *
  49. * @param iso8601
  50. * @param isEndDate
  51. */
  52. function dateFromISO(iso8601, isEndDate = false) {
  53. if (iso8601 instanceof Date) {
  54. return iso8601;
  55. }
  56. if (!iso8601 || typeof iso8601 !== "string") {
  57. return null;
  58. }
  59. const d = iso8601.split(/[: T-]/).map(parseFloat);
  60. const date = new Date(d[0], (d[1] || 1) - 1, d[2] || 1);
  61. date.setFullYear(d[0]);
  62. if (isNaN(date.getTime())) {
  63. throw new Error(`Invalid ISO 8601 date: "${iso8601}"`);
  64. }
  65. if (isEndDate) {
  66. return setEndOfDay(date);
  67. }
  68. return date;
  69. }
  70. /**
  71. * Return first portion of ISO string (YYYY-mm-dd)
  72. *
  73. * @param date
  74. */
  75. function dateToISO(date) {
  76. if (typeof date === "string") {
  77. return date;
  78. }
  79. if (date instanceof Date) {
  80. return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().split("T")[0];
  81. }
  82. return "";
  83. }
  84. /**
  85. * Check if two dates are the same day, month, year
  86. *
  87. * @param d1
  88. * @param d2
  89. */
  90. function sameDate(d1, d2) {
  91. return (d1 instanceof Date &&
  92. d2 instanceof Date &&
  93. d1.getDate() === d2.getDate() &&
  94. d1.getMonth() === d2.getMonth() &&
  95. d1.getFullYear() === d2.getFullYear());
  96. }
  97. /**
  98. * Get a date one month in the past
  99. *
  100. * @param date
  101. */
  102. function prevMonth(date) {
  103. const month = date.getMonth();
  104. const nextDate = new Date(date);
  105. nextDate.setMonth(month - 1);
  106. // date doesn't exist in new month, use last day
  107. if (month === nextDate.getMonth()) {
  108. return new Date(date.getFullYear(), month, 0);
  109. }
  110. return nextDate;
  111. }
  112. /**
  113. * Get a date one month in the future
  114. *
  115. * @param date
  116. */
  117. function nextMonth(date) {
  118. const month = date.getMonth();
  119. const nextDate = new Date(date);
  120. nextDate.setMonth(month + 1);
  121. // date doesn't exist in new month, use last day
  122. if ((month + 2) % 7 === nextDate.getMonth() % 7) {
  123. return new Date(date.getFullYear(), month + 2, 0);
  124. }
  125. return nextDate;
  126. }
  127. /**
  128. * Parse numeric units for day, month, and year from a localized string
  129. * month starts at 0 (can pass to date constructor)
  130. *
  131. * @param str
  132. * @param localeData
  133. */
  134. function parseDateString(str, localeData) {
  135. const { separator, unitOrder } = localeData;
  136. const order = getOrder(unitOrder);
  137. const values = str.split(separator).map((part) => locale.numberStringFormatter.delocalize(part));
  138. return {
  139. day: parseInt(values[order.indexOf("d")]),
  140. month: parseInt(values[order.indexOf("m")]) - 1,
  141. year: parseInt(values[order.indexOf("y")])
  142. };
  143. }
  144. /**
  145. * Based on the unitOrder string, find order of month, day, and year for locale
  146. *
  147. * @param unitOrder
  148. */
  149. function getOrder(unitOrder) {
  150. const signifiers = ["d", "m", "y"];
  151. const order = unitOrder.toLowerCase();
  152. return signifiers.sort((a, b) => order.indexOf(a) - order.indexOf(b));
  153. }
  154. /**
  155. * Get number of days between two dates
  156. *
  157. * @param date1
  158. * @param date2
  159. */
  160. function getDaysDiff(date1, date2) {
  161. const ts1 = date1.getTime();
  162. const ts2 = date2.getTime();
  163. return (ts1 - ts2) / (1000 * 3600 * 24);
  164. }
  165. /**
  166. * Set time of the day to the end.
  167. *
  168. * @param {Date} date Date.
  169. * @returns {Date} Date with time set to end of day .
  170. */
  171. function setEndOfDay(date) {
  172. date.setHours(23, 59, 59, 999);
  173. return date;
  174. }
  175. /**
  176. * CLDR cache.
  177. * Exported for testing purposes.
  178. *
  179. * @private
  180. */
  181. const translationCache = {};
  182. /**
  183. * CLDR request cache.
  184. * Exported for testing purposes.
  185. *
  186. * @private
  187. */
  188. const requestCache = {};
  189. /**
  190. * Fetch calendar data for a given locale from list of supported languages
  191. *
  192. * @param lang
  193. * @public
  194. */
  195. async function getLocaleData(lang) {
  196. const locale$1 = locale.getSupportedLocale(lang);
  197. if (translationCache[locale$1]) {
  198. return translationCache[locale$1];
  199. }
  200. if (!requestCache[locale$1]) {
  201. requestCache[locale$1] = fetch(index.getAssetPath(`./assets/date-picker/nls/${locale$1}.json`))
  202. .then((resp) => resp.json())
  203. .catch(() => {
  204. console.error(`Translations for "${locale$1}" not found or invalid, falling back to english`);
  205. return getLocaleData("en");
  206. });
  207. }
  208. const data = await requestCache[locale$1];
  209. translationCache[locale$1] = data;
  210. return data;
  211. }
  212. /**
  213. * Maps value to valueAsDate
  214. *
  215. * @param value
  216. */
  217. function getValueAsDateRange(value) {
  218. return value.map((v, index) => dateFromISO(v, index === 1));
  219. }
  220. const HEADING_LEVEL = 2;
  221. const TEXT = {
  222. nextMonth: "Next month",
  223. prevMonth: "Previous month",
  224. year: "Year"
  225. };
  226. exports.HEADING_LEVEL = HEADING_LEVEL;
  227. exports.TEXT = TEXT;
  228. exports.dateFromISO = dateFromISO;
  229. exports.dateFromRange = dateFromRange;
  230. exports.dateToISO = dateToISO;
  231. exports.getDaysDiff = getDaysDiff;
  232. exports.getLocaleData = getLocaleData;
  233. exports.getOrder = getOrder;
  234. exports.getValueAsDateRange = getValueAsDateRange;
  235. exports.inRange = inRange;
  236. exports.nextMonth = nextMonth;
  237. exports.parseDateString = parseDateString;
  238. exports.prevMonth = prevMonth;
  239. exports.sameDate = sameDate;
  240. exports.setEndOfDay = setEndOfDay;