2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
"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<t;n++)o[n]=e[n];return o}function u(e){if(Array.isArray(e))return p(e)}function c(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function f(e,t){if(e){if("string"==typeof e)return p(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)?p(e,t):void 0}}function d(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function h(e){return u(e)||c(e)||f(e)||d()}function m(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function y(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function v(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function b(e,t){return b=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},b(e,t)}function g(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&b(e,t)}function E(e){return E="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},E(e)}function S(e,t){if(t&&("object"===E(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return v(e)}function k(e){return k=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},k(e)}function O(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=B(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0,l=function(){};return{s:l,n:function(){return o>=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);n<t;n++)o[n]=e[n];return o}function F(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var I,D,N,C=function(r){g(E,e.Component);var p,u,c,f,d,b=(p=E,u=F(),function(){var e,t=k(p);if(u){var n=k(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return S(this,e)});function E(e){var t;return m(this,E),(t=b.call(this,e)).state={files:[],msgs:[],focused:!1,progress:0},t.choose=t.choose.bind(v(t)),t.upload=t.upload.bind(v(t)),t.clear=t.clear.bind(v(t)),t.onFileSelect=t.onFileSelect.bind(v(t)),t.onDragEnter=t.onDragEnter.bind(v(t)),t.onDragOver=t.onDragOver.bind(v(t)),t.onDragLeave=t.onDragLeave.bind(v(t)),t.onDrop=t.onDrop.bind(v(t)),t.onKeyDown=t.onKeyDown.bind(v(t)),t.onFocus=t.onFocus.bind(v(t)),t.onBlur=t.onBlur.bind(v(t)),t.onSimpleUploaderClick=t.onSimpleUploaderClick.bind(v(t)),t.duplicateIEEvent=!1,t}return c=E,(f=[{key:"hasFiles",value:function(){return this.state.files&&this.state.files.length>0}},{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;o<n.length;o++){var l=n[o];this.isFileSelected(l)||this.validate(l)&&(this.isImage(l)&&(l.objectURL=window.URL.createObjectURL(l)),this.files.push(l))}this.setState({files:this.files},(function(){t.hasFiles()&&t.props.auto&&t.upload()})),this.props.onSelect&&this.props.onSelect({originalEvent:e,files:n}),"drop"!==e.type&&this.isIE11()?this.clearIEInput():this.clearInputElement(),"basic"===this.props.mode&&this.files.length>0&&(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;
|