This repository has been archived by the owner on Mar 3, 2021. It is now read-only.
forked from flimflamjs/ff-file-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
133 lines (109 loc) · 3.68 KB
/
build.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
'use strict';
var _ramda = require('ramda');
var _ramda2 = _interopRequireDefault(_ramda);
var _flyd = require('flyd');
var _flyd2 = _interopRequireDefault(_flyd);
var _filter = require('flyd/module/filter');
var _filter2 = _interopRequireDefault(_filter);
var _h = require('snabbdom/h');
var _h2 = _interopRequireDefault(_h);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var init = function init(config) {
var state = _ramda2.default.merge({ fileTypes: [], maxKB: undefined }, config);
state.error$ = _flyd2.default.stream();
state.file$ = _flyd2.default.stream();
var imageFile$ = (0, _filter2.default)(isType('image'), state.file$);
var textFile$ = (0, _filter2.default)(isType('text'), state.file$);
state.image$ = _flyd2.default.map(readImage(state), imageFile$);
state.text$ = _flyd2.default.map(readText(state), textFile$);
return state;
};
var isType = function isType(type) {
return function (file) {
return file && file.type.split('/')[0] === type;
};
};
var readImage = function readImage(state) {
return function (file) {
state.text$(undefined);
var reader = new FileReader();
reader.onload = function (ev) {
state.image$(ev.target.result);
};
reader.readAsDataURL(file);
};
};
var readText = function readText(state) {
return function (file) {
state.image$(undefined);
var reader = new FileReader();
reader.onload = function (ev) {
state.text$(ev.target.result);
};
reader.readAsText(file);
};
};
var handleDrop = function handleDrop(state) {
return function (e) {
e.preventDefault();
handleFile(state, e.dataTransfer.files[0]);
};
};
var handleChange = function handleChange(state) {
return function (e) {
e.preventDefault();
handleFile(state, e.target.files[0]);
e.target.value = '';
};
};
var error = function error(state, msg) {
state.file$(undefined);
state.image$(undefined);
state.text$(undefined);
state.error$(msg);
throw msg;
};
var handleFile = function handleFile(state, file) {
if (!file) return;
var type = _ramda2.default.last(file.type.split('/'));
// checks file type against whitelisted file types
if (!_ramda2.default.contains(type, state.fileTypes)) {
var fileTypeError = type.toUpperCase() + ' files cannot be uploaded';
error(state, fileTypeError);
return;
}
// checks file size against maxKB
if (state.maxKB && file.size > state.maxKB * 1000) {
var fileSizeError = 'File size must not exceed ' + KBMB(state.maxKB);
error(state, fileSizeError);
return;
}
state.error$(undefined);
state.file$(file);
};
var KBMB = function KBMB(KB) {
if (KB >= 1000) return KB / 1000 + ' MB';
return KB + ' KB';
};
var supportsTouch = function supportsTouch() {
return 'ontouchstart' in document.documentElement;
};
var clickView = function clickView(obj) {
return (0, _h2.default)('div', { attrs: { 'data-ff-file-uploader-click-content': '' } }, [(0, _h2.default)('input', { props: { type: 'file' },
attrs: { 'data-ff-file-uploader-input': '' },
on: { change: handleChange(obj.state) } }), obj.clickContent || 'Select file']);
};
var dragView = function dragView(obj) {
return (0, _h2.default)('div', {
attrs: { 'data-ff-file-uploader': '' },
on: {
dragover: function dragover(e) {
return e.preventDefault();
},
drop: handleDrop(obj.state) } }, [(0, _h2.default)('div', { attrs: { 'data-ff-file-uploader-drag-content': '' } }, [obj.dragContent || 'Drag a file to upload']), clickView(obj)]);
};
var view = function view(obj) {
if (supportsTouch() || obj.noDrag) return clickView(obj);
return dragView(obj);
};
module.exports = { view: view, init: init };