117 lines
4.2 KiB
JavaScript
117 lines
4.2 KiB
JavaScript
/**
|
|
* Discount inline editor for discount_setting Tabular.
|
|
* Scans discount cells after render and enables click-to-edit.
|
|
*/
|
|
(function() {
|
|
var PROCESSED_CELLS = new WeakSet();
|
|
var SETUP_INTERVAL = 300;
|
|
|
|
function findTabular() {
|
|
if (typeof bricks === 'undefined') return null;
|
|
try {
|
|
return bricks.getWidgetById('product_discount_tbl', bricks.app.root);
|
|
} catch(e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function setupCell(cell) {
|
|
if (PROCESSED_CELLS.has(cell)) return;
|
|
var text = cell.textContent.trim();
|
|
if (text === '' || isNaN(parseFloat(text))) return;
|
|
PROCESSED_CELLS.add(cell);
|
|
|
|
cell.style.cursor = 'pointer';
|
|
cell.title = '点击编辑折扣';
|
|
|
|
cell.addEventListener('click', function(e) {
|
|
e.stopPropagation();
|
|
if (cell.querySelector('input')) return;
|
|
|
|
var oldVal = cell.textContent.trim();
|
|
var inp = document.createElement('input');
|
|
inp.type = 'number';
|
|
inp.step = '0.01';
|
|
inp.min = '0';
|
|
inp.max = '1';
|
|
inp.value = oldVal;
|
|
inp.style.cssText = 'width:100%;box-sizing:border-box;text-align:center;background:#1E293B;color:#F1F5F9;border:1px solid #3B82F6;border-radius:4px;padding:2px 4px;font-size:12px;';
|
|
|
|
cell.textContent = '';
|
|
cell.appendChild(inp);
|
|
inp.focus();
|
|
inp.select();
|
|
|
|
var saved = false;
|
|
var saveFn = async function() {
|
|
if (saved) return;
|
|
saved = true;
|
|
var newVal = inp.value.trim();
|
|
inp.disabled = true;
|
|
|
|
if (newVal === oldVal) {
|
|
cell.textContent = oldVal;
|
|
return;
|
|
}
|
|
|
|
try {
|
|
var url = new URL(window.location.href);
|
|
var discountid = url.searchParams.get('discountid') || '';
|
|
var rowEl = cell.closest('.tabular-row');
|
|
var productId = '';
|
|
var detailId = '';
|
|
var dv = findTabular();
|
|
if (dv && dv.scrollpanel) {
|
|
for (var i = 0; i < dv.scrollpanel.children.length; i++) {
|
|
var r = dv.scrollpanel.children[i];
|
|
if (r.dom_element === rowEl && r.user_data) {
|
|
productId = r.user_data.productid || '';
|
|
detailId = r.user_data.detail_id || '';
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
var resp = await fetch(
|
|
url.origin + url.pathname.replace(/\/[^\/]*$/, '/save_discount.dspy'),
|
|
{
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({
|
|
discountid: discountid,
|
|
productid: productId,
|
|
detail_id: detailId,
|
|
discount: newVal
|
|
})
|
|
}
|
|
);
|
|
|
|
if (resp.ok) {
|
|
cell.textContent = parseFloat(newVal).toString();
|
|
} else {
|
|
cell.textContent = oldVal;
|
|
}
|
|
} catch(ex) {
|
|
cell.textContent = oldVal;
|
|
}
|
|
};
|
|
|
|
inp.addEventListener('blur', saveFn);
|
|
inp.addEventListener('keydown', function(ev) {
|
|
if (ev.key === 'Enter') { ev.preventDefault(); inp.blur(); }
|
|
if (ev.key === 'Escape') { cell.textContent = oldVal; }
|
|
});
|
|
});
|
|
}
|
|
|
|
function scan() {
|
|
var dv = findTabular();
|
|
if (!dv || !dv.scrollpanel || !dv.scrollpanel.dom_element) return;
|
|
var cells = dv.scrollpanel.dom_element.querySelectorAll('.tabular-cell');
|
|
cells.forEach(setupCell);
|
|
}
|
|
|
|
// Keep scanning for new cells after each render
|
|
setInterval(scan, SETUP_INTERVAL);
|
|
})();
|