@pnp/spfx-controls-react
Version:
Reusable React controls for SharePoint Framework solutions
157 lines • 7.41 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UploadAttachment = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var Dialog_1 = require("@fluentui/react/lib/Dialog");
var Button_1 = require("@fluentui/react/lib/Button");
var SPService_1 = tslib_1.__importDefault(require("../../services/SPService"));
var strings = tslib_1.__importStar(require("ControlStrings"));
var CommandBar_1 = require("@fluentui/react/lib/CommandBar");
var ProgressIndicator_1 = require("@fluentui/react/lib/ProgressIndicator");
var ListItemAttachments_module_scss_1 = tslib_1.__importDefault(require("./ListItemAttachments.module.scss"));
var UploadAttachment = /** @class */ (function (_super) {
tslib_1.__extends(UploadAttachment, _super);
function UploadAttachment(props) {
var _this = _super.call(this, props) || this;
_this._isFileExplorerOpen = false;
/**
* On attachment upload event
*/
_this.onAttachmentUpload = function () {
// fire click event
_this.fileInput.current.value = '';
_this.fileInput.current.click();
};
/**
* Add a new attachment
*/
_this.addAttachment = function (e) {
_this.setState({
isLoading: true
});
var reader = new FileReader();
var file = e.target.files[0];
return new Promise(function (resolve, reject) {
reader.onloadend = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var updatedItem, error_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 3, , 4]);
if (!(this.props.itemId && this.props.itemId > 0)) return [3 /*break*/, 2];
return [4 /*yield*/, this._spservice.addAttachment(this.props.listId, this.props.itemId, file.name, file, this.props.webUrl)];
case 1:
updatedItem = _a.sent();
// Notify parent component of the ETag change
if (updatedItem && this.props.onAttachmentChange) {
this.props.onAttachmentChange(updatedItem);
}
_a.label = 2;
case 2:
this.props.onAttachmentUpload(file);
this.setState({
isLoading: false
});
resolve();
return [3 /*break*/, 4];
case 3:
error_1 = _a.sent();
this.setState({
hideDialog: false,
isLoading: false,
dialogMessage: strings.ListItemAttachmentsuploadAttachmentErrorMsg.replace('{0}', file.name).replace('{1}', error_1.message)
});
reject(error_1);
return [3 /*break*/, 4];
case 4:
this._isFileExplorerOpen = false;
return [2 /*return*/];
}
});
}); };
reader.readAsDataURL(file);
});
};
/**
* Called when the hidden file input is clicked (activated).
* @param e - Mouse click event on the file input element.
*/
_this.onInputActivated = function (e) {
setTimeout(function () {
window.addEventListener('focus', _this.handleFocusAfterDialog);
}, 300);
};
/**
* Handles window focus event after the file picker dialog is closed.
*/
_this.handleFocusAfterDialog = function () {
window.removeEventListener('focus', _this.handleFocusAfterDialog);
_this._isFileExplorerOpen = false;
_this.props.onUploadDialogClosed();
};
/**
* Close dialog
*/
_this.closeDialog = function () {
_this.setState({
hideDialog: true,
dialogMessage: '',
});
};
_this.state = {
hideDialog: true,
dialogMessage: '',
isLoading: false,
};
// Get SPService
_this._spservice = new SPService_1.default(_this.props.context);
_this.fileInput = React.createRef();
return _this;
}
/**
* componentDidUpdate lifecycle hook
*
* @param prevProps
* @param prevState
*/
UploadAttachment.prototype.componentDidUpdate = function (prevProps, prevState) {
if (this.props.fireUpload && !this._isFileExplorerOpen) {
this.fileInput.current.value = '';
this.fileInput.current.click();
this._isFileExplorerOpen = true;
}
};
/**
* Default React render method
*/
UploadAttachment.prototype.render = function () {
var _this = this;
return (React.createElement("div", null,
React.createElement("input", { id: "file-picker", style: { display: 'none' }, type: "file", onChange: function (e) { return _this.addAttachment(e); }, onClick: function (e) { return _this.onInputActivated(e); }, ref: this.fileInput }),
React.createElement("div", { className: ListItemAttachments_module_scss_1.default.uploadBar },
React.createElement(CommandBar_1.CommandBar, { items: [{
key: 'Add',
name: strings.ListItemAttachmentsCommandBarAddAttachmentLabel,
iconProps: {
iconName: 'Upload'
},
onClick: this.onAttachmentUpload,
disabled: this.props.disabled
}] })),
React.createElement("div", null, this.state.isLoading ? React.createElement(ProgressIndicator_1.ProgressIndicator, { label: strings.ListItemAttachmentsloadingMessage }) : ""),
React.createElement(Dialog_1.Dialog, { hidden: this.state.hideDialog, type: Dialog_1.DialogType.normal, onDismiss: this.closeDialog, dialogContentProps: {
type: Dialog_1.DialogType.normal,
title: strings.ListItemAttachmentsuploadAttachmentDialogTitle,
subText: this.state.dialogMessage
}, modalProps: {
isBlocking: true,
containerClassName: 'ms-dialogMainOverride'
} },
React.createElement(Dialog_1.DialogFooter, null,
React.createElement(Button_1.PrimaryButton, { onClick: this.closeDialog }, "OK")))));
};
return UploadAttachment;
}(React.Component));
exports.UploadAttachment = UploadAttachment;
//# sourceMappingURL=UploadAttachment.js.map