@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
331 lines • 19.7 kB
JavaScript
"use strict";
/**
* RelatedItems originally copied from
* https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-page-navigator
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var all_1 = require("@pnp/sp/presets/all");
var Icon_1 = require("office-ui-fabric-react/lib/Icon");
var GetItems_1 = require("./GetItems");
var filtering_1 = require("../Services/Strings/filtering");
require('./RelatedItems.css');
var RelatedItems = /** @class */ (function (_super) {
__extends(RelatedItems, _super);
function RelatedItems(props) {
var _this = _super.call(this, props) || this;
_this.regExpOrigin = new RegExp("".concat(window.location.origin), 'gim');
_this.regExpWeb = new RegExp("".concat(_this.props.fetchInfo.web), 'gim');
var fetched = false;
var items = [];
if (_this.props.items && _this.props.items.length > 0) {
items = _this.props.items;
fetched = true;
}
_this.state = {
items: items,
errMess: '',
textFilter: _this.props.fetchInfo.textFilter ? _this.props.fetchInfo.textFilter : '',
linkFilter: _this.props.linkFilter ? _this.props.linkFilter : '',
fetched: fetched,
filteredItems: items,
canvasImagesExpanded: false,
canvasLinksExpanded: false,
};
return _this;
// this.onLinkClick = this.onLinkClick.bind(this);
}
RelatedItems.prototype.componentDidMount = function () {
this.getRelatedItems(this.props.fetchInfo.textFilter, this.props.linkFilter);
};
RelatedItems.prototype.componentDidUpdate = function (prevProps) {
// if (JSON.stringify(prevProps.items) !== JSON.stringify(this.props.items)) {
// this.setState({ items: this.props.items, selectedKey: this.props.items[0] ? this.props.items[0].key : '' });
// } else if (prevProps.showItems !== this.props.showItems) { //Force component update in case it was not previously rendered
if (prevProps.showItems !== this.props.showItems) { //Force component update in case it was not previously rendered
this.getRelatedItems(this.props.fetchInfo.textFilter, this.props.linkFilter);
}
else if (prevProps.isExpanded !== this.props.isExpanded) { //Force component update in case it was not previously rendered
this.getRelatedItems(this.props.fetchInfo.textFilter, this.props.linkFilter);
}
else if (prevProps.linkFilter !== this.props.linkFilter) { //Force component update in case it was not previously rendered
this.getRelatedItems(this.props.fetchInfo.textFilter, this.props.linkFilter);
}
};
RelatedItems.prototype.getRelatedItems = function (textFilter, linkFilter) {
return __awaiter(this, void 0, void 0, function () {
var canvasImagesExpanded, canvasLinksExpanded, results, fetched, filteredItems_1;
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
canvasImagesExpanded = linkFilter ? true : this.state.canvasImagesExpanded;
canvasLinksExpanded = linkFilter ? true : this.state.canvasLinksExpanded;
if (!(this.props.showItems === true && this.props.isExpanded === true && this.state.fetched !== true)) return [3 /*break*/, 2];
return [4 /*yield*/, (0, GetItems_1.getRelatedItems)(this.props.fetchInfo, null)];
case 1:
results = _a.sent();
fetched = results.error ? false : true;
filteredItems_1 = results.items;
if (this.state.textFilter && results.items && results.items.length > 0) {
filteredItems_1 = [];
results.items.map(function (item) {
var pushItem = true;
if (_this.props && _this.props.fetchInfo && _this.props.fetchInfo.textFilterProps && _this.props.fetchInfo.textFilterProps.length > 0) {
//Search all props ( must be string values or it will error )
_this.props.fetchInfo.textFilterProps.map(function (key) {
//If key does not exist or the key.lowerCase !== textFilter.lowerCase, do not push item.
var itemKey = typeof item[key] === 'string' ? item[key] : ''; //Added to resolve typing error
if (item[key] === null || item[key] === undefined || itemKey.toLowerCase().indexOf(_this.state.textFilter.toLowerCase()) < 0) {
pushItem = false;
}
});
}
if (pushItem === true) {
filteredItems_1.push(item);
}
});
}
this.setState({ items: results.items, filteredItems: filteredItems_1, errMess: results.error, fetched: fetched,
textFilter: textFilter ? textFilter : '', linkFilter: linkFilter ? linkFilter : '',
canvasImagesExpanded: canvasImagesExpanded,
canvasLinksExpanded: canvasLinksExpanded,
});
return [3 /*break*/, 3];
case 2:
if (this.props.showItems === true && this.props.isExpanded === true) { //Already fetched, just update filters
this.setState({
linkFilter: linkFilter ? linkFilter : '',
canvasImagesExpanded: canvasImagesExpanded,
canvasLinksExpanded: canvasLinksExpanded,
});
}
_a.label = 3;
case 3: return [2 /*return*/];
}
});
});
};
// private onLinkClick(ev: React.MouseEvent<HTMLElement>, item?: INavLink) {
// this.setState({ selectedKey: item.key });
// }
RelatedItems.prototype.filterUrlPairs = function (items, linkFilter, expanded, UrlType) {
var _this = this;
var showPropsStyles = expanded === true ? 'showProperties' : 'hideProperties';
var returnItems = [];
items.map(function (item) {
var desc = decodeURI(item.url.replace(_this.regExpOrigin, '').replace(_this.regExpWeb, '/ThisSite').replace(/(?<=\/ThisSite\/).*(?=\/)/gi, '...'));
var showItem = (0, filtering_1.complexStringSearch)(linkFilter, desc, ';', '<>', true);
// if ( linkFilter && desc.toLowerCase().indexOf( linkFilter.toLowerCase() ) < 0) { showItem = false; }
if (showItem === true) {
var label = React.createElement("span", { className: 'trimText' }, desc);
if (item.url) {
var liTitle = "Go to ".concat(item.url);
// return <li className = { 'isLink' } style={ this.props.itemsStyle } title={liTitle} onClick={ () => { this.onLinkClick.bind( this, item.url, item.embed ); }}>{ label }
returnItems.push(React.createElement("li", { className: 'isLink', style: _this.props.itemsStyle, title: liTitle, onClick: _this.onLinkClick.bind(_this, item.url, item.embed) },
label,
React.createElement(Icon_1.Icon, { title: "Go to ".concat(item.url), iconName: 'OpenInNewTab' })));
}
else {
returnItems.push(React.createElement("li", { style: _this.props.itemsStyle }, label));
}
}
});
var headingCount = linkFilter && linkFilter.length > 0 ? "".concat(returnItems.length, " of ").concat(items.length) : "".concat(returnItems.length);
var heading = "Embedded ".concat(UrlType, " ( ").concat(headingCount, " )");
return React.createElement("div", null,
React.createElement("div", { className: 'relatedSubTitle', onClick: function () { _this.toggleRelated("canvas".concat(UrlType, "Expanded")); }, title: "Click to toggle ".concat(UrlType) },
" ",
heading,
" "),
React.createElement("div", { className: showPropsStyles }, returnItems));
};
RelatedItems.prototype.filterRelatedItems = function (items, textFilter) {
var _this = this;
var returnItems = [];
items.map(function (item) {
var searchLinkText = typeof item.linkText === 'string' ? item.linkText : '';
var searchLinkUrl = typeof item.linkUrl === 'string' ? item.linkUrl : '';
var searchString = [searchLinkText, searchLinkUrl].join(' || ');
var showItem = true;
if (textFilter && searchString.toLowerCase().indexOf(textFilter.toLowerCase()) < 0) {
showItem = false;
}
if (showItem === true) {
var label = React.createElement("span", { className: 'trimText' }, item.linkText);
if (item.linkUrl) {
var liTitle = "Go to ".concat(item.linkText);
// return <li className = { 'isLink' } style={ this.props.itemsStyle } title={liTitle} onClick={ () => { this.onLinkClick.bind( this, item.linkUrl, item.linkAlt ); }}>{ label }
returnItems.push(React.createElement("li", { className: 'isLink', style: _this.props.itemsStyle, title: liTitle, onClick: _this.onLinkClick.bind(_this, item.linkUrl, item.linkAlt) },
label,
React.createElement(Icon_1.Icon, { title: "Go to ".concat(item.linkUrl), iconName: 'OpenInNewTab' })));
}
else {
returnItems.push(React.createElement("li", { style: _this.props.itemsStyle }, label));
}
}
});
return returnItems;
};
RelatedItems.prototype.render = function () {
// const { semanticColors }: IReadonlyTheme = this.props.themeVariant;
if (this.props.showItems === false) {
return (null);
}
else { //If there is a null value, it will just show it
var linksElement = null;
if (this.props.parentKey !== 'pageLinks') {
var noItemsMessage = this.state.errMess ? React.createElement("div", { style: { color: 'red', fontWeight: 600 } }, this.state.errMess)
: 'There are no related items ;(';
linksElement = this.state.items.length === 0 ? React.createElement("div", { style: { paddingLeft: '20px', paddingBottom: '10px', fontSize: 'larger' } }, noItemsMessage) :
React.createElement("div", null, this.filterRelatedItems(this.state.items, this.state.textFilter));
}
var imgList = null;
if (this.props.fetchInfo.canvasImgs === true && this.state.items.length > 0 && this.state.items[0].images.length > 0) {
imgList = this.filterUrlPairs(this.state.items[0].images, this.state.linkFilter, this.state.canvasImagesExpanded, 'Images');
}
var linksList = null;
if (this.props.fetchInfo.canvasLinks === true && this.state.items.length > 0 && this.state.items[0].links.length > 0) {
var paddingTop = imgList ? '10px' : undefined;
linksList = this.filterUrlPairs(this.state.items[0].links, this.state.linkFilter, this.state.canvasLinksExpanded, 'Links');
}
return (React.createElement("div", { className: 'relatedItems' },
linksElement,
imgList,
linksList));
}
};
RelatedItems.prototype.toggleRelated = function (propToToggle) {
var newExpanded = this.state[propToToggle] === true ? false : true;
if (propToToggle === 'canvasImagesExpanded') {
this.setState({ canvasImagesExpanded: newExpanded });
}
else if (propToToggle === 'canvasLinksExpanded') {
this.setState({ canvasLinksExpanded: newExpanded });
}
else {
alert("Whhhooaaa, was not expecting this propToToggle: ".concat(propToToggle, " ~ RelatedItems 171"));
}
};
// private async onLinkClick( gotoLink: string, altLink: string, ev: MouseEvent ) {
RelatedItems.prototype.onLinkClick = function (gotoLink, altLink, ev) {
return __awaiter(this, void 0, void 0, function () {
var decodedLink, web, item, e_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// alert('Going to ' + gotoLink );
console.log('onLinkClick ev:', ev);
if (!(ev.altKey === true && altLink)) return [3 /*break*/, 6];
if (!(altLink !== 'gotoLink')) return [3 /*break*/, 1];
window.open(altLink, '_blank');
return [3 /*break*/, 5];
case 1:
decodedLink = decodeURI(gotoLink);
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
web = (0, all_1.Web)("".concat(window.location.origin).concat(this.props.fetchInfo.web));
return [4 /*yield*/, web.getFileByServerRelativePath(decodedLink).select('*,ServerRedirectedEmbedUrl').getItem()];
case 3:
item = _a.sent();
console.log('onLinkClick alt-click item: ', decodedLink, item);
if (item.ServerRedirectedEmbedUrl) {
window.open(item.ServerRedirectedEmbedUrl, '_blank');
}
else {
window.open(decodedLink, '_blank');
}
return [3 /*break*/, 5];
case 4:
e_1 = _a.sent();
console.log('onLinkClick alt-click error: ', decodedLink, e_1);
window.open(decodedLink, '_blank');
return [3 /*break*/, 5];
case 5: return [3 /*break*/, 7];
case 6:
window.open(gotoLink, '_blank');
_a.label = 7;
case 7: return [2 /*return*/];
}
});
});
};
return RelatedItems;
}(React.Component));
exports.default = RelatedItems;
//# sourceMappingURL=RelatedItems.js.map