aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/functionality/sheet-map/index.ts
blob: e76650eb80ffc3f427f34652a5486860207746b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { generateWebPage } from "@/shared/generate-web-page";
import getDownloadMapPDFPageContent from "@/scripts/functionality/sheet-map/download-map-pdf-page-content.html";
import getMapPlotPolygonPathElement from "@/scripts/functionality/sheet-map/map-plot-polygon-path-element.html";
import getMapPlotNumberLabelTextElement from "@/scripts/functionality/sheet-map/map-plot-number-label-text-element.html";
import getPlotInformationElement from "@/scripts/functionality/sheet-map/plot-information-element.html";

interface PlotPolygon {
	plotArea: number;
	plotno: string;
	polygon: string;
};

interface PlotNumberLabel {
	plotno: string;
	point: string;
}

const headerElement = document.querySelector("#headerTable > tbody > tr")!;
const plotNumberInput = document.querySelector<HTMLInputElement>("#txtPlotNo")!;
const plotInformation = document.createElement("div") as HTMLDivElement;
let plotPolygonPathElements = "";
let plotNumberLabelTextElements = "";

document.body.appendChild(plotInformation);

const createHeaderButton = (text: string) => {
	const buttonContainer = document.createElement("td");
	const button = document.createElement("button");
	button.innerHTML = text;
	button.style.display = "none";
	buttonContainer.appendChild(button);
	headerElement.appendChild(buttonContainer);
	return button;
};

const setPlotInformation = (plotPolygon: PlotPolygon | null | undefined = null) => {
	if (plotPolygon) {
		plotInformation.innerHTML = getPlotInformationElement({
			area: (plotPolygon.plotArea/1000).toFixed(3),
			plotNumber: plotPolygon.plotno
		});
	} else {
		plotInformation.innerHTML = getPlotInformationElement({
			area: "0.000",
			plotNumber: ""
		});
	}
};

const downloadPDF = (labelPoints: boolean = true) => {
	generateWebPage(
		getDownloadMapPDFPageContent({
			mapContent: plotPolygonPathElements + (labelPoints ? plotNumberLabelTextElements : "")
		}),
		"map"
	);
};

const downloadSVGButton = createHeaderButton("SAVE SVG");
const downloadPDFButton = createHeaderButton("SAVE PDF");
const downloadPDFNoLabelButton = createHeaderButton("SAVE PDF (NO LABEL)");

downloadSVGButton.addEventListener("click", e => {
	e.preventDefault();
	const element = document.createElement("a");
	element.setAttribute(
		"href",
		"data:text/plain;charset=utf-8," + encodeURIComponent(plotPolygonPathElements + plotNumberLabelTextElements)
	);
	element.setAttribute("download", "map.svg");
	element.style.display = "none";
	document.body.appendChild(element);
	element.click();
	document.body.removeChild(element);
});

downloadPDFButton.addEventListener("click", e => {
	e.preventDefault();
	downloadPDF();
});

downloadPDFNoLabelButton.addEventListener("click", e => {
	e.preventDefault();
	downloadPDF(false);
});

setPlotInformation();

(async () => {
	const searchParams = new URLSearchParams(window.location.search);
	const response1 = await fetch(
		"/BanglarBhumi/sheetMap_populateLayerData?lstSheetNo=" + searchParams.get("lstSheetNo"),
		{
			method: "POST"
		});
	const plotPolygonList: PlotPolygon[] = (await response1.json()).features;
	const response2 = await fetch(
		"/BanglarBhumi/sheetMap_populateCentroidData?lstSheetNo=" + searchParams.get("lstSheetNo"),
		{
			method: "POST"
		});
	const plotNumberLabelList: PlotNumberLabel[] = (await response2.json()).features;
	let plotPolygons: { [key: string]: PlotPolygon } = {};

	for (const plotPolygon of plotPolygonList) {
		const plotPolygonPoints = plotPolygon.polygon.split("(((")[1].split(")))")[0];
		plotPolygonPathElements += getMapPlotPolygonPathElement({
			polygonVertices: plotPolygonPoints
		});
	}

	for (const plotNumberLabel of plotNumberLabelList) {
		const plotNumberLabelPoint = plotNumberLabel.point.split("(")[1].split(")")[0].split(" ");
		plotNumberLabelTextElements += getMapPlotNumberLabelTextElement({
			x: plotNumberLabelPoint[0],
			y: plotNumberLabelPoint[1],
			plotNumber: plotNumberLabel.plotno
		});
	}

	downloadSVGButton.style.display = "inherit";
	downloadPDFButton.style.display = "inherit";
	downloadPDFNoLabelButton.style.display = "inherit";

	document.querySelector("#btnSearch")!.addEventListener("click", () => {
		let plotPolygon;
		for (const p of plotPolygonList) {
			if (p.plotno === plotNumberInput.value) {
				plotPolygon = p;
				break;
			}
		}
		setPlotInformation(plotPolygon);
	});

	await new Promise<void>((resolve, reject) => {
		const plotElementIdsControl = setInterval(() => {
			let i = 0;
			let plotElements = document.querySelector("#OpenLayers_Layer_Vector_25_vroot")?.children || [];
			if (plotElements.length) {
				clearInterval(plotElementIdsControl);
				for (let i = 0; i < plotElements.length; i++) {
					plotPolygons[plotElements[i].id] = plotPolygonList[i];
				}
				resolve();
			}
		}, 50);
	});

	setInterval(() => {
		for (const plotElementId in plotPolygons) {
			const e = document.getElementById(plotElementId);
			if (e) {
				e.removeEventListener("click", handlePlotClick);
				e.addEventListener("click", handlePlotClick);
			}
		}
	}, 300);

	const handlePlotClick = (e: MouseEvent) => {
		document.querySelectorAll("path").forEach(e => {
			e.setAttribute("fill", "#ffcc66");
		});

		const target = e.target as SVGPathElement;

		target.setAttribute("fill", "#8aeeef");
		setPlotInformation(plotPolygons[target.id]);
	};
})();