chore(root): remove DRF and django-filter dependencies
drop DRF and django-filter to make django_nepkit framework‑agnostic, simplify dependencies, and align with v0.2.2 breaking change.
This commit is contained in:
40 files changed
+3286
-1406
No files matched your search
@@ -35,3 +35,15 @@
|
||||
.ndp-container.ndp-dark .ndp-table td.ndp-today{
|
||||
color:#93c5fd; /* blue-300 */
|
||||
}
|
||||
|
||||
/* Validation state for Nepali time inputs */
|
||||
.nepkit-time-input.nepkit-time-invalid {
|
||||
border-color: #dc2626; /* red-600 */
|
||||
background-color: rgba(220, 38, 38, 0.04);
|
||||
box-shadow: 0 0 0 1px #dc2626 inset;
|
||||
}
|
||||
.nepkit-time-input.nepkit-time-invalid:focus {
|
||||
outline: none;
|
||||
border-color: #b91c1c;
|
||||
box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.35);
|
||||
}
|
||||
@@ -1,158 +1,243 @@
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
// Placeholder text constants
|
||||
const PLACEHOLDERS = {
|
||||
var PLACEHOLDERS = {
|
||||
province: { ne: 'प्रदेश छान्नुहोस्', en: 'Select Province' },
|
||||
district: { ne: 'जिल्ला छान्नुहोस्', en: 'Select District' },
|
||||
municipality: { ne: 'नगरपालिका छान्नुहोस्', en: 'Select Municipality' }
|
||||
};
|
||||
|
||||
/**
|
||||
* Get placeholder text for a specific location type
|
||||
*/
|
||||
function getPlaceholder(type, isNepali) {
|
||||
return PLACEHOLDERS[type][isNepali ? 'ne' : 'en'];
|
||||
var map = PLACEHOLDERS[type];
|
||||
return (map && map[isNepali ? 'ne' : 'en']) || '';
|
||||
}
|
||||
|
||||
function updateOptions(selectElement, data, placeholder) {
|
||||
selectElement.innerHTML = '';
|
||||
if (placeholder) {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = '';
|
||||
opt.textContent = placeholder;
|
||||
selectElement.appendChild(opt);
|
||||
/**
|
||||
* Replace the option list of a select element.
|
||||
*
|
||||
* If ``preserveSelection`` is true, the currently selected value is
|
||||
* kept when it also appears in the new option list — this is what
|
||||
* lets the admin initialise a preselected form without losing the
|
||||
* saved province/district value.
|
||||
*
|
||||
* If ``dispatchChange`` is true (the default) a synthetic ``change``
|
||||
* event is fired so downstream listeners can react. Pass ``false``
|
||||
* when the update is happening as part of initialisation to avoid
|
||||
* running user-defined change handlers during page load.
|
||||
*/
|
||||
function updateOptions(selectElement, data, placeholder, preserveSelection, dispatchChange) {
|
||||
if (!selectElement) return;
|
||||
if (dispatchChange === undefined) {
|
||||
dispatchChange = true;
|
||||
}
|
||||
data.forEach(item => {
|
||||
const opt = document.createElement('option');
|
||||
var currentValue = preserveSelection ? selectElement.value : '';
|
||||
selectElement.innerHTML = '';
|
||||
|
||||
if (placeholder) {
|
||||
var placeholderOpt = document.createElement('option');
|
||||
placeholderOpt.value = '';
|
||||
placeholderOpt.textContent = placeholder;
|
||||
selectElement.appendChild(placeholderOpt);
|
||||
}
|
||||
|
||||
var found = false;
|
||||
(data || []).forEach(function(item) {
|
||||
var opt = document.createElement('option');
|
||||
opt.value = item.id;
|
||||
opt.textContent = item.text;
|
||||
selectElement.appendChild(opt);
|
||||
if (currentValue && item.id === currentValue) {
|
||||
found = true;
|
||||
}
|
||||
});
|
||||
selectElement.dispatchEvent(new Event('change'));
|
||||
|
||||
if (preserveSelection && currentValue) {
|
||||
selectElement.value = found ? currentValue : '';
|
||||
}
|
||||
|
||||
if (dispatchChange) {
|
||||
selectElement.dispatchEvent(new Event('change'));
|
||||
}
|
||||
}
|
||||
|
||||
function getLocalData(type, parentId, isNepali) {
|
||||
if (!window.NEPKIT_DATA) return null;
|
||||
const lang = isNepali ? 'ne' : 'en';
|
||||
const data = window.NEPKIT_DATA[lang];
|
||||
var lang = isNepali ? 'ne' : 'en';
|
||||
var data = window.NEPKIT_DATA[lang];
|
||||
if (!data) return null;
|
||||
|
||||
if (type === 'districts') {
|
||||
return data.districts[parentId] || [];
|
||||
} else if (type === 'municipalities') {
|
||||
}
|
||||
if (type === 'municipalities') {
|
||||
return data.municipalities[parentId] || [];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getMatchingSelect(container, selector, isNepali) {
|
||||
const matches = container.querySelectorAll(selector);
|
||||
for (let el of matches) {
|
||||
const elIsNepali = el.dataset.ne === 'true';
|
||||
if (elIsNepali === isNepali) {
|
||||
var matches = container.querySelectorAll(selector);
|
||||
for (var i = 0; i < matches.length; i++) {
|
||||
var el = matches[i];
|
||||
if (el.dataset.ne === 'true' === isNepali) {
|
||||
return el;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data from server endpoint
|
||||
*/
|
||||
function fetchData(selectElement, paramName, paramValue, placeholderType, isNepali) {
|
||||
if (!selectElement.dataset.url) return;
|
||||
|
||||
let url = selectElement.dataset.url + '?' + paramName + '=' + encodeURIComponent(paramValue);
|
||||
if (!selectElement.dataset.url) return Promise.resolve();
|
||||
var url = selectElement.dataset.url + '?' + paramName + '=' + encodeURIComponent(paramValue);
|
||||
if (isNepali) url += '&ne=true';
|
||||
if (selectElement.dataset.en === 'true') url += '&en=true';
|
||||
|
||||
fetch(url)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
updateOptions(selectElement, data, getPlaceholder(placeholderType, isNepali));
|
||||
return fetch(url)
|
||||
.then(function(response) {
|
||||
if (!response.ok) {
|
||||
throw new Error('HTTP ' + response.status + ' from ' + url);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(function(data) {
|
||||
updateOptions(selectElement, data, getPlaceholder(placeholderType, isNepali), true);
|
||||
})
|
||||
.catch(function(err) {
|
||||
// Don't swallow fetch errors silently — log them so the
|
||||
// empty dropdown the user sees is at least traceable.
|
||||
if (window.console && console.error) {
|
||||
console.error(
|
||||
'nepkit: failed to load ' + placeholderType + ' for ' + paramValue + ':',
|
||||
err
|
||||
);
|
||||
}
|
||||
updateOptions(selectElement, [], getPlaceholder(placeholderType, isNepali), true);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update dependent select element based on parent value
|
||||
*/
|
||||
function updateDependentSelect(childSelect, dataType, parentValue, placeholderType, isNepali, paramName) {
|
||||
if (!childSelect) return;
|
||||
function updateDependentSelect(childSelect, dataType, parentValue, placeholderType, isNepali, paramName, preserveSelection, dispatchChange) {
|
||||
if (!childSelect) return Promise.resolve();
|
||||
if (preserveSelection === undefined) preserveSelection = false;
|
||||
if (dispatchChange === undefined) dispatchChange = true;
|
||||
|
||||
if (!parentValue) {
|
||||
updateOptions(childSelect, [], getPlaceholder(placeholderType, isNepali));
|
||||
return;
|
||||
updateOptions(
|
||||
childSelect,
|
||||
[],
|
||||
getPlaceholder(placeholderType, isNepali),
|
||||
preserveSelection,
|
||||
dispatchChange
|
||||
);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const localData = getLocalData(dataType, parentValue, isNepali);
|
||||
|
||||
var localData = getLocalData(dataType, parentValue, isNepali);
|
||||
if (localData) {
|
||||
updateOptions(childSelect, localData, getPlaceholder(placeholderType, isNepali));
|
||||
} else {
|
||||
fetchData(childSelect, paramName, parentValue, placeholderType, isNepali);
|
||||
updateOptions(
|
||||
childSelect,
|
||||
localData,
|
||||
getPlaceholder(placeholderType, isNepali),
|
||||
preserveSelection,
|
||||
dispatchChange
|
||||
);
|
||||
return Promise.resolve();
|
||||
}
|
||||
return fetchData(childSelect, paramName, parentValue, placeholderType, isNepali);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise a single form. Walks the chained selects in order
|
||||
* (province → district → municipality) and, when a parent already has
|
||||
* a value (e.g. when editing an existing record in the admin),
|
||||
* filters the child list to match. ``dispatchChange`` is forced to
|
||||
* ``false`` so that init doesn't fire synthetic change events at
|
||||
* user-defined listeners.
|
||||
*/
|
||||
function initForm(container) {
|
||||
if (!container) return;
|
||||
var provinceSelects = container.querySelectorAll('.nepkit-province-select');
|
||||
|
||||
provinceSelects.forEach(function(provinceSelect) {
|
||||
var isNepali = provinceSelect.dataset.ne === 'true';
|
||||
var formScope = provinceSelect.closest('form') || container;
|
||||
var districtSelect = getMatchingSelect(formScope, '.nepkit-district-select', isNepali);
|
||||
var municipalitySelect = getMatchingSelect(formScope, '.nepkit-municipality-select', isNepali);
|
||||
|
||||
if (provinceSelect.value) {
|
||||
updateDependentSelect(
|
||||
districtSelect,
|
||||
'districts',
|
||||
provinceSelect.value,
|
||||
'district',
|
||||
isNepali,
|
||||
'province',
|
||||
true,
|
||||
false
|
||||
);
|
||||
} else if (districtSelect) {
|
||||
updateOptions(districtSelect, [], getPlaceholder('district', isNepali), false, false);
|
||||
}
|
||||
|
||||
if (districtSelect && districtSelect.value) {
|
||||
updateDependentSelect(
|
||||
municipalitySelect,
|
||||
'municipalities',
|
||||
districtSelect.value,
|
||||
'municipality',
|
||||
isNepali,
|
||||
'district',
|
||||
true,
|
||||
false
|
||||
);
|
||||
} else if (municipalitySelect) {
|
||||
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali), false, false);
|
||||
}
|
||||
});
|
||||
|
||||
// District-only forms (no province in the chain).
|
||||
var districtOnlySelects = container.querySelectorAll('.nepkit-district-select');
|
||||
districtOnlySelects.forEach(function(districtSelect) {
|
||||
if (districtSelect.value) {
|
||||
var isNepali = districtSelect.dataset.ne === 'true';
|
||||
var formScope2 = districtSelect.closest('form') || container;
|
||||
var municipalitySelect = getMatchingSelect(formScope2, '.nepkit-municipality-select', isNepali);
|
||||
updateDependentSelect(
|
||||
municipalitySelect,
|
||||
'municipalities',
|
||||
districtSelect.value,
|
||||
'municipality',
|
||||
isNepali,
|
||||
'district',
|
||||
true,
|
||||
false
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function init() {
|
||||
// Initialize province selects
|
||||
document.querySelectorAll('.nepkit-province-select').forEach(provinceSelect => {
|
||||
const isNepali = provinceSelect.dataset.ne === 'true';
|
||||
const container = provinceSelect.closest('form') || document;
|
||||
const districtSelect = getMatchingSelect(container, '.nepkit-district-select', isNepali);
|
||||
const municipalitySelect = getMatchingSelect(container, '.nepkit-municipality-select', isNepali);
|
||||
|
||||
if (!provinceSelect.value) {
|
||||
if (districtSelect && !districtSelect.value) {
|
||||
updateOptions(districtSelect, [], getPlaceholder('district', isNepali));
|
||||
}
|
||||
if (municipalitySelect && !municipalitySelect.value) {
|
||||
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Initialize district selects
|
||||
document.querySelectorAll('.nepkit-district-select').forEach(districtSelect => {
|
||||
const isNepali = districtSelect.dataset.ne === 'true';
|
||||
const container = districtSelect.closest('form') || document;
|
||||
const municipalitySelect = getMatchingSelect(container, '.nepkit-municipality-select', isNepali);
|
||||
|
||||
if (!districtSelect.value) {
|
||||
if (municipalitySelect && !municipalitySelect.value) {
|
||||
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali));
|
||||
}
|
||||
}
|
||||
});
|
||||
initForm(document);
|
||||
}
|
||||
|
||||
document.addEventListener('change', function(e) {
|
||||
// Handle province select change
|
||||
if (e.target.matches('.nepkit-province-select')) {
|
||||
const province = e.target.value;
|
||||
const isNepali = e.target.dataset.ne === 'true';
|
||||
const container = e.target.closest('form') || document;
|
||||
const districtSelect = getMatchingSelect(container, '.nepkit-district-select', isNepali);
|
||||
const municipalitySelect = getMatchingSelect(container, '.nepkit-municipality-select', isNepali);
|
||||
var province = e.target.value;
|
||||
var isNepali = e.target.dataset.ne === 'true';
|
||||
var container = e.target.closest('form') || document;
|
||||
var districtSelect = getMatchingSelect(container, '.nepkit-district-select', isNepali);
|
||||
var municipalitySelect = getMatchingSelect(container, '.nepkit-municipality-select', isNepali);
|
||||
|
||||
// Always clear municipality if province changes
|
||||
if (municipalitySelect) {
|
||||
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali));
|
||||
updateOptions(municipalitySelect, [], getPlaceholder('municipality', isNepali), false, true);
|
||||
}
|
||||
|
||||
// Update district options
|
||||
updateDependentSelect(districtSelect, 'districts', province, 'district', isNepali, 'province');
|
||||
updateDependentSelect(districtSelect, 'districts', province, 'district', isNepali, 'province', false, true);
|
||||
}
|
||||
|
||||
// Handle district select change
|
||||
if (e.target.matches('.nepkit-district-select')) {
|
||||
const district = e.target.value;
|
||||
const isNepali = e.target.dataset.ne === 'true';
|
||||
const container = e.target.closest('form') || document;
|
||||
const municipalitySelect = getMatchingSelect(container, '.nepkit-municipality-select', isNepali);
|
||||
|
||||
// Update municipality options
|
||||
updateDependentSelect(municipalitySelect, 'municipalities', district, 'municipality', isNepali, 'district');
|
||||
var district = e.target.value;
|
||||
var isNepali2 = e.target.dataset.ne === 'true';
|
||||
var container2 = e.target.closest('form') || document;
|
||||
var municipalitySelect2 = getMatchingSelect(container2, '.nepkit-municipality-select', isNepali2);
|
||||
updateDependentSelect(municipalitySelect2, 'municipalities', district, 'municipality', isNepali2, 'district', false, true);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -161,4 +246,41 @@
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
|
||||
// Re-initialise on DOM mutations so dynamically-injected forms
|
||||
// (admin inlines, htmx swaps, etc.) get the same treatment.
|
||||
// We only watch for new <form> / .nepkit-province-select / .nepkit-district-select
|
||||
// additions inside a <form> ancestor; everything else is ignored to
|
||||
// avoid the O(document) scan on every keystroke.
|
||||
if (typeof MutationObserver !== 'undefined' && document.body) {
|
||||
var debounceTimer = null;
|
||||
var observer = new MutationObserver(function(mutations) {
|
||||
var needsInit = false;
|
||||
for (var i = 0; i < mutations.length; i++) {
|
||||
var added = mutations[i].addedNodes;
|
||||
for (var j = 0; j < added.length; j++) {
|
||||
var node = added[j];
|
||||
if (node.nodeType !== 1) continue;
|
||||
if (
|
||||
node.matches && (
|
||||
node.matches('form') ||
|
||||
node.matches('.nepkit-province-select') ||
|
||||
node.matches('.nepkit-district-select') ||
|
||||
(node.querySelector && node.querySelector('.nepkit-province-select, .nepkit-district-select'))
|
||||
)
|
||||
) {
|
||||
needsInit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (needsInit) break;
|
||||
}
|
||||
if (!needsInit) return;
|
||||
if (debounceTimer) clearTimeout(debounceTimer);
|
||||
debounceTimer = setTimeout(function() {
|
||||
initForm(document);
|
||||
}, 50);
|
||||
});
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,81 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Lightweight time-input helper. Validates HH:MM / HH:MM:SS / hh:mm AM-PM
|
||||
* and converts English digits to Devanagari when the field is in `ne` mode.
|
||||
* Kept intentionally simple so it does not require jQuery.
|
||||
*
|
||||
* Nepali-digit input (०-९) is normalized to ASCII before validation so
|
||||
* users typing on a Nepali keyboard layout don't see spurious errors.
|
||||
*/
|
||||
var TIME_RE = /^([0-9]{1,2}):([0-9]{2})(:([0-9]{2}))?(\s*([AaPp][Mm]))?$/;
|
||||
var NEPALI_TO_ASCII = {'०':'0','१':'1','२':'2','३':'3','४':'4','५':'5','६':'6','७':'7','८':'8','९':'9'};
|
||||
var ASCII_TO_NEPALI = {'0':'०','1':'१','2':'२','3':'३','4':'४','5':'५','6':'६','7':'७','8':'८','9':'९'};
|
||||
|
||||
function isNepali(el) {
|
||||
return el.dataset.ne === 'true';
|
||||
}
|
||||
|
||||
function toAsciiDigits(text) {
|
||||
return String(text).replace(/[०-९]/g, function (d) { return NEPALI_TO_ASCII[d]; });
|
||||
}
|
||||
|
||||
function toNepaliDigits(text) {
|
||||
return String(text).replace(/[0-9]/g, function (d) { return ASCII_TO_NEPALI[d]; });
|
||||
}
|
||||
|
||||
function validate(rawValue) {
|
||||
if (!rawValue) return true;
|
||||
var match = TIME_RE.exec(toAsciiDigits(rawValue).trim());
|
||||
if (!match) return false;
|
||||
var h = parseInt(match[1], 10);
|
||||
var m = parseInt(match[2], 10);
|
||||
if (m > 59) return false;
|
||||
if (match[5]) {
|
||||
if (h < 1 || h > 12) return false;
|
||||
} else if (h > 23) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function initOne(el) {
|
||||
if (el.dataset.nepkitTimeInit === '1') return;
|
||||
el.dataset.nepkitTimeInit = '1';
|
||||
|
||||
el.addEventListener('blur', function () {
|
||||
if (!el.value) {
|
||||
el.classList.remove('nepkit-time-invalid');
|
||||
return;
|
||||
}
|
||||
if (!validate(el.value)) {
|
||||
el.classList.add('nepkit-time-invalid');
|
||||
return;
|
||||
}
|
||||
el.classList.remove('nepkit-time-invalid');
|
||||
if (isNepali(el)) {
|
||||
el.value = toNepaliDigits(el.value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function initAll(root) {
|
||||
var scope = root || document;
|
||||
var nodes = scope.querySelectorAll('.nepkit-time-input:not([data-nepkit-time-init="1"])');
|
||||
nodes.forEach(initOne);
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', function () { initAll(); });
|
||||
} else {
|
||||
initAll();
|
||||
}
|
||||
|
||||
if (typeof MutationObserver !== 'undefined' && document.body) {
|
||||
new MutationObserver(function () { initAll(); }).observe(document.body, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user