1/* =========================================================== 2 * bootstrap-fileupload.js j1a 3 * http://jasny.github.com/bootstrap/javascript.html#fileupload 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 ($) { 21 22 "use strict"; // jshint ;_ 23 24 /* INPUTMASK PUBLIC CLASS DEFINITION 25 * ================================= */ 26 27 var Fileupload = function (element, options) { 28 this.$element = $(element) 29 this.type = this.$element.data('uploadtype') || (this.$element.find('.img-thumbnail').length > 0 ? "image" : "file") 30 31 this.$input = this.$element.find(':file') 32 if (this.$input.length === 0) return 33 34 this.name = this.$input.attr('name') || options.name 35 36 this.$hidden = this.$element.find(':hidden[name="'+this.name+'"]') 37 if (this.$hidden.length === 0) { 38 this.$hidden = $('<input type="hidden" />') 39 this.$element.prepend(this.$hidden) 40 } 41 42 this.$preview = this.$element.find('.fileupload-preview') 43 var height = this.$preview.css('height') 44 if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none') this.$preview.css('line-height', height) 45 46 this.$remove = this.$element.find('[data-dismiss="fileupload"]') 47 48 this.listen() 49 } 50 51 Fileupload.prototype = { 52 53 listen: function() { 54 this.$input.on('change.fileupload', $.proxy(this.change, this)) 55 if (this.$remove) this.$remove.on('click.fileupload', $.proxy(this.clear, this)) 56 }, 57 58 change: function(e, invoked) { 59 var file = e.target.files !== undefined ? e.target.files[0] : { name: e.target.value.replace(/^.+\\/, '') } 60 if (!file || invoked === 'clear') return 61 62 this.$hidden.val('') 63 this.$hidden.attr('name', '') 64 this.$input.attr('name', this.name) 65 66 if (this.type === "image" && this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match('image.*') : file.name.match('\\.(gif|png|jpe?g)$')) && typeof FileReader !== "undefined") { 67 var reader = new FileReader() 68 var preview = this.$preview 69 var element = this.$element 70 71 reader.onload = function(e) { 72 preview.html('<img src="' + e.target.result + '" ' + (preview.css('max-height') != 'none' ? 'style="max-height: ' + preview.css('max-height') + ';"' : '') + ' />') 73 element.addClass('fileupload-exists').removeClass('fileupload-new') 74 } 75 76 reader.readAsDataURL(file) 77 } else { 78 this.$preview.text(file.name) 79 this.$element.addClass('fileupload-exists').removeClass('fileupload-new') 80 } 81 }, 82 83 clear: function(e) { 84 this.$hidden.val('') 85 this.$hidden.attr('name', this.name) 86 this.$input.attr('name', '') 87 88 this.$preview.html('') 89 this.$element.addClass('fileupload-new').removeClass('fileupload-exists') 90 91 this.$input.trigger('change', [ 'clear' ]) 92 93 e.preventDefault() 94 return false 95 } 96 } 97 98 99 /* INPUTMASK PLUGIN DEFINITION 100 * =========================== */ 101 102 $.fn.fileupload = function (options) { 103 return this.each(function () { 104 var $this = $(this) 105 , data = $this.data('fileupload') 106 if (!data) $this.data('fileupload', (data = new Fileupload(this, options))) 107 }) 108 } 109 110 $.fn.fileupload.Constructor = Fileupload 111 112 113 /* INPUTMASK DATA-API 114 * ================== */ 115 116 $(function () { 117 $('body').on('click.fileupload.data-api', '[data-provides="fileupload"]', function (e) { 118 var $this = $(this) 119 if ($this.data('fileupload')) return 120 $this.fileupload($this.data()) 121
122 if ($(e.target).data('dismiss') == 'fileupload') $(e.target).trigger('click.fileupload') 123 }) 124 }) 125 126}(window.jQuery)
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.