Files
itflow/agent/js/document_template_make.js
johnnyq 3ee183a09a Document templates stage 2, filled in from the client: placeholders [Client], [Website], [Site], [Address], [Site Phone], [Contact], [First Name], [Contact Email], [Contact Phone], [Agent], [Date], [Month], [Quarter] and [Year] are filled in across a template's name, summary and content when a document is made, using the client's primary site and contact unless another or none is picked; tables of records [Contacts], [Locations], [Assets], [Servers], [Workstations], [Network Equipment], [Printers], [Networks], [Domains], [Certificates], [Licenses], [Vendors] and [Services] put in the client's current records, as a table on a line of their own (an empty row when there are none) or as names in a sentence, all made safe; any other bracketed word is a fill-in asked for when a document is made, set up on the template page as text, date, list or yes/no, required or with help, renamed along with the template text, reset or removed, and dragged into order, with answers checked by ticketFillInAnswers and blanks left in the document; the template page gets Fill-ins and Filled in from the client cards with each kind of word marked in its own colour, and the editor gets a button for each placeholder and table to insert it at the cursor (js/document_template_form.js); New document for a client and Document from template draw the site and contact pickers and fill-ins once the template and client are picked (agent/ajax.php document_template_asks, agent/js/document_template_make.js) and open larger; copying a template copies its fill-ins; Draft it with AI is told the placeholders and tables; migration 3.2.6 adds document_template_fill_ins; fixed the fill-in add and reset messages showing raw HTML around the bracketed name on document and ticket templates; changelog updated
2026-10-18 19:47:57 -04:00

69 lines
2.6 KiB
JavaScript

/*
* Making a document from a template - the Document from template window
* (agent/modals/document/document_add_from_template.php) and New document for
* a client (admin/modals/document_template/document_template_use.php). When
* the template or the client is picked, what it asks for - the site and
* contact its placeholders come from, and its fill-ins - is drawn in the
* form's [data-doc-template-asks] box (agent/ajax.php document_template_asks).
*
* The windows are ajax modals that run this each time they open: each form is
* drawn as it opens, and the change listener is bound once.
*/
(function () {
function draw(form) {
const box = form.querySelector('[data-doc-template-asks]');
const template = form.querySelector('[data-doc-template-select]');
const client = form.querySelector('[data-doc-template-client]');
if (!box || !template || !client) {
return;
}
const templateId = parseInt(template.value, 10) || 0;
const clientId = parseInt(client.value, 10) || 0;
const key = templateId + ':' + clientId;
if (box.dataset.drawn === key) {
return;
}
box.dataset.drawn = key;
if (!templateId || !clientId) {
box.innerHTML = '';
box.hidden = true;
return;
}
fetch('/agent/ajax.php?document_template_asks=' + templateId + '&client_id=' + clientId, { credentials: 'same-origin' })
.then(function (response) {
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
return response.json();
})
.then(function (data) {
// Picked again meanwhile - the later answer draws it
if (box.dataset.drawn !== key) {
return;
}
box.innerHTML = data.html || '';
box.hidden = !data.html;
})
.catch(function () {
box.innerHTML = '';
box.hidden = true;
});
}
document.querySelectorAll('form[data-doc-template-make]').forEach(draw);
if (window.itflowDocumentTemplateMakeBound) {
return;
}
window.itflowDocumentTemplateMakeBound = true;
document.addEventListener('change', function (e) {
const field = e.target.closest ? e.target.closest('[data-doc-template-select], [data-doc-template-client]') : null;
const form = field ? field.closest('form[data-doc-template-make]') : null;
if (form) {
draw(form);
}
});
})();