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.

92 lines
2.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <script lang="ts">
  2. import { formatDate } from "../utils/time";
  3. import EveryMinute from "./EveryMinute.svelte";
  4. export let startTime: Date | string = new Date();
  5. export let endTime: Date | string = new Date();
  6. export let compact: boolean = false;
  7. let started = false;
  8. let overdue = false;
  9. let danger = false;
  10. let amount = 0;
  11. let amountStr = "0";
  12. let unit = "days";
  13. let titleTimeStr = "";
  14. let now = new Date();
  15. $: {
  16. overdue = false;
  17. unit = "days";
  18. const st = (startTime instanceof Date) ? startTime : new Date(startTime);
  19. const et = (endTime instanceof Date) ? endTime : new Date(endTime);
  20. if (now < st) {
  21. started = false;
  22. amount = (st.getTime() - now.getTime()) / 86400000
  23. } else {
  24. started = true;
  25. amount = (et.getTime() - now.getTime()) / 86400000
  26. }
  27. if (amount < 0) {
  28. overdue = true;
  29. amount = -amount;
  30. }
  31. danger = (!overdue && started && amount <= 3);
  32. if (amount < 2) {
  33. amount *= 24;
  34. unit = "hours"
  35. }
  36. if (amount < 2) {
  37. amount *= 60;
  38. unit = "minutes";
  39. }
  40. amount = Math.floor(amount);
  41. if (amount <= 1) {
  42. unit = unit.slice(0, -1);
  43. }
  44. if (amount < 1) {
  45. amountStr = "< 1"
  46. } else {
  47. amountStr = amount.toString()
  48. }
  49. if (compact) {
  50. unit = unit.slice(0, 1);
  51. }
  52. titleTimeStr = `${formatDate(new Date(startTime))} – ${formatDate(new Date(endTime))}`
  53. }
  54. </script>
  55. <EveryMinute bind:now={now} />
  56. <span title={titleTimeStr}>
  57. {#if (overdue)}
  58. <span class="overdue">{amountStr}{compact ? "" : " "}{unit} {!compact ? "ago" : ""}</span>
  59. {:else if (started)}
  60. <span class:danger class="started">{amountStr}{compact ? "" : " "}{unit} {!compact ? "left" : ""}</span>
  61. {:else}
  62. <span class="pending">{compact ? "" : "In "}{amountStr}{compact ? "" : " "}{unit}</span>
  63. {/if}
  64. </span>
  65. <style>
  66. span {
  67. color: #ccc;
  68. }
  69. span.pending {
  70. color: #2797e2;
  71. }
  72. span.danger {
  73. color: #e28127;
  74. }
  75. span.overdue {
  76. color: #666666;
  77. }
  78. </style>