mui-spfx-controls
Version:
SPFx component library built with MUI
90 lines • 4.15 kB
JavaScript
import { __awaiter, __generator } from "tslib";
import { getSp } from '../config/pnp.config';
var SiteService = /** @class */ (function () {
/**
* Initializes the SiteService instance.
* @param {WebPartContext} context - The SharePoint WebPart context.
*/
function SiteService(context) {
this.sp = getSp(context);
}
/**
* Recursively generates breadcrumb data from the current site up to the root site collection.
* @param {IWeb} web - The current site web instance.
* @param {string} rootUrl - The root site absolute URL.
* @param {ILinkItem[]} breadcrumbs - The collected breadcrumb items.
* @returns {Promise<ILinkItem[]>} A promise resolving to the breadcrumb items.
*/
SiteService.prototype.generateData = function (web_1, rootUrl_1) {
return __awaiter(this, arguments, void 0, function (web, rootUrl, breadcrumbs) {
var webInfo, parentWeb, _a, _b;
if (breadcrumbs === void 0) { breadcrumbs = []; }
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_c.trys.push([0, 6, , 7]);
return [4 /*yield*/, web.select('Title', 'ServerRelativeUrl', 'Id')()];
case 1:
webInfo = _c.sent();
if (!(webInfo === null || webInfo === void 0 ? void 0 : webInfo.Title) || !(webInfo === null || webInfo === void 0 ? void 0 : webInfo.ServerRelativeUrl)) {
return [2 /*return*/, breadcrumbs];
}
// Add current site to the breadcrumb list
breadcrumbs.unshift({
key: webInfo.Id,
label: webInfo.Title,
href: webInfo.ServerRelativeUrl,
});
// Stop recursion when reaching the root site
if (webInfo.ServerRelativeUrl === new URL(rootUrl).pathname) {
return [2 /*return*/, breadcrumbs];
}
_c.label = 2;
case 2:
_c.trys.push([2, 4, , 5]);
return [4 /*yield*/, web.getParentWeb()];
case 3:
parentWeb = _c.sent();
return [2 /*return*/, this.generateData(parentWeb, rootUrl, breadcrumbs)];
case 4:
_a = _c.sent();
return [2 /*return*/, breadcrumbs];
case 5: return [3 /*break*/, 7];
case 6:
_b = _c.sent();
return [2 /*return*/, breadcrumbs];
case 7: return [2 /*return*/];
}
});
});
};
/**
* Retrieves breadcrumb navigation data from the current site up to the root site collection.
* @returns {Promise<ILinkItem[]>} A promise resolving to an array of breadcrumb link items.
*/
SiteService.prototype.getBreadcrumbData = function () {
return __awaiter(this, void 0, void 0, function () {
var rootSite, data, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 3, , 4]);
return [4 /*yield*/, this.sp.site.select('Url')()];
case 1:
rootSite = _b.sent();
return [4 /*yield*/, this.generateData(this.sp.web, rootSite.Url, [])];
case 2:
data = _b.sent();
return [2 /*return*/, data];
case 3:
_a = _b.sent();
return [2 /*return*/, []];
case 4: return [2 /*return*/];
}
});
});
};
return SiteService;
}());
export { SiteService };
//# sourceMappingURL=SiteService.js.map