Files
hrm_eva/wwwroot/js/coregen.js
2020-05-24 00:33:22 +07:00

527 lines
16 KiB
JavaScript

function formatNumber(num) {
if(num === "" || num === null) return "";
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
}
function formatNumberTemp(num) {
if(num === "" || num === null) return "";
return num.toString();
}
function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function AjaxGetRequest(path, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: GetUnix(path),
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: GetUnix(path),
type: 'GET',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: successFunc,
error: failFunc
});
}
}
function AjaxGetRequestWithData(path, jData, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: GetUnix(path),
type: 'GET',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: GetUnix(path),
type: 'GET',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
}
}
function AjaxPostRequest(path, jData, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: path,
type: 'POST',
//headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: path,
type: 'POST',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
}
}
function AjaxPutRequest(path, jData, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: path,
type: 'PUT',
//headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: path,
type: 'PUT',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
}
}
function AjaxPutRequestFile(path, jData, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: path,
type: 'PUT',
//headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'binary',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: path,
type: 'PUT',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'binary',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
}
}
function AjaxDeleteRequest(path, jData, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: path,
type: 'DELETE',
//headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: path,
type: 'DELETE',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify(jData),
success: successFunc,
error: failFunc
});
}
}
function AjaxUploadRequest(path, formData, successFunc, failFunc) {
if (getCookie("access_token") === null || getCookie("access_token") === "") {
$.ajax({
url: path,
type: 'POST',
processData: false, // important
contentType: false, // important
dataType: 'json',
data: formData,
success: successFunc,
error: failFunc
});
} else {
$.ajax({
url: path,
type: 'POST',
headers: { 'Authorization': "Bearer " + Cookies.get('access_token') },
processData: false, // important
contentType: false, // important
dataType: 'json',
data: formData,
success: successFunc,
error: failFunc
});
}
}
function DropDownClearFormAndFeedWithData(d, result, x, y, z, i) {
$(d).html('');
$(d).append($("<option></option>")
.attr("value", "")
.text("กรุณาเลือก"));
if (z !== "") {
$.each(result[z], function (key, value) {
$(d).append($("<option></option>")
.attr("value", value[x])
.text(value[y]));
});
} else {
$.each(result, function (key, value) {
$(d).append($("<option></option>")
.attr("value", value[x])
.text(value[y]));
});
}
$(d).val(i);
}
function DropDownInitialFormWithData(d, result, x, y, z) {
$(d).html('');
$(d).append($("<option></option>")
.attr("value", "")
.text("กรุณาเลือก"));
if (z !== "") {
$.each(result[z], function (key, value) {
$(d).append($("<option></option>")
.attr("value", value[x])
.text(value[y]));
});
} else {
$.each(result, function (key, value) {
$(d).append($("<option></option>")
.attr("value", value[x])
.text(value[y]));
});
}
}
////////////////////////// Alert Functions //////////////////////////////////
function AlertSuccess(msg) {
alert(msg);
}
function AlertDanger(xhr, status, error) {
var errorMessage = xhr.responseText;
endLoad();
alert(errorMessage);
}
function GetUnix(path) {
if (path !== "") {
if (path.indexOf("?") > -1)
path += "&";
else
path += "?";
path += "_=" + moment().unix();
}
return path;
}
function startLoad() {
$.LoadingOverlay("show");
}
function endLoad() {
$.LoadingOverlay("hide");
}
////////////////////////// Service Frame //////////////////////////////////
function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
function window_open_from_root(frameSrc) {
if (inIframe()) {
window.parent.window_open_from_root(frameSrc);
} else {
window.location = frameSrc;
}
}
function window_open(frameSrc) {
$(this).parents('body').addClass('overflow-fix');
$('#myframe').addClass('iframe-visible');
$('#myframe').attr('src', frameSrc);
//return false;
}
function window_close() {
$(window.frameElement).removeClass('iframe-visible');
$(window.frameElement).parents('body').removeClass('overflow-fix');
$(window.frameElement).attr('src', '');
endLoad();
//return false;
}
function refresh_frame() {
//&#xf021;
console.log("refresh_btn");
var ref = $('#refresh_icon');
//if (window.parent == window.top) {
ref.addClass("fa fa-refresh fa-spin");
ref.html("");
window.location.reload(true);
//}
}
////////////////////////// Control Service //////////////////////////////////
function CheckBoxGetFromForm(d) {
if ($(d).is(':checked')) {
return true;
} else {
return false;
}
}
function CheckBoxFeedDataToForm(d, v) {
if (v === true) {
$(d).prop('checked', true);
} else {
$(d).prop('checked', false);
}
}
function formatDate(date) {
let value = '';
if (date === null) {
value = '';
} else {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + (d.getDate()),
year = ''+(d.getFullYear()+543);
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
value = [day, month, year].join('/');
}
return value;
}
function getDate(dateText) {
//console.log(dateText);
if (dateText !== "") {
var parts = dateText.split('/');
var day = parseInt(parts[0]); // + 1;
var month = parseInt(parts[1]) - 1;
var year = parseInt(parts[2]) - 543;
//console.log(parts);
//console.log(year);
//console.log(month);
//console.log(day);
//var d = new Date(year, month, day);
var d = new Date(Date.UTC(year, month, day));
return d;
} else {
return null;
}
}
function formatDateForGetParameter(date) {
let value = '';
if (date === null) {
value = '';
} else {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
//day = '' + (d.getDate() - 1),
day = '' + (d.getDate()),
year = '' + (d.getFullYear());
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
value = [year, month, day].join('-');
}
return value;
}
// ================= File Upload ========================
function UploadImage(file, code) {
var file_data = file.prop("files")[0];
var myFormData = new FormData();
myFormData.append('files', file_data);
var successFunc = function (response) {
$("#" + code + "_linkurl").text("ดูหรือดาวโหลดไฟล์ของคุณ");
$("#" + code + "_linkurl").attr('href', apisite + "/" + response.filesUploaded[0]);
$("#" + code + "_linkurl").attr('target', '_blank');
$("#" + code + "_remove").show();
$("#" + code + "_hidURL").val(response.filesUploaded[0]);
endLoad();
};
startLoad();
AjaxUploadRequest(apisite + "/api/Attachment/UploadMultipleFiles", myFormData, successFunc, AlertDanger);
}
function feedFileToControl(thefile, thefileDisplay, code, mode) {
if (!thefile) {
$("#" + code + "_linkurl").text("ยังไม่มีการแนบไฟล์");
$("#" + code + "_linkurl").attr('href', '#');
$("#" + code + "_linkurl").attr('target', '');
$("#" + code + "_remove").hide();
$("#" + code + "_hidURL").val("");
$("#" + code + "_file").val("");
} else {
$("#" + code + "_linkurl").text("ดูหรือดาวโหลดไฟล์ของคุณ");
if (mode === "file") {
$("#" + code + "_linkurl").attr('href', appsite + "/" + thefileDisplay);
} else if(mode === "db") {
$("#" + code + "_linkurl").attr('href', thefileDisplay);
}
$("#" + code + "_linkurl").attr('target', '_blank');
$("#" + code + "_remove").show();
$("#" + code + "_hidURL").val(thefile);
$("#" + code + "_file").val("");
}
}
function removeFile(code) {
if (confirm('คุณต้องการลบไฟล์ ใช่หรือไม่?')) {
$("#" + code + "_linkurl").text("ยังไม่มีการแนบไฟล์");
$("#" + code + "_linkurl").attr('href', '#');
$("#" + code + "_linkurl").attr('target', '');
$("#" + code + "_remove").hide();
$("#" + code + "_hidURL").val("");
$("#" + code + "_file").val("");
}
}
function setupUploadEvent() {
}
function hasExtension(inputID, exts) {
var fileName = document.getElementById(inputID).value;
return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$')).test(fileName);
}
// ================= Validation ========================
function SetupValidationRemark(group) {
$("[iGroup='" + group + "'][iRequire='true']").each(function (index) {
var k = "lab_" + $(this).attr("id");
$("<font color='red'> *</font>").insertAfter("#" + k);
});
// in case of file upload
$("input[iGroup='" + group + "'][iRequire='true'][type='hidden']").each(function (index) {
var k = "lab_" + $(this).attr("id");
$("<font color='red'> *</font>").insertAfter("#" + k.replace('_hidURL', ''));
});
}
function ValidateForm(group, customValidate) {
//console.log("ValidateForm "+group);
var result = "";
var obj = null;
$("input[iRequire='true'][iGroup='" + group + "']").each(function (index) {
if ($(this).val() === "" || $(this).val() === null) {
result += "- กรุณาระบุ " + $(this).attr("iLabel") + "\n";
$(this).css('border-color', 'red');
if (obj === null) obj = $(this);
} else {
$(this).css('border-color', '');
}
});
$("textarea[iRequire='true'][iGroup='" + group + "']").each(function (index) {
if ($(this).val() === "" || $(this).val() === null) {
result += "- กรุณาระบุ " + $(this).attr("iLabel") + "\n";
$(this).css('border-color', 'red');
if (obj === null) obj = $(this);
} else {
$(this).css('border-color', '');
}
});
$("select[iRequire='true'][iGroup='" + group + "']").each(function (index) {
if ($(this).val() === "" || $(this).val() === null) {
result += "- กรุณาระบุ " + $(this).attr("iLabel") + "\n";
$(this).css('border-color', 'red');
if (obj === null) obj = $(this);
} else {
$(this).css('border-color', '');
}
});
result += customValidate(group);
if (result !== "") {
obj.focus();
alert(result);
return false;
} else {
return true;
}
}