1import { Controller } from '@hotwired/stimulus' 2import { 3 claimRun, 4 cancelRun, 5 teardownBeforeCache, 6 stopTeardownBeforeCache, 7} from 'src/lazy_widget' 8 9export default class extends Controller { 10 static values = { 11 time: { type: Boolean, default: false }, 12 minDate: { type: String, default: '' }, 13 // Renders the calendar inline next to the input instead of appending it 14 // to document.body. Required inside fixed-position containers (modals), 15 // where body-appended calendars get positioned off-screen. 16 static: { type: Boolean, default: false }, 17 } 18 19 async connect() { 20 // static mode wraps the input in a new element, which moves it in the 21 // DOM and re-triggers connect(); without this guard that loops forever. 22 if (this.element._flatpickr) return 23 // The page is on its way into Turbo's snapshot cache - see src/lazy_widget. 24 if (this.tornDownForCache) return 25 26 // See src/lazy_widget: bail if a later connect() or a disconnect() overtook 27 // this run while the import was in flight. 28 const module = await claimRun(this, () => import('flatpickr')) 29 if (!module) return 30 // ...and a concurrent run may still have built one while we waited. 31 if (this.element._flatpickr) return 32 33 const options = this.timeValue 34 ? { dateFormat: 'Y-m-d H:i', enableTime: true } 35 : { dateFormat: 'Y-m-d' } 36 if (this.minDateValue) options.minDate = this.minDateValue 37 if (this.staticValue) options.static = true 38 module.default(this.element, options) 39 40 teardownBeforeCache(this) 41 } 42 43 disconnect() { 44 // Both building and destroying a static calendar move the input (into and 45 // out of flatpickr's wrapper), which makes Stimulus fire disconnect and 46 // then connect again. Tearing down on a move would unwrap, reconnect, 47 // rewrap, forever - so only tear down once the input has really left the 48 // page, which is the one case where it is detached by the time we run. 49 if (this.element.isConnected) return 50 51 cancelRun(this) 52 stopTeardownBeforeCache(this) 53 this.teardown() 54 } 55 56 // Without this the calendar flatpickr appends to document.body outlives the 57 // input it belongs to, on every Turbo navigation away from the page. 58 teardown() { 59 this.element._flatpickr?.destroy() 60 // 4.6.13's destroy() leaves the dead instance on the element, and the 61 // guard at the top of connect() reads exactly that property. 62 this.element._flatpickr = undefined 63 } 64}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.