2023-05-12 23:57:21 +02:00
// Javascript to enable link to tab
// This magic js codes enables anchor links to work with bootstrap tabs
// Taken from https://stackoverflow.com/a/9393768/11317151 (and edited, like a lot)
2023-07-10 15:34:04 +02:00
const FLAG _supports _new _data _loader = true ;
2023-05-12 23:57:21 +02:00
// Also update on location change
2023-05-16 00:05:39 +02:00
window . addEventListener (
'hashchange' ,
function ( ) {
var hash = location . hash . replace ( /^#/ , '' ) ; // ^ means starting, meaning only match the first hash
if ( hash ) {
bootstrap . Tab . getOrCreateInstance ( document . querySelector ( '#' + hash ) ) . show ( ) ;
}
} ,
false
) ;
var hash = location . hash . replace ( /^#/ , '' ) ; // ^ means starting, meaning only match the first hash
2023-05-12 23:57:21 +02:00
if ( hash ) {
2023-05-16 00:05:39 +02:00
bootstrap . Tab . getOrCreateInstance ( document . querySelector ( '#' + hash ) ) . show ( ) ;
}
2023-05-12 23:57:21 +02:00
// Change hash for page-reload
$ ( '.nav-link' ) . on ( 'click' , function ( e ) {
window . location . hash = e . target . id ;
2023-05-16 00:05:39 +02:00
} ) ;
function primeCreateNew ( ) {
2023-05-16 22:58:08 +02:00
// Clear the form
$ ( '.form-control' ) . val ( '' ) ;
2023-05-16 00:05:39 +02:00
const form = document . getElementById ( 'storageUnitModalForm' ) ;
2023-05-16 22:58:08 +02:00
const form2 = document . getElementById ( 'storageLocationModalForm' ) ;
document . getElementById ( 'createNewLocationSelection' ) . disabled = false ;
2023-07-10 15:34:04 +02:00
document . getElementById ( 'storageUnitModalLocationSelectText' ) . innerText = 'Select or create a new location.' ;
document . getElementById ( 'storageUnitModalLabel' ) . innerText = 'Create new storage unit' ;
document . getElementById ( 'storageLocationModalTitle' ) . innerText = 'Create new storage location' ;
2023-05-16 00:05:39 +02:00
form . setAttribute ( 'method' , 'POST' ) ;
form2 . setAttribute ( 'method' , 'POST' ) ;
return true ;
}
function primeEdit ( ) {
const form = document . getElementById ( 'storageUnitModalForm' ) ;
2023-05-16 22:58:08 +02:00
const form2 = document . getElementById ( 'storageLocationModalForm' ) ;
// Disable create new location
document . getElementById ( 'createNewLocationSelection' ) . disabled = true ;
2023-07-10 15:34:04 +02:00
document . getElementById ( 'storageUnitModalLocationSelectText' ) . innerText = 'While editing you can only select already existing locations. Use the settings to create new ones.' ;
document . getElementById ( 'storageUnitModalLabel' ) . innerText = 'Edit a storage unit' ;
document . getElementById ( 'storageLocationModalTitle' ) . innerText = 'Edit a storage location' ;
document . getElementById ( 'storageUnitModalLocationSelect' ) . selectedIndex = 1 ;
handleSelector ( ) ;
2023-05-16 00:05:39 +02:00
form . setAttribute ( 'method' , 'PATCH' ) ;
form2 . setAttribute ( 'method' , 'PATCH' ) ;
return true ;
}
2023-07-10 15:34:04 +02:00
function handleSelector ( ) {
const selector = document . getElementById ( 'storageUnitModalLocationSelect' ) ;
2023-05-16 00:05:39 +02:00
const value = selector . options [ selector . selectedIndex ] . value ;
2023-07-10 15:34:04 +02:00
if ( value == 'META_CREATENEW' ) {
$ ( '#storageUnitModalContactInfoCreator' ) . removeClass ( 'd-none' ) ;
$ ( '.requireOnCreate' ) . attr ( 'required' , true ) ;
2023-05-16 00:05:39 +02:00
} else {
2023-07-10 15:34:04 +02:00
$ ( '#storageUnitModalContactInfoCreator' ) . addClass ( 'd-none' ) ;
$ ( '.requireOnCreate' ) . attr ( 'required' , false ) ;
2023-05-16 00:05:39 +02:00
}
2023-05-16 22:58:08 +02:00
}
function getDataForEdit ( id ) {
$ . ajax ( {
type : 'get' ,
url : ` /api/v1/storageUnits?id= ${ id } ` ,
2023-07-09 18:07:28 +02:00
success : function ( result ) {
2023-05-16 22:58:08 +02:00
// Get elements inside the editCategoryModal
const modal _unitName = document . getElementById ( 'storageUnitModalName' ) ;
const modal _unitLocation = document . getElementById ( 'storageUnitModalLocationSelect' ) ;
const modal _unitId = document . getElementById ( 'storageUnitModalLocationSelectHidden' ) ;
// const modal_categoryid = document.getElementById('editCategoryModalId');
modal _unitName . value = result . name ;
modal _unitId . value = result . id ;
// Select the correct location from the select based on the value of the option
2023-07-10 15:34:04 +02:00
for ( var i , j = 0 ; ( i = modal _unitLocation . options [ j ] ) ; j ++ ) {
if ( i . value == result . contactInfoId ) {
console . log ( 'Found it' ) ;
2023-05-16 22:58:08 +02:00
modal _unitLocation . selectedIndex = j ;
break ;
}
}
} ,
error : function ( data ) {
console . log ( '!!!! ERROR !!!!' , data ) ;
// Hide overlay with spinner
$ ( '.loader-overlay' ) . removeClass ( 'active' ) ;
// Close the modal
$ ( '.modal' ) . modal ( 'hide' ) ;
2023-07-10 15:34:04 +02:00
createNewToast ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. The storage unit does no longer exist.' , 'text-bg-danger' ) ;
2023-05-16 22:58:08 +02:00
}
} ) ;
}
function getDataForEditLoc ( id ) {
$ . ajax ( {
type : 'get' ,
url : ` /api/v1/storageLocations?id= ${ id } ` ,
2023-07-09 18:07:28 +02:00
success : function ( result ) {
2023-05-16 22:58:08 +02:00
// Get elements inside the editCategoryModal
const modal _locationName = document . getElementById ( 'storageLocationModalName' ) ;
const modal _locationUnitSel = document . getElementById ( 'storageLocationModalUnit' ) ;
const modal _locationId = document . getElementById ( 'storageLocationModalIdHidden' ) ;
// const modal_categoryid = document.getElementById('editCategoryModalId');
modal _locationName . value = result . name ;
modal _locationId . value = result . id ;
// Select the correct location from the select based on the value of the option
2023-07-10 15:34:04 +02:00
for ( var i , j = 0 ; ( i = modal _locationUnitSel . options [ j ] ) ; j ++ ) {
if ( i . value == result . storageUnitId ) {
console . log ( 'Found it' ) ;
2023-05-16 22:58:08 +02:00
modal _locationUnitSel . selectedIndex = j ;
break ;
}
}
} ,
error : function ( data ) {
console . log ( '!!!! ERROR !!!!' , data ) ;
// Hide overlay with spinner
$ ( '.loader-overlay' ) . removeClass ( 'active' ) ;
// Close the modal
$ ( '.modal' ) . modal ( 'hide' ) ;
2023-07-10 15:34:04 +02:00
createNewToast ( '<i class="bi bi-exclamation-triangle-fill"></i> Something went wrong. The storage unit does no longer exist.' , 'text-bg-danger' ) ;
2023-05-16 22:58:08 +02:00
}
} ) ;
2023-05-16 00:05:39 +02:00
}
2023-07-10 15:34:04 +02:00
const itemList = $ ( '#itemList' ) ;
const itemListUnit = $ ( '#itemListUnit' ) ;
// itemList.empty();
2023-07-10 17:36:59 +02:00
itemListUnit . bootstrapTable ( { url : '/api/v1/storageUnits' , search : true , showRefresh : true , responseHandler : dataResponseHandlerUnit , sidePagination : 'server' , serverSort : true , silentSort : false } ) ;
itemList . bootstrapTable ( { url : '/api/v1/storageLocations' , search : true , showRefresh : true , responseHandler : dataResponseHandler , sidePagination : 'server' , serverSort : true , silentSort : false } ) ;
2023-07-10 15:34:04 +02:00
setTimeout ( ( ) => {
activateTooltips ( ) ;
} , 1000 ) ;
function loadPageData ( ) {
// itemList.empty();
itemList . bootstrapTable ( 'refresh' ) ;
itemListUnit . bootstrapTable ( 'refresh' ) ;
setTimeout ( ( ) => {
activateTooltips ( ) ;
} , 1000 ) ;
}
function dataResponseHandler ( json ) {
// console.log(json)
totalNotFiltered = json . totalNotFiltered ;
total = json . total ;
json = json . items ;
json . forEach ( ( item ) => {
colorStatus = '' ;
item . actions = `
< button class = "btn btn-primary" data - bs - toggle = "modal" data - bs - target = "#storageLocationModal" onclick = "primeEdit(); getDataForEditLoc('${item.id}')" >
< i class = "bi bi-pencil" > < / i >
< / b u t t o n >
< button class = "btn btn-danger" onclick = "preFillDeleteModalNxt('${item.id}','storageLocations','Storage Location')" data - bs - toggle = "modal" data - bs - target = "#staticBackdrop" >
< i class = "bi bi-trash" > < / i >
< / b u t t o n > ` ;
if ( item . storageUnit == null ) {
item . storageUnit = '<i>No storage unit assigned</i>' ;
} else {
item . storageUnit = item . storageUnit . name ;
console . log ( item . storageUnit ) ;
}
// item.SKU = `<p data-bs-toggle="tooltip" data-bs-placement="left" data-bs-title="ID: ${item.id}">${item.SKU}</p>`
} ) ;
///// --------------------------------- /////
setTimeout ( ( ) => {
activateTooltips ( ) ;
} , 200 ) ;
return { rows : json , total : total , totalNotFiltered : totalNotFiltered , totalRows : total } ;
}
function dataResponseHandlerUnit ( json ) {
// console.log(json)
totalNotFiltered = json . totalNotFiltered ;
total = json . total ;
json = json . items ;
json . forEach ( ( item ) => {
colorStatus = '' ;
item . actions = `
< button class = "btn btn-primary" data - bs - toggle = "modal" data - bs - target = "#storageUnitModal" onclick = "primeEdit(); getDataForEdit('${item.id}')" >
< i class = "bi bi-pencil" > < / i >
< / b u t t o n >
< button class = "btn btn-danger" onclick = "preFillDeleteModalNxt('${item.id}','storageUnits','Storage Unit')" data - bs - toggle = "modal" data - bs - target = "#staticBackdrop" >
< i class = "bi bi-trash" > < / i >
< / b u t t o n > ` ;
if ( item . contactInfo == null ) {
item . address = '<i>No address assigned</i>' ;
} else {
item . address = ` ${ item . contactInfo . street } ${ item . contactInfo . houseNumber } , ${ item . contactInfo . city } ${ item . contactInfo . country } ` ;
}
// item.SKU = `<p data-bs-toggle="tooltip" data-bs-placement="left" data-bs-title="ID: ${item.id}">${item.SKU}</p>`
} ) ;
///// --------------------------------- /////
setTimeout ( ( ) => {
activateTooltips ( ) ;
} , 200 ) ;
return { rows : json , total : total , totalNotFiltered : totalNotFiltered , totalRows : total } ;
}
handleSelector ( ) ;