|
| 1 | +<script lang="ts"> |
| 2 | + import type { Moment } from "moment"; |
| 3 | + import { derived, Readable } from "svelte/store"; |
| 4 | +
|
| 5 | + import Arrow from "./Arrow.svelte"; |
| 6 | + import Day from "./Day.svelte"; |
| 7 | + import WeekNum from "./WeekNum.svelte"; |
| 8 | + import { displayedMonth, metadata } from "./stores"; |
| 9 | + import { |
| 10 | + IMonth, |
| 11 | + getDaysOfWeek, |
| 12 | + getMonth, |
| 13 | + getStartOfWeek, |
| 14 | + isWeekend, |
| 15 | + } from "./utils"; |
| 16 | +
|
| 17 | + export let today: Moment = window.moment(); |
| 18 | + export let showWeekNums: boolean = false; |
| 19 | +
|
| 20 | + export let onHoverDay: (date: Moment, targetEl: EventTarget) => void; |
| 21 | + export let onHoverWeek: (date: Moment, targetEl: EventTarget) => void; |
| 22 | + export let onClickDay: (date: Moment, isMetaPressed: boolean) => void; |
| 23 | + export let onClickWeek: (date: Moment, isMetaPressed: boolean) => void; |
| 24 | +
|
| 25 | + export let localeData: any; |
| 26 | + export let dependencies: [Readable<any>, ...Array<Readable<any>>]; |
| 27 | +
|
| 28 | + const dependencyStore = derived(dependencies, (values) => values); |
| 29 | +
|
| 30 | + let month: IMonth; |
| 31 | + let daysOfWeek: string[]; |
| 32 | +
|
| 33 | + // Get the word 'Today' but localized to the current language |
| 34 | + const todayDisplayStr = today.calendar().split(/\d|\s/)[0]; |
| 35 | +
|
| 36 | + $: month = getMonth($displayedMonth, localeData); |
| 37 | + $: daysOfWeek = getDaysOfWeek(localeData); |
| 38 | +</script> |
| 39 | + |
| 40 | +<svelte:options immutable /> |
| 41 | +<div id="calendar-container" class="container"> |
| 42 | + <div class="nav"> |
| 43 | + <h3 class="title" on:click="{displayedMonth.reset}"> |
| 44 | + <span class="month">{$displayedMonth.format('MMM')}</span> |
| 45 | + <span class="year">{$displayedMonth.format('YYYY')}</span> |
| 46 | + </h3> |
| 47 | + <div class="right-nav"> |
| 48 | + <Arrow |
| 49 | + direction="left" |
| 50 | + onClick="{displayedMonth.decrement}" |
| 51 | + tooltip="Previous Month" |
| 52 | + /> |
| 53 | + <div class="reset-button" on:click="{displayedMonth.reset}"> |
| 54 | + {todayDisplayStr} |
| 55 | + </div> |
| 56 | + <Arrow |
| 57 | + direction="right" |
| 58 | + onClick="{displayedMonth.increment}" |
| 59 | + tooltip="Next Month" |
| 60 | + /> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + <table class="calendar"> |
| 64 | + <colgroup> |
| 65 | + {#if showWeekNums} |
| 66 | + <col /> |
| 67 | + {/if} |
| 68 | + {#each month[1].days as date} |
| 69 | + <col class:weekend="{isWeekend(date)}" /> |
| 70 | + {/each} |
| 71 | + </colgroup> |
| 72 | + <thead> |
| 73 | + <tr> |
| 74 | + {#if showWeekNums} |
| 75 | + <th>W</th> |
| 76 | + {/if} |
| 77 | + {#each daysOfWeek as dayOfWeek} |
| 78 | + <th>{dayOfWeek}</th> |
| 79 | + {/each} |
| 80 | + </tr> |
| 81 | + </thead> |
| 82 | + <tbody> |
| 83 | + {#each month as week} |
| 84 | + <tr> |
| 85 | + {#if showWeekNums} |
| 86 | + <WeekNum |
| 87 | + {...week} |
| 88 | + onClick="{onClickWeek}" |
| 89 | + onHover="{onHoverWeek}" |
| 90 | + metadata="{metadata.getWeek(getStartOfWeek(week.days), $dependencyStore)}" |
| 91 | + /> |
| 92 | + {/if} |
| 93 | + {#each week.days as date (date.format())} |
| 94 | + <Day |
| 95 | + today="{today}" |
| 96 | + date="{date}" |
| 97 | + onClick="{onClickDay}" |
| 98 | + onHover="{onHoverDay}" |
| 99 | + metadata="{metadata.getDay(date, $dependencyStore)}" |
| 100 | + /> |
| 101 | + {/each} |
| 102 | + </tr> |
| 103 | + {/each} |
| 104 | + </tbody> |
| 105 | + </table> |
| 106 | +</div> |
| 107 | + |
| 108 | +<style> |
| 109 | + .container { |
| 110 | + --color-background-heading: transparent; |
| 111 | + --color-background-day: transparent; |
| 112 | + --color-background-weeknum: transparent; |
| 113 | + --color-background-weekend: transparent; |
| 114 | +
|
| 115 | + --color-dot: var(--text-muted); |
| 116 | + --color-arrow: var(--text-muted); |
| 117 | + --color-button: var(--text-muted); |
| 118 | +
|
| 119 | + --color-text-title: var(--text-normal); |
| 120 | + --color-text-heading: var(--text-muted); |
| 121 | + --color-text-day: var(--text-normal); |
| 122 | + --color-text-today: var(--interactive-accent); |
| 123 | + --color-text-weeknum: var(--text-muted); |
| 124 | + } |
| 125 | +
|
| 126 | + .container { |
| 127 | + padding: 0 8px; |
| 128 | + } |
| 129 | +
|
| 130 | + th { |
| 131 | + text-align: center; |
| 132 | + } |
| 133 | +
|
| 134 | + .nav { |
| 135 | + align-items: center; |
| 136 | + display: flex; |
| 137 | + margin: 0.6em 0 1em; |
| 138 | + padding: 0 8px; |
| 139 | + width: 100%; |
| 140 | + } |
| 141 | +
|
| 142 | + .title { |
| 143 | + color: var(--color-text-title); |
| 144 | + font-size: 1.5em; |
| 145 | + margin: 0; |
| 146 | + } |
| 147 | +
|
| 148 | + .month { |
| 149 | + font-weight: 500; |
| 150 | + text-transform: capitalize; |
| 151 | + } |
| 152 | +
|
| 153 | + .year { |
| 154 | + color: var(--interactive-accent); |
| 155 | + } |
| 156 | +
|
| 157 | + .right-nav { |
| 158 | + display: flex; |
| 159 | + justify-content: center; |
| 160 | + margin-left: auto; |
| 161 | + } |
| 162 | +
|
| 163 | + .reset-button { |
| 164 | + border-radius: 4px; |
| 165 | + color: var(--text-muted); |
| 166 | + font-size: 0.7em; |
| 167 | + font-weight: 600; |
| 168 | + letter-spacing: 1px; |
| 169 | + margin: 0 4px; |
| 170 | + padding: 0px 4px; |
| 171 | + text-transform: uppercase; |
| 172 | + } |
| 173 | +
|
| 174 | + .weekend { |
| 175 | + background-color: var(--color-background-weekend); |
| 176 | + } |
| 177 | +
|
| 178 | + .calendar { |
| 179 | + border-collapse: collapse; |
| 180 | + width: 100%; |
| 181 | + } |
| 182 | +
|
| 183 | + th { |
| 184 | + background-color: var(--color-background-heading); |
| 185 | + color: var(--color-text-heading); |
| 186 | + font-size: 0.6em; |
| 187 | + letter-spacing: 1px; |
| 188 | + padding: 4px 8px; |
| 189 | + text-transform: uppercase; |
| 190 | + } |
| 191 | +</style> |
0 commit comments