-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: loadLeaflet() helper function #3
Comments
Could base it on https://github.com/simonw/datasette-leaflet-geojson/blob/0.7/datasette_leaflet_geojson/static/datasette-leaflet-geojson.js#L2-L33 const loadDependencies = (callback) => {
let loaded = [];
function hasLoaded() {
loaded.push(this);
if (loaded.length == 2) {
callback();
}
}
let stylesheet = document.createElement("link");
stylesheet.setAttribute("type", "text/css");
stylesheet.setAttribute("rel", "stylesheet");
stylesheet.setAttribute(
"href",
"https://unpkg.com/[email protected]/dist/leaflet.css"
);
stylesheet.setAttribute(
"integrity",
"sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
);
stylesheet.setAttribute("crossorigin", "anonymous");
stylesheet.onload = hasLoaded;
document.head.appendChild(stylesheet);
let script = document.createElement("script");
script.src = "https://unpkg.com/[email protected]/dist/leaflet.js";
script.setAttribute(
"integrity",
"sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
);
script.setAttribute("crossorigin", "anonymous");
script.onload = stylesheet.onload = hasLoaded;
document.head.appendChild(script);
}; |
Maybe something like this: function loadLeaflet(callback) {
let loaded = [];
let callbackResult = null;
function hasLoaded(result) {
if (result) {
callbackResult = result;
}
loaded.push(this);
if (loaded.length == 2) {
callback(callbackResult);
}
}
let stylesheet = document.createElement("link");
stylesheet.setAttribute("rel", "stylesheet");
stylesheet.setAttribute("href", "{{ datasette_leaflet_url }}");
stylesheet.onload = hasLoaded;
document.head.appendChild(stylesheet);
import('{{ datasette_leaflet_url }}').then(hasLoaded);
} |
It minifies to 305 bytes: https://skalman.github.io/UglifyJS-online/
|
If I add that script using the extra_body_script() I'll need to depend on Datasette 0.54 which adds module support. |
A helper function that loads the CSS, loads the JavaScript and then calls your callback function. It could work like this:
The text was updated successfully, but these errors were encountered: