-
Notifications
You must be signed in to change notification settings - Fork 0
/
jqput.js
143 lines (100 loc) · 3.91 KB
/
jqput.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
/*jslint indent: 2, maxlen: 80, continue: false, unparam: false, browser: true */
/* -*- tab-width: 2 -*- */
(function () {
'use strict';
var jq = window.jQuery;
function logEvent(evt) { console.log('event:', evt.type, evt); }
function saneFn(fn) {
return (fn.match(/[A-Za-z0-9\-\.]+/g) || []).join('_'
).replace(/[_\.]*(\.)[_\.]*/g, '$1'
).replace(/(\.jp)(g(?:\.|$))/g, '$1e$2'
).replace(/^[_\.]+/, '').replace(/[_\.]+$/, '');
}
function makeDestFn(fileMeta) {
var isoNow = (new Date()).toISOString().replace(/\D/g, '').substr(2, 12);
return (isoNow.slice(0, 6) + '-' + isoNow.slice(-4) + '.' +
Math.random().toString(36).replace(/^0?\./, '').slice(-6) + '.' +
saneFn(String(fileMeta.name).toLowerCase()));
}
function xhrUpload(cfgStr, destUrl, payload, whenDone, whenUploadProgress) {
var xhr = new XMLHttpRequest();
(function openXhr() {
var useAsyncXhr = true, un = cfgStr('dest_user'),
pw = cfgStr('dest_pass');
if ((un + pw) === '') { un = pw = null; }
xhr.open('PUT', destUrl, useAsyncXhr, un, pw);
}());
cfgStr('dest_headers').split(/\n/).forEach(function (v) {
var n = /^\s*(\S+):\s*/.exec(v);
if (!n) { return; }
xhr.setRequestHeader(n[1], v.slice(n[0].length));
});
xhr.overrideMimeType('application/octet-stream');
// ^-- Browser should not even try to parse the response as anything.
xhr.upload.onprogress = (whenUploadProgress && function (evt) {
logEvent(evt);
if (!evt.lengthComputable) { return; }
whenUploadProgress((+evt.loaded || 0), xhr, evt);
});
xhr.onload = xhr.onerror = (whenDone
&& function (evt) { whenDone(xhr, evt); });
xhr.send(payload);
return xhr;
}
function nqFile(form, fileBlob) {
console.log('fileBlob:', fileBlob);
form.latestFile = fileBlob;
if (!fileBlob) { return; }
var fileSize = (+fileBlob.size || 0), li, jqLi;
function hid(n) { return String((form.elements[n] || false).value || ''); }
jqLi = jq('<li class="file wip">' +
'<span class="status"><progress min="0" max="' + fileSize +
'" value="0"></progress></span>' +
' <span class="orig-fn"></span>' +
' → <a class="dest-fn" target="_blank"></a>' +
'</li>');
li = jqLi[0];
li.status = jqLi.find('.status');
li.progressBar = jqLi.find('progress')[0];
li.fileBlob = fileBlob;
jqLi.find('.orig-fn').text(fileBlob.name);
li.destFn = (form.makeDestFn || makeDestFn)(fileBlob);
li.destLink = jqLi.find('.dest-fn').text(li.destFn)[0];
li.destLink.href = hid('dest_prefix') + li.destFn;
jqLi.prependTo(form.uploadsList || jq(form).find('.uploads').first());
function onProgress(bytes) {
var pb = li.progressBar;
if (!pb) { return; }
pb.value = bytes;
pb.innerHTML = ((100 * bytes) / fileSize).toFixed(2) + '%';
}
function onXhrDone(xhr, evt) {
var httpStatus = (+xhr.status || 0),
success = ((httpStatus >= 200) && (httpStatus < 300));
logEvent(evt);
li.progressBar = false;
li.status.text(httpStatus + ' ' + xhr.statusText);
jqLi.removeClass('wip').addClass(success ? 'sxs' : 'err');
}
li.xhr = xhrUpload(hid, li.destLink.href, fileBlob, onXhrDone, onProgress);
}
function upgradeForm(form) {
var jqForm = jq(form), blobF = form.elements.file_blob;
jq(blobF).on('change', function () {
nqFile(form, blobF.files[0]);
form.reset();
});
jqForm.on('dragenter dragover drop submit', function (evt) {
evt.stopPropagation();
evt.preventDefault();
});
jqForm.on('drop', function (evt) {
logEvent(evt);
var dt = (evt.dataTransfer || evt.originalEvent.dataTransfer);
if (!dt.files) { return; }
nqFile(form, dt.files[0]);
});
jqForm.addClass('dropzone');
}
jq('form.easydav-jqput').each(function (i, f) { upgradeForm(f, i); });
}());