forked from SAP-samples/cloud-cap-samples-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Upload.js
119 lines (99 loc) · 3.96 KB
/
Upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
sap.ui.define(
["sap/m/MessageBox", "sap/m/MessageToast"],
function (MessageBox, MessageToast) {
"use strict";
function _createUploadController(oExtensionAPI) {
var oUploadDialog;
function setOkButtonEnabled(bOk) {
oUploadDialog && oUploadDialog.getBeginButton().setEnabled(bOk);
}
function setDialogBusy(bBusy) {
oUploadDialog.setBusy(bBusy)
}
function closeDialog() {
oUploadDialog && oUploadDialog.close()
}
function showError(sMessage) {
MessageBox.error(sMessage || "Upload failed")
}
// TODO: Better option for this?
function byId(sId) {
return sap.ui.core.Fragment.byId("uploadDialog", sId);
}
return {
onBeforeOpen: function (oEvent) {
oUploadDialog = oEvent.getSource();
oExtensionAPI.addDependent(oUploadDialog);
},
onAfterClose: function (oEvent) {
oExtensionAPI.removeDependent(oUploadDialog);
oUploadDialog.destroy();
oUploadDialog = undefined;
},
onOk: function (oEvent) {
setDialogBusy(true)
var oFileUploader = byId("uploader")
oFileUploader
.checkFileReadable()
.then(function () {
oFileUploader.upload();
})
.catch(function (error) {
showError("The file cannot be read.");
setDialogBusy(false)
})
},
onCancel: function (oEvent) {
closeDialog();
},
onTypeMismatch: function (oEvent) {
var sSupportedFileTypes = oEvent
.getSource()
.getFileType()
.map(function (sFileType) {
return "*." + sFileType;
})
.join(", ");
showError(
"The file type *." +
oEvent.getParameter("fileType") +
" is not supported. Choose one of the following types: " +
sSupportedFileTypes
);
},
onFileAllowed: function (oEvent) {
setOkButtonEnabled(true)
},
onFileEmpty: function (oEvent) {
setOkButtonEnabled(false)
},
onUploadComplete: function (oEvent) {
var iStatus = oEvent.getParameter("status");
var oFileUploader = oEvent.getSource()
oFileUploader.clear();
setOkButtonEnabled(false)
setDialogBusy(false)
if (iStatus >= 400) {
var oRawResponse = JSON.parse(oEvent.getParameter("responseRaw"));
showError(oRawResponse && oRawResponse.error && oRawResponse.error.message);
} else {
MessageToast.show("Uploaded successfully");
oExtensionAPI.refresh()
closeDialog();
}
}
};
}
return {
showUploadDialog: function (oBindingContext, aSelectedContexts) {
this.loadFragment({
id: "uploadDialog",
name: "admin.extension.UploadDialog",
controller: _createUploadController(this)
}).then(function (oDialog) {
oDialog.open();
});
}
};
}
);