Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading before DOMContentLoaded #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions date-input-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,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 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.js';
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 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 @@ -329,6 +324,4 @@ class Picker {
}
}

window.thePicker = new Picker();

export default window.thePicker;
export default Picker;