/
plupload.html4.js
396 lines (323 loc) · 11.9 KB
/
plupload.html4.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* plupload.html4.js
*
* Copyright 2010, Ryan Demmer
* Copyright 2009, Moxiecode Systems AB
* Released under GPL License.
*
* License: http://www.plupload.com/license
* Contributing: http://www.plupload.com/contributing
*/
// JSLint defined globals
/*global plupload:false, window:false */
(function(window, document, plupload, undef) {
function getById(id) {
return document.getElementById(id);
}
/**
* HTML4 implementation. This runtime has no special features it uses an form that posts files into an hidden iframe.
*
* @static
* @class plupload.runtimes.Html4
* @extends plupload.Runtime
*/
plupload.runtimes.Html4 = plupload.addRuntime("html4", {
/**
* Returns a list of supported features for the runtime.
*
* @return {Object} Name/value object with supported features.
*/
getFeatures : function() {
// Only multipart feature
return {
multipart: true,
/* WebKit let you trigger file dialog programmatically while FF and Opera - do not, so we
sniff for it here... probably not that good idea, but impossibillity of controlling cursor style
on top of add files button obviously feels even worse */
canOpenDialog: navigator.userAgent.indexOf('WebKit') !== -1
};
},
/**
* Initializes the upload runtime.
*
* @method init
* @param {plupload.Uploader} uploader Uploader instance that needs to be initialized.
* @param {function} callback Callback to execute when the runtime initializes or fails to initialize. If it succeeds an object with a parameter name success will be set to true.
*/
init : function(uploader, callback) {
uploader.bind("Init", function(up) {
var container = document.body, iframe, url = "javascript", currentFile,
input, currentFileId, fileIds = [], IE = /MSIE/.test(navigator.userAgent), mimes = [],
filters = up.settings.filters, i, ext, type, y;
// Convert extensions to mime types list
for (i = 0; i < filters.length; i++) {
ext = filters[i].extensions.split(/,/);
for (y = 0; y < ext.length; y++) {
type = plupload.mimeTypes[ext[y]];
if (type) {
mimes.push(type);
}
}
}
mimes = mimes.join(',');
function createForm() {
var form, input, bgcolor, browseButton;
// Setup unique id for form
currentFileId = plupload.guid();
// Save id for Destroy handler
fileIds.push(currentFileId);
// Create form
form = document.createElement('form');
form.setAttribute('id', 'form_' + currentFileId);
form.setAttribute('method', 'post');
form.setAttribute('enctype', 'multipart/form-data');
form.setAttribute('encoding', 'multipart/form-data');
form.setAttribute("target", up.id + '_iframe');
form.style.position = 'absolute';
// Create input and set attributes
input = document.createElement('input');
input.setAttribute('id', 'input_' + currentFileId);
input.setAttribute('type', 'file');
input.setAttribute('accept', mimes);
input.setAttribute('size', 1);
browseButton = getById(up.settings.browse_button);
// Route click event to input element programmatically, if possible
if (up.features.canOpenDialog && browseButton) {
plupload.addEvent(getById(up.settings.browse_button), 'click', function(e) {
input.click();
e.preventDefault();
}, up.id);
}
// Set input styles
plupload.extend(input.style, {
width : '100%',
height : '100%',
opacity : 0,
fontSize: '2em' // force input element to be bigger then needed to occupy whole space
});
plupload.extend(form.style, {
overflow: 'hidden'
});
// Show the container if shim_bgcolor is specified
bgcolor = up.settings.shim_bgcolor;
if (bgcolor) {
form.style.background = bgcolor;
}
// no opacity in IE
if (IE) {
plupload.extend(input.style, {
filter : "alpha(opacity=0)"
});
}
// add change event
plupload.addEvent(input, 'change', function(e) {
var element = e.target, name, files = [], topElement;
if (element.value) {
getById('form_' + currentFileId).style.top = -0xFFFFF + "px";
// Get file name
name = element.value.replace(/\\/g, '/');
name = name.substring(name.length, name.lastIndexOf('/') + 1);
// Push files
files.push(new plupload.File(currentFileId, name));
// Clean-up events - they won't be needed anymore
if (!up.features.canOpenDialog) {
plupload.removeAllEvents(form, up.id);
} else {
plupload.removeEvent(browseButton, 'click', up.id);
}
plupload.removeEvent(input, 'change', up.id);
// Create and position next form
createForm();
// Fire FilesAdded event
if (files.length) {
uploader.trigger("FilesAdded", files);
}
}
}, up.id);
// append to container
form.appendChild(input);
container.appendChild(form);
up.refresh();
}
function createIframe() {
var temp = document.createElement('div');
// Create iframe using a temp div since IE 6 won't be able to set the name using setAttribute or iframe.name
temp.innerHTML = '<iframe id="' + up.id + '_iframe" name="' + up.id + '_iframe" src="' + url + ':""" style="display:none"></iframe>';
iframe = temp.firstChild;
container.appendChild(iframe);
// Add IFrame onload event
plupload.addEvent(iframe, 'load', function(e) {
var n = e.target, el, result;
// Ignore load event if there is no file
if (!currentFile) {
return;
}
try {
el = n.contentWindow.document || n.contentDocument || window.frames[n.id].document;
} catch (ex) {
// Probably a permission denied error
up.trigger('Error', {
code : plupload.SECURITY_ERROR,
message : plupload.translate('Security error.'),
file : currentFile
});
return;
}
// Get result
result = el.documentElement.innerText || el.documentElement.textContent;
// Assume no error
if (result) {
currentFile.status = plupload.DONE;
currentFile.loaded = 1025;
currentFile.percent = 100;
up.trigger('UploadProgress', currentFile);
up.trigger('FileUploaded', currentFile, {
response : result
});
}
}, up.id);
// Upload file
up.bind("UploadFile", function(up, file) {
var form, input;
// File upload finished
if (file.status == plupload.DONE || file.status == plupload.FAILED || up.state == plupload.STOPPED) {
return;
}
// Get the form and input elements
form = getById('form_' + file.id);
input = getById('input_' + file.id);
// Set input element name attribute which allows it to be submitted
input.setAttribute('name', up.settings.file_data_name);
// Store action
form.setAttribute("action", up.settings.url);
// Append multipart parameters
plupload.each(plupload.extend({name : file.target_name || file.name}, up.settings.multipart_params), function(value, name) {
var hidden = document.createElement('input');
plupload.extend(hidden, {
type : 'hidden',
name : name,
value : value
});
form.insertBefore(hidden, form.firstChild);
});
currentFile = file;
// Hide the current form
getById('form_' + currentFileId).style.top = -0xFFFFF + "px";
form.submit();
form.parentNode.removeChild(form);
});
} // end createIframe
if (up.settings.container) {
container = getById(up.settings.container);
container.style.position = 'relative';
}
uploader.bind('FileUploaded', function(up) {
up.refresh(); // just to get the form back on top of browse_button
});
up.bind('StateChanged', function(up) {
if (up.state == plupload.STARTED) {
createIframe();
}
if (up.state == plupload.STOPPED) {
window.setTimeout(function() {
plupload.removeEvent(iframe, 'load', up.id);
iframe.parentNode.removeChild(iframe);
}, 0);
}
});
// Refresh button, will reposition the input form
up.bind("Refresh", function(up) {
var browseButton, topElement, hoverClass, activeClass, browsePos, browseSize, inputContainer, inputFile, pzIndex;
browseButton = getById(up.settings.browse_button);
if (browseButton) {
browsePos = plupload.getPos(browseButton, getById(up.settings.container));
browseSize = plupload.getSize(browseButton);
inputContainer = getById('form_' + currentFileId);
inputFile = getById('input_' + currentFileId);
plupload.extend(inputContainer.style, {
top : browsePos.y + 'px',
left : browsePos.x + 'px',
width : browseSize.w + 'px',
height : browseSize.h + 'px'
});
// for IE and WebKit place input element underneath the browse button and route onclick event
// TODO: revise when browser support for this feature will change
if (up.features.canOpenDialog) {
pzIndex = parseInt(browseButton.parentNode.style.zIndex, 10);
if (isNaN(pzIndex)) {
pzIndex = 0;
}
plupload.extend(browseButton.style, {
position : 'relative',
zIndex : pzIndex
});
plupload.extend(inputContainer.style, {
zIndex : pzIndex - 1
});
}
/* Since we have to place input[type=file] on top of the browse_button for some browsers (FF, Opera),
browse_button loses interactivity, here we try to neutralize this issue highlighting browse_button
with a special class
TODO: needs to be revised as things will change */
hoverClass = up.settings.browse_button_hover;
activeClass = up.settings.browse_button_active;
topElement = up.features.canOpenDialog ? browseButton : inputContainer;
if (hoverClass) {
plupload.addEvent(topElement, 'mouseover', function() {
plupload.addClass(browseButton, hoverClass);
}, up.id);
plupload.addEvent(topElement, 'mouseout', function() {
plupload.removeClass(browseButton, hoverClass);
}, up.id);
}
if (activeClass) {
plupload.addEvent(topElement, 'mousedown', function() {
plupload.addClass(browseButton, activeClass);
}, up.id);
plupload.addEvent(document.body, 'mouseup', function() {
plupload.removeClass(browseButton, activeClass);
}, up.id);
}
}
});
// Remove files
uploader.bind("FilesRemoved", function(up, files) {
var i, n;
for (i = 0; i < files.length; i++) {
n = getById('form_' + files[i].id);
if (n) {
n.parentNode.removeChild(n);
}
}
});
// Completely destroy the runtime
uploader.bind("Destroy", function(up) {
var name, element, form,
elements = {
inputContainer: 'form_' + currentFileId,
inputFile: 'input_' + currentFileId,
browseButton: up.settings.browse_button
};
// Unbind event handlers
for (name in elements) {
element = getById(elements[name]);
if (element) {
plupload.removeAllEvents(element, up.id);
}
}
plupload.removeAllEvents(document.body, up.id);
// Remove mark-up
plupload.each(fileIds, function(id, i) {
form = getById('form_' + id);
if (form) {
container.removeChild(form);
}
});
});
// Create initial form
createForm();
});
callback({success : true});
}
});
})(window, document, plupload);