!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.jsFileDownloader=e():t.jsFileDownloader=e()}(this,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var r=0;r=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(u&&c){if(this.prev=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),j(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;j(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:P(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}function y(t,e,r,n,o,i,a){try{var u=t[i](a),c=u.value}catch(t){return void r(t)}u.done?e(c):Promise.resolve(c).then(n,o)}function v(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var i=t.apply(e,r);function a(t){y(i,n,o,a,u,"next",t)}function u(t){y(i,n,o,a,u,"throw",t)}a(void 0)}))}}function m(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function b(t,e){for(var r=0;r0&&void 0!==arguments[0]?arguments[0]:{};return m(this,t),this.params=Object.assign({},w,e),this.link=this.createLink(),this.request=null,this.downloadedFile=null,this.abortController=void 0,this.params.autoStart?this.downloadFile():this}var e,r,n,o,i;return e=t,(r=[{key:"start",value:function(){return this.downloadFile()}},{key:"abort",value:function(t){this.abortController&&this.abortController.abort(t||"Download cancelled")}},{key:"downloadFile",value:function(){var t=this;return new Promise((function(e,r){t.initDownload(e,r)}))}},{key:"setAbortListner",value:function(t){this.abortController&&this.abortController.signal.addEventListener("abort",t)}},{key:"unsetAbortListner",value:function(t){this.abortController&&this.abortController.signal.removeEventListener("abort",t)}},{key:"initDownload",value:function(t,e){var r=this,n=function(){r.link.target="_blank",r.link.href=r.params.url,r.clickLink()};if(!("download"in this.link)||this.isMobile())return n(),t();this.request=this.createRequest(),this.abortController="AbortController"in window?new AbortController:null;var o=function t(n){var o=n.target;r.unsetAbortListner(t),r.request&&r.request.abort(),e(o.reason)};return this.setAbortListner(o),this.params.url?(this.request.onload=v(d().mark((function n(){return d().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(r.unsetAbortListner(o),200===parseInt(r.request.status,10)){n.next=3;break}return n.abrupt("return",e(new f("status code [".concat(r.request.status,"]"),r.request)));case 3:return n.next=5,r.startDownload();case 5:return n.abrupt("return",t(r));case 6:case"end":return n.stop()}}),n)}))),this.request.ontimeout=function(){r.unsetAbortListner(o),e(new f("request timeout",r.request))},this.request.onerror=function(){r.unsetAbortListner(o),r.params.nativeFallbackOnError?(n(),t(r)):e(new f("network error",r.request))},this.request.send(this.params.body),this):e(new f("url param not defined!",this.request))}},{key:"isMobile",value:function(){return!this.params.forceDesktopMode&&/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)}},{key:"createRequest",value:function(){var t=new XMLHttpRequest;return t.open(this.params.method,this.params.url,!0),!1!==this.params.contentType&&t.setRequestHeader("Content-type",this.params.contentType),this.params.headers.forEach((function(e){t.setRequestHeader(e.name,e.value)})),t.responseType="arraybuffer",this.params.process&&"function"==typeof this.params.process&&t.addEventListener("progress",this.params.process),this.params.onloadstart&&"function"==typeof this.params.onloadstart&&(t.onloadstart=this.params.onloadstart),t.timeout=this.params.timeout,t.withCredentials=!!this.params.withCredentials||!!this.params.includeCredentials,t}},{key:"getFileName",value:function(){if("string"==typeof this.params.filename)return this.params.filename;var t=this.request.getResponseHeader("Content-Disposition"),e=[];t&&(e=t.replace(/["']/g,"").match(/filename\*?=([^;]+)/));var r=e&&e.length>=1?e[1]:this.params.url.split("/").pop().split("?")[0];return this.params.nameCallback(r)}},{key:"getContentTypeFromFileSignature",value:function(t){var e=Object.assign({},p,this.params.customFileSignatures);return new Promise((function(r,n){var o=new FileReader,i=t.slice(0,4);o.onloadend=function(t){if(t.target.readyState===FileReader.DONE){var o=new Uint8Array(t.target.result),i=[];o.forEach((function(t){i.push(t.toString(16))}));var a=i.join("").toUpperCase();r(e[a])}else n()},o.onerror=n,o.readAsArrayBuffer(i)}))}},{key:"getContentTypeFromResponseHeader",value:function(){return this.request.getResponseHeader("content-type")}},{key:"getContentType",value:function(t){var e=this;return new Promise(function(){var r=v(d().mark((function r(n){var o,i,a,u,c,s,l,f;return d().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(o=e.params.contentTypeDetermination,i="application/octet-stream","header"!==o&&"full"!==o||(a=e.getContentTypeFromResponseHeader()),"signature"!==o&&"full"!==o){r.next=7;break}return r.next=6,e.getContentTypeFromFileSignature(new Blob([t]));case 6:u=r.sent;case 7:n("header"===o?null!==(c=a)&&void 0!==c?c:i:"signature"===o?null!==(s=u)&&void 0!==s?s:i:"full"===o&&null!==(l=null!==(f=u)&&void 0!==f?f:a)&&void 0!==l?l:i);case 8:case"end":return r.stop()}}),r)})));return function(t){return r.apply(this,arguments)}}())}},{key:"createLink",value:function(){var t=document.createElement("a");return t.style.display="none",t}},{key:"clickLink",value:function(){var t;try{t=new MouseEvent("click")}catch(e){(t=document.createEvent("MouseEvent")).initMouseEvent("click",!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null)}this.link.dispatchEvent(t)}},{key:"getFile",value:(i=v(d().mark((function t(e,r){var n,o,i;return d().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,this.getContentType(e);case 2:n=t.sent,i={type:n};try{o=new File([e],r,i)}catch(t){(o=new Blob([e],i)).name=r,o.lastModifiedDate=new Date}return t.abrupt("return",o);case 6:case"end":return t.stop()}}),t,this)}))),function(t,e){return i.apply(this,arguments)})},{key:"startDownload",value:(o=v(d().mark((function t(){var e,r;return d().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return e=this.getFileName(),t.next=3,this.getFile(this.request.response,e);case 3:if(this.downloadedFile=t.sent,!("msSaveOrOpenBlob"in window.navigator)){t.next=6;break}return t.abrupt("return",window.navigator.msSaveOrOpenBlob(this.downloadedFile,e));case 6:return r=window.URL.createObjectURL(this.downloadedFile),this.link.href=r,this.link.download=e,this.clickLink(),setTimeout((function(){(window.URL||window.webkitURL||window).revokeObjectURL(r)}),4e4),t.abrupt("return",this.downloadedFile);case 12:case"end":return t.stop()}}),t,this)}))),function(){return o.apply(this,arguments)})}])&&b(e.prototype,r),n&&b(e,n),Object.defineProperty(e,"prototype",{writable:!1}),t}();e.default=g}]).default})); //# sourceMappingURL=js-file-downloader.min.js.map