You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.9 KiB

  1. <script lang="ts">
  2. import { onMount } from "svelte";
  3. import { endOfMonth, endOfWeek, endOfYear, formatFormTime, lastMonth, monthName, nextMonth, startOfMonth, startOfWeek, startOfYear } from "../utils/time";
  4. import EveryMinute from "./EveryMinute.svelte";
  5. interface DateOption {
  6. id: string
  7. label: string
  8. value: Date
  9. }
  10. export let value: string;
  11. export let disabled: boolean;
  12. let selected: string = "custom";
  13. let options: DateOption[] = [];
  14. let now: Date = new Date();
  15. onMount(() => {
  16. if (value === "") {
  17. selected = "none";
  18. return;
  19. }
  20. for (const option of options) {
  21. if (formatFormTime(option.value) === value) {
  22. selected = option.id;
  23. }
  24. }
  25. });
  26. $: {
  27. if (options.length === 0) {
  28. let current = startOfMonth(now);
  29. const nextWeek = new Date(now.getTime() + (86400000 * 7));
  30. const lastWeek = new Date(now.getTime() - (86400000 * 7));
  31. options.push({
  32. id: "none",
  33. label: "No deadline",
  34. value: new Date(Number.NaN),
  35. });
  36. options.push({
  37. id: "this_week",
  38. label: "End of this Week",
  39. value: endOfWeek(now),
  40. });
  41. options.push({
  42. id: "next_week",
  43. label: "End of next Week",
  44. value: endOfWeek(nextWeek),
  45. });
  46. options.push({
  47. id: "last_week",
  48. label: "End of last Week",
  49. value: endOfWeek(lastWeek),
  50. });
  51. options.push({
  52. id: "this_month",
  53. label: "End of this month",
  54. value: endOfMonth(current),
  55. });
  56. options.push({
  57. id: "next_month",
  58. label: "End of next month",
  59. value: endOfMonth(nextMonth(current)),
  60. });
  61. options.push({
  62. id: "last_month",
  63. label: "End of last month",
  64. value: endOfMonth(lastMonth(current)),
  65. });
  66. options.push({
  67. id: "this_year",
  68. label: "End of this year",
  69. value: endOfYear(now),
  70. });
  71. options.push({
  72. id: "next_year",
  73. label: "End of next year",
  74. value: endOfYear(new Date(Date.now() + (365.25 * 86400000))),
  75. });
  76. options.push({
  77. id: "last_year",
  78. label: "End of last year",
  79. value: endOfYear(new Date(Date.now() - (365.25 * 86400000))),
  80. });
  81. options.push({
  82. id: "custom",
  83. label: "Specific Date",
  84. value: null,
  85. });
  86. }
  87. }
  88. $: {
  89. if (selected !== "custom") {
  90. const option = options.find(o => o.id === selected);
  91. if (option != null) {
  92. value = formatFormTime(option.value);
  93. }
  94. }
  95. }
  96. </script>
  97. <EveryMinute bind:now={now} />
  98. <select name="endTime" disabled={disabled} bind:value={selected}>
  99. {#each options as option (option.id)}
  100. <option value={option.id}>{option.label}</option>
  101. {/each}
  102. </select>
  103. {#if selected === "custom"}
  104. <input disabled={disabled} name="value" type="datetime-local" bind:value={value} />
  105. {/if}