Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/fileinput.js
1292 views
1
/* ===========================================================
2
* Bootstrap: fileinput.js v3.0.0-p7
3
* http://jasny.github.com/bootstrap/javascript.html#fileinput
4
* ===========================================================
5
* Copyright 2012 Jasny BV, Netherlands.
6
*
7
* Licensed under the Apache License, Version 2.0 (the "License")
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
10
*
11
* http://www.apache.org/licenses/LICENSE-2.0
12
*
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
18
* ========================================================== */
19
20
+function ($) { "use strict";
21
22
var isIE = window.navigator.appName == 'Microsoft Internet Explorer'
23
24
// FILEUPLOAD PUBLIC CLASS DEFINITION
25
// =================================
26
27
var Fileupload = function (element, options) {
28
this.$element = $(element)
29
30
this.$input = this.$element.find(':file')
31
if (this.$input.length === 0) return
32
33
this.name = this.$input.attr('name') || options.name
34
35
this.$hidden = this.$element.find('input[type=hidden][name="'+this.name+'"]')
36
if (this.$hidden.length === 0) {
37
this.$hidden = $('<input type="hidden" />')
38
this.$element.prepend(this.$hidden)
39
}
40
41
this.$preview = this.$element.find('.fileinput-preview')
42
var height = this.$preview.css('height')
43
if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none') this.$preview.css('line-height', height)
44
45
this.original = {
46
exists: this.$element.hasClass('fileinput-exists'),
47
preview: this.$preview.html(),
48
hiddenVal: this.$hidden.val()
49
}
50
51
this.listen()
52
}
53
54
Fileupload.prototype.listen = function() {
55
this.$input.on('change.bs.fileinput', $.proxy(this.change, this))
56
$(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))
57
58
this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))
59
this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))
60
},
61
62
Fileupload.prototype.change = function(e) {
63
if (e.target.files === undefined) e.target.files = e.target && e.target.value ? [ {name: e.target.value.replace(/^.+\\/, '')} ] : []
64
if (e.target.files.length === 0) return
65
66
this.$hidden.val('')
67
this.$hidden.attr('name', '')
68
this.$input.attr('name', this.name)
69
70
var file = e.target.files[0]
71
72
if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match('image.*') : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") {
73
var reader = new FileReader()
74
var preview = this.$preview
75
var element = this.$element
76
77
reader.onload = function(re) {
78
var $img = $('<img>').attr('src', re.target.result)
79
e.target.files[0].result = re.target.result
80
81
element.find('.fileinput-filename').text(file.name)
82
83
// if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account
84
if (preview.css('max-height') != 'none') $img.css('max-height', parseInt(preview.css('max-height'), 10) - parseInt(preview.css('padding-top'), 10) - parseInt(preview.css('padding-bottom'), 10) - parseInt(preview.css('border-top'), 10) - parseInt(preview.css('border-bottom'), 10))
85
86
preview.html($img)
87
element.addClass('fileinput-exists').removeClass('fileinput-new')
88
89
element.trigger('change.bs.fileinput', e.target.files)
90
}
91
92
reader.readAsDataURL(file)
93
} else {
94
this.$element.find('.fileinput-filename').text(file.name)
95
this.$preview.text(file.name)
96
97
this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
98
99
this.$element.trigger('change.bs.fileinput')
100
}
101
},
102
103
Fileupload.prototype.clear = function(e) {
104
if (e) e.preventDefault()
105
106
this.$hidden.val('')
107
this.$hidden.attr('name', this.name)
108
this.$input.attr('name', '')
109
110
//ie8+ doesn't support changing the value of input with type=file so clone instead
111
if (isIE) {
112
var inputClone = this.$input.clone(true);
113
this.$input.after(inputClone);
114
this.$input.remove();
115
this.$input = inputClone;
116
} else {
117
this.$input.val('')
118
}
119
120
this.$preview.html('')
121
this.$element.find('.fileinput-filename').text('')
122
this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
123
124
if (e !== false) {
125
this.$input.trigger('change')
126
this.$element.trigger('clear.bs.fileinput')
127
}
128
},
129
130
Fileupload.prototype.reset = function() {
131
this.clear(false)
132
133
this.$hidden.val(this.original.hiddenVal)
134
this.$preview.html(this.original.preview)
135
this.$element.find('.fileinput-filename').text('')
136
137
if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')
138
else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')
139
140
this.$element.trigger('reset.bs.fileinput')
141
},
142
143
Fileupload.prototype.trigger = function(e) {
144
this.$input.trigger('click')
145
e.preventDefault()
146
}
147
148
149
// FILEUPLOAD PLUGIN DEFINITION
150
// ===========================
151
152
$.fn.fileinput = function (options) {
153
return this.each(function () {
154
var $this = $(this)
155
, data = $this.data('fileinput')
156
if (!data) $this.data('fileinput', (data = new Fileupload(this, options)))
157
if (typeof options == 'string') data[options]()
158
})
159
}
160
161
$.fn.fileinput.Constructor = Fileupload
162
163
164
// FILEUPLOAD DATA-API
165
// ==================
166
167
$(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {
168
var $this = $(this)
169
if ($this.data('fileinput')) return
170
$this.fileinput($this.data())
171
172
var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');
173
if ($target.length > 0) {
174
e.preventDefault()
175
$target.trigger('click.bs.fileinput')
176
}
177
})
178
179
}(window.jQuery);
180
181