1/* =========================================================== 2 * bootstrap-fileupload.js j2 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 21!function ($) { 22 23 "use strict"; // jshint ;_ 24 25 /* FILEUPLOAD PUBLIC CLASS DEFINITION 26 * ================================= */ 27 28 var Fileupload = function (element, options) { 29 this.$element = $(element) 30 this.type = this.$element.data('uploadtype') || (this.$element.find('.img-thumbnail').length > 0 ? "image" : "file") 31 32 this.$input = this.$element.find(':file') 33 if (this.$input.length === 0) return 34 35 // this.name = this.$input.attr('name') || options.name 36 37 this.$hidden = this.$element.find('input[type=hidden][name="'+this.name+'"]') 38 if (this.$hidden.length === 0) { 39 this.$hidden = $('<input type="hidden" />') 40 this.$element.prepend(this.$hidden) 41 } 42 43 this.$preview = this.$element.find('.fileupload-preview') 44 var height = this.$preview.css('height') 45 if (this.$preview.css('display') != 'inline' && height != '0px' && height != 'none') this.$preview.css('line-height', height) 46 47 this.original = { 48 'exists': this.$element.hasClass('fileupload-exists'), 49 'preview': this.$preview.html(), 50 'hiddenVal': this.$hidden.val() 51 } 52 53 this.$remove = this.$element.find('[data-dismiss="fileupload"]') 54 55 this.$element.find('[data-trigger="fileupload"]').on('click.fileupload', $.proxy(this.trigger, this)) 56 57 this.listen() 58 } 59 60 Fileupload.prototype = { 61 62 listen: function() { 63 this.$input.on('change.fileupload', $.proxy(this.change, this)) 64 $(this.$input[0].form).on('reset.fileupload', $.proxy(this.reset, this)) 65 if (this.$remove) this.$remove.on('click.fileupload', $.proxy(this.clear, this)) 66 }, 67 68 change: function(e, invoked) { 69 if (invoked === 'clear') return 70 71 var file = e.target.files !== undefined ? e.target.files[0] : (e.target.value ? { name: e.target.value.replace(/^.+\\/, '') } : null) 72 73 if (!file) { 74 this.clear() 75 return 76 } 77 78 this.$hidden.val('') 79 this.$hidden.attr('name', '') 80 this.$input.attr('name', this.name) 81 82 if (this.type === "image" && this.$preview.length > 0 && (typeof file.type !== "undefined" ? file.type.match('image.*') : file.name.match(/\.(gif|png|jpe?g)$/i)) && typeof FileReader !== "undefined") { 83 var reader = new FileReader() 84 var preview = this.$preview 85 var element = this.$element 86 87 console.log(preview.css('max-height')); 88 89 reader.onload = function(e) { 90 preview.html('<img src="' + e.target.result + '" class="img-responsive" />') 91 element.addClass('fileupload-exists').removeClass('fileupload-new') 92 $(element).trigger('imagechange') 93 } 94 95 reader.readAsDataURL(file) 96 } else { 97 this.$preview.text(file.name) 98 this.$element.addClass('fileupload-exists').removeClass('fileupload-new') 99 } 100 }, 101 102 clear: function(e) { 103 this.$hidden.val('') 104 this.$hidden.attr('name', this.name) 105 this.$input.attr('name', '') 106 107 //ie8+ doesn't support changing the value of input with type=file so clone instead 108 if (navigator.userAgent.match(/msie/i)){ 109 var inputClone = this.$input.clone(true); 110 this.$input.after(inputClone); 111 this.$input.remove(); 112 this.$input = inputClone; 113 }else{ 114 this.$input.val('') 115 } 116 117 this.$preview.html('') 118 this.$element.addClass('fileupload-new').removeClass('fileupload-exists') 119 120 if (e) { 121 this.$input.trigger('change', [ 'clear' ]) 122 e.preventDefault() 123 } 124 }, 125 126 reset: function(e) { 127 this.clear() 128 129 this.$hidden.val(this.original.hiddenVal) 130 this.$preview.html(this.original.preview) 131 132 if (this.original.exists) this.$element.addClass('fileupload-exists').removeClass('fileupload-new') 133 else this.$element.addClass('fileupload-new').removeClass('fileupload-exists') 134 }, 135 136 trigger: function(e) { 137 this.$input.trigger('click') 138 e.preventDefault() 139 } 140 } 141 142 143 /* FILEUPLOAD PLUGIN DEFINITION 144 * =========================== */ 145 146 $.fn.fileupload = function (options) { 147 return this.each(function () { 148 var $this = $(this) 149 , data = $this.data('fileupload') 150 if (!data) $this.data('fileupload', (data = new Fileupload(this, options))) 151 if (typeof options == 'string') data[options]() 152 }) 153 } 154 155 $.fn.fileupload.Constructor = Fileupload 156 157 158 /* FILEUPLOAD DATA-API 159 * ================== */ 160
161 $(document).on('click.fileupload.data-api', '[data-provides="fileupload"]', function (e) { 162 var $this = $(this) 163 if ($this.data('fileupload')) return 164 $this.fileupload($this.data()) 165 166 var $target = $(e.target).closest('[data-dismiss="fileupload"],[data-trigger="fileupload"]'); 167 if ($target.length > 0) { 168 $target.trigger('click.fileupload') 169 e.preventDefault() 170 } 171 }) 172 173}(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.