-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.list-airports.php
153 lines (132 loc) · 5.19 KB
/
section.list-airports.php
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
<?php
require_once 'autoload.php';
use Transport\Airport;
?>
<div class="d-flex justify-content-between top-page-buttons">
<button class="btn btn-sm btn-outline-primary px-2 ms-auto me-2" onclick="$(document).trigger('airports:reloadList');">
<i class="fa-solid fa-arrow-rotate-right"></i>
Reload
</button>
<button class=" btn btn-outline-primary btn-sm px-2" onclick="$(document).trigger('loadMainSection', { sectionId: 'airports', url: 'section.edit-airport.php', forceReload: true });">
New Airport
</button>
</div>
<div class="d-flex justify-content-between mt-3">
<h2>Airports</h2>
</div>
<table id="table-airports" class="table table-striped table-hover row-select">
<thead>
<tr class="table-dark">
<th>IATA</th>
<th>Airport</th>
<th data-bs-toggle="tooltip" data-bs-title="Time to arrive at airport before scheduled flight.">Lead Time</th>
<th data-bs-toggle="tooltip" data-bs-title="Time to travel from airport to staging location.">Travel Time</th>
<th class="fit no-sort" data-priority="1"> </th>
</tr>
</thead>
<tbody>
<?php if ($rows = Airport::getAll()): ?>
<?php foreach ($rows as $row): ?>
<tr data-id="<?= $row->id ?>">
<td><?= $row->iata ?></td>
<td><?= $row->name ?></td>
<td data-order="<?= $row->lead_time ?>"><?= intdiv($row->lead_time, 60) . ':' . sprintf('%02s', ($row->lead_time % 60)) ?></td>
<td data-order="<?= $row->travel_time ?>"><?= intdiv($row->travel_time, 60) . ':' . sprintf('%02s', ($row->travel_time % 60)) ?></td>
<td class="text-center align-middle">
<i class="fa-solid fa-ellipsis fa-xl text-body-tertiary action-icon pointer hidden-content" onclick="$(document).trigger('listActionItem:airport', this)"></i>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<script>
if (!documentEventExists('airports:reloadList')) {
$(document).on('airports:reloadList', async (e) => {
$(document).trigger('loadMainSection', {
sectionId: 'airports',
url: 'section.list-airports.php',
forceReload: true
});
});
}
if (!documentEventExists('listActionItem:airport')) {
$(document).on('listActionItem:airport', async (e, el) => {
e.stopPropagation();
e.stopImmediatePropagation();
const id = $(el).closest('tr').data('id');
const offset = $(el).offset();
const myRandomId = Math.random().toString(36).substring(7);
// Remove any existing dropdown menus
$(document).trigger('click');
let additionalItems = '';
// Create the dropdown menu
const dropdownMenu = `
<div id="${myRandomId}" data-id="${id}" class="dropdown-menu show shadow" style="position: absolute; left: ${offset.top}px; top: ${offset.left}px; z-index: 1000;">
<button class="dropdown-item" onclick="$(document).trigger('airport:edit', ${id})"><i class="fa-duotone fa-regular fa-pen-to-square"></i> Edit</button>
${additionalItems}
<hr class="dropdown-divider">
<button class="dropdown-item text-danger" onclick="$(document).trigger('airport:delete', ${id})"><i class="fa-duotone fa-regular fa-trash"></i> Delete</button>
</div>
`;
// Append the dropdown menu to the body
$('body').append(dropdownMenu);
// Calculate the position of the dropdown menu
const dropdownElement = $('#' + myRandomId);
const dropdownWidth = dropdownElement.outerWidth();
const leftPosition = event.pageX - dropdownWidth;
// Set the position of the dropdown menu
dropdownElement.css({
left: `${leftPosition}px`,
top: `${event.pageY}px`
});
console.log('dropdownElement:', dropdownElement);
// Remove the dropdown menu when clicking outside
setTimeout(() => {
$(document).on('click', function() {
$('#' + myRandomId).remove();
});
}, 100);
});
}
if (!documentEventExists('airport:edit')) {
$(document).on('airport:edit', async (e, id) => {
$(document).trigger('loadMainSection', {
sectionId: 'airports',
url: `section.edit-airport.php?id=${id}`,
forceReload: true
});
});
}
if (!documentEventExists('airport:delete')) {
$(document).on('airport:delete', async (e, id) => {
if (await ui.ask('Are you sure you want to delete this airport?')) {
const resp = await net.get('/api/get.delete-airport.php', {
id
});
if (resp.result) {
ui.toastr.success('Airport deleted successfully.', 'Success');
$(document).trigger('airportChange');
return $(document).trigger('airports:reloadList');
}
ui.toastr.error('Failed to delete airport: ' + resp.error, 'Error');
}
});
}
$(async ƒ => {
const tableId = 'table-airports';
const dataTableOptions = {
responsive: true
};
const reloadOnEventName = 'airportChange';
const parentSectionId = `#<?= $_GET["loadedToId"] ?>`;
const thisURI = `<?= $_SERVER['REQUEST_URI'] ?>`;
initListPage({
tableId,
dataTableOptions,
reloadOnEventName,
parentSectionId,
thisURI
});
});
</script>