blob: 411efad3c2baebc5a9e5425d22afd1672e202582 (
plain)
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
|
/* globals $ */
var smapi = smapi || {};
/**
* Implements the logic for a log/JSON file upload form.
*
* @param {object} opts The file upload form options.
* @param {jQuery} opts.chooseFileLink The clickable link which shows the file chooser.
* @param {jQuery} opts.chooseFileInput The file input element.
* @param {jQuery} opts.contentArea The file content area.
* @param {jQuery} opts.submitButton The submit button.
*/
smapi.fileUpload = function (opts) {
/**
* Toggle the submit button if the form has content.
*/
var toggleSubmit = function () {
var hasText = !!opts.contentArea.val().trim();
opts.submitButton.prop("disabled", !hasText);
};
/**
* Paste the content of a file into the content area.
* @param {File} file The file whose content to paste.
*/
var pasteFile = function (file) {
var reader = new FileReader();
reader.onload = $.proxy(function (file, $input, event) {
$input.val(event.target.result);
toggleSubmit();
}, this, file, $("#input"));
reader.readAsText(file);
};
// initialize
if (opts.contentArea.length) {
// disable submit button if it's empty
opts.contentArea.on("input", toggleSubmit);
toggleSubmit();
// drag & drop file
opts.contentArea.on({
"dragover dragenter": function (e) {
e.preventDefault();
},
"drop": function (e) {
e.preventDefault();
var transfer = e.originalEvent.dataTransfer;
if (transfer && transfer.files.length)
pasteFile(transfer.files[0]);
}
});
// choose file
opts.chooseFileLink.on({
"click": function (e) {
e.preventDefault();
opts.chooseFileInput.click();
}
});
opts.chooseFileInput.on({
"change": function (e) {
if (!e.target.files || !e.target.files.length)
return;
pasteFile(e.target.files[0]);
}
});
}
};
|