874 lines
27 KiB
JavaScript
874 lines
27 KiB
JavaScript
|
||
|
||
document.getElementById("MainModal").style.visibility = "visible";
|
||
|
||
|
||
|
||
$(document).on('keydown', '#EndTaskDate', function () {
|
||
$("input:radio").prop('checked', false);
|
||
});
|
||
|
||
$(document).ready(function () {
|
||
// نمایش توضیحات و رفع نمایش br
|
||
var textarea = $('#Command_Description');
|
||
var originalText = textarea.val();
|
||
textarea.val(originalText.replace(/<br\s*\/?>/gi, "\n"));
|
||
textarea.on('input', function () {
|
||
var updatedText = textarea.val().replace(/<br\s*\/?>/gi, "\n");
|
||
textarea.val(updatedText);
|
||
});
|
||
// نمایش توضیحات و رفع نمایش br
|
||
|
||
$(document).on('click', '#today', function () {
|
||
updateDateInput(0);
|
||
$("#today").prop('checked', true);
|
||
});
|
||
|
||
$(document).on('click', '#tommorow', function () {
|
||
updateDateInput(1);
|
||
$("#tommorow").prop('checked', true);
|
||
});
|
||
|
||
$(document).on('click', '#two-day-later', function () {
|
||
updateDateInput(2);
|
||
$("#two-day-later").prop('checked', true);
|
||
});
|
||
|
||
|
||
$('.select2Member').select2({
|
||
placeholder: {
|
||
id: '-1', // the value of the option
|
||
text: "انتخاب اعضاء ..."
|
||
}
|
||
});
|
||
|
||
$('.select2Group').select2({
|
||
placeholder: {
|
||
id: '-1', // the value of the option
|
||
text: "انتخاب گروهی ..."
|
||
}
|
||
});
|
||
|
||
// $(document).on('click', '#memberSelect', function () {
|
||
// $('#select2MemberList').show();
|
||
// $('#select2GroupList').hide();
|
||
// if ($('#memberSelect').is(':checked')) {
|
||
// $('#Command_PositionId').val(null).trigger('change');
|
||
// }
|
||
|
||
// });
|
||
|
||
// $(document).on('click', '#groupSelect', function () {
|
||
// $('#select2MemberList').hide();
|
||
// $('#select2GroupList').show();
|
||
// if ($('#groupSelect').is(':checked')) {
|
||
// $('#Command_ReceiverId').val(null).trigger('change');
|
||
// }
|
||
// });
|
||
|
||
$('#save').on('click', function (e) {
|
||
e.preventDefault();
|
||
|
||
var member = $('.select2Member').select2('data');
|
||
//var group = $('.select2Group').select2('data');
|
||
|
||
if ($('#memberSelect').is(':checked')) {
|
||
if (member.length == 0) {
|
||
$('#select2MemberList').addClass('errored');
|
||
$('.alert-msg').show();
|
||
$('.alert-msg p').text('لطفا انتخاب اعضاء را مشخص نمائید.');
|
||
setTimeout(function () {
|
||
$('.alert-msg').hide();
|
||
$('.alert-msg p').text('');
|
||
$('#select2MemberList').removeClass('errored');
|
||
}, 3500);
|
||
return;
|
||
}
|
||
}
|
||
|
||
// if ($('#groupSelect').is(':checked')) {
|
||
// if (group.length == 0) {
|
||
// $('#select2GroupList').addClass('errored');
|
||
// $('.alert-msg').show();
|
||
// $('.alert-msg p').text('لطفا انتخاب گروهی را مشخص نمائید.');
|
||
// setTimeout(function () {
|
||
// $('.alert-msg').hide();
|
||
// $('.alert-msg p').text('');
|
||
// $('#select2GroupList').removeClass('errored');
|
||
// }, 3500);
|
||
// return;
|
||
// }
|
||
// }
|
||
|
||
|
||
var textArea = $('#Command_Description');
|
||
var content = textArea.val();
|
||
/* content = content.replace(/\n/g, '<br>');*/
|
||
textArea.val(content);
|
||
|
||
|
||
var data = new FormData(this.form);
|
||
|
||
$.ajax({
|
||
async: false,
|
||
dataType: 'json',
|
||
type: 'POST',
|
||
processData: false,
|
||
contentType: false,
|
||
url: EditSaveTask ,
|
||
headers: { "RequestVerificationToken": AntiForgeryToken },
|
||
data: data,
|
||
success: function (response) {
|
||
console.log(response);
|
||
if (response.isSuccess) {
|
||
$('.alert-success-msg').show();
|
||
$('.alert-success-msg p').text(response.message);
|
||
setTimeout(function () {
|
||
$('.alert-success-msg').hide();
|
||
$('.alert-success-msg p').text('');
|
||
window.location.replace('/AdminNew/Company/Task');
|
||
}, 1500);
|
||
|
||
} else {
|
||
if (response.message == 'لطفا عنوان وظیفه خود ار وارد کنید') {
|
||
$('#Command_Title').addClass('errored');
|
||
$('.alert-msg p').text('لطفا انتخاب گروهی را مشخص نمائید.');
|
||
setTimeout(function () {
|
||
$('#Command_Title').removeClass('errored');
|
||
}, 3500);
|
||
}
|
||
|
||
if (response.message == 'لطفا طرف حساب خودرا وارد کنید') {
|
||
$('#partyNameSearch').addClass('errored');
|
||
$('.alert-msg p').text('لطفا انتخاب گروهی را مشخص نمائید.');
|
||
setTimeout(function () {
|
||
$('#partyNameSearch').removeClass('errored');
|
||
}, 3500);
|
||
}
|
||
|
||
if (response.message == 'لطفا تاریخ انجام وظیفه را وارد کنید') {
|
||
$('#Command_EndTaskDate').addClass('errored');
|
||
$('.alert-msg p').text('لطفا انتخاب گروهی را مشخص نمائید.');
|
||
setTimeout(function () {
|
||
$('#Command_EndTaskDate').removeClass('errored');
|
||
}, 3500);
|
||
}
|
||
|
||
$('.alert-msg').show();
|
||
$('.alert-msg p').text(response.message);
|
||
setTimeout(function () {
|
||
$('.alert-msg').hide();
|
||
$('.alert-msg p').text('');
|
||
}, 3500);
|
||
}
|
||
},
|
||
error: function (err) {
|
||
console.log(err);
|
||
}
|
||
});
|
||
});
|
||
|
||
|
||
//$(".date").mask("0000/00/00");
|
||
$(".date").on('input', function () {
|
||
var value = $(this).val();
|
||
$(this).val(convertPersianNumbersToEnglish(value));
|
||
}).mask("0000/00/00");
|
||
|
||
$('.date').on('input', function () {
|
||
let startDate = this.value;
|
||
if (startDate.length == 10) {
|
||
let submitcheck = dateValidcheck(this);
|
||
if (submitcheck) {
|
||
|
||
$(this).removeClass('errored');
|
||
} else {
|
||
$(this).addClass('errored');
|
||
|
||
}
|
||
|
||
} else {
|
||
$(this).addClass('errored');
|
||
}
|
||
});
|
||
|
||
$('#EndTaskTime').on("keyup", function () {
|
||
var isValid = /^([2][0-3]|[1][0-9]|[0-9]|[0][0-9])([:][0-5][0-9])$/.test($(this).val());
|
||
if (isValid) {
|
||
$(this).addClass("validTime");
|
||
$(this).removeClass("invalidTime");
|
||
// if ($('#EndTaskTime').val() != null) {
|
||
// computeDays();
|
||
// }
|
||
} else {
|
||
$(this).removeClass("validTime");
|
||
$(this).addClass("invalidTime");
|
||
}
|
||
});
|
||
|
||
});
|
||
|
||
function updateDateInput(daysToAdd) {
|
||
var today = new Date();
|
||
today.setDate(today.getDate() + daysToAdd);
|
||
var jalaaliDate = jalaali.toJalaali(today);
|
||
var formattedDate = jalaaliDate.jy + '/' + (jalaaliDate.jm < 10 ? '0' + jalaaliDate.jm : jalaaliDate.jm) + '/' + (jalaaliDate.jd < 10 ? '0' + jalaaliDate.jd : jalaaliDate.jd);
|
||
$('#EndTaskDate').val(formattedDate);
|
||
//computeDays();
|
||
}
|
||
|
||
function dateValidcheck(inputField1) {
|
||
|
||
let persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
|
||
arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
|
||
fixNumbers = function (str) {
|
||
if (typeof str === 'string') {
|
||
for (var i = 0; i < 10; i++) {
|
||
str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
|
||
}
|
||
}
|
||
return str;
|
||
};
|
||
let getdate = inputField1.value;
|
||
|
||
let m1, m2;
|
||
let y1, y2, y3, y4;
|
||
let d1, d2;
|
||
let s1, s2;
|
||
for (var i = 0; i < getdate.length; i++) {
|
||
if (i === 0) {
|
||
y1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 1) {
|
||
y2 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 2) {
|
||
y3 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 3) {
|
||
y4 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 4) {
|
||
s1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 5) {
|
||
m1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 6) {
|
||
m2 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 7) {
|
||
s2 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 8) {
|
||
d1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 9) {
|
||
d2 = fixNumbers(getdate[i]);
|
||
}
|
||
|
||
}
|
||
let yRes = y1 + y2 + y3 + y4;
|
||
let year = parseInt(yRes);
|
||
let mRes = m1 + m2;
|
||
let month = parseInt(mRes);
|
||
let dRes = d1 + d2;
|
||
let day = parseInt(dRes);
|
||
let fixResult = yRes + s1 + mRes + s2 + dRes;
|
||
let test1 = checkEnValid(inputField1.value);
|
||
|
||
let isValid = /^([1][3-4][0-9][0-9][/])([0][1-9]|[1][0-2])([/])([0][1-9]|[1-2][0-9]|[3][0-1])$/.test(fixResult);
|
||
|
||
|
||
if (isValid && test1) {
|
||
// inputField1.style.backgroundColor = '#a6e9a6';
|
||
start1valid = true;
|
||
|
||
|
||
} else {
|
||
|
||
if (inputField1.value != "") {
|
||
// inputField1.style.backgroundColor = '#f94c4c';
|
||
$.Notification.autoHideNotify('error', 'top center', 'پیام سیستم ', "لطفا تاریخ را بصورت صحیح وارد کنید");
|
||
start1valid = false;
|
||
}
|
||
|
||
|
||
}
|
||
return start1valid;
|
||
|
||
}
|
||
|
||
function checkEnValid(fixDate1) {
|
||
|
||
let persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g],
|
||
arabicNumbers = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g],
|
||
fixNumbers = function (str) {
|
||
if (typeof str === 'string') {
|
||
for (var i = 0; i < 10; i++) {
|
||
str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);
|
||
}
|
||
}
|
||
return str;
|
||
};
|
||
let getdate = fixDate1;
|
||
|
||
let m1, m2;
|
||
let y1, y2, y3, y4;
|
||
let d1, d2;
|
||
for (let i = 0; i < getdate.length; i++) {
|
||
if (i === 0) {
|
||
y1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 1) {
|
||
y2 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 2) {
|
||
y3 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 3) {
|
||
y4 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 5) {
|
||
m1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 6) {
|
||
m2 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 8) {
|
||
d1 = fixNumbers(getdate[i]);
|
||
}
|
||
if (i === 9) {
|
||
d2 = fixNumbers(getdate[i]);
|
||
}
|
||
}
|
||
let yRes = y1 + y2 + y3 + y4;
|
||
let year = parseInt(yRes);
|
||
let mRes = m1 + m2;
|
||
let month = parseInt(mRes);
|
||
let dRes = d1 + d2;
|
||
let day = parseInt(dRes);
|
||
let kabiseh = false;
|
||
if (month <= 6 && day > 31) {
|
||
return false;
|
||
} else if (month > 6 && month < 12 && day > 30) {
|
||
return false;
|
||
} else if (month === 12) {
|
||
|
||
switch (year) {
|
||
case 1346:
|
||
kabiseh = true;
|
||
break;
|
||
case 1350:
|
||
kabiseh = true;
|
||
break;
|
||
case 1354:
|
||
kabiseh = true;
|
||
break;
|
||
case 1358:
|
||
kabiseh = true;
|
||
break;
|
||
case 1362:
|
||
kabiseh = true;
|
||
break;
|
||
case 1366:
|
||
kabiseh = true;
|
||
break;
|
||
case 1370:
|
||
kabiseh = true;
|
||
break;
|
||
case 1375:
|
||
kabiseh = true;
|
||
break;
|
||
case 1379:
|
||
kabiseh = true;
|
||
break;
|
||
case 1383:
|
||
kabiseh = true;
|
||
break;
|
||
case 1387:
|
||
kabiseh = true;
|
||
break;
|
||
case 1391:
|
||
kabiseh = true;
|
||
break;
|
||
case 1395:
|
||
kabiseh = true;
|
||
break;
|
||
case 1399:
|
||
kabiseh = true;
|
||
break;
|
||
case 1403:
|
||
kabiseh = true;
|
||
break;
|
||
case 1408:
|
||
kabiseh = true;
|
||
break;
|
||
case 1412:
|
||
kabiseh = true;
|
||
break;
|
||
case 1416:
|
||
kabiseh = true;
|
||
break;
|
||
case 1420:
|
||
kabiseh = true;
|
||
break;
|
||
case 1424:
|
||
kabiseh = true;
|
||
break;
|
||
case 1428:
|
||
kabiseh = true;
|
||
break;
|
||
case 1432:
|
||
kabiseh = true;
|
||
break;
|
||
case 1436:
|
||
kabiseh = true;
|
||
break;
|
||
case 1441:
|
||
kabiseh = true;
|
||
break;
|
||
case 1445:
|
||
kabiseh = true;
|
||
break;
|
||
default:
|
||
kabiseh = false;
|
||
|
||
}
|
||
if (kabiseh == true && day > 30) {
|
||
return false;
|
||
} else if (kabiseh == false && day > 29) {
|
||
return false;
|
||
} else {
|
||
return true;
|
||
}
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
$(document).ready(function () {
|
||
$(document).on('click', '#upload-doc', function (event) {
|
||
event.preventDefault();
|
||
|
||
for (let i = 1; i <= 6; i++) {
|
||
let fileInput = document.getElementById(`EditTask_Document${i}`);
|
||
if (fileInput && fileInput.type === 'file' && fileInput.files.length === 0) {
|
||
attachFileChangeHandler(fileInput, i);
|
||
fileInput.click(); // Trigger file selection
|
||
return; // Exit loop after triggering the first empty input
|
||
}
|
||
}
|
||
});
|
||
});
|
||
|
||
function attachFileChangeHandler(fileInput, index) {
|
||
$(fileInput).off('change').on('change', function () {
|
||
let file = fileInput.files[0];
|
||
if (file) {
|
||
uploadFile(file, index, `.inBox${index}`);
|
||
}
|
||
});
|
||
}
|
||
|
||
var indexCount = 0;
|
||
var indexDeleteCount = 0;
|
||
function uploadFile(file, id, boxClass) {
|
||
var formData = new FormData();
|
||
formData.append('media', file);
|
||
|
||
console.log(file);
|
||
var loading = $(boxClass).find('.spinner-loading');
|
||
loading.show();
|
||
|
||
$.ajax({
|
||
dataType: 'json',
|
||
type: 'POST',
|
||
processData: false,
|
||
contentType: false,
|
||
url: uploadFileAjax,
|
||
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
|
||
data: formData,
|
||
success: function (response) {
|
||
loading.hide();
|
||
if (response.isSuccedded) {
|
||
if (file) {
|
||
let reader = new FileReader();
|
||
reader.onload = function (e) {
|
||
let img = $(`<img class="b${id}">`).attr('src', e.target.result);
|
||
let box = $(boxClass);
|
||
if (box.length) {
|
||
box.removeClass('empty');
|
||
let deleteBtn = $(`<div class="b${id}" style="cursor: pointer;" onclick="remove(${id}, ${response.id})">
|
||
<svg width="16" height="16" viewBox="0 0 10 10" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||
<rect width="10" height="10" rx="5" fill="#FF4A4A"/>
|
||
<path d="M7 7L3 3" stroke="#FFFEFE" stroke-linecap="round" stroke-linejoin="round"/>
|
||
<path d="M3 7L7 3" stroke="#FFFEFE" stroke-linecap="round" stroke-linejoin="round"/>
|
||
</svg>
|
||
</div>`);
|
||
box.find(`.b${id}`).remove(); // Remove any existing delete buttons
|
||
box.append(img);
|
||
box.append(deleteBtn);
|
||
}
|
||
};
|
||
reader.readAsDataURL(file);
|
||
}
|
||
|
||
var inputItems = `<input type="hidden" value="${response.id}" name="EditTask.UploadedMedia[${indexCount}]"/>`;
|
||
$('#fileItems').append(inputItems);
|
||
indexCount++;
|
||
} else {
|
||
showAlertMessage('.alert-msg', response.message, 3500);
|
||
$('#EditTask_Document' + id).val('');
|
||
}
|
||
},
|
||
error: function (err) {
|
||
console.log(err);
|
||
loading.hide();
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
function remove(id, resId) {
|
||
$('#EditTask_Document' + id).val('');
|
||
$('.inBox' + id).find('img, div.b' + id).remove();
|
||
$('.inBox' + id).addClass('empty');
|
||
|
||
var input = `<input type="file" class="d-none" name="EditTask.Document${id}" id="EditTask_Document${id}" accept=".pdf,.doc,.docx,.txt, image/*">`;
|
||
var inputDelete = `<input type="hidden" value="${resId}" name="EditTask.DeletedFileIds[${indexDeleteCount}]"/>`;
|
||
|
||
$('.inBox' + id).html(input);
|
||
$('#deletedItems').append(inputDelete);
|
||
indexDeleteCount++;
|
||
|
||
var loading = $('.inBox' + id + ' .spinner-loading');
|
||
loading.show();
|
||
}
|
||
|
||
function showAlertMessage(selector, message, timeout) {
|
||
$(selector).show();
|
||
$(selector + ' p').text(message);
|
||
setTimeout(function () {
|
||
$(selector).hide();
|
||
$(selector + ' p').text('');
|
||
}, timeout);
|
||
}
|
||
|
||
|
||
function removeVoice(id) {
|
||
console.log('#EditTask_Voice');
|
||
$('#EditTask_Voice').val('');
|
||
$('#NewVoice').removeClass('d-none');
|
||
$('#NewVoice').addClass('d-block d-md-flex');
|
||
$('#OldVoice').addClass('d-none');
|
||
$('#OldVoice').removeClass('d-block d-md-flex');
|
||
|
||
// var inputDelete = `<input type="hidden" value="${id}" name="EditTask.DeletedFileVoice"/>"`;
|
||
var inputDelete = `<input type="hidden" value="${id}" name="EditTask.DeletedFileIds[${indexCount}]"/>"`;
|
||
|
||
$('#deletedItems').append(inputDelete);
|
||
}
|
||
|
||
|
||
|
||
// نمایش اسامی کارگاه
|
||
var container = $('#partyName');
|
||
var searchBox = $('#partyNameSearch');
|
||
var searchResul = $('.searchResult');
|
||
var mixContainerAndSerchResult = $('#partyName , .searchResult');
|
||
|
||
container.hide();
|
||
var liList;
|
||
var liPointer;
|
||
var count = 0;
|
||
|
||
//close search Employee when click on body
|
||
$(document).on('click', function (event) {
|
||
if (!$(event.target).closest(container).length) {
|
||
container.hide();
|
||
}
|
||
});
|
||
//select option by mouse
|
||
function selectItem(employeeFullName) {
|
||
searchBox.val(employeeFullName);
|
||
container.hide();
|
||
};
|
||
//search by Ajax
|
||
searchBox.on('keyup keypress',
|
||
function (e) {
|
||
searchResul.html('');
|
||
|
||
//stop submit form with enter
|
||
var keyCode = e.keyCode || e.which;
|
||
if (keyCode === 13) {
|
||
e.preventDefault();
|
||
if (count > 0 && count <= liList.length) {
|
||
liPointer.click();
|
||
}
|
||
return false;
|
||
}
|
||
|
||
let searchText = $(this).val();
|
||
|
||
if (searchText.length > 1) {
|
||
|
||
$.ajax({
|
||
async: false,
|
||
contentType: 'charset=utf-8',
|
||
dataType: 'json',
|
||
type: 'GET',
|
||
url: SearchContractingParties,
|
||
data: { "searchName": searchText },
|
||
headers: { "RequestVerificationToken": AntiForgeryToken },
|
||
|
||
success: function (response) {
|
||
$(".opt").remove();
|
||
if (response.list.length > 0) {
|
||
$(".noResult").remove();
|
||
container.show();
|
||
$.each(response.list,
|
||
function (i, item) {
|
||
let li = `<li data-employeeId="${item.id}" onclick="selectItem('${item.fullName}');" >${item.fullName}</li>`;
|
||
searchResul.append(li);
|
||
});
|
||
} else {
|
||
$(".noResult").remove();
|
||
container.show();
|
||
let noResult = `<li class="noResult">نتیجه ای یافت نشد!</li>`;
|
||
searchResul.append(noResult);
|
||
}
|
||
}// endOfSuccess
|
||
|
||
}); //endOfAjax
|
||
|
||
} else {
|
||
container.hide();
|
||
count = 0;
|
||
}
|
||
});
|
||
// نمایش اسامی کارگاه
|
||
|
||
|
||
|
||
|
||
// صدا
|
||
var msg_box = document.getElementById('msg_box'),
|
||
button = document.getElementById('upload-voice'),
|
||
canvas = document.getElementById('canvas'),
|
||
lang = {
|
||
'mic_error': 'خطا! به میکروفون دسترسی ندارد.',
|
||
'press_to_start': 'برای ضبط صدا، پیام صوتی را کلیک نمائید',
|
||
'recording': 'در حال ضبط پیام صوتی ...',
|
||
'play': 'پخش',
|
||
'stop': 'متوقف',
|
||
'download': 'دانلود',
|
||
'use_https': 'This application in not working over insecure connection. Try to use HTTPS'
|
||
},
|
||
time;
|
||
|
||
msg_box.innerHTML = lang.press_to_start;
|
||
|
||
if (navigator.mediaDevices === undefined) {
|
||
navigator.mediaDevices = {};
|
||
}
|
||
|
||
if (navigator.mediaDevices.getUserMedia === undefined) {
|
||
navigator.mediaDevices.getUserMedia = function (constrains) {
|
||
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia
|
||
if (!getUserMedia) {
|
||
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
|
||
}
|
||
|
||
return new Promise(function (resolve, reject) {
|
||
getUserMedia.call(navigator, constrains, resolve, reject);
|
||
});
|
||
}
|
||
}
|
||
|
||
if (navigator.mediaDevices.getUserMedia) {
|
||
var btn_status = 'inactive',
|
||
mediaRecorder,
|
||
chunks = [],
|
||
audio = new Audio(),
|
||
mediaStream,
|
||
audioSrc,
|
||
type = {
|
||
'type': 'audio/ogg,codecs=opus'
|
||
},
|
||
ctx,
|
||
analys,
|
||
blob;
|
||
|
||
button.onclick = function () {
|
||
if (btn_status == 'inactive') {
|
||
start();
|
||
} else if (btn_status == 'recording') {
|
||
stop();
|
||
}
|
||
}
|
||
|
||
function parseTime(sec) {
|
||
var h = parseInt(sec / 3600);
|
||
var m = parseInt(sec / 60);
|
||
var sec = sec - (h * 3600 + m * 60);
|
||
|
||
h = h == 0 ? '' : h + ':';
|
||
sec = sec < 10 ? '0' + sec : sec;
|
||
|
||
return h + m + ':' + sec;
|
||
}
|
||
|
||
function start() {
|
||
console.log('start')
|
||
navigator.mediaDevices.getUserMedia({ 'audio': true }).then(function (stream) {
|
||
mediaRecorder = new MediaRecorder(stream);
|
||
mediaRecorder.start();
|
||
|
||
button.classList.add('recording');
|
||
btn_status = 'recording';
|
||
|
||
msg_box.innerHTML = lang.recording;
|
||
|
||
if (navigator.vibrate) navigator.vibrate(150);
|
||
|
||
time = Math.ceil(new Date().getTime() / 1000);
|
||
|
||
mediaRecorder.ondataavailable = function (event) {
|
||
chunks.push(event.data);
|
||
}
|
||
|
||
mediaRecorder.onstop = function () {
|
||
stream.getTracks().forEach(function (track) { track.stop() });
|
||
|
||
blob = new Blob(chunks, type);
|
||
audioSrc = window.URL.createObjectURL(blob);
|
||
|
||
audio.src = audioSrc;
|
||
|
||
chunks = [];
|
||
|
||
// Save the Blob to the input element
|
||
saveToInput(blob);
|
||
}
|
||
|
||
}).catch(function (error) {
|
||
if (location.protocol != 'https:') {
|
||
msg_box.innerHTML = lang.mic_error + '<br>' + lang.use_https;
|
||
} else {
|
||
msg_box.innerHTML = lang.mic_error;
|
||
}
|
||
button.disabled = true;
|
||
});
|
||
}
|
||
|
||
function stop() {
|
||
console.log('stop');
|
||
mediaRecorder.stop();
|
||
button.classList.remove('recording');
|
||
btn_status = 'inactive';
|
||
|
||
if (navigator.vibrate) navigator.vibrate([200, 100, 200]);
|
||
|
||
var now = Math.ceil(new Date().getTime() / 1000);
|
||
var t = parseTime(now - time);
|
||
|
||
msg_box.innerHTML = '<a href="#" onclick="play(); return false;" class="txt_btn">' + lang.play + ' (' + t + 's)</a><br>' +
|
||
'<a href="#" id="saveToInput" onclick="save(); return false;" class="txt_btn">' + lang.download + '</a>';
|
||
}
|
||
|
||
function play() {
|
||
audio.play();
|
||
msg_box.innerHTML = '<a href="#" onclick="pause(); return false;" class="txt_btn">' + lang.stop + '</a><br>' +
|
||
'<a href="#" onclick="save(); return false;" class="txt_btn">' + lang.download + '</a>';
|
||
}
|
||
|
||
function pause() {
|
||
audio.pause();
|
||
audio.currentTime = 0;
|
||
msg_box.innerHTML = '<a href="#" onclick="play(); return false;" class="txt_btn">' + lang.play + '</a><br>' +
|
||
'<a href="#" onclick="save(); return false;" class="txt_btn">' + lang.download + '</a>'
|
||
}
|
||
|
||
function roundedRect(ctx, x, y, width, height, radius, fill) {
|
||
ctx.beginPath();
|
||
ctx.moveTo(x, y + radius);
|
||
ctx.lineTo(x, y + height - radius);
|
||
ctx.quadraticCurveTo(x, y + height, x + radius, y + height);
|
||
ctx.lineTo(x + width - radius, y + height);
|
||
ctx.quadraticCurveTo(x + width, y + height, x + width, y + height - radius);
|
||
ctx.lineTo(x + width, y + radius);
|
||
ctx.quadraticCurveTo(x + width, y, x + width - radius, y);
|
||
ctx.lineTo(x + radius, y);
|
||
ctx.quadraticCurveTo(x, y, x, y + radius);
|
||
|
||
ctx.fillStyle = fill;
|
||
ctx.fill();
|
||
}
|
||
|
||
function saveToInput(blob) {
|
||
// Create the File object with the actual Blob data
|
||
const myFile = new File([blob], 'record.ogg', {
|
||
type: 'audio/ogg',
|
||
lastModified: new Date(),
|
||
});
|
||
|
||
var formData = new FormData();
|
||
formData.append('media', myFile);
|
||
|
||
console.log(myFile);
|
||
|
||
$.ajax({
|
||
dataType: 'json',
|
||
type: 'POST',
|
||
processData: false,
|
||
contentType: false,
|
||
url: uploadFileAjax,
|
||
headers: { "RequestVerificationToken": antiForgeryToken },
|
||
data: formData,
|
||
success: function (response) {
|
||
if (response.isSuccedded) {
|
||
|
||
var inputVoice = `<input type="hidden" value="${response.id}" name="EditTask.UploadedVoiceMedia" />"`;
|
||
$('#voiceItem').append(inputVoice);
|
||
|
||
//showAlertMessage('.alert-success-msg', response.message, 1500);
|
||
console.log('voice uploaded');
|
||
} else {
|
||
showAlertMessage('.alert-msg', response.message, 3500);
|
||
$('#Command_Voice').val('');
|
||
}
|
||
loading.hide();
|
||
},
|
||
error: function (err) {
|
||
console.log(err);
|
||
loading.hide();
|
||
}
|
||
});
|
||
}
|
||
|
||
function save() {
|
||
console.log(audioSrc);
|
||
var a = document.createElement('a');
|
||
a.download = 'record.ogg';
|
||
a.href = audioSrc;
|
||
document.body.appendChild(a);
|
||
a.click();
|
||
|
||
document.body.removeChild(a);
|
||
}
|
||
|
||
} else {
|
||
if (location.protocol != 'https:') {
|
||
msg_box.innerHTML = lang.mic_error + '<br>' + lang.use_https;
|
||
} else {
|
||
msg_box.innerHTML = lang.mic_error;
|
||
}
|
||
button.disabled = true;
|
||
}
|
||
// صدا
|
||
|
||
|
||
|
||
|
||
|