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

Improve formset initialization #19

Merged
merged 3 commits into from
Mar 25, 2024
Merged
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
5 changes: 0 additions & 5 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ if (typeof window.crispyFormsetModal !== "undefined") {
}
```

!!! warning

The `refresh` method will recreate all formsets in the page, because of that, it is not recommended to use it when only you need to recreate a specific formset.


## Events

Django Crispy Formset Modal provides two events that you can utilize to perform additional actions when forms are added or removed from the formset. These events are:
Expand Down
1 change: 1 addition & 0 deletions js/src/formset-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class FormsetModal {
this._configureSelectAllToggler();
this._refresh();
}
this.targetEl.setAttribute("data-formset-initialized", "true");
}
_getVariant() {
return this.targetEl.getAttribute("data-formset-variant");
Expand Down
19 changes: 13 additions & 6 deletions js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ window.crispyFormsetModal = {};
function configureFormsets() {
let formsets = document.querySelectorAll(".formset");
formsets.forEach(function (formsetEl) {
new FormsetModal(formsetEl.getAttribute("id"));
if (!formsetEl.hasAttribute("data-formset-initialized")) {
new FormsetModal(formsetEl.getAttribute("id"));
}
});
}

Expand Down Expand Up @@ -88,19 +90,24 @@ $.fn[pluginName] = function () {
}
};

window.crispyFormsetModal.init = function () {
injectStyles();
configureFormsets();
configureModals();
hookCalculatedFields();
executeAllCalculatedFields();
}

window.crispyFormsetModal.refresh = function () {
configureFormsets();
configureModals();
hookCalculatedFields();
executeAllCalculatedFields();
};

window.crispyFormsetModal.onFormAdded = function () {};
window.crispyFormsetModal.onFormDeleted = function () {};

ready(function () {
injectStyles();
configureModals();
hookCalculatedFields();
configureFormsets();
executeAllCalculatedFields();
window.crispyFormsetModal.init();
});
82 changes: 43 additions & 39 deletions js/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,45 +98,49 @@ export function removeChildren(a) {
}

export function injectStyles() {
const css = `
.cfm-selection-border {
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
width: 0.150rem;
background-color: rgb(220 53 69 / 1);
}
.cfm-modal-backdrop {
position: fixed;
inset: 0px;
z-index: 100;
background-color: rgb(31 41 55 / 1);
opacity: 0;
transition-property: opacity;
transition-duration: 300ms;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
.cfm-opacity-50 {
opacity: 0.5;
}
.cfm-opacity-100 {
opacity: 1;
}
.cfm-tr.selected{
background-color: rgba(0, 0, 0, 0.08) !important;
}
.cfm .modal-body {
max-height: calc(100vh - 150px);
overflow-x: auto;
}
.cfm-cursor-pointer {
cursor: pointer;
}
`;
const style = document.createElement("style");
style.innerHTML = css;
document.head.appendChild(style);
const styleId = "cfm-styles";
if (!document.getElementById(styleId)) {
const css = `
.cfm-selection-border {
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
width: 0.150rem;
background-color: rgb(220 53 69 / 1);
}
.cfm-modal-backdrop {
position: fixed;
inset: 0px;
z-index: 100;
background-color: rgb(31 41 55 / 1);
opacity: 0;
transition-property: opacity;
transition-duration: 300ms;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}
.cfm-opacity-50 {
opacity: 0.5;
}
.cfm-opacity-100 {
opacity: 1;
}
.cfm-tr.selected{
background-color: rgba(0, 0, 0, 0.08) !important;
}
.cfm .modal-body {
max-height: calc(100vh - 150px);
overflow-x: auto;
}
.cfm-cursor-pointer {
cursor: pointer;
}
`;
const style = document.createElement("style");
style.innerHTML = css;
style.id = styleId;
document.head.appendChild(style);
}
}

export function getModalInstance(id, instances) {
Expand Down