-
Notifications
You must be signed in to change notification settings - Fork 19
/
jquery.fileinput.js
81 lines (76 loc) · 3.22 KB
/
jquery.fileinput.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
/**
* {{= it.title}} v{{= it.version}}
*
* Copyright 2013, {{= it.author.name}} <{{= it.author.email}}>
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($) {
$.support.cssPseudoClasses = (function() {
try {
var input = $("<input type='checkbox' checked/>").appendTo('body');
var style = $('<style type="text/css" />').appendTo('head');
style.text("input:checked {width: 200px; display: none}");
var support = input.css('width') == "200px";
input.remove();
style.remove();
return support;
} catch (e) {
return false;
}
})();
$.fn.fileinput = function(replacement) {
var selector = this;
var replacementHtml = "<button class=\"fileinput\">Browse...</button>";
if (replacement) {
if (replacement instanceof jQuery)
replacementHtml = $(replacement).wrap("<div />").parent().html();
else
replacementHtml = replacement;
}
selector.each(function() {
var element = $(this);
element.wrap("<div class=\"fileinput-wrapper\" style=\"overflow:hidden; position: relative; display: inline-block;\" />");
element.parent().mousemove(function(e) {
var offL, offT, el = $(this);
offL = el.offset().left;
offT = el.offset().top;
el.find("input").css({
"left":e.pageX - offL - el.find("input[type=file]").width() + 30,
"top":e.pageY - offT - 10
})
});
element.attr("tabindex", "-1").css({filter: "alpha(opacity=0)", "-moz-opacity": 0, opacity: 0, position: "absolute", "z-index": -1});
element.before(replacementHtml);
element.prev().addClass("fileinput");
if (!$.support.cssPseudoClasses) {
element.css({"z-index":"auto", "cursor":"pointer"});
element.prev(".fileinput").css("z-index", -1);
element.removeAttr("tabindex");
element.prev(".fileinput").attr("tabindex", "-1");
element.hover(function() {
$(this).prev(".fileinput").addClass("hover");
}, function() {
$(this).prev(".fileinput").removeClass("hover");
}).focusin(function() {
$(this).prev(".fileinput").addClass("focus");
}).focusout(function() {
$(this).prev(".fileinput").removeClass("focus");
}).mousedown(function() {
$(this).prev(".fileinput").addClass("active");
}).mouseup(function() {
$(this).prev(".fileinput").removeClass("active");
});
} else {
element.prev(".fileinput").click(function() {
element.click();
});
element.prev(":submit.fileinput").click(function(event) {
event.preventDefault();
});
}
});
return selector;
};
})(jQuery);