/* * The ticket page's reply form (agent/ticket.php): * * - Drafts. What is typed is kept as it is written (agent/ajax.php ticket_reply_draft -> * functions/ticket_reply_draft.php) - a moment after typing stops, and as the page is left - and * the page puts it back in the reply box next time. Discard it drops it. Sending drops it too, * on the server. * - Files dropped on the reply box go in its Attach files field. * - +15m, +30m and +1h add to Time worked. The timer stops at the new total, as it does when the * time is typed. * - A reply with something in it and no time, on a billable ticket, asks first. * - Undo send: an emailed or texted reply waits the form's data-undo-seconds (Admin > Settings > Tickets, * functions/ticket_reply_send.php) under the reply box, locked, with Undo and Send now. Nothing is posted until then, * so Undo leaves everything as it was, the reply still in the box and kept as a draft. Last of the checks: it holds * only a reply nothing else held back, and lets it go with every check already answered. */ (function () { 'use strict'; var form = document.getElementById('ticketReplyForm'); if (!form) { return; } var ticketId = form.querySelector('[name="ticket_id"]').value; var csrf = form.querySelector('[name="csrf_token"]').value; var composer = document.getElementById('replyComposer'); var status = form.querySelector('[data-reply-draft-status]'); // Only once it is ready - reading an editor still starting up throws function editor() { var ed = window.tinymce ? tinymce.get('ticket_reply') : null; return ed && ed.initialized ? ed : null; } function content() { var ed = editor(); if (ed) { return ed.getContent(); } var textarea = document.getElementById('ticket_reply'); return textarea ? textarea.value : ''; } function hasWords() { var ed = editor(); var text = ed ? ed.getContent({ format: 'text' }) : content().replace(/<[^>]*>/g, ''); return text.replace(/ /g, ' ').trim() !== '' || / 50) { var textarea = document.getElementById('ticket_reply'); if (textarea) { lastSaved = textarea.value; textarea.addEventListener('input', queueSave); } return; } setTimeout(waitForEditor, 200); })(); // Leaving with something not yet kept - it goes now window.addEventListener('pagehide', function () { if (sending || lastSaved === null || !navigator.sendBeacon) { return; } var html = content(); if (html === lastSaved) { return; } var data = new FormData(); data.append('ticket_reply_draft', '1'); data.append('csrf_token', csrf); data.append('ticket_id', ticketId); data.append('html', html); navigator.sendBeacon('ajax.php', data); }); var discard = form.querySelector('[data-reply-draft-discard]'); if (discard) { discard.addEventListener('click', function (e) { e.preventDefault(); var drop = function () { clearTimeout(saveTimer); itflowPost('ajax.php', { ticket_reply_draft: 1, csrf_token: csrf, ticket_id: ticketId, html: '' }, function () { var ed = editor(); if (ed) { ed.setContent(''); ed.setDirty(false); } else if (document.getElementById('ticket_reply')) { document.getElementById('ticket_reply').value = ''; } lastSaved = content(); var note = form.querySelector('[data-reply-draft-note]'); if (note) { note.remove(); } setStatus(''); }); }; if (typeof Swal === 'undefined') { drop(); return; } Swal.fire({ theme: document.documentElement.getAttribute('data-bs-theme') === 'dark' ? 'dark' : 'light', title: 'Discard your unsent reply?', icon: 'warning', showCancelButton: true, confirmButtonText: 'Discard', cancelButtonText: 'Keep it', reverseButtons: true, focusCancel: true, buttonsStyling: false, customClass: { confirmButton: 'btn btn-danger mx-1', cancelButton: 'btn btn-secondary mx-1' } }).then(function (result) { if (result.isConfirmed) { drop(); } }); }); } } // ---- Files dropped on the reply box var fileInput = document.getElementById('ticket_reply_attachments'); var dropBox = form.querySelector('.card'); var filesNote = form.querySelector('[data-reply-files-note]'); var depth = 0; function carriesFiles(e) { return e.dataTransfer && Array.prototype.indexOf.call(e.dataTransfer.types || [], 'Files') !== -1; } function showFilesNote() { if (!filesNote || !fileInput) { return; } var count = fileInput.files.length; filesNote.hidden = !count || composerOpen(); filesNote.innerHTML = ''; filesNote.appendChild(document.createTextNode(count + (count === 1 ? ' file is' : ' files are') + ' ready to attach - pick how to send your reply.')); } if (fileInput && dropBox) { dropBox.addEventListener('dragenter', function (e) { if (carriesFiles(e)) { depth++; dropBox.classList.add('itflow-reply-drop'); } }); dropBox.addEventListener('dragleave', function (e) { if (carriesFiles(e)) { depth = Math.max(0, depth - 1); if (!depth) { dropBox.classList.remove('itflow-reply-drop'); } } }); dropBox.addEventListener('dragover', function (e) { if (carriesFiles(e)) { e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; } }); dropBox.addEventListener('drop', function (e) { if (!carriesFiles(e)) { return; } e.preventDefault(); depth = 0; dropBox.classList.remove('itflow-reply-drop'); var transfer = new DataTransfer(); Array.prototype.forEach.call(fileInput.files, function (file) { transfer.items.add(file); }); // A folder comes as an entry with no size and no type - left out Array.prototype.forEach.call(e.dataTransfer.files, function (file) { if (file.size || file.type) { transfer.items.add(file); } }); fileInput.files = transfer.files; fileInput.dispatchEvent(new Event('change', { bubbles: true })); showFilesNote(); }); if (composer) { composer.addEventListener('shown.bs.collapse', showFilesNote); } } // ---- +15m, +30m, +1h function pad(value) { return value < 10 ? '0' + value : String(value); } form.addEventListener('click', function (e) { var button = e.target.closest('[data-time-add-minutes]'); if (!button) { return; } var h = document.getElementById('hours'); var m = document.getElementById('minutes'); var s = document.getElementById('seconds'); if (!h || !m || !s) { return; } // The timer stops where it is first, as when the time is typed (agent/js/ticket_time_tracking.js) m.dispatchEvent(new Event('focus')); var total = (parseInt(h.value, 10) || 0) * 3600 + (parseInt(m.value, 10) || 0) * 60 + (parseInt(s.value, 10) || 0) + parseInt(button.dataset.timeAddMinutes, 10) * 60; total = Math.min(total, 23 * 3600 + 59 * 60 + 59); h.value = pad(Math.floor(total / 3600)); m.value = pad(Math.floor((total % 3600) / 60)); s.value = pad(total % 60); [h, m, s].forEach(function (field) { field.dispatchEvent(new Event('input', { bubbles: true })); field.dispatchEvent(new Event('change', { bubbles: true })); }); }); // ---- No time on a billable ticket function timeWorked() { var total = 0; [['hours', 3600], ['minutes', 60], ['seconds', 1]].forEach(function (part) { var field = document.getElementById(part[0]); total += field ? (parseInt(field.value, 10) || 0) * part[1] : 0; }); return total; } // After the page's own checks (the composer shut, the To line) - they are added as the page is read function onSubmit(e) { if (e.defaultPrevented) { return; } if (form.dataset.billable === '1' && form.dataset.noTimeOk !== '1' && composerOpen() && hasWords() && timeWorked() === 0 && document.getElementById('hours') && typeof Swal !== 'undefined') { e.preventDefault(); var submitter = e.submitter || null; Swal.fire({ theme: document.documentElement.getAttribute('data-bs-theme') === 'dark' ? 'dark' : 'light', title: 'No time on this reply', text: 'This ticket is billable. Send the reply without any time?', icon: 'question', showCancelButton: true, confirmButtonText: 'Send without time', cancelButtonText: 'Add time', reverseButtons: true, focusCancel: true, returnFocus: false, buttonsStyling: false, customClass: { confirmButton: 'btn btn-primary mx-1', cancelButton: 'btn btn-secondary mx-1' } }).then(function (result) { if (result.isConfirmed) { form.dataset.noTimeOk = '1'; if (typeof form.requestSubmit === 'function') { form.requestSubmit(submitter); } else { form.submit(); } } else { var minutes = document.getElementById('minutes'); if (minutes) { minutes.focus(); } } }); return; } // Going, unless something after this holds it back - nothing more to keep as a draft setTimeout(function () { if (!e.defaultPrevented) { sending = true; clearTimeout(saveTimer); } }, 0); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { form.addEventListener('submit', onSubmit); }); } else { form.addEventListener('submit', onSubmit); } // ---- Undo send var holdSeconds = parseInt(form.dataset.undoSeconds || '0', 10) || 0; var hold = form.querySelector('[data-reply-hold]'); var card = form.querySelector('.card'); var holding = null; function replyType() { var picked = form.querySelector('input[name="public_reply_type"]:checked'); return picked ? picked.value : ''; } function stopHold() { if (!holding) { return null; } var submitter = holding.submitter; clearInterval(holding.timer); holding = null; hold.hidden = true; if (card) { card.inert = false; card.classList.remove('opacity-50'); } return submitter; } // Go - every check before this one was answered on the way in, so none asks again function release() { var submitter = stopHold(); if (submitter === null) { return; } form.dataset.holdOk = '1'; form.dataset.cannedSendAnyway = '1'; form.dataset.noTimeOk = '1'; if (typeof form.requestSubmit === 'function') { form.requestSubmit(submitter); } else { form.submit(); } } function undo() { if (stopHold() === null) { return; } setStatus('Not sent - it is still here'); var ed = editor(); if (ed) { ed.focus(); } } if (holdSeconds > 0 && hold) { // On the window, so it hears the submit after the form's checks and the canned response check (on the document) window.addEventListener('submit', function (e) { if (e.target !== form) { return; } // Already waiting - a second Send waits with it if (holding) { e.preventDefault(); return; } var let_go = form.dataset.holdOk === '1'; delete form.dataset.holdOk; var type = replyType(); if (let_go || e.defaultPrevented || (type !== '2' && type !== '3') || !hasWords()) { return; } e.preventDefault(); var left = holdSeconds; var count = hold.querySelector('[data-reply-hold-count]'); hold.querySelector('[data-reply-hold-what]').textContent = type === '3' ? 'Texting it' : 'Emailing it'; count.textContent = left; hold.hidden = false; if (card) { card.inert = true; card.classList.add('opacity-50'); } holding = { submitter: e.submitter || form.querySelector('#ticket_add_reply'), timer: setInterval(function () { left -= 1; count.textContent = Math.max(0, left); if (left <= 0) { release(); } }, 1000) }; hold.querySelector('[data-reply-hold-undo]').focus(); }); hold.querySelector('[data-reply-hold-undo]').addEventListener('click', undo); hold.querySelector('[data-reply-hold-now]').addEventListener('click', release); document.addEventListener('keydown', function (e) { if (holding && e.key === 'Escape') { e.preventDefault(); undo(); } }); // Leaving now would lose the send - the draft is kept, but say so first window.addEventListener('beforeunload', function (e) { if (holding) { e.preventDefault(); e.returnValue = ''; } }); } })();