UNPKG

imagekit-media-library-widget

Version:
3 lines (2 loc) 6.11 kB
"use strict";var i,t;exports.InitialViewParameterEnum=void 0,(i=exports.InitialViewParameterEnum||(exports.InitialViewParameterEnum={})).SEARCH_QUERY="searchQuery",i.FOLDER_PATH="folderPath",i.FILE_ID="fileId",i.COLLECTION="collection",i.FILE_TYPE="fileType",exports.FileTypeValue=void 0,(t=exports.FileTypeValue||(exports.FileTypeValue={})).IMAGE="images",t.VIDEO="videos",t.CSSJS="cssJs",t.OTHERS="others";var e=function(){function i(i,t){var e=this;this.IK_HOST="https://eml.imagekit.io",this.IK_FRAME_TITLE="ImageKit Embedded Media Library",this.widgetHost=window.location.href,this.options=this.getDefaultOptions(),i&&"object"==typeof i&&Object.assign(this.options,i),this.callbackFunction=t&&"function"==typeof t?t:function(){},this.view=this.options.view,this.windowClickHandler=function(i){e.modal&&i.target===e.modal&&e.close()},this.messageHandler=function(i){i.origin===e.IK_HOST&&("CLOSE_MEDIA_LIBRARY_WIDGET"!==i.data.eventType&&"INSERT"!==i.data.eventType||(e.callbackFunction(i.data),e.close()))},this.registerStyles(),this.buildOut(),this.setListeners()}return i.prototype.getDefaultOptions=function(){return{className:"",container:"",containerDimensions:{height:"100%",width:"100%"},dimensions:{height:"100%",width:"100%"},style:{border:"none"},view:"modal",renderOpenButton:!0}},i.prototype.registerStyles=function(){this.styleEl=document.createElement("style"),this.styleEl.innerHTML="\n /* The Modal (background) */\n .ik-media-library-widget-modal {\n display: none; /* Hidden by default */\n position: fixed; /* Stay in place */\n z-index: 1; /* Sit on top */\n padding-top: 2%; /* Location of the box */\n left: 0;\n top: 0;\n width: 100%; /* Full width */\n height: 100%; /* Full height */\n overflow: auto; /* Enable scroll if needed */\n background-color: rgb(0,0,0); /* Fallback color */\n background-color: rgba(0,0,0,0.4); /* Black w/ opacity */\n }\n \n /* Modal Content */\n .ik-media-library-widget-modal-content {\n background-color: #fefefe;\n margin: auto;\n border: 1px solid #888;\n width: 96%;\n height: 94%;\n }\n ",document.head.appendChild(this.styleEl)},i.prototype.buildOut=function(){var i,t,e,o,n,s,a,l,d,r,c,h,p,m=this;if(r="string"==typeof this.options.container?document.querySelector(this.options.container):this.options.container,c=document.createDocumentFragment(),this.ikFrame=document.createElement("div"),this.ikFrame.className=this.options.className||"",this.ikFrame.style.height=(null===(t=null===(i=this.options)||void 0===i?void 0:i.containerDimensions)||void 0===t?void 0:t.height)||"100%",this.ikFrame.style.width=(null===(o=null===(e=this.options)||void 0===e?void 0:e.containerDimensions)||void 0===o?void 0:o.width)||"100%",(h=document.createElement("iframe")).title=this.IK_FRAME_TITLE,h.src=this.generateInitialUrl(),h.setAttribute("sandbox","allow-top-navigation allow-same-origin allow-scripts allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-downloads"),h.height=(null===(s=null===(n=this.options)||void 0===n?void 0:n.dimensions)||void 0===s?void 0:s.height)||"100%",h.width=(null===(l=null===(a=this.options)||void 0===a?void 0:a.dimensions)||void 0===l?void 0:l.width)||"100%",h.style.border=this.options.style.border,this.ikFrame.appendChild(h),"modal"!==(null===(d=this.view)||void 0===d?void 0:d.toLowerCase()))c.appendChild(this.ikFrame),r&&r.appendChild(c);else{this.options.renderOpenButton&&((p=document.createElement("button")).innerHTML="Open Media Library",p.onclick=function(){m.modal&&(m.modal.style.display="block")});var u=document.createElement("div"),w=document.createElement("div");u.classList.add("ik-media-library-widget-modal"),w.classList.add("ik-media-library-widget-modal-content"),w.appendChild(this.ikFrame),u.appendChild(w),this.modal=u,p&&this.options.renderOpenButton&&c.appendChild(p),c.appendChild(u),r&&r.appendChild(c)}var g=document.querySelector('[title="'.concat(this.IK_FRAME_TITLE,'"]'));g&&this.postMessageOnLoad(g,this.options,this.IK_HOST)},i.prototype.generateInitialUrl=function(){var i,t;if(null===(t=null===(i=this.options)||void 0===i?void 0:i.mlSettings)||void 0===t?void 0:t.initialView){var e=Object.keys(this.options.mlSettings.initialView)[0];if(Object.values(exports.InitialViewParameterEnum).includes(e))return"".concat(this.IK_HOST,"/media-library-widget?redirectTo=media-library-widget&isMediaLibraryWidget=true&widgetHost=").concat(this.widgetHost,"&mlWidgetInitialView=").concat(btoa(JSON.stringify(this.options.mlSettings.initialView)))}return"".concat(this.IK_HOST,"/media-library-widget?redirectTo=media-library-widget&isMediaLibraryWidget=true&widgetHost=").concat(this.widgetHost)},i.prototype.postMessageOnLoad=function(i,t,e){i.onload=function(){i.contentWindow&&i.contentWindow.postMessage(JSON.stringify({mlSettings:t.mlSettings}),e)}},i.prototype.open=function(){var i;"modal"===(null===(i=this.view)||void 0===i?void 0:i.toLowerCase())&&this.modal&&(this.modal.style.display="block")},i.prototype.close=function(){var i;"modal"===(null===(i=this.view)||void 0===i?void 0:i.toLowerCase())&&this.closeModal()},i.prototype.closeModal=function(){this.modal&&(this.modal.style.display="none")},i.prototype.destroy=function(){window.removeEventListener("click",this.windowClickHandler),window.removeEventListener("message",this.messageHandler),this.modal?(this.modal.remove(),this.modal=void 0):this.ikFrame&&this.ikFrame.parentNode&&this.ikFrame.parentNode.removeChild(this.ikFrame),this.styleEl&&(this.styleEl.remove(),this.styleEl=void 0)},i.prototype.setListeners=function(){window.addEventListener("click",this.windowClickHandler),window.addEventListener("message",this.messageHandler)},i}();window.IKMediaLibraryWidget=e,exports.ImagekitMediaLibraryWidget=e; //# sourceMappingURL=imagekit-media-library-widget.cjs.js.map