2
2
import * as React from 'react' ;
3
3
import { Dialog , DialogType , DialogFooter } from 'office-ui-fabric-react/lib/Dialog' ;
4
4
import { PrimaryButton , DefaultButton } from 'office-ui-fabric-react/lib/Button' ;
5
- import { Icon , IconType } from 'office-ui-fabric-react/lib/Icon' ;
6
5
import { Label } from "office-ui-fabric-react/lib/Label" ;
7
- import { Link } from 'office-ui-fabric-react/lib/Link' ;
8
6
import * as strings from 'ControlStrings' ;
9
7
import styles from './ListItemAttachments.module.scss' ;
10
8
import { UploadAttachment } from './UploadAttachment' ;
@@ -13,7 +11,6 @@ import {
13
11
DocumentCard ,
14
12
DocumentCardActions ,
15
13
DocumentCardPreview ,
16
- DocumentCardTitle ,
17
14
IDocumentCardPreviewImage
18
15
} from 'office-ui-fabric-react/lib/DocumentCard' ;
19
16
import { ImageFit } from 'office-ui-fabric-react/lib/Image' ;
@@ -43,65 +40,132 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
43
40
showPlaceHolder : false ,
44
41
fireUpload : false
45
42
} ;
43
+
46
44
// Get SPService Factory
47
45
this . _spservice = new SPservice ( this . props . context ) ;
48
46
this . _utilities = new utilities ( ) ;
47
+ }
49
48
50
- // registo de event handlers
51
- this . _onDeleteAttachment = this . _onDeleteAttachment . bind ( this ) ;
52
- this . _closeDialog = this . _closeDialog . bind ( this ) ;
53
- this . _onAttachmentpload = this . _onAttachmentpload . bind ( this ) ;
54
- this . _onConfirmedDeleteAttachment = this . _onConfirmedDeleteAttachment . bind ( this ) ;
49
+ /**
50
+ * componentDidMount lifecycle hook
51
+ */
52
+ public componentDidMount ( ) {
53
+ this . loadAttachments ( ) ;
55
54
}
56
- // Load Item Attachments
57
- private async _loadAttachments ( ) {
55
+
56
+ /**
57
+ * Load Item Attachments
58
+ */
59
+ private async loadAttachments ( ) {
58
60
this . previewImages = [ ] ;
59
61
try {
60
62
const files : IListItemAttachmentFile [ ] = await this . _spservice . getListItemAttachments ( this . props . listId , this . props . itemId ) ;
61
- for ( const _file of files ) {
62
63
63
- const _previewImage = await this . _utilities . GetFileImageUrl ( _file ) ;
64
+ for ( const file of files ) {
65
+ const _previewImage = await this . _utilities . GetFileImageUrl ( file ) ;
64
66
this . previewImages . push ( {
65
- name : _file . FileName ,
67
+ name : file . FileName ,
66
68
previewImageSrc : _previewImage ,
67
69
iconSrc : '' ,
68
70
imageFit : ImageFit . center ,
69
71
width : 187 ,
70
72
height : 130 ,
71
73
} ) ;
72
74
}
75
+
73
76
this . setState ( {
74
77
hideDialog : true ,
75
78
dialogMessage : '' ,
76
79
attachments : files ,
77
80
showPlaceHolder : files . length === 0 ? true : false
78
-
79
81
} ) ;
80
- }
81
- catch ( error ) {
82
+ } catch ( error ) {
82
83
this . setState ( {
83
84
hideDialog : true ,
84
85
dialogMessage : strings . ListItemAttachmentserrorLoadAttachments . replace ( '{0}' , error . message )
85
86
} ) ;
86
87
}
87
88
}
88
- // LoadAttachments
89
- public componentDidMount ( ) {
90
89
91
- this . _loadAttachments ( ) ;
90
+ /**
91
+ * Close the dialog
92
+ */
93
+ private _closeDialog = ( ) => {
94
+ this . setState ( {
95
+ hideDialog : true ,
96
+ dialogMessage : '' ,
97
+ file : null ,
98
+ deleteAttachment : false ,
99
+ } ) ;
100
+
101
+ this . loadAttachments ( ) ;
102
+ }
103
+
104
+ /**
105
+ * Attachment uploaded event handler
106
+ */
107
+ private _onAttachmentUpload = ( ) => {
108
+ // load Attachments
109
+ this . loadAttachments ( ) ;
110
+ }
111
+
112
+ /**
113
+ * On delete attachment event handler
114
+ *
115
+ * @param file
116
+ */
117
+ private onDeleteAttachment = ( file : IListItemAttachmentFile ) => {
118
+ this . setState ( {
119
+ hideDialog : false ,
120
+ deleteAttachment : true ,
121
+ file : file ,
122
+ dialogMessage : strings . ListItemAttachmentsconfirmDelete . replace ( '{0}' , file . FileName ) ,
123
+ } ) ;
124
+ }
125
+
126
+ /**
127
+ * Delete the attachment once it was confirmed
128
+ */
129
+ private onConfirmedDeleteAttachment = async ( ) => {
130
+ // Delete Attachment
131
+ const { file } = this . state ;
132
+
133
+ this . setState ( {
134
+ disableButton : true ,
135
+ } ) ;
136
+
137
+ try {
138
+ await this . _spservice . deleteAttachment ( file . FileName , this . props . listId , this . props . itemId , this . props . webUrl ) ;
139
+
140
+ this . setState ( {
141
+ hideDialog : false ,
142
+ deleteAttachment : false ,
143
+ disableButton : false ,
144
+ file : null ,
145
+ dialogMessage : strings . ListItemAttachmentsfileDeletedMsg . replace ( '{0}' , file . FileName ) ,
146
+ } ) ;
147
+ } catch ( error ) {
148
+ this . setState ( {
149
+ hideDialog : false ,
150
+ file : null ,
151
+ deleteAttachment : false ,
152
+ dialogMessage : strings . ListItemAttachmentsfileDeleteError . replace ( '{0}' , file . FileName ) . replace ( '{1}' , error . message )
153
+ } ) ;
154
+ }
92
155
}
93
156
94
- // Render Attachments
157
+ /**
158
+ * Default React render method
159
+ */
95
160
public render ( ) {
96
161
return (
97
-
98
162
< div className = { styles . ListItemAttachments } >
99
163
< UploadAttachment
100
164
listId = { this . props . listId }
101
165
itemId = { this . props . itemId }
102
166
disabled = { this . props . disabled }
103
167
context = { this . props . context }
104
- onAttachmentUpload = { this . _onAttachmentpload }
168
+ onAttachmentUpload = { this . _onAttachmentUpload }
105
169
fireUpload = { this . state . fireUpload }
106
170
/>
107
171
@@ -115,21 +179,21 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
115
179
onConfigure = { ( ) => this . setState ( { fireUpload : true } ) } />
116
180
:
117
181
118
- this . state . attachments . map ( ( _file , i : number ) => {
182
+ this . state . attachments . map ( ( file , i : number ) => {
119
183
return (
120
184
< div className = { styles . documentCardWrapper } >
121
185
< TooltipHost
122
- content = { _file . FileName }
186
+ content = { file . FileName }
123
187
calloutProps = { { gapSpace : 0 , isBeakVisible : true } }
124
188
closeDelay = { 200 }
125
189
directionalHint = { DirectionalHint . rightCenter } >
126
190
127
191
< DocumentCard
128
- onClickHref = { `${ _file . ServerRelativeUrl } ?web=1` }
192
+ onClickHref = { `${ file . ServerRelativeUrl } ?web=1` }
129
193
className = { styles . documentCard } >
130
194
< DocumentCardPreview previewImages = { [ this . previewImages [ i ] ] } />
131
195
< Label className = { styles . fileLabel } >
132
- { _file . FileName }
196
+ { file . FileName }
133
197
</ Label >
134
198
< DocumentCardActions
135
199
actions = {
@@ -145,7 +209,7 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
145
209
onClick : ( ev ) => {
146
210
ev . preventDefault ( ) ;
147
211
ev . stopPropagation ( ) ;
148
- this . _onDeleteAttachment ( _file ) ;
212
+ this . onDeleteAttachment ( file ) ;
149
213
}
150
214
} ,
151
215
]
@@ -174,11 +238,11 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
174
238
< DialogFooter >
175
239
< div style = { { marginBottom : 7 } } >
176
240
{
177
- this . state . disableButton ? < Spinner size = { SpinnerSize . medium } /> : ''
241
+ this . state . disableButton ? < Spinner size = { SpinnerSize . medium } /> : null
178
242
}
179
243
</ div >
180
244
{
181
- this . state . deleteAttachment ? ( < PrimaryButton disabled = { this . state . disableButton } onClick = { this . _onConfirmedDeleteAttachment } > { strings . ListItemAttachmentsdialogOKbuttonLabelOnDelete } </ PrimaryButton > ) : ""
245
+ this . state . deleteAttachment ? ( < PrimaryButton disabled = { this . state . disableButton } onClick = { this . onConfirmedDeleteAttachment } > { strings . ListItemAttachmentsdialogOKbuttonLabelOnDelete } </ PrimaryButton > ) : null
182
246
}
183
247
{
184
248
this . state . deleteAttachment ? ( < DefaultButton disabled = { this . state . disableButton } onClick = { this . _closeDialog } > { strings . ListItemAttachmentsdialogCancelButtonLabel } </ DefaultButton > )
@@ -190,68 +254,4 @@ export class ListItemAttachments extends React.Component<IListItemAttachmentsPro
190
254
</ div >
191
255
) ;
192
256
}
193
-
194
- // close dialog
195
- private _closeDialog ( e ) {
196
- e . preventDefault ( ) ;
197
-
198
- this . setState ( {
199
- hideDialog : true ,
200
- dialogMessage : '' ,
201
- file : null ,
202
- deleteAttachment : false ,
203
- } ) ;
204
- this . _loadAttachments ( ) ;
205
- }
206
-
207
- // On onAttachmentpload
208
- private _onAttachmentpload ( ) {
209
- // load Attachments
210
- this . _loadAttachments ( ) ;
211
- }
212
-
213
- // On _onDeleteAttachment
214
- private _onDeleteAttachment ( _file : IListItemAttachmentFile ) {
215
- this . setState ( {
216
- hideDialog : false ,
217
- deleteAttachment : true ,
218
- file : _file ,
219
- dialogMessage : strings . ListItemAttachmentsconfirmDelete . replace ( '{0}' , _file . FileName ) ,
220
- } ) ;
221
- }
222
- /*
223
- * Confirmed Delete
224
- */
225
- private _onConfirmedDeleteAttachment ( ) {
226
- // Delete Attachment
227
- const _file = this . state . file ;
228
-
229
- this . setState ( {
230
- disableButton : true ,
231
- } ) ;
232
-
233
- this . _spservice . deleteAttachment ( _file . FileName , this . props . listId , this . props . itemId , this . props . webUrl )
234
- . then ( ( ) => {
235
-
236
- this . setState ( {
237
- hideDialog : false ,
238
- deleteAttachment : false ,
239
- disableButton : false ,
240
- file : null ,
241
- dialogMessage : strings . ListItemAttachmentsfileDeletedMsg . replace ( '{0}' , _file . FileName ) ,
242
- } ) ;
243
-
244
- } )
245
- . catch ( ( reason ) => {
246
-
247
- this . setState ( {
248
- hideDialog : false ,
249
- file : null ,
250
- deleteAttachment : false ,
251
- dialogMessage : strings . ListItemAttachmentsfileDeleteError . replace ( '{0}' , _file . FileName ) . replace ( '{1}' , reason )
252
- } ) ;
253
-
254
- } ) ;
255
- }
256
257
}
257
- export default ListItemAttachments ;
0 commit comments