gigafibre-fsm/apps/dispatch/scripts/fix_client_script.py
louispaulb 7da22ff132 merge: import dispatch-app into apps/dispatch/ (17 commits preserved)
Integrates the Dispatch PWA (Vue/Quasar) into the gigafibre-fsm monorepo.
Full git history accessible via `git log -- apps/dispatch/`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 08:08:51 -04:00

89 lines
3.8 KiB
Python

import frappe, os
os.chdir('/home/frappe/frappe-bench')
frappe.init('frontend', sites_path='/home/frappe/frappe-bench/sites')
frappe.connect()
SCRIPT = """
frappe.ui.form.on('Dispatch Job', {
setup(frm) {
frm._addr_bound = false;
},
refresh(frm) {
if (frm._addr_bound) return;
frappe.run_serially([
() => frappe.timeout(1),
() => {
try { _bind_address_autocomplete(frm); }
catch(e) { console.warn('Address autocomplete deferred:', e.message); }
}
]);
}
});
function _bind_address_autocomplete(frm) {
var ctrl = frm.fields_dict && frm.fields_dict.address;
if (!ctrl) return;
var input = ctrl.input || (ctrl.$input && ctrl.$input[0]);
if (!input) return;
if (frm._addr_bound) return;
frm._addr_bound = true;
var dropdown = document.createElement('div');
dropdown.style.cssText = 'position:absolute;z-index:1000;background:#fff;border:1px solid #d1d5db;border-radius:6px;max-height:250px;overflow-y:auto;width:100%;box-shadow:0 4px 12px rgba(0,0,0,0.15);display:none;';
input.parentElement.style.position = 'relative';
input.parentElement.appendChild(dropdown);
var timer = null;
input.addEventListener('input', function() {
clearTimeout(timer);
var q = this.value.trim();
if (q.length < 3) { dropdown.style.display = 'none'; return; }
timer = setTimeout(function() {
frappe.call({
method: 'search_address',
args: { q: q },
callback: function(r) {
dropdown.innerHTML = '';
var results = (r && r.results) || (r && r.message && r.message.results) || [];
if (!results.length) {
dropdown.innerHTML = '<div style="padding:8px 12px;color:#6b7280;font-size:12px">Aucun resultat</div>';
} else {
results.forEach(function(addr) {
var item = document.createElement('div');
item.style.cssText = 'padding:8px 12px;cursor:pointer;font-size:13px;border-bottom:1px solid #f3f4f6;';
var html = '<strong>' + addr.address_full + '</strong>';
if (addr.ville) html += ' <span style="float:right;color:#6b7280;font-size:11px">' + addr.ville + '</span>';
item.innerHTML = html;
item.addEventListener('mousedown', function(e) {
e.preventDefault();
frm.set_value('address', addr.address_full);
if (addr.latitude) frm.set_value('latitude', parseFloat(addr.latitude));
if (addr.longitude) frm.set_value('longitude', parseFloat(addr.longitude));
dropdown.style.display = 'none';
frm.dirty();
});
item.addEventListener('mouseenter', function() { this.style.background = '#f3f4f6'; });
item.addEventListener('mouseleave', function() { this.style.background = ''; });
dropdown.appendChild(item);
});
}
dropdown.style.display = 'block';
}
});
}, 300);
});
input.addEventListener('blur', function() {
setTimeout(function() { dropdown.style.display = 'none'; }, 200);
});
}
"""
cs = frappe.get_doc('Client Script', 'Dispatch Job Address Autocomplete')
cs.enabled = 1
cs.script = SCRIPT
cs.save(ignore_permissions=True)
frappe.db.commit()
print('Client Script fixed and re-enabled')
frappe.destroy()