Skip to content

Commit 1e8f666

Browse files
committed
Added options (maxFileSizePreview, allowedFileExtensions). Jshint.
1 parent 7d574f3 commit 1e8f666

File tree

8 files changed

+97
-75
lines changed

8 files changed

+97
-75
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ $('.dropify').dropify();
150150
```
151151

152152

153+
* __allowedFileExtensions:__ You can allow only some file extensions. If you add the attr __data-allowed-file-extensions="pdf png psd"__ only PDF, PNG and PSD files will be allowed. By default, everything is allowed. Default: ['*'].
154+
155+
```html
156+
<input type="file" class="dropify" data-allowed-file-extensions="pdf png psd" />
157+
```
158+
159+
160+
* __maxFileSizePreview:__ Set the max filesize of the previewed document (if it's an image). If the file size is bigger than the option, it will be only the file icon and disabled the preview. You can use unit like K, M and G.
161+
162+
```html
163+
<input type="file" class="dropify" data-max-file-size-preview="3M" />
164+
```
165+
166+
153167
* __messages:__ You can translate default messages. You juste have to add an options array when you init the plugin. This messages will be replaced in the __tpl__ option.
154168

155169
```javascript

dist/css/demo.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* =============================================================
3-
* dropify v0.2.0 - Override your input files with style.
3+
* dropify v0.2.1 - Override your input files with style.
44
* https://github.com/JeremyFagis/dropify
55
*
66
* (c) 2016 - Jeremy FAGIS <[email protected]> (http://fagis.fr)

dist/css/dropify.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* =============================================================
3-
* dropify v0.2.0 - Override your input files with style.
3+
* dropify v0.2.1 - Override your input files with style.
44
* https://github.com/JeremyFagis/dropify
55
*
66
* (c) 2016 - Jeremy FAGIS <[email protected]> (http://fagis.fr)

dist/css/dropify.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/dropify.js

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* =============================================================
3-
* dropify v0.2.0 - Override your input files with style.
3+
* dropify v0.2.1 - Override your input files with style.
44
* https://github.com/JeremyFagis/dropify
55
*
66
* (c) 2016 - Jeremy FAGIS <[email protected]> (http://fagis.fr)
@@ -41,8 +41,10 @@ function Dropify(element, options) {
4141
showErrors: true,
4242
errorTimeout: 3000,
4343
errorsPosition: 'overlay',
44+
imgFileExtensions: ['png', 'jpg', 'jpeg', 'gif', 'bmp'],
45+
maxFileSizePreview: "5M",
4446
allowedFormats: ['portrait', 'square', 'landscape'],
45-
allowedFileExtensions: ['*'],
47+
allowedFileExtensions: ['*'],
4648
messages: {
4749
'default': 'Drag and drop a file here or click',
4850
'replace': 'Drag and drop or click to replace',
@@ -76,8 +78,6 @@ function Dropify(element, options) {
7678
this.preview = null;
7779
this.filenameWrapper = null;
7880
this.settings = $.extend(true, defaults, options, this.input.data());
79-
this.imgFileExtensions = ['png', 'jpg', 'jpeg', 'gif', 'bmp'];
80-
this.maxFileSizePreview = 5000000;
8181
this.errorsEvent = $.Event('dropify.errors');
8282
this.isDisabled = false;
8383
this.isInit = false;
@@ -94,6 +94,10 @@ function Dropify(element, options) {
9494
this.settings.allowedFormats = this.settings.allowedFormats.split(' ');
9595
}
9696

97+
if (!Array.isArray(this.settings.allowedFileExtensions)) {
98+
this.settings.allowedFileExtensions = this.settings.allowedFileExtensions.split(' ');
99+
}
100+
97101
this.onChange = this.onChange.bind(this);
98102
this.clearElement = this.clearElement.bind(this);
99103
this.onFileReady = this.onFileReady.bind(this);
@@ -166,7 +170,7 @@ Dropify.prototype.createElements = function()
166170

167171
var defaultFile = this.settings.defaultFile || '';
168172

169-
if (defaultFile.trim() != '') {
173+
if (defaultFile.trim() !== '') {
170174
this.file.name = this.cleanFilename(defaultFile);
171175
this.setPreview(this.isImage(), defaultFile);
172176
}
@@ -194,7 +198,7 @@ Dropify.prototype.readFile = function(input)
194198
this.checkFileSize();
195199
this.isFileExtensionAllowed();
196200

197-
if (this.isImage() && this.file.size < this.maxFileSizePreview) {
201+
if (this.isImage() && this.file.size < this.sizeToByte(this.settings.maxFileSizePreview)) {
198202
this.input.on('dropify.fileReady', this.onFileReady);
199203
reader.readAsDataURL(file);
200204
reader.onload = function(_file) {
@@ -217,14 +221,15 @@ Dropify.prototype.readFile = function(input)
217221
* On file ready to show
218222
*
219223
* @param {Event} event
224+
* @param {Bool} previewable
220225
* @param {String} src
221226
*/
222-
Dropify.prototype.onFileReady = function(event, showImage, src)
227+
Dropify.prototype.onFileReady = function(event, previewable, src)
223228
{
224229
this.input.off('dropify.fileReady', this.onFileReady);
225230

226231
if (this.errorsEvent.errors.length === 0) {
227-
this.setPreview(showImage, src);
232+
this.setPreview(previewable, src);
228233
} else {
229234
this.input.trigger(this.errorsEvent, [this]);
230235
for (var i = this.errorsEvent.errors.length - 1; i >= 0; i--) {
@@ -278,15 +283,15 @@ Dropify.prototype.setFileDimensions = function(width, height)
278283
*
279284
* @param {String} src
280285
*/
281-
Dropify.prototype.setPreview = function(showImage, src)
286+
Dropify.prototype.setPreview = function(previewable, src)
282287
{
283288
this.wrapper.removeClass('has-error').addClass('has-preview');
284289
this.filenameWrapper.children('.dropify-filename-inner').html(this.file.name);
285290
var render = this.preview.children('.dropify-render');
286291

287292
this.hideLoader();
288293

289-
if (showImage === true) {
294+
if (previewable === true) {
290295
var imgTag = $('<img />').attr('src', src);
291296

292297
if (this.settings.height) {
@@ -329,7 +334,7 @@ Dropify.prototype.cleanFilename = function(src)
329334
filename = src.split('/').pop();
330335
}
331336

332-
return src != "" ? filename : '';
337+
return src !== "" ? filename : '';
333338
};
334339

335340
/**
@@ -385,9 +390,9 @@ Dropify.prototype.setContainerSize = function()
385390
*/
386391
Dropify.prototype.isTouchDevice = function()
387392
{
388-
return (('ontouchstart' in window)
389-
|| (navigator.MaxTouchPoints > 0)
390-
|| (navigator.msMaxTouchPoints > 0));
393+
return (('ontouchstart' in window) ||
394+
(navigator.MaxTouchPoints > 0) ||
395+
(navigator.msMaxTouchPoints > 0));
391396
};
392397

393398
/**
@@ -407,7 +412,7 @@ Dropify.prototype.getFileType = function()
407412
*/
408413
Dropify.prototype.isImage = function()
409414
{
410-
if (this.imgFileExtensions.indexOf(this.getFileType()) != "-1") {
415+
if (this.settings.imgFileExtensions.indexOf(this.getFileType()) != "-1") {
411416
return true;
412417
}
413418

@@ -421,15 +426,14 @@ Dropify.prototype.isImage = function()
421426
*/
422427
Dropify.prototype.isFileExtensionAllowed = function () {
423428

424-
if (this.settings.allowedFileExtensions.indexOf('*') != "-1") {
425-
return true;
426-
} else if (this.settings.allowedFileExtensions.indexOf(this.getFileType()) != "-1") {
429+
if (this.settings.allowedFileExtensions.indexOf('*') != "-1" || 
430+
this.settings.allowedFileExtensions.indexOf(this.getFileType()) != "-1") {
427431
return true;
428432
}
429433
this.pushError("fileExtension");
430434

431435
return false;
432-
}
436+
};
433437

434438
/**
435439
* Translate messages if needed.
@@ -448,7 +452,7 @@ Dropify.prototype.translateMessages = function()
448452
*/
449453
Dropify.prototype.checkFileSize = function()
450454
{
451-
if (this.maxFileSizeToByte() !== 0 && this.file.size > this.maxFileSizeToByte()) {
455+
if (this.sizeToByte(this.settings.maxFileSize) !== 0 && this.file.size > this.sizeToByte(this.settings.maxFileSize)) {
452456
this.pushError("fileSize");
453457
}
454458
};
@@ -458,22 +462,22 @@ Dropify.prototype.checkFileSize = function()
458462
*
459463
* @return {Int} value
460464
*/
461-
Dropify.prototype.maxFileSizeToByte = function()
465+
Dropify.prototype.sizeToByte = function(size)
462466
{
463467
var value = 0;
464468

465-
if (this.settings.maxFileSize !== 0) {
466-
var unit = this.settings.maxFileSize.slice(-1).toUpperCase(),
469+
if (size !== 0) {
470+
var unit = size.slice(-1).toUpperCase(),
467471
kb = 1024,
468472
mb = kb * 1024,
469473
gb = mb * 1024;
470474

471475
if (unit === 'K') {
472-
value = parseFloat(this.settings.maxFileSize) * kb;
476+
value = parseFloat(size) * kb;
473477
} else if (unit === 'M') {
474-
value = parseFloat(this.settings.maxFileSize) * mb;
478+
value = parseFloat(size) * mb;
475479
} else if (unit === 'G') {
476-
value = parseFloat(this.settings.maxFileSize) * gb;
480+
value = parseFloat(size) * gb;
477481
}
478482
}
479483

dist/js/dropify.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)