-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-input.js
63 lines (48 loc) · 1.97 KB
/
file-input.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
define(['jquery'], function($) {
// Using ECMAScript 5 strict mode during development. By default r.js will ignore that.
'use strict';
var $fileInputs;
function add() {
$fileInputs = $('input[type=file].file-input');
$fileInputs.each(function() {
var $this = $(this),
buttonWord = 'Browse';
// Maybe some fields don't need to be standardized.
if ($this.data('file-input-enabled'))
return;
if (typeof $this.attr('title') != 'undefined')
buttonWord = $this.attr('title');
var $newInput = $this.clone().data('file-input-enabled', true),
$newEl = $('<a class="file-input-wrapper btn ' + $this.attr('class') + '"></a>')
.append(buttonWord)
.append($newInput);
$this.replaceWith($newEl);
$newInput.on('change', function(event) {
var $this = $(this);
// Remove any previous file names
$this.parent().next('.file-input-name').remove();
if ($this.prop('files') && $this.prop('files').length > 1) {
$this.parent().after('<span class="file-input-name">' + $this[0].files.length + ' files</span>');
}
else {
var fileName = $this.val();
if (!fileName && ($.browser && $.browser.msie))
fileName = event.target.value;
if (fileName = fileName.match(/[^\\/:*?"<>|\r\n]+$/))
fileName = fileName[0];
$this.parent().after('<span class="file-input-name">' + fileName + '</span>');
}
});
});
}
function remove(){
if (!$fileInputs || !$fileInputs.length)
return;
$fileInputs.off();
}
return {
init: add,
add: add,
remove: remove
};
});