@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
229 lines • 13.5 kB
JavaScript
// import { EasyPagesCCSPages, EasyPagesSysPages, EasyPagesSysTab, ISourcePropsEP } from '../../../../banner/components/EasyPages/interfaces/epTypes';
import { DefaultThumbCCS, DefaultThumbEasyContents, DefaultThumbExtreme, DefaultThumbEarth, DefaultSiteLogo } from '../SystemIcons/DefaultEasyIcons';
import { FileTypeZipName, FileTypesZip } from '../interfaces/eiTypes';
import { EasyIconLocation } from "../interfaces/EasyIconLocation";
// import { IMinStandardIconObject } from '../interfaces/IMinStandardIconObject';
import { getEasySystemIcon } from '../SystemIcons/getEasySystemIcon';
import { EasySystemLibraryIcons } from '../SystemIcons/EasySystemLibraryIcons';
// import { AllEasySystemIcons } from '../SystemIcons/AllSystemEasyIcons';
import { EasyCCSPageIcons } from '../SystemIcons/EasyCCSPageIcons';
import { EasySystemListIcons } from '../SystemIcons/EasySystemListIcons';
// import { check4This } from '@mikezimm/fps-pnp2/lib/services/sp/CheckSearch';
import { updateFPSEasyIconSummary } from './windowEasyIcons';
import { check4This } from '../../../../logic/Links/CheckSearch';
import { EasyPagesCCSPages, EasyPagesSysPages, EasyPagesSysTab } from '../../easy-pages/interfaces/epTypes';
export const commonDefaultIcons = [DefaultSiteLogo];
/**
* Logic order:
* First checks keywords in the first Prop to test ( Title )
* Then checks for all the Icons in Title
* Then repeats for the next Prop - Description
* @param EasyIcons
* @param item
* @param fallbackIcon? Fallback Icon if one is not found
* @returns
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getEasyIcon(EasyIcons, item, updateFPSEasyIcons, fallbackIcon) {
//If this is disabled, then exit
if (EasyIcons.Enabled !== true)
return fallbackIcon;
const FoundCommonProps = [];
// https://github.com/fps-solutions/HubCon/issues/77
commonDefaultIcons.map((thumb) => {
['PictureThumbnailURL', 'imageUrl'].map(prop => {
// 2024-09-06: Had to create itemX in getEasyIcon.ts to pass linting
const itemX = item[prop];
if (itemX && itemX.indexOf(thumb) > -1) {
// The current thumbnail is a default one.
// Place in temp storage and then put it back later it does not find a better one.
fallbackIcon = `${item[prop]}`;
item[prop] = ``;
FoundCommonProps.push(prop);
}
});
});
// https://github.com/mikezimm/pivottiles7/issues/281
const EasyIgnoresLC = EasyIcons.Ignore ? EasyIcons.Ignore.map(ignor => { return ignor.toLowerCase(); }) : [];
const EasyErrors = [];
let EasyIconUrl = '';
EasyIcons.Priority.map(prop => {
// 2024-09-06: Had to create itemX in getEasyIcon.ts to pass linting
const itemX = item[prop];
if (itemX) {
EasyIcons.GroupKeys.map(Key => {
var _a;
if (Key === 'SharePoint' || Key === 'FileTypes') {
// Skip this key because it should be gotten previously via getEasySystemIcon and FileTypes is done below
}
else if (EasyIcons.Valid.indexOf(Key) < 0) {
if (EasyErrors.indexOf(Key) < 0) {
EasyErrors.push(Key);
}
}
else if (!EasyIconUrl && EasyIcons.Groups[Key].Status === 'Active') {
(_a = EasyIcons.Groups[Key]) === null || _a === void 0 ? void 0 : _a.Icons.map((Icon) => {
if (!EasyIconUrl && Icon) { //Only continue if EasyIconUrl is not found and Icon is a non-empty string
if (EasyIgnoresLC.indexOf(Icon.toLowerCase()) < 0) { // https://github.com/mikezimm/pivottiles7/issues/281
//Combine all the options into regex as optional qualifiers
const KeyOptions = EasyIcons.Groups[Key]; // Added this to remove type error in line below where its' possibly undefined
const Options = KeyOptions.Options ? `(${KeyOptions === null || KeyOptions === void 0 ? void 0 : KeyOptions.Options.filter(x => x !== 'exactSpacing').join(')?(')})?` : '';
// eslint-disable-next-line @rushstack/security/no-unsafe-regexp
const IconRegex = new RegExp(`(\\b)${Icon}${Options}(\\b)`, 'i');
const ThisIconUrl = `${EasyIconLocation}${Key}/${Icon}.png`;
if (itemX.match(IconRegex)) {
EasyIconUrl = ThisIconUrl;
}
else if (!Options || Options.indexOf('exactSpacing') < 0) {
// Added this for https://github.com/mikezimm/pivottiles7/issues/378 to check sample 'Power Apps' AND 'PowerApps' without the spaces
// Removes all spaces in IconName and to compare to testString - should find 'PowerApps' in item if the Icon is 'Power Apps'
const IconRegexNoSpace = new RegExp(`(\\b)${Icon.replace(/[-\s]/gim, '')}(\\b)`, 'i');
// Replaces spaces with hyphens in IconName and to compares to testString - should find 'Power-Apps' in item if the Icon is 'Power Apps'
const IconRegexHyphen = new RegExp(`(\\b)${Icon.replace(/\s/gim, '-')}(\\b)`, 'i');
if (itemX.match(IconRegexNoSpace)) {
EasyIconUrl = ThisIconUrl;
}
else if (itemX.match(IconRegexHyphen)) {
EasyIconUrl = ThisIconUrl;
}
else {
// Not sure what would happen here for now....
}
}
// Update FPSEasyIcons Usage
if (EasyIconUrl && updateFPSEasyIcons === true) {
updateFPSEasyIconSummary(Key, Icon, itemX);
}
}
}
});
}
});
}
});
// https://github.com/mikezimm/pivottiles7/issues/314
const ItemFileName = !item.File ? '' : item.File.Name ? item.File.Name : item.File.ServerRelativeUrl ? item.File.ServerRelativeUrl.substring(item.File.ServerRelativeUrl.lastIndexOf('/') + 1) : ``;
// https://github.com/mikezimm/fps-library-v2/issues/70
if (EasyIcons.GroupKeys.indexOf('FileTypes') > -1 && !EasyIconUrl && item.File) {
// https://github.com/mikezimm/pivottiles7/issues/275
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let isAnArchive = false;
FileTypesZip.map(zip => {
if (isAnArchive === false && ItemFileName.toLowerCase().indexOf(zip) > 0)
isAnArchive = true;
});
if (isAnArchive === true) {
EasyIconUrl = `${EasyIconLocation}${`FileTypes`}/${FileTypeZipName}.png`;
}
else {
// Eventually add other file types... NOTE this is more complex than other types of icons.
}
}
// https://github.com/fps-solutions/HubCon/issues/77
// Added this to set prop back if it was not updated
FoundCommonProps.map(prop => {
if (item[prop] === '') {
item[prop] = fallbackIcon;
}
});
return EasyIconUrl ? EasyIconUrl : fallbackIcon;
}
export function getStandardEasyIcon(item) {
}
export function addEasyIcons(items, sourceProps, EasyIcons, updateFPSEasyIcons) {
let FPSEasyIcons = updateFPSEasyIcons;
if ((FPSEasyIcons === null || FPSEasyIcons === undefined) && check4This('Mode=Edit') === true)
FPSEasyIcons = true;
items.map(item => {
var _a, _b, _c, _d, _e;
let { searchTextLC, } = item.FPSItem.Search;
item.tabs = [];
item.title = item.Title;
item.description = item.Description;
item.url = (_a = item.File) === null || _a === void 0 ? void 0 : _a.ServerRelativeUrl;
item.imageUrl = (_b = item.BannerImageUrl) === null || _b === void 0 ? void 0 : _b.Url;
item.imageDesc = (_c = item.BannerImageUrl) === null || _c === void 0 ? void 0 : _c.Description;
// https://github.com/mikezimm/fps-library-v2/issues/59: Gracefully support if imageUrl is OOTB
// https://github.com/fps-solutions/HubCon/issues/77 - Keeping this here which is also dupped in getEasyIcon but want to avoid potential unknown issues
let fallbackIcon = ``;
commonDefaultIcons.map((thumb) => {
if (item.imageUrl && item.imageUrl.indexOf(thumb) > -1) {
// The current thumbnail is a default one.
// Place in temp storage and then put it back later it does not find a better one.
fallbackIcon = `${item.imageUrl}`;
item.imageUrl = ``;
}
});
// https://github.com/mikezimm/drilldown7/issues/280
EasyPagesCCSPages.map(ccs => {
var _a;
if (((_a = item.url) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase().indexOf(`/${ccs.toLocaleLowerCase()}.aspx`)) > -1) {
item.imageUrl = DefaultThumbCCS;
}
});
if (!item.imageUrl || item.imageUrl.indexOf(DefaultSiteLogo) > -1) {
if (((_d = item.title) === null || _d === void 0 ? void 0 : _d.indexOf('Contents')) > -1) {
item.imageUrl = DefaultThumbEasyContents;
}
else if (((_e = item.title) === null || _e === void 0 ? void 0 : _e.toLocaleLowerCase().indexOf('extreme')) > -1) {
item.imageUrl = DefaultThumbExtreme;
}
else if (item.title === 'Home') {
item.imageUrl = DefaultThumbEarth;
}
else {
// Added here for https://github.com/mikezimm/pivottiles7/issues/262; https://github.com/mikezimm/fps-library-v2/issues/60
// 2023-07-06: First do Page level icons, do Lists and Libraries after looking for other icons
const EasySystemIcon1 = getEasySystemIcon(EasyCCSPageIcons, item);
if (EasySystemIcon1) {
item.imageUrl = EasySystemIcon1;
item.imageDesc = `Using EasySystemIcon:) ${EasySystemIcon1.replace(EasyIconLocation, '')}`;
}
else {
const EasyIconUrl = getEasyIcon(EasyIcons, item, FPSEasyIcons, fallbackIcon);
if (EasyIconUrl)
item.imageUrl = EasyIconUrl ? EasyIconUrl : item.imageUrl; // If one is found, then use it, else use the defaul sitepagelogo
if (EasyIconUrl)
item.imageDesc = EasyIconUrl ? `Using EasyIcon:) ${EasyIconUrl.replace(EasyIconLocation, '')}` : item.imageDesc; // If one is found, then use it, else use the defaul sitepagelogo
}
}
}
// 2023-07-06: First do Page level icons, do Lists and Libraries after looking for other icons
if (!item.imageUrl) { // Look on List/Library name level
const EasySystemIcon2 = getEasySystemIcon([...EasySystemLibraryIcons, ...EasySystemListIcons], item);
if (EasySystemIcon2) {
item.imageUrl = EasySystemIcon2;
item.imageDesc = `Using EasySystemIcon:) ${EasySystemIcon2.replace(EasyIconLocation, '')}`;
}
}
// 2023-02-16: Added ternary here for cases where searchTextLC is already set
searchTextLC = searchTextLC ? searchTextLC : `${item.Title} || ${item.Description}`.toLocaleLowerCase();
// https://github.com/mikezimm/drilldown7/issues/280
EasyPagesSysPages.map(sysPage => {
var _a;
if (((_a = item.url) === null || _a === void 0 ? void 0 : _a.toLocaleLowerCase().indexOf(`/${sysPage.toLocaleLowerCase()}.aspx`)) > -1) {
item.tabs.push(EasyPagesSysTab);
}
});
//Only add to user tabs if it's NOT a known System page
if (sourceProps.meta1 && item.tabs.indexOf(EasyPagesSysTab) < 0) {
sourceProps.meta1.map((tab) => {
if (item.FPSItem.Search.searchTextLC.indexOf(tab.toLocaleLowerCase()) > -1)
item.tabs.push(tab);
});
}
});
items.map(item => {
if (item.tabs.length === 0) {
//2023-016: Added check for if there isn't an EasyPages overflow tab on the sourceProps
if (sourceProps.EasyPageOverflowTab) {
item.tabs.push(sourceProps.EasyPageOverflowTab);
}
else if (sourceProps.OverflowTab) {
item.tabs.push(sourceProps.OverflowTab);
}
}
});
return items;
}
//# sourceMappingURL=getEasyIcon.js.map