Path: blob/master/web-gui/buildyourownbotnet/assets/js/fileinput.js
1292 views
/* ===========================================================1* Bootstrap: fileinput.js v3.0.0-p72* http://jasny.github.com/bootstrap/javascript.html#fileinput3* ===========================================================4* Copyright 2012 Jasny BV, Netherlands.5*6* Licensed under the Apache License, Version 2.0 (the "License")7* you may not use this file except in compliance with the License.8* You may obtain a copy of the License at9*10* http://www.apache.org/licenses/LICENSE-2.011*12* Unless required by applicable law or agreed to in writing, software13* distributed under the License is distributed on an "AS IS" BASIS,14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15* See the License for the specific language governing permissions and16* limitations under the License.17* ========================================================== */1819+function ($) { "use strict";2021var isIE = window.navigator.appName == 'Microsoft Internet Explorer'2223// FILEUPLOAD PUBLIC CLASS DEFINITION24// =================================2526var Fileupload = function (element, options) {27this.$element = $(element)2829this.$input = this.$element.find(':file')30if (this.$input.length === 0) return3132this.name = this.$input.attr('name') || options.name3334this.$hidden = this.$element.find('input[type=hidden][name="'+this.name+'"]')35if (this.$hidden.length === 0) {36this.$hidden = $('<input type="hidden" />')37this.$element.prepend(this.$hidden)38}3940this.$preview = this.$element.find('.fileinput-preview')41var height = this.$preview.css('height')42if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none') this.$preview.css('line-height', height)4344this.original = {45exists: this.$element.hasClass('fileinput-exists'),46preview: this.$preview.html(),47hiddenVal: this.$hidden.val()48}4950this.listen()51}5253Fileupload.prototype.listen = function() {54this.$input.on('change.bs.fileinput', $.proxy(this.change, this))55$(this.$input[0].form).on('reset.bs.fileinput', $.proxy(this.reset, this))5657this.$element.find('[data-trigger="fileinput"]').on('click.bs.fileinput', $.proxy(this.trigger, this))58this.$element.find('[data-dismiss="fileinput"]').on('click.bs.fileinput', $.proxy(this.clear, this))59},6061Fileupload.prototype.change = function(e) {62if (e.target.files === undefined) e.target.files = e.target && e.target.value ? [ {name: e.target.value.replace(/^.+\\/, '')} ] : []63if (e.target.files.length === 0) return6465this.$hidden.val('')66this.$hidden.attr('name', '')67this.$input.attr('name', this.name)6869var file = e.target.files[0]7071if (this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match('image.*') : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") {72var reader = new FileReader()73var preview = this.$preview74var element = this.$element7576reader.onload = function(re) {77var $img = $('<img>').attr('src', re.target.result)78e.target.files[0].result = re.target.result7980element.find('.fileinput-filename').text(file.name)8182// if parent has max-height, using `(max-)height: 100%` on child doesn't take padding and border into account83if (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))8485preview.html($img)86element.addClass('fileinput-exists').removeClass('fileinput-new')8788element.trigger('change.bs.fileinput', e.target.files)89}9091reader.readAsDataURL(file)92} else {93this.$element.find('.fileinput-filename').text(file.name)94this.$preview.text(file.name)9596this.$element.addClass('fileinput-exists').removeClass('fileinput-new')9798this.$element.trigger('change.bs.fileinput')99}100},101102Fileupload.prototype.clear = function(e) {103if (e) e.preventDefault()104105this.$hidden.val('')106this.$hidden.attr('name', this.name)107this.$input.attr('name', '')108109//ie8+ doesn't support changing the value of input with type=file so clone instead110if (isIE) {111var inputClone = this.$input.clone(true);112this.$input.after(inputClone);113this.$input.remove();114this.$input = inputClone;115} else {116this.$input.val('')117}118119this.$preview.html('')120this.$element.find('.fileinput-filename').text('')121this.$element.addClass('fileinput-new').removeClass('fileinput-exists')122123if (e !== false) {124this.$input.trigger('change')125this.$element.trigger('clear.bs.fileinput')126}127},128129Fileupload.prototype.reset = function() {130this.clear(false)131132this.$hidden.val(this.original.hiddenVal)133this.$preview.html(this.original.preview)134this.$element.find('.fileinput-filename').text('')135136if (this.original.exists) this.$element.addClass('fileinput-exists').removeClass('fileinput-new')137else this.$element.addClass('fileinput-new').removeClass('fileinput-exists')138139this.$element.trigger('reset.bs.fileinput')140},141142Fileupload.prototype.trigger = function(e) {143this.$input.trigger('click')144e.preventDefault()145}146147148// FILEUPLOAD PLUGIN DEFINITION149// ===========================150151$.fn.fileinput = function (options) {152return this.each(function () {153var $this = $(this)154, data = $this.data('fileinput')155if (!data) $this.data('fileinput', (data = new Fileupload(this, options)))156if (typeof options == 'string') data[options]()157})158}159160$.fn.fileinput.Constructor = Fileupload161162163// FILEUPLOAD DATA-API164// ==================165166$(document).on('click.fileinput.data-api', '[data-provides="fileinput"]', function (e) {167var $this = $(this)168if ($this.data('fileinput')) return169$this.fileinput($this.data())170171var $target = $(e.target).closest('[data-dismiss="fileinput"],[data-trigger="fileinput"]');172if ($target.length > 0) {173e.preventDefault()174$target.trigger('click.bs.fileinput')175}176})177178}(window.jQuery);179180181