| 1 |
function ticketFileUpload(inputId, nonce) { |
| 2 |
var input = document.getElementById(inputId); |
| 3 |
if (!input) return; |
| 4 |
|
| 5 |
var wrapper = input.closest('.ticket-file-upload'); |
| 6 |
var previews = wrapper.querySelector('.ticket-file-upload__previews'); |
| 7 |
|
| 8 |
input.addEventListener('change', function () { |
| 9 |
var file = this.files[0]; |
| 10 |
if (!file) return; |
| 11 |
this.value = ''; |
| 12 |
|
| 13 |
var objectUrl = URL.createObjectURL(file); |
| 14 |
var item = document.createElement('div'); |
| 15 |
item.className = 'ticket-file-upload__preview-item ticket-file-upload__preview-item--loading'; |
| 16 |
item.innerHTML = |
| 17 |
'<img src="' + objectUrl + '" class="ticket-file-upload__preview-img" alt="">' + |
| 18 |
'<div class="ticket-file-upload__preview-info">' + |
| 19 |
'<span class="ticket-file-upload__preview-name">' + file.name + '</span>' + |
| 20 |
'<span class="ticket-file-upload__preview-status">در حال آپلود...</span>' + |
| 21 |
'</div>' + |
| 22 |
'<button type="button" class="ticket-file-upload__preview-remove" aria-label="حذف">×</button>'; |
| 23 |
|
| 24 |
previews.appendChild(item); |
| 25 |
|
| 26 |
item.querySelector('.ticket-file-upload__preview-remove').addEventListener('click', function () { |
| 27 |
URL.revokeObjectURL(objectUrl); |
| 28 |
item.remove(); |
| 29 |
}); |
| 30 |
|
| 31 |
var fd = new FormData(); |
| 32 |
fd.append('action', 'upload_ticket_media'); |
| 33 |
fd.append('_wpnonce', nonce); |
| 34 |
fd.append('file', file); |
| 35 |
|
| 36 |
fetch(ajaxurl, { method: 'POST', body: fd }) |
| 37 |
.then(function (r) { return r.json(); }) |
| 38 |
.then(function (res) { |
| 39 |
item.classList.remove('ticket-file-upload__preview-item--loading'); |
| 40 |
var statusEl = item.querySelector('.ticket-file-upload__preview-status'); |
| 41 |
if (res.success) { |
| 42 |
item.classList.add('ticket-file-upload__preview-item--done'); |
| 43 |
statusEl.textContent = 'آپلود شد'; |
| 44 |
var hidden = document.createElement('input'); |
| 45 |
hidden.type = 'hidden'; |
| 46 |
hidden.name = 'file_ids[]'; |
| 47 |
hidden.value = res.data.file_id; |
| 48 |
item.appendChild(hidden); |
| 49 |
} else { |
| 50 |
item.classList.add('ticket-file-upload__preview-item--error'); |
| 51 |
statusEl.textContent = (res.data && res.data.message) ? res.data.message : 'خطا در آپلود'; |
| 52 |
} |
| 53 |
}) |
| 54 |
.catch(function () { |
| 55 |
item.classList.remove('ticket-file-upload__preview-item--loading'); |
| 56 |
item.classList.add('ticket-file-upload__preview-item--error'); |
| 57 |
item.querySelector('.ticket-file-upload__preview-status').textContent = 'خطا در آپلود'; |
| 58 |
}); |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
(function (window, document) { |
| 63 |
'use strict'; |
| 64 |
|
| 65 |
var sections = [ |
| 66 |
{ |
| 67 |
prefix: 'dashboard', |
| 68 |
label: 'پیشخوان وردپرس' |
| 69 |
}, |
| 70 |
{ |
| 71 |
prefix: 'host_panel', |
| 72 |
label: 'کنترل پنل هاست' |
| 73 |
} |
| 74 |
]; |
| 75 |
|
| 76 |
function showError(message) { |
| 77 |
if (window.BasalamToast) { |
| 78 |
window.BasalamToast.error(message); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
function field(form, name) { |
| 83 |
return form.querySelector('[name="' + name + '"]'); |
| 84 |
} |
| 85 |
|
| 86 |
function validateAccess(form) { |
| 87 |
for (var i = 0; i < sections.length; i++) { |
| 88 |
var section = sections[i]; |
| 89 |
var loginUrl = field(form, section.prefix + '_login_url'); |
| 90 |
var username = field(form, section.prefix + '_username'); |
| 91 |
var password = field(form, section.prefix + '_password'); |
| 92 |
if (!loginUrl || !username || !password) continue; |
| 93 |
|
| 94 |
var loginValue = loginUrl.value.trim(); |
| 95 |
var usernameValue = username.value.trim(); |
| 96 |
var passwordValue = password.value; |
| 97 |
var hasAnyValue = loginValue !== '' || usernameValue !== '' || passwordValue !== ''; |
| 98 |
if (!hasAnyValue) continue; |
| 99 |
|
| 100 |
if (usernameValue === '' || passwordValue === '') { |
| 101 |
var missingField = usernameValue === '' ? username : password; |
| 102 |
missingField.setAttribute('aria-invalid', 'true'); |
| 103 |
missingField.focus(); |
| 104 |
showError('برای ' + section.label + ' نا� |
| 105 |
کاربری و ر� |
| 106 |
ز عبور را کا� |
| 107 |
ل وارد کنید.'); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
if (passwordValue.length > 1024 || /[\u0000-\u001F\u007F]/.test(passwordValue)) { |
| 112 |
password.setAttribute('aria-invalid', 'true'); |
| 113 |
password.focus(); |
| 114 |
showError(passwordValue.length > 1024 |
| 115 |
? 'ر� |
| 116 |
ز عبور طولانیتر از حد � |
| 117 |
جاز است.' |
| 118 |
: 'ر� |
| 119 |
ز عبور دارای نویسه کنترلی نا� |
| 120 |
عتبر است.'); |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
function setSubmitting(form, submitting) { |
| 129 |
var submitButton = form.querySelector('button[type="submit"]'); |
| 130 |
if (!submitButton) return; |
| 131 |
|
| 132 |
submitButton.disabled = submitting; |
| 133 |
if (submitting) { |
| 134 |
submitButton.dataset.originalText = submitButton.textContent; |
| 135 |
submitButton.textContent = 'در حال ارسال...'; |
| 136 |
} else if (submitButton.dataset.originalText) { |
| 137 |
submitButton.textContent = submitButton.dataset.originalText; |
| 138 |
delete submitButton.dataset.originalText; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
function rememberCreatedTicket(form, ticketId) { |
| 143 |
if (!ticketId) return; |
| 144 |
|
| 145 |
var input = field(form, 'existing_ticket_id'); |
| 146 |
if (!input) { |
| 147 |
input = document.createElement('input'); |
| 148 |
input.type = 'hidden'; |
| 149 |
input.name = 'existing_ticket_id'; |
| 150 |
form.appendChild(input); |
| 151 |
} |
| 152 |
input.value = String(ticketId); |
| 153 |
} |
| 154 |
|
| 155 |
function submitCreateTicket(form) { |
| 156 |
setSubmitting(form, true); |
| 157 |
|
| 158 |
fetch(window.ajaxurl, { |
| 159 |
method: 'POST', |
| 160 |
body: new FormData(form), |
| 161 |
credentials: 'same-origin' |
| 162 |
}) |
| 163 |
.then(function (response) { |
| 164 |
return response.json(); |
| 165 |
}) |
| 166 |
.then(function (response) { |
| 167 |
var data = response && response.data ? response.data : {}; |
| 168 |
if (!response || !response.success) { |
| 169 |
rememberCreatedTicket(form, data.ticket_id); |
| 170 |
showError(data.message || 'خطایی در ثبت تیکت رخ داد. اطلاعات فر� |
| 171 |
حفظ شده است.'); |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if (data.redirect_url) { |
| 176 |
window.location.assign(data.redirect_url); |
| 177 |
} |
| 178 |
}) |
| 179 |
.catch(function () { |
| 180 |
showError('ارتباط با سرور برقرار نشد. اطلاعات فر� |
| 181 |
حفظ شده است.'); |
| 182 |
}) |
| 183 |
.finally(function () { |
| 184 |
setSubmitting(form, false); |
| 185 |
}); |
| 186 |
} |
| 187 |
|
| 188 |
document.addEventListener('DOMContentLoaded', function () { |
| 189 |
var actionInputs = document.querySelectorAll( |
| 190 |
'input[name="action"][value="create_ticket"], ' + |
| 191 |
'input[name="action"][value="create_ticket_item"]' |
| 192 |
); |
| 193 |
|
| 194 |
for (var i = 0; i < actionInputs.length; i++) { |
| 195 |
var form = actionInputs[i].closest('form'); |
| 196 |
if (!form || form.dataset.ticketAccessValidation === '1') continue; |
| 197 |
form.dataset.ticketAccessValidation = '1'; |
| 198 |
form.addEventListener('submit', function (event) { |
| 199 |
if (!validateAccess(event.currentTarget)) { |
| 200 |
event.preventDefault(); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
var action = field(event.currentTarget, 'action'); |
| 205 |
if (action && action.value === 'create_ticket') { |
| 206 |
event.preventDefault(); |
| 207 |
submitCreateTicket(event.currentTarget); |
| 208 |
} |
| 209 |
}); |
| 210 |
} |
| 211 |
}); |
| 212 |
})(window, document); |
| 213 |
|