getAbsoluteUri.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Uri from "../ThirdParty/Uri.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defined from "./defined.js";
  4. import DeveloperError from "./DeveloperError.js";
  5. /**
  6. * Given a relative Uri and a base Uri, returns the absolute Uri of the relative Uri.
  7. * @function
  8. *
  9. * @param {String} relative The relative Uri.
  10. * @param {String} [base] The base Uri.
  11. * @returns {String} The absolute Uri of the given relative Uri.
  12. *
  13. * @example
  14. * //absolute Uri will be "https://test.com/awesome.png";
  15. * const absoluteUri = Cesium.getAbsoluteUri('awesome.png', 'https://test.com');
  16. */
  17. function getAbsoluteUri(relative, base) {
  18. let documentObject;
  19. if (typeof document !== "undefined") {
  20. documentObject = document;
  21. }
  22. return getAbsoluteUri._implementation(relative, base, documentObject);
  23. }
  24. getAbsoluteUri._implementation = function (relative, base, documentObject) {
  25. //>>includeStart('debug', pragmas.debug);
  26. if (!defined(relative)) {
  27. throw new DeveloperError("relative uri is required.");
  28. }
  29. //>>includeEnd('debug');
  30. if (!defined(base)) {
  31. if (typeof documentObject === "undefined") {
  32. return relative;
  33. }
  34. base = defaultValue(documentObject.baseURI, documentObject.location.href);
  35. }
  36. const relativeUri = new Uri(relative);
  37. if (relativeUri.scheme() !== "") {
  38. return relativeUri.toString();
  39. }
  40. return relativeUri.absoluteTo(base).toString();
  41. };
  42. export default getAbsoluteUri;