"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/button"),n=require("primereact/messages"),o=require("primereact/progressbar"),l=require("primereact/utils"),s=require("primereact/ripple"),i=require("primereact/api");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=r(e);function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,o=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[o++]}},e:function(e){throw e},f:l}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,i=!0,r=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return i=e.done,e},e:function(e){r=!0,s=e},f:function(){try{i||null==n.return||n.return()}finally{if(r)throw s}}}}function B(e,t){if(e){if("string"==typeof e)return w(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?w(e,t):void 0}}function w(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,o=new Array(t);n0}},{key:"isImage",value:function(e){return/^image\//.test(e.type)}},{key:"chooseDisabled",value:function(){return this.props.disabled||this.props.fileLimit&&this.props.fileLimit<=this.state.files.length+this.uploadedFileCount}},{key:"uploadDisabled",value:function(){return this.props.disabled||!this.hasFiles()}},{key:"cancelDisabled",value:function(){return this.props.disabled||!this.hasFiles()}},{key:"chooseButtonLabel",value:function(){return this.props.chooseLabel||this.props.chooseOptions.label||i.localeOption("choose")}},{key:"uploadButtonLabel",value:function(){return this.props.uploadLabel||this.props.uploadOptions.label||i.localeOption("upload")}},{key:"cancelButtonLabel",value:function(){return this.props.cancelLabel||this.props.cancelOptions.label||i.localeOption("cancel")}},{key:"remove",value:function(e,t){this.clearInputElement();var n=h(this.state.files),o=this.state.files[t];n.splice(t,1),this.setState({files:n}),this.props.onRemove&&this.props.onRemove({originalEvent:e,file:o})}},{key:"clearInputElement",value:function(){this.fileInput&&(this.fileInput.value="")}},{key:"clearIEInput",value:function(){this.fileInput&&(this.duplicateIEEvent=!0,this.fileInput.value="")}},{key:"formatSize",value:function(e){if(0===e)return"0 B";var t=Math.floor(Math.log(e)/Math.log(1e3));return parseFloat((e/Math.pow(1e3,t)).toFixed(3))+" "+["B","KB","MB","GB","TB","PB","EB","ZB","YB"][t]}},{key:"onFileSelect",value:function(e){var t=this;if("drop"!==e.type&&this.isIE11()&&this.duplicateIEEvent)this.duplicateIEEvent=!1;else{this.setState({msgs:[]}),this.files=this.state.files?h(this.state.files):[];for(var n=e.dataTransfer?e.dataTransfer.files:e.target.files,o=0;o0&&(this.fileInput.style.display="none")}}},{key:"isFileSelected",value:function(e){var t,n=O(this.state.files);try{for(n.s();!(t=n.n()).done;){var o=t.value;if(o.name+o.type+o.size===e.name+e.type+e.size)return!0}}catch(e){n.e(e)}finally{n.f()}return!1}},{key:"isIE11",value:function(){return!!window.MSInputMethodContext&&!!document.documentMode}},{key:"validate",value:function(e){if(this.props.maxFileSize&&e.size>this.props.maxFileSize){var t={severity:"error",summary:this.props.invalidFileSizeMessageSummary.replace("{0}",e.name),detail:this.props.invalidFileSizeMessageDetail.replace("{0}",this.formatSize(this.props.maxFileSize))};return"advanced"===this.props.mode&&this.messagesUI.show(t),this.props.onValidationFail&&this.props.onValidationFail(e),!1}return!0}},{key:"upload",value:function(){var e=this;if(this.props.customUpload)this.props.fileLimit&&(this.uploadedFileCount+=this.state.files.length),this.props.uploadHandler&&this.props.uploadHandler({files:this.state.files,options:{clear:this.clear,props:this.props}});else{this.setState({msgs:[]});var t=new XMLHttpRequest,n=new FormData;this.props.onBeforeUpload&&this.props.onBeforeUpload({xhr:t,formData:n});var o,l=O(this.state.files);try{for(l.s();!(o=l.n()).done;){var s=o.value;n.append(this.props.name,s,s.name)}}catch(e){l.e(e)}finally{l.f()}t.upload.addEventListener("progress",(function(t){t.lengthComputable&&e.setState({progress:Math.round(100*t.loaded/t.total)},(function(){e.props.onProgress&&e.props.onProgress({originalEvent:t,progress:e.state.progress})}))})),t.onreadystatechange=function(){4===t.readyState&&(e.setState({progress:0}),t.status>=200&&t.status<300?(e.props.fileLimit&&(e.uploadedFileCount+=e.state.files.length),e.props.onUpload&&e.props.onUpload({xhr:t,files:e.state.files})):e.props.onError&&e.props.onError({xhr:t,files:e.state.files}),e.clear())},t.open("POST",this.props.url,!0),this.props.onBeforeSend&&this.props.onBeforeSend({xhr:t,formData:n}),t.withCredentials=this.props.withCredentials,t.send(n)}}},{key:"clear",value:function(){this.setState({files:[]}),this.props.onClear&&this.props.onClear(),this.clearInputElement()}},{key:"choose",value:function(){this.fileInput.click()}},{key:"onFocus",value:function(){this.setState({focused:!0})}},{key:"onBlur",value:function(){this.setState({focused:!1})}},{key:"onKeyDown",value:function(e){13===e.which&&this.choose()}},{key:"onDragEnter",value:function(e){this.props.disabled||(e.dataTransfer.dropEffect="copy",e.stopPropagation(),e.preventDefault())}},{key:"onDragOver",value:function(e){this.props.disabled||(e.dataTransfer.dropEffect="copy",l.DomHandler.addClass(this.content,"p-fileupload-highlight"),e.stopPropagation(),e.preventDefault())}},{key:"onDragLeave",value:function(e){this.props.disabled||(e.dataTransfer.dropEffect="copy",l.DomHandler.removeClass(this.content,"p-fileupload-highlight"))}},{key:"onDrop",value:function(e){if(!this.props.disabled){l.DomHandler.removeClass(this.content,"p-fileupload-highlight"),e.stopPropagation(),e.preventDefault();var t=e.dataTransfer?e.dataTransfer.files:e.target.files;(this.props.multiple||t&&0===t.length)&&this.onFileSelect(e)}}},{key:"onSimpleUploaderClick",value:function(){this.hasFiles()?this.upload():this.fileInput.click()}},{key:"renderChooseButton",value:function(){var e=this,t=this.props.chooseOptions,n=t.style,o=t.icon,i=t.iconOnly,r=l.classNames("p-button p-fileupload-choose p-component",{"p-disabled":this.props.disabled,"p-focus":this.state.focused,"p-button-icon-only":i},t.className),p="p-button-label p-clickable",u=i?a.default.createElement("span",{className:p,dangerouslySetInnerHTML:{__html:" "}}):a.default.createElement("span",{className:p},this.chooseButtonLabel());return a.default.createElement("span",{className:r,style:n,onClick:this.choose,onKeyDown:this.onKeyDown,onFocus:this.onFocus,onBlur:this.onBlur,tabIndex:0},a.default.createElement("input",{ref:function(t){return e.fileInput=t},type:"file",onChange:this.onFileSelect,multiple:this.props.multiple,accept:this.props.accept,disabled:this.chooseDisabled()}),l.IconUtils.getJSXIcon(o||"pi pi-fw pi-plus",{className:"p-button-icon p-button-icon-left p-clickable"},{props:this.props}),u,a.default.createElement(s.Ripple,null))}},{key:"renderFile",value:function(e,n){var o=this,s=this.isImage(e)?a.default.createElement("div",null,a.default.createElement("img",{alt:e.name,role:"presentation",src:e.objectURL,width:this.props.previewWidth})):null,i=a.default.createElement("div",{className:"p-fileupload-filename"},e.name),r=a.default.createElement("div",null,this.formatSize(e.size)),p=a.default.createElement("div",null,a.default.createElement(t.Button,{type:"button",icon:"pi pi-times",onClick:function(e){return o.remove(e,n)}})),u=a.default.createElement(a.default.Fragment,null,s,i,r,p);if(this.props.itemTemplate){var c={onRemove:function(e){return o.remove(e,n)},previewElement:s,fileNameElement:i,sizeElement:r,removeElement:p,formatSize:this.formatSize(e.size),files:this.state.files,index:n,element:u,props:this.props};u=l.ObjectUtils.getJSXElement(this.props.itemTemplate,e,c)}return a.default.createElement("div",{className:"p-fileupload-row",key:e.name+e.type+e.size},u)}},{key:"renderFiles",value:function(){var e=this;return a.default.createElement("div",{className:"p-fileupload-files"},this.state.files.map((function(t,n){return e.renderFile(t,n)})))}},{key:"renderEmptyContent",value:function(){return this.props.emptyTemplate&&!this.hasFiles()?l.ObjectUtils.getJSXElement(this.props.emptyTemplate,this.props):null}},{key:"renderProgressBarContent",value:function(){return this.props.progressBarTemplate?l.ObjectUtils.getJSXElement(this.props.progressBarTemplate,this.props):a.default.createElement(o.ProgressBar,{value:this.state.progress,showValue:!1})}},{key:"renderAdvanced",value:function(){var e,o,s,i,r=this,p=l.classNames("p-fileupload p-fileupload-advanced p-component",this.props.className),u=l.classNames("p-fileupload-buttonbar",this.props.headerClassName),c=l.classNames("p-fileupload-content",this.props.contentClassName),f=this.renderChooseButton(),d=this.renderEmptyContent();if(!this.props.auto){var h=this.props.uploadOptions,m=this.props.cancelOptions,y=h.iconOnly?"":this.uploadButtonLabel(),v=m.iconOnly?"":this.cancelButtonLabel();e=a.default.createElement(t.Button,{type:"button",label:y,icon:h.icon||"pi pi-upload",onClick:this.upload,disabled:this.uploadDisabled(),style:h.style,className:h.className}),o=a.default.createElement(t.Button,{type:"button",label:v,icon:m.icon||"pi pi-times",onClick:this.clear,disabled:this.cancelDisabled(),style:m.style,className:m.className})}this.hasFiles()&&(s=this.renderFiles(),i=this.renderProgressBarContent());var b=a.default.createElement("div",{className:u,style:this.props.headerStyle},f,e,o);return this.props.headerTemplate&&(b=l.ObjectUtils.getJSXElement(this.props.headerTemplate,{className:u,chooseButton:f,uploadButton:e,cancelButton:o,element:b,props:this.props})),a.default.createElement("div",{id:this.props.id,className:p,style:this.props.style},b,a.default.createElement("div",{ref:function(e){r.content=e},className:c,style:this.props.contentStyle,onDragEnter:this.onDragEnter,onDragOver:this.onDragOver,onDragLeave:this.onDragLeave,onDrop:this.onDrop},i,a.default.createElement(n.Messages,{ref:function(e){return r.messagesUI=e}}),s,d))}},{key:"renderBasic",value:function(){var e=this,t=this.hasFiles(),o=this.props.chooseOptions,i=l.classNames("p-fileupload p-fileupload-basic p-component",this.props.className),r=l.classNames("p-button p-component p-fileupload-choose",{"p-fileupload-choose-selected":t,"p-disabled":this.props.disabled,"p-focus":this.state.focused},o.className),p=o.icon||l.classNames({"pi pi-plus":!o.icon&&(!t||this.props.auto),"pi pi-upload":!o.icon&&t&&!this.props.auto}),u="p-button-label p-clickable",c=o.iconOnly?a.default.createElement("span",{className:u,dangerouslySetInnerHTML:{__html:" "}}):a.default.createElement("span",{className:u},this.chooseButtonLabel()),f=this.props.auto?c:a.default.createElement("span",{className:u},t?this.state.files[0].name:c),d=l.IconUtils.getJSXIcon(p,{className:"p-button-icon p-button-icon-left"},{props:this.props,hasFiles:t});return a.default.createElement("div",{className:i,style:this.props.style},a.default.createElement(n.Messages,{ref:function(t){return e.messagesUI=t}}),a.default.createElement("span",{className:r,style:o.style,onMouseUp:this.onSimpleUploaderClick,onKeyDown:this.onKeyDown,onFocus:this.onFocus,onBlur:this.onBlur,tabIndex:0},d,f,!t&&a.default.createElement("input",{ref:function(t){return e.fileInput=t},type:"file",accept:this.props.accept,multiple:this.props.multiple,disabled:this.props.disabled,onChange:this.onFileSelect}),a.default.createElement(s.Ripple,null)))}},{key:"render",value:function(){return"advanced"===this.props.mode?this.renderAdvanced():"basic"===this.props.mode?this.renderBasic():void 0}}])&&y(c.prototype,f),d&&y(c,d),Object.defineProperty(c,"prototype",{writable:!1}),E}();N={id:null,name:null,url:null,mode:"advanced",multiple:!1,accept:null,disabled:!1,auto:!1,maxFileSize:null,invalidFileSizeMessageSummary:"{0}: Invalid file size, ",invalidFileSizeMessageDetail:"maximum upload size is {0}.",style:null,className:null,widthCredentials:!1,previewWidth:50,chooseLabel:null,uploadLabel:null,cancelLabel:null,chooseOptions:{label:null,icon:null,iconOnly:!1,className:null,style:null},uploadOptions:{label:null,icon:null,iconOnly:!1,className:null,style:null},cancelOptions:{label:null,icon:null,iconOnly:!1,className:null,style:null},customUpload:!1,headerClassName:null,headerStyle:null,contentClassName:null,contentStyle:null,headerTemplate:null,itemTemplate:null,emptyTemplate:null,progressBarTemplate:null,onBeforeUpload:null,onBeforeSend:null,onUpload:null,onError:null,onClear:null,onSelect:null,onProgress:null,onValidationFail:null,uploadHandler:null,onRemove:null},(D="defaultProps")in(I=C)?Object.defineProperty(I,D,{value:N,enumerable:!0,configurable:!0,writable:!0}):I[D]=N,exports.FileUpload=C;