Diff -- /var/www/vhosts/3dshawn.com/crm.3dshawn.com/assets/javascript/datepicker.js
Diff
/var/www/vhosts/3dshawn.com/crm.3dshawn.com/assets/javascript/datepicker.js
added on local at 2026-07-01 15:02:36
Added
+0
lines
Removed
-0
lines
Context
824
unchanged
Blobs
from b14a57ce027b
to b14a57ce027b
to b14a57ce027b
Unified diff
Naive LCS-based line diff. Additions in emerald, deletions in rose. Both sides decompressed live from BLOB_STORE.| 1 | 1 | /*====================================================================== |
| 2 | 2 | * datepicker.js -- vanilla-JS date + time picker (no deps). |
| 3 | 3 | * |
| 4 | 4 | * Auto-attaches to every `<input type="datetime-local" data-fancy>` |
| 5 | 5 | * on DOMContentLoaded. Renders a polished popover with: |
| 6 | 6 | * - month-grid calendar (prev / next month arrows, day-of-week header, |
| 7 | 7 | * cells outside the active month are dimmed, today is ringed, |
| 8 | 8 | * selected date is filled with the brand color) |
| 9 | 9 | * - hour / minute number inputs with +/- spinners + quick-set chips |
| 10 | 10 | * - "Today" / "Now" / "Clear" actions |
| 11 | 11 | * - keyboard nav: arrows move the day, Enter selects, Esc closes, |
| 12 | 12 | * PageUp/PageDown change month |
| 13 | 13 | * - smart placement (flips above when the viewport bottom is too close) |
| 14 | 14 | * - theme follows CSS vars --col-brand / --col-brand-2 / --col-bg-2 / |
| 15 | 15 | * --col-border / --col-text / --col-surface (with sensible fallbacks) |
| 16 | 16 | * |
| 17 | 17 | * The native input is hidden visually but kept in the DOM so the form |
| 18 | 18 | * still submits its value. The picker writes to .value with the standard |
| 19 | 19 | * datetime-local format (YYYY-MM-DDTHH:MM). |
| 20 | 20 | * |
| 21 | 21 | * Usage: |
| 22 | 22 | * <input type="datetime-local" name="from" data-fancy> |
| 23 | 23 | *======================================================================*/ |
| 24 | 24 | (function () { |
| 25 | 25 | 'use strict'; |
| 26 | 26 | |
| 27 | 27 | // -------------------------------------------------------------------- |
| 28 | 28 | // Style injection -- one global stylesheet for all pickers on the page. |
| 29 | 29 | // -------------------------------------------------------------------- |
| 30 | 30 | function injectStyles() { |
| 31 | 31 | if (document.getElementById('fdp-styles')) return; |
| 32 | 32 | var css = ` |
| 33 | 33 | .fdp-wrap { position: relative; display: inline-block; } |
| 34 | 34 | .fdp-display { |
| 35 | 35 | background: var(--col-bg-2, #0e1822); |
| 36 | 36 | color: var(--col-text, #e2e8f0); |
| 37 | 37 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 38 | 38 | border-radius: 8px; |
| 39 | 39 | padding: 6px 32px 6px 12px; |
| 40 | 40 | font: inherit; |
| 41 | 41 | font-size: 12.5px; |
| 42 | 42 | cursor: pointer; |
| 43 | 43 | outline: none; |
| 44 | 44 | min-width: 200px; |
| 45 | 45 | transition: border-color .15s ease, box-shadow .15s ease; |
| 46 | 46 | font-variant-numeric: tabular-nums; |
| 47 | 47 | } |
| 48 | 48 | .fdp-display:hover, .fdp-display:focus { border-color: var(--col-brand, #06b6d4); } |
| 49 | 49 | .fdp-display.has-value { color: var(--col-text, #e2e8f0); } |
| 50 | 50 | .fdp-display.empty { color: var(--col-text-3, #64748b); } |
| 51 | 51 | .fdp-wrap::after { |
| 52 | 52 | content: ''; |
| 53 | 53 | position: absolute; |
| 54 | 54 | right: 12px; top: 50%; |
| 55 | 55 | width: 7px; height: 7px; |
| 56 | 56 | border-right: 1.5px solid var(--col-text-3, #64748b); |
| 57 | 57 | border-bottom: 1.5px solid var(--col-text-3, #64748b); |
| 58 | 58 | transform: translateY(-70%) rotate(45deg); |
| 59 | 59 | pointer-events: none; |
| 60 | 60 | transition: transform .15s ease, border-color .15s ease; |
| 61 | 61 | } |
| 62 | 62 | .fdp-wrap.is-open::after { |
| 63 | 63 | transform: translateY(-30%) rotate(-135deg); |
| 64 | 64 | border-color: var(--col-brand, #06b6d4); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | .fdp-pop { |
| 68 | 68 | position: absolute; |
| 69 | 69 | top: calc(100% + 6px); left: 0; |
| 70 | 70 | background: var(--col-surface, #0f1a26); |
| 71 | 71 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 72 | 72 | border-radius: 14px; |
| 73 | 73 | box-shadow: 0 20px 60px rgba(0,0,0,0.55), 0 0 0 1px rgba(6,182,212,0.08); |
| 74 | 74 | padding: 14px; |
| 75 | 75 | z-index: 9999; |
| 76 | 76 | width: 320px; |
| 77 | 77 | font-size: 12.5px; |
| 78 | 78 | color: var(--col-text, #e2e8f0); |
| 79 | 79 | user-select: none; |
| 80 | 80 | animation: fdp-pop-in .12s ease-out; |
| 81 | 81 | backdrop-filter: blur(10px); |
| 82 | 82 | -webkit-backdrop-filter: blur(10px); |
| 83 | 83 | } |
| 84 | 84 | .fdp-pop.flip-up { top: auto; bottom: calc(100% + 6px); } |
| 85 | 85 | @keyframes fdp-pop-in { |
| 86 | 86 | from { opacity: 0; transform: translateY(-4px); } |
| 87 | 87 | to { opacity: 1; transform: translateY(0); } |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | .fdp-pop-head { |
| 91 | 91 | display: flex; |
| 92 | 92 | align-items: center; |
| 93 | 93 | justify-content: space-between; |
| 94 | 94 | margin-bottom: 10px; |
| 95 | 95 | gap: 6px; |
| 96 | 96 | } |
| 97 | 97 | .fdp-pop-title { |
| 98 | 98 | flex: 1; |
| 99 | 99 | text-align: center; |
| 100 | 100 | font-weight: 700; |
| 101 | 101 | font-size: 13.5px; |
| 102 | 102 | letter-spacing: .3px; |
| 103 | 103 | color: #fff; |
| 104 | 104 | background: none; |
| 105 | 105 | border: 1px solid transparent; |
| 106 | 106 | border-radius: 6px; |
| 107 | 107 | padding: 4px 10px; |
| 108 | 108 | cursor: pointer; |
| 109 | 109 | transition: all .15s ease; |
| 110 | 110 | font-family: inherit; |
| 111 | 111 | } |
| 112 | 112 | .fdp-pop-title:hover { background: var(--col-bg-2, #0e1822); border-color: var(--col-border, rgba(148,163,184,0.18)); } |
| 113 | 113 | .fdp-nav-btn { |
| 114 | 114 | background: var(--col-bg-2, #0e1822); |
| 115 | 115 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 116 | 116 | color: var(--col-text-2, #cbd5e1); |
| 117 | 117 | border-radius: 6px; |
| 118 | 118 | width: 28px; height: 28px; |
| 119 | 119 | display: flex; align-items: center; justify-content: center; |
| 120 | 120 | cursor: pointer; |
| 121 | 121 | transition: all .15s ease; |
| 122 | 122 | padding: 0; |
| 123 | 123 | } |
| 124 | 124 | .fdp-nav-btn:hover { color: var(--col-brand, #06b6d4); border-color: var(--col-brand, #06b6d4); } |
| 125 | 125 | .fdp-nav-btn svg { width: 12px; height: 12px; } |
| 126 | 126 | |
| 127 | 127 | .fdp-dows { |
| 128 | 128 | display: grid; |
| 129 | 129 | grid-template-columns: repeat(7, 1fr); |
| 130 | 130 | gap: 2px; |
| 131 | 131 | margin-bottom: 4px; |
| 132 | 132 | } |
| 133 | 133 | .fdp-dow { |
| 134 | 134 | font-size: 10px; |
| 135 | 135 | font-weight: 700; |
| 136 | 136 | letter-spacing: 1px; |
| 137 | 137 | text-transform: uppercase; |
| 138 | 138 | text-align: center; |
| 139 | 139 | color: var(--col-text-3, #64748b); |
| 140 | 140 | padding: 4px 0; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | .fdp-grid { |
| 144 | 144 | display: grid; |
| 145 | 145 | grid-template-columns: repeat(7, 1fr); |
| 146 | 146 | gap: 2px; |
| 147 | 147 | } |
| 148 | 148 | .fdp-cell { |
| 149 | 149 | aspect-ratio: 1; |
| 150 | 150 | display: flex; |
| 151 | 151 | align-items: center; |
| 152 | 152 | justify-content: center; |
| 153 | 153 | font-size: 12px; |
| 154 | 154 | font-weight: 500; |
| 155 | 155 | color: var(--col-text-2, #cbd5e1); |
| 156 | 156 | background: transparent; |
| 157 | 157 | border: 1px solid transparent; |
| 158 | 158 | border-radius: 8px; |
| 159 | 159 | cursor: pointer; |
| 160 | 160 | transition: all .12s ease; |
| 161 | 161 | position: relative; |
| 162 | 162 | font-variant-numeric: tabular-nums; |
| 163 | 163 | padding: 0; |
| 164 | 164 | font-family: inherit; |
| 165 | 165 | } |
| 166 | 166 | .fdp-cell:hover { background: var(--col-bg-2, #0e1822); color: #fff; } |
| 167 | 167 | .fdp-cell.outside { color: var(--col-text-3, #64748b); opacity: .5; } |
| 168 | 168 | .fdp-cell.today { |
| 169 | 169 | border-color: var(--col-border, rgba(148,163,184,0.25)); |
| 170 | 170 | color: var(--col-brand-2, #67e8f9); |
| 171 | 171 | font-weight: 700; |
| 172 | 172 | } |
| 173 | 173 | .fdp-cell.selected { |
| 174 | 174 | background: var(--col-brand, #06b6d4); |
| 175 | 175 | color: #0c1018; |
| 176 | 176 | font-weight: 700; |
| 177 | 177 | border-color: var(--col-brand, #06b6d4); |
| 178 | 178 | box-shadow: 0 0 0 2px rgba(6,182,212,0.25); |
| 179 | 179 | } |
| 180 | 180 | .fdp-cell.selected.today { color: #0c1018; } |
| 181 | 181 | .fdp-cell:focus { outline: none; box-shadow: 0 0 0 2px rgba(103,232,249,0.5); } |
| 182 | 182 | |
| 183 | 183 | /* Year/Month grid pickers */ |
| 184 | 184 | .fdp-yo { |
| 185 | 185 | display: grid; |
| 186 | 186 | grid-template-columns: repeat(4, 1fr); |
| 187 | 187 | gap: 6px; |
| 188 | 188 | padding: 4px 0; |
| 189 | 189 | } |
| 190 | 190 | .fdp-yo-cell { |
| 191 | 191 | padding: 10px 6px; |
| 192 | 192 | border-radius: 8px; |
| 193 | 193 | background: var(--col-bg-2, #0e1822); |
| 194 | 194 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 195 | 195 | color: var(--col-text-2, #cbd5e1); |
| 196 | 196 | text-align: center; |
| 197 | 197 | font-size: 12.5px; |
| 198 | 198 | font-weight: 600; |
| 199 | 199 | cursor: pointer; |
| 200 | 200 | transition: all .12s ease; |
| 201 | 201 | font-family: inherit; |
| 202 | 202 | } |
| 203 | 203 | .fdp-yo-cell:hover { color: #fff; border-color: var(--col-brand, #06b6d4); } |
| 204 | 204 | .fdp-yo-cell.selected { |
| 205 | 205 | background: var(--col-brand, #06b6d4); |
| 206 | 206 | color: #0c1018; |
| 207 | 207 | border-color: var(--col-brand, #06b6d4); |
| 208 | 208 | font-weight: 700; |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | .fdp-time { |
| 212 | 212 | margin-top: 12px; |
| 213 | 213 | padding-top: 12px; |
| 214 | 214 | border-top: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 215 | 215 | display: flex; |
| 216 | 216 | align-items: center; |
| 217 | 217 | gap: 6px; |
| 218 | 218 | flex-wrap: nowrap; |
| 219 | 219 | } |
| 220 | 220 | .fdp-time-label { |
| 221 | 221 | font-size: 10px; |
| 222 | 222 | letter-spacing: 1.2px; |
| 223 | 223 | text-transform: uppercase; |
| 224 | 224 | font-weight: 700; |
| 225 | 225 | color: var(--col-text-3, #64748b); |
| 226 | 226 | margin-right: 2px; |
| 227 | 227 | flex: 0 0 auto; |
| 228 | 228 | } |
| 229 | 229 | .fdp-tinput { |
| 230 | 230 | display: inline-flex; |
| 231 | 231 | align-items: center; |
| 232 | 232 | background: var(--col-bg-2, #0e1822); |
| 233 | 233 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 234 | 234 | border-radius: 7px; |
| 235 | 235 | overflow: hidden; |
| 236 | 236 | } |
| 237 | 237 | .fdp-tinput.focused { border-color: var(--col-brand, #06b6d4); box-shadow: 0 0 0 2px rgba(6,182,212,0.15); } |
| 238 | 238 | .fdp-tspin { |
| 239 | 239 | background: transparent; |
| 240 | 240 | border: none; |
| 241 | 241 | color: var(--col-text-3, #64748b); |
| 242 | 242 | width: 20px; height: 28px; |
| 243 | 243 | cursor: pointer; |
| 244 | 244 | font-size: 14px; |
| 245 | 245 | line-height: 1; |
| 246 | 246 | padding: 0; |
| 247 | 247 | transition: color .12s ease; |
| 248 | 248 | font-family: inherit; |
| 249 | 249 | } |
| 250 | 250 | .fdp-tspin:hover { color: var(--col-brand, #06b6d4); } |
| 251 | 251 | .fdp-tnum { |
| 252 | 252 | background: transparent; |
| 253 | 253 | border: none; |
| 254 | 254 | color: #fff; |
| 255 | 255 | width: 30px; |
| 256 | 256 | text-align: center; |
| 257 | 257 | font: inherit; |
| 258 | 258 | font-size: 13px; |
| 259 | 259 | font-weight: 600; |
| 260 | 260 | font-variant-numeric: tabular-nums; |
| 261 | 261 | outline: none; |
| 262 | 262 | -moz-appearance: textfield; |
| 263 | 263 | } |
| 264 | 264 | .fdp-tnum::-webkit-outer-spin-button, |
| 265 | 265 | .fdp-tnum::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; } |
| 266 | 266 | .fdp-tcolon { color: var(--col-text-3, #64748b); font-weight: 700; } |
| 267 | 267 | |
| 268 | 268 | .fdp-period { |
| 269 | 269 | display: inline-flex; |
| 270 | 270 | background: var(--col-bg-2, #0e1822); |
| 271 | 271 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 272 | 272 | border-radius: 7px; |
| 273 | 273 | overflow: hidden; |
| 274 | 274 | margin-left: auto; |
| 275 | 275 | flex: 0 0 auto; |
| 276 | 276 | } |
| 277 | 277 | .fdp-pseg { |
| 278 | 278 | background: transparent; |
| 279 | 279 | border: none; |
| 280 | 280 | color: var(--col-text-3, #64748b); |
| 281 | 281 | padding: 5px 7px; |
| 282 | 282 | font-size: 11px; |
| 283 | 283 | font-weight: 700; |
| 284 | 284 | letter-spacing: .5px; |
| 285 | 285 | cursor: pointer; |
| 286 | 286 | transition: all .12s ease; |
| 287 | 287 | font-family: inherit; |
| 288 | 288 | font-variant-numeric: tabular-nums; |
| 289 | 289 | } |
| 290 | 290 | .fdp-pseg:hover { color: #fff; } |
| 291 | 291 | .fdp-pseg.active { |
| 292 | 292 | background: var(--col-brand, #06b6d4); |
| 293 | 293 | color: #0c1018; |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | .fdp-quick { |
| 297 | 297 | display: flex; |
| 298 | 298 | gap: 4px; |
| 299 | 299 | margin-top: 10px; |
| 300 | 300 | flex-wrap: wrap; |
| 301 | 301 | } |
| 302 | 302 | .fdp-qbtn { |
| 303 | 303 | background: var(--col-bg-2, #0e1822); |
| 304 | 304 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 305 | 305 | color: var(--col-text-2, #cbd5e1); |
| 306 | 306 | font-size: 11px; |
| 307 | 307 | font-weight: 600; |
| 308 | 308 | border-radius: 6px; |
| 309 | 309 | padding: 4px 9px; |
| 310 | 310 | cursor: pointer; |
| 311 | 311 | transition: all .12s ease; |
| 312 | 312 | font-family: inherit; |
| 313 | 313 | } |
| 314 | 314 | .fdp-qbtn:hover { color: var(--col-brand, #06b6d4); border-color: var(--col-brand, #06b6d4); } |
| 315 | 315 | |
| 316 | 316 | .fdp-actions { |
| 317 | 317 | display: flex; |
| 318 | 318 | gap: 6px; |
| 319 | 319 | margin-top: 12px; |
| 320 | 320 | padding-top: 10px; |
| 321 | 321 | border-top: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 322 | 322 | justify-content: flex-end; |
| 323 | 323 | } |
| 324 | 324 | .fdp-act { |
| 325 | 325 | font-size: 12px; |
| 326 | 326 | font-weight: 600; |
| 327 | 327 | padding: 6px 14px; |
| 328 | 328 | border-radius: 7px; |
| 329 | 329 | cursor: pointer; |
| 330 | 330 | border: 1px solid var(--col-border, rgba(148,163,184,0.18)); |
| 331 | 331 | background: var(--col-bg-2, #0e1822); |
| 332 | 332 | color: var(--col-text-2, #cbd5e1); |
| 333 | 333 | transition: all .12s ease; |
| 334 | 334 | font-family: inherit; |
| 335 | 335 | } |
| 336 | 336 | .fdp-act:hover { color: #fff; border-color: var(--col-text-3, #64748b); } |
| 337 | 337 | .fdp-act.primary { |
| 338 | 338 | background: var(--col-brand, #06b6d4); |
| 339 | 339 | color: #0c1018; |
| 340 | 340 | border-color: var(--col-brand, #06b6d4); |
| 341 | 341 | } |
| 342 | 342 | .fdp-act.primary:hover { background: var(--col-brand-2, #67e8f9); border-color: var(--col-brand-2, #67e8f9); } |
| 343 | 343 | `; |
| 344 | 344 | var s = document.createElement('style'); |
| 345 | 345 | s.id = 'fdp-styles'; |
| 346 | 346 | s.appendChild(document.createTextNode(css)); |
| 347 | 347 | document.head.appendChild(s); |
| 348 | 348 | } |
| 349 | 349 | |
| 350 | 350 | // -------------------------------------------------------------------- |
| 351 | 351 | // Date utilities. All work on local time (matches HTML5 datetime-local). |
| 352 | 352 | // -------------------------------------------------------------------- |
| 353 | 353 | var MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December']; |
| 354 | 354 | var DOWS = ['Mo','Tu','We','Th','Fr','Sa','Su']; |
| 355 | 355 | |
| 356 | 356 | function pad(n) { return (n < 10 ? '0' : '') + n; } |
| 357 | 357 | // 24-hour -> 12-hour display. Returns { h: 1..12, period: 'AM'|'PM' }. |
| 358 | 358 | // Midnight = 12 AM, Noon = 12 PM. |
| 359 | 359 | function to12(h24) { |
| 360 | 360 | var p = h24 >= 12 ? 'PM' : 'AM'; |
| 361 | 361 | var h = h24 % 12; |
| 362 | 362 | if (h === 0) h = 12; |
| 363 | 363 | return { h: h, period: p }; |
| 364 | 364 | } |
| 365 | 365 | // 12-hour + period -> 24-hour (0..23). |
| 366 | 366 | function from12(h12, period) { |
| 367 | 367 | h12 = ((h12 - 1) % 12 + 12) % 12 + 1; // clamp to 1..12 |
| 368 | 368 | if (period === 'AM') return h12 === 12 ? 0 : h12; |
| 369 | 369 | return h12 === 12 ? 12 : h12 + 12; |
| 370 | 370 | } |
| 371 | 371 | function toInputString(d) { |
| 372 | 372 | if (!d) return ''; |
| 373 | 373 | return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + |
| 374 | 374 | 'T' + pad(d.getHours()) + ':' + pad(d.getMinutes()); |
| 375 | 375 | } |
| 376 | 376 | function toDisplayString(d) { |
| 377 | 377 | if (!d) return ''; |
| 378 | 378 | var t = to12(d.getHours()); |
| 379 | 379 | return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + |
| 380 | 380 | ' ' + pad(t.h) + ':' + pad(d.getMinutes()) + ' ' + t.period; |
| 381 | 381 | } |
| 382 | 382 | function parseInputString(s) { |
| 383 | 383 | // Accept YYYY-MM-DDTHH:MM, YYYY-MM-DD HH:MM, YYYY-MM-DDTHH:MM:SS, YYYY-MM-DD |
| 384 | 384 | if (!s) return null; |
| 385 | 385 | var m = String(s).match(/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::\d{2})?)?$/); |
| 386 | 386 | if (!m) return null; |
| 387 | 387 | var d = new Date(+m[1], +m[2] - 1, +m[3], m[4] ? +m[4] : 0, m[5] ? +m[5] : 0, 0); |
| 388 | 388 | return isNaN(d.getTime()) ? null : d; |
| 389 | 389 | } |
| 390 | 390 | function isSameDay(a, b) { |
| 391 | 391 | return a && b && a.getFullYear() === b.getFullYear() && |
| 392 | 392 | a.getMonth() === b.getMonth() && |
| 393 | 393 | a.getDate() === b.getDate(); |
| 394 | 394 | } |
| 395 | 395 | function startOfMonth(y, m) { return new Date(y, m, 1); } |
| 396 | 396 | |
| 397 | 397 | // -------------------------------------------------------------------- |
| 398 | 398 | // Picker class -- one instance per attached input. |
| 399 | 399 | // -------------------------------------------------------------------- |
| 400 | 400 | function Picker(input) { |
| 401 | 401 | this.input = input; |
| 402 | 402 | this.value = parseInputString(input.value); // Date | null |
| 403 | 403 | // The viewMonth controls the grid's displayed month/year. Defaults to |
| 404 | 404 | // the selected month, or the current month if no value. |
| 405 | 405 | var ref = this.value || new Date(); |
| 406 | 406 | this.viewYear = ref.getFullYear(); |
| 407 | 407 | this.viewMonth = ref.getMonth(); |
| 408 | 408 | this.viewMode = 'days'; // 'days' | 'months' | 'years' |
| 409 | 409 | this.yearOffset = 0; // for years-grid pagination (multiples of 12) |
| 410 | 410 | this.build(); |
| 411 | 411 | this.attach(); |
| 412 | 412 | } |
| 413 | 413 | Picker.prototype.build = function () { |
| 414 | 414 | // Hide the native input but keep it in DOM for form submission. |
| 415 | 415 | this.input.style.position = 'absolute'; |
| 416 | 416 | this.input.style.left = '-9999px'; |
| 417 | 417 | this.input.style.opacity = '0'; |
| 418 | 418 | this.input.style.pointerEvents = 'none'; |
| 419 | 419 | this.input.tabIndex = -1; |
| 420 | 420 | |
| 421 | 421 | var wrap = document.createElement('span'); |
| 422 | 422 | wrap.className = 'fdp-wrap'; |
| 423 | 423 | var display = document.createElement('button'); |
| 424 | 424 | display.type = 'button'; |
| 425 | 425 | display.className = 'fdp-display' + (this.value ? ' has-value' : ' empty'); |
| 426 | 426 | display.textContent = this.value ? toDisplayString(this.value) : 'mm/dd/yyyy --:--'; |
| 427 | 427 | this.display = display; |
| 428 | 428 | this.wrap = wrap; |
| 429 | 429 | wrap.appendChild(display); |
| 430 | 430 | |
| 431 | 431 | this.input.parentNode.insertBefore(wrap, this.input); |
| 432 | 432 | wrap.appendChild(this.input); // move native input into wrap so layout stays |
| 433 | 433 | |
| 434 | 434 | var pop = document.createElement('div'); |
| 435 | 435 | pop.className = 'fdp-pop'; |
| 436 | 436 | pop.style.display = 'none'; |
| 437 | 437 | pop.setAttribute('role', 'dialog'); |
| 438 | 438 | pop.addEventListener('mousedown', function (e) { e.stopPropagation(); }); |
| 439 | 439 | pop.addEventListener('click', function (e) { e.stopPropagation(); }); |
| 440 | 440 | wrap.appendChild(pop); |
| 441 | 441 | this.pop = pop; |
| 442 | 442 | }; |
| 443 | 443 | Picker.prototype.attach = function () { |
| 444 | 444 | var self = this; |
| 445 | 445 | this.display.addEventListener('click', function (e) { e.preventDefault(); self.toggle(); }); |
| 446 | 446 | this.display.addEventListener('keydown', function (e) { |
| 447 | 447 | if (e.key === 'Enter' || e.key === ' ' || e.key === 'ArrowDown') { |
| 448 | 448 | e.preventDefault(); |
| 449 | 449 | self.open(); |
| 450 | 450 | } |
| 451 | 451 | }); |
| 452 | 452 | }; |
| 453 | 453 | Picker.prototype.open = function () { |
| 454 | 454 | if (this.isOpen) return; |
| 455 | 455 | this.isOpen = true; |
| 456 | 456 | this.wrap.classList.add('is-open'); |
| 457 | 457 | this.render(); |
| 458 | 458 | this.pop.style.display = 'block'; |
| 459 | 459 | // Flip up if not enough room below. |
| 460 | 460 | var rect = this.pop.getBoundingClientRect(); |
| 461 | 461 | var below = window.innerHeight - rect.top; |
| 462 | 462 | if (below < rect.height + 8) { |
| 463 | 463 | this.pop.classList.add('flip-up'); |
| 464 | 464 | } else { |
| 465 | 465 | this.pop.classList.remove('flip-up'); |
| 466 | 466 | } |
| 467 | 467 | document.addEventListener('mousedown', this._outside = this._outside || this.handleOutside.bind(this)); |
| 468 | 468 | document.addEventListener('keydown', this._keydown = this._keydown || this.handleKeydown.bind(this)); |
| 469 | 469 | }; |
| 470 | 470 | Picker.prototype.close = function () { |
| 471 | 471 | if (!this.isOpen) return; |
| 472 | 472 | this.isOpen = false; |
| 473 | 473 | this.wrap.classList.remove('is-open'); |
| 474 | 474 | this.pop.style.display = 'none'; |
| 475 | 475 | document.removeEventListener('mousedown', this._outside); |
| 476 | 476 | document.removeEventListener('keydown', this._keydown); |
| 477 | 477 | }; |
| 478 | 478 | Picker.prototype.toggle = function () { this.isOpen ? this.close() : this.open(); }; |
| 479 | 479 | Picker.prototype.handleOutside = function (e) { |
| 480 | 480 | if (!this.wrap.contains(e.target)) this.close(); |
| 481 | 481 | }; |
| 482 | 482 | Picker.prototype.handleKeydown = function (e) { |
| 483 | 483 | if (e.key === 'Escape') { e.preventDefault(); this.close(); this.display.focus(); return; } |
| 484 | 484 | if (this.viewMode !== 'days') return; |
| 485 | 485 | // Arrow nav on the day grid |
| 486 | 486 | var d = this.value || new Date(this.viewYear, this.viewMonth, 1); |
| 487 | 487 | var moved = null; |
| 488 | 488 | if (e.key === 'ArrowLeft') moved = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1, d.getHours(), d.getMinutes()); |
| 489 | 489 | if (e.key === 'ArrowRight') moved = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1, d.getHours(), d.getMinutes()); |
| 490 | 490 | if (e.key === 'ArrowUp') moved = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 7, d.getHours(), d.getMinutes()); |
| 491 | 491 | if (e.key === 'ArrowDown') moved = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 7, d.getHours(), d.getMinutes()); |
| 492 | 492 | if (e.key === 'PageUp') moved = new Date(d.getFullYear(), d.getMonth() - 1, d.getDate(), d.getHours(), d.getMinutes()); |
| 493 | 493 | if (e.key === 'PageDown') moved = new Date(d.getFullYear(), d.getMonth() + 1, d.getDate(), d.getHours(), d.getMinutes()); |
| 494 | 494 | if (e.key === 'Enter') { e.preventDefault(); this.commit(); return; } |
| 495 | 495 | if (moved) { |
| 496 | 496 | e.preventDefault(); |
| 497 | 497 | this.value = moved; |
| 498 | 498 | this.viewYear = moved.getFullYear(); |
| 499 | 499 | this.viewMonth = moved.getMonth(); |
| 500 | 500 | this.render(); |
| 501 | 501 | } |
| 502 | 502 | }; |
| 503 | 503 | Picker.prototype.shiftMonth = function (delta) { |
| 504 | 504 | var d = new Date(this.viewYear, this.viewMonth + delta, 1); |
| 505 | 505 | this.viewYear = d.getFullYear(); |
| 506 | 506 | this.viewMonth = d.getMonth(); |
| 507 | 507 | this.render(); |
| 508 | 508 | }; |
| 509 | 509 | Picker.prototype.commit = function () { |
| 510 | 510 | if (!this.value) this.value = new Date(); |
| 511 | 511 | this.input.value = toInputString(this.value); |
| 512 | 512 | this.display.textContent = toDisplayString(this.value); |
| 513 | 513 | this.display.classList.remove('empty'); |
| 514 | 514 | this.display.classList.add('has-value'); |
| 515 | 515 | // Fire input + change for any framework / form listeners. |
| 516 | 516 | this.input.dispatchEvent(new Event('input', { bubbles: true })); |
| 517 | 517 | this.input.dispatchEvent(new Event('change', { bubbles: true })); |
| 518 | 518 | this.close(); |
| 519 | 519 | }; |
| 520 | 520 | Picker.prototype.clear = function () { |
| 521 | 521 | this.value = null; |
| 522 | 522 | this.input.value = ''; |
| 523 | 523 | this.display.textContent = 'mm/dd/yyyy --:--'; |
| 524 | 524 | this.display.classList.add('empty'); |
| 525 | 525 | this.display.classList.remove('has-value'); |
| 526 | 526 | this.input.dispatchEvent(new Event('input', { bubbles: true })); |
| 527 | 527 | this.input.dispatchEvent(new Event('change', { bubbles: true })); |
| 528 | 528 | this.render(); |
| 529 | 529 | }; |
| 530 | 530 | Picker.prototype.setQuick = function (kind) { |
| 531 | 531 | var now = new Date(); |
| 532 | 532 | if (kind === 'today') this.value = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0); |
| 533 | 533 | if (kind === 'now') this.value = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes()); |
| 534 | 534 | if (kind === 'yesterday') this.value = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1, 0, 0); |
| 535 | 535 | this.viewYear = this.value.getFullYear(); |
| 536 | 536 | this.viewMonth = this.value.getMonth(); |
| 537 | 537 | this.render(); |
| 538 | 538 | }; |
| 539 | 539 | |
| 540 | 540 | // ------------------------------------------------------------------ |
| 541 | 541 | // Render -- repaints the popover contents based on view mode. |
| 542 | 542 | // ------------------------------------------------------------------ |
| 543 | 543 | Picker.prototype.render = function () { |
| 544 | 544 | var html = ''; |
| 545 | 545 | if (this.viewMode === 'days') html = this.renderDays(); |
| 546 | 546 | if (this.viewMode === 'months') html = this.renderMonths(); |
| 547 | 547 | if (this.viewMode === 'years') html = this.renderYears(); |
| 548 | 548 | this.pop.innerHTML = html; |
| 549 | 549 | this.bindPop(); |
| 550 | 550 | }; |
| 551 | 551 | Picker.prototype.renderDays = function () { |
| 552 | 552 | var y = this.viewYear, m = this.viewMonth; |
| 553 | 553 | var first = startOfMonth(y, m); |
| 554 | 554 | // first day-of-week (Mon=0) |
| 555 | 555 | var dow = (first.getDay() + 6) % 7; |
| 556 | 556 | var daysInMonth = new Date(y, m + 1, 0).getDate(); |
| 557 | 557 | var prevMonth = new Date(y, m, 0); |
| 558 | 558 | var daysPrev = prevMonth.getDate(); |
| 559 | 559 | |
| 560 | 560 | var cells = []; |
| 561 | 561 | // Lead-in (previous month days) |
| 562 | 562 | for (var i = dow - 1; i >= 0; i--) { |
| 563 | 563 | cells.push({ y: prevMonth.getFullYear(), m: prevMonth.getMonth(), d: daysPrev - i, outside: true }); |
| 564 | 564 | } |
| 565 | 565 | // Current month days |
| 566 | 566 | for (var d = 1; d <= daysInMonth; d++) { |
| 567 | 567 | cells.push({ y: y, m: m, d: d, outside: false }); |
| 568 | 568 | } |
| 569 | 569 | // Trail (next month) to fill the grid (6 rows of 7 = 42 cells) |
| 570 | 570 | var nextDay = 1; |
| 571 | 571 | while (cells.length < 42) { |
| 572 | 572 | var nm = new Date(y, m + 1, 1); |
| 573 | 573 | cells.push({ y: nm.getFullYear(), m: nm.getMonth(), d: nextDay++, outside: true }); |
| 574 | 574 | } |
| 575 | 575 | |
| 576 | 576 | var today = new Date(); |
| 577 | 577 | var self = this; |
| 578 | 578 | var cellHtml = cells.map(function (c) { |
| 579 | 579 | var cd = new Date(c.y, c.m, c.d); |
| 580 | 580 | var classes = 'fdp-cell'; |
| 581 | 581 | if (c.outside) classes += ' outside'; |
| 582 | 582 | if (isSameDay(cd, today)) classes += ' today'; |
| 583 | 583 | if (isSameDay(cd, self.value)) classes += ' selected'; |
| 584 | 584 | return '<button type="button" class="' + classes + '" ' + |
| 585 | 585 | 'data-y="' + c.y + '" data-m="' + c.m + '" data-d="' + c.d + '">' + c.d + '</button>'; |
| 586 | 586 | }).join(''); |
| 587 | 587 | |
| 588 | 588 | var dowHtml = DOWS.map(function (s) { return '<div class="fdp-dow">' + s + '</div>'; }).join(''); |
| 589 | 589 | |
| 590 | 590 | return '' + |
| 591 | 591 | '<div class="fdp-pop-head">' + |
| 592 | 592 | '<button type="button" class="fdp-nav-btn" data-act="prev" aria-label="Previous month">' + |
| 593 | 593 | '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>' + |
| 594 | 594 | '</button>' + |
| 595 | 595 | '<button type="button" class="fdp-pop-title" data-act="months">' + MONTHS[m] + ' ' + y + '</button>' + |
| 596 | 596 | '<button type="button" class="fdp-nav-btn" data-act="next" aria-label="Next month">' + |
| 597 | 597 | '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>' + |
| 598 | 598 | '</button>' + |
| 599 | 599 | '</div>' + |
| 600 | 600 | '<div class="fdp-dows">' + dowHtml + '</div>' + |
| 601 | 601 | '<div class="fdp-grid">' + cellHtml + '</div>' + |
| 602 | 602 | this.renderTime() + |
| 603 | 603 | this.renderQuick() + |
| 604 | 604 | this.renderActions(); |
| 605 | 605 | }; |
| 606 | 606 | Picker.prototype.renderMonths = function () { |
| 607 | 607 | var self = this; |
| 608 | 608 | var cells = MONTHS.map(function (mn, i) { |
| 609 | 609 | var sel = (self.value && self.value.getMonth() === i && self.value.getFullYear() === self.viewYear) || |
| 610 | 610 | (!self.value && self.viewMonth === i); |
| 611 | 611 | return '<button type="button" class="fdp-yo-cell' + (sel ? ' selected' : '') + '" data-act="pickmonth" data-m="' + i + '">' + mn.slice(0, 3) + '</button>'; |
| 612 | 612 | }).join(''); |
| 613 | 613 | return '' + |
| 614 | 614 | '<div class="fdp-pop-head">' + |
| 615 | 615 | '<button type="button" class="fdp-nav-btn" data-act="prevyear" aria-label="Previous year">' + |
| 616 | 616 | '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>' + |
| 617 | 617 | '</button>' + |
| 618 | 618 | '<button type="button" class="fdp-pop-title" data-act="years">' + this.viewYear + '</button>' + |
| 619 | 619 | '<button type="button" class="fdp-nav-btn" data-act="nextyear" aria-label="Next year">' + |
| 620 | 620 | '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>' + |
| 621 | 621 | '</button>' + |
| 622 | 622 | '</div>' + |
| 623 | 623 | '<div class="fdp-yo">' + cells + '</div>' + |
| 624 | 624 | this.renderTime() + |
| 625 | 625 | this.renderActions(); |
| 626 | 626 | }; |
| 627 | 627 | Picker.prototype.renderYears = function () { |
| 628 | 628 | var self = this; |
| 629 | 629 | // Show a 12-year grid centered on viewYear + yearOffset. |
| 630 | 630 | var base = (Math.floor((this.viewYear + this.yearOffset) / 12)) * 12; |
| 631 | 631 | var cells = []; |
| 632 | 632 | for (var i = 0; i < 12; i++) { |
| 633 | 633 | var yy = base + i; |
| 634 | 634 | var sel = self.value && self.value.getFullYear() === yy; |
| 635 | 635 | cells.push('<button type="button" class="fdp-yo-cell' + (sel ? ' selected' : '') + '" data-act="pickyear" data-y="' + yy + '">' + yy + '</button>'); |
| 636 | 636 | } |
| 637 | 637 | return '' + |
| 638 | 638 | '<div class="fdp-pop-head">' + |
| 639 | 639 | '<button type="button" class="fdp-nav-btn" data-act="prevdecade" aria-label="Previous 12 years">' + |
| 640 | 640 | '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"/></svg>' + |
| 641 | 641 | '</button>' + |
| 642 | 642 | '<div class="fdp-pop-title" style="cursor:default">' + base + ' – ' + (base + 11) + '</div>' + |
| 643 | 643 | '<button type="button" class="fdp-nav-btn" data-act="nextdecade" aria-label="Next 12 years">' + |
| 644 | 644 | '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"/></svg>' + |
| 645 | 645 | '</button>' + |
| 646 | 646 | '</div>' + |
| 647 | 647 | '<div class="fdp-yo">' + cells.join('') + '</div>' + |
| 648 | 648 | this.renderActions(); |
| 649 | 649 | }; |
| 650 | 650 | Picker.prototype.renderTime = function () { |
| 651 | 651 | var t = to12(this.value ? this.value.getHours() : 0); |
| 652 | 652 | var hh = pad(t.h); |
| 653 | 653 | var mm = this.value ? pad(this.value.getMinutes()) : '00'; |
| 654 | 654 | var pAm = t.period === 'AM'; |
| 655 | 655 | return '' + |
| 656 | 656 | '<div class="fdp-time">' + |
| 657 | 657 | '<span class="fdp-time-label">Time</span>' + |
| 658 | 658 | '<div class="fdp-tinput" data-time="h">' + |
| 659 | 659 | '<button type="button" class="fdp-tspin" data-act="hdec" aria-label="Decrease hour">▾</button>' + |
| 660 | 660 | '<input class="fdp-tnum" type="text" data-field="h" value="' + hh + '" maxlength="2" inputmode="numeric">' + |
| 661 | 661 | '<button type="button" class="fdp-tspin" data-act="hinc" aria-label="Increase hour">▴</button>' + |
| 662 | 662 | '</div>' + |
| 663 | 663 | '<span class="fdp-tcolon">:</span>' + |
| 664 | 664 | '<div class="fdp-tinput" data-time="m">' + |
| 665 | 665 | '<button type="button" class="fdp-tspin" data-act="mdec" aria-label="Decrease minute">▾</button>' + |
| 666 | 666 | '<input class="fdp-tnum" type="text" data-field="m" value="' + mm + '" maxlength="2" inputmode="numeric">' + |
| 667 | 667 | '<button type="button" class="fdp-tspin" data-act="minc" aria-label="Increase minute">▴</button>' + |
| 668 | 668 | '</div>' + |
| 669 | 669 | '<div class="fdp-period" role="group" aria-label="AM or PM">' + |
| 670 | 670 | '<button type="button" class="fdp-pseg' + (pAm ? ' active' : '') + '" data-act="setam">AM</button>' + |
| 671 | 671 | '<button type="button" class="fdp-pseg' + (pAm ? '' : ' active') + '" data-act="setpm">PM</button>' + |
| 672 | 672 | '</div>' + |
| 673 | 673 | '</div>'; |
| 674 | 674 | }; |
| 675 | 675 | Picker.prototype.renderQuick = function () { |
| 676 | 676 | return '' + |
| 677 | 677 | '<div class="fdp-quick">' + |
| 678 | 678 | '<button type="button" class="fdp-qbtn" data-quick="today">Today</button>' + |
| 679 | 679 | '<button type="button" class="fdp-qbtn" data-quick="yesterday">Yesterday</button>' + |
| 680 | 680 | '<button type="button" class="fdp-qbtn" data-quick="now">Now</button>' + |
| 681 | 681 | '</div>'; |
| 682 | 682 | }; |
| 683 | 683 | Picker.prototype.renderActions = function () { |
| 684 | 684 | return '' + |
| 685 | 685 | '<div class="fdp-actions">' + |
| 686 | 686 | '<button type="button" class="fdp-act" data-act="clear">Clear</button>' + |
| 687 | 687 | '<button type="button" class="fdp-act primary" data-act="apply">Apply</button>' + |
| 688 | 688 | '</div>'; |
| 689 | 689 | }; |
| 690 | 690 | |
| 691 | 691 | // ------------------------------------------------------------------ |
| 692 | 692 | // Bind all interactive elements in the rendered popover. |
| 693 | 693 | // ------------------------------------------------------------------ |
| 694 | 694 | Picker.prototype.bindPop = function () { |
| 695 | 695 | var self = this; |
| 696 | 696 | // Cell click (day select) |
| 697 | 697 | this.pop.querySelectorAll('.fdp-cell').forEach(function (el) { |
| 698 | 698 | el.addEventListener('click', function () { |
| 699 | 699 | var y = +el.dataset.y, m = +el.dataset.m, d = +el.dataset.d; |
| 700 | 700 | var h = self.value ? self.value.getHours() : 0; |
| 701 | 701 | var mi = self.value ? self.value.getMinutes() : 0; |
| 702 | 702 | self.value = new Date(y, m, d, h, mi); |
| 703 | 703 | // If user clicked outside-month cell, advance the view too |
| 704 | 704 | if (m !== self.viewMonth || y !== self.viewYear) { |
| 705 | 705 | self.viewYear = y; |
| 706 | 706 | self.viewMonth = m; |
| 707 | 707 | } |
| 708 | 708 | self.render(); |
| 709 | 709 | }); |
| 710 | 710 | }); |
| 711 | 711 | // Nav buttons / mode switches |
| 712 | 712 | this.pop.querySelectorAll('[data-act]').forEach(function (el) { |
| 713 | 713 | el.addEventListener('click', function () { |
| 714 | 714 | var act = el.dataset.act; |
| 715 | 715 | if (act === 'prev') self.shiftMonth(-1); |
| 716 | 716 | else if (act === 'next') self.shiftMonth(+1); |
| 717 | 717 | else if (act === 'prevyear') { self.viewYear--; self.render(); } |
| 718 | 718 | else if (act === 'nextyear') { self.viewYear++; self.render(); } |
| 719 | 719 | else if (act === 'prevdecade') { self.yearOffset -= 12; self.render(); } |
| 720 | 720 | else if (act === 'nextdecade') { self.yearOffset += 12; self.render(); } |
| 721 | 721 | else if (act === 'months') { self.viewMode = 'months'; self.render(); } |
| 722 | 722 | else if (act === 'years') { self.viewMode = 'years'; self.render(); } |
| 723 | 723 | else if (act === 'pickmonth') { |
| 724 | 724 | self.viewMonth = +el.dataset.m; |
| 725 | 725 | self.viewMode = 'days'; |
| 726 | 726 | self.render(); |
| 727 | 727 | } |
| 728 | 728 | else if (act === 'pickyear') { |
| 729 | 729 | self.viewYear = +el.dataset.y; |
| 730 | 730 | self.viewMode = 'months'; |
| 731 | 731 | self.render(); |
| 732 | 732 | } |
| 733 | 733 | else if (act === 'apply') self.commit(); |
| 734 | 734 | else if (act === 'clear') self.clear(); |
| 735 | 735 | else if (act === 'hinc') self.bumpTime('h', +1); |
| 736 | 736 | else if (act === 'hdec') self.bumpTime('h', -1); |
| 737 | 737 | else if (act === 'minc') self.bumpTime('m', +1); |
| 738 | 738 | else if (act === 'mdec') self.bumpTime('m', -1); |
| 739 | 739 | else if (act === 'setam') self.setPeriod('AM'); |
| 740 | 740 | else if (act === 'setpm') self.setPeriod('PM'); |
| 741 | 741 | }); |
| 742 | 742 | }); |
| 743 | 743 | // Quick buttons |
| 744 | 744 | this.pop.querySelectorAll('[data-quick]').forEach(function (el) { |
| 745 | 745 | el.addEventListener('click', function () { self.setQuick(el.dataset.quick); }); |
| 746 | 746 | }); |
| 747 | 747 | // Time inputs (live typing + wheel scroll) |
| 748 | 748 | this.pop.querySelectorAll('.fdp-tnum').forEach(function (inp) { |
| 749 | 749 | var field = inp.dataset.field; |
| 750 | 750 | inp.addEventListener('input', function () { |
| 751 | 751 | // Strip non-digits, clamp. Hour input is 12-hour (1..12); |
| 752 | 752 | // preserve the current AM/PM period when converting to 24-hour storage. |
| 753 | 753 | var n = parseInt(inp.value.replace(/[^0-9]/g, ''), 10); |
| 754 | 754 | if (isNaN(n)) n = 0; |
| 755 | 755 | if (!self.value) self.value = new Date(self.viewYear, self.viewMonth, 1, 0, 0); |
| 756 | 756 | if (field === 'h') { |
| 757 | 757 | if (n < 1) n = 1; |
| 758 | 758 | if (n > 12) n = 12; |
| 759 | 759 | var period = to12(self.value.getHours()).period; |
| 760 | 760 | self.value.setHours(from12(n, period)); |
| 761 | 761 | } |
| 762 | 762 | if (field === 'm') { |
| 763 | 763 | if (n < 0) n = 0; |
| 764 | 764 | if (n > 59) n = 59; |
| 765 | 765 | self.value.setMinutes(n); |
| 766 | 766 | } |
| 767 | 767 | }); |
| 768 | 768 | inp.addEventListener('blur', function () { |
| 769 | 769 | // Re-pad after blur. Hour shows 12-hour; minute shows 24-hour digit value. |
| 770 | 770 | if (field === 'h' && self.value) inp.value = pad(to12(self.value.getHours()).h); |
| 771 | 771 | else inp.value = pad(parseInt(inp.value || '0', 10)); |
| 772 | 772 | }); |
| 773 | 773 | inp.addEventListener('focus', function () { |
| 774 | 774 | inp.parentNode.classList.add('focused'); |
| 775 | 775 | inp.select(); |
| 776 | 776 | }); |
| 777 | 777 | inp.addEventListener('blur', function () { inp.parentNode.classList.remove('focused'); }); |
| 778 | 778 | inp.addEventListener('wheel', function (e) { |
| 779 | 779 | e.preventDefault(); |
| 780 | 780 | self.bumpTime(field, e.deltaY < 0 ? +1 : -1); |
| 781 | 781 | }, { passive: false }); |
| 782 | 782 | }); |
| 783 | 783 | }; |
| 784 | 784 | Picker.prototype.bumpTime = function (field, delta) { |
| 785 | 785 | if (!this.value) this.value = new Date(this.viewYear, this.viewMonth, 1, 0, 0); |
| 786 | 786 | if (field === 'h') { |
| 787 | 787 | var h = (this.value.getHours() + delta + 24) % 24; |
| 788 | 788 | this.value.setHours(h); |
| 789 | 789 | } |
| 790 | 790 | if (field === 'm') { |
| 791 | 791 | var m = (this.value.getMinutes() + delta + 60) % 60; |
| 792 | 792 | this.value.setMinutes(m); |
| 793 | 793 | } |
| 794 | 794 | this.render(); |
| 795 | 795 | }; |
| 796 | 796 | Picker.prototype.setPeriod = function (period) { |
| 797 | 797 | if (!this.value) this.value = new Date(this.viewYear, this.viewMonth, 1, 0, 0); |
| 798 | 798 | var cur = to12(this.value.getHours()); |
| 799 | 799 | if (cur.period === period) { this.render(); return; } |
| 800 | 800 | this.value.setHours(from12(cur.h, period)); |
| 801 | 801 | this.render(); |
| 802 | 802 | }; |
| 803 | 803 | |
| 804 | 804 | // -------------------------------------------------------------------- |
| 805 | 805 | // Auto-init. |
| 806 | 806 | // -------------------------------------------------------------------- |
| 807 | 807 | function init() { |
| 808 | 808 | injectStyles(); |
| 809 | 809 | var all = document.querySelectorAll('input[type="datetime-local"][data-fancy]'); |
| 810 | 810 | Array.prototype.forEach.call(all, function (input) { |
| 811 | 811 | if (input.__fdp) return; |
| 812 | 812 | input.__fdp = new Picker(input); |
| 813 | 813 | }); |
| 814 | 814 | } |
| 815 | 815 | |
| 816 | 816 | if (document.readyState === 'loading') { |
| 817 | 817 | document.addEventListener('DOMContentLoaded', init); |
| 818 | 818 | } else { |
| 819 | 819 | init(); |
| 820 | 820 | } |
| 821 | 821 | |
| 822 | 822 | // Expose for manual init if scripts add inputs later. |
| 823 | 823 | window.FancyDatePicker = { init: init, Picker: Picker }; |
| 824 | 824 | })(); |