@striven-erp/striven-fileviewer
Version:
A pure javascript fileviewer made for Striven ERP
1 lines • 8.62 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.strivenfileviewer=t():e.strivenfileviewer=t()}(window,(function(){return function(e){var t={};function i(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,i),o.l=!0,o.exports}return i.m=e,i.c=t,i.d=function(e,t,n){i.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,t){if(1&t&&(e=i(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(i.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)i.d(n,o,function(t){return e[t]}.bind(null,o));return n},i.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(t,"a",t),t},i.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},i.p="",i(i.s=0)}([function(e,t,i){"use strict";function n(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),i.push.apply(i,n)}return i}function o(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function r(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}i.r(t),i.d(t,"default",(function(){return a}));var l=["ase","art","bmp","blp","cd5","cit","cpt","cr2","cut","dds","dib","djvu","egt","exif","gif","gpl","grf","icns","ico","iff","jng","jpeg","jpg","jfif","jp2","jps","lbm","max","miff","mng","msp","nitf","ota","pbm","pc1","pc2","pc3","pcf","pcx","pdn","pgm","PI1","PI2","PI3","pict","pct","pnm","pns","ppm","psb","psd","pdd","psp","px","pxm","pxr","qfx","raw","rle","sct","sgi","rgb","int","bw","tga","tiff","tif","vtf","xbm","xcf","xpm","3dv","amf","ai","awg","cgm","cdr","cmx","dxf","e2d","egt","eps","fs","gbr","odg","svg","stl","vrml","x3d","sxd","v2d","vnd","wmf","emf","art","xar","png","webp","jxr","hdp","wdp","cur","ecw","iff","lbm","liff","nrrd","pam","pcx","pgf","sgi","rgb","rgba","bw","int","inta","sid","ras","sun","tga"],c=["doc","dot","wbk","docx","docm","dotx","dotm","docb","xls","xlt","xlm","xlsx","xlsm","xltx","xltm","xlsb","xla","xlam","xll","xlw","ppt","pot","pps","pptx","pptm","potx","potm","ppam","ppsx","ppsm","sldx","sldm"],s={useSVG:!0,downloadIconClass:"",closeIconClass:""},a=function(){function e(t,i,r){var l=this;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.path=i.path,this.fileName=i.name,this.fileType=i.type,this.view=r,t.style.cursor="pointer",t.onclick=function(){return r?l.viewFile():l.initFileViewer()},this.config=function(e){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?n(i,!0).forEach((function(t){o(e,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(i)):n(i).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(i,t))}))}return e}({},s,{},i)}var t,i,a;return t=e,(i=[{key:"downloadFile",value:function(e){var t=document.createElement("a");t.href=this.path,e&&t.setAttribute("target","blank"),t.setAttribute("download","".concat(this.fileName,".").concat(this.fileType)),document.body.append(t),t.click(),t.remove()}},{key:"constructSVG",value:function(e){var t=e.viewBox,i=e.d,n=document.createElement("span"),o='<svg width="'.concat("25",'" height="').concat("25",'" viewBox="').concat(t,'" xmlns="').concat("http://www.w3.org/2000/svg",'">'),r='<path fill="'.concat("#fff",'" d="').concat(i,'"/>');return n.innerHTML="".concat(o).concat(r,"</svg>"),n}},{key:"constructFontIcons",value:function(e){var t=document.createElement("i");return e.split(" ").forEach((function(e){return t.classList.add(e)})),t}},{key:"createFileViewerControl",value:function(e,t){var i,n=document.createElement("div");return n.style.display="flex",n.style.justifyContent="center",n.style.alignItems="center",n.style.color="#fff",n.style.textAlign="center",n.style.padding="10px",n.style.backgroundColor="#333",n.style.cursor="pointer",n.onclick=function(){return t()},this.config.useSVG?(i=this.constructSVG(e),n.onmouseenter=function(e){return e.target.querySelector("path").setAttribute("fill","#ddd")},n.onmouseleave=function(e){return e.target.querySelector("path").setAttribute("fill","#fff")}):(i=this.constructFontIcons(e),n.onmouseenter=function(e){return e.target.style.color="#ddd"},n.onmouseleave=function(e){return e.target.style.color="#fff"}),n.append(i),n}},{key:"viewFile",value:function(){var e=this;function t(){e.view&&(e.view.innerHTML=""),e.view&&(e.lightbox=null)}if(window.matchMedia("(max-width: 400px)").matches)this.lightbox=null,this.downloadFile();else{var i=this.fileType.toLowerCase();if(l.includes(i)){t();var n=document.createElement("img");n.style.maxWidth="90%",n.style.maxHeight="90%",n.style.alignSelf="center",n.src=this.path,this.view?this.view.append(n):this.lightbox.append(n)}else if(c.includes(i)){t();var o=document.createElement("embed");o.src="https://view.officeapps.live.com/op/embed.aspx?src=".concat(this.path),o.href="http://office.com",o.style.alignSelf="center",o.width="100%",o.height="100%",this.view?this.view.append(o):this.lightbox.append(o)}else switch(i){case"pdf":case"txt":t();var r=document.createElement("embed");r.src="".concat(this.path),r.width="100%",r.height="100%",r.style.alignSelf="center","txt"===i&&(r.style.backgroundColor="#fff"),this.view?this.view.append(r):this.lightbox.append(r);break;default:this.lightbox=null,this.downloadFile(!0)}}}},{key:"initFileViewer",value:function(){var e=this;document.body.style.overflow="hidden",this.lightbox=document.createElement("div"),this.lightbox.style.overflow="hidden",this.lightbox.style.display="flex",this.lightbox.style.justifyContent="space-between",this.lightbox.style.flexDirection="column",this.lightbox.style.backgroundColor="rgba(0, 0, 0, .75)",this.lightbox.style.position="fixed",this.lightbox.style.zIndex="1000000",this.lightbox.style.top="0",this.lightbox.style.right="0",this.lightbox.style.left="0",this.lightbox.style.bottom="0",window.addEventListener("keyup",(function(t){t.key?"Escape"===t.key&&e.close():"27"===t.keyCode&&e.close()})),this.fileviewerControlHeader=document.createElement("div"),this.fileviewerControlHeader.style.display="flex",this.fileviewerControlHeader.style.justifyContent="flex-end",this.config.useSVG?(this.fileviewerControlHeader.append(this.createFileViewerControl({viewBox:"0 0 512 512",d:"M216 0h80c13.3 0 24 10.7 24 24v168h87.7c17.8 0 26.7 21.5 14.1 34.1L269.7 378.3c-7.5 7.5-19.8 7.5-27.3 0L90.1 226.1c-12.6-12.6-3.7-34.1 14.1-34.1H192V24c0-13.3 10.7-24 24-24zm296 376v112c0 13.3-10.7 24-24 24H24c-13.3 0-24-10.7-24-24V376c0-13.3 10.7-24 24-24h146.7l49 49c20.1 20.1 52.5 20.1 72.6 0l49-49H488c13.3 0 24 10.7 24 24zm-124 88c0-11-9-20-20-20s-20 9-20 20 9 20 20 20 20-9 20-20zm64 0c0-11-9-20-20-20s-20 9-20 20 9 20 20 20 20-9 20-20z"},(function(){return e.downloadFile()}))),this.fileviewerControlHeader.append(this.createFileViewerControl({viewBox:"0 0 352 512",d:"M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"},(function(){return e.lightbox.remove()})))):(this.fileviewerControlHeader.append(this.createFileViewerControl(this.config.downloadIconClass,(function(){return e.downloadFile()}))),this.fileviewerControlHeader.append(this.createFileViewerControl(this.config.closeIconClass,(function(){return e.close()})))),this.lightbox&&this.lightbox.append(this.fileviewerControlHeader),this.viewFile(),this.fileviewerControlFooter=document.createElement("div"),this.lightbox&&this.lightbox.append(this.fileviewerControlFooter),this.lightbox&&document.querySelector("body").appendChild(this.lightbox)}},{key:"close",value:function(){document.body.style.overflow="",this.lightbox.remove()}}])&&r(t.prototype,i),a&&r(t,a),e}()}])}));