fileinput.min.js 9.6 KB

123456
  1. /*!
  2. * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
  3. * @version 1.6.0
  4. * bootstrap-fileinput
  5. * For more JQuery Plugins visit http://plugins.krajee.com
  6. */!function(e){var i='{preview}\n<div class="input-group {class}">\n {caption}\n <div class="input-group-btn">\n {remove}\n {upload}\n {browse}\n </div>\n</div>',n="{preview}\n{remove}\n{upload}\n{browse}\n",t='<div class="file-preview {class}">\n <div class="file-preview-status text-center text-success"></div>\n <div class="close fileinput-remove text-right">&times;</div>\n <div class="file-preview-thumbnails"></div>\n <div class="clearfix"></div></div>',a='<div class="form-control file-caption {class}">\n <span class="glyphicon glyphicon-file"></span> <span class="file-caption-name"></span>\n</div>',l='<div id="{id}" class="modal fade">\n <div class="modal-dialog modal-lg">\n <div class="modal-content">\n <div class="modal-header">\n <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\n <h3 class="modal-title">Detailed Preview <small>{title}</small></h3>\n </div>\n <div class="modal-body">\n <textarea class="form-control" style="font-family:Monaco,Consolas,monospace; height: {height}px;" readonly>{body}</textarea>\n </div>\n </div>\n </div>\n</div>\n',r=function(i,n){return null===i||void 0===i||i==[]||""===i||n&&""===e.trim(i)},o=Array.isArray||function(e){return"[object Array]"===Object.prototype.toString.call(e)},s=function(i,n,t){return r(i)||r(i[n])?t:e(i[n])},p=function(e,i){return"undefined"!=typeof e?e.match("image.*"):i.match(/\.(gif|png|jpe?g)$/i)},d=function(e,i){return"undefined"!=typeof e?e.match("text.*"):i.match(/\.(txt|md|csv|htm|html|php|ini)$/i)},c=function(){return Math.round((new Date).getTime()+100*Math.random())},v=function(i,n){this.$element=e(i),this.init(n),this.listen()};v.prototype={constructor:v,init:function(t){var a=this;a.showCaption=t.showCaption,a.showPreview=t.showPreview,a.initialPreview=t.initialPreview,a.initialCaption=t.initialCaption,a.showRemove=t.showRemove,a.showUpload=t.showUpload,a.captionClass=t.captionClass,a.previewClass=t.previewClass,a.mainClass=t.mainClass,a.mainTemplate=r(t.mainTemplate)?a.showCaption?i:n:t.mainTemplate,a.previewTemplate=a.showPreview?t.previewTemplate:"",a.captionTemplate=t.captionTemplate,a.browseLabel=t.browseLabel,a.browseIcon=t.browseIcon,a.browseClass=t.browseClass,a.removeLabel=t.removeLabel,a.removeIcon=t.removeIcon,a.removeClass=t.removeClass,a.uploadLabel=t.uploadLabel,a.uploadIcon=t.uploadIcon,a.uploadClass=t.uploadClass,a.uploadUrl=t.uploadUrl,a.msgLoading=t.msgLoading,a.msgProgress=t.msgProgress,a.msgSelected=t.msgSelected,a.previewFileType=t.previewFileType,a.wrapTextLength=t.wrapTextLength,a.wrapIndicator=t.wrapIndicator,a.isDisabled=a.$element.attr("disabled")||a.$element.attr("readonly"),r(a.$element.attr("id"))&&a.$element.attr("id",c()),"undefined"==typeof a.$container?a.$container=a.createContainer():a.refreshContainer(),a.$captionContainer=s(t,"elCaptionContainer",a.$container.find(".file-caption")),a.$caption=s(t,"elCaptionText",a.$container.find(".file-caption-name")),a.$previewContainer=s(t,"elPreviewContainer",a.$container.find(".file-preview")),a.$preview=s(t,"elPreviewImage",a.$container.find(".file-preview-thumbnails")),a.$previewStatus=s(t,"elPreviewStatus",a.$container.find(".file-preview-status")),a.initPreview(),a.$name=a.$element.attr("name")||t.name,a.$hidden=a.$container.find('input[type=hidden][name="'+a.$name+'"]'),0===a.$hidden.length&&(a.$hidden=e('<input type="hidden" />'),a.$container.prepend(a.$hidden)),a.original={preview:a.$preview.html(),caption:a.$caption.html(),hiddenVal:a.$hidden.val()},this.options=t},listen:function(){var i=this;i.$element.on("change",e.proxy(i.change,i)),e(i.$element[0].form).on("reset",e.proxy(i.reset,i)),i.$container.find(".fileinput-remove").on("click",e.proxy(i.clear,i))},refresh:function(i){var n=this,t=arguments.length?e.extend(n.options,i):n.options;n.init(t)},initPreview:function(){var e=this,i="",n=e.initialPreview,t=e.initialPreview.length,a=e.initialCaption.length,l=a>0?e.initialCaption:t+" file selected";if(o(n)&&t>0){for(var r=0;t>r;r++)i+='<div class="file-preview-frame">'+n[r]+"</div>\n";e.$preview.html(i),t>1&&0==a&&(l=t+" files selected")}else{if(!(t>0))return;i='<div class="file-preview-frame">'+n+"</div>"}e.$preview.html(i),e.$caption.html(l),e.$container.removeClass("file-input-new")},clear:function(e){var i=this;e&&e.preventDefault(),i.$hidden.val(""),i.$hidden.attr("name",i.name),i.$element.attr("name",""),i.$element.val(""),e!==!1&&(i.$element.trigger("change"),i.$element.trigger("fileclear")),i.$preview.html(""),i.$caption.html(""),i.$container.removeClass("file-input-new").addClass("file-input-new")},reset:function(){var e=this;e.clear(!1),e.$hidden.val(e.original.hiddenVal),e.$preview.html(e.original.preview),e.$caption.html(e.original.caption),e.$container.find(".fileinput-filename").text(""),e.$element.trigger("filereset"),e.initialPreview.length>0&&e.$container.removeClass("file-input-new")},change:function(e){var i,n=this,t=n.$element,a=t.get(0).files,r=a?a.length:1,o=t.val().replace(/\\/g,"/").replace(/.*\//,""),s=n.$preview,v=n.$previewContainer,m=n.$previewStatus,f=n.msgLoading,u=n.msgProgress,h=n.msgSelected,w=n.previewFileType,g=parseInt(n.wrapTextLength),b=n.wrapIndicator;if(i=void 0===e.target.files?e.target&&e.target.value?[{name:e.target.value.replace(/^.+\\/,"")}]:[]:e.target.files,0!==i.length){s.html("");for(var $=i.length,n=n,C=0;$>C;C++)!function(e){var i=e.name,n=p(e.type,e.name),t=d(e.type,e.name);if(s.length>0&&("any"==w?n||t:"text"==w?t:n)&&"undefined"!=typeof FileReader){var a=new FileReader;m.html(f),v.addClass("loading"),a.onload=function(e){var n="",a="";if(t){var r=e.target.result;if(r.length>g){var o=c(),p=.75*window.innerHeight,a=l.replace("{id}",o).replace("{title}",i).replace("{body}",r).replace("{height}",p);b=b.replace("{title}",i).replace("{dialog}","$('#"+o+"').modal('show')"),r=r.substring(0,g-1)+b}n='<div class="file-preview-frame"><div class="file-preview-text" title="'+i+'">'+r+"</div></div>"+a}else n='<div class="file-preview-frame"><img src="'+e.target.result+'" class="file-preview-image" title="'+i+'" alt="'+i+'"></div>';s.append("\n"+n),C>=$-1&&(v.removeClass("loading"),m.html(""))},a.onprogress=function(i){if(i.lengthComputable){var n=parseInt(i.loaded/i.total*100,10),t=u.replace("{percent}",n).replace("{file}",e.name);m.html(t)}},t?a.readAsText(e):a.readAsDataURL(e)}else s.append('\n<div class="file-preview-frame"><div class="file-preview-other"><h2><i class="glyphicon glyphicon-file"></i></h2>'+i+"</div></div>")}(i[C]);var y=r>1?h.replace("{n}",r):o;n.$caption.html(y),n.$container.removeClass("file-input-new"),t.trigger("fileselect",[r,o])}},createContainer:function(){var i=this,n=e(document.createElement("div")).attr({"class":"file-input file-input-new"}).html(i.renderMain());return i.$element.before(n),n.find(".btn-file").append(i.$element),n},refreshContainer:function(){var e=this;e.$container.before(e.$element),e.$container.html(e.renderMain()),e.$container.find(".btn-file").append(e.$element)},renderMain:function(){var e=this,i=e.previewTemplate.replace("{class}",e.previewClass),n=e.isDisabled?e.captionClass+" file-caption-disabled":e.captionClass,t=e.captionTemplate.replace("{class}",n);return e.mainTemplate.replace("{class}",e.mainClass).replace("{preview}",i).replace("{caption}",t).replace("{upload}",e.renderUpload()).replace("{remove}",e.renderRemove()).replace("{browse}",e.renderBrowse())},renderBrowse:function(){var e=this,i=e.browseClass+" btn-file",n="";return e.isDisabled&&(n=" disabled "),'<div class="'+i+'"'+n+"> "+e.browseIcon+e.browseLabel+" </div>"},renderRemove:function(){var e=this,i=e.removeClass+" fileinput-remove fileinput-remove-button",n="";return e.showRemove?(e.isDisabled&&(n=" disabled "),'<button type="button" class="'+i+'"'+n+">"+e.removeIcon+e.removeLabel+"</button>"):""},renderUpload:function(){var e=this,i="",n="";return e.showUpload?(e.isDisabled&&(n=" disabled "),i=r(e.uploadUrl)?'<button type="submit" class="'+e.uploadClass+'"'+n+">"+e.uploadIcon+e.uploadLabel+"</button>":'<a href="'+e.uploadUrl+'" class="'+e.uploadClass+'"'+n+">"+e.uploadIcon+e.uploadLabel+"</a>"):""}},e.fn.fileinput=function(i){return this.each(function(){var n=e(this),t=n.data("fileinput");t||n.data("fileinput",t=new v(this,i)),"string"==typeof i&&t[i]()})},e.fn.fileinput=function(i){var n=Array.apply(null,arguments);return n.shift(),this.each(function(){var t=e(this),a=t.data("fileinput"),l="object"==typeof i&&i;a||t.data("fileinput",a=new v(this,e.extend({},e.fn.fileinput.defaults,l,e(this).data()))),"string"==typeof i&&a[i].apply(a,n)})},e.fn.fileinput.defaults={showCaption:!0,showPreview:!0,showRemove:!0,showUpload:!0,captionClass:"",previewClass:"",mainClass:"",mainTemplate:null,initialPreview:"",initialCaption:"",previewTemplate:t,captionTemplate:a,browseLabel:"Browse &hellip;",browseIcon:'<i class="glyphicon glyphicon-folder-open"></i> &nbsp;',browseClass:"btn btn-primary",removeLabel:"Remove",removeIcon:'<i class="glyphicon glyphicon-ban-circle"></i> ',removeClass:"btn btn-default",uploadLabel:"Upload",uploadIcon:'<i class="glyphicon glyphicon-upload"></i> ',uploadClass:"btn btn-default",uploadUrl:null,msgLoading:"Loading &hellip;",msgProgress:"Loaded {percent}% of {file}",msgSelected:"{n} files selected",previewFileType:"image",wrapTextLength:250,wrapIndicator:' <span class="wrap-indicator" title="{title}" onclick="{dialog}">[&hellip;]</span>',elCaptionContainer:null,elCaptionText:null,elPreviewContainer:null,elPreviewImage:null,elPreviewStatus:null},e(function(){var i=e("input.file[type=file]");i.length>0&&i.fileinput()})}(window.jQuery);