forked from danharper/avatar-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar.js
226 lines (179 loc) · 5.56 KB
/
avatar.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
(function ( root, factory ) {
if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define(function () {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return ( root.AvatarUpload = factory() );
});
} else {
// Browser globals
root.AvatarUpload = factory();
}
}( this, function () {
var extend = function(original, extra) {
return Object.keys(extra).forEach(function(key) {
original[key] = extra[key];
});
};
var parseJson = function(input) {
try {
return JSON.parse(input);
}
catch (e) {
return false;
}
};
var AvatarUpload = function(config) {
extend(this.config, config);
if ( ! this.config.el) {
throw new Error('An element is required to manipulate');
}
if ( ! this.config.uploadUrl && ! this.config.pretendUpload) {
throw new Error('Upload URL not specified');
}
this.el = this.config.el;
this.renderInput();
this.bindInput();
this.progressText = this.el.querySelector('span');
this.imageWrapper = this.el.querySelector('.avatar-upload__image-wrapper');
};
AvatarUpload.prototype.config = {
el: undefined,
uploadUrl: '',
uploadMethod: 'post',
uploadImageKey: 'upload',
uploadData: {},
pretendUpload: false,
onProgress: undefined,
onSuccess: undefined,
onError: undefined
};
AvatarUpload.prototype.renderInput = function() {
var imgEl = this.el.querySelector('img'),
img = imgEl.src;
var el = document.createElement('div');
el.className = 'avatar-upload__shell';
el.innerHTML = [
'<div class="avatar-upload__wrapper avatar-upload--complete">',
'<div class="avatar-upload__image-wrapper">',
'<img src="'+img+'">',
'</div>',
'<img src="'+img+'" class="avatar-upload__faded-image">',
'<div class="avatar-upload__progress-wrapper">',
'<span>0%</span>',
'</div>',
'<input type="file">',
'</div>',
].join('');
imgEl.parentNode.removeChild(imgEl);
this.el.appendChild(el);
return this;
};
AvatarUpload.prototype.bindInput = function(event) {
this.el.querySelector('input').addEventListener(
'change', this.initiateUpload.bind(this), true
);
};
AvatarUpload.prototype.initiateUpload = function(event) {
var file = event.target.files[0];
// reset input to allow selecting same file again
event.target.value = null;
if ( ! file.type.match(/image.*/)) return;
// read file & run upload
var reader = new FileReader;
reader.onload = this.displayUpload.bind(this);
reader.readAsDataURL(file);
this.upload(file);
};
AvatarUpload.prototype.displayUpload = function(event) {
var img = event.target.result;
this.uiUploadStart(img);
};
AvatarUpload.prototype.upload = function(file) {
var Uploader = this.config.pretendUpload ? FakeUploader : XhrUploader;
Uploader(file, this.config, {
progress: this.uploadProgress.bind(this),
success: this.uploadSuccess.bind(this),
error: this.uploadError.bind(this),
});
};
AvatarUpload.prototype.uploadProgress = function(progress) {
this.uiUploadProgress(progress);
if (this.config.onProgress) this.config.onProgress(progress, this.el, this);
};
AvatarUpload.prototype.uploadSuccess = function(xhr, json) {
this.uiUploadSuccess(xhr, json);
if (this.config.onSuccess) this.config.onSuccess(xhr, json);
};
AvatarUpload.prototype.uploadError = function(xhr, json) {
this.uiUploadError(xhr, json);
if (this.config.onError) this.config.onError(xhr, json);
};
AvatarUpload.prototype.uiUploadStart = function(img) {
var origSrc;
Array.prototype.forEach.call(this.el.querySelectorAll('img'), function(imgEl) {
origSrc = imgEl.src;
imgEl.src = img;
});
this.origSrc = origSrc;
this.el.querySelector('.avatar-upload__wrapper').className = 'avatar-upload__wrapper'; // remove complete class
};
AvatarUpload.prototype.uiUploadProgress = function(progress) {
this.progressText.textContent = progress+'%';
this.imageWrapper.style.width = progress+'%';
};
AvatarUpload.prototype.uiUploadSuccess = function(xhr, json) {
this.progressText.textContent = '0%';
this.imageWrapper.style.width = null;
this.el.querySelector('.avatar-upload__wrapper').className = 'avatar-upload__wrapper avatar-upload--complete';
};
AvatarUpload.prototype.uiUploadError = function(xhr, json) {
this.uiUploadSuccess();
var origSrc = this.origSrc;
Array.prototype.forEach.call(this.el.querySelectorAll('img'), function(imgEl) {
imgEl.src = origSrc;
});
};
var FakeUploader = function(file, config, callbacks) {
var progress = 0;
var id = setInterval(function() {
progress += 1;
// if (progress == 50) {
// callbacks.error();
// return clearInterval(id);
// }
if (progress > 100) {
callbacks.success();
return clearInterval(id);
}
callbacks.progress(progress);
}, 50);
};
var XhrUploader = function(file, config, callbacks) {
var xhr = new XMLHttpRequest(),
formData = new FormData();
xhr.upload.addEventListener('progress', function(transfer) {
callbacks.progress(parseInt(
transfer.loaded / transfer.total * 100
, 10));
});
xhr.onreadystatechange = function(e) {
if (xhr.readyState !== 4) return;
if (xhr.status === 200) {
callbacks.success(xhr, parseJson(xhr.response));
}
else {
callbacks.error(xhr, parseJson(xhr.response));
}
};
formData.append(config.uploadImageKey, file);
for (val in config.uploadData) {
formData.append(val, config.uploadData[val]);
}
xhr.open(config.uploadMethod, config.uploadUrl);
xhr.send(formData);
};
return AvatarUpload;
}));