@pnp/spfx-property-controls
Version:
Reusable property pane controls for SharePoint Framework solutions
111 lines • 5.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const GeneralHelper_1 = require("../../../../helpers/GeneralHelper");
const react_1 = require("@fluentui/react");
const strings = tslib_1.__importStar(require("PropertyControlStrings"));
const LinkFilePickerTab_module_scss_1 = tslib_1.__importDefault(require("./LinkFilePickerTab.module.scss"));
class LinkFilePickerTab extends React.Component {
constructor(props) {
super(props);
/**
* Called as user types in a new value
*/
this._handleChange = (fileUrl) => {
const filePickerResult = fileUrl && this._isUrl(fileUrl)
? {
fileAbsoluteUrl: fileUrl,
fileName: GeneralHelper_1.GeneralHelper.getFileNameFromUrl(fileUrl),
fileNameWithoutExtension: GeneralHelper_1.GeneralHelper.getFileNameWithoutExtension(fileUrl),
downloadFileContent: () => {
return this.props.fileSearchService.downloadBingContent(fileUrl, GeneralHelper_1.GeneralHelper.getFileNameFromUrl(fileUrl));
},
}
: null;
this.setState({
filePickerResult,
});
};
/**
* Verifies the url that was typed in
* @param value
*/
this._getErrorMessagePromise = (value) => tslib_1.__awaiter(this, void 0, void 0, function* () {
// DOn't give an error for blank or placeholder value, but don't make it a valid entry either
if (value === undefined || value === 'https://') {
this.setState({ isValid: false });
return '';
}
// Make sure that user is typing a valid URL format
if (!this._isUrl(value)) {
this.setState({ isValid: false });
return '';
}
// If we don't allow external links, verify that we're in the same domain
if (!this.props.allowExternalLinks && !this._isSameDomain(value)) {
this.setState({ isValid: false });
return strings.NoExternalLinksValidationMessage;
}
if (!this.props.checkIfFileExists) {
this.setState({ isValid: true });
return '';
}
const fileExists = yield this.props.fileSearchService.checkFileExists(value);
this.setState({ isValid: fileExists });
const strResult = fileExists ? '' : strings.ProvidedValueIsInvalid;
return strResult;
});
/**
* Handles when user saves
*/
this._handleSave = () => {
this.props.onSave(this.state.filePickerResult);
};
/**
* HAndles when user closes without saving
*/
this._handleClose = () => {
this.props.onClose();
};
/**
* Is this a URL ?
* (insert guy holding a butterfly meme)
*/
this._isUrl = (fileUrl) => {
try {
const myURL = new URL(fileUrl.toLowerCase());
return myURL.host !== undefined;
}
catch (_a) {
return false;
}
};
this._isSameDomain = (fileUrl) => {
const siteUrl = this.props.context.pageContext.web.absoluteUrl;
return (GeneralHelper_1.GeneralHelper.getAbsoluteDomainUrl(siteUrl) ===
GeneralHelper_1.GeneralHelper.getAbsoluteDomainUrl(fileUrl));
};
this.state = { isValid: false };
}
render() {
const fileUrl = this.state.filePickerResult
? this.state.filePickerResult.fileAbsoluteUrl
: null;
return (React.createElement("div", { className: LinkFilePickerTab_module_scss_1.default.tabContainer },
React.createElement("div", { className: LinkFilePickerTab_module_scss_1.default.tabHeaderContainer },
React.createElement("h2", { className: LinkFilePickerTab_module_scss_1.default.tabHeader }, strings.LinkHeader)),
React.createElement("div", { className: (0, react_1.css)(LinkFilePickerTab_module_scss_1.default.tab, LinkFilePickerTab_module_scss_1.default.tabOffset) },
React.createElement(react_1.TextField, { multiline: true, required: true, resizable: false, deferredValidationTime: 300, className: LinkFilePickerTab_module_scss_1.default.linkTextField, label: this.props.allowExternalLinks
? strings.ExternalLinkFileInstructions
: strings.LinkFileInstructions, ariaLabel: this.props.allowExternalLinks
? strings.ExternalLinkFileInstructions
: strings.LinkFileInstructions, defaultValue: 'https://', onGetErrorMessage: (value) => this._getErrorMessagePromise(value), autoAdjustHeight: false, underlined: false, borderless: false, validateOnFocusIn: false, validateOnFocusOut: false, validateOnLoad: true, value: fileUrl, onChange: (e, newValue) => this._handleChange(newValue) })),
React.createElement("div", { className: LinkFilePickerTab_module_scss_1.default.actionButtonsContainer },
React.createElement("div", { className: LinkFilePickerTab_module_scss_1.default.actionButtons },
React.createElement(react_1.PrimaryButton, { disabled: !this.state.isValid, onClick: () => this._handleSave(), className: LinkFilePickerTab_module_scss_1.default.actionButton }, strings.OpenButtonLabel),
React.createElement(react_1.DefaultButton, { onClick: () => this._handleClose(), className: LinkFilePickerTab_module_scss_1.default.actionButton }, strings.CancelButtonLabel)))));
}
}
exports.default = LinkFilePickerTab;
//# sourceMappingURL=LinkFilePickerTab.js.map