UNPKG

@pnp/spfx-property-controls

Version:

Reusable property pane controls for SharePoint Framework solutions

193 lines 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const React = tslib_1.__importStar(require("react")); const react_1 = require("@fluentui/react"); const placeHolderControl_1 = require("../../placeHolderControl"); const GeneralHelper_1 = require("../../../../helpers/GeneralHelper"); const strings = tslib_1.__importStar(require("PropertyControlStrings")); const RecentFilesTab_module_scss_1 = tslib_1.__importDefault(require("./RecentFilesTab.module.scss")); /** * Rows per page */ const ROWS_PER_PAGE = 3; /** * Maximum row height */ const MAX_ROW_HEIGHT = 175; class RecentFilesTab extends React.Component { constructor(props) { super(props); this._listElem = undefined; this._onSelectionChanged = () => { // Get the selected item const selectedItems = this._selection.getSelection(); if (selectedItems && selectedItems.length > 0) { //Get the selected key const selectedKey = selectedItems[0]; const filePickerResult = { fileAbsoluteUrl: selectedKey.fileUrl, fileName: GeneralHelper_1.GeneralHelper.getFileNameFromUrl(selectedKey.fileUrl), fileNameWithoutExtension: GeneralHelper_1.GeneralHelper.getFileNameWithoutExtension(selectedKey.fileUrl), downloadFileContent: () => { return this.props.fileSearchService.downloadSPFileContent(selectedKey.fileUrl, GeneralHelper_1.GeneralHelper.getFileNameFromUrl(selectedKey.fileUrl)); }, }; // Save the selected file this.setState({ filePickerResult, }); } else { // Remove any selected file this.setState({ filePickerResult: undefined, }); } if (this._listElem) { // Force the list to update to show the selection check this._listElem.forceUpdate(); } }; /** * Calculates how many items there should be in the page */ this._getItemCountForPage = (itemIndex, surfaceRect) => { if (itemIndex === 0 && surfaceRect.width > 0) { this._columnCount = Math.ceil(surfaceRect.width / MAX_ROW_HEIGHT); this._columnWidth = Math.floor(surfaceRect.width / this._columnCount); this._rowHeight = this._columnWidth; } return this._columnCount * ROWS_PER_PAGE; }; /** Calculates the list "page" height (a.k.a. row) */ this._getPageHeight = () => { return this._rowHeight * ROWS_PER_PAGE; }; /** * Renders a "please wait" spinner while we're loading */ this._renderSpinner = () => { return React.createElement(react_1.Spinner, { label: strings.Loading }); }; /** * Renders a message saying that there are no recent files */ this._renderPlaceholder = () => { return (React.createElement(placeHolderControl_1.Placeholder, { iconName: 'OpenFolderHorizontal', iconText: strings.NoRecentFiles, description: strings.NoRecentFilesDescription })); }; /** * Renders a grid list containing results */ this._renderGridList = () => { return (React.createElement("span", { className: RecentFilesTab_module_scss_1.default.recentGridList, role: 'grid' }, React.createElement(react_1.FocusZone, null, React.createElement(react_1.SelectionZone, { selection: this._selection, onItemInvoked: (item) => { if (item) this._handleItemInvoked(item); } }, React.createElement(react_1.List, { ref: this._linkElement, items: this.state.results, onRenderCell: this._onRenderCell, getItemCountForPage: this._getItemCountForPage, getPageHeight: this._getPageHeight, renderedWindowsAhead: 4 }))))); }; /** * Renders each result in its own cell */ this._onRenderCell = (item, index) => { let isSelected = false; if (this._selection && index !== undefined) { isSelected = this._selection.isIndexSelected(index); } return (React.createElement("div", { className: RecentFilesTab_module_scss_1.default.gridListCell, role: 'gridCell' }, React.createElement("div", { className: (0, react_1.css)(RecentFilesTab_module_scss_1.default.itemTile, RecentFilesTab_module_scss_1.default.isFile, RecentFilesTab_module_scss_1.default.hasThumbnail, isSelected ? RecentFilesTab_module_scss_1.default.isSelected : undefined), role: 'link', "aria-selected": isSelected, "data-is-draggable": 'false', "data-is-focusable": 'true', "data-selection-index": index, "data-selection-invoke": 'true', "data-item-index": index, "data-automationid": 'ItemTile', style: { width: this._columnWidth, height: this._rowHeight, } }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileContent }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileFile }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileFileContainer }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileThumbnail }, React.createElement(react_1.Image, { src: item.fileUrl, width: this._columnWidth, height: this._rowHeight, imageFit: react_1.ImageFit.cover })), React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileCheckCircle, role: 'checkbox', "aria-checked": isSelected, "data-item-index": index, "data-selection-toggle": true, "data-automationid": 'CheckCircle' }, React.createElement(react_1.Check, { checked: isSelected })), React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileNamePlate }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileName }, item.name), React.createElement("div", { className: RecentFilesTab_module_scss_1.default.itemTileSubText }, React.createElement("span", null, strings.EditedByNamePlate, item.editedBy))))))))); }; /** * Gets called what a file is selected. */ this._handleItemInvoked = (item) => { this._selection.setKeySelected(item.key, true, true); }; /** * Gets called when it is time to save the currently selected item */ this._handleSave = () => { this.props.onSave(this.state.filePickerResult); }; /** * Gets called when it is time to close (without saving) */ this._handleClose = () => { this.props.onClose(); }; /** * Creates a ref to the list */ this._linkElement = (e) => { const needToUpdate = !this._listElem && !!e && !this._columnWidth; this._listElem = e; // // sometimes getItemCountForPage is called when surfaceRect is still has 0 width // We need to rerender the list if that happens // if (needToUpdate) { setTimeout(() => { this._listElem.forceUpdate(); }, 0); } }; this._selection = new react_1.Selection({ selectionMode: react_1.SelectionMode.single, onSelectionChanged: this._onSelectionChanged, }); this.state = { isLoading: true, results: [], filePickerResult: null, }; } /** * Gets the most recently used files */ componentDidMount() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const recentFilesResult = yield this.props.fileSearchService.executeRecentSearch(this.props.accepts); this._selection.setItems(recentFilesResult, true); this.setState({ isLoading: false, results: recentFilesResult, }); }); } /** * Render the tab */ render() { const { results, isLoading } = this.state; return (React.createElement("div", { className: RecentFilesTab_module_scss_1.default.tabContainer }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.tabHeaderContainer }, React.createElement("h2", { className: RecentFilesTab_module_scss_1.default.tabHeader }, strings.RecentDocumentsHeader)), React.createElement("div", { className: (0, react_1.css)(RecentFilesTab_module_scss_1.default.tab, RecentFilesTab_module_scss_1.default.tabOffset) }, isLoading ? this._renderSpinner() : results === undefined || results.length < 1 ? this._renderPlaceholder() : this._renderGridList()), React.createElement("div", { className: RecentFilesTab_module_scss_1.default.actionButtonsContainer }, React.createElement("div", { className: RecentFilesTab_module_scss_1.default.actionButtons }, React.createElement(react_1.PrimaryButton, { disabled: !this.state.filePickerResult, onClick: () => this._handleSave(), className: RecentFilesTab_module_scss_1.default.actionButton }, strings.OpenButtonLabel), React.createElement(react_1.DefaultButton, { onClick: () => this._handleClose(), className: RecentFilesTab_module_scss_1.default.actionButton }, strings.CancelButtonLabel))))); } } exports.default = RecentFilesTab; //# sourceMappingURL=RecentFilesTab.js.map