@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
49 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceHandleBars = exports.HandleBarReplacements = void 0;
exports.HandleBarReplacements = {
Pages: ['{{PageId}}'],
Lists: ['{{ListTitle}}', '{{ListId}}', '{{ListUrl}}'],
Users: ['{{UserName}}', '{{UserLogin}}', '{{UserEmail}}'],
Webs: ['{{WebTitle}}', '{{WebUrl}}', '{{SiteId}}'],
Sites: ['{{SiteTitle}}', '{{SiteUrl}}', '{{SiteId}}'],
};
function replaceHandleBars(str, context) {
if (!str) {
return '';
}
else {
if (str.indexOf('{{') === -1 || str.indexOf('}}') === -1) {
return str;
}
else {
var newStr = str.replace(/{{PageId}}/gi, "".concat(context.pageContext.listItem ? context.pageContext.listItem.id.toFixed() : '{{PageId}}'));
if (str.indexOf('{{List') > -1) {
newStr = newStr.replace(/{{ListTitle}}/gi, "".concat(context.pageContext.list ? context.pageContext.list.title : '{{ListTitle}}'));
newStr = newStr.replace(/{{ListId}}/gi, "".concat(context.pageContext.list ? context.pageContext.list.id : '{{ListId}}'));
newStr = newStr.replace(/{{ListUrl}}/gi, "".concat(context.pageContext.list ? context.pageContext.list.serverRelativeUrl : '{{ListUrl}}'));
}
if (str.indexOf('{{User') > -1) {
newStr = newStr.replace(/{{UserName}}/gi, "".concat(context.pageContext.user.displayName));
newStr = newStr.replace(/{{UserLogin}}/gi, "".concat(context.pageContext.user.loginName));
newStr = newStr.replace(/{{UserEmail}}/gi, "".concat(context.pageContext.user.email));
}
if (str.indexOf('{{Web') > -1) {
newStr = newStr.replace(/{{WebTitle}}/gi, "".concat(context.pageContext.web.title));
newStr = newStr.replace(/{{WebUrl}}/gi, "".concat(context.pageContext.web.serverRelativeUrl));
newStr = newStr.replace(/{{WebId}}/gi, "".concat(context.pageContext.web.id));
}
if (str.indexOf('{{Site') > -1) {
newStr = newStr.replace(/{{SiteTitle}}/gi, "".concat(context.pageContext.web.title));
newStr = newStr.replace(/{{SiteUrl}}/gi, "".concat(context.pageContext.web.serverRelativeUrl));
newStr = newStr.replace(/{{SiteId}}/gi, "".concat(context.pageContext.web.id));
}
var now = new Date();
newStr = newStr.replace(/{{Now}}/gi, "".concat(now.toLocaleString()));
newStr = newStr.replace(/{{Today}}/gi, "".concat(now.toLocaleDateString()));
return newStr;
}
}
}
exports.replaceHandleBars = replaceHandleBars;
//# sourceMappingURL=handleBars.js.map