gd-sprest-react
Advanced tools
Comparing version 2.6.8 to 2.6.9
@@ -8,4 +8,2 @@ /// <reference types="react" /> | ||
data?: any; | ||
deleteFl?: boolean; | ||
existsFl?: boolean; | ||
ext: string; | ||
@@ -48,6 +46,10 @@ name: string; | ||
errorMessage?: string; | ||
/** The file attachments */ | ||
files: Array<IAttachmentFile>; | ||
/** The attachment files */ | ||
files: { | ||
Delete: Array<IAttachmentFile>; | ||
New: Array<IAttachmentFile>; | ||
Existing: Array<IAttachmentFile>; | ||
}; | ||
/** The list information */ | ||
listInfo: Types.Helper.ListForm.IListFormProps; | ||
listInfo: Types.Helper.ListForm.IListFormResult; | ||
/** Loading Flag */ | ||
@@ -61,6 +63,9 @@ loadingFl?: boolean; | ||
/** | ||
* Refreshes the item attachments. | ||
*/ | ||
refresh: () => PromiseLike<void>; | ||
/** | ||
* Saves the item attachments. | ||
* @param itemId - The item id. | ||
*/ | ||
save: (itemId: number) => PromiseLike<any>; | ||
save: () => PromiseLike<void>; | ||
/** | ||
@@ -67,0 +72,0 @@ * Displays the file upload dialog. |
@@ -15,19 +15,4 @@ /// <reference types="react" /> | ||
constructor(props: IFieldAttachmentsProps); | ||
/** | ||
* Method to render the component | ||
*/ | ||
render(): JSX.Element; | ||
/** | ||
* Method to refresh the attachments. | ||
*/ | ||
refresh: () => PromiseLike<void>; | ||
/** | ||
* Method to save the attachments to the item | ||
*/ | ||
save: () => PromiseLike<any>; | ||
/** | ||
* Method to show the file dialog | ||
*/ | ||
showFileDialog: () => void; | ||
/** | ||
* Methods | ||
@@ -52,2 +37,3 @@ */ | ||
private linkClick; | ||
refresh: () => PromiseLike<void>; | ||
/** | ||
@@ -63,2 +49,6 @@ * Event triggered by clicking on the attachment delete icon | ||
/** | ||
* Method to save the attachments to the item | ||
*/ | ||
save: () => PromiseLike<void>; | ||
/** | ||
* Method to save the attachments | ||
@@ -68,2 +58,6 @@ */ | ||
/** | ||
* Method to show the file dialog | ||
*/ | ||
showFileDialog: () => void; | ||
/** | ||
* Method to convert the item value to the attachment file array | ||
@@ -70,0 +64,0 @@ * @param attachments - The file attachments. |
@@ -30,32 +30,2 @@ "use strict"; | ||
/** | ||
* Method to refresh the attachments. | ||
*/ | ||
_this.refresh = function () { | ||
// Load the attachments | ||
return _this.loadAttachments(); | ||
}; | ||
/** | ||
* Method to save the attachments to the item | ||
*/ | ||
_this.save = function () { | ||
// Return a promise | ||
return new Promise(function (resolve, reject) { | ||
// Delete the attachments | ||
_this.deleteAttachments().then(function () { | ||
// Save the attachments | ||
_this.saveAttachments().then(function (attachments) { | ||
// Resolve the promise | ||
resolve(attachments); | ||
}); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Method to show the file dialog | ||
*/ | ||
_this.showFileDialog = function () { | ||
// Show the file dialog | ||
_this._file.click(); | ||
}; | ||
/** | ||
* Methods | ||
@@ -81,18 +51,36 @@ */ | ||
var attachment = null; | ||
var files = _this.state.files || []; | ||
var state = _this.state; | ||
// Parse the attachments | ||
for (var i = 0; i < files.length; i++) { | ||
var file = files[i]; | ||
for (var i = 0; i < _this.state.files.Existing.length; i++) { | ||
var file = _this.state.files.Existing[i]; | ||
// See if the file already exists | ||
if (file.name.toLowerCase() == srcFile.name.toLowerCase()) { | ||
var deleteFl = true; | ||
// Set the flag | ||
newFl = false; | ||
// Update the file | ||
file.data = ev.target.result; | ||
file.deleteFl = false; | ||
file.name = srcFile.name; | ||
file.ext = file.name.split("."); | ||
file.ext = file.ext[file.ext.length - 1].toLowerCase(); | ||
// Set the attachment | ||
attachment = file; | ||
// Delete the file | ||
for (var j = 0; j < _this.state.files.Delete.length; j++) { | ||
// See if this file is already flagged to be deleted | ||
if (_this.state.files.Delete[j].name == file.name) { | ||
// Set the flag | ||
deleteFl = false; | ||
} | ||
} | ||
// See if we are deleting the file | ||
if (deleteFl) { | ||
// Delete the file | ||
_this.state.files.Delete.push(file); | ||
} | ||
else { | ||
// Parse the files to add | ||
for (var j = 0; j < _this.state.files.New.length; j++) { | ||
var newFile = _this.state.files.New[j]; | ||
// See if this is the file | ||
if (newFile.name == file.name) { | ||
// Update the file | ||
newFile.data = ev.target.result; | ||
newFile.name = srcFile.name; | ||
} | ||
} | ||
} | ||
// Break from the loop | ||
@@ -106,12 +94,8 @@ break; | ||
ext = ext[ext.length - 1].toLowerCase(); | ||
// Set the attachment | ||
attachment = { | ||
// Add the attachment | ||
state.files.New.push({ | ||
data: ev.target.result, | ||
deleteFl: false, | ||
existsFl: false, | ||
ext: ext, | ||
name: srcFile.name | ||
}; | ||
// Add the file | ||
files.push(attachment); | ||
}); | ||
} | ||
@@ -121,6 +105,3 @@ // Call the file added event | ||
// Update the state | ||
_this.setState({ | ||
files: files, | ||
loadingFl: false | ||
}); | ||
_this.setState(state); | ||
}; | ||
@@ -131,4 +112,3 @@ // Set the error | ||
_this.setState({ | ||
errorMessage: ev.target.error, | ||
loadingFl: false | ||
errorMessage: ev.target.error | ||
}); | ||
@@ -143,3 +123,3 @@ }; | ||
*/ | ||
_this.deleteAttachments = function () { | ||
_this.deleteAttachments = function (state) { | ||
// Return a promise | ||
@@ -149,4 +129,4 @@ return new Promise(function (resolve, reject) { | ||
// Parse the files to delete | ||
for (var i = 0; i < _this.state.files.length; i++) { | ||
var file = _this.state.files[i]; | ||
for (var i = 0; i < _this.state.files.Delete.length; i++) { | ||
var file = _this.state.files.Delete[i]; | ||
// add the file | ||
@@ -161,5 +141,11 @@ files.push({ | ||
// Remove the attachments | ||
gd_sprest_1.Helper.ListForm.removeAttachments(_this.state.listInfo, files).then(function () { | ||
gd_sprest_1.Helper.ListForm.removeAttachments({ | ||
itemId: _this.props.itemId, | ||
listName: _this.props.listName, | ||
webUrl: _this.props.webUrl | ||
}, files).then(function () { | ||
// Clear the delete array | ||
state.files.Delete = []; | ||
// Resolve the promise | ||
resolve(); | ||
resolve(state); | ||
}); | ||
@@ -169,3 +155,3 @@ } | ||
// Resolve the promise | ||
resolve(); | ||
resolve(state); | ||
} | ||
@@ -178,12 +164,17 @@ }); | ||
_this.loadAttachments = function () { | ||
// Return a promise | ||
return new Promise(function (resolve, reject) { | ||
// Load the attachments | ||
gd_sprest_1.Helper.ListForm.loadAttachments(_this.state.listInfo).then(function (attachments) { | ||
// Update the state | ||
_this.setState({ | ||
files: _this.toArray(attachments) | ||
}); | ||
// Resolve the promise | ||
resolve(); | ||
// Create the list information | ||
(new gd_sprest_1.Helper.ListForm({ | ||
itemId: _this.props.itemId, | ||
listName: _this.props.listName, | ||
loadAttachments: true, | ||
webUrl: _this.props.webUrl | ||
})).then(function (listInfo) { | ||
// Update the state | ||
_this.setState({ | ||
files: { | ||
Delete: [], | ||
Existing: _this.toArray(listInfo.attachments), | ||
New: [] | ||
}, | ||
listInfo: listInfo | ||
}); | ||
@@ -203,5 +194,4 @@ }); | ||
// Parse the attachments | ||
var files = _this.state.files || []; | ||
for (var i = 0; i < files.length; i++) { | ||
var file = files[i]; | ||
for (var i = 0; i < _this.state.files.Existing.length; i++) { | ||
var file = _this.state.files.Existing[i]; | ||
// See if this is the attachment to remove | ||
@@ -217,2 +207,16 @@ if (file.name.toLowerCase() == fileName) { | ||
}; | ||
// Refresh the attachments | ||
_this.refresh = function () { | ||
// Return a promise | ||
return new Promise(function (resolve, reject) { | ||
// Clear the existing items | ||
var state = _this.state; | ||
state.files.Existing = null; | ||
// Update the state | ||
_this.setState(state, function () { | ||
// Resolve the promise | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -226,18 +230,13 @@ * Event triggered by clicking on the attachment delete icon | ||
// Get the file name | ||
var fileName = ev.currentTarget.getAttribute("data-filename"); | ||
var fileName = ev.currentTarget.getAttribute("data-filename").toLowerCase(); | ||
// Parse the attachments | ||
var files = _this.state.files || []; | ||
for (var i = 0; i < files.length; i++) { | ||
var file = files[i]; | ||
for (var i = 0; i < _this.state.files.Existing.length; i++) { | ||
var file = _this.state.files.Existing[i]; | ||
// See if this is the attachment to remove | ||
if (file.name.toLowerCase() == fileName) { | ||
// See if this item exists | ||
if (file.existsFl) { | ||
// Set the delete flag | ||
file.deleteFl = true; | ||
} | ||
else { | ||
// Remove the file | ||
files.splice(i, 1); | ||
} | ||
var files = _this.state.files; | ||
// Delete the attachment | ||
files.Delete.push(file); | ||
// Update the state | ||
_this.setState({ files: files }); | ||
// Break from the loop | ||
@@ -247,4 +246,2 @@ break; | ||
} | ||
// Update the state | ||
_this.setState({ files: files }); | ||
}; | ||
@@ -257,9 +254,4 @@ /** | ||
// Parse the files | ||
var files = _this.state.files || []; | ||
for (var i = 0; i < files.length; i++) { | ||
var file = files[i]; | ||
// Ensure we are not deleting this field | ||
if (file.deleteFl) { | ||
continue; | ||
} | ||
for (var i = 0; i < _this.state.files.Existing.length; i++) { | ||
var file = _this.state.files.Existing[i]; | ||
// See if the file render event exists | ||
@@ -287,15 +279,35 @@ var attachment = null; | ||
/** | ||
* Method to save the attachments to the item | ||
*/ | ||
_this.save = function () { | ||
// Return a promise | ||
return new Promise(function (resolve, reject) { | ||
// Update the state | ||
_this.setState({ loadingFl: true }, function () { | ||
// Delete the attachments | ||
_this.deleteAttachments(_this.state).then(function (state) { | ||
// Save the attachments | ||
_this.saveAttachments(state).then(function (state) { | ||
// Set the loading flag | ||
state.loadingFl = false; | ||
// Update the state | ||
_this.setState(state, function () { | ||
// Resolve the promise | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Method to save the attachments | ||
*/ | ||
_this.saveAttachments = function () { | ||
_this.saveAttachments = function (state) { | ||
// Return a promise | ||
return new Promise(function (resolve, reject) { | ||
var files = []; | ||
// Parse the files to delete | ||
for (var i = 0; i < _this.state.files.length; i++) { | ||
var file = _this.state.files[i]; | ||
// See if we are deleting this file | ||
if (file.deleteFl) { | ||
continue; | ||
} | ||
// Parse the new files | ||
for (var i = 0; i < state.files.New.length; i++) { | ||
var file = state.files.New[i]; | ||
// See if data exists | ||
@@ -310,8 +322,16 @@ if (file.data) { | ||
} | ||
// Clear the new items | ||
state.files.New = []; | ||
// Ensure files exist | ||
if (files.length > 0) { | ||
// Save the attachments | ||
gd_sprest_1.Helper.ListForm.saveAttachments(_this.state.listInfo, files).then(function (attachments) { | ||
gd_sprest_1.Helper.ListForm.saveAttachments({ | ||
itemId: _this.props.itemId, | ||
listName: _this.props.listName, | ||
webUrl: _this.props.webUrl | ||
}, files).then(function (attachments) { | ||
// Update the attachments | ||
state.listInfo.attachments = attachments; | ||
// Resolve the promise | ||
resolve(attachments); | ||
resolve(state); | ||
}); | ||
@@ -321,3 +341,3 @@ } | ||
// Resolve the promise | ||
resolve([]); | ||
resolve(state); | ||
} | ||
@@ -327,2 +347,9 @@ }); | ||
/** | ||
* Method to show the file dialog | ||
*/ | ||
_this.showFileDialog = function () { | ||
// Show the file dialog | ||
_this._file.click(); | ||
}; | ||
/** | ||
* Method to convert the item value to the attachment file array | ||
@@ -334,6 +361,6 @@ * @param attachments - The file attachments. | ||
// Ensure attachments exist | ||
if (attachments && attachments.results) { | ||
if (attachments) { | ||
// Parse the attachments | ||
for (var i = 0; i < attachments.results.length; i++) { | ||
var attachment = attachments.results[i]; | ||
for (var i = 0; i < attachments.length; i++) { | ||
var attachment = attachments[i]; | ||
// Set the file extension | ||
@@ -345,4 +372,2 @@ var ext = attachment.FileName.split("."); | ||
data: null, | ||
deleteFl: false, | ||
existsFl: true, | ||
ext: ext, | ||
@@ -360,8 +385,8 @@ name: attachment.FileName, | ||
errorMessage: "", | ||
files: props.files && typeof (props.files) !== "function" ? _this.toArray(props.files) : null, | ||
listInfo: { | ||
itemId: _this.props.itemId, | ||
listName: _this.props.listName, | ||
webUrl: _this.props.webUrl | ||
files: { | ||
Delete: [], | ||
Existing: props.files, | ||
New: [] | ||
}, | ||
listInfo: null, | ||
loadingFl: false | ||
@@ -371,17 +396,16 @@ }; | ||
} | ||
/** | ||
* Method to render the component | ||
*/ | ||
// Render the component | ||
FieldAttachments.prototype.render = function () { | ||
var _this = this; | ||
var elAttachments = null; | ||
// See if we are loading the attachments | ||
if (this.state.loadingFl) { | ||
// Render a loading dialog | ||
return (React.createElement(office_ui_fabric_react_1.Spinner, { label: "Loading..." })); | ||
} | ||
// Ensure the files exist | ||
if (this.state.files == null) { | ||
var loadingFl = this.state.loadingFl; | ||
// Ensure the attachments have been loaded | ||
if (this.state.files.Existing == null) { | ||
// Load the attachments | ||
this.loadAttachments(); | ||
// Set the flag | ||
loadingFl = true; | ||
} | ||
// See if we are loading the attachments | ||
if (loadingFl) { | ||
// Render a loading dialog | ||
@@ -392,3 +416,3 @@ return (React.createElement(office_ui_fabric_react_1.Spinner, { label: "Loading..." })); | ||
if (this.props.onRender) { | ||
elAttachments = this.props.onRender(this.state.files); | ||
elAttachments = this.props.onRender(this.state.files.Existing); | ||
} | ||
@@ -395,0 +419,0 @@ else { |
{ | ||
"name": "gd-sprest-react", | ||
"version": "2.6.8", | ||
"version": "2.6.9", | ||
"description": "SharePoint react components.", | ||
@@ -41,3 +41,3 @@ "main": "build/index.js", | ||
"css-loader": "^0.28.9", | ||
"gd-sprest": "^3.0.8", | ||
"gd-sprest": "^3.1.1", | ||
"node-sass": "^4.7.2", | ||
@@ -44,0 +44,0 @@ "office-ui-fabric-react": "^5.40.2", |
Sorry, the diff of this file is not supported yet
363156
6240