Files
itflow/agent/js/ticket_tasks_modal.js
johnnyq 3f90d2b0d0 Ticket templates stage 1: templates now set up the whole ticket - category, priority, billable, agent, due time and tags (migration 3.1.9 adds them, the ticket_template_tags table and tickets.ticket_template_id) - and fill [Client], [Contact], [Asset], [Site], [Agent], [Project], [Ticket], [Date], [Month], [Quarter] and [Year] in the subject, details and tasks when a ticket is raised, leaving other bracketed text for whoever picks it up; tickets remember the template they came from and each template shows how many tickets it raised, how many are open and when it was last used; the template picker fills New Ticket, the New Tickets windows on the assets and clients lists and recurring tickets, and project templates use each template's setup and agent; Admin > Ticket Templates rebuilt with search, Current/Archived, Copy, Archive/Restore and New ticket from it, and each template gets a page showing the ticket it starts, its tasks in drag-to-sort order with time estimates, what it sets up, its placeholders and the tickets, recurring tickets and project templates that use it; template handling moved into functions/ticket_template.php with ticketTemplateFinish, tagKeepIds and tagIdsFromValues split out of tagSetForRecord, and template task editing moved to admin/post; fixes editing a template task moving it to the top of the list, client bulk tickets skipping the template's tasks and time estimates, and the client bulk window not validating the agent, priority and category, and adds an option there to raise each client's ticket for its primary contact; changelog updated
2026-10-18 00:01:43 -04:00

206 lines
7.3 KiB
JavaScript

// Editable task rows for the ticket and recurring ticket add/edit modals, and the
// ticket template picker (agent/includes/inc_ticket_template_select.php) that
// fills in the form - its fields marked data-template-field.
//
// Rows submit as parallel tasks[] and task_estimates[] arrays, aligned by their
// order in the form, so removing a row removes both of its inputs together.
//
// Wrapped in an IIFE because a modal can be opened, closed and opened again in one
// page load, which re-runs this file - top-level const/let would throw on the
// second run. Delegated handlers are namespaced and unbound first for the same
// reason, otherwise "Add Task" would add one row per time the modal was opened.
(function () {
// Builds one task row. Values are assigned as properties rather than built into
// markup, so a task name containing quotes or angle brackets needs no escaping.
function buildTaskRow(taskName, taskEstimate) {
const row = document.createElement("div");
row.className = "row g-2 mb-2 ticket-task-row";
const nameColumn = document.createElement("div");
nameColumn.className = "col-7";
const nameInput = document.createElement("input");
nameInput.type = "text";
nameInput.className = "form-control";
nameInput.name = "tasks[]";
nameInput.placeholder = "Task name";
nameInput.maxLength = 255;
nameInput.value = taskName || '';
nameColumn.appendChild(nameInput);
const estimateColumn = document.createElement("div");
estimateColumn.className = "col-3";
const estimateInput = document.createElement("input");
estimateInput.type = "number";
estimateInput.className = "form-control";
estimateInput.name = "task_estimates[]";
estimateInput.placeholder = "Mins";
estimateInput.min = 0;
estimateInput.value = taskEstimate || '';
estimateColumn.appendChild(estimateInput);
const removeColumn = document.createElement("div");
removeColumn.className = "col-2";
const removeButton = document.createElement("button");
removeButton.type = "button";
removeButton.className = "btn btn-secondary w-100 ticket-task-remove";
removeButton.title = "Remove task";
removeButton.innerHTML = '<i class="fa fa-fw fa-trash"></i>';
removeColumn.appendChild(removeButton);
row.appendChild(nameColumn);
row.appendChild(estimateColumn);
row.appendChild(removeColumn);
return row;
}
function addTaskRow(taskName, taskEstimate) {
const container = document.getElementById("ticketTasksContainer");
if (!container) {
return;
}
container.appendChild(buildTaskRow(taskName, taskEstimate));
}
// Replaces the whole list - used when a template is picked
function setTaskRows(tasks) {
const container = document.getElementById("ticketTasksContainer");
if (!container) {
return;
}
container.innerHTML = '';
(tasks || []).forEach(task => {
addTaskRow(task.name, task.estimate);
});
}
document.addEventListener('click', function (e) {
if (!e.target.closest('#ticketTaskAdd')) {
return;
}
addTaskRow('', '');
});
document.addEventListener('click', function (e) {
const remove = e.target.closest('.ticket-task-remove');
if (!remove) {
return;
}
const row = remove.closest('.ticket-task-row');
if (row) {
row.remove();
}
});
// The form's field for one thing a template sets - marked data-template-field
function templateField(form, name) {
return form.querySelector('[data-template-field="' + name + '"]');
}
// Pick a value in a <select> when it has it - the picker redraws by itself
function pickOption(select, value) {
if (!select || value === '' || value === undefined) {
return;
}
const option = Array.from(select.options).find(o => o.value === String(value));
if (option) {
select.value = option.value;
select.dispatchEvent(new Event('change', { bubbles: true }));
}
}
// Write a template into its form: what it sets, field by field. A field
// the form doesn't have is skipped, and anything the template leaves
// alone ('' on the option) stays as it is.
function applyTemplate(select) {
const option = select.options[select.selectedIndex];
const form = select.closest('form');
// Selecting "- No Template -" only unlinks the template - it must not wipe
// whatever the user has already written or added
if (!option || !parseInt(option.value, 10) || !form) {
return;
}
const data = option.dataset;
const subject = templateField(form, 'subject') || document.getElementById('subjectInput');
if (subject && data.templateSubject) {
subject.value = data.templateSubject;
}
const details = templateField(form, 'details') || document.getElementById('detailsInput');
if (details && data.templateDetails) {
const editor = window.tinymce && details.id ? tinymce.get(details.id) : null;
if (editor) {
editor.setContent(data.templateDetails);
} else {
details.value = data.templateDetails;
}
}
pickOption(templateField(form, 'priority'), data.templatePriority);
pickOption(templateField(form, 'category'), data.templateCategory);
pickOption(templateField(form, 'assigned'), data.templateAssigned);
const billable = templateField(form, 'billable');
if (billable && data.templateBillable !== '' && data.templateBillable !== undefined) {
billable.checked = data.templateBillable === '1';
}
const due = templateField(form, 'due');
if (due && data.templateDue) {
due.value = data.templateDue;
}
// Its tags are added to any already picked
const tags = templateField(form, 'tags');
if (tags && data.templateTags) {
data.templateTags.split(',').forEach(function (id) {
const tag = Array.from(tags.options).find(o => o.value === id);
if (tag) {
tag.selected = true;
}
});
tags.dispatchEvent(new Event('change', { bubbles: true }));
}
if (document.getElementById('ticketTasksContainer')) {
let tasks = [];
try {
tasks = JSON.parse(data.templateTasks || '[]');
} catch (error) {
tasks = [];
}
setTaskRows(tasks);
}
}
document.addEventListener('change', function (e) {
const select = e.target.closest('[data-template-picker]');
if (select) {
applyTemplate(select);
}
});
// Opened for one template - New ticket from it - it is filled in straight away
document.querySelectorAll('[data-template-picker][data-template-apply]').forEach(function (select) {
select.removeAttribute('data-template-apply');
applyTemplate(select);
});
})();