Hi there !
I am working on a drag&drop tool with kirby. My page is divided in two sections, i have a imgs list on the left and a grid with cols and rows on the right. I can drag imgs from the left into the grid on the right. (see img below)
What i am trying to do is to store the position and the url of the images dropped into the grid, so when i refresh the page they’re still at the same location on the grid. If i move them or drag new imgs into the grid, all imags’s location and url are still stored.
I have multiple pages like this one as subpages.
What i’ve done :
- A javascript script to get all the imgs dropped (url) and their location (col, row), and store them into an array in json format.
function getWallState(){
var jsonFile = [];
let droppedCeramic = document.querySelectorAll(".dropped-img[draggable='true']");
for(item of droppedCeramic){
let ceramicsURL = {
"name" : item.getAttribute("src"),
"col" : item.parentNode.getAttribute("col"),
"row" : item.parentNode.getAttribute("row")
}
jsonFile.push(ceramicsURL);
}
var jsonToString = JSON.stringify(jsonFile);
console.log(jsonToString); // output json as text
}
the json file :
[
{
"name": "http://localhost:8021/calepinage_tool/plainkit-main/media/pages/ceramics/8608efaff4-1649948519/09-100x-crop.jpg",
"col": "7",
"row": "1"
},
{
"name": "http://localhost:8021/calepinage_tool/plainkit-main/media/pages/ceramics/0e70c6bebf-1649948519/14-100x-crop.jpg",
"col": "2",
"row": "2"
},
{
"name": "http://localhost:8021/calepinage_tool/plainkit-main/media/pages/ceramics/21bc659827-1650018671/screenshot-emoji-glyphs-5-100x-crop.png",
"col": "5",
"row": "2"
},
{
"name": "http://localhost:8021/calepinage_tool/plainkit-main/media/pages/ceramics/3b1d9f3851-1649948519/07-100x-crop.jpg",
"col": "4",
"row": "3"
}
]
- I saw the topic about generating json with kirby but i don’t think it’s what i need, or maybe i’m missing something (i am new to kirby and php).
I think that i need to create a json file after the page is created, and use kirby API to write my jsonArray to this file and get it back with an XMLHttpRequest ( i got this last one).
XMLHttpRequest :
var ceramicsDropped;
var requestURL = 'ceramics.json';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.responseType = 'json';
request.send();
request.onload = function() {
ceramicsDropped = request.response;
var rowItems = document.getElementsByClassName("row-item");
for (ceramic of ceramicsDropped){
//console.log(ceramic);
var cols = ceramic.col;
var rows = ceramic.row;
for (row of rowItems){
// console.log("col : ", row.getAttribute("col"));
// console.log("row : ", row.getAttribute("row"));
var colTag = row.getAttribute("col");
var rowTag = row.getAttribute("row");
if ((cols == colTag) && (rows == rowTag)){
// console.log("row", row);
// console.log("ceramic", ceramic);
calepinage(row, ceramic.name);
}
}
}
} // FIN DU ONLOAD
To be more clear
- how to generate a json file each time a subpage is created
- how to write into this json file
Is anyone could help me to figure it out ? :;-)]}