@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
190 lines • 8.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSharePointOnline = isSharePointOnline;
exports.isSharePointOnlineSync = isSharePointOnlineSync;
exports.isSPPageContextInfoReady = isSPPageContextInfoReady;
exports.isSPPageContextInfoReadySync = isSPPageContextInfoReadySync;
exports.GetPageAsJson = GetPageAsJson;
exports.GetPageAsJsonSync = GetPageAsJsonSync;
const common_logger_1 = require("../../common-logger");
const browser_1 = require("../../helpers/browser");
const collections_base_1 = require("../../helpers/collections.base");
const sharepoint_1 = require("../../helpers/sharepoint");
const typecheckers_1 = require("../../helpers/typecheckers");
const url_1 = require("../../helpers/url");
const sharepoint_types_1 = require("../../types/sharepoint.types");
const localstoragecache_1 = require("../localstoragecache");
const rest_1 = require("../rest");
const rest_vars_1 = require("../rest.vars");
const list_1 = require("./list");
const user_1 = require("./user");
const web_1 = require("./web");
const logger = new common_logger_1.CommonLogger("utils/sharepoint/context");
async function isSharePointOnline() {
if (isSPOCommonUrl() === true
|| isSPOCachedURL() === true
|| (!(0, typecheckers_1.isTypeofFullNameNullOrUndefined)("_spPageContextInfo") && _spPageContextInfo.isSPO === true)) {
return true;
}
// Can be an expensive call
await isSPPageContextInfoReady();
return getAndCacheIsSharePointOnlineResult();
}
function isSharePointOnlineSync() {
if (isSPOCommonUrl() === true
|| isSPOCachedURL() === true
|| (!(0, typecheckers_1.isTypeofFullNameNullOrUndefined)("_spPageContextInfo") && _spPageContextInfo.isSPO === true)) {
return true;
}
// Can be an expensive call
isSPPageContextInfoReadySync();
return getAndCacheIsSharePointOnlineResult();
}
async function isSPPageContextInfoReady() {
const contextReady = await (0, browser_1.waitForWindowObject)("_spPageContextInfo", null, 1000);
if (contextReady !== true) {
let pageAsJson = await GetPageAsJson();
if (!(0, typecheckers_1.isNullOrUndefined)(pageAsJson) && !(0, typecheckers_1.isNullOrUndefined)(pageAsJson.spPageContextInfo)) {
globalThis["_spPageContextInfo"] = pageAsJson.spPageContextInfo;
}
}
if (!(0, typecheckers_1.isTypeofFullNameNullOrUndefined)("_spPageContextInfo")) {
expandPageContext();
return true;
}
return false;
}
function isSPPageContextInfoReadySync() {
const contextReady = !(0, typecheckers_1.isTypeofFullNameNullOrUndefined)("_spPageContextInfo");
if (contextReady !== true) {
let pageAsJson = GetPageAsJsonSync();
if (!(0, typecheckers_1.isNullOrUndefined)(pageAsJson) && !(0, typecheckers_1.isNullOrUndefined)(pageAsJson.spPageContextInfo)) {
globalThis["_spPageContextInfo"] = pageAsJson.spPageContextInfo;
}
}
if (!(0, typecheckers_1.isTypeofFullNameNullOrUndefined)("_spPageContextInfo")) {
expandPageContext();
return true;
}
return false;
}
function _getPageAsJsonRequestParams(url) {
if ((0, typecheckers_1.isNullOrEmptyString)(url)) {
url = window.location.pathname;
}
return [
`${url}?as=json`,
null,
{
...rest_vars_1.longLocalCache,
headers: {
"Content-Type": "application/json;charset=utf-8",
"ACCEPT": "application/json; odata.metadata=minimal",
"ODATA-VERSION": "4.0"
}
}
];
}
async function GetPageAsJson(url) {
try {
let response = await (0, rest_1.GetJson)(..._getPageAsJsonRequestParams(url));
return response;
}
catch {
}
return null;
}
function GetPageAsJsonSync(url) {
try {
let response = (0, rest_1.GetJsonSync)(..._getPageAsJsonRequestParams(url));
return response.success === true ? response.result : null;
}
catch {
}
return null;
}
function isSPOCommonUrl() {
let url = new URL(window.location.href);
//Most cases are satisfied by this check. Very few customers have custom domains for SharePoint online.
if (url.host.toLowerCase().endsWith(".sharepoint.com")) {
return true;
}
return false;
}
function isSPOCachedURL() {
let isSPO = (0, localstoragecache_1.getCacheItem)(`${window.location.host}_isSPO`);
return isSPO === true;
}
function getAndCacheIsSharePointOnlineResult() {
let isSPO = !(0, typecheckers_1.isTypeofFullNameUndefined)("_spPageContextInfo") && _spPageContextInfo.isSPO === true;
(0, localstoragecache_1.setCacheItem)(`${window.location.host}_isSPO`, isSPO === true, { days: 365 });
return isSPO === true;
}
function expandPageContext() {
if (!(0, typecheckers_1.isTypeofFullNameNullOrUndefined)("_spPageContextInfo")
&& _spPageContextInfo["_hasExpandPageContext"] === true) {
return;
}
logger.groupSync("expandPageContext", log => {
const ctx = _spPageContextInfo;
ctx["_hasExpandPageContext"] = true;
if ((0, typecheckers_1.isNullOrUndefined)(ctx.siteId)) {
log("GetSiteIdSync");
ctx.siteId = (0, web_1.GetSiteIdSync)(ctx.siteServerRelativeUrl);
}
if ((0, typecheckers_1.isNullOrEmptyString)(ctx.webId)) {
log("GetWebIdSync");
ctx.webId = (0, web_1.GetWebIdSync)(ctx.webServerRelativeUrl);
}
if ((0, typecheckers_1.isNullOrUndefined)(ctx.hasManageWebPermissions) && !(0, typecheckers_1.isNullOrUndefined)(ctx.webPermMasks)) {
log("hasManageWebPermissions");
let webPerms = new sharepoint_1.SPBasePermissions(_spPageContextInfo.webPermMasks);
ctx.hasManageWebPermissions = webPerms.has(sharepoint_types_1.SPBasePermissionKind.ManageWeb);
}
if ((0, typecheckers_1.isNullOrEmptyString)(ctx.listId)) {
log("ctx.listId");
ctx.listId = ctx.pageListId;
}
if ((0, typecheckers_1.isNotEmptyString)(ctx.listId)) {
//has list
if ((0, typecheckers_1.isNullOrEmptyString)(ctx.listUrl)) {
log("GetListRootFolderSync");
ctx.listUrl = (0, list_1.GetListRootFolderSync)(ctx.webServerRelativeUrl, ctx.listId).ServerRelativeUrl;
}
if ((0, typecheckers_1.isNullOrNaN)(ctx.listBaseTemplate)) {
log("GetListSync");
const list = (0, list_1.GetListSync)(ctx.webServerRelativeUrl, ctx.listId);
ctx.listTitle = list.Title;
ctx.listBaseTemplate = list.BaseTemplate;
//ctx.listBaseType = list.BaseType;
ctx.listPermsMask = list.EffectiveBasePermissions;
}
if ((0, typecheckers_1.isNullOrUndefined)(ctx.pageItemId)) {
log("ctx.pageItemId");
let idParam = (0, url_1.getQueryStringParameter)("ID");
if ((0, typecheckers_1.isNotEmptyString)(idParam) && (0, typecheckers_1.isNumeric)(idParam)) {
ctx.pageItemId = Number(idParam);
}
}
if ((0, typecheckers_1.isNullOrNaN)(ctx.pageItemId)) {
//no item, in a view
if ((0, typecheckers_1.isNullOrUndefined)(ctx.viewId)) {
log("GetListViewsSync");
const viewEndsWith = window.location.pathname.substr(window.location.pathname.lastIndexOf("/")).toLowerCase();
const views = (0, list_1.GetListViewsSync)(ctx.webServerRelativeUrl, ctx.listId);
const view = (0, collections_base_1.firstOrNull)(views, v => v.ServerRelativeUrl.toLowerCase().endsWith(viewEndsWith));
if (view)
ctx.viewId = view.Id;
}
}
if ((0, typecheckers_1.isNullOrEmptyString)(ctx.userEmail)) {
log("GetCurrentUserSync");
const user = (0, user_1.GetCurrentUserSync)(ctx.webServerRelativeUrl);
ctx.userEmail = user.Email;
ctx.userDisplayName = user.Title;
}
log({ label: "expanded", value: ctx });
}
});
}
//# sourceMappingURL=context.js.map