-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporter.js
More file actions
144 lines (129 loc) · 5.79 KB
/
exporter.js
File metadata and controls
144 lines (129 loc) · 5.79 KB
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
/**
* Generate a text-based summary of flights for clipboard
*/
export function generateTextSummary(flights) {
let text = '';
flights.forEach((f, i) => {
if (i > 0) text += '\n' + '─'.repeat(40) + '\n\n';
text += (f.type === 'arrival' ? '🛬 ARRIVING' : '🛫 DEPARTING') + `\nFlight: ${f.number || 'Unknown'}\n`;
if (f.date) text += `Date: ${f.date}\n`;
if (f.registration) text += `Aircraft: ${f.registration} (${f.aircraft || ''})\n`;
const time = f.eta || f.sta || f.std || f.etd;
if (time) {
let label = f.eta ? 'ETA' : f.sta ? 'STA' : f.std ? 'STD' : 'ETD';
text += `${label}: ${time}\n`;
}
if (f.gate) text += `Gate: ${f.gate}${f.towGate ? ` (Tow to ${f.towGate})` : ''}\n`;
if (f.stand) text += `Stand: ${f.stand}\n`;
if (f.counters) text += `Counters: ${f.counters}\n`;
if (f.lateral) text += `Baggage Belt: ${f.lateral}\n`;
if (f.total) {
let paxStr = `Pax: ${f.total}`;
let details = [];
if (f.paxBusiness) details.push(`${f.paxBusiness}C`);
if (f.paxEconomy) details.push(`${f.paxEconomy}Y`);
if (f.infants) details.push(`${f.infants}INF`);
if (details.length > 0) paxStr += ` (${details.join('/')})`;
text += paxStr + '\n';
}
if (f.wheelchairs || f.specialServices) {
let lines = [];
if (f.wheelchairs) for (const wc of f.wheelchairs) lines.push(`${wc.count}x ${wc.type}`);
if (f.specialServices) for (const svc of f.specialServices) lines.push(`${svc.count}x ${svc.code}`);
text += `Special: ${lines.join(', ')}\n`;
}
if (f.connecting) {
let conx = `Connections: ${f.connecting.count} pax`;
if (f.connecting.flight) conx += ` to ${f.connecting.flight} @ ${f.connecting.time}`;
text += conx + '\n';
}
if (f.bags) text += `Bags: ${f.bags}\n`;
if (f.carousel) text += `Carousel: ${f.carousel}${f.carouselNote ? ` (${f.carouselNote})` : ''}\n`;
if (f.cargo) text += `Cargo: ${f.cargo.toLocaleString()} kg${f.cargoPieces ? ` (${f.cargoPieces} pcs)` : ''}\n`;
else if (f.cargoNil) text += `Cargo: NIL\n`;
if (f.specialCargo && f.specialCargo.length > 0) {
text += `Special Cargo:\n - ${f.specialCargo.map(c => c.replace(/^[^\s]+\s*/, '')).join('\n - ')}\n`;
}
if (f.remarks) text += `Remarks: ${f.remarks}\n`;
});
return text.trim();
}
/**
* Clipboard operations (Text and Image)
*/
export async function copyToClipboard(flights, btn) {
try {
const text = generateTextSummary(flights);
await navigator.clipboard.writeText(text);
showSuccess(btn);
} catch (err) { console.error(err); }
}
export async function copyImageToClipboard(cardElement, btn) {
const label = btn.querySelector('.btn-label');
const originalText = label.textContent;
try {
label.textContent = 'Copying...';
btn.disabled = true;
const canvas = await generateCanvas(cardElement);
canvas.toBlob(async (blob) => {
try {
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
showSuccess(btn, 'Copied!', originalText);
} catch (err) { console.error(err); btn.disabled = false; label.textContent = originalText; }
}, 'image/png');
} catch (err) { console.error(err); btn.disabled = false; label.textContent = originalText; }
}
/**
* File Download (PNG)
*/
export async function saveAsPNG(cardElement, flights, btn) {
const label = btn.querySelector('.btn-label');
const originalText = label.textContent;
try {
label.textContent = 'Saving...';
btn.disabled = true;
const canvas = await generateCanvas(cardElement);
let filename = 'squawk';
if (flights.length > 0) {
const flightNumbers = flights.filter(f => f.number).map(f => f.number);
filename += `-${flightNumbers.join('-')}`;
const fWithDate = flights.find(f => f.date);
if (fWithDate && fWithDate.date) {
const dateMatch = fWithDate.date.match(/(\w+)\s+(\d+),?\s*(\d{4})?/);
if (dateMatch) filename += `-${dateMatch[2]}${dateMatch[1]}${dateMatch[3] || ''}`;
}
}
const link = document.createElement('a');
link.download = filename + '.png';
link.href = canvas.toDataURL('image/png');
link.click();
showSuccess(btn, 'Saved!', originalText);
} catch (err) { console.error(err); btn.disabled = false; label.textContent = originalText; }
}
/**
* Internal helpers
*/
async function generateCanvas(element) {
const cardClone = element.cloneNode(true);
cardClone.style.cssText = `position: absolute; left: -9999px; top: 0; width: 500px;`;
document.body.appendChild(cardClone);
await new Promise(resolve => setTimeout(resolve, 150));
const canvas = await html2canvas(cardClone, { backgroundColor: null, scale: 2, logging: false, useCORS: true });
document.body.removeChild(cardClone);
return canvas;
}
function showSuccess(btn, successText = 'Copied!', originalText = 'Copy Text') {
const label = btn.querySelector('.btn-label');
const oldText = label.textContent;
const oldAriaLabel = btn.getAttribute('aria-label') || btn.getAttribute('title');
label.textContent = successText;
btn.setAttribute('aria-label', successText);
btn.classList.add('success');
btn.disabled = false;
setTimeout(() => {
label.textContent = originalText || oldText;
if (oldAriaLabel) btn.setAttribute('aria-label', oldAriaLabel);
else btn.removeAttribute('aria-label');
btn.classList.remove('success');
}, 2000);
}