-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.list-locations.php
165 lines (144 loc) · 5.41 KB
/
section.list-locations.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
154
155
156
157
158
159
160
161
162
163
164
165
<?php
require_once 'autoload.php';
use Transport\Location;
?>
<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('locations: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: 'locations', url: 'section.edit-location.php', forceReload: true });">
New Location
</button>
</div>
<div class="d-flex justify-content-between mt-3">
<h2>Locations</h2>
</div>
<table id="table-locations" class="table table-striped">
<thead>
<tr class="table-dark">
<th>Name</th>
<th data-priority="2">Short Name</th>
<th>Type</th>
<th>Map Address</th>
<th class="fit no-sort" data-priority="1"> </th>
</tr>
</thead>
<tbody>
<?php if ($rows = Location::getAll()): ?>
<?php foreach ($rows as $row): ?>
<tr data-id="<?= $row->id ?>">
<td><?= $row->name ?></td>
<td><?= $row->short_name ?></td>
<td><?= $row->type ?></td>
<td data-order="<?= $row->map_address ?>">
<?php if ($row->type === 'virtual'): ?>
<i class="fa-solid fa-globe fa-xl me-2" style="color:cornflowerblue"></i> Not applicable
<?php else:?>
<?php if ($row->place_id || $row->osm_id): ?>
<i class="fa-solid fa-location-check fa-xl text-primary me-2"></i>
<?php else: ?>
<i class="fa-solid fa-location-exclamation fa-xl text-warning me-2"></i>
<?php endif; ?>
<?= $row->map_address ?>
<?php endif; ?>
</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:location', this)"></i>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<script>
if (!documentEventExists('locations:reloadList')) {
$(document).on('locations:reloadList', async (e) => {
$(document).trigger('loadMainSection', {
sectionId: 'locations',
url: 'section.list-locations.php',
forceReload: true
});
});
}
if (!documentEventExists('listActionItem:location')) {
$(document).on('listActionItem:location', 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('location: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('location: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('location:edit')) {
$(document).on('location:edit', async (e, id) => {
$(document).trigger('loadMainSection', {
sectionId: 'locations',
url: `section.edit-location.php?id=${id}`,
forceReload: true
});
});
}
if (!documentEventExists('location:delete')) {
$(document).on('location:delete', async (e, id) => {
if (await ui.ask('Are you sure you want to delete this location?')) {
const resp = await net.get('/api/get.delete-location.php', {
id
});
if (resp.result) {
ui.toastr.success('Location deleted successfully.', 'Success');
$(document).trigger('locationChange');
return $(document).trigger('locations:reloadList');
}
ui.toastr.error('Failed to delete location: ' + resp.error, 'Error');
}
});
}
$(async ƒ => {
const tableId = 'table-locations';
const dataTableOptions = {
responsive: true,
paging: true,
};
const reloadOnEventName = 'locationChange';
const parentSectionId = `#<?= $_GET["loadedToId"] ?>`;
const thisURI = `<?= $_SERVER['REQUEST_URI'] ?>`;
initListPage({
tableId,
dataTableOptions,
reloadOnEventName,
parentSectionId,
thisURI
});
});
</script>