convert.js 3.91 KB
"use strict";

/** Coordinate container that occupies the whole page **/
const A4 = new Div(
	new Vector(0, 0),
	new Vector(595, 842)
);

const Position = {
	"beggining": 0,
	"beggining_margin": 5,
	"quarter": 25,
	"third": 33.33,
	"middle": 50,
	"two_thirds": 66.67,
	"three_quarters": 75,
	"end_margin": 95,
	"end": 100
};

function generateDivTree(data) {
	/**
	 * The lambdas serve as "apply" functions
	 * Each function processes an element, executed in order of appeareance in HTML.
	 * They all return a coordinate "pointer" to draw.
	 */
	const functionMapping = [
		(pointer, data) => headerToPDF(pointer, data['header']),
		(pointer, data) => alineadorToPDF(pointer, data['alineador']),
		(pointer, data) => suspensionToPDF(pointer, data['suspension']),
		(pointer, data) => frenosToPDF(pointer, data['frenos']),
		(pointer, data) => traseroToPDF(pointer, data['trasero']),
		(pointer, data) => gaseshumosToPDF(pointer, data['gaseshumos'])
	];

	/**
	 * We pass the pointer to each function, which then modify it and passes it to the next,
	 * We start with (0%,0%)
	 */
	functionMapping.reduce(
		(pointer, treeMaker) => treeMaker(pointer, data),
		A4.position
	);

	return A4;
}

function headerToPDF(pointer, data) {

	const header = A4.addChild(pointer, new Vector(Position.end, 3));

	header.addText(
		"Dominio: " + data['dominio'],
		new Vector(Position.beggining_margin, Position.two_thirds)
	);

	header.addText(
		"Fecha: " + data['fecha'],
		new Vector(Position.third, Position.two_thirds)
	);

	return new Vector(pointer.x, pointer.y + 3);
}

function alineadorToPDF(pointer, data) {

	const alineador = A4.addChild(pointer, new Vector(Position.end, 5));

	const title = alineador.addText(
		"Resultado Pruebas en Banco de Alineador al Paso",
		new Vector(Position.beggining_margin + 2, Position.third)
	);

	const subtitle = alineador.addText(
		"Eje Delantero",
		new Vector(Position.beggining_margin + 2, Position.two_thirds)
	);

	const value = alineador.addText(
		data['eje_delan'],
		new Vector(Position.quarter, Position.two_thirds)
	);

	return new Vector(pointer.x, pointer.y + 5);
}

function suspensionToPDF(pointer, data) {

	const suspension = A4.addChild(pointer, new Vector(Position.end, 15));

	const eje1 = suspension.addChild(new Vector(Position.beggining_margin + 2, 0), new Vector(86, 50))
	const eje2 = suspension.addChild(new Vector(Position.beggining_margin + 2, 50), new Vector(86, 50))

	const table = (container, eje, RI, peso, RD) => {
		
		container.addText(
			`Resultado Pruebas en Banco de Suspensión - Eje ${eje}`,
			new Vector(0, Position.third)
		);
		
		container.addText(
			"Rendimiento Izquierdo",
			new Vector(0, Position.two_thirds)
		);

		container.addText(
			RI,
			new Vector(0, Position.end)
		);
		
		container.addText(
			"Peso Total del Eje",
			new Vector(37, Position.two_thirds)
		);

		container.addText(
			peso,
			new Vector(45, Position.end)
		);
		
		container.addText(
			"Rendimiento Derecho",
			new Vector(80, Position.two_thirds)
		);

		container.addText(
			RD,
			new Vector(90, Position.end)
		);
	};

	table(eje1, 1, data[0]["ren_izq"], data[0]["peso"], data[0]["ren_der"])
	table(eje2, 2, data[1]["ren_izq"], data[1]["peso"], data[1]["ren_der"])

	return new Vector(pointer.x, pointer.y + 15);
}

function frenosToPDF(pointer, data) {
	A4.addChild("frenos", pointer, new Vector(Position.end, 60));
	return new Vector(pointer.x, pointer.y + 60);
}

function traseroToPDF(pointer, data) {
	A4.addChild("trasero", pointer, new Vector(Position.end, 9));
	return new Vector(pointer.x, pointer.y + 9);
}

function gaseshumosToPDF(pointer, data) {
	A4.addChild("gaseshumos", pointer, new Vector(Position.end, 9));
	return new Vector(pointer.x, pointer.y + 9);
}

function compile(tree, pdf) {
	tree.children.forEach( child => {
		switch (child.constructor.name) {
			case "Text":
				pdf.text(child.text, child.position.x, child.position.y);
				break;
			case "Div":
				compile(child, pdf);
				break;
		}
	});
}