require('./bootstrap');
//----------------------------------------------------------------------
/*
Helper function
*/
window.getMonday=function(date,dateStr) {
if(dateStr){
date = new Date(dateStr);
}
let day = date.getDay(),
diff = date.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
return new Date(date.setDate(diff));
}
window.debug=function (txt)
{
try {
if (txt && typeof txt === 'object' && !Array.isArray(txt)) {
const seen = new WeakSet();
const safeObj = JSON.parse(JSON.stringify(txt, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular]';
seen.add(value);
}
return value;
}));
console.log(safeObj);
} else {
console.log(txt);
}
}
catch(e){
console.log(txt);
}
};
window.buildFormData=function(formData, data, parentKey) {
if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) {
Object.keys(data).forEach(key => {
buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key);
});
} else {
const value = data == null ? '' : data;
formData.append(parentKey, value);
}
}
window.jsonToFormData=function(data) {
const formData = new FormData();
buildFormData(formData, data);
return formData;
}
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1)
}
window.arrayRemove=function(arr, value) {
return arr.filter(function(ele){
return ele != value;
});
}
window.getRandomInt=function (max=Number.MAX_SAFE_INTEGER,min=0) {
debug(max);
debug(min);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
window.setImageFromLocal=function(input,targetImgCssSelector) {
if (input.files && input.files[0]) {
let reader = new FileReader();
reader.onload = function (e) {
$(targetImgCssSelector).attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
require('./latinise');
/*
disable back button
*/
$(function() {
if (window.history && window.history.pushState) {
window.history.pushState('', null, window.location.href);
$(window).on('popstate', function(event) {
// alert('Back button was pressed.');
//document.location.href = '#';
window.history.pushState('', document.title, window.location.href);
//console.log('mevagy');
event.stopPropagation();
});
}
});
//----------------------------------------------------------------------
/*
init custom radio buttons
*/
window.initRadioButtons=function (Options){
let Opts = {
onChange: function (value,node) {
},
};
$.extend(Opts, Options);
$('.customRadioBtnGroup .active').each(function() {
let activeValue = $(this).data('title');
let target = $(this).data('toggle');
$('input[name='+target+']').prop('value', activeValue);
});
$('.customRadioBtnGroup a').unbind('click');
$('.customRadioBtnGroup a').bind('click',function(){
let sel = $(this).data('title');
let tog = $(this).data('toggle');
$('input[name='+tog+']').prop('value', sel);
$('a[data-toggle="'+tog+'"]').not('[data-title="'+sel+'"]').removeClass('active').addClass('notActive');
$('a[data-toggle="'+tog+'"][data-title="'+sel+'"]').removeClass('notActive').addClass('active');
Opts.onChange(sel,tog);
});
}
//----------------------------------------------------------------------
// Defaults
//----------------------------------------------------------------------
$.extend( true, $.fn.dataTable.defaults, {
language: {
url: '//cdn.datatables.net/plug-ins/1.10.24/i18n/Hungarian.json'
},
lengthMenu: [
[ 10, 25, 50, -1 ],
[ '10 sor', '25 sor', '50 sor', 'Összes' ]
],
buttons:{
dom:{
button:{
className:'fg-button ui-button dataTableHeaderButtons'
}
},
},
dom: '<"fg-toolbar ui-toolbar ui-widget-header ui-helper-clearfix ui-corner-tl ui-corner-tr"lBfr>'+
't'+
'<"fg-toolbar ui-toolbar ui-widget-header ui-helper-clearfix ui-corner-bl ui-corner-br"ip>',
} );
Inputmask.extendDefinitions({
'x': {
validator: "[\.,-áéíóöőúàèìòùüűÁÉÍÓÖŐÚÀÈÌÒÙÜŰA-Za-z0-9 ]",
//cardinality: 1
}
});
//----------------------------------------------------------------------
Toast.enableTimers(false);
$.fn.select2.defaults.set('language', 'jp');
window.select2CreateTag=function (params){
var term = $.trim(params.term);
var count = 0
var existsVar = false;
//check if there is any option already
if($('#keywords option').length > 0){
$('#keywords option').each(function(){
if ($(this).text().toUpperCase() == term.toUpperCase()) {
existsVar = true
return false;
}else{
existsVar = false
}
});
if(existsVar){
return null;
}
return {
id: params.term,
text: params.term,
newTag: true
}
}
//since select has 0 options, add new without comparing
else{
return {
id: params.term,
text: params.term,
newTag: true
}
}
}
window.ajaxErrorHandler= function (xhr, ajaxOptions, thrownError){
debug(this);
console.error(xhr);
console.error(ajaxOptions);
console.error(thrownError);
// Biztonsági ellenőrzés – ha nincs responseJSON (pl. HTML válasz), megpróbáljuk parszolni
if (!xhr.responseJSON && xhr.responseText) {
try {
xhr.responseJSON = JSON.parse(xhr.responseText);
} catch (e) {
console.error("Nem sikerült a responseText JSON parszolása");
}
}
// Biztonsági ellenőrzés – ha nincs responseJSON (pl. HTML válasz)
if (!xhr.responseJSON) {
ajaxErrorFieldHandler({'detail': 'Váratlan szerverhiba. Kérjük próbálja újra később.'});
return;
}
// Validációs hibák (422) – ezeket részletesen megjelenítjük
if (xhr.responseJSON.errors) {
console.error("d2---------------");
if("jsonapi" in xhr.responseJSON){
ajaxErrorFieldHandlerJsonAPI(xhr.responseJSON.errors);
}else{
ajaxErrorFieldHandler(xhr.responseJSON.errors);
}
}
// Egyedi referencia kódos hiba (500, stb.)
else if (xhr.responseJSON.error_ref) {
console.error("error_ref: " + xhr.responseJSON.error_ref);
Toast.create({
title: "Szerverhiba",
message: "Váratlan hiba történt a feldolgozás során.
" +
"Hivatkozási kód: " + xhr.responseJSON.error_ref + "
" +
"Kérjük, ezt a kódot adja meg a hibajelentésben.",
status: TOAST_STATUS.DANGER,
timeout: 0
});
}
// 404
else if (xhr.status == 404) {
console.error("d3---------------");
ajaxErrorFieldHandler({'detail':'Nincs ilyen elem.'});
}
// Egyéb ismeretlen hiba
else {
console.error("d4---------------");
ajaxErrorFieldHandler({'detail':'Általános hiba.'});
}
}
window.ajaxErrorFieldHandlerJsonAPI= function (errors, container) {
console.error("d6---------------");
let toastBodyHtml = '';
let toastTitle = 'Hiba';
console.error(errors);
$.each(errors, function (index, value) {
console.error(value);
toastTitle = 'Hiba';
toastBodyHtml = '';
if("title" in value){
toastTitle=value.title;
}
if("detail" in value){
toastBodyHtml += '