-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathsqlpage.js
213 lines (198 loc) · 7.67 KB
/
sqlpage.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* !include https://cdn.jsdelivr.net/npm/@tabler/[email protected]/dist/js/tabler.min.js */
/* !include https://cdn.jsdelivr.net/npm/[email protected]/dist/list.min.js */
const nonce = document.currentScript.nonce;
function sqlpage_embed() {
for (const c of document.querySelectorAll("[data-embed]:not([aria-busy=true])")) {
c.ariaBusy = true;
let url;
try {
url = new URL(c.dataset.embed, window.location.href)
} catch {
console.error(`'${c.dataset.embed}' is not a valid url`)
continue;
}
url.searchParams.set("_sqlpage_embed", "");
fetch(url)
.then(res => res.text())
.then(html => {
c.innerHTML = html;
c.ariaBusy = false;
delete c.dataset.embed;
c.dispatchEvent(new CustomEvent("fragment-loaded", {
bubbles: true
}));
})
.catch(err => console.error("Fetch error: ", err));
}
}
function sqlpage_table(){
// Tables
for (const r of document.querySelectorAll("[data-pre-init=table]")) {
new List(r, {
valueNames: [...r.getElementsByTagName("th")].map(t => t.textContent),
searchDelay: 100,
// Hurts performance, but prevents https://github.com/lovasoa/SQLpage/issues/542
// indexAsync: true
});
r.removeAttribute("data-pre-init");
}
}
function sqlpage_select_dropdown(){
const selects = document.querySelectorAll("[data-pre-init=select-dropdown]");
if (!selects.length) return;
const src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.popular.min.js";
if (!window.TomSelect) {
const script = document.createElement("script");
script.src= src;
script.integrity = "sha384-aAqv9vleUwO75zAk1sGKd5VvRqXamBXwdxhtihEUPSeq1HtxwmZqQG/HxQnq7zaE";
script.crossOrigin = "anonymous";
script.nonce = nonce;
script.onload = sqlpage_select_dropdown;
document.head.appendChild(script);
return;
}
for (const s of selects) {
new TomSelect(s, {
create: s.dataset.create_new
});
}
}
let is_leaflet_injected = false;
let is_leaflet_loaded = false;
function sqlpage_map() {
const first_map = document.querySelector("[data-pre-init=map]");
if (first_map && !is_leaflet_injected) {
// Add the leaflet js and css to the page
const leaflet_css = document.createElement("link");
leaflet_css.rel = "stylesheet";
leaflet_css.href = "https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.css";
leaflet_css.integrity = "sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=";
leaflet_css.crossOrigin = "anonymous";
document.head.appendChild(leaflet_css);
const leaflet_js = document.createElement("script");
leaflet_js.src = "https://cdn.jsdelivr.net/npm/[email protected]/dist/leaflet.js";
leaflet_js.integrity = "sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=";
leaflet_js.crossOrigin = "anonymous";
leaflet_js.nonce = nonce;
leaflet_js.onload = onLeafletLoad;
document.head.appendChild(leaflet_js);
is_leaflet_injected = true;
}
if (first_map && is_leaflet_loaded) {
onLeafletLoad();
}
function parseCoords(coords) {
return coords && coords.split(",").map(c => parseFloat(c));
}
function onLeafletLoad() {
is_leaflet_loaded = true;
const maps = document.querySelectorAll("[data-pre-init=map]");
for (const m of maps) {
const tile_source = m.dataset.tile_source;
const maxZoom = +m.dataset.max_zoom;
const attribution = m.dataset.attribution;
const map = L.map(m, { attributionControl: !!attribution });
const zoom = m.dataset.zoom;
let center = parseCoords(m.dataset.center);
if (tile_source) L.tileLayer(tile_source, { attribution, maxZoom }).addTo(map);
map._sqlpage_markers = [];
for (const marker_elem of m.getElementsByClassName("marker")) {
setTimeout(addMarker, 0, marker_elem, map);
}
setTimeout(() => {
if (center == null && map._sqlpage_markers.length) {
map.fitBounds(map._sqlpage_markers.map(m =>
m.getLatLng ? m.getLatLng() : m.getBounds()
));
if (zoom != null) map.setZoom(+zoom);
} else map.setView(center, +zoom);
}, 100);
m.removeAttribute("data-pre-init");
m.getElementsByClassName("spinner-border")[0]?.remove();
}
}
function addMarker(marker_elem, map) {
const { dataset } = marker_elem;
const options = {
color: marker_elem.dataset.color,
title: marker_elem.getElementsByTagName("h3")[0].textContent.trim(),
};
const marker =
dataset.coords ? createMarker(marker_elem, options)
: createGeoJSONMarker(marker_elem, options);
marker.addTo(map);
map._sqlpage_markers.push(marker);
if (options.title) marker.bindPopup(marker_elem);
else if (marker_elem.dataset.link) marker.on('click', () => window.location = marker_elem.dataset.link);
}
function createMarker(marker_elem, options) {
const coords = parseCoords(marker_elem.dataset.coords);
const icon_obj = marker_elem.getElementsByClassName("mapicon")[0];
if (icon_obj) {
const size = 1.5 * +(options.size || icon_obj.firstChild?.getAttribute('width') || 24);
options.icon = L.divIcon({
html: icon_obj,
className: `border-0 bg-${options.color || 'primary'} bg-gradient text-white rounded-circle shadow d-flex justify-content-center align-items-center`,
iconSize: [size, size],
iconAnchor: [size/2, size/2],
});
}
return L.marker(coords, options);
}
function createGeoJSONMarker(marker_elem, options) {
let geojson = JSON.parse(marker_elem.dataset.geojson);
if (options.color) {
options.color = get_tabler_color(options.color) || options.color;
}
function style({ properties }) {
if (typeof properties !== "object") return options;
return {...options, ...properties};
}
function pointToLayer(feature, latlng) {
marker_elem.dataset.coords = latlng.lat + "," + latlng.lng;
return createMarker(marker_elem, { ...options, ...feature.properties });
}
return L.geoJSON(geojson, { style, pointToLayer });
}
}
function sqlpage_form() {
const file_inputs = document.querySelectorAll("input[type=file][data-max-size]");
for (const input of file_inputs) {
const max_size = +input.dataset.maxSize;
input.addEventListener("change", function() {
input.classList.remove("is-invalid");
input.setCustomValidity("");
for (const {size} of this.files) {
if (size > max_size){
input.classList.add("is-invalid");
return input.setCustomValidity(`File size must be less than ${max_size/1000} kB.`);
}
}
});
}
}
function create_tabler_color() {
const style = getComputedStyle(document.documentElement);
return function get_tabler_color(name) {
return style.getPropertyValue('--tblr-' + name);
}
}
const get_tabler_color = create_tabler_color();
function load_scripts() {
let addjs = document.querySelectorAll("[data-sqlpage-js]");
for (const js of new Set([...addjs].map(({dataset}) => dataset.sqlpageJs))) {
const script = document.createElement("script");
script.src = js;
document.head.appendChild(script);
}
}
function add_init_fn(f) {
document.addEventListener('DOMContentLoaded', f);
document.addEventListener('fragment-loaded', f);
if (document.readyState !== "loading") setTimeout(f, 0);
}
add_init_fn(sqlpage_table);
add_init_fn(sqlpage_map);
add_init_fn(sqlpage_embed);
add_init_fn(sqlpage_form);
add_init_fn(load_scripts);