css.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
  2. * Apache-2.0 */
  3. import { getCdnCssUrl, parseVersion } from './url';
  4. function createStylesheetLink(href) {
  5. var link = document.createElement('link');
  6. link.rel = 'stylesheet';
  7. link.href = href;
  8. return link;
  9. }
  10. function insertLink(link, before) {
  11. if (before) {
  12. // the link should be inserted before a specific node
  13. var beforeNode = document.querySelector(before);
  14. beforeNode.parentNode.insertBefore(link, beforeNode);
  15. }
  16. else {
  17. // append the link to then end of the head tag
  18. document.head.appendChild(link);
  19. }
  20. }
  21. // check if the css url has been injected or added manually
  22. function getCss(url) {
  23. return document.querySelector("link[href*=\"" + url + "\"]");
  24. }
  25. function getCssUrl(urlOrVersion) {
  26. return !urlOrVersion || parseVersion(urlOrVersion)
  27. // if it's a valid version string return the CDN URL
  28. ? getCdnCssUrl(urlOrVersion)
  29. // otherwise assume it's a URL and return that
  30. : urlOrVersion;
  31. }
  32. // lazy load the CSS needed for the ArcGIS API
  33. export function loadCss(urlOrVersion, before) {
  34. var url = getCssUrl(urlOrVersion);
  35. var link = getCss(url);
  36. if (!link) {
  37. // create & load the css link
  38. link = createStylesheetLink(url);
  39. insertLink(link, before);
  40. }
  41. return link;
  42. }