@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
52 lines • 2.29 kB
JavaScript
// window.location properties
// host: "tenant.sharepoint.com"
// hostname: "tenant.sharepoint.com"
// href: "https://tenant.sharepoint.com/sites/WebPartDev/SitePages/ECStorage.aspx?debug=true&noredir=true&debugManifestsFile=https%3A%2F%2Flocalhost%3A4321%2Ftemp%2Fmanifests.js&allowOtherSites=true&scenario=dev"
// origin: "https://tenant.sharepoint.com"
// pathname: "/sites/WebPartDev/SitePages/ECStorage.aspx"
// protocol: "https:"
// search: "?debug=true&noredir=true&debugManifestsFile=https%3A%2F%2Flocalhost%3A4321%2Ftemp%2Fmanifests.js&allowOtherSites=true&scenario=dev"
import { check4This, Check4 } from "../Links/CheckSearch";
// This is a duplicate of the value in WindowLocationConstants but eliminates circular reference
const CurrentOriginX = `${window.location.origin}`;
const CurrentPathnameX = `${window.location.pathname}`;
/**
* NOTE: WORKS ONLY ON CURRENT TENANT
* returns this:
* https://tenant.sharepoint.com/sites/SiteCollection
*
* Use makeAbsoluteUrl if you need the full Url after the collection
*
* @param link
* @returns
*/
export function getSiteCollectionUrlFromLink(link) {
if (!link || link.length === 0) {
link = `${CurrentPathnameX}`;
}
else if (link.indexOf('http') === 0) {
link = link.replace(CurrentOriginX, '');
}
//At this point, link should be relative url /sites/collection....
const parts = link.split('/');
// 2024/12/06: updated logic to work for both /sites/ and /teams/
const collectionUrl = `${CurrentOriginX}/${parts[1]}/${parts[2]}`;
return collectionUrl;
}
/**
* returns this:
* https://tenant.sharepoint.com/sites/SiteCollection/Subsite/SitePages/Page.aspx
*
* NOTE This only works when the /[ sites | teams ]/ you are refering to is in the same tenant
* @param relUrl
*/
export function makeAbsoluteUrl(useUrl) {
let newUrl = `${useUrl ? useUrl : ''}`;
if (newUrl && newUrl.indexOf('/sites/') === 0 || newUrl.indexOf('/teams/') === 0)
newUrl = `${CurrentOriginX}${newUrl}`;
if (check4This(Check4.prepSource_Eq_true) === true) {
console.log('updated Url to: ', newUrl);
}
return newUrl;
} // protocol: "https:"
//# sourceMappingURL=getSiteCollectionUrlFromLink.js.map