UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

122 lines 4.45 kB
/** * 2024-09-07: Migrated from the same folder in fps-library-v2/banner/features/WebPartHistory/... */ import { check4This } from '../../../logic/Links/CheckSearch'; export function createWebpartHistory(prop, newValue, user) { let now = new Date(); let timeString = now.toUTCString(); // fields: prop === 'onInit' ? [] : [ prop ], // newValues: prop === 'onInit' ? [] : [ newValue ], let change = { prop: prop, value: newValue, }; let history = { time: timeString, user: user, changes: prop === 'onInit' ? [] : [change], }; return history; } export function updateCurrentHistorySaved(allHistory, thisInstance) { let maxHistoryLength = 20; let history = allHistory.history; if (!history || history.length === 0) { history = [thisInstance]; } else { if (history[0].time !== thisInstance.time || history[0].user !== thisInstance.user) { history.unshift(thisInstance); } else { history[0] = thisInstance; } } //Trim history to only last 20 saves if (history.length > maxHistoryLength) { history.length = maxHistoryLength; } allHistory.history = history; return allHistory; } export function upgradeV1History(allHistory) { //rebuild history if needed from IWebpartHistoryItem1 if (allHistory && allHistory.history.length > 0 && Object.keys(allHistory.history[0]).indexOf('newValues') > -1) { let newHistory = []; allHistory.history.map((instance) => { let changes = []; let oldProps = instance.fields; let oldValues = instance.newValues; oldProps.map((field, idx) => { changes.push({ prop: field, value: oldValues[idx] }); }); newHistory.push({ time: instance.time, user: instance.user, changes: changes, }); }); allHistory.history = newHistory; } return allHistory; } /** * Added 2022-07-25 from FPSPageInfo to simplify re-usability. * It just has logic to get the trimThis based on the passed in array of strings * @param webpartHistory * @param prop * @param newValue * @param user * @param noTrimProps * @param startTrimProps * @param trimLength * @returns */ export function updateWebpartHistoryV2(webpartHistory, prop, newValue, user, noTrimProps, startTrimProps, trimLength = 20) { //ADDED FOR WEBPART HISTORY: This sets the webpartHistory let trimThis = 'end'; if (noTrimProps.indexOf(prop) > -1) { trimThis = 'none'; } else if (startTrimProps.indexOf(prop) > -1) { trimThis = 'start'; } webpartHistory = updateWebpartHistory(webpartHistory, prop, newValue, user, trimThis, trimLength); return webpartHistory; } export function updateWebpartHistory(webpartHistory, prop, newValue, user, trimThis = 'start', trimLength = 20) { let thisInstance = webpartHistory.thisInstance; if (!thisInstance) { thisInstance = createWebpartHistory(prop, newValue, user); } let propIdx = -1; thisInstance.changes.map((thisChange, idx) => { if (thisChange.prop === prop) { propIdx = idx; } }); let strValue = typeof newValue === 'string' ? newValue + '' : newValue.toString(); let origLength = strValue.length + 0; //Need this check ( -5 ) to make sure we don't trim it unneccessarily if (origLength > trimLength + 5) { if (trimThis === 'start') { strValue = strValue.substring(0, trimLength); strValue += ` ...[+${origLength - strValue.length}]`; } else if (trimThis === 'end') { strValue = strValue.substring(origLength - trimLength); strValue = `[+${origLength - strValue.length}]...${strValue}`; } } if (propIdx < 0) { thisInstance.changes.push({ prop: prop, value: strValue }); } else { thisInstance.changes[propIdx].value = strValue; } webpartHistory = updateCurrentHistorySaved(webpartHistory, thisInstance); if (check4This('showHistory=true') === true) console.log('webpartHistory: function', webpartHistory); return webpartHistory; } //# sourceMappingURL=Functions.js.map