UNPKG

sp-js-provisioning

Version:
280 lines 15.5 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import { ClientsideText, ClientsideWebpart, CreateClientsidePage } from '@pnp/sp/presets/all'; import { replaceUrlTokens } from '../util'; import { TokenHelper } from '../util/tokenhelper'; import { HandlerBase } from './handlerbase'; import '@pnp/sp/presets/all'; import '@pnp/sp/comments/clientside-page'; /** * Describes the Composed Look Object Handler */ var ClientSidePages = /** @class */ (function (_super) { __extends(ClientSidePages, _super); /** * Creates a new instance of the ObjectClientSidePages class */ function ClientSidePages(config) { return _super.call(this, 'ClientSidePages', config) || this; } /** * Provisioning Client Side Pages * * @param web - The web * @param clientSidePages - The client side pages to provision * @param context - Provisioning context */ ClientSidePages.prototype.ProvisionObjects = function (web, clientSidePages, context) { return __awaiter(this, void 0, void 0, function () { var partDefinitions_1, error_1; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: this.tokenHelper = new TokenHelper(context, this.config); _super.prototype.scope_started.call(this); _a.label = 1; case 1: _a.trys.push([1, 4, , 5]); return [4 /*yield*/, web.getClientsideWebParts()]; case 2: partDefinitions_1 = _a.sent(); return [4 /*yield*/, clientSidePages.reduce(function (chain, clientSidePage) { return chain.then(function () { return _this._processClientSidePage(web, clientSidePage, partDefinitions_1); }); }, Promise.resolve())]; case 3: _a.sent(); return [3 /*break*/, 5]; case 4: error_1 = _a.sent(); _super.prototype.scope_ended.call(this, error_1); throw error_1; case 5: return [2 /*return*/]; } }); }); }; ClientSidePages.prototype._getClientSideWebPart = function (partDefinitions, control) { if (control.Id === 'Text' || control.Id === 'PageText') { _super.prototype.log_info.call(this, 'getClientSideWebPart', "Control ID is " + control.Id + ". Creating a ClientsideText part with text " + control.Text); var textPart = new ClientsideText(control.Text); return { part: textPart, partDef: { Name: 'PageText' } }; } var partDef = partDefinitions.find(function (c) { return c.Id.toLowerCase().includes(control.Id.toLowerCase()); }); if (!partDef) { _super.prototype.log_warn.call(this, 'getClientSideWebPart', "Client side web part with definition id " + control.Id + " not found."); return { part: null, partDef: null }; } var properties = this.tokenHelper.replaceTokens(JSON.stringify(control.Properties)); properties = replaceUrlTokens(properties, this.config); var part = ClientsideWebpart.fromComponentDef(partDef).setProperties(JSON.parse(properties)); if (control.ServerProcessedContent) { var serverProcessedContent = this.tokenHelper.replaceTokens(JSON.stringify(control.ServerProcessedContent)); part.setServerProcessedContent(JSON.parse(serverProcessedContent)); } return { part: part, partDef: partDef }; }; /** * Provision a client side page * * @param web - The web * @param clientSidePage - Cient side page * @param partDefinitions - Cient side web parts */ ClientSidePages.prototype._processClientSidePage = function (web, clientSidePage, partDefinitions) { return __awaiter(this, void 0, void 0, function () { var ServerRelativeUrl, page, _a, verticalSection, _b, _c, control, _d, part, partDef, sections, sections_1, sections_1_1, s, section, _e, _f, col, column, _g, _h, control, _j, part, partDef; var e_1, _k, e_2, _l, e_3, _m, e_4, _o; return __generator(this, function (_p) { switch (_p.label) { case 0: _super.prototype.log_info.call(this, 'processClientSidePage', "Processing client side page " + clientSidePage.Name); return [4 /*yield*/, web.select('ServerRelativeUrl')()]; case 1: ServerRelativeUrl = (_p.sent()).ServerRelativeUrl; page = null; _p.label = 2; case 2: _p.trys.push([2, 6, , 7]); return [4 /*yield*/, web.loadClientsidePage(ServerRelativeUrl + "/SitePages/" + clientSidePage.Name)]; case 3: page = _p.sent(); if (!(clientSidePage.Overwrite && page)) return [3 /*break*/, 5]; _super.prototype.log_info.call(this, 'processClientSidePage', "Overwrite option is enabled. Deleting client side page " + clientSidePage.Name); return [4 /*yield*/, page.delete()]; case 4: _p.sent(); _p.label = 5; case 5: return [3 /*break*/, 7]; case 6: _a = _p.sent(); return [3 /*break*/, 7]; case 7: if (!clientSidePage.Overwrite && page) { _super.prototype.log_info.call(this, 'processClientSidePage', "Client side page " + clientSidePage.Name + " already exists and overwrite option is disabled. Skipping"); return [2 /*return*/]; } return [4 /*yield*/, CreateClientsidePage(web, clientSidePage.Name, clientSidePage.Title, clientSidePage.PageLayoutType)]; case 8: page = _p.sent(); if (clientSidePage.VerticalSection) { verticalSection = page.addVerticalSection(); try { for (_b = __values(clientSidePage.VerticalSection), _c = _b.next(); !_c.done; _c = _b.next()) { control = _c.value; _d = this._getClientSideWebPart(partDefinitions, control), part = _d.part, partDef = _d.partDef; if (!part) continue; try { verticalSection.addControl(part); } catch (_q) { _super.prototype.log_info.call(this, 'processClientSidePage', "Failed adding part " + partDef.Name + " to client side page " + clientSidePage.Name); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_k = _b.return)) _k.call(_b); } finally { if (e_1) throw e_1.error; } } } sections = clientSidePage.Sections || []; try { for (sections_1 = __values(sections), sections_1_1 = sections_1.next(); !sections_1_1.done; sections_1_1 = sections_1.next()) { s = sections_1_1.value; section = page.addSection(); try { for (_e = (e_3 = void 0, __values(s.Columns)), _f = _e.next(); !_f.done; _f = _e.next()) { col = _f.value; column = section.addColumn(col.Factor); try { for (_g = (e_4 = void 0, __values(col.Controls)), _h = _g.next(); !_h.done; _h = _g.next()) { control = _h.value; _j = this._getClientSideWebPart(partDefinitions, control), part = _j.part, partDef = _j.partDef; if (!part) continue; try { column.addControl(part); } catch (_r) { _super.prototype.log_info.call(this, 'processClientSidePage', "Failed adding part " + partDef.Name + " to client side page " + clientSidePage.Name); } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_h && !_h.done && (_o = _g.return)) _o.call(_g); } finally { if (e_4) throw e_4.error; } } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_f && !_f.done && (_m = _e.return)) _m.call(_e); } finally { if (e_3) throw e_3.error; } } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (sections_1_1 && !sections_1_1.done && (_l = sections_1.return)) _l.call(sections_1); } finally { if (e_2) throw e_2.error; } } _super.prototype.log_info.call(this, 'processClientSidePage', "Saving client side page " + clientSidePage.Name); page.commentsDisabled = clientSidePage.CommentsDisabled; return [4 /*yield*/, page.save()]; case 9: _p.sent(); return [4 /*yield*/, (clientSidePage.CommentsDisabled ? page.disableComments() : page.enableComments())]; case 10: _p.sent(); return [2 /*return*/]; } }); }); }; return ClientSidePages; }(HandlerBase)); export { ClientSidePages }; //# sourceMappingURL=clientsidepages.js.map