// 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 - and draws what the // template asks for (its fill-ins) under it. // // 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. A // template task that is only for some answers - "Order a laptop" when Needs a // laptop is Yes - is hidden, and left out of the form, while the answer is // another one; until it is answered it stays. // // 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. The document-wide listeners are bound on the first run only (the // window flag), or every modal opened would add one more of each and "Add Task" // would add a row per time the window had been 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. // when: {fill_in, value} - the answer the task is for, or nothing. function buildTaskRow(taskName, taskEstimate, when) { 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); if (when && when.fill_in) { row.dataset.whenFillIn = when.fill_in; row.dataset.whenValue = when.value || ''; const note = document.createElement("div"); note.className = "form-text mt-1"; note.innerHTML = ''; note.appendChild(document.createTextNode('Only when ' + when.fill_in + ' is ' + (when.value || ''))); nameColumn.appendChild(note); } 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 = ''; removeColumn.appendChild(removeButton); row.appendChild(nameColumn); row.appendChild(estimateColumn); row.appendChild(removeColumn); return row; } function addTaskRow(taskName, taskEstimate, when) { const container = document.getElementById("ticketTasksContainer"); if (!container) { return; } container.appendChild(buildTaskRow(taskName, taskEstimate, when)); } // 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, task.when); }); } // 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