class Form{
constructor(){
}
static TextBox(mandatory, translation_key, value){
let validateClass = "";
if(mandatory){
validateClass = " needs-validation"
}
return `
`;
}
static Hidden(translation_key, value){
return `
`;
}
static NumberField(mandatory, translation_key, value, field_class = "", readonly = false, includeLabel = true){
let validateClass = "";
let disabled = "";
if(mandatory){
validateClass = " needs-validation"
}
if(field_class != ""){
field_class = " " + field_class;
}
if(readonly){
disabled = " disabled";
}
let disabledKeys = ["+", "." , "-", ","];
let step = "1";
let onkeydownkeyCodes = "";
for(var i=0; i < disabledKeys.length;i++){
onkeydownkeyCodes += (onkeydownkeyCodes == "" ? "" : " || " ) + "event.key==='"+disabledKeys[i]+"'";
}
if(includeLabel){
return `
`;
}
else{
return `
`;
}
}
static Float(mandatory, translation_key, value){
let validateClass = "";
if(mandatory){
validateClass = " needs-validation"
}
let disabledKeys = ["+", "-", ","];
let step = "1";
let onkeydownkeyCodes = "";
for(var i=0; i < disabledKeys.length;i++){
onkeydownkeyCodes += (onkeydownkeyCodes == "" ? "" : " || " ) + "event.key==='"+disabledKeys[i]+"'";
}
return `
`;
}
static TextAreaGrow(){
Array.from(document.querySelectorAll(".ta-autogrow")).forEach(function(ta){
ta.style.height='auto';
ta.style.height = (ta.scrollHeight + 2) + 'px';
});
}
static TextArea(mandatory, translation_key, value, autogrow = false, rows = 3){
let validateClass = "";
let onInput = "";
let autoGrowClass = "";
if(mandatory){
validateClass = " needs-validation"
}
if(autogrow){
onInput = `onInput="this.style.height='auto'; this.style.height = (this.scrollHeight + 2) + 'px';"`;
autoGrowClass = " ta-autogrow";
}
return `
`;
}
static DateField(mandatory, translation_key, value){
let validateClass = "";
if(mandatory){
validateClass = " needs-validation"
}
return `
`;
}
static Combobox(mandatory, translation_key, value, options, field_class){
let validateClass = "";
if(mandatory){
validateClass = " needs-validation";
}
if(field_class != ""){
field_class = " " + field_class;
}
let optionsHtml = ``;
for(let i=0; i${options[i].name}`;
}
return `
`;
}
static refillFilesDropDown(element, data){
data = data.data
let filesOptions = [
{"id":-1, "name":getTranslation("unknown")}
,{"id":-2, "name":getTranslation("new_file"), "show_extra_field":true}
];
if(sync.isActive("files")){
filesOptions = [
{"id":-1, "name":getTranslation("unknown")}
];
}
data = [...filesOptions, ...data];
let parentDiv = element.parentElement;
let buttonLabel = "...";
let mandatory = element.getAttribute("data-mandatory") == "true";
let translation_key = element.getAttribute("data-translationkey");
let li = parentDiv.getElementsByTagName("li");
while (li[0]){
parentDiv.removeChild(li[0])
}
for(let i=0; i${data[i].name}`;
el.onclick = function(){
if(!this.classList.contains("active")){
Array.from(this.parentElement.querySelectorAll(".active")).forEach(function(itm){
itm.classList.remove("active");
});
Array.from(this.getElementsByTagName("a")).forEach(function(itm){
itm.classList.add("active");
});
document.getElementById(this.getAttribute("parentId") + "-new-name").value = "";
document.getElementById(this.getAttribute("parentId") + "-button").innerText = this.innerText;
document.getElementById(this.getAttribute("parentId") + "-dropdown").blur();
document.getElementById(this.getAttribute("parentId")).value = this.getAttribute("value");
if(this.getAttribute("show-childtext") == "true"){
document.getElementById(this.getAttribute("parentId") + "-new-name").style.display = "";
if(this.getAttribute("data-validate") == "true"){
document.getElementById(this.getAttribute("parentId") + "-new-name").classList.add("needs-validation");
}
}
else{
document.getElementById(this.getAttribute("parentId") + "-new-name").style.display = "none";
document.getElementById(this.getAttribute("parentId") + "-new-name").classList.remove("needs-validation");
}
}
Form.validate();
}
parentDiv.appendChild(el)
}
}
static initSearchComboboxes(){
Array.from(document.getElementById("currentpage-content").querySelectorAll(".filterablecombobox-search")).forEach(function(item){
item.onkeyup = function(){
globalFileSearcher.startTimer(500, this)
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll(".filterablecombobox")).forEach(function(item){
//get children
let childs = item.getElementsByTagName("li");
for(let i=0; i${options[i].name}`;
}
return `
`;
}
static Divier(text = ""){
return `
${text}
`;
}
static validate(){
if(document.getElementById("input-save-button")){
let valid = true;
Array.from(document.getElementById("currentpage-content").querySelectorAll(".needs-validation")).forEach(function(el){
if(el.value.trim() === ""){
valid = false;
if(!el.classList.contains("text-pink-600")){
el.classList.add("text-pink-600");
el.classList.add("border-pink-500");
}
}
else{
if(el.classList.contains("text-pink-600")){
el.classList.remove("text-pink-600");
el.classList.remove("border-pink-500");
}
}
});
document.getElementById("input-save-button").disabled = !valid;
}
}
static initValidation(){
Array.from(document.getElementById("currentpage-content").querySelectorAll("input")).forEach(function(el){
if(!el.classList.contains("filterablecombobox-search")){
el.onkeyup = function(){
if(el.classList.contains("lawbook_laws_calc_penalty")){
FileEntry.ValidateAndCalcLawBookLaws();
}
else{
Form.validate();
}
}
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("input")).forEach(function(el){
el.onchange = function(){
Form.validate();
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("textarea")).forEach(function(el){
el.onkeyup = function(){
Form.validate();
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("select")).forEach(function(el){
el.onchange = function(){
if(el.classList.contains("police_fileentry_type_of_entry")){
FileEntry.ToggleLawBooks();
}
else{
Form.validate();
}
}
});
}
static getFormData(){
let retval = {};
Array.from(document.getElementById("currentpage-content").querySelectorAll("input")).forEach(function(el){
if(!el.classList.contains("filterablecombobox-search")){
let key = el.id.replace("input-","");
if(el.type != "checkbox"){
retval[key] = el.value;
}
else{
retval[key] = el.checked ? 1 : 0;
}
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("textarea")).forEach(function(el){
let key = el.id.replace("input-","");
retval[key] = el.value;
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("onchange")).forEach(function(el){
let key = el.id.replace("input-","");
retval[key] = el.value;
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("select")).forEach(function(el){
let key = el.id.replace("input-","");
retval[key] = el.value;
});
return retval;
}
static disableAll(){
Array.from(document.getElementById("currentpage-content").querySelectorAll("input")).forEach(function(el){
if(!el.classList.contains("ignore-readonly")){
el.readOnly = true;
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("textarea")).forEach(function(el){
if(!el.classList.contains("ignore-readonly")){
el.readOnly = true;
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll("select")).forEach(function(el){
if(!el.classList.contains("ignore-readonly")){
el.disabled = true;
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll(".filterabledropdownbutton")).forEach(function(el){
if(!el.classList.contains("ignore-readonly")){
el.disabled = true;
el.readOnly = true;
}
});
}
static cancelButton(source, destination, destination_id){
return ``;
}
static saveButton(source, destination, uid, destination_id){
return ``;
}
static initSaveButtons(){
document.getElementById("input-cancel-button").onclick = function(){
loadData(
this.getAttribute("data-destination"),
this.getAttribute("data-destination-id"),
true
)
}
document.getElementById("input-save-button").onclick = function(){
saveData(
this.getAttribute("data-source"),
this.getAttribute("data-destination"),
this.getAttribute("data-uid"),
this.getAttribute("data-destination-id")
)
}
}
static initViewModeTopButtons(){
if(document.getElementById("input-back-button")){
document.getElementById("input-back-button").onclick=function(){
loadPage(this.getAttribute("data-destination"), this.getAttribute("data-destinationId"), "true");
}
}
if(document.getElementById("input-edit-button")){
document.getElementById("input-edit-button").onclick=function(){
loadPage(this.getAttribute("data-destination"), this.getAttribute("data-destinationId"));
}
}
}
static BackEditBtn(backDestination, editDest, editId, allowEditButton, backDestinationId = -1 ){
let editBtn = ``;
if(allowEditButton){
editBtn = ``;
}
return `
${editBtn}
`;
}
static getViewButtonIcon(recordid, destination){
return `
`;
}
static getIsWantedButton(recordid, destination, isWanted){
let disabled = isWanted ? " disabled" : ""
return `
`;
}
static getEditButtonIcon(recordid, destination, allowButton=true){
if(!allowButton){
return "";
}
return `
`;
}
static getDeleteButtonIcon(recordid, delete_destination , allowButton=true){
if(!allowButton){
return ``;
}
return `
`;
}
static overviewHeadline(destination, inclSearch = true, allowAddNew = true){
let search = "";
if(inclSearch){
let addBtn = "";
if(allowAddNew){
addBtn = ``;
}
return `
`;
}
else{
if(allowAddNew){
return `
`;
}
else{
return ``;
}
}
}
static getPageChanger(curPage, maxPage){
curPage = curPage + 1;
let nextDisabled = "";
let prevDisabled = "";
if(curPage == 1){
prevDisabled = " disabled";
}
if(curPage >= maxPage){
nextDisabled = " disabled";
}
document.body.setAttribute("data-cur-pageNum", curPage);
return `
`;
}
static initPageChange(){
if(document.getElementById("global-previous-page")){
document.getElementById("global-previous-page").onclick=function(){
let curpage = document.body.getAttribute("data-cur-pageNum");
curpage = 1*curpage;
let destPage = curpage-1;
document.body.setAttribute("data-cur-pageNum", destPage);
reload();
}
}
if(document.getElementById("global-next-page")){
document.getElementById("global-next-page").onclick=function(){
let curpage = document.body.getAttribute("data-cur-pageNum");
curpage = 1*curpage;
let destPage = curpage+1;
document.body.setAttribute("data-cur-pageNum", destPage);
reload();
}
}
}
static openDeleteModal(id, source){
document.getElementById("delete-modal-title").innerHTML = getTranslation("delete");
document.getElementById("delete-modal-message").innerHTML = getTranslation("delete_message");
document.getElementById("delete-modal-cancel").innerHTML = getTranslation("cancel");
document.getElementById("delete-modal-delete").innerHTML = getTranslation("delete");
document.getElementById("delete-modal").setAttribute("data-id", id);
document.getElementById("delete-modal").setAttribute("data-delete-destination", source);
document.getElementById("delete-modal").checked = true;
}
static initTableButtons(){
Array.from(document.getElementById("currentpage-content").querySelectorAll(".table-deletebutton")).forEach(function(el){
el.onclick = function(){
Form.openDeleteModal(this.getAttribute("data-id"), this.getAttribute("data-deleteDestination"));
}
});
if(document.getElementById("global-searchbar")){
document.getElementById("global-searchbar").focus();
document.getElementById("global-searchbar").setSelectionRange(document.getElementById("global-searchbar").value.length, document.getElementById("global-searchbar").value.length);
document.getElementById("global-searchbar").onkeyup = function(e){
if(e.key.toLowerCase() == "enter"){
resetPageNum();
reload();
}
}
}
if(document.getElementById("global-searchbar-start")){
document.getElementById("global-searchbar-start").onclick=function(){
resetPageNum();
reload();
}
}
Array.from(document.getElementById("currentpage-content").querySelectorAll(".table-addbutton")).forEach(function(el){
el.onclick = function(){
loadPage(this.getAttribute("data-destination"), -1);
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll(".table-editbutton")).forEach(function(el){
el.onclick = function(){
loadPage(this.getAttribute("data-destination"), this.getAttribute("data-id"));
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll(".table-viewbutton")).forEach(function(el){
el.onclick = function(){
loadPage(this.getAttribute("data-destination"), this.getAttribute("data-id"));
}
});
Array.from(document.getElementById("currentpage-content").querySelectorAll(".table-iswantedbutton")).forEach(function(el){
el.onclick = function(){
let fromObj = this.getAttribute("data-destination").toLowerCase();
let fromId = this.getAttribute("data-id");
if(fromObj == "regvehicle" || fromObj == "regvehicleimpounded"){
changeDataInColumn(fromObj, "is_wanted", fromId, 1, true);
}
else if(fromObj == "regweapons"){
changeDataInColumn(fromObj, "is_wanted", fromId, 1, true);
}
else if(fromObj == "files"){
loadPage('fileentry.add',fromId,'true')
}
}
});
}
}