bootstrap-fancyfile.js
4.75 KB
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
!function ($) {
"use strict"; // jshint ;_;
/* FANCYFILE CLASS DEFINITION
* ========================= */
var fancyfile = '[data-toggle=fancyfile]'
, FancyFile = function (element, options) {
var $el = $(element);
this.options = $.extend({}, $.fn.fancyfile.defaults, options);
this.makeFancy($el);
};
FancyFile.prototype = {
constructor: FancyFile,
makeFancy : function (element) {
var $fancy = $(this.options.container);
var $clone = element.clone( true );
var classes = $clone.attr('class');
var $aLink = $(this.options.fakeButton);
var $fakeInput = $(this.options.fakeInput);
var colorized = false;
if(classes) {
classes.split(' ').forEach(function(bc){
if(/input|span/i.test(bc)) {
$fancy.find('div').addClass(bc);
}
if(/primary|info|success|warning|danger|inverse/.test(bc)) {
colorized = true;
$aLink.addClass(bc);
}
});
}
var buttonText = element.attr('data-text');
if(buttonText) {
this.options.text = buttonText === 'false' ? '' : buttonText;
}
var buttonIcon = element.attr('data-icon');
if(buttonIcon) {
this.options.icon = buttonIcon === 'false' ? '' : '<i class="' + buttonIcon + '"></i>';
}
var buttonStyle = element.attr('data-style');
if(buttonStyle) {
colorized = buttonStyle === 'false' ? false : true ;
this.options.style = buttonStyle === 'false' ? '' : buttonStyle;
}
var icon = this.options.icon ? this.options.icon + ' ' : '';
$aLink.html(icon + this.options.text);
if(colorized || /primary|info|success|warning|danger|inverse/.test(this.options.style)) {
$aLink.find('i').addClass('icon-white');
}
if(this.options.style !== '') {
$aLink.addClass(this.options.style);
}
var buttonPlaceholder = element.attr('data-placeholder');
if(buttonPlaceholder) {
this.options.placeholder = buttonPlaceholder === 'false' ? '' : buttonPlaceholder;
}
$fakeInput.attr('placeholder', this.options.placeholder);
$fancy.insertBefore(element);
$clone.attr('type','file');
$fancy.append($clone);
$fancy.find('div').append($fakeInput).append($aLink);
var nW = $fancy.find('div').width();
$clone.width(nW);
element.remove();
$fakeInput.width((nW - $aLink.width()) - 41).height(20);
$aLink.height(20);
$clone.hover(function(e){
$(this).parent().find('.fake-input').addClass('active');
$(this).parent().find('.btn').addClass('active');
}, function(e){
$(this).parent().find('.fake-input').removeClass('active');
$(this).parent().find('.btn').removeClass('active');
});
$clone.on('change.fancyfile.data-api', FancyFile.prototype.change);
},
change: function (e) {
var file = this.files[0],
name = file.name;
$(this).parent().find('.fake-input').val(name);
}
};
/* FANCYFILE PLUGIN DEFINITION
* ========================== */
var old = $.fn.fancyfile;
$.fn.fancyfile = function (options) {
return this.each(function () {
var $this = $(this)
, data = $this.data('fancyfile');
if (!data) $this.data('fancyfile', (data = new FancyFile(this, options)));
if (typeof options === 'string') data[options].call($this);
});
};
$.fn.fancyfile.defaults = {
container : '<div class="fancy-file"><div class="fake-file"></div></div>',
fakeInput : '<input class="fake-input form-control" type="text" />',
fakeButton : '<button type="button" class="btn"></button>',
text : 'Select File',
icon : '<i class="icon-file glyphicon glyphicon-file"></i>',
style : '',
placeholder : 'Select File…'
};
$.fn.fancyfile.Constructor = FancyFile;
/* FANCYFILE NO CONFLICT
* ==================== */
$.fn.fancyfile.noConflict = function () {
$.fn.fancyfile = old;
return this;
};
/* DATA-API APPLY TO STANDARD FANCYFILE ELEMENTS
* ============== */
$(window).on('load', function () {
$(fancyfile).each(function () {
$(this).fancyfile();
});
});
}(window.jQuery);