@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
137 lines • 6.05 kB
JavaScript
/**
* 2024-09-03: Migrated from fps-library-v2\src\common\PropPaneHelp\preconfig
*/
// import { WebPartContextCopy_15_2 } from "../../interfaces/indexes";
import { getDeepPropUsingDotNotation } from "../../../logic/Objects/deep";
import { check4This, Check4 } from "../../../logic/Links/CheckSearch";
/**
* 2025-03-08: Updated applyPreConfiguredProps/applyPresetCollectionDefaults to activate the _class: [] options for setting class based variables based upon the webpart and site
* https://github.com/mikezimm/pivottiles7/issues/393
*
* import { getThisSitesPreConfigProps } from "./getThisSitesPreConfigProps";
* run first: sitePresets = getThisSitesPreConfigProps( PreConfiguredProps, thisProps, context.pageContext.web.serverRelativeUrl );
*
* @param sitePresets = getThisSitesPreConfigProps( PreConfiguredProps, thisProps, context.pageContext.web.serverRelativeUrl );
* @param PreConfiguredProps
* @param thisProps
* @param thisWPClass
* @returns
*/
export function applyPreConfiguredProps(sitePresets, thisProps, thisWPClass) {
const { context } = thisWPClass;
sitePresets.presets.map(setting => {
const { prop, value } = setting;
let valueX = setActualValue(value, context);
if (thisProps[prop] === valueX) {
setting.status = 'valid';
}
else if (thisProps[prop] === undefined || thisProps[prop] === null) { //Changed from just !this... because if value was 'false' it would set to true
thisProps[prop] = valueX;
setting.status = 'preset';
}
});
sitePresets.forces.map(setting => {
const { prop, value } = setting;
let valueX = setActualValue(value, context);
if (thisProps.overrideKeys && thisProps.overrideKeys.indexOf(prop) > -1) {
setting.status = 'override';
}
else if (thisProps[prop] === valueX) {
setting.status = 'valid';
}
else if (!thisProps[prop]) {
thisProps[prop] = valueX;
setting.status = 'preset';
}
else if (thisProps[prop] !== valueX) {
thisProps[prop] = valueX;
setting.status = 'changed';
}
});
if (check4This(Check4.showPresets_Eq_true))
console.log('showPresets=true Preset props:', sitePresets);
return sitePresets;
}
/**
* import { getThisSitesPreConfigProps } from "./getThisSitesPreConfigProps";
* run first: sitePresets = getThisSitesPreConfigProps( PreConfiguredProps, thisProps, context.pageContext.web.serverRelativeUrl );
*
* @param PreConfiguredProps
* @param thisProps
* @param thisWPClass
* @returns
*/
export function applyPreConfiguredClass(sitePresets, thisWPClass) {
const { context } = thisWPClass;
// const sitePresets: ISitePreConfigProps = getThisSitesPreConfigProps( PreConfiguredProps, thisProps, context.pageContext.web.serverRelativeUrl );
sitePresets._class.map(setting => {
const { prop, value } = setting;
let valueX = setActualValue(value, context);
if (thisWPClass[prop] === valueX) {
setting.status = 'valid';
}
else if (thisWPClass[prop] === undefined || thisWPClass[prop] === null) { //Changed from just !this... because if value was 'false' it would set to true
thisWPClass[prop] = valueX;
setting.status = 'preset';
// https://github.com/mikezimm/fps-library-v2/issues/224 - added || valueX === false
}
else if (valueX || valueX === false) {
if (JSON.stringify(thisWPClass[prop]) === JSON.stringify(valueX)) {
setting.status = 'valid';
}
else {
thisWPClass[prop] = valueX;
// Set to changed here
setting.status = 'changed';
if (check4This(Check4.showPresets_Eq_true))
console.log('_class_Defaults: Else', setting, valueX);
}
}
});
sitePresets._webpart.map(setting => {
const { prop, value } = setting;
let valueX = setActualValue(value, context);
if (thisWPClass[prop] === valueX) {
setting.status = 'valid';
}
else if (thisWPClass[prop] === undefined || thisWPClass[prop] === null) { //Changed from just !this... because if value was 'false' it would set to true
thisWPClass[prop] = valueX;
setting.status = 'preset';
}
else if (valueX) {
if (JSON.stringify(thisWPClass[prop]) === JSON.stringify(valueX)) {
setting.status = 'valid';
}
else {
thisWPClass[prop] = valueX;
// Set to override on the _webpart level if it was previously set on the _class level
setting.status = setting.status === 'changed' ? 'override' : 'changed';
if (check4This(Check4.showPresets_Eq_true))
console.log('_webpart_Defaults: Else', setting, valueX);
}
}
});
return sitePresets;
}
/**
* This just gets the deep prop from context if it's used in the value
* @param value
* @param context
* @returns
*/
function setActualValue(value, context) {
if (typeof value === 'string' && value.indexOf('{{context') > -1) {
/**
* replace the context. from the beginning of the value because the that is the object we are passing in to check
* Explanation:
/^\{\{context\./:
^: Asserts the position at the start of the string.
\{\{: Matches the literal characters {{.
context\.: Matches the literal characters context..
\: Escapes the special characters { and ..
*/
value = getDeepPropUsingDotNotation(context, value.replace(/^\{\{context\./, '{{'), 'Actual');
}
return value;
}
//# sourceMappingURL=ApplyPresets.js.map