UNPKG

@mikezimm/fps-library-v2

Version:

Library of reusable typescript/javascript functions, interfaces and constants

181 lines (180 loc) 10.3 kB
/** * CodeAnalizerComment: Updated 2 imports on 2024-09-22 14:49:52 * Update:: import { OriginalHTMLConversions } to '@mikezimm/fps-core-v7/lib/logic/Strings/htmlV2;' * Update:: import { IStateSource } to '@mikezimm/fps-core-v7/lib/components/molecules/state-source/IStateSource;' */ // 2023-11-24: POTENTIAL BREAKING CHANGE: when changing to: logic/Strings/htmlV2 - does not find image links import { replaceHTMLEntitiesv2, OriginalHTMLConversions } from '@mikezimm/fps-core-v7/lib/logic/Strings/htmlV2'; import { checkDeepProperty } from '@mikezimm/fps-core-v7/lib/logic/Objects/deep'; import { sortObjectArrayByStringKey } from '@mikezimm/fps-core-v7/lib/logic/Arrays/sorting/objects'; import { getSourceItemsAPI } from "@mikezimm/fps-core-v7/lib/restAPIs/lists/items/getSourceItemsAPI"; import { CurrentTenant } from '@mikezimm/fps-core-v7/lib/components/molecules/source-props/WindowLocationConstants'; import { EmptyFPSItemSearch } from '@mikezimm/fps-core-v7/lib/components/molecules/AnyContent/IFPSItemSearch'; import { makeid } from '@mikezimm/fps-core-v7/lib/logic/Strings/guids'; import { getExpandColumns } from '@mikezimm/fps-core-v7/lib/components/molecules/source-props/Lists/getVX/getExpandV2'; //Standards are really site pages, supporting docs are files /** * 2024-12-15: Updated to fetch from fps-core-v7 * * @param fetchInfo * @returns */ export async function getRelatedItems(fetchInfo) { // let errMess = ''; // if ( !fetchInfo.web ) { errMess += 'Web url, '; } // if ( !fetchInfo.listTitle ) { errMess += 'ListTitle, '; } // if ( !fetchInfo.displayProp ) { errMess += 'LabelColumn, '; } // if ( errMess ) { // errMess += ' are Required! Tip: Click yellow button for prop pane help :)'; // return { items: [], filtered: [], error: errMess , fetchInfo: fetchInfo }; // } // // debugger; // let web = await Web( `${window.location.origin}${fetchInfo.web}` ); // if ( fetchInfo.canvasImgs === true || fetchInfo.canvasLinks === true ) { // fetchInfo.linkProp = 'CanvasContent1'; // fetchInfo.displayProp = ''; // } // let baseSelectColumns = ['ID']; // if ( fetchInfo.displayProp ) baseSelectColumns.push( fetchInfo.displayProp ); // if ( fetchInfo.linkProp ) baseSelectColumns.push( fetchInfo.linkProp ); // if ( fetchInfo.itemsAreFiles ) baseSelectColumns.push( 'ServerRedirectedEmbedUrl' ); // let expColumns = getExpandColumns( baseSelectColumns ); // let selColumns = getSelectColumns( baseSelectColumns ); // let expandThese = expColumns.length > 1 ? expColumns.join(",") : expColumns[0]; // if ( !expandThese ) expandThese = ''; //Added this for cases where there are no expanded columns and therefore expColumns is undefined. //Do not get * columns when using standards so you don't pull WikiFields //itemFetchCol //let selectThese = '*,WikiField,FileRef,FileLeafRef,' + selColumns.join(","); if (fetchInfo.canvasImgs === true || fetchInfo.canvasLinks === true) { fetchInfo.linkProp = 'CanvasContent1'; fetchInfo.displayProp = ''; } let baseSelectColumns = ['ID', 'Title']; if (fetchInfo.displayProp) baseSelectColumns.push(fetchInfo.displayProp); if (fetchInfo.linkProp) baseSelectColumns.push(fetchInfo.linkProp); if (fetchInfo.itemsAreFiles) baseSelectColumns.push('ServerRedirectedEmbedUrl'); // 2024-12-22: digestValue not required when using SpHTTPClient // const digestValue = await getThisFPSDigestValueFromUrl(fetchInfo.web); const sourceProps = { //These are specific to ISourceProps fpsSpService: fetchInfo.fpsSpService, key: 'related', refreshId: makeid(5), defType: 'related', fpsContentType: ['item'], webRelativeLink: '', searchSource: 'Related', searchSourceDesc: 'Related Items', tenant: CurrentTenant, webUrl: fetchInfo.web, listTitle: fetchInfo.listTitle, digestValue: '', itemFetchCol: [], columns: [...baseSelectColumns], searchProps: [...baseSelectColumns], selectThese: [...baseSelectColumns], expandThese: getExpandColumns(baseSelectColumns), restFilter: fetchInfo.restFilter, fetchCount: 500, orderBy: { prop: fetchInfo.displayProp, order: 'asc', asc: true, }, performanceSettings: { label: 'RelatedItems', updateMiliseconds: true, includeMsStr: true, op: 'fetch11' }, defSearchButtons: [], }; if (fetchInfo.itemsAreFiles) sourceProps.fpsContentType.push('file'); const stateSource = await getSourceItemsAPI({ ...sourceProps, ...{ editMode: null } }, false, false, true); // eslint-disable-next-line @typescript-eslint/no-unused-vars, prefer-const let { items, errorInfo, } = stateSource; if (errorInfo && errorInfo.returnMess.indexOf(`"List 'Site Pages' does not exist`) > 1) alert(`I'm sorry, this site does NOT have a library Titled 'Site Pages :(`); // let filtered: IAnyContent[] = []; // console.log('getRelatedItems: fetchInfo', fetchInfo ); // try { // items = await web.lists.getByTitle( fetchInfo.listTitle ).items // .select(selectTheseString).filter(fetchInfo.restFilter).expand(expandThese).getAll(); // } catch (e) { // errMess = getHelpfullErrorV2( e, true, true, 'getRelatedItems ~ 60'); // console.log('getRelatedItems: fetchInfo', fetchInfo ); // } items.map((item) => { // https://github.com/mikezimm/PageInfo/issues/144 // 2024-09-08: Added Search and File: undefined per latest IFPSItem interface if (!item.FPSItem) item.FPSItem = { IsA: { allIsAKeys: [], allIsAKeysStr: '' }, Search: EmptyFPSItemSearch, File: undefined }; item.FPSItem.Related = { linkUrl: '', linkAlt: '', linkText: '', images: [], links: [], //Added this for possible expanding alt-click on item to go to Embed link instead of actual link }; if ((fetchInfo.canvasImgs === true || fetchInfo.canvasLinks === true) && item.CanvasContent1) { // 2023-11-24: POTENTIAL BREAKING CHANGE: when changing to: logic/Strings/htmlV2 - does not find image links item.CanvasContent1 = replaceHTMLEntitiesv2(item.CanvasContent1, OriginalHTMLConversions); if (fetchInfo.canvasImgs === true) { let sourceStrings = item.CanvasContent1.split('"imageSources":'); if (sourceStrings.length > 1) { sourceStrings.map((source, idx) => { if (idx > 0) { //Always skip index 0 because it is the string before the first tag. let sourceString = source.substring(0, source.indexOf('}') + 1); let sources = JSON.parse(sourceString); Object.keys(sources).map(key => { let url = decodeURI(sources[key]); //Address https://github.com/mikezimm/PageInfo/issues/79 if (fetchInfo.ignoreDefaultImages !== true) { //Always push if this setting is false item.FPSItem.Related.images.push({ url: url, embed: 'gotoLink' }); //Else it's true and we need to only push if it does not contain this string. } else { let pushMe = true; if (url.toLowerCase().indexOf('/_LAYOUTS/IMAGES/REPORTSITEDEFAULT'.toLowerCase()) > -1) { pushMe = false; } else if (url.toLowerCase().indexOf('https://go.microsoft.com/fwlink/?link'.toLowerCase()) > -1) { pushMe = false; } if (pushMe === true) { item.FPSItem.Related.images.push({ url: url, embed: 'gotoLink' }); } } }); } }); } } if (fetchInfo.canvasLinks === true) { let sourceStrings = item.CanvasContent1.split('<a '); if (sourceStrings.length > 1) { sourceStrings.map((source, idx) => { if (idx > 0) { //Always skip index 0 because it is the string before the first tag. let sourceString = source.substring(source.indexOf(' href="') + 7); sourceString = sourceString.substring(0, sourceString.indexOf('"')); item.FPSItem.Related.links.push({ url: sourceString, embed: 'gotoLink' }); } }); } } } else { item.FPSItem.Related.linkUrl = checkDeepProperty(item, fetchInfo.linkProp.split('/'), 'ShortError'); item.FPSItem.Related.linkText = checkDeepProperty(item, fetchInfo.displayProp.split('/'), 'ShortError'); item.FPSItem.Related.linkText = item.FPSItem.Related.linkText ? decodeURI(item.FPSItem.Related.linkText) : item.FPSItem.Related.linkText; if (item.FPSItem.Related.linkText === '...Title = undef') { item.FPSItem.Related.linkText = 'No Title - Possibly DRAFT'; } item.CanvasContent1 = ''; item.FPSItem.Related.linkAlt = item.ServerRedirectedEmbedUrl ? item.ServerRedirectedEmbedUrl : ''; } }); items = !fetchInfo.displayProp ? items : sortObjectArrayByStringKey(items, 'asc', 'linkText'); console.log('getRelatedItems: fetchInfo, items', fetchInfo, items); return stateSource; } //# sourceMappingURL=GetItems.js.map