250 lines
6 KiB
JavaScript
250 lines
6 KiB
JavaScript
// http.js
|
|
import { serve } from 'bun';
|
|
|
|
serve({
|
|
port: 5000,
|
|
async fetch(request) {
|
|
console.log(`${request.method}: ${request.url}`);
|
|
if (request.method === 'POST') {
|
|
const url = new URL(request.url);
|
|
let json;
|
|
if (url.pathname === '/GetSVGLayoutConfiguration') {
|
|
json = GetSVGLayoutConfiguration();
|
|
} else if (url.pathname === '/SetContainerList') {
|
|
const bodyParsed = await request.json();
|
|
console.log(bodyParsed);
|
|
switch (bodyParsed.Action) {
|
|
case 'FillHoleWithChassis':
|
|
json = FillHoleWithChassis(bodyParsed);
|
|
break;
|
|
case 'SplitRemplissage':
|
|
json = SplitRemplissage(bodyParsed);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} else if (url.pathname === '/GetFeedback') {
|
|
const bodyParsed = await request.json();
|
|
console.log(bodyParsed);
|
|
json = {
|
|
messages: [
|
|
{
|
|
text: `${new Date()}`,
|
|
type: 3
|
|
}
|
|
]
|
|
}
|
|
} else {
|
|
// TODO: Return 404 rather than this
|
|
json = GetSVGLayoutConfiguration();
|
|
}
|
|
return new Response(JSON.stringify(json), {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Headers': '*'
|
|
}
|
|
});
|
|
}
|
|
|
|
return new Response('Welcome to Bun!', {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'text/plain',
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Headers': '*'
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
const GetSVGLayoutConfiguration = () => {
|
|
return {
|
|
AvailableContainers: [
|
|
{
|
|
Type: 'Chassis',
|
|
MaxWidth: 500,
|
|
MinWidth: 200,
|
|
Width: 200,
|
|
DefaultChildType: 'Trou',
|
|
Style: {
|
|
fillOpacity: 1,
|
|
strokeWidth: 2,
|
|
stroke: 'red',
|
|
fill: '#d3c9b7',
|
|
},
|
|
ShowSelfDimensions: true,
|
|
IsDimensionBorrower: true,
|
|
Category: "Category"
|
|
},
|
|
{
|
|
Type: 'Trou',
|
|
DefaultX: 0,
|
|
DefaultY: 0,
|
|
Margin: {
|
|
left: 10,
|
|
bottom: 10,
|
|
top: 10,
|
|
right: 10,
|
|
},
|
|
DefaultChildType: 'Remplissage',
|
|
Style: {
|
|
fillOpacity: 1,
|
|
strokeWidth: 2,
|
|
stroke: 'green',
|
|
fill: 'white'
|
|
},
|
|
Category: "Category"
|
|
},
|
|
{
|
|
Type: 'Remplissage',
|
|
Category: "Category2",
|
|
CustomSVG: `
|
|
<rect width="{width}" height="{height}" style="{style}"></rect>
|
|
<rect width="{width}" height="{height}" stroke="black" fill-opacity="0"></rect>
|
|
<line x1="0" y1="0" x2="{width}" y2="{height}" stroke="black" style='{{ "transform":"scaleY(0.5)"}}'></line>
|
|
<line x1="{width}" y1="0" x2="0" y2="{height}" stroke="black" style='{userData.styleLine}'></line>
|
|
`
|
|
,
|
|
Actions: [
|
|
{
|
|
Action: "SplitRemplissage",
|
|
Label: "Diviser le remplissage",
|
|
Description: "Diviser le remplissage en insérant un montant",
|
|
CustomLogo: {
|
|
Url: ""
|
|
},
|
|
AddingBehavior: 2
|
|
}
|
|
],
|
|
Style: {
|
|
fillOpacity: 1,
|
|
strokeWidth: 1,
|
|
fill: '#bfdbfe'
|
|
},
|
|
UserData: {
|
|
styleLine: {
|
|
transform: "scaleY(0.5) translateY(100%)",
|
|
transformBox: "fill-box"
|
|
}
|
|
}
|
|
},
|
|
{
|
|
Type: 'Montant',
|
|
Width: 10,
|
|
XPositionReference: 1,
|
|
MarkPositionToDimensionBorrower: true,
|
|
Style: {
|
|
fillOpacity: 0,
|
|
strokeWidth: 2,
|
|
stroke: '#713f12',
|
|
fill: '#713f12',
|
|
}
|
|
},
|
|
{
|
|
Type: 'Dilatation',
|
|
Width: 4,
|
|
XPositionReference: 1,
|
|
MarkPositionToDimensionBorrower: true,
|
|
Style: {
|
|
fillOpacity: 0,
|
|
strokeWidth: 2,
|
|
stroke: '#713f12',
|
|
fill: '#713f12',
|
|
}
|
|
},
|
|
{
|
|
Type: '200',
|
|
MaxWidth: 500,
|
|
MinWidth: 200,
|
|
Style: {
|
|
fillOpacity: 1,
|
|
strokeWidth: 2,
|
|
stroke: 'blue',
|
|
fill: 'blue',
|
|
}
|
|
},
|
|
{
|
|
Type: '400',
|
|
MaxWidth: 500,
|
|
MinWidth: 400,
|
|
Style: {
|
|
fillOpacity: 1,
|
|
strokeWidth: 2,
|
|
stroke: 'red',
|
|
fill: 'red',
|
|
}
|
|
}
|
|
],
|
|
AvailableSymbols: [
|
|
{
|
|
Width: 32,
|
|
Height: 32,
|
|
Image: {
|
|
Base64Image: null,
|
|
Name: null,
|
|
Svg: null,
|
|
Url: 'https://www.manutan.fr/img/S/GRP/ST/AIG3930272.jpg'
|
|
},
|
|
Name: 'Poteau structure',
|
|
XPositionReference: 1
|
|
},
|
|
{
|
|
Width: 32,
|
|
Height: 32,
|
|
Image: {
|
|
Base64Image: null,
|
|
Name: null,
|
|
Svg: null,
|
|
Url: 'https://e7.pngegg.com/pngimages/647/127/png-clipart-svg-working-group-information-world-wide-web-internet-structure.png'
|
|
},
|
|
Name: 'Joint de structure',
|
|
XPositionReference: 0
|
|
}
|
|
],
|
|
MainContainer: {
|
|
Height: 200,
|
|
Width: 800
|
|
}
|
|
};
|
|
};
|
|
|
|
const FillHoleWithChassis = (request) => {
|
|
const maxWidthChassis = 3000;
|
|
const nbChassis = Math.ceil(request.ContainerAction.Width / maxWidthChassis);
|
|
const lstModels = [];
|
|
for (let i = 0; i <= nbChassis; i++) {
|
|
if (i === 1 && request.ContainerAction.ExistOnBefore) {
|
|
lstModels.push({ Type: 'Dilatation' });
|
|
}
|
|
lstModels.push({ Type: 'Chassis' });
|
|
if (i < nbChassis) {
|
|
lstModels.push({ Type: 'Dilatation' });
|
|
}
|
|
if (i === nbChassis && request.ContainerAction.ExistOnAfter) {
|
|
lstModels.push({ Type: 'Dilatation' });
|
|
}
|
|
}
|
|
return {
|
|
Containers: lstModels
|
|
};
|
|
};
|
|
|
|
const SplitRemplissage = (request) => {
|
|
const lstModels = [
|
|
{
|
|
Type: 'Remplissage'
|
|
},
|
|
{
|
|
Type: 'Montant'
|
|
},
|
|
{
|
|
Type: 'Remplissage'
|
|
},
|
|
];
|
|
|
|
return {
|
|
Containers: lstModels
|
|
};
|
|
};
|