aboutsummaryrefslogtreecommitdiffstats
path: root/src/scripts/functionality/view-khatian/index.ts
blob: baf6d8e514a39e245af216dacdfc8a69f5a60418 (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
import { downloadPDF } from "@/shared/download-pdf";
import getKhatinDetailsPDFPageData from "@/scripts/functionality/view-khatian/khatian-download-document";

declare function loadKhatian(): void;

document.addEventListener("DOMContentLoaded", () => {
	const proxiedPost = $.post;

	const submitButtonElement = document.querySelector("#khbutton") as HTMLButtonElement;
	const separatorElement = document.querySelector("#bodycover > div > form > hr:nth-child(2)") as HTMLElement;
	const formElement = document.querySelector("#bodycover > div > form") as HTMLElement;
	let downloadPDFButton: HTMLButtonElement | null = null;

	const submitButtonElementComputedStyles = window.getComputedStyle(submitButtonElement);
	const styles = Array.from(submitButtonElementComputedStyles)
		.reduce(
			(str, property) => `${str}${property}:${submitButtonElementComputedStyles.getPropertyValue(property)};`,
			""
		);

	const getValueOfSelectElement = (selector: string) => {
		const element = document.querySelector(selector) as HTMLSelectElement;
		return element.options[element.selectedIndex].text;
	};

	const downloadKhatianInformationPDF = () => {
		const documentStringContent = getKhatinDetailsPDFPageData(document.querySelector("#khdetails")!.innerHTML, {
			district: getValueOfSelectElement("#lstDistrictCode1"),
			block: getValueOfSelectElement("#lstBlockCode1"),
			mouza: getValueOfSelectElement("#lstMouzaList")
		});
		downloadPDF(`<div id="content">${documentStringContent}</div>`, "khatian-details.pdf");
	};

	$.post = function() {
		if (arguments[0].startsWith("khDetailsAction_LandInfo.action")) {
			const callback = arguments[2];
			arguments[2] = (data: any) => {
				callback(data);
				const tableElement = document.querySelector("#khdetails > table > tbody > tr > td:nth-child(1) > div:nth-child(5) > table > tbody") as HTMLElement | null;

				if (tableElement) {
					tableElement.style.height = "auto";
					tableElement.style.width = "100%";

					if (!downloadPDFButton) {
						downloadPDFButton = document.createElement("button");
						downloadPDFButton.innerHTML = "Download PDF";
						downloadPDFButton.style.cssText = styles;
						downloadPDFButton.style.width = "auto";
						downloadPDFButton.addEventListener("click", downloadKhatianInformationPDF);
						formElement.insertBefore(downloadPDFButton, separatorElement);
					}
				} else if (downloadPDFButton) {
					formElement.removeChild(downloadPDFButton);
					downloadPDFButton = null;
				}
			}
		}

		return proxiedPost.apply(this, Array.from(arguments) as any);
	}
});