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

Does not works in Bootstrap modal #16

Open
rez0n opened this issue Jul 25, 2023 · 1 comment
Open

Does not works in Bootstrap modal #16

rez0n opened this issue Jul 25, 2023 · 1 comment

Comments

@rez0n
Copy link

rez0n commented Jul 25, 2023

Hi @monim67
Thanks for this module!
I faced with issue that it does not works in the Bootstrap modal as initialization JS waiting for DOMContentLoaded event, but modal does not fires this event.

django-flatpickr.js:53

  document.addEventListener('DOMContentLoaded', function (event) {
    findAndProcessFlatpickrInputs(document);
    document.addEventListener('DOMNodeInserted', function (event) {
      setTimeout(() => {
        if (event.target.querySelectorAll) findAndProcessFlatpickrInputs(event.target);
      });
    });
  });

Modifying django-flatpickr.js:53 this way - everything becamoe work correctly, I created another copy of this file to use in modals, would be cool to find solution for both cases (DOMContentLoaded and shown.bs.modal) but I have no ideas right now.

document.addEventListener('shown.bs.modal', function (event) {
    findAndProcessFlatpickrInputs(document);
    document.addEventListener('DOMNodeInserted', function (event) {
      setTimeout(() => {
        if (event.target.querySelectorAll) findAndProcessFlatpickrInputs(event.target);
      });
    });
  });
@pakal
Copy link

pakal commented Mar 22, 2024

I encountered the same problem, and had to put a local copy of django-flatpickr.js (in static/vendor/django-flatpickr-2/js/django-flatpickr.js), that I referenced in config like so:

DJANGO_FLATPICKR = {
    "app_static_url": STATIC_URL + "vendor/django-flatpickr-2/",
}

I tweaked the code to support initialization on both MODAL and DOCUMENT loadings :

  function setupEventForFindAndProcessFlatpickrInputs() {
    if (window.isFlatpickrHandlerInitialized) return;
    window.isFlatpickrHandlerInitialized = true;

    findAndProcessFlatpickrInputs(document);
    document.addEventListener('DOMNodeInserted', function (event) {
      setTimeout(() => {
        if (event.target.querySelectorAll) findAndProcessFlatpickrInputs(event.target);
      });
    });
  }

  document.addEventListener('DOMContentLoaded', function (event) {  // Setup for initial page loading
    setTimeout(() => { setupEventForFindAndProcessFlatpickrInputs(); });
  });
  document.addEventListener('shown.bs.modal', function (event) {  // Setup for form in modal
    setTimeout(() => { setupEventForFindAndProcessFlatpickrInputs(); });
  });

PS : I added setTimeout() because locale is still loaded from CDN AFTER this script, so I needed this short delay to avoid a "locale not found" error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants