use dry principle possible and added example project
This commit is contained in:
46 files changed
+3521
-772
No files matched your search
@@ -0,0 +1,38 @@
|
||||
.ndp-container.ndp-dark{
|
||||
background:#1f2937; /* slate-800-ish */
|
||||
color:#e5e7eb;
|
||||
border:1px solid rgba(255,255,255,.12);
|
||||
box-shadow:0 10px 25px rgba(0,0,0,.45);
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndp-header{
|
||||
border-bottom:1px solid rgba(255,255,255,.12);
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndp-header .ndp-header-link{
|
||||
color:#e5e7eb;
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndc-nav-button:hover,
|
||||
.ndp-container.ndp-dark .ndp-header-link:hover,
|
||||
.ndp-container.ndp-dark .ndp-table td:hover{
|
||||
background:rgba(255,255,255,.08);
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndp-table th{
|
||||
color:rgba(229,231,235,.75);
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndp-table td{
|
||||
color:#e5e7eb;
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndp-table td.ndp-selected{
|
||||
background:#2563eb; /* blue-600 */
|
||||
color:#fff;
|
||||
}
|
||||
|
||||
.ndp-container.ndp-dark .ndp-table td.ndp-today{
|
||||
color:#93c5fd; /* blue-300 */
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
function updateOptions(selectElement, data, placeholder) {
|
||||
selectElement.innerHTML = '';
|
||||
if (placeholder) {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = '';
|
||||
opt.textContent = placeholder;
|
||||
selectElement.appendChild(opt);
|
||||
}
|
||||
data.forEach(item => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = item.id;
|
||||
opt.textContent = item.text;
|
||||
selectElement.appendChild(opt);
|
||||
});
|
||||
selectElement.dispatchEvent(new Event('change'));
|
||||
}
|
||||
|
||||
document.addEventListener('change', function(e) {
|
||||
if (e.target.matches('.nepkit-province-select')) {
|
||||
const province = e.target.value;
|
||||
const container = e.target.closest('form') || document;
|
||||
const districtSelect = container.querySelector('.nepkit-district-select');
|
||||
const municipalitySelect = container.querySelector('.nepkit-municipality-select');
|
||||
|
||||
if (districtSelect) {
|
||||
if (!province) {
|
||||
updateOptions(districtSelect, [], 'Select District');
|
||||
if (municipalitySelect) updateOptions(municipalitySelect, [], 'Select Municipality');
|
||||
return;
|
||||
}
|
||||
|
||||
const url = districtSelect.dataset.url + '?province=' + encodeURIComponent(province);
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
updateOptions(districtSelect, data, 'Select District');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (e.target.matches('.nepkit-district-select')) {
|
||||
const district = e.target.value;
|
||||
const container = e.target.closest('form') || document;
|
||||
const municipalitySelect = container.querySelector('.nepkit-municipality-select');
|
||||
|
||||
if (municipalitySelect) {
|
||||
if (!district) {
|
||||
updateOptions(municipalitySelect, [], 'Select Municipality');
|
||||
return;
|
||||
}
|
||||
|
||||
const url = municipalitySelect.dataset.url + '?district=' + encodeURIComponent(district);
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
updateOptions(municipalitySelect, data, 'Select Municipality');
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
// Django admin exposes jQuery as `django.jQuery`.
|
||||
// Many third-party plugins expect `window.jQuery`/`window.$`.
|
||||
if (typeof window !== "undefined" && window.django && window.django.jQuery) {
|
||||
window.jQuery = window.django.jQuery;
|
||||
window.$ = window.django.jQuery;
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function isDarkTheme() {
|
||||
var docEl = document.documentElement;
|
||||
var body = document.body;
|
||||
|
||||
// Common theme markers used by Django admin themes / dark-mode packages
|
||||
var markers = [
|
||||
docEl && docEl.getAttribute('data-theme'),
|
||||
docEl && docEl.getAttribute('data-color-scheme'),
|
||||
body && body.getAttribute('data-theme'),
|
||||
body && body.getAttribute('data-color-scheme')
|
||||
].filter(Boolean).join(' ').toLowerCase();
|
||||
|
||||
var classNames = [
|
||||
docEl && docEl.className,
|
||||
body && body.className
|
||||
].filter(Boolean).join(' ').toLowerCase();
|
||||
|
||||
if (markers.indexOf('dark') !== -1 || classNames.indexOf('dark') !== -1) return true;
|
||||
|
||||
// Fallback to OS preference
|
||||
try {
|
||||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function applyThemeToDatepickerContainers() {
|
||||
var dark = isDarkTheme();
|
||||
var containers = document.querySelectorAll('.ndp-container');
|
||||
if (!containers || !containers.length) return;
|
||||
|
||||
containers.forEach(function (c) {
|
||||
c.classList.remove('ndp-dark');
|
||||
c.classList.remove('ndp-light');
|
||||
c.classList.add(dark ? 'ndp-dark' : 'ndp-light');
|
||||
});
|
||||
}
|
||||
|
||||
function initNepaliDatePickers() {
|
||||
var inputs = document.querySelectorAll('.nepkit-datepicker:not(.nepali-datepicker-initialized)');
|
||||
if (!inputs || !inputs.length) return;
|
||||
|
||||
inputs.forEach(function (el) {
|
||||
// Nepali Datepicker v5 exposes `element.NepaliDatePicker(options)`
|
||||
if (typeof el.NepaliDatePicker === 'function') {
|
||||
el.NepaliDatePicker({
|
||||
dateFormat: 'YYYY-MM-DD'
|
||||
});
|
||||
el.classList.add('nepali-datepicker-initialized');
|
||||
|
||||
// Ensure theme is applied when the picker is actually shown.
|
||||
// (The plugin often creates/inserts DOM on focus.)
|
||||
var applySoon = function () {
|
||||
// Run twice: once immediately, once after paint.
|
||||
applyThemeToDatepickerContainers();
|
||||
window.setTimeout(applyThemeToDatepickerContainers, 0);
|
||||
};
|
||||
|
||||
el.addEventListener('focus', applySoon);
|
||||
el.addEventListener('click', applySoon);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Initialize on page load
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', initNepaliDatePickers);
|
||||
} else {
|
||||
initNepaliDatePickers();
|
||||
}
|
||||
|
||||
// Keep in sync if system theme changes (fallback path)
|
||||
if (window.matchMedia) {
|
||||
try {
|
||||
var mql = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
if (mql && typeof mql.addEventListener === 'function') {
|
||||
mql.addEventListener('change', applyThemeToDatepickerContainers);
|
||||
} else if (mql && typeof mql.addListener === 'function') {
|
||||
mql.addListener(applyThemeToDatepickerContainers);
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Re-initialize when Django admin adds inlines dynamically
|
||||
if (typeof django !== 'undefined' && django.jQuery) {
|
||||
django.jQuery(document).on('formset:added', function () {
|
||||
initNepaliDatePickers();
|
||||
});
|
||||
}
|
||||
|
||||
// Also listen for DOM changes (admin popups/other dynamic content)
|
||||
if (typeof MutationObserver !== 'undefined') {
|
||||
var observer = new MutationObserver(function () {
|
||||
initNepaliDatePickers();
|
||||
applyThemeToDatepickerContainers();
|
||||
});
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true
|
||||
});
|
||||
}
|
||||
})();
|
||||
@@ -1,89 +0,0 @@
|
||||
(function() {
|
||||
"use strict";
|
||||
|
||||
function updateOptions(selectElement, data, placeholder) {
|
||||
selectElement.innerHTML = "";
|
||||
if (placeholder) {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = "";
|
||||
opt.textContent = placeholder;
|
||||
selectElement.appendChild(opt);
|
||||
}
|
||||
data.forEach((item) => {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = item.id;
|
||||
opt.textContent = item.text;
|
||||
selectElement.appendChild(opt);
|
||||
});
|
||||
selectElement.dispatchEvent(new Event("change"));
|
||||
}
|
||||
|
||||
document.addEventListener("change", function(e) {
|
||||
if (e.target.matches(".nepkit-province-select")) {
|
||||
const province = e.target.value;
|
||||
const container = e.target.closest("form") || document;
|
||||
const districtSelect = container.querySelector(
|
||||
".nepkit-district-select",
|
||||
);
|
||||
const municipalitySelect = container.querySelector(
|
||||
".nepkit-municipality-select",
|
||||
);
|
||||
|
||||
if (districtSelect) {
|
||||
if (!province) {
|
||||
updateOptions(districtSelect, [], "Select District");
|
||||
if (municipalitySelect)
|
||||
updateOptions(
|
||||
municipalitySelect,
|
||||
[],
|
||||
"Select Municipality",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const url =
|
||||
districtSelect.dataset.url +
|
||||
"?province=" +
|
||||
encodeURIComponent(province);
|
||||
fetch(url)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
updateOptions(districtSelect, data, "Select District");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (e.target.matches(".nepkit-district-select")) {
|
||||
const district = e.target.value;
|
||||
const container = e.target.closest("form") || document;
|
||||
const municipalitySelect = container.querySelector(
|
||||
".nepkit-municipality-select",
|
||||
);
|
||||
|
||||
if (municipalitySelect) {
|
||||
if (!district) {
|
||||
updateOptions(
|
||||
municipalitySelect,
|
||||
[],
|
||||
"Select Municipality",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const url =
|
||||
municipalitySelect.dataset.url +
|
||||
"?district=" +
|
||||
encodeURIComponent(district);
|
||||
fetch(url)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
updateOptions(
|
||||
municipalitySelect,
|
||||
data,
|
||||
"Select Municipality",
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
})();
|
||||
@@ -1,45 +0,0 @@
|
||||
(function($) {
|
||||
"use strict";
|
||||
|
||||
function initNepaliDatePickers() {
|
||||
if ($.fn.nepaliDatePicker) {
|
||||
// Initialize any date pickers that aren't already initialized
|
||||
$(".nepkit-datepicker")
|
||||
.not(".nepali-datepicker-initialized")
|
||||
.each(function() {
|
||||
$(this)
|
||||
.nepaliDatePicker({
|
||||
dateFormat: "%y-%m-%d",
|
||||
closeOnDateSelect: true,
|
||||
minDate: "१९७५-१-१",
|
||||
maxDate: "२१००-१२-३०",
|
||||
})
|
||||
.addClass("nepali-datepicker-initialized");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize on page load
|
||||
$(document).ready(function() {
|
||||
initNepaliDatePickers();
|
||||
});
|
||||
|
||||
// Re-initialize when Django admin adds forms dynamically (e.g., inlines, popups)
|
||||
// This handles Django admin's dynamic form loading
|
||||
if (typeof django !== "undefined" && django.jQuery) {
|
||||
django.jQuery(document).on("formset:added", function() {
|
||||
initNepaliDatePickers();
|
||||
});
|
||||
}
|
||||
|
||||
// Also listen for DOM changes (for admin popups and other dynamic content)
|
||||
if (typeof MutationObserver !== "undefined") {
|
||||
var observer = new MutationObserver(function(mutations) {
|
||||
initNepaliDatePickers();
|
||||
});
|
||||
observer.observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
}
|
||||
})(window.jQuery || window.django.jQuery);
|
||||
Reference in new issue
Block a user