Liferay.Loader.define("message-boards-web@5.0.70/message_boards/js/MBPortlet.es",["module","exports","require","frontend-js-web"],(function(e,t,n){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var a=n("frontend-js-web");const e={multiple:Liferay.Language.get("x-recently-removed-attachments"),single:Liferay.Language.get("x-recently-removed-attachment")},o=Liferay.Language.get("uploads-are-in-progress-confirmation");class MBPortlet{constructor({constants:e,currentAction:t,getAttachmentsURL:n,namespace:a,replyToMessageId:s,rootNodeId:c,strings:i={confirmDiscardImages:o},viewTrashAttachmentsURL:d}){this._namespace=a,this._constants=e,this._currentAction=t,this._getAttachmentsURL=n,this._replyToMessageId=s,this._strings=i,this._viewTrashAttachmentsURL=d,this.rootNode=document.getElementById(c),this.workflowActionInputNode=document.getElementById("".concat(this._namespace,"workflowAction")),this._events=[],this._attachEvents()}dispose(){this._events.forEach((({event:e,listener:t,target:n})=>n.removeEventListener(e,t))),this._events=[]}_addEventListener(e,t,n){e.addEventListener(t,n),this._events.push({event:t,fn:n,target:e})}_attachEvents(){const e=this.rootNode.querySelector('.sheet-footer button[type="submit"]');e&&this._addEventListener(e,"click",(()=>{this.workflowActionInputNode.value=this._constants.ACTION_PUBLISH,this._saveFn()}));const t=document.getElementById("".concat(this._namespace,"saveButton"));t&&this._addEventListener(t,"click",(()=>{this.workflowActionInputNode.value=this._constants.ACTION_SAVE_DRAFT,this._saveFn()}));const n=this.rootNode.querySelector(".advanced-reply");n&&this._addEventListener(n,"click",(()=>{this._openAdvancedReply()}));const s="".concat(this._namespace,"messageAttachments");Liferay.componentReady(s).then((e=>{e.get("contentBox").delegate("click",this._removeAttachment.bind(this),".delete-attachment")})),this.searchContainerId=s;const o=document.getElementById("view-removed-attachments-link");o&&this._addEventListener(o,"click",(()=>{(0,a.openModal)({id:this._namespace+"openRemovedPageAttachments",onClose:this._updateRemovedAttachments.bind(this),title:Liferay.Language.get("removed-attachments"),url:this._viewTrashAttachmentsURL})}))}_openAdvancedReply(){const e=this._namespace,t=this._replyToMessageId,n=document.getElementById("".concat(e,"body"));n.value=window["".concat(e,"replyMessageBody").concat(t)].getHTML();const a=this.rootNode.querySelector('[name="'.concat(e,"advancedReplyFm").concat(t,'"]'));a.querySelector('[name="'.concat(e,'body"]')).value=n.value,submitForm(a)}_removeAttachment(e){const t=e.currentTarget,n=t.getAttribute("data-url");(0,a.fetch)(n).then((()=>{Liferay.componentReady(this.searchContainerId).then((e=>{e.deleteRow(t.ancestor("tr"),t.getAttribute("data-rowid")),e.updateDataStore()})),this._updateRemovedAttachments()}))}_saveFn(){const e=this.rootNode.querySelectorAll("img[data-random-id]");e.length?(0,a.openConfirmModal)({message:this._strings.confirmDiscardImages,onConfirm:t=>{t&&(e.forEach((e=>{e.parentElement.remove()})),this._submitMBForm())}}):this._submitMBForm()}_submitMBForm(){const e=this._namespace,t=this._replyToMessageId;document.getElementById("".concat(e).concat(this._constants.CMD)).value=this._currentAction,this._updateMultipleMBMessageAttachments();const n=document.getElementById("".concat(e,"body"));t?(n.value=window["".concat(e,"replyMessageBody").concat(t)].getHTML(),submitForm(document["".concat(e,"addQuickReplyFm").concat(t)])):(n.value=window["".concat(e,"bodyEditor")].getHTML(),submitForm(document["".concat(e,"fm")]))}_updateMultipleMBMessageAttachments(){const e=this._namespace,t=document.getElementById("".concat(e,"selectedFileNameContainer"));if(t){const n="".concat(e,"selectUploadedFile"),s=[].slice.call(this.rootNode.querySelectorAll("input[name=".concat(n,"]:checked"))).map(((t,n)=>{const s=t.value;return'<input id="'.concat(e,"selectedFileName").concat(n,'" name="').concat(e,'selectedFileName" type="hidden" value="').concat((0,a.escapeHTML)(s),'" />')})).join("");t.innerHTML=s}}_updateRemovedAttachments(){(0,a.fetch)(this._getAttachmentsURL).then((e=>e.json())).then((t=>{t.active.length&&Liferay.componentReady(this.searchContainerId).then((e=>{const n=e.getData();document.getElementById(this._namespace+"fileAttachments").classList.remove("hide"),t.active.forEach((t=>{-1===n.indexOf(t.id)&&(e.addRow([t.title,t.size,'<a class="delete-attachment" data-rowId="'.concat(t.id,'" data-url="').concat(t.deleteURL,'" href="javascript:void(0);">').concat(Liferay.Language.get("delete"),"</a>")],t.id.toString()),e.updateDataStore())}))}));const n=document.getElementById("view-removed-attachments-link");t.deleted.length?(n.style.display="initial",n.innerHTML=(0,a.sub)(t.deleted.length>1?e.multiple:e.single,t.deleted.length)+" &raquo"):n.style.display="none"}))}}var s=MBPortlet;t.default=s}}));
//# sourceMappingURL=MBPortlet.es.js.map