-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-file-uploader.js
114 lines (83 loc) · 3.09 KB
/
bootstrap-file-uploader.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
/* ========================================================================
* Bootstrap Custom File Uploader Input v0.0.1
* https://github.com/diaswrd/bootstrap-file-uploader
* ========================================================================
* Copyright (c) 2013 William Dias
*
* Licensed under the MIT License (MIT);
* https://github.com/diaswrd/bootstrap-file-uploader/blob/master/LICENSE
* ======================================================================== */
(function($) {
"use strict";
//- Public class definition
var FileUploader = function(element, options) {
var defaults = {
buttonLabel: 'Upload',
buttonClass: '',
hasTextInput: true,
elementToAttachFileName: null,
_template: null,
_data: {}
};
this.$input = $(element);
this.options = $.extend({}, defaults, options);
this.build();
};
FileUploader.prototype = {
build: function () {
this.$input.hide();
this.$el = $(this.getMarkup());
this.$input.after(this.$el);
this.$el.find('input').css({
float: 'left'
})
var btnCls = this.options.buttonClass || 'btn';
this.$el.on('click', 'input, .' + btnCls, $.proxy(this.click, this));
},
click: function (ev) {
ev.preventDefault();
this.$input.click();
var elInput = this.$el.find('input');
this.$input.on('change', function() {
var file = $(this).val();
elInput.val(file);
});
},
getMarkup: function () {
var markup = '';
if (this.options._template) {
this.checkUnderscoreJs();
_.extend(this.options._data, {
buttonLabel: this.options.buttonLabel,
buttonClass: this.options.buttonClass
});
var tpl = $(this.options._template).html()
markup = _.template(tpl, {
data: this.options._data
});
return markup;
}
markup = "<div class='bootstrap-file-uploader row-fluid'>";
if (this.options.hasTextInput) {
markup += "<input type='text' class='uploader-input span8'></input>";
}
markup += "<button class='btn "+ this.options.buttonClass +" span4'>";
markup += this.options.buttonLabel;
markup += "</button>";
markup += "</div>";
return markup;
},
checkUnderscoreJs: function() {
if (typeof(_) == "undefined") {
throw("You defined an _template but underscore.js is missing.");
}
}
};
//- Plugin definition
$.fn.fileUploader = function (options) {
return this.each(function() {
var uploader = new FileUploader(this, options);
});
};
$.fn.fileUploader.Constructor = FileUploader;
})(window.jQuery);