UNPKG

@mikezimm/fps-library-v2

Version:

Library of reusable typescript/javascript functions, interfaces and constants

177 lines (175 loc) 10.5 kB
/** * CodeAnalizerComment: Updated 2 imports on 2024-09-22 14:49:52 * Update:: import { IPropertyPaneGroup } to '@mikezimm/fps-core-v7/lib/types/@msft/1.15.2/sp-property-pane;' * Update:: import { exportNotAvailMess } to '@mikezimm/fps-core-v7/lib/banner/features/ImportExport/ExportFunctions;' */ /** * CodeAnalizerComment: Updated 4 imports on 2024-09-21 23:07:24 * Update:: import { getHelpfullErrorV2 } to '@mikezimm/fps-core-v7/lib/logic/Errors/friendly;' * Update:: import { indexOfAnyCase } to '@mikezimm/fps-core-v7/lib/logic/Arrays/searching/objectfind;' * Update:: import { exportNotAvailMess } to '@mikezimm/fps-core-v7/lib/banner/features/ImportExport/ExportFunctions;' * Update:: import { IMinBannerUtilityProps } to '@mikezimm/fps-core-v7/lib/banner/interfaces/MinWP/IMinBannerUtilityProps;' */ import { PropertyPaneTextField, } from '@microsoft/sp-property-pane'; import { getHelpfullErrorV2 } from '@mikezimm/fps-core-v7/lib/logic/Errors/friendly'; import { indexOfAnyCase } from '@mikezimm/fps-core-v7/lib/logic/Arrays/searching/objectfind'; import { exportNotAvailMess } from '@mikezimm/fps-core-v7/lib/banner/features/ImportExport/ExportFunctions'; import { JSON_Edit_Link } from '../../../common/PropPaneHelp/atoms/JSONEdit'; // import { JSON_Edit_Link } from './zReusablePropPane_'; const SpecialImportObjectIndexes = ['', 0, 1, 2, 3, 4]; export const SpecialObjectImports = [ ...SpecialImportObjectIndexes.map(i => { return `QFButtons${i}`; }), ...SpecialImportObjectIndexes.map(i => { return `MultiLists${i}`; }), ...SpecialImportObjectIndexes.map(i => { return `SingleLists${i}`; }), ]; export const FPSImportPropsGroup = { groupName: 'Import Props', isCollapsed: true, groupFields: [ 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, }), JSON_Edit_Link, ] }; export function updateFpsImportProps(thisProps, importBlockProps, propertyPath, newValue, refreshPane, restartPane, exitPropPaneChanged) { let 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 { let 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; } /** * @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 ) */ export function importProps(thisProperties, fpsImportProps, validImports, blockImports) { let errMessage = ''; let propsObject = {}; let importError = false; let updateCount = 0; let blockCount = 0; let unChangedCount = 0; let notAcceptCount = 0; let blocked = []; let updated = []; let unchanged = []; let notAccepted = []; if (fpsImportProps && fpsImportProps.length > 0) { try { propsObject = JSON.parse(fpsImportProps); // https://github.com/mikezimm/drilldown7/issues/404 const defLabel = `See Prop Pane Help`; if (propsObject.viewType === 'React-List') { propsObject.viewHB1 = defLabel; propsObject.viewHB2 = defLabel; propsObject.viewHB3 = defLabel; } else if (propsObject.Layout && propsObject.Layout.viewType === 'React-List') { if (propsObject.HandleBars) { propsObject.HandleBars.viewHB1 = defLabel; propsObject.HandleBars.viewHB2 = defLabel; propsObject.HandleBars.viewHB3 = defLabel; } } } catch (e) { let errMessageImport = 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(propGroup => { //added indexOfAnyCase check for if group is part of blocked... https://github.com/mikezimm/SecureScript7/issues/77 if (propGroup !== 'wpInstanceID' && 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(`${propGroup} - ${propsObject[propGroup]}`); } else { unChangedCount++; unchanged.push(`${propGroup} - ${propsObject[propGroup]}`); } } else { Object.keys(propsObject[propGroup]).map(thisProp => { // Check for explicitly blocked properties if (propsObject[propGroup][thisProp] !== exportNotAvailMess && (blockImports.length === 0 || indexOfAnyCase(thisProp, blockImports, true, false) < 0)) { // Check for whitelist of valid props if (validImports.length === 0 || indexOfAnyCase(thisProp, validImports, true, false) >= 0) { // Added for https://github.com/mikezimm/drilldown7/issues/429 because it was not checking deep props if (SpecialObjectImports.indexOf(thisProp) > -1) { delete thisProperties[thisProp]; thisProperties[thisProp] = propsObject[propGroup][thisProp]; updateCount++; updated.push(`${thisProp} - ${propsObject[propGroup][thisProp].length} items`); } else if (thisProperties[thisProp] !== propsObject[propGroup][thisProp]) { updateCount++; thisProperties[thisProp] = propsObject[propGroup][thisProp]; updated.push(`${thisProp} - ${propsObject[propGroup][thisProp]}`); // Added for https://github.com/mikezimm/drilldown7/issues/429 because it was not checking deep props // } else if ( JSON.stringify(thisProperties[thisProp]) !== JSON.stringify( propsObject[propGroup][thisProp])) { // updateCount++; // thisProperties[thisProp] = propsObject[propGroup][thisProp]; // updated.push(`${thisProp} - ${propsObject[propGroup][thisProp]}`); } else { unChangedCount++; unchanged.push(`${thisProp} - ${propsObject[propGroup][thisProp]}`); } } else { console.log('importProps - invalid prop', thisProp, propsObject[propGroup][thisProp]); notAcceptCount++; notAccepted.push(`${thisProp} - ${propsObject[propGroup][thisProp]}`); } } else { console.log('importProps - blocked prop', thisProp, propsObject[propGroup][thisProp]); blockCount++; blocked.push(`${thisProp} - ${propsObject[propGroup][thisProp]}`); } }); } } else { //Added this to count blocked props https://github.com/mikezimm/SecureScript7/issues/77 blockCount++; blocked.push(`${propGroup} - ${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 ${updateCount} settings, ${unChangedCount} where unchanged, ${blockCount} where blocked and ${notAcceptCount} where not valid`); } } return { thisProps: thisProperties, importError: importError, errMessage: errMessage, updateCount: updateCount, blockCount: blockCount, notAcceptCount: notAcceptCount, unChanged: unChangedCount }; } //# sourceMappingURL=ImportFunctions.js.map