Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04b6ea4b0a |
@ -4,14 +4,13 @@ from pathlib import Path
|
|||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.postgresql_psycopg2',
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
'HOST': "localhost",
|
'NAME': os.getenv('DB_NAME'),
|
||||||
'NAME': os.getenv('POSTGRES_DB', ''),
|
'USER': os.getenv('DB_USER'),
|
||||||
'USER': os.getenv('POSTGRES_USER', ''),
|
'PASSWORD': os.getenv('DB_PASSWORD'),
|
||||||
'PASSWORD': os.getenv('POSTGRES_PASSWORD', ''),
|
'HOST': os.getenv('DB_HOST'),
|
||||||
'PORT': int(os.getenv('POSTGRES_PORT', '0')),
|
'PORT': os.getenv('DB_PORT'),
|
||||||
'CONN_MAX_AGE':None,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,15 +28,22 @@ APP_YEARS = f"{2023} - {datetime.now().year}"
|
|||||||
|
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
"unfold", # before django.contrib.admin
|
||||||
|
"unfold.contrib.filters", # optional, if special filters are needed
|
||||||
|
"unfold.contrib.forms", # optional, if special form elements are needed
|
||||||
|
"unfold.contrib.inlines", # optional, if special inlines are needed
|
||||||
|
"unfold.contrib.import_export", # optional, if django-import-export package is used
|
||||||
|
"unfold.contrib.guardian", # optional, if django-guardian package is used
|
||||||
|
"unfold.contrib.simple_history", # optional, if django-simple-history package is used
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'django_ckeditor_5',
|
|
||||||
|
|
||||||
'website',
|
'website',
|
||||||
|
|
||||||
|
'django_ckeditor_5',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@ -132,7 +139,7 @@ customColorPalette = [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
# CKEDITOR_5_CUSTOM_CSS = 'css/ckeditor-font-color.css' # optional
|
# CKEDITOR_5_CUSTOM_CSS = 'app/static/cybercode/styles/editor.css' # optional
|
||||||
# CKEDITOR_5_FILE_STORAGE = "path_to_storage.CustomStorage" # optional
|
# CKEDITOR_5_FILE_STORAGE = "path_to_storage.CustomStorage" # optional
|
||||||
CKEDITOR_5_CONFIGS = {
|
CKEDITOR_5_CONFIGS = {
|
||||||
'simple': {
|
'simple': {
|
||||||
@ -141,6 +148,7 @@ CKEDITOR_5_CONFIGS = {
|
|||||||
'default': {
|
'default': {
|
||||||
'toolbar': ['heading', '|', 'bold', 'italic', 'link',
|
'toolbar': ['heading', '|', 'bold', 'italic', 'link',
|
||||||
'bulletedList', 'numberedList', 'blockQuote', 'imageUpload', ],
|
'bulletedList', 'numberedList', 'blockQuote', 'imageUpload', ],
|
||||||
|
'contentsCss': ['/static/cybercode/styles/editor.css'],
|
||||||
'language': 'ru',
|
'language': 'ru',
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|||||||
@ -20,7 +20,7 @@ from django.contrib import admin
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('ofite/', admin.site.urls),
|
||||||
path('', include('website.urls'))
|
path('', include('website.urls'))
|
||||||
]
|
]
|
||||||
if settings.DEBUG:
|
if settings.DEBUG:
|
||||||
|
|||||||
0
app/static/admin/css/autocomplete.css
Normal file → Executable file
0
app/static/admin/css/base.css
Normal file → Executable file
0
app/static/admin/css/changelists.css
Normal file → Executable file
0
app/static/admin/css/dark_mode.css
Normal file → Executable file
0
app/static/admin/css/dashboard.css
Normal file → Executable file
0
app/static/admin/css/forms.css
Normal file → Executable file
0
app/static/admin/css/login.css
Normal file → Executable file
0
app/static/admin/css/nav_sidebar.css
Normal file → Executable file
0
app/static/admin/css/responsive.css
Normal file → Executable file
0
app/static/admin/css/responsive_rtl.css
Normal file → Executable file
0
app/static/admin/css/rtl.css
Normal file → Executable file
0
app/static/admin/css/vendor/select2/LICENSE-SELECT2.md
vendored
Normal file → Executable file
0
app/static/admin/css/vendor/select2/select2.css
vendored
Normal file → Executable file
0
app/static/admin/css/vendor/select2/select2.min.css
vendored
Normal file → Executable file
0
app/static/admin/css/widgets.css
Normal file → Executable file
0
app/static/admin/img/LICENSE
Normal file → Executable file
0
app/static/admin/img/README.txt
Normal file → Executable file
0
app/static/admin/img/calendar-icons.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
app/static/admin/img/gis/move_vertex_off.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
app/static/admin/img/gis/move_vertex_on.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
app/static/admin/img/icon-addlink.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 331 B After Width: | Height: | Size: 331 B |
0
app/static/admin/img/icon-alert.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 504 B After Width: | Height: | Size: 504 B |
0
app/static/admin/img/icon-calendar.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
app/static/admin/img/icon-changelink.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 380 B After Width: | Height: | Size: 380 B |
0
app/static/admin/img/icon-clock.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 677 B After Width: | Height: | Size: 677 B |
0
app/static/admin/img/icon-deletelink.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 392 B After Width: | Height: | Size: 392 B |
0
app/static/admin/img/icon-no.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 560 B After Width: | Height: | Size: 560 B |
0
app/static/admin/img/icon-unknown-alt.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 655 B After Width: | Height: | Size: 655 B |
0
app/static/admin/img/icon-unknown.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 655 B After Width: | Height: | Size: 655 B |
0
app/static/admin/img/icon-viewlink.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 581 B After Width: | Height: | Size: 581 B |
0
app/static/admin/img/icon-yes.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 436 B After Width: | Height: | Size: 436 B |
0
app/static/admin/img/inline-delete.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 560 B After Width: | Height: | Size: 560 B |
0
app/static/admin/img/search.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 458 B After Width: | Height: | Size: 458 B |
0
app/static/admin/img/selector-icons.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 3.2 KiB |
0
app/static/admin/img/sorting-icons.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
0
app/static/admin/img/tooltag-add.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 331 B After Width: | Height: | Size: 331 B |
0
app/static/admin/img/tooltag-arrowright.svg
Normal file → Executable file
|
Before Width: | Height: | Size: 280 B After Width: | Height: | Size: 280 B |
0
app/static/admin/js/SelectBox.js
Normal file → Executable file
0
app/static/admin/js/SelectFilter2.js
Normal file → Executable file
0
app/static/admin/js/actions.js
Normal file → Executable file
0
app/static/admin/js/admin/DateTimeShortcuts.js
Normal file → Executable file
493
app/static/admin/js/admin/RelatedObjectLookups.js
Normal file → Executable file
@ -1,301 +1,238 @@
|
|||||||
/*global SelectBox, interpolate*/
|
/*global SelectBox, interpolate*/
|
||||||
// Handles related-objects functionality: lookup link for raw_id_fields
|
// Handles related-objects functionality: lookup link for raw_id_fields
|
||||||
// and Add Another links.
|
// and Add Another links.
|
||||||
"use strict";
|
'use strict';
|
||||||
{
|
{
|
||||||
const $ = django.jQuery;
|
const $ = django.jQuery;
|
||||||
let popupIndex = 0;
|
let popupIndex = 0;
|
||||||
const relatedWindows = [];
|
const relatedWindows = [];
|
||||||
|
|
||||||
function dismissChildPopups() {
|
function dismissChildPopups() {
|
||||||
relatedWindows.forEach(function (win) {
|
relatedWindows.forEach(function(win) {
|
||||||
if (!win.closed) {
|
if(!win.closed) {
|
||||||
win.dismissChildPopups();
|
win.dismissChildPopups();
|
||||||
win.close();
|
win.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
function setPopupIndex() {
|
|
||||||
if (document.getElementsByName("_popup").length > 0) {
|
|
||||||
const index = window.name.lastIndexOf("__") + 2;
|
|
||||||
popupIndex = parseInt(window.name.substring(index));
|
|
||||||
} else {
|
|
||||||
popupIndex = 0;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function addPopupIndex(name) {
|
function setPopupIndex() {
|
||||||
return name + "__" + (popupIndex + 1);
|
if(document.getElementsByName("_popup").length > 0) {
|
||||||
}
|
const index = window.name.lastIndexOf("__") + 2;
|
||||||
|
popupIndex = parseInt(window.name.substring(index));
|
||||||
function removePopupIndex(name) {
|
|
||||||
return name.replace(new RegExp("__" + (popupIndex + 1) + "$"), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
function showAdminPopup(triggeringLink, name_regexp, add_popup) {
|
|
||||||
const name = addPopupIndex(triggeringLink.id.replace(name_regexp, ""));
|
|
||||||
const href = new URL(triggeringLink.href);
|
|
||||||
if (add_popup) {
|
|
||||||
href.searchParams.set("_popup", 1);
|
|
||||||
}
|
|
||||||
const win = window.open(
|
|
||||||
href,
|
|
||||||
name,
|
|
||||||
"height=768,width=1024,resizable=yes,scrollbars=yes"
|
|
||||||
);
|
|
||||||
relatedWindows.push(win);
|
|
||||||
win.focus();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function showRelatedObjectLookupPopup(triggeringLink) {
|
|
||||||
return showAdminPopup(triggeringLink, /^lookup_/, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
function dismissRelatedLookupPopup(win, chosenId) {
|
|
||||||
const name = removePopupIndex(win.name);
|
|
||||||
const elem = document.getElementById(name);
|
|
||||||
if (elem.classList.contains("vManyToManyRawIdAdminField") && elem.value) {
|
|
||||||
elem.value += "," + chosenId;
|
|
||||||
} else {
|
|
||||||
document.getElementById(name).value = chosenId;
|
|
||||||
}
|
|
||||||
const index = relatedWindows.indexOf(win);
|
|
||||||
if (index > -1) {
|
|
||||||
relatedWindows.splice(index, 1);
|
|
||||||
}
|
|
||||||
win.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
function showRelatedObjectPopup(triggeringLink) {
|
|
||||||
return showAdminPopup(triggeringLink, /^(change|add|delete)_/, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateRelatedObjectLinks(triggeringLink) {
|
|
||||||
const $this = $(triggeringLink);
|
|
||||||
// !CHANGED from original
|
|
||||||
// const siblings = $this.nextAll(
|
|
||||||
// ".view-related, .change-related, .delete-related"
|
|
||||||
// );
|
|
||||||
|
|
||||||
const siblings = $this
|
|
||||||
.parent()
|
|
||||||
.nextAll(".view-related, .change-related, .delete-related");
|
|
||||||
|
|
||||||
if (!siblings.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const value = $this.val();
|
|
||||||
if (value) {
|
|
||||||
siblings.each(function () {
|
|
||||||
const elm = $(this);
|
|
||||||
elm.attr(
|
|
||||||
"href",
|
|
||||||
elm.attr("data-href-template").replace("__fk__", value)
|
|
||||||
);
|
|
||||||
elm.removeAttr("aria-disabled");
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
siblings.removeAttr("href");
|
|
||||||
siblings.attr("aria-disabled", true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateRelatedSelectsOptions(
|
|
||||||
currentSelect,
|
|
||||||
win,
|
|
||||||
objId,
|
|
||||||
newRepr,
|
|
||||||
newId,
|
|
||||||
skipIds = []
|
|
||||||
) {
|
|
||||||
// After create/edit a model from the options next to the current
|
|
||||||
// select (+ or :pencil:) update ForeignKey PK of the rest of selects
|
|
||||||
// in the page.
|
|
||||||
|
|
||||||
const path = win.location.pathname;
|
|
||||||
// Extract the model from the popup url '.../<model>/add/' or
|
|
||||||
// '.../<model>/<id>/change/' depending the action (add or change).
|
|
||||||
const modelName = path.split("/")[path.split("/").length - (objId ? 4 : 3)];
|
|
||||||
// Select elements with a specific model reference and context of "available-source".
|
|
||||||
const selectsRelated = document.querySelectorAll(
|
|
||||||
`[data-model-ref="${modelName}"] [data-context="available-source"]`
|
|
||||||
);
|
|
||||||
|
|
||||||
selectsRelated.forEach(function (select) {
|
|
||||||
if (
|
|
||||||
currentSelect === select ||
|
|
||||||
(skipIds && skipIds.includes(select.id))
|
|
||||||
) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let option = select.querySelector(`option[value="${objId}"]`);
|
|
||||||
|
|
||||||
if (!option) {
|
|
||||||
option = new Option(newRepr, newId);
|
|
||||||
select.options.add(option);
|
|
||||||
// Update SelectBox cache for related fields.
|
|
||||||
if (
|
|
||||||
window.SelectBox !== undefined &&
|
|
||||||
!SelectBox.cache[currentSelect.id]
|
|
||||||
) {
|
|
||||||
SelectBox.add_to_cache(select.id, option);
|
|
||||||
SelectBox.redisplay(select.id);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
option.textContent = newRepr;
|
|
||||||
option.value = newId;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function dismissAddRelatedObjectPopup(win, newId, newRepr) {
|
|
||||||
const name = removePopupIndex(win.name);
|
|
||||||
const elem = document.getElementById(name);
|
|
||||||
if (elem) {
|
|
||||||
const elemName = elem.nodeName.toUpperCase();
|
|
||||||
if (elemName === "SELECT") {
|
|
||||||
elem.options[elem.options.length] = new Option(
|
|
||||||
newRepr,
|
|
||||||
newId,
|
|
||||||
true,
|
|
||||||
true
|
|
||||||
);
|
|
||||||
updateRelatedSelectsOptions(elem, win, null, newRepr, newId);
|
|
||||||
} else if (elemName === "INPUT") {
|
|
||||||
if (
|
|
||||||
elem.classList.contains("vManyToManyRawIdAdminField") &&
|
|
||||||
elem.value
|
|
||||||
) {
|
|
||||||
elem.value += "," + newId;
|
|
||||||
} else {
|
} else {
|
||||||
elem.value = newId;
|
popupIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Trigger a change event to update related links if required.
|
|
||||||
$(elem).trigger("change");
|
|
||||||
} else {
|
|
||||||
const toId = name + "_to";
|
|
||||||
const toElem = document.getElementById(toId);
|
|
||||||
const o = new Option(newRepr, newId);
|
|
||||||
SelectBox.add_to_cache(toId, o);
|
|
||||||
SelectBox.redisplay(toId);
|
|
||||||
if (toElem && toElem.nodeName.toUpperCase() === "SELECT") {
|
|
||||||
const skipIds = [name + "_from"];
|
|
||||||
updateRelatedSelectsOptions(toElem, win, null, newRepr, newId, skipIds);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const index = relatedWindows.indexOf(win);
|
|
||||||
if (index > -1) {
|
|
||||||
relatedWindows.splice(index, 1);
|
|
||||||
}
|
|
||||||
win.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
function dismissChangeRelatedObjectPopup(win, objId, newRepr, newId) {
|
function addPopupIndex(name) {
|
||||||
const id = removePopupIndex(win.name.replace(/^edit_/, ""));
|
return name + "__" + (popupIndex + 1);
|
||||||
const selectsSelector = interpolate("#%s, #%s_from, #%s_to", [id, id, id]);
|
}
|
||||||
const selects = $(selectsSelector);
|
|
||||||
selects
|
function removePopupIndex(name) {
|
||||||
.find("option")
|
return name.replace(new RegExp("__" + (popupIndex + 1) + "$"), '');
|
||||||
.each(function () {
|
}
|
||||||
if (this.value === objId) {
|
|
||||||
this.textContent = newRepr;
|
function showAdminPopup(triggeringLink, name_regexp, add_popup) {
|
||||||
this.value = newId;
|
const name = addPopupIndex(triggeringLink.id.replace(name_regexp, ''));
|
||||||
|
const href = new URL(triggeringLink.href);
|
||||||
|
if (add_popup) {
|
||||||
|
href.searchParams.set('_popup', 1);
|
||||||
}
|
}
|
||||||
})
|
const win = window.open(href, name, 'height=500,width=800,resizable=yes,scrollbars=yes');
|
||||||
.trigger("change");
|
relatedWindows.push(win);
|
||||||
updateRelatedSelectsOptions(selects[0], win, objId, newRepr, newId);
|
win.focus();
|
||||||
selects
|
return false;
|
||||||
.next()
|
|
||||||
.find(".select2-selection__rendered")
|
|
||||||
.each(function () {
|
|
||||||
// The element can have a clear button as a child.
|
|
||||||
// Use the lastChild to modify only the displayed value.
|
|
||||||
this.lastChild.textContent = newRepr;
|
|
||||||
this.title = newRepr;
|
|
||||||
});
|
|
||||||
const index = relatedWindows.indexOf(win);
|
|
||||||
if (index > -1) {
|
|
||||||
relatedWindows.splice(index, 1);
|
|
||||||
}
|
}
|
||||||
win.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
function dismissDeleteRelatedObjectPopup(win, objId) {
|
function showRelatedObjectLookupPopup(triggeringLink) {
|
||||||
const id = removePopupIndex(win.name.replace(/^delete_/, ""));
|
return showAdminPopup(triggeringLink, /^lookup_/, true);
|
||||||
const selectsSelector = interpolate("#%s, #%s_from, #%s_to", [id, id, id]);
|
}
|
||||||
const selects = $(selectsSelector);
|
|
||||||
selects
|
function dismissRelatedLookupPopup(win, chosenId) {
|
||||||
.find("option")
|
const name = removePopupIndex(win.name);
|
||||||
.each(function () {
|
const elem = document.getElementById(name);
|
||||||
if (this.value === objId) {
|
if (elem.classList.contains('vManyToManyRawIdAdminField') && elem.value) {
|
||||||
$(this).remove();
|
elem.value += ',' + chosenId;
|
||||||
|
} else {
|
||||||
|
document.getElementById(name).value = chosenId;
|
||||||
}
|
}
|
||||||
})
|
const index = relatedWindows.indexOf(win);
|
||||||
.trigger("change");
|
if (index > -1) {
|
||||||
const index = relatedWindows.indexOf(win);
|
relatedWindows.splice(index, 1);
|
||||||
if (index > -1) {
|
}
|
||||||
relatedWindows.splice(index, 1);
|
win.close();
|
||||||
}
|
}
|
||||||
win.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
window.showRelatedObjectLookupPopup = showRelatedObjectLookupPopup;
|
function showRelatedObjectPopup(triggeringLink) {
|
||||||
window.dismissRelatedLookupPopup = dismissRelatedLookupPopup;
|
return showAdminPopup(triggeringLink, /^(change|add|delete)_/, false);
|
||||||
window.showRelatedObjectPopup = showRelatedObjectPopup;
|
}
|
||||||
window.updateRelatedObjectLinks = updateRelatedObjectLinks;
|
|
||||||
window.dismissAddRelatedObjectPopup = dismissAddRelatedObjectPopup;
|
|
||||||
window.dismissChangeRelatedObjectPopup = dismissChangeRelatedObjectPopup;
|
|
||||||
window.dismissDeleteRelatedObjectPopup = dismissDeleteRelatedObjectPopup;
|
|
||||||
window.dismissChildPopups = dismissChildPopups;
|
|
||||||
|
|
||||||
// Kept for backward compatibility
|
function updateRelatedObjectLinks(triggeringLink) {
|
||||||
window.showAddAnotherPopup = showRelatedObjectPopup;
|
const $this = $(triggeringLink);
|
||||||
window.dismissAddAnotherPopup = dismissAddRelatedObjectPopup;
|
const siblings = $this.nextAll('.view-related, .change-related, .delete-related');
|
||||||
|
if (!siblings.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const value = $this.val();
|
||||||
|
if (value) {
|
||||||
|
siblings.each(function() {
|
||||||
|
const elm = $(this);
|
||||||
|
elm.attr('href', elm.attr('data-href-template').replace('__fk__', value));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
siblings.removeAttr('href');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener("unload", function (evt) {
|
function updateRelatedSelectsOptions(currentSelect, win, objId, newRepr, newId) {
|
||||||
window.dismissChildPopups();
|
// After create/edit a model from the options next to the current
|
||||||
});
|
// select (+ or :pencil:) update ForeignKey PK of the rest of selects
|
||||||
|
// in the page.
|
||||||
|
|
||||||
$(document).ready(function () {
|
const path = win.location.pathname;
|
||||||
setPopupIndex();
|
// Extract the model from the popup url '.../<model>/add/' or
|
||||||
$("a[data-popup-opener]").on("click", function (event) {
|
// '.../<model>/<id>/change/' depending the action (add or change).
|
||||||
event.preventDefault();
|
const modelName = path.split('/')[path.split('/').length - (objId ? 4 : 3)];
|
||||||
opener.dismissRelatedLookupPopup(window, $(this).data("popup-opener"));
|
// Exclude autocomplete selects.
|
||||||
|
const selectsRelated = document.querySelectorAll(`[data-model-ref="${modelName}"] select:not(.admin-autocomplete)`);
|
||||||
|
|
||||||
|
selectsRelated.forEach(function(select) {
|
||||||
|
if (currentSelect === select) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let option = select.querySelector(`option[value="${objId}"]`);
|
||||||
|
|
||||||
|
if (!option) {
|
||||||
|
option = new Option(newRepr, newId);
|
||||||
|
select.options.add(option);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
option.textContent = newRepr;
|
||||||
|
option.value = newId;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function dismissAddRelatedObjectPopup(win, newId, newRepr) {
|
||||||
|
const name = removePopupIndex(win.name);
|
||||||
|
const elem = document.getElementById(name);
|
||||||
|
if (elem) {
|
||||||
|
const elemName = elem.nodeName.toUpperCase();
|
||||||
|
if (elemName === 'SELECT') {
|
||||||
|
elem.options[elem.options.length] = new Option(newRepr, newId, true, true);
|
||||||
|
updateRelatedSelectsOptions(elem, win, null, newRepr, newId);
|
||||||
|
} else if (elemName === 'INPUT') {
|
||||||
|
if (elem.classList.contains('vManyToManyRawIdAdminField') && elem.value) {
|
||||||
|
elem.value += ',' + newId;
|
||||||
|
} else {
|
||||||
|
elem.value = newId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Trigger a change event to update related links if required.
|
||||||
|
$(elem).trigger('change');
|
||||||
|
} else {
|
||||||
|
const toId = name + "_to";
|
||||||
|
const o = new Option(newRepr, newId);
|
||||||
|
SelectBox.add_to_cache(toId, o);
|
||||||
|
SelectBox.redisplay(toId);
|
||||||
|
}
|
||||||
|
const index = relatedWindows.indexOf(win);
|
||||||
|
if (index > -1) {
|
||||||
|
relatedWindows.splice(index, 1);
|
||||||
|
}
|
||||||
|
win.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
function dismissChangeRelatedObjectPopup(win, objId, newRepr, newId) {
|
||||||
|
const id = removePopupIndex(win.name.replace(/^edit_/, ''));
|
||||||
|
const selectsSelector = interpolate('#%s, #%s_from, #%s_to', [id, id, id]);
|
||||||
|
const selects = $(selectsSelector);
|
||||||
|
selects.find('option').each(function() {
|
||||||
|
if (this.value === objId) {
|
||||||
|
this.textContent = newRepr;
|
||||||
|
this.value = newId;
|
||||||
|
}
|
||||||
|
}).trigger('change');
|
||||||
|
updateRelatedSelectsOptions(selects[0], win, objId, newRepr, newId);
|
||||||
|
selects.next().find('.select2-selection__rendered').each(function() {
|
||||||
|
// The element can have a clear button as a child.
|
||||||
|
// Use the lastChild to modify only the displayed value.
|
||||||
|
this.lastChild.textContent = newRepr;
|
||||||
|
this.title = newRepr;
|
||||||
|
});
|
||||||
|
const index = relatedWindows.indexOf(win);
|
||||||
|
if (index > -1) {
|
||||||
|
relatedWindows.splice(index, 1);
|
||||||
|
}
|
||||||
|
win.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
function dismissDeleteRelatedObjectPopup(win, objId) {
|
||||||
|
const id = removePopupIndex(win.name.replace(/^delete_/, ''));
|
||||||
|
const selectsSelector = interpolate('#%s, #%s_from, #%s_to', [id, id, id]);
|
||||||
|
const selects = $(selectsSelector);
|
||||||
|
selects.find('option').each(function() {
|
||||||
|
if (this.value === objId) {
|
||||||
|
$(this).remove();
|
||||||
|
}
|
||||||
|
}).trigger('change');
|
||||||
|
const index = relatedWindows.indexOf(win);
|
||||||
|
if (index > -1) {
|
||||||
|
relatedWindows.splice(index, 1);
|
||||||
|
}
|
||||||
|
win.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.showRelatedObjectLookupPopup = showRelatedObjectLookupPopup;
|
||||||
|
window.dismissRelatedLookupPopup = dismissRelatedLookupPopup;
|
||||||
|
window.showRelatedObjectPopup = showRelatedObjectPopup;
|
||||||
|
window.updateRelatedObjectLinks = updateRelatedObjectLinks;
|
||||||
|
window.dismissAddRelatedObjectPopup = dismissAddRelatedObjectPopup;
|
||||||
|
window.dismissChangeRelatedObjectPopup = dismissChangeRelatedObjectPopup;
|
||||||
|
window.dismissDeleteRelatedObjectPopup = dismissDeleteRelatedObjectPopup;
|
||||||
|
window.dismissChildPopups = dismissChildPopups;
|
||||||
|
|
||||||
|
// Kept for backward compatibility
|
||||||
|
window.showAddAnotherPopup = showRelatedObjectPopup;
|
||||||
|
window.dismissAddAnotherPopup = dismissAddRelatedObjectPopup;
|
||||||
|
|
||||||
|
window.addEventListener('unload', function(evt) {
|
||||||
|
window.dismissChildPopups();
|
||||||
});
|
});
|
||||||
$("body").on(
|
|
||||||
"click",
|
$(document).ready(function() {
|
||||||
'.related-widget-wrapper-link[data-popup="yes"]',
|
setPopupIndex();
|
||||||
function (e) {
|
$("a[data-popup-opener]").on('click', function(event) {
|
||||||
e.preventDefault();
|
event.preventDefault();
|
||||||
if (this.href) {
|
opener.dismissRelatedLookupPopup(window, $(this).data("popup-opener"));
|
||||||
const event = $.Event("django:show-related", { href: this.href });
|
});
|
||||||
$(this).trigger(event);
|
$('body').on('click', '.related-widget-wrapper-link[data-popup="yes"]', function(e) {
|
||||||
if (!event.isDefaultPrevented()) {
|
e.preventDefault();
|
||||||
showRelatedObjectPopup(this);
|
if (this.href) {
|
||||||
}
|
const event = $.Event('django:show-related', {href: this.href});
|
||||||
}
|
$(this).trigger(event);
|
||||||
}
|
if (!event.isDefaultPrevented()) {
|
||||||
);
|
showRelatedObjectPopup(this);
|
||||||
$("body").on("change", ".related-widget-wrapper select", function (e) {
|
}
|
||||||
const event = $.Event("django:update-related");
|
}
|
||||||
$(this).trigger(event);
|
});
|
||||||
if (!event.isDefaultPrevented()) {
|
$('body').on('change', '.related-widget-wrapper select', function(e) {
|
||||||
updateRelatedObjectLinks(this);
|
const event = $.Event('django:update-related');
|
||||||
}
|
$(this).trigger(event);
|
||||||
|
if (!event.isDefaultPrevented()) {
|
||||||
|
updateRelatedObjectLinks(this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$('.related-widget-wrapper select').trigger('change');
|
||||||
|
$('body').on('click', '.related-lookup', function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const event = $.Event('django:lookup-related');
|
||||||
|
$(this).trigger(event);
|
||||||
|
if (!event.isDefaultPrevented()) {
|
||||||
|
showRelatedObjectLookupPopup(this);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
$(".related-widget-wrapper select").trigger("change");
|
|
||||||
$("body").on("click", ".related-lookup", function (e) {
|
|
||||||
e.preventDefault();
|
|
||||||
const event = $.Event("django:lookup-related");
|
|
||||||
$(this).trigger(event);
|
|
||||||
if (!event.isDefaultPrevented()) {
|
|
||||||
showRelatedObjectLookupPopup(this);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
0
app/static/admin/js/autocomplete.js
Normal file → Executable file
0
app/static/admin/js/calendar.js
Normal file → Executable file
0
app/static/admin/js/cancel.js
Normal file → Executable file
0
app/static/admin/js/change_form.js
Normal file → Executable file
0
app/static/admin/js/collapse.js
Normal file → Executable file
0
app/static/admin/js/core.js
Normal file → Executable file
0
app/static/admin/js/filters.js
Normal file → Executable file
755
app/static/admin/js/inlines.js
Normal file → Executable file
@ -15,460 +15,345 @@
|
|||||||
* Licensed under the New BSD License
|
* Licensed under the New BSD License
|
||||||
* See: https://opensource.org/licenses/bsd-license.php
|
* See: https://opensource.org/licenses/bsd-license.php
|
||||||
*/
|
*/
|
||||||
"use strict";
|
'use strict';
|
||||||
{
|
{
|
||||||
const $ = django.jQuery;
|
const $ = django.jQuery;
|
||||||
$.fn.formset = function (opts) {
|
$.fn.formset = function(opts) {
|
||||||
const options = $.extend({}, $.fn.formset.defaults, opts);
|
const options = $.extend({}, $.fn.formset.defaults, opts);
|
||||||
const $this = $(this);
|
const $this = $(this);
|
||||||
const $parent = $this.parent();
|
const $parent = $this.parent();
|
||||||
const updateElementIndex = function (el, prefix, ndx) {
|
const updateElementIndex = function(el, prefix, ndx) {
|
||||||
const id_regex = new RegExp("(" + prefix + "-(\\d+|__prefix__))");
|
const id_regex = new RegExp("(" + prefix + "-(\\d+|__prefix__))");
|
||||||
const replacement = prefix + "-" + ndx;
|
const replacement = prefix + "-" + ndx;
|
||||||
if ($(el).prop("for")) {
|
if ($(el).prop("for")) {
|
||||||
$(el).prop("for", $(el).prop("for").replace(id_regex, replacement));
|
$(el).prop("for", $(el).prop("for").replace(id_regex, replacement));
|
||||||
}
|
}
|
||||||
if (el.id) {
|
if (el.id) {
|
||||||
el.id = el.id.replace(id_regex, replacement);
|
el.id = el.id.replace(id_regex, replacement);
|
||||||
}
|
}
|
||||||
if (el.name) {
|
if (el.name) {
|
||||||
// !CHANGED from original
|
el.name = el.name.replace(id_regex, replacement);
|
||||||
// el.name = el.name.replace(id_regex, replacement);
|
}
|
||||||
el.setAttribute("name", el.name.replace(id_regex, replacement));
|
};
|
||||||
}
|
const totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS").prop("autocomplete", "off");
|
||||||
};
|
let nextIndex = parseInt(totalForms.val(), 10);
|
||||||
const totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS").prop(
|
const maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").prop("autocomplete", "off");
|
||||||
"autocomplete",
|
const minForms = $("#id_" + options.prefix + "-MIN_NUM_FORMS").prop("autocomplete", "off");
|
||||||
"off"
|
let addButton;
|
||||||
);
|
|
||||||
let nextIndex = parseInt(totalForms.val(), 10);
|
|
||||||
const maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").prop(
|
|
||||||
"autocomplete",
|
|
||||||
"off"
|
|
||||||
);
|
|
||||||
const minForms = $("#id_" + options.prefix + "-MIN_NUM_FORMS").prop(
|
|
||||||
"autocomplete",
|
|
||||||
"off"
|
|
||||||
);
|
|
||||||
let addButton;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The "Add another MyModel" button below the inline forms.
|
* The "Add another MyModel" button below the inline forms.
|
||||||
*/
|
*/
|
||||||
const addInlineAddButton = function () {
|
const addInlineAddButton = function() {
|
||||||
if (addButton === null) {
|
if (addButton === null) {
|
||||||
if ($this.prop("tagName") === "TR") {
|
if ($this.prop("tagName") === "TR") {
|
||||||
// If forms are laid out as table rows, insert the
|
// If forms are laid out as table rows, insert the
|
||||||
// "add" button in a new table row:
|
// "add" button in a new table row:
|
||||||
const numCols = $this.eq(-1).children().length;
|
const numCols = $this.eq(-1).children().length;
|
||||||
$parent.append(
|
$parent.append('<tr class="' + options.addCssClass + '"><td colspan="' + numCols + '"><a href="#">' + options.addText + "</a></tr>");
|
||||||
'<tr class="' +
|
addButton = $parent.find("tr:last a");
|
||||||
options.addCssClass +
|
} else {
|
||||||
'"><td colspan="' +
|
// Otherwise, insert it immediately after the last form:
|
||||||
numCols +
|
$this.filter(":last").after('<div class="' + options.addCssClass + '"><a href="#">' + options.addText + "</a></div>");
|
||||||
'"><a href="#">' +
|
addButton = $this.filter(":last").next().find("a");
|
||||||
options.addText +
|
}
|
||||||
"</a></tr>"
|
}
|
||||||
);
|
addButton.on('click', addInlineClickHandler);
|
||||||
addButton = $parent.find("tr:last a");
|
};
|
||||||
|
|
||||||
|
const addInlineClickHandler = function(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
const template = $("#" + options.prefix + "-empty");
|
||||||
|
const row = template.clone(true);
|
||||||
|
row.removeClass(options.emptyCssClass)
|
||||||
|
.addClass(options.formCssClass)
|
||||||
|
.attr("id", options.prefix + "-" + nextIndex);
|
||||||
|
addInlineDeleteButton(row);
|
||||||
|
row.find("*").each(function() {
|
||||||
|
updateElementIndex(this, options.prefix, totalForms.val());
|
||||||
|
});
|
||||||
|
// Insert the new form when it has been fully edited.
|
||||||
|
row.insertBefore($(template));
|
||||||
|
// Update number of total forms.
|
||||||
|
$(totalForms).val(parseInt(totalForms.val(), 10) + 1);
|
||||||
|
nextIndex += 1;
|
||||||
|
// Hide the add button if there's a limit and it's been reached.
|
||||||
|
if ((maxForms.val() !== '') && (maxForms.val() - totalForms.val()) <= 0) {
|
||||||
|
addButton.parent().hide();
|
||||||
|
}
|
||||||
|
// Show the remove buttons if there are more than min_num.
|
||||||
|
toggleDeleteButtonVisibility(row.closest('.inline-group'));
|
||||||
|
|
||||||
|
// Pass the new form to the post-add callback, if provided.
|
||||||
|
if (options.added) {
|
||||||
|
options.added(row);
|
||||||
|
}
|
||||||
|
row.get(0).dispatchEvent(new CustomEvent("formset:added", {
|
||||||
|
bubbles: true,
|
||||||
|
detail: {
|
||||||
|
formsetName: options.prefix
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The "X" button that is part of every unsaved inline.
|
||||||
|
* (When saved, it is replaced with a "Delete" checkbox.)
|
||||||
|
*/
|
||||||
|
const addInlineDeleteButton = function(row) {
|
||||||
|
if (row.is("tr")) {
|
||||||
|
// If the forms are laid out in table rows, insert
|
||||||
|
// the remove button into the last table cell:
|
||||||
|
row.children(":last").append('<div><a class="' + options.deleteCssClass + '" href="#">' + options.deleteText + "</a></div>");
|
||||||
|
} else if (row.is("ul") || row.is("ol")) {
|
||||||
|
// If they're laid out as an ordered/unordered list,
|
||||||
|
// insert an <li> after the last list item:
|
||||||
|
row.append('<li><a class="' + options.deleteCssClass + '" href="#">' + options.deleteText + "</a></li>");
|
||||||
|
} else {
|
||||||
|
// Otherwise, just insert the remove button as the
|
||||||
|
// last child element of the form's container:
|
||||||
|
row.children(":first").append('<span><a class="' + options.deleteCssClass + '" href="#">' + options.deleteText + "</a></span>");
|
||||||
|
}
|
||||||
|
// Add delete handler for each row.
|
||||||
|
row.find("a." + options.deleteCssClass).on('click', inlineDeleteHandler.bind(this));
|
||||||
|
};
|
||||||
|
|
||||||
|
const inlineDeleteHandler = function(e1) {
|
||||||
|
e1.preventDefault();
|
||||||
|
const deleteButton = $(e1.target);
|
||||||
|
const row = deleteButton.closest('.' + options.formCssClass);
|
||||||
|
const inlineGroup = row.closest('.inline-group');
|
||||||
|
// Remove the parent form containing this button,
|
||||||
|
// and also remove the relevant row with non-field errors:
|
||||||
|
const prevRow = row.prev();
|
||||||
|
if (prevRow.length && prevRow.hasClass('row-form-errors')) {
|
||||||
|
prevRow.remove();
|
||||||
|
}
|
||||||
|
row.remove();
|
||||||
|
nextIndex -= 1;
|
||||||
|
// Pass the deleted form to the post-delete callback, if provided.
|
||||||
|
if (options.removed) {
|
||||||
|
options.removed(row);
|
||||||
|
}
|
||||||
|
document.dispatchEvent(new CustomEvent("formset:removed", {
|
||||||
|
detail: {
|
||||||
|
formsetName: options.prefix
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
// Update the TOTAL_FORMS form count.
|
||||||
|
const forms = $("." + options.formCssClass);
|
||||||
|
$("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
|
||||||
|
// Show add button again once below maximum number.
|
||||||
|
if ((maxForms.val() === '') || (maxForms.val() - forms.length) > 0) {
|
||||||
|
addButton.parent().show();
|
||||||
|
}
|
||||||
|
// Hide the remove buttons if at min_num.
|
||||||
|
toggleDeleteButtonVisibility(inlineGroup);
|
||||||
|
// Also, update names and ids for all remaining form controls so
|
||||||
|
// they remain in sequence:
|
||||||
|
let i, formCount;
|
||||||
|
const updateElementCallback = function() {
|
||||||
|
updateElementIndex(this, options.prefix, i);
|
||||||
|
};
|
||||||
|
for (i = 0, formCount = forms.length; i < formCount; i++) {
|
||||||
|
updateElementIndex($(forms).get(i), options.prefix, i);
|
||||||
|
$(forms.get(i)).find("*").each(updateElementCallback);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const toggleDeleteButtonVisibility = function(inlineGroup) {
|
||||||
|
if ((minForms.val() !== '') && (minForms.val() - totalForms.val()) >= 0) {
|
||||||
|
inlineGroup.find('.inline-deletelink').hide();
|
||||||
|
} else {
|
||||||
|
inlineGroup.find('.inline-deletelink').show();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$this.each(function(i) {
|
||||||
|
$(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create the delete buttons for all unsaved inlines:
|
||||||
|
$this.filter('.' + options.formCssClass + ':not(.has_original):not(.' + options.emptyCssClass + ')').each(function() {
|
||||||
|
addInlineDeleteButton($(this));
|
||||||
|
});
|
||||||
|
toggleDeleteButtonVisibility($this);
|
||||||
|
|
||||||
|
// Create the add button, initially hidden.
|
||||||
|
addButton = options.addButton;
|
||||||
|
addInlineAddButton();
|
||||||
|
|
||||||
|
// Show the add button if allowed to add more items.
|
||||||
|
// Note that max_num = None translates to a blank string.
|
||||||
|
const showAddButton = maxForms.val() === '' || (maxForms.val() - totalForms.val()) > 0;
|
||||||
|
if ($this.length && showAddButton) {
|
||||||
|
addButton.parent().show();
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, insert it immediately after the last form:
|
addButton.parent().hide();
|
||||||
$this
|
|
||||||
.filter(":last")
|
|
||||||
.after(
|
|
||||||
'<div class="' +
|
|
||||||
options.addCssClass +
|
|
||||||
'"><a href="#">' +
|
|
||||||
options.addText +
|
|
||||||
"</a></div>"
|
|
||||||
);
|
|
||||||
addButton = $this.filter(":last").next().find("a");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
addButton.on("click", addInlineClickHandler);
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
const addInlineClickHandler = function (e) {
|
/* Setup plugin defaults */
|
||||||
e.preventDefault();
|
$.fn.formset.defaults = {
|
||||||
const template = $("#" + options.prefix + "-empty");
|
prefix: "form", // The form prefix for your django formset
|
||||||
const row = template.clone(true);
|
addText: "add another", // Text for the add link
|
||||||
row
|
deleteText: "remove", // Text for the delete link
|
||||||
.removeClass(options.emptyCssClass)
|
addCssClass: "add-row", // CSS class applied to the add link
|
||||||
.addClass(options.formCssClass)
|
deleteCssClass: "delete-row", // CSS class applied to the delete link
|
||||||
.attr("id", options.prefix + "-" + nextIndex);
|
emptyCssClass: "empty-row", // CSS class applied to the empty row
|
||||||
|
formCssClass: "dynamic-form", // CSS class applied to each form in a formset
|
||||||
addInlineDeleteButton(row);
|
added: null, // Function called each time a new form is added
|
||||||
row.find("*").each(function () {
|
removed: null, // Function called each time a form is deleted
|
||||||
updateElementIndex(this, options.prefix, totalForms.val());
|
addButton: null // Existing add button to use
|
||||||
});
|
|
||||||
|
|
||||||
// Insert the new form when it has been fully edited.
|
|
||||||
// !CHANGED from original
|
|
||||||
if ($(template).parent().is("tbody")) {
|
|
||||||
row
|
|
||||||
.wrap('<tbody class="template"></tbody>')
|
|
||||||
.parent()
|
|
||||||
.insertBefore($(template).parent());
|
|
||||||
} else {
|
|
||||||
row.insertBefore($(template));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update number of total forms.
|
|
||||||
$(totalForms).val(parseInt(totalForms.val(), 10) + 1);
|
|
||||||
nextIndex += 1;
|
|
||||||
// Hide the add button if there's a limit and it's been reached.
|
|
||||||
if (maxForms.val() !== "" && maxForms.val() - totalForms.val() <= 0) {
|
|
||||||
addButton.parent().hide();
|
|
||||||
}
|
|
||||||
// Show the remove buttons if there are more than min_num.
|
|
||||||
toggleDeleteButtonVisibility(row.closest(".inline-group"));
|
|
||||||
|
|
||||||
// Pass the new form to the post-add callback, if provided.
|
|
||||||
if (options.added) {
|
|
||||||
options.added(row);
|
|
||||||
}
|
|
||||||
row.get(0).dispatchEvent(
|
|
||||||
new CustomEvent("formset:added", {
|
|
||||||
bubbles: true,
|
|
||||||
detail: {
|
|
||||||
formsetName: options.prefix,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* The "X" button that is part of every unsaved inline.
|
|
||||||
* (When saved, it is replaced with a "Delete" checkbox.)
|
|
||||||
*/
|
|
||||||
const addInlineDeleteButton = function (row) {
|
|
||||||
if (row.is("tr")) {
|
|
||||||
// If the forms are laid out in table rows, insert
|
|
||||||
// the remove button into the last table cell:
|
|
||||||
row
|
|
||||||
.children(":last")
|
|
||||||
.append(
|
|
||||||
'<div><a class="' +
|
|
||||||
options.deleteCssClass +
|
|
||||||
'" href="#">' +
|
|
||||||
options.deleteText +
|
|
||||||
"</a></div>"
|
|
||||||
);
|
|
||||||
} else if (row.is("ul") || row.is("ol")) {
|
|
||||||
// If they're laid out as an ordered/unordered list,
|
|
||||||
// insert an <li> after the last list item:
|
|
||||||
row.append(
|
|
||||||
'<li><a class="' +
|
|
||||||
options.deleteCssClass +
|
|
||||||
'" href="#">' +
|
|
||||||
options.deleteText +
|
|
||||||
"</a></li>"
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Otherwise, just insert the remove button as the
|
|
||||||
// last child element of the form's container:
|
|
||||||
row
|
|
||||||
.children(":first")
|
|
||||||
.append(
|
|
||||||
'<span><a class="' +
|
|
||||||
options.deleteCssClass +
|
|
||||||
'" href="#">' +
|
|
||||||
options.deleteText +
|
|
||||||
"</a></span>"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// Add delete handler for each row.
|
|
||||||
row
|
|
||||||
.find("a." + options.deleteCssClass)
|
|
||||||
.on("click", inlineDeleteHandler.bind(this));
|
|
||||||
};
|
|
||||||
|
|
||||||
const inlineDeleteHandler = function (e1) {
|
// Tabular inlines ---------------------------------------------------------
|
||||||
e1.preventDefault();
|
$.fn.tabularFormset = function(selector, options) {
|
||||||
const deleteButton = $(e1.target);
|
const $rows = $(this);
|
||||||
const row = deleteButton.closest("." + options.formCssClass);
|
|
||||||
const inlineGroup = row.closest(".inline-group");
|
|
||||||
// Remove the parent form containing this button,
|
|
||||||
// and also remove the relevant row with non-field errors:
|
|
||||||
const prevRow = row.prev();
|
|
||||||
if (prevRow.length && prevRow.hasClass("row-form-errors")) {
|
|
||||||
prevRow.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
// !CHANGED from original
|
const reinitDateTimeShortCuts = function() {
|
||||||
if (deleteButton.parent().parent().parent().parent().is("tbody")) {
|
// Reinitialize the calendar and clock widgets by force
|
||||||
row.parent().remove();
|
if (typeof DateTimeShortcuts !== "undefined") {
|
||||||
} else {
|
$(".datetimeshortcuts").remove();
|
||||||
row.remove();
|
DateTimeShortcuts.init();
|
||||||
}
|
}
|
||||||
nextIndex -= 1;
|
};
|
||||||
// Pass the deleted form to the post-delete callback, if provided.
|
|
||||||
if (options.removed) {
|
|
||||||
options.removed(row);
|
|
||||||
}
|
|
||||||
document.dispatchEvent(
|
|
||||||
new CustomEvent("formset:removed", {
|
|
||||||
detail: {
|
|
||||||
formsetName: options.prefix,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
);
|
|
||||||
// Update the TOTAL_FORMS form count.
|
|
||||||
const forms = $("." + options.formCssClass);
|
|
||||||
$("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
|
|
||||||
// Show add button again once below maximum number.
|
|
||||||
if (maxForms.val() === "" || maxForms.val() - forms.length > 0) {
|
|
||||||
addButton.parent().show();
|
|
||||||
}
|
|
||||||
// Hide the remove buttons if at min_num.
|
|
||||||
toggleDeleteButtonVisibility(inlineGroup);
|
|
||||||
// Also, update names and ids for all remaining form controls so
|
|
||||||
// they remain in sequence:
|
|
||||||
let i, formCount;
|
|
||||||
const updateElementCallback = function () {
|
|
||||||
updateElementIndex(this, options.prefix, i);
|
|
||||||
};
|
|
||||||
for (i = 0, formCount = forms.length; i < formCount; i++) {
|
|
||||||
updateElementIndex($(forms).get(i), options.prefix, i);
|
|
||||||
$(forms.get(i)).find("*").each(updateElementCallback);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const toggleDeleteButtonVisibility = function (inlineGroup) {
|
const updateSelectFilter = function() {
|
||||||
if (minForms.val() !== "" && minForms.val() - totalForms.val() >= 0) {
|
// If any SelectFilter widgets are a part of the new form,
|
||||||
inlineGroup.find(".inline-deletelink").hide();
|
// instantiate a new SelectFilter instance for it.
|
||||||
} else {
|
if (typeof SelectFilter !== 'undefined') {
|
||||||
inlineGroup.find(".inline-deletelink").show();
|
$('.selectfilter').each(function(index, value) {
|
||||||
}
|
SelectFilter.init(value.id, this.dataset.fieldName, false);
|
||||||
};
|
});
|
||||||
|
$('.selectfilterstacked').each(function(index, value) {
|
||||||
|
SelectFilter.init(value.id, this.dataset.fieldName, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// !CHANGED from original. Business logic for tabular inlines is different.
|
const initPrepopulatedFields = function(row) {
|
||||||
if ($this.parent().is("tbody")) {
|
row.find('.prepopulated_field').each(function() {
|
||||||
$this
|
const field = $(this),
|
||||||
.parent()
|
input = field.find('input, select, textarea'),
|
||||||
.parent()
|
dependency_list = input.data('dependency_list') || [],
|
||||||
.find("tr.form-row")
|
dependencies = [];
|
||||||
.each(function (i) {
|
$.each(dependency_list, function(i, field_name) {
|
||||||
$(this)
|
dependencies.push('#' + row.find('.field-' + field_name).find('input, select, textarea').attr('id'));
|
||||||
.not("." + options.emptyCssClass)
|
});
|
||||||
.addClass(options.formCssClass);
|
if (dependencies.length) {
|
||||||
|
input.prepopulate(dependencies, input.attr('maxlength'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
$rows.formset({
|
||||||
|
prefix: options.prefix,
|
||||||
|
addText: options.addText,
|
||||||
|
formCssClass: "dynamic-" + options.prefix,
|
||||||
|
deleteCssClass: "inline-deletelink",
|
||||||
|
deleteText: options.deleteText,
|
||||||
|
emptyCssClass: "empty-form",
|
||||||
|
added: function(row) {
|
||||||
|
initPrepopulatedFields(row);
|
||||||
|
reinitDateTimeShortCuts();
|
||||||
|
updateSelectFilter();
|
||||||
|
},
|
||||||
|
addButton: options.addButton
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
$this.each(function (i) {
|
|
||||||
$(this)
|
|
||||||
.not("." + options.emptyCssClass)
|
|
||||||
.addClass(options.formCssClass);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the delete buttons for all unsaved inlines:
|
return $rows;
|
||||||
// !CHANGED from original, added parent() and used find() instead of filter()
|
|
||||||
$this
|
|
||||||
.parent()
|
|
||||||
.parent()
|
|
||||||
.find(
|
|
||||||
"." +
|
|
||||||
options.formCssClass +
|
|
||||||
":not(.has_original):not(." +
|
|
||||||
options.emptyCssClass +
|
|
||||||
")"
|
|
||||||
)
|
|
||||||
.each(function () {
|
|
||||||
addInlineDeleteButton($(this));
|
|
||||||
});
|
|
||||||
toggleDeleteButtonVisibility($this);
|
|
||||||
|
|
||||||
// Create the add button, initially hidden.
|
|
||||||
addButton = options.addButton;
|
|
||||||
addInlineAddButton();
|
|
||||||
|
|
||||||
// Show the add button if allowed to add more items.
|
|
||||||
// Note that max_num = None translates to a blank string.
|
|
||||||
const showAddButton =
|
|
||||||
maxForms.val() === "" || maxForms.val() - totalForms.val() > 0;
|
|
||||||
if ($this.length && showAddButton) {
|
|
||||||
addButton.parent().show();
|
|
||||||
} else {
|
|
||||||
addButton.parent().hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
return this;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Setup plugin defaults */
|
|
||||||
$.fn.formset.defaults = {
|
|
||||||
prefix: "form", // The form prefix for your django formset
|
|
||||||
addText: "add another", // Text for the add link
|
|
||||||
deleteText: "remove", // Text for the delete link
|
|
||||||
addCssClass: "add-row", // CSS class applied to the add link
|
|
||||||
deleteCssClass: "delete-row", // CSS class applied to the delete link
|
|
||||||
emptyCssClass: "empty-row", // CSS class applied to the empty row
|
|
||||||
formCssClass: "dynamic-form", // CSS class applied to each form in a formset
|
|
||||||
added: null, // Function called each time a new form is added
|
|
||||||
removed: null, // Function called each time a form is deleted
|
|
||||||
addButton: null, // Existing add button to use
|
|
||||||
};
|
|
||||||
|
|
||||||
// Tabular inlines ---------------------------------------------------------
|
|
||||||
$.fn.tabularFormset = function (selector, options) {
|
|
||||||
const $rows = $(this);
|
|
||||||
|
|
||||||
const reinitDateTimeShortCuts = function () {
|
|
||||||
// Reinitialize the calendar and clock widgets by force
|
|
||||||
if (typeof DateTimeShortcuts !== "undefined") {
|
|
||||||
$(".datetimeshortcuts").remove();
|
|
||||||
DateTimeShortcuts.init();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateSelectFilter = function () {
|
// Stacked inlines ---------------------------------------------------------
|
||||||
// If any SelectFilter widgets are a part of the new form,
|
$.fn.stackedFormset = function(selector, options) {
|
||||||
// instantiate a new SelectFilter instance for it.
|
const $rows = $(this);
|
||||||
if (typeof SelectFilter !== "undefined") {
|
const updateInlineLabel = function(row) {
|
||||||
$(".selectfilter").each(function (index, value) {
|
$(selector).find(".inline_label").each(function(i) {
|
||||||
SelectFilter.init(value.id, this.dataset.fieldName, false);
|
const count = i + 1;
|
||||||
|
$(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const reinitDateTimeShortCuts = function() {
|
||||||
|
// Reinitialize the calendar and clock widgets by force, yuck.
|
||||||
|
if (typeof DateTimeShortcuts !== "undefined") {
|
||||||
|
$(".datetimeshortcuts").remove();
|
||||||
|
DateTimeShortcuts.init();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateSelectFilter = function() {
|
||||||
|
// If any SelectFilter widgets were added, instantiate a new instance.
|
||||||
|
if (typeof SelectFilter !== "undefined") {
|
||||||
|
$(".selectfilter").each(function(index, value) {
|
||||||
|
SelectFilter.init(value.id, this.dataset.fieldName, false);
|
||||||
|
});
|
||||||
|
$(".selectfilterstacked").each(function(index, value) {
|
||||||
|
SelectFilter.init(value.id, this.dataset.fieldName, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const initPrepopulatedFields = function(row) {
|
||||||
|
row.find('.prepopulated_field').each(function() {
|
||||||
|
const field = $(this),
|
||||||
|
input = field.find('input, select, textarea'),
|
||||||
|
dependency_list = input.data('dependency_list') || [],
|
||||||
|
dependencies = [];
|
||||||
|
$.each(dependency_list, function(i, field_name) {
|
||||||
|
// Dependency in a fieldset.
|
||||||
|
let field_element = row.find('.form-row .field-' + field_name);
|
||||||
|
// Dependency without a fieldset.
|
||||||
|
if (!field_element.length) {
|
||||||
|
field_element = row.find('.form-row.field-' + field_name);
|
||||||
|
}
|
||||||
|
dependencies.push('#' + field_element.find('input, select, textarea').attr('id'));
|
||||||
|
});
|
||||||
|
if (dependencies.length) {
|
||||||
|
input.prepopulate(dependencies, input.attr('maxlength'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
$rows.formset({
|
||||||
|
prefix: options.prefix,
|
||||||
|
addText: options.addText,
|
||||||
|
formCssClass: "dynamic-" + options.prefix,
|
||||||
|
deleteCssClass: "inline-deletelink",
|
||||||
|
deleteText: options.deleteText,
|
||||||
|
emptyCssClass: "empty-form",
|
||||||
|
removed: updateInlineLabel,
|
||||||
|
added: function(row) {
|
||||||
|
initPrepopulatedFields(row);
|
||||||
|
reinitDateTimeShortCuts();
|
||||||
|
updateSelectFilter();
|
||||||
|
updateInlineLabel(row);
|
||||||
|
},
|
||||||
|
addButton: options.addButton
|
||||||
});
|
});
|
||||||
$(".selectfilterstacked").each(function (index, value) {
|
|
||||||
SelectFilter.init(value.id, this.dataset.fieldName, true);
|
return $rows;
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const initPrepopulatedFields = function (row) {
|
$(document).ready(function() {
|
||||||
row.find(".prepopulated_field").each(function () {
|
$(".js-inline-admin-formset").each(function() {
|
||||||
const field = $(this),
|
const data = $(this).data(),
|
||||||
input = field.find("input, select, textarea"),
|
inlineOptions = data.inlineFormset;
|
||||||
dependency_list = input.data("dependency_list") || [],
|
let selector;
|
||||||
dependencies = [];
|
switch(data.inlineType) {
|
||||||
$.each(dependency_list, function (i, field_name) {
|
case "stacked":
|
||||||
dependencies.push(
|
selector = inlineOptions.name + "-group .inline-related";
|
||||||
"#" +
|
$(selector).stackedFormset(selector, inlineOptions.options);
|
||||||
row
|
break;
|
||||||
.find(".field-" + field_name)
|
case "tabular":
|
||||||
.find("input, select, textarea")
|
selector = inlineOptions.name + "-group .tabular.inline-related tbody:first > tr.form-row";
|
||||||
.attr("id")
|
$(selector).tabularFormset(selector, inlineOptions.options);
|
||||||
);
|
break;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
if (dependencies.length) {
|
|
||||||
input.prepopulate(dependencies, input.attr("maxlength"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
$rows.formset({
|
|
||||||
prefix: options.prefix,
|
|
||||||
addText: options.addText,
|
|
||||||
formCssClass: "dynamic-" + options.prefix,
|
|
||||||
deleteCssClass: "inline-deletelink",
|
|
||||||
deleteText: options.deleteText,
|
|
||||||
emptyCssClass: "empty-form",
|
|
||||||
added: function (row) {
|
|
||||||
initPrepopulatedFields(row);
|
|
||||||
reinitDateTimeShortCuts();
|
|
||||||
updateSelectFilter();
|
|
||||||
},
|
|
||||||
addButton: options.addButton,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return $rows;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Stacked inlines ---------------------------------------------------------
|
|
||||||
$.fn.stackedFormset = function (selector, options) {
|
|
||||||
const $rows = $(this);
|
|
||||||
const updateInlineLabel = function (row) {
|
|
||||||
$(selector)
|
|
||||||
.find(".inline_label")
|
|
||||||
.each(function (i) {
|
|
||||||
const count = i + 1;
|
|
||||||
$(this).html(
|
|
||||||
$(this)
|
|
||||||
.html()
|
|
||||||
.replace(/(#\d+)/g, "#" + count)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const reinitDateTimeShortCuts = function () {
|
|
||||||
// Reinitialize the calendar and clock widgets by force, yuck.
|
|
||||||
if (typeof DateTimeShortcuts !== "undefined") {
|
|
||||||
$(".datetimeshortcuts").remove();
|
|
||||||
DateTimeShortcuts.init();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateSelectFilter = function () {
|
|
||||||
// If any SelectFilter widgets were added, instantiate a new instance.
|
|
||||||
if (typeof SelectFilter !== "undefined") {
|
|
||||||
$(".selectfilter").each(function (index, value) {
|
|
||||||
SelectFilter.init(value.id, this.dataset.fieldName, false);
|
|
||||||
});
|
|
||||||
$(".selectfilterstacked").each(function (index, value) {
|
|
||||||
SelectFilter.init(value.id, this.dataset.fieldName, true);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const initPrepopulatedFields = function (row) {
|
|
||||||
row.find(".prepopulated_field").each(function () {
|
|
||||||
const field = $(this),
|
|
||||||
input = field.find("input, select, textarea"),
|
|
||||||
dependency_list = input.data("dependency_list") || [],
|
|
||||||
dependencies = [];
|
|
||||||
$.each(dependency_list, function (i, field_name) {
|
|
||||||
// Dependency in a fieldset.
|
|
||||||
let field_element = row.find(".form-row .field-" + field_name);
|
|
||||||
// Dependency without a fieldset.
|
|
||||||
if (!field_element.length) {
|
|
||||||
field_element = row.find(".form-row.field-" + field_name);
|
|
||||||
}
|
|
||||||
dependencies.push(
|
|
||||||
"#" + field_element.find("input, select, textarea").attr("id")
|
|
||||||
);
|
|
||||||
});
|
|
||||||
if (dependencies.length) {
|
|
||||||
input.prepopulate(dependencies, input.attr("maxlength"));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
$rows.formset({
|
|
||||||
prefix: options.prefix,
|
|
||||||
addText: options.addText,
|
|
||||||
formCssClass: "dynamic-" + options.prefix,
|
|
||||||
deleteCssClass: "inline-deletelink",
|
|
||||||
deleteText: options.deleteText,
|
|
||||||
emptyCssClass: "empty-form",
|
|
||||||
removed: updateInlineLabel,
|
|
||||||
added: function (row) {
|
|
||||||
initPrepopulatedFields(row);
|
|
||||||
reinitDateTimeShortCuts();
|
|
||||||
updateSelectFilter();
|
|
||||||
updateInlineLabel(row);
|
|
||||||
},
|
|
||||||
addButton: options.addButton,
|
|
||||||
});
|
|
||||||
|
|
||||||
return $rows;
|
|
||||||
};
|
|
||||||
|
|
||||||
$(document).ready(function () {
|
|
||||||
$(".js-inline-admin-formset").each(function () {
|
|
||||||
const data = $(this).data(),
|
|
||||||
inlineOptions = data.inlineFormset;
|
|
||||||
let selector;
|
|
||||||
switch (data.inlineType) {
|
|
||||||
case "stacked":
|
|
||||||
selector = inlineOptions.name + "-group .inline-related";
|
|
||||||
$(selector).stackedFormset(selector, inlineOptions.options);
|
|
||||||
break;
|
|
||||||
case "tabular":
|
|
||||||
selector =
|
|
||||||
inlineOptions.name +
|
|
||||||
"-group .tabular.inline-related tbody:last > tr.form-row";
|
|
||||||
$(selector).tabularFormset(selector, inlineOptions.options);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||