Upload folder using huggingface_hub
Browse files- 6.0.1/datetime/DateTimePicker.svelte +433 -0
- 6.0.1/datetime/Example.svelte +5 -0
- 6.0.1/datetime/Index.svelte +293 -0
- 6.0.1/datetime/package.json +41 -0
- 6.0.1/datetime/types.ts +12 -0
- 6.0.1/datetime/utils.ts +183 -0
6.0.1/datetime/DateTimePicker.svelte
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
import { createEventDispatcher } from "svelte";
|
| 3 |
+
import {
|
| 4 |
+
format_date,
|
| 5 |
+
generate_calendar_days,
|
| 6 |
+
calculate_display_hour,
|
| 7 |
+
convert_display_hour_to_24h,
|
| 8 |
+
month_names
|
| 9 |
+
} from "./utils";
|
| 10 |
+
|
| 11 |
+
export let selected_date: Date;
|
| 12 |
+
export let current_year: number;
|
| 13 |
+
export let current_month: number;
|
| 14 |
+
export let selected_hour: number;
|
| 15 |
+
export let selected_minute: number;
|
| 16 |
+
export let selected_second: number;
|
| 17 |
+
export let is_pm: boolean;
|
| 18 |
+
export let include_time: boolean;
|
| 19 |
+
export let position: { top: number; left: number };
|
| 20 |
+
|
| 21 |
+
const dispatch = createEventDispatcher<{
|
| 22 |
+
close: void;
|
| 23 |
+
clear: void;
|
| 24 |
+
update: { date: Date; formatted: string };
|
| 25 |
+
}>();
|
| 26 |
+
|
| 27 |
+
$: display_hour = calculate_display_hour(selected_hour, is_pm);
|
| 28 |
+
$: calendar_days = generate_calendar_days(current_year, current_month);
|
| 29 |
+
|
| 30 |
+
const select_date = (day: number): void => {
|
| 31 |
+
selected_date = new Date(
|
| 32 |
+
current_year,
|
| 33 |
+
current_month,
|
| 34 |
+
day,
|
| 35 |
+
selected_hour,
|
| 36 |
+
selected_minute,
|
| 37 |
+
selected_second
|
| 38 |
+
);
|
| 39 |
+
update_value();
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
const update_value = (): void => {
|
| 43 |
+
const formatted = format_date(selected_date, include_time);
|
| 44 |
+
dispatch("update", { date: selected_date, formatted });
|
| 45 |
+
};
|
| 46 |
+
|
| 47 |
+
const update_time = (): void => {
|
| 48 |
+
selected_date = new Date(
|
| 49 |
+
current_year,
|
| 50 |
+
current_month,
|
| 51 |
+
selected_date.getDate(),
|
| 52 |
+
selected_hour,
|
| 53 |
+
selected_minute,
|
| 54 |
+
selected_second
|
| 55 |
+
);
|
| 56 |
+
update_value();
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
const previous_month = (): void => {
|
| 60 |
+
if (current_month === 0) {
|
| 61 |
+
current_month = 11;
|
| 62 |
+
current_year--;
|
| 63 |
+
} else {
|
| 64 |
+
current_month--;
|
| 65 |
+
}
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
const next_month = (): void => {
|
| 69 |
+
if (current_month === 11) {
|
| 70 |
+
current_month = 0;
|
| 71 |
+
current_year++;
|
| 72 |
+
} else {
|
| 73 |
+
current_month++;
|
| 74 |
+
}
|
| 75 |
+
};
|
| 76 |
+
|
| 77 |
+
const toggle_am_pm = (): void => {
|
| 78 |
+
is_pm = !is_pm;
|
| 79 |
+
if (is_pm && selected_hour < 12) {
|
| 80 |
+
selected_hour += 12;
|
| 81 |
+
} else if (!is_pm && selected_hour >= 12) {
|
| 82 |
+
selected_hour -= 12;
|
| 83 |
+
}
|
| 84 |
+
update_time();
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
const update_display_hour = (new_hour: number): void => {
|
| 88 |
+
selected_hour = convert_display_hour_to_24h(new_hour, is_pm);
|
| 89 |
+
update_time();
|
| 90 |
+
};
|
| 91 |
+
|
| 92 |
+
const handle_now = (): void => {
|
| 93 |
+
const now = new Date();
|
| 94 |
+
selected_date = now;
|
| 95 |
+
current_year = now.getFullYear();
|
| 96 |
+
current_month = now.getMonth();
|
| 97 |
+
selected_hour = now.getHours();
|
| 98 |
+
selected_minute = now.getMinutes();
|
| 99 |
+
selected_second = now.getSeconds();
|
| 100 |
+
is_pm = selected_hour >= 12;
|
| 101 |
+
update_value();
|
| 102 |
+
};
|
| 103 |
+
</script>
|
| 104 |
+
|
| 105 |
+
<div
|
| 106 |
+
class="picker-container"
|
| 107 |
+
style="top: {position.top}px; left: {position.left}px;"
|
| 108 |
+
>
|
| 109 |
+
<div class="picker">
|
| 110 |
+
<div class="picker-header">
|
| 111 |
+
<button type="button" class="nav-button" on:click={previous_month}
|
| 112 |
+
>‹</button
|
| 113 |
+
>
|
| 114 |
+
<div class="month-year">
|
| 115 |
+
{month_names[current_month]}
|
| 116 |
+
{current_year}
|
| 117 |
+
</div>
|
| 118 |
+
<button type="button" class="nav-button" on:click={next_month}>›</button>
|
| 119 |
+
</div>
|
| 120 |
+
|
| 121 |
+
<div class="calendar-grid">
|
| 122 |
+
<div class="weekdays">
|
| 123 |
+
<div class="weekday">Su</div>
|
| 124 |
+
<div class="weekday">Mo</div>
|
| 125 |
+
<div class="weekday">Tu</div>
|
| 126 |
+
<div class="weekday">We</div>
|
| 127 |
+
<div class="weekday">Th</div>
|
| 128 |
+
<div class="weekday">Fr</div>
|
| 129 |
+
<div class="weekday">Sa</div>
|
| 130 |
+
</div>
|
| 131 |
+
|
| 132 |
+
<div class="days">
|
| 133 |
+
{#each calendar_days as { day, is_current_month, is_next_month }}
|
| 134 |
+
<button
|
| 135 |
+
type="button"
|
| 136 |
+
class="day"
|
| 137 |
+
class:other-month={!is_current_month}
|
| 138 |
+
class:selected={is_current_month &&
|
| 139 |
+
day === selected_date.getDate() &&
|
| 140 |
+
current_month === selected_date.getMonth() &&
|
| 141 |
+
current_year === selected_date.getFullYear()}
|
| 142 |
+
on:click={() => {
|
| 143 |
+
if (is_current_month) {
|
| 144 |
+
select_date(day);
|
| 145 |
+
} else if (is_next_month) {
|
| 146 |
+
next_month();
|
| 147 |
+
select_date(day);
|
| 148 |
+
} else {
|
| 149 |
+
previous_month();
|
| 150 |
+
select_date(day);
|
| 151 |
+
}
|
| 152 |
+
}}
|
| 153 |
+
>
|
| 154 |
+
{day}
|
| 155 |
+
</button>
|
| 156 |
+
{/each}
|
| 157 |
+
</div>
|
| 158 |
+
</div>
|
| 159 |
+
|
| 160 |
+
{#if include_time}
|
| 161 |
+
<div class="time-picker">
|
| 162 |
+
<div class="time-inputs">
|
| 163 |
+
<div class="time-input-group">
|
| 164 |
+
<label for="hour">Hour</label>
|
| 165 |
+
<input
|
| 166 |
+
id="hour"
|
| 167 |
+
type="number"
|
| 168 |
+
min="1"
|
| 169 |
+
max="12"
|
| 170 |
+
bind:value={display_hour}
|
| 171 |
+
on:input={() => update_display_hour(display_hour)}
|
| 172 |
+
/>
|
| 173 |
+
</div>
|
| 174 |
+
<div class="time-input-group">
|
| 175 |
+
<label for="minute">Min</label>
|
| 176 |
+
<input
|
| 177 |
+
id="minute"
|
| 178 |
+
type="number"
|
| 179 |
+
min="0"
|
| 180 |
+
max="59"
|
| 181 |
+
bind:value={selected_minute}
|
| 182 |
+
on:input={update_time}
|
| 183 |
+
/>
|
| 184 |
+
</div>
|
| 185 |
+
<div class="time-input-group">
|
| 186 |
+
<label for="second">Sec</label>
|
| 187 |
+
<input
|
| 188 |
+
id="second"
|
| 189 |
+
type="number"
|
| 190 |
+
min="0"
|
| 191 |
+
max="59"
|
| 192 |
+
bind:value={selected_second}
|
| 193 |
+
on:input={update_time}
|
| 194 |
+
/>
|
| 195 |
+
</div>
|
| 196 |
+
<div class="time-input-group">
|
| 197 |
+
<span class="am-pm-label">Period</span>
|
| 198 |
+
<button
|
| 199 |
+
type="button"
|
| 200 |
+
class="am-pm-toggle"
|
| 201 |
+
on:click={toggle_am_pm}
|
| 202 |
+
aria-label="Toggle AM/PM"
|
| 203 |
+
>
|
| 204 |
+
{is_pm ? "PM" : "AM"}
|
| 205 |
+
</button>
|
| 206 |
+
</div>
|
| 207 |
+
</div>
|
| 208 |
+
</div>
|
| 209 |
+
{/if}
|
| 210 |
+
|
| 211 |
+
<div class="picker-actions">
|
| 212 |
+
<button
|
| 213 |
+
type="button"
|
| 214 |
+
class="action-button"
|
| 215 |
+
on:click={() => dispatch("clear")}
|
| 216 |
+
>
|
| 217 |
+
Clear
|
| 218 |
+
</button>
|
| 219 |
+
<div class="picker-actions-right">
|
| 220 |
+
<button type="button" class="action-button" on:click={handle_now}>
|
| 221 |
+
Now
|
| 222 |
+
</button>
|
| 223 |
+
<button
|
| 224 |
+
type="button"
|
| 225 |
+
class="action-button"
|
| 226 |
+
on:click={() => dispatch("close")}
|
| 227 |
+
>
|
| 228 |
+
Done
|
| 229 |
+
</button>
|
| 230 |
+
</div>
|
| 231 |
+
</div>
|
| 232 |
+
</div>
|
| 233 |
+
</div>
|
| 234 |
+
|
| 235 |
+
<style>
|
| 236 |
+
.picker-container {
|
| 237 |
+
position: fixed;
|
| 238 |
+
z-index: 9999;
|
| 239 |
+
box-shadow: var(--shadow-drop-lg);
|
| 240 |
+
border-radius: var(--radius-lg);
|
| 241 |
+
background: var(--background-fill-primary);
|
| 242 |
+
border: 1px solid var(--border-color-primary);
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
.picker {
|
| 246 |
+
padding: var(--size-3);
|
| 247 |
+
min-width: 280px;
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
.picker-header {
|
| 251 |
+
display: flex;
|
| 252 |
+
justify-content: space-between;
|
| 253 |
+
align-items: center;
|
| 254 |
+
margin-bottom: var(--size-3);
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
.nav-button {
|
| 258 |
+
background: none;
|
| 259 |
+
border: none;
|
| 260 |
+
font-size: var(--text-lg);
|
| 261 |
+
padding: var(--size-1);
|
| 262 |
+
border-radius: var(--radius-sm);
|
| 263 |
+
transition: var(--button-transition);
|
| 264 |
+
cursor: pointer;
|
| 265 |
+
color: var(--body-text-color-subdued);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
.nav-button:hover {
|
| 269 |
+
background: var(--button-secondary-background-fill-hover);
|
| 270 |
+
color: var(--body-text-color);
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
.month-year {
|
| 274 |
+
font-weight: var(--weight-semibold);
|
| 275 |
+
font-size: var(--text-base);
|
| 276 |
+
color: var(--body-text-color);
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
.calendar-grid {
|
| 280 |
+
margin-bottom: var(--size-3);
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
.weekdays {
|
| 284 |
+
display: grid;
|
| 285 |
+
grid-template-columns: repeat(7, 1fr);
|
| 286 |
+
gap: 1px;
|
| 287 |
+
margin-bottom: var(--size-2);
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
.weekday {
|
| 291 |
+
text-align: center;
|
| 292 |
+
font-size: var(--text-sm);
|
| 293 |
+
font-weight: var(--weight-semibold);
|
| 294 |
+
color: var(--body-text-color-subdued);
|
| 295 |
+
padding: var(--size-1);
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
.days {
|
| 299 |
+
display: grid;
|
| 300 |
+
grid-template-columns: repeat(7, 1fr);
|
| 301 |
+
gap: 1px;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
.day {
|
| 305 |
+
aspect-ratio: 1;
|
| 306 |
+
border: none;
|
| 307 |
+
background: none;
|
| 308 |
+
border-radius: var(--radius-sm);
|
| 309 |
+
font-size: var(--text-sm);
|
| 310 |
+
transition: var(--button-transition);
|
| 311 |
+
color: var(--body-text-color);
|
| 312 |
+
cursor: pointer;
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
.day:hover {
|
| 316 |
+
background: var(--button-secondary-background-fill-hover);
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
.day.other-month {
|
| 320 |
+
color: var(--body-text-color-subdued);
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
.day.selected {
|
| 324 |
+
background: var(--button-primary-background-fill);
|
| 325 |
+
color: var(--button-primary-text-color);
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.day.selected:hover {
|
| 329 |
+
background: var(--button-primary-background-fill-hover);
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
.time-picker {
|
| 333 |
+
border-top: 1px solid var(--border-color-primary);
|
| 334 |
+
padding-top: var(--size-3);
|
| 335 |
+
margin-bottom: var(--size-3);
|
| 336 |
+
}
|
| 337 |
+
|
| 338 |
+
.time-inputs {
|
| 339 |
+
display: flex;
|
| 340 |
+
gap: var(--size-2);
|
| 341 |
+
justify-content: center;
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
.time-input-group {
|
| 345 |
+
display: flex;
|
| 346 |
+
flex-direction: column;
|
| 347 |
+
align-items: center;
|
| 348 |
+
gap: var(--size-1);
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
.time-input-group label {
|
| 352 |
+
font-size: var(--text-xs);
|
| 353 |
+
color: var(--body-text-color-subdued);
|
| 354 |
+
font-weight: var(--weight-semibold);
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
.am-pm-label {
|
| 358 |
+
font-size: var(--text-xs);
|
| 359 |
+
color: var(--body-text-color-subdued);
|
| 360 |
+
font-weight: var(--weight-semibold);
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
.time-input-group input {
|
| 364 |
+
width: 50px;
|
| 365 |
+
padding: var(--size-1);
|
| 366 |
+
border: 1px solid var(--input-border-color);
|
| 367 |
+
border-radius: var(--radius-sm);
|
| 368 |
+
text-align: center;
|
| 369 |
+
font-size: var(--text-sm);
|
| 370 |
+
background: var(--input-background-fill);
|
| 371 |
+
color: var(--body-text-color);
|
| 372 |
+
}
|
| 373 |
+
|
| 374 |
+
.time-input-group input:focus {
|
| 375 |
+
outline: none;
|
| 376 |
+
border-color: var(--input-border-color-focus);
|
| 377 |
+
box-shadow: var(--input-shadow-focus);
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
.am-pm-toggle {
|
| 381 |
+
width: 50px;
|
| 382 |
+
padding: var(--size-1);
|
| 383 |
+
border: 1px solid var(--button-primary-border-color);
|
| 384 |
+
border-radius: var(--radius-sm);
|
| 385 |
+
text-align: center;
|
| 386 |
+
font-size: var(--text-sm);
|
| 387 |
+
background: var(--button-primary-background-fill);
|
| 388 |
+
color: var(--button-primary-text-color);
|
| 389 |
+
cursor: pointer;
|
| 390 |
+
transition: var(--button-transition);
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
.am-pm-toggle:hover {
|
| 394 |
+
background: var(--button-primary-background-fill-hover);
|
| 395 |
+
border-color: var(--button-primary-border-color-hover);
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
.am-pm-toggle:focus {
|
| 399 |
+
outline: none;
|
| 400 |
+
border-color: var(--button-primary-border-color-focus);
|
| 401 |
+
box-shadow: var(--button-primary-shadow-focus);
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
.picker-actions {
|
| 405 |
+
display: flex;
|
| 406 |
+
gap: var(--size-2);
|
| 407 |
+
justify-content: space-between;
|
| 408 |
+
align-items: center;
|
| 409 |
+
border-top: 1px solid var(--border-color-primary);
|
| 410 |
+
padding-top: var(--size-3);
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
.picker-actions-right {
|
| 414 |
+
display: flex;
|
| 415 |
+
gap: var(--size-2);
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
.action-button {
|
| 419 |
+
padding: var(--size-1) var(--size-3);
|
| 420 |
+
border: 1px solid var(--button-secondary-border-color);
|
| 421 |
+
border-radius: var(--radius-sm);
|
| 422 |
+
background: var(--button-secondary-background-fill);
|
| 423 |
+
color: var(--button-secondary-text-color);
|
| 424 |
+
font-size: var(--text-sm);
|
| 425 |
+
transition: var(--button-transition);
|
| 426 |
+
cursor: pointer;
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
.action-button:hover {
|
| 430 |
+
background: var(--button-secondary-background-fill-hover);
|
| 431 |
+
border-color: var(--button-secondary-border-color-hover);
|
| 432 |
+
}
|
| 433 |
+
</style>
|
6.0.1/datetime/Example.svelte
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script lang="ts">
|
| 2 |
+
export let value: string | null;
|
| 3 |
+
</script>
|
| 4 |
+
|
| 5 |
+
{value || ""}
|
6.0.1/datetime/Index.svelte
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<script context="module" lang="ts">
|
| 2 |
+
export { default as BaseExample } from "./Example.svelte";
|
| 3 |
+
</script>
|
| 4 |
+
|
| 5 |
+
<script lang="ts">
|
| 6 |
+
import { Block, BlockTitle } from "@gradio/atoms";
|
| 7 |
+
import { Calendar } from "@gradio/icons";
|
| 8 |
+
import { onDestroy } from "svelte";
|
| 9 |
+
import DateTimePicker from "./DateTimePicker.svelte";
|
| 10 |
+
import { format_date, date_is_valid_format, parse_date_value } from "./utils";
|
| 11 |
+
import { Gradio } from "@gradio/utils";
|
| 12 |
+
import type { DateTimeProps, DateTimeEvents } from "./types";
|
| 13 |
+
|
| 14 |
+
const props = $props();
|
| 15 |
+
const gradio = new Gradio<DateTimeEvents, DateTimeProps>(props);
|
| 16 |
+
|
| 17 |
+
let old_value = $state(gradio.props.value);
|
| 18 |
+
let show_picker = $state(false);
|
| 19 |
+
let entered_value = $state(gradio.props.value);
|
| 20 |
+
let picker_ref: HTMLDivElement;
|
| 21 |
+
let input_ref: HTMLInputElement;
|
| 22 |
+
let calendar_button_ref: HTMLButtonElement;
|
| 23 |
+
let picker_position = $state({ top: 0, left: 0 });
|
| 24 |
+
|
| 25 |
+
let current_year = $state(new Date().getFullYear());
|
| 26 |
+
let current_month = $state(new Date().getMonth());
|
| 27 |
+
let selected_date = $state(new Date());
|
| 28 |
+
let selected_hour = $state(new Date().getHours());
|
| 29 |
+
let selected_minute = $state(new Date().getMinutes());
|
| 30 |
+
let selected_second = $state(new Date().getSeconds());
|
| 31 |
+
let is_pm = $state(selected_hour >= 12);
|
| 32 |
+
|
| 33 |
+
let valid = $derived.by(() =>
|
| 34 |
+
date_is_valid_format(entered_value, gradio.props.include_time)
|
| 35 |
+
);
|
| 36 |
+
let disabled = $derived(!gradio.shared.interactive);
|
| 37 |
+
|
| 38 |
+
$effect(() => {
|
| 39 |
+
if (old_value != gradio.props.value) {
|
| 40 |
+
old_value = gradio.props.value;
|
| 41 |
+
entered_value = gradio.props.value;
|
| 42 |
+
update_picker_from_value();
|
| 43 |
+
gradio.dispatch("change");
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
const update_picker_from_value = (): void => {
|
| 48 |
+
const parsed = parse_date_value(entered_value, gradio.props.include_time);
|
| 49 |
+
selected_date = parsed.selected_date;
|
| 50 |
+
current_year = parsed.current_year;
|
| 51 |
+
current_month = parsed.current_month;
|
| 52 |
+
selected_hour = parsed.selected_hour;
|
| 53 |
+
selected_minute = parsed.selected_minute;
|
| 54 |
+
selected_second = parsed.selected_second;
|
| 55 |
+
is_pm = parsed.is_pm;
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
const submit_values = (): void => {
|
| 59 |
+
if (entered_value === gradio.props.value) return;
|
| 60 |
+
if (!date_is_valid_format(entered_value, gradio.props.include_time)) return;
|
| 61 |
+
old_value = gradio.props.value = entered_value;
|
| 62 |
+
gradio.dispatch("change");
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
const calculate_picker_position = (): void => {
|
| 66 |
+
if (calendar_button_ref) {
|
| 67 |
+
const rect = calendar_button_ref.getBoundingClientRect();
|
| 68 |
+
picker_position = {
|
| 69 |
+
top: rect.bottom + 4,
|
| 70 |
+
left: rect.right - 280
|
| 71 |
+
};
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
const toggle_picker = (event: MouseEvent): void => {
|
| 76 |
+
if (!disabled) {
|
| 77 |
+
event.stopPropagation();
|
| 78 |
+
show_picker = !show_picker;
|
| 79 |
+
if (show_picker) {
|
| 80 |
+
update_picker_from_value();
|
| 81 |
+
calculate_picker_position();
|
| 82 |
+
setTimeout(() => {
|
| 83 |
+
if (typeof window !== "undefined") {
|
| 84 |
+
window.addEventListener("click", handle_click_outside);
|
| 85 |
+
window.addEventListener("scroll", handle_scroll, true);
|
| 86 |
+
}
|
| 87 |
+
}, 10);
|
| 88 |
+
} else if (typeof window !== "undefined") {
|
| 89 |
+
window.removeEventListener("click", handle_click_outside);
|
| 90 |
+
window.removeEventListener("scroll", handle_scroll, true);
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
const close_picker = (): void => {
|
| 96 |
+
show_picker = false;
|
| 97 |
+
if (typeof window !== "undefined") {
|
| 98 |
+
window.removeEventListener("click", handle_click_outside);
|
| 99 |
+
window.removeEventListener("scroll", handle_scroll, true);
|
| 100 |
+
}
|
| 101 |
+
};
|
| 102 |
+
|
| 103 |
+
const handle_click_outside = (event: MouseEvent): void => {
|
| 104 |
+
if (
|
| 105 |
+
show_picker &&
|
| 106 |
+
picker_ref &&
|
| 107 |
+
!picker_ref.contains(event.target as Node) &&
|
| 108 |
+
calendar_button_ref &&
|
| 109 |
+
!calendar_button_ref.contains(event.target as Node)
|
| 110 |
+
) {
|
| 111 |
+
close_picker();
|
| 112 |
+
}
|
| 113 |
+
};
|
| 114 |
+
|
| 115 |
+
const handle_scroll = (): void => {
|
| 116 |
+
if (show_picker) {
|
| 117 |
+
calculate_picker_position();
|
| 118 |
+
}
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
+
const handle_picker_update = (
|
| 122 |
+
event: CustomEvent<{ date: Date; formatted: string }>
|
| 123 |
+
): void => {
|
| 124 |
+
entered_value = event.detail.formatted;
|
| 125 |
+
submit_values();
|
| 126 |
+
};
|
| 127 |
+
|
| 128 |
+
const handle_picker_clear = (): void => {
|
| 129 |
+
entered_value = "";
|
| 130 |
+
gradio.props.value = "";
|
| 131 |
+
close_picker();
|
| 132 |
+
gradio.dispatch("change");
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
onDestroy(() => {
|
| 136 |
+
if (typeof window !== "undefined") {
|
| 137 |
+
window.removeEventListener("click", handle_click_outside);
|
| 138 |
+
window.removeEventListener("scroll", handle_scroll, true);
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
|
| 142 |
+
update_picker_from_value();
|
| 143 |
+
</script>
|
| 144 |
+
|
| 145 |
+
<Block
|
| 146 |
+
visible={gradio.shared.visible}
|
| 147 |
+
elem_id={gradio.shared.elem_id}
|
| 148 |
+
elem_classes={gradio.shared.elem_classes}
|
| 149 |
+
scale={gradio.shared.scale}
|
| 150 |
+
min_width={gradio.shared.min_width}
|
| 151 |
+
allow_overflow={false}
|
| 152 |
+
padding={true}
|
| 153 |
+
>
|
| 154 |
+
<div class="label-content">
|
| 155 |
+
<BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
|
| 156 |
+
>{gradio.shared.label || "Date"}</BlockTitle
|
| 157 |
+
>
|
| 158 |
+
</div>
|
| 159 |
+
<div class="timebox">
|
| 160 |
+
<input
|
| 161 |
+
bind:this={input_ref}
|
| 162 |
+
class="time"
|
| 163 |
+
bind:value={entered_value}
|
| 164 |
+
class:invalid={!valid}
|
| 165 |
+
on:keydown={(evt) => {
|
| 166 |
+
if (evt.key === "Enter") {
|
| 167 |
+
submit_values();
|
| 168 |
+
gradio.dispatch("submit");
|
| 169 |
+
}
|
| 170 |
+
}}
|
| 171 |
+
on:blur={submit_values}
|
| 172 |
+
{disabled}
|
| 173 |
+
placeholder={gradio.props.include_time
|
| 174 |
+
? "YYYY-MM-DD HH:MM:SS"
|
| 175 |
+
: "YYYY-MM-DD"}
|
| 176 |
+
/>
|
| 177 |
+
|
| 178 |
+
{#if gradio.shared.interactive}
|
| 179 |
+
<button
|
| 180 |
+
bind:this={calendar_button_ref}
|
| 181 |
+
class="calendar"
|
| 182 |
+
{disabled}
|
| 183 |
+
on:click={toggle_picker}
|
| 184 |
+
>
|
| 185 |
+
<Calendar />
|
| 186 |
+
</button>
|
| 187 |
+
{/if}
|
| 188 |
+
</div>
|
| 189 |
+
|
| 190 |
+
{#if show_picker}
|
| 191 |
+
<div bind:this={picker_ref}>
|
| 192 |
+
<DateTimePicker
|
| 193 |
+
bind:selected_date
|
| 194 |
+
bind:current_year
|
| 195 |
+
bind:current_month
|
| 196 |
+
bind:selected_hour
|
| 197 |
+
bind:selected_minute
|
| 198 |
+
bind:selected_second
|
| 199 |
+
bind:is_pm
|
| 200 |
+
include_time={gradio.props.include_time}
|
| 201 |
+
position={picker_position}
|
| 202 |
+
on:update={handle_picker_update}
|
| 203 |
+
on:clear={handle_picker_clear}
|
| 204 |
+
on:close={close_picker}
|
| 205 |
+
/>
|
| 206 |
+
</div>
|
| 207 |
+
{/if}
|
| 208 |
+
</Block>
|
| 209 |
+
|
| 210 |
+
<style>
|
| 211 |
+
.label-content {
|
| 212 |
+
display: flex;
|
| 213 |
+
justify-content: space-between;
|
| 214 |
+
align-items: flex-start;
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
button {
|
| 218 |
+
cursor: pointer;
|
| 219 |
+
color: var(--body-text-color-subdued);
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
button:hover {
|
| 223 |
+
color: var(--body-text-color);
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
::placeholder {
|
| 227 |
+
color: var(--input-placeholder-color);
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
.timebox {
|
| 231 |
+
flex-grow: 1;
|
| 232 |
+
flex-shrink: 1;
|
| 233 |
+
display: flex;
|
| 234 |
+
position: relative;
|
| 235 |
+
background: var(--input-background-fill);
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
.timebox :global(svg) {
|
| 239 |
+
height: 18px;
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
.time {
|
| 243 |
+
padding: var(--input-padding);
|
| 244 |
+
color: var(--body-text-color);
|
| 245 |
+
font-weight: var(--input-text-weight);
|
| 246 |
+
font-size: var(--input-text-size);
|
| 247 |
+
line-height: var(--line-sm);
|
| 248 |
+
outline: none;
|
| 249 |
+
flex-grow: 1;
|
| 250 |
+
background: none;
|
| 251 |
+
border: var(--input-border-width) solid var(--input-border-color);
|
| 252 |
+
border-right: none;
|
| 253 |
+
border-top-left-radius: var(--input-radius);
|
| 254 |
+
border-bottom-left-radius: var(--input-radius);
|
| 255 |
+
box-shadow: var(--input-shadow);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
.time:disabled {
|
| 259 |
+
border-right: var(--input-border-width) solid var(--input-border-color);
|
| 260 |
+
border-top-right-radius: var(--input-radius);
|
| 261 |
+
border-bottom-right-radius: var(--input-radius);
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
.time.invalid {
|
| 265 |
+
color: var(--body-text-color-subdued);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
.calendar {
|
| 269 |
+
display: inline-flex;
|
| 270 |
+
justify-content: center;
|
| 271 |
+
align-items: center;
|
| 272 |
+
transition: var(--button-transition);
|
| 273 |
+
box-shadow: var(--button-primary-shadow);
|
| 274 |
+
text-align: center;
|
| 275 |
+
background: var(--button-secondary-background-fill);
|
| 276 |
+
color: var(--button-secondary-text-color);
|
| 277 |
+
font-weight: var(--button-large-text-weight);
|
| 278 |
+
font-size: var(--button-large-text-size);
|
| 279 |
+
border-top-right-radius: var(--input-radius);
|
| 280 |
+
border-bottom-right-radius: var(--input-radius);
|
| 281 |
+
padding: var(--size-2);
|
| 282 |
+
border: var(--input-border-width) solid var(--input-border-color);
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
.calendar:hover {
|
| 286 |
+
background: var(--button-secondary-background-fill-hover);
|
| 287 |
+
box-shadow: var(--button-primary-shadow-hover);
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
.calendar:active {
|
| 291 |
+
box-shadow: var(--button-primary-shadow-active);
|
| 292 |
+
}
|
| 293 |
+
</style>
|
6.0.1/datetime/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/datetime",
|
| 3 |
+
"version": "0.3.23",
|
| 4 |
+
"description": "Gradio UI packages",
|
| 5 |
+
"type": "module",
|
| 6 |
+
"author": "",
|
| 7 |
+
"license": "ISC",
|
| 8 |
+
"private": false,
|
| 9 |
+
"main_changeset": true,
|
| 10 |
+
"main": "Index.svelte",
|
| 11 |
+
"exports": {
|
| 12 |
+
".": {
|
| 13 |
+
"gradio": "./Index.svelte",
|
| 14 |
+
"svelte": "./dist/Index.svelte",
|
| 15 |
+
"types": "./dist/Index.svelte.d.ts"
|
| 16 |
+
},
|
| 17 |
+
"./example": {
|
| 18 |
+
"gradio": "./Example.svelte",
|
| 19 |
+
"svelte": "./dist/Example.svelte",
|
| 20 |
+
"types": "./dist/Example.svelte.d.ts"
|
| 21 |
+
},
|
| 22 |
+
"./package.json": "./package.json"
|
| 23 |
+
},
|
| 24 |
+
"dependencies": {
|
| 25 |
+
"@gradio/atoms": "workspace:^",
|
| 26 |
+
"@gradio/icons": "workspace:^",
|
| 27 |
+
"@gradio/statustracker": "workspace:^",
|
| 28 |
+
"@gradio/utils": "workspace:^"
|
| 29 |
+
},
|
| 30 |
+
"peerDependencies": {
|
| 31 |
+
"svelte": "^5.43.4"
|
| 32 |
+
},
|
| 33 |
+
"devDependencies": {
|
| 34 |
+
"@gradio/preview": "workspace:^"
|
| 35 |
+
},
|
| 36 |
+
"repository": {
|
| 37 |
+
"type": "git",
|
| 38 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 39 |
+
"directory": "js/datetime"
|
| 40 |
+
}
|
| 41 |
+
}
|
6.0.1/datetime/types.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export interface DateTimeProps {
|
| 2 |
+
value: string;
|
| 3 |
+
include_time: boolean;
|
| 4 |
+
type: "timestamp" | "datetime" | "string";
|
| 5 |
+
timezone: string | null;
|
| 6 |
+
info: string;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export interface DateTimeEvents {
|
| 10 |
+
change: never;
|
| 11 |
+
submit: never;
|
| 12 |
+
}
|
6.0.1/datetime/utils.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const format_date = (date: Date, include_time: boolean): string => {
|
| 2 |
+
if (date.toJSON() === null) return "";
|
| 3 |
+
const pad = (num: number): string => num.toString().padStart(2, "0");
|
| 4 |
+
|
| 5 |
+
const year = date.getFullYear();
|
| 6 |
+
const month = pad(date.getMonth() + 1);
|
| 7 |
+
const day = pad(date.getDate());
|
| 8 |
+
const hours = pad(date.getHours());
|
| 9 |
+
const minutes = pad(date.getMinutes());
|
| 10 |
+
const seconds = pad(date.getSeconds());
|
| 11 |
+
|
| 12 |
+
const date_str = `${year}-${month}-${day}`;
|
| 13 |
+
const time_str = `${hours}:${minutes}:${seconds}`;
|
| 14 |
+
if (include_time) {
|
| 15 |
+
return `${date_str} ${time_str}`;
|
| 16 |
+
}
|
| 17 |
+
return date_str;
|
| 18 |
+
};
|
| 19 |
+
|
| 20 |
+
export const date_is_valid_format = (
|
| 21 |
+
date: string | null,
|
| 22 |
+
include_time: boolean
|
| 23 |
+
): boolean => {
|
| 24 |
+
if (date == null || date === "") return true;
|
| 25 |
+
const valid_regex = include_time
|
| 26 |
+
? /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/
|
| 27 |
+
: /^\d{4}-\d{2}-\d{2}$/;
|
| 28 |
+
const is_valid_date = date.match(valid_regex) !== null;
|
| 29 |
+
const is_valid_now =
|
| 30 |
+
date.match(/^(?:\s*now\s*(?:-\s*\d+\s*[dmhs])?)?\s*$/) !== null;
|
| 31 |
+
return is_valid_date || is_valid_now;
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
export const get_days_in_month = (year: number, month: number): number => {
|
| 35 |
+
return new Date(year, month + 1, 0).getDate();
|
| 36 |
+
};
|
| 37 |
+
|
| 38 |
+
export const get_first_day_of_month = (year: number, month: number): number => {
|
| 39 |
+
return new Date(year, month, 1).getDay();
|
| 40 |
+
};
|
| 41 |
+
|
| 42 |
+
export const parse_date_value = (
|
| 43 |
+
entered_value: string,
|
| 44 |
+
include_time: boolean
|
| 45 |
+
): {
|
| 46 |
+
selected_date: Date;
|
| 47 |
+
current_year: number;
|
| 48 |
+
current_month: number;
|
| 49 |
+
selected_hour: number;
|
| 50 |
+
selected_minute: number;
|
| 51 |
+
selected_second: number;
|
| 52 |
+
is_pm: boolean;
|
| 53 |
+
} => {
|
| 54 |
+
if (!entered_value || entered_value === "") {
|
| 55 |
+
const now = new Date();
|
| 56 |
+
return {
|
| 57 |
+
selected_date: now,
|
| 58 |
+
current_year: now.getFullYear(),
|
| 59 |
+
current_month: now.getMonth(),
|
| 60 |
+
selected_hour: now.getHours(),
|
| 61 |
+
selected_minute: now.getMinutes(),
|
| 62 |
+
selected_second: now.getSeconds(),
|
| 63 |
+
is_pm: now.getHours() >= 12
|
| 64 |
+
};
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
try {
|
| 68 |
+
let date_to_parse = entered_value;
|
| 69 |
+
if (!include_time && entered_value.match(/^\d{4}-\d{2}-\d{2}$/)) {
|
| 70 |
+
date_to_parse += " 00:00:00";
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
const parsed = new Date(date_to_parse.replace(" ", "T"));
|
| 74 |
+
if (!isNaN(parsed.getTime())) {
|
| 75 |
+
return {
|
| 76 |
+
selected_date: parsed,
|
| 77 |
+
current_year: parsed.getFullYear(),
|
| 78 |
+
current_month: parsed.getMonth(),
|
| 79 |
+
selected_hour: parsed.getHours(),
|
| 80 |
+
selected_minute: parsed.getMinutes(),
|
| 81 |
+
selected_second: parsed.getSeconds(),
|
| 82 |
+
is_pm: parsed.getHours() >= 12
|
| 83 |
+
};
|
| 84 |
+
}
|
| 85 |
+
} catch (e) {
|
| 86 |
+
// fallback to current date
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
const now = new Date();
|
| 90 |
+
return {
|
| 91 |
+
selected_date: now,
|
| 92 |
+
current_year: now.getFullYear(),
|
| 93 |
+
current_month: now.getMonth(),
|
| 94 |
+
selected_hour: now.getHours(),
|
| 95 |
+
selected_minute: now.getMinutes(),
|
| 96 |
+
selected_second: now.getSeconds(),
|
| 97 |
+
is_pm: now.getHours() >= 12
|
| 98 |
+
};
|
| 99 |
+
};
|
| 100 |
+
|
| 101 |
+
export const generate_calendar_days = (
|
| 102 |
+
current_year: number,
|
| 103 |
+
current_month: number
|
| 104 |
+
): {
|
| 105 |
+
day: number;
|
| 106 |
+
is_current_month: boolean;
|
| 107 |
+
is_next_month: boolean;
|
| 108 |
+
}[] => {
|
| 109 |
+
const days_in_month = get_days_in_month(current_year, current_month);
|
| 110 |
+
const first_day = get_first_day_of_month(current_year, current_month);
|
| 111 |
+
const days = [];
|
| 112 |
+
|
| 113 |
+
const prev_month = current_month === 0 ? 11 : current_month - 1;
|
| 114 |
+
const prev_year = current_month === 0 ? current_year - 1 : current_year;
|
| 115 |
+
const days_in_prev_month = get_days_in_month(prev_year, prev_month);
|
| 116 |
+
|
| 117 |
+
for (let i = first_day - 1; i >= 0; i--) {
|
| 118 |
+
days.push({
|
| 119 |
+
day: days_in_prev_month - i,
|
| 120 |
+
is_current_month: false,
|
| 121 |
+
is_next_month: false
|
| 122 |
+
});
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
for (let day = 1; day <= days_in_month; day++) {
|
| 126 |
+
days.push({
|
| 127 |
+
day,
|
| 128 |
+
is_current_month: true,
|
| 129 |
+
is_next_month: false
|
| 130 |
+
});
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
const remaining_slots = 42 - days.length;
|
| 134 |
+
for (let day = 1; day <= remaining_slots; day++) {
|
| 135 |
+
days.push({
|
| 136 |
+
day,
|
| 137 |
+
is_current_month: false,
|
| 138 |
+
is_next_month: true
|
| 139 |
+
});
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
return days;
|
| 143 |
+
};
|
| 144 |
+
|
| 145 |
+
export const calculate_display_hour = (
|
| 146 |
+
selected_hour: number,
|
| 147 |
+
is_pm: boolean
|
| 148 |
+
): number => {
|
| 149 |
+
return is_pm
|
| 150 |
+
? selected_hour === 0
|
| 151 |
+
? 12
|
| 152 |
+
: selected_hour > 12
|
| 153 |
+
? selected_hour - 12
|
| 154 |
+
: selected_hour
|
| 155 |
+
: selected_hour === 0
|
| 156 |
+
? 12
|
| 157 |
+
: selected_hour;
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
export const convert_display_hour_to_24h = (
|
| 161 |
+
display_hour: number,
|
| 162 |
+
is_pm: boolean
|
| 163 |
+
): number => {
|
| 164 |
+
if (is_pm) {
|
| 165 |
+
return display_hour === 12 ? 12 : display_hour + 12;
|
| 166 |
+
}
|
| 167 |
+
return display_hour === 12 ? 0 : display_hour;
|
| 168 |
+
};
|
| 169 |
+
|
| 170 |
+
export const month_names = [
|
| 171 |
+
"January",
|
| 172 |
+
"February",
|
| 173 |
+
"March",
|
| 174 |
+
"April",
|
| 175 |
+
"May",
|
| 176 |
+
"June",
|
| 177 |
+
"July",
|
| 178 |
+
"August",
|
| 179 |
+
"September",
|
| 180 |
+
"October",
|
| 181 |
+
"November",
|
| 182 |
+
"December"
|
| 183 |
+
];
|