Skip to content
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

Open
simonw opened this issue Jan 24, 2021 · 4 comments
Open

Idea: loadLeaflet() helper function #3

simonw opened this issue Jan 24, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Owner

simonw commented Jan 24, 2021

A helper function that loads the CSS, loads the JavaScript and then calls your callback function. It could work like this:

loadLeaflet(leaflet => {
  /* Do stuff with leaflet */
]
@simonw simonw added the enhancement New feature or request label Jan 24, 2021
@simonw
Copy link
Owner Author

simonw commented Jan 24, 2021

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);
  };

@simonw
Copy link
Owner Author

simonw commented Jan 24, 2021

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);
}

@simonw
Copy link
Owner Author

simonw commented Jan 24, 2021

It minifies to 305 bytes: https://skalman.github.io/UglifyJS-online/

function loadLeaflet(e){let t=[],l=null;function n(n){n&&(l=n),t.push(this),2==t.length&&e(l)}let a=document.createElement("link");a.setAttribute("rel","stylesheet"),a.setAttribute("href","{{ datasette_leaflet_url }}"),a.onload=n,document.head.appendChild(a),import("{{ datasette_leaflet_url }}").then(n)}

@simonw
Copy link
Owner Author

simonw commented Jan 24, 2021

If I add that script using the extra_body_script() I'll need to depend on Datasette 0.54 which adds module support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant