Skip to content

Commit

Permalink
Loading before DOM content has laoded
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Nov 3, 2020
1 parent a6f4f91 commit e73a087
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
25 changes: 15 additions & 10 deletions src/date-input-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ const addPickers = () => {
}
}

// Run the above code on any <input type="date"> in the document, also on dynamically created ones.
addPickers()

document.addEventListener('DOMContentLoaded', () => {
const init = () => {
// Run the above code on any <input type="date"> in the document, also on dynamically created ones.
addPickers()
})
// This is also on mousedown event so it will capture new inputs that might
// be added to the DOM dynamically.
document.querySelector('body').addEventListener('mousedown', () => {
addPickers()
})
}

// This is also on mousedown event so it will capture new inputs that might
// be added to the DOM dynamically.
document.querySelector('body').addEventListener('mousedown', () => {
addPickers()
})
if (document.readyState !== 'loading') {
init()
} else {
document.addEventListener('DOMContentLoaded', () => {
init()
})
}
9 changes: 8 additions & 1 deletion src/input.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import thePicker from './picker.js'
import locales from './locales.js'
import dateFormat from 'dateformat'
import Picker from './picker.js'

// This is a singleton.
let thePicker

export default class Input {
constructor (input) {
if (!thePicker) {
thePicker = new Picker()
}

this.element = input
this.element.setAttribute('data-has-picker', '')

Expand Down
9 changes: 1 addition & 8 deletions src/picker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
class Picker {
constructor () {
// This is a singleton.
if (window.thePicker) {
return window.thePicker
}

this.date = new Date()
this.input = null
this.isOpen = false
Expand Down Expand Up @@ -328,6 +323,4 @@ class Picker {
}
}

window.thePicker = new Picker()

export default window.thePicker
export default Picker

0 comments on commit e73a087

Please sign in to comment.