-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajaxFileUpload.js
More file actions
301 lines (225 loc) · 12.2 KB
/
ajaxFileUpload.js
File metadata and controls
301 lines (225 loc) · 12.2 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"use strict";
(function($){
var iFrame = {
id: "",
currentSpanVisibleId: "",
options: {},
uploadForm : {
id: "",
addFileInput: function(fileInput) {
$("#" + iFrame.id).contents().find("#" + this.id).append(fileInput.clone());
},
showUploadingAnim: function (){
iFrame.currentSpanVisibleId = "uploadAnim";
$("#" + iFrame.currentSpanVisibleId).fadeIn(500, function(){
iFrame.uploadForm.submitAction();
});
},
submitAction: function() {
$("#" + iFrame.id).contents().find("#" + this.id).submit();
},
submit: function(){
if (iFrame.currentSpanVisibleId !== "") {
console.log("HIDING " + iFrame.currentSpanVisibleId);
$("#" + iFrame.currentSpanVisibleId).fadeOut(500, function(){
iFrame.uploadForm.showUploadingAnim();
});
} else {
console.log("here");
this.showUploadingAnim();
}
}
},
openDocument: function(){
var iFrameDoc = $("#" + this.id).contents()[0];
iFrameDoc.open();
iFrameDoc.write('<!doctype html><html><head></head><body></body></html>');
iFrameDoc.close();
},
addInputControlToForm: function(formId){
$("<input>", {
id: "iFrameLoaded",
type: "hidden",
value: 0
}).appendTo("#" + formId);
this.addReadyListener("iFrameLoaded");
},
appendToForm : function(formId) {
$("<iframe>", {
id: this.id,
style: "display:none;"
}).appendTo($("#" + formId));
},
createHTML: function(uploadFormId, uploadFormAction){
this.openDocument();
var contentHTML = iFrameHTML.replace("{{UPLOADFORMID}}", uploadFormId);
contentHTML = contentHTML.replace("{{UPLOADFORMACTION}}", uploadFormAction);
$('body', $("#" + this.id).contents()).append(contentHTML);
},
eventHandler: function(e){
if (e.nodeName === 'JSON') {
console.log("JSON NODE DETECTED");
}
},
/**
* Listen for value change of inputFieldId and reacts as a consequence
* parsing the json tag in the iFrame
* @param {string} inputFieldId
* @returns {undefined}
*/
addReadyListener: function(inputFieldId){
$("#" + inputFieldId).on("change", function(e) {
if (parseInt($(e.target).val()) === 1) {
var spanToShowId = "uploadError";
// Parse the <json></json> tag
var jsonData = $.parseJSON($("#" + iFrame.id).contents().find("json").text());
// If error property is null was successfull
if (jsonData.error === null){
$("#uploadInfoText")
.html("<strong>File type</strong>: "
+ jsonData.mimeType
+ " - <strong>Size</strong>: "
+ jsonData.size);
spanToShowId = "uploadInfo";
// Adds a hidden input text field to the main form
// which stores the session uid containing the file
// uploaded
$("<input/>", {
name: "uid",
id: "c_file_uid",
value: jsonData.uid,
type: "hidden"
}).appendTo($("#" + iFrame.options.formId));
// otherwise upload failed
} else {
$("#uploadErrorText")
.html("<strong>Error while uploading file</strong>: "
+ jsonData.error);
}
$("#c_fileUploadControlsContainer").hide(300, function(){
$("#c_filename_disabled")
.val($("#" + iFrame.options.fileNameId).val());
$("#c_discardUploadContainer").fadeIn(300);
});
// Reset input field value
$(e.target).val(0);
iFrame.createHTML(iFrame.options.iFrameUploadFormId, iFrame.options.iFrameUploadFormAction);
// Hides loading animation and shows information/errors
$("#" + iFrame.currentSpanVisibleId).fadeOut(500, function(){
iFrame.currentSpanVisibleId = spanToShowId;
$("#" + spanToShowId).fadeIn(500);
});
}
});
}
};
var instances = {};
var infoHTML = "" +
'<p id="uploadAnim" class="text-warning" style="display:none;margin-left:10px;"><i class="fa fa-cog faa-spin animated"></i> Uploading file...</p>' +
'<p id="uploadError" class="text-error" style="display:none;margin-left:10px;"><i class="fa fa-exclamation-triangle faa-flash animated"></i> <span id="uploadErrorText">Unexpected error...</span></p>'+
'<p id="uploadInfo" class="text-muted" style="display:none;margin-left:10px;"><i class="fa fa-file-code-o"></i> <span id="uploadInfoText">Generic file informations...</span></p>';
// The following script is used to inform the parent page that
// the iframe was loaded
var iFrameHTML = "" +
'<form id="{{UPLOADFORMID}}" method="post" action="{{UPLOADFORMACTION}}" enctype="multipart/form-data">' +
'<input type="hidden" name="MAX_FILE_SIZE" value="8388608" />' +
'</form>' +
'<script type="text/javascript">' +
'window.onload = function(){' +
'var inputField = parent.window.document.getElementById("iFrameLoaded");' +
'inputField.value = 1;' +
'parent.$(inputField).trigger("change");' +
'};' +
'</script>' ;
var template = [
'<div id="c_discardUploadContainer" class="input-prepend" style="display:none;">',
'<button type="button" class="btn btn-danger" id="c_discard_button">Discard</button>',
'<input class="{{FILENAMECLASS}}" id="c_filename_disabled" type="text" disabled>',
'</div>',
'<div id="c_fileUploadControlsContainer" class="input-prepend">',
'<span id="c_fileInputContainer" class="btn btn-default btn-file">',
'{{TEXT}}',
'</span>',
'<input class="{{FILENAMECLASS}}" id="{{ID}}" type="text" placeholder="{{FILENAMEPLACEHOLDER}}">',
'</div>'
];
var AjaxFileUpload = function(inputFileField, options) {
this.detachEventHandlers = function(){
$("#c_fileInputContainer, #" + options.fileNameId).off("click", function(e){
inputFileField.trigger("click");
});
inputFileField.off("change", function(e){
$("#" + options.fileNameId).val($(this).val());
});
};
this.attachEventHandlers = function(){
$("#c_fileInputContainer, #" + options.fileNameId).on("click", function(e){
inputFileField.trigger("click");
});
inputFileField.on("change", function(e){
$("#" + options.fileNameId).val($(this).val());
iFrame.uploadForm.addFileInput(inputFileField);
iFrame.uploadForm.submit();
});
// Manages discard file upload
$("#c_discard_button").on("click", function(e){
// Hides the discard container and shows the upload one
$("#c_discardUploadContainer").fadeOut(300, function(){
$("#" + iFrame.options.fileNameId).val("");
$("#c_fileUploadControlsContainer").fadeIn(300);
});
// Hides messages
$("#" + iFrame.currentSpanVisibleId).fadeOut(300);
// Removes the old uploaded file uid
$("#" + options.formId).find("#c_file_uid").remove();
inputFileField.val("");
});
};
this.publishTemplate = function(){
var inputFieldClone = inputFileField.clone().attr("style", "display:none;");
// Creates template HTML
var templateString = template.join("");
templateString = templateString.replace("{{TEXT}}", options.inputFileButtonText);
templateString = templateString.replace("{{ID}}", options.fileNameId);
templateString = templateString.replace("{{FILENAMECLASS}}", options.fileNameClass);
templateString = templateString.replace("{{FILENAMECLASS}}", options.fileNameClass);
templateString = templateString.replace("{{FILENAMEPLACEHOLDER}}", options.filenamePlaceholder);
inputFileField.replaceWith(templateString);
$(inputFieldClone)
.appendTo($("#c_fileInputContainer"));
$(infoHTML)
.insertAfter($("#c_fileUploadControlsContainer"));
};
this.appendIFrame = function(){
iFrame.addInputControlToForm(options.formId);
iFrame.appendToForm(options.formId);
iFrame.createHTML(options.iFrameUploadFormId, options.iFrameUploadFormAction);
};
};
$.fn.ajaxFileUpload = function(options) {
var options = $.extend({
fileTextFieldId: 'c_filename',
fileNameClass: 'span4',
fileNameId: 'c_filename',
filenamePlaceholder: 'File name',
inputFileButtonText: 'Browse',
formId: "myForm",
iFrameUploadFormAction: "fileUpload.php",
iFrameUploadFormId: "uploadFileForm",
iFrameId: "fileUploadIFrame"
}, options);
iFrame.id = options.iFrameId;
iFrame.uploadForm.id = options.iFrameUploadFormId;
iFrame.options = options;
if (this.attr("type") !== "file") {
throw new Error("This plugin must be attached to an input element of type file");
}
if ($.isEmptyObject(instances) || instances[this.selector] === undefined) {
instances[this.selector] = new AjaxFileUpload(this, options);
instances[this.selector].publishTemplate();
instances[this.selector].attachEventHandlers();
instances[this.selector].appendIFrame();
}
return this;
};
}(jQuery));