@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
135 lines • 7.73 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.importProps = exports.updateFpsImportProps = exports.FPSImportPropsGroup = void 0;
var sp_property_pane_1 = require("@microsoft/sp-property-pane");
var ErrorHandler_1 = require("../Logging/ErrorHandler");
var checks_1 = require("../Arrays/checks");
var ExportFunctions_1 = require("./ExportFunctions");
var zReusablePropPane_1 = require("./zReusablePropPane");
exports.FPSImportPropsGroup = {
groupName: 'Import Props',
isCollapsed: true,
groupFields: [
(0, sp_property_pane_1.PropertyPaneTextField)('fpsImportProps', {
label: "Import settings from another FPS PageInfo Web part",
description: 'For complex settings, use the link below to edit as JSON Object',
multiline: true,
}),
zReusablePropPane_1.JSON_Edit_Link,
]
};
function updateFpsImportProps(thisProps, importBlockProps, propertyPath, newValue, refreshPane, restartPane, exitPropPaneChanged) {
var importErrorMessage = '';
if (propertyPath === 'fpsImportProps') {
if (exitPropPaneChanged === true) { //Added to prevent re-running this function on import. Just want re-render. )
exitPropPaneChanged = false; //Added to prevent re-running this function on import. Just want re-render.
}
else {
var result = importProps(thisProps, newValue, [], importBlockProps);
importErrorMessage = result.errMessage;
if (result.importError === false) {
thisProps.fpsImportProps = '';
refreshPane();
}
exitPropPaneChanged = true; //Added to prevent re-running this function on import. Just want re-render.
restartPane();
// this.render();
}
}
return importErrorMessage;
}
exports.updateFpsImportProps = updateFpsImportProps;
/**
* @param thisProperties - this.properties of the webpart (to update and verify)
* @param fpsImportProps - string from property pane with import properties
* @param validImports - white list specific properties
* @param blockImports - explicitly block these props (to avoid over-writing ones we don't want changed like scenario )
*/
function importProps(thisProperties, fpsImportProps, validImports, blockImports) {
var errMessage = '';
var propsObject = {};
var importError = false;
var updateCount = 0;
var blockCount = 0;
var unChangedCount = 0;
var notAcceptCount = 0;
var blocked = [];
var updated = [];
var unchanged = [];
var notAccepted = [];
if (fpsImportProps && fpsImportProps.length > 0) {
try {
propsObject = JSON.parse(fpsImportProps);
}
catch (e) {
var errMessageImport = (0, ErrorHandler_1.getHelpfullErrorV2)(e, false, true, 'importProps ~20');
console.log('Unable to parse import Props:', fpsImportProps);
errMessage += errMessage.length > 0 ? ' -- ' : '';
errMessage += 'Error parsing import Props. Check JSON. ' + errMessageImport;
importError = true;
}
if (importError === false) {
Object.keys(propsObject).map(function (propGroup) {
//added indexOfAnyCase check for if group is part of blocked... https://github.com/mikezimm/SecureScript7/issues/77
if (propGroup !== 'wpInstanceID' && (0, checks_1.indexOfAnyCase)(propGroup, blockImports, true, false) < 0) {
//Added this entire if for cases when propGroup is not agroup but a single prop: https://github.com/mikezimm/SecureScript7/issues/77
if (typeof propsObject[propGroup] !== 'object') {
//This is not an object, only test this level.
if (thisProperties[propGroup] !== propsObject[propGroup]) {
updateCount++;
thisProperties[propGroup] = propsObject[propGroup];
updated.push("".concat(propGroup, " - ").concat(propsObject[propGroup]));
}
else {
unChangedCount++;
unchanged.push("".concat(propGroup, " - ").concat(propsObject[propGroup]));
}
}
else {
Object.keys(propsObject[propGroup]).map(function (thisProp) {
// Check for explicitly blocked properties
if (propsObject[propGroup][thisProp] !== ExportFunctions_1.exportNotAvailMess &&
(blockImports.length === 0 || (0, checks_1.indexOfAnyCase)(thisProp, blockImports, true, false) < 0)) {
// Check for whitelist of valid props
if (validImports.length === 0 || (0, checks_1.indexOfAnyCase)(thisProp, validImports, true, false) >= 0) {
if (thisProperties[thisProp] !== propsObject[propGroup][thisProp]) {
updateCount++;
thisProperties[thisProp] = propsObject[propGroup][thisProp];
updated.push("".concat(thisProp, " - ").concat(propsObject[propGroup][thisProp]));
}
else {
unChangedCount++;
unchanged.push("".concat(thisProp, " - ").concat(propsObject[propGroup][thisProp]));
}
}
else {
console.log('importProps - invalid prop', thisProp, propsObject[propGroup][thisProp]);
notAcceptCount++;
notAccepted.push("".concat(thisProp, " - ").concat(propsObject[propGroup][thisProp]));
}
}
else {
console.log('importProps - blocked prop', thisProp, propsObject[propGroup][thisProp]);
blockCount++;
blocked.push("".concat(thisProp, " - ").concat(propsObject[propGroup][thisProp]));
}
});
}
}
else {
//Added this to count blocked props https://github.com/mikezimm/SecureScript7/issues/77
blockCount++;
blocked.push("".concat(propGroup, " - ").concat(propsObject[propGroup]));
}
});
console.log('FPS Import Props: updated', updated);
console.log('FPS Import Props: unchanged', unchanged);
console.log('FPS Import Props: blocked', blocked);
console.log('FPS Import Props: notAccepted', notAccepted);
alert("We just imported ".concat(updateCount, " settings, ").concat(unChangedCount, " where unchanged, ").concat(blockCount, " where blocked and ").concat(notAcceptCount, " where not valid"));
}
}
return { thisProps: thisProperties, importError: importError, errMessage: errMessage, updateCount: updateCount, blockCount: blockCount, notAcceptCount: notAcceptCount, unChanged: unChangedCount };
}
exports.importProps = importProps;
//# sourceMappingURL=ImportFunctions.js.map
;