Newer
Older
vmk-demo-bot / admin / src / js / index.js
function doAction(action, params, callback) {
	url = BACKSERV;
	fetch(url, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json'
		},
		body: JSON.stringify({ action: action, params: params })
	})
	.then(response => response.json())
	.then(data => {
    callback(data);
  })
  .catch(error => {
    console.error("Err of request:", error);
  });
}


function getSwitchersVal(container) {
	const switchers = container.querySelectorAll(".form-check-input");
	const data = {};

	for(switcher of switchers) {
		data[switcher.name] = switcher.checked;
	}

	return data;
}

function entryTypeToView(entryType) {
	const map = {
		"cources": "Навчання",
		"catalog": "Фізичний товар",
		"promo": "Промо/Акція"
	};

	return map[entryType];
}

function formattedTimestamp(timestamp) {
	const date = new Date(timestamp.replace(" ", "T"));
	const pad = num => num.toString().padStart(2, '0');
	const formatted = `${pad(date.getDate())}.${pad(date.getMonth() + 1)}.${date.getFullYear()} ${pad(date.getHours())}:${pad(date.getMinutes())}`;

	return formatted;
}

function paginator_handler(container, data, reloadCallback) {  
	const pagination = container.querySelector(".pagination");
	pagination.innerHTML = "";

	if(data.total_pages < 2) {
		return;
	}

	// rendering
	let html = "";
	for(let i=1; i<=data.total_pages; i++) {
		html += `<li class="page-item"><span class="page-link" data-pagenum="${i}">${i}</span></li>`;
	}

	pagination.innerHTML = html;

	// make active
	
	pagination.querySelector(`[data-pagenum="${data.pagenum}"]`)?.parentNode.classList.add("active");

	// addEventsListeners
	
	pagination.querySelectorAll(".page-link").forEach(item => {
		item.addEventListener("click", e => {
			const pagenum = e.currentTarget.dataset.pagenum;
			e.currentTarget.parentNode.parentNode.parentNode.dataset.gotoPagenum = pagenum;
			reloadCallback();
		});
	});
}

function validityChecking(form) {
	form.querySelectorAll('.invalid').forEach(i => {
    i.classList.remove("invalid");
  });

  const validFlag = true;

  form.querySelectorAll('[required]').forEach(i => {
    if(!i.checkValidity()) {
      i.classList.add("invalid");
      validFlag = false;
    }
  });

  const hlink = form.querySelector(`[name="hlink"]`);
  if(hlink.value.length) {
  	if(hlink.value.indexOf("https://") == -1 || hlink.value.indexOf("http://") != -1) {
  		hlink.classList.add("invalid");
  		validFlag = false;
  	}
  }

  return validFlag;
} 

document.addEventListener("DOMContentLoaded", e => {
	document.querySelectorAll("[required]").forEach(i => {
		i.addEventListener("input", e => {
			e.currentTarget.classList.remove("invalid");
		});
	});

	const location = document.location.hash;
	let triggerEl = document.querySelector(`#pills-tab [data-coreui-target="#pills-knbase"]`);
	
	if(location.indexOf("#target-") != -1) {
		const page = location.split("#target-")[1];
		triggerEl = document.querySelector(`#pills-tab [data-coreui-target="#${page}"]`);
	}
	
	const tabTrigger = new coreui.Tab(triggerEl);
	tabTrigger.show()

	document.querySelectorAll('[data-coreui-toggle]').forEach(tabEl => {
	  tabEl.addEventListener('shown.coreui.tab', event => {
	    const page = "#target-" + event.target.dataset.coreuiTarget.replace("#", "");
	    document.location.hash = page;
	  });
	});

});