/* * This file is part of the TYPO3 CMS project. * * It is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License, either version 2 * of the License, or any later version. * * For the full copyright and license information, please read the * LICENSE.txt file that was distributed with this source code. * * The TYPO3 project - inspiring people to share! */ import C from"@typo3/core/document-service.js";import d from"@typo3/backend/notification.js";import E from"@typo3/backend/info-window.js";import{FileListActionSelector as M,FileListActionUtility as T,FileListActionEvent as p}from"@typo3/filelist/file-list-actions.js";import y from"@typo3/backend/icons.js";import v from"@typo3/core/ajax/ajax-request.js";import l from"@typo3/core/event/regular-event.js";import{ModuleStateStorage as U}from"@typo3/backend/storage/module-state-storage.js";import h from"@typo3/backend/modal.js";import{SeverityEnum as S}from"@typo3/backend/enum/severity.js";import R from"@typo3/backend/severity.js";import{MultiRecordSelectionSelectors as k}from"@typo3/backend/multi-record-selection.js";import D from"@typo3/backend/context-menu.js";import m from"~labels/core.core";import F from"~labels/core.common";import"@typo3/backend/element/progress-bar-element.js";var u;(function(g){g.fileListFormSelector='form[name="fileListForm"]',g.commandSelector='input[name="cmd"]',g.searchFieldSelector='input[name="searchTerm"]',g.pointerFieldSelector='input[name="pointer"]'})(u||(u={}));const w="typo3:filelist:openElementBrowser";class c{constructor(){this.downloadFilesAndFolders=t=>{t.preventDefault();const e=t.target,o=t.detail,i=o.configuration,r=[];o.checkboxes.forEach(a=>{if(a.checked){const s=a.closest(M.elementSelector),f=T.getResourceForElement(s);r.unshift(f)}}),r.length?this.triggerDownload(r,i.downloadUrl,e):d.warning(m.get("file_download.invalidSelection"))},this.registerPaginationEvents=()=>{document.querySelectorAll(".t3js-filelist-paging").forEach(t=>{t.addEventListener("keyup",e=>{e.preventDefault();let o=Number(t.value);const i=Number(t.min),r=Number(t.max);if(i&&or&&(o=r),t.value=o.toString(10),e.key==="Enter"&&o!==Number(t.dataset.currentpage)){const a=t.closest('form[name="fileListForm"]'),s=new URL(a.action,window.origin);s.searchParams.set("currentPage",o.toString()),window.location.href=s.toString()}})})},new l(w,t=>{const e=new URL(t.detail.actionUrl,window.location.origin);e.searchParams.set("expandFolder",t.detail.identifier),e.searchParams.set("mode",t.detail.mode),h.advanced({type:h.types.iframe,content:e.toString(),size:h.sizes.large}).addEventListener("typo3-modal-hidden",()=>{top.list_frame.document.location.reload()})}).bindTo(document),new l(p.primary,t=>{const e=t.detail,o=e.resources[0],i=e.trigger.closest("[data-default-language-access]");if(o.type==="file"&&i!==null){const r=new URL(top.TYPO3.settings.FormEngine.moduleUrl,window.location.origin);o.metaUid>0?r.searchParams.set("edit[sys_file_metadata]["+o.metaUid+"]","edit"):(r.searchParams.set("edit[sys_file_metadata][0]","new"),r.searchParams.set("defVals[sys_file_metadata][file]",o.uid.toString(10))),r.searchParams.set("module",top.TYPO3.ModuleMenu.App.getCurrentModule()),r.searchParams.set("returnUrl",c.getReturnUrl("")),window.location.href=r.toString()}if(o.type==="folder"){const r=c.parseQueryParameters(document.location);r.id=o.identifier;const a=new URL(window.location.pathname,window.location.origin);for(const[s,f]of Object.entries(r))a.searchParams.set(s,f);window.location.href=a.toString()}}).bindTo(document),new l(p.primaryContextmenu,t=>{const e=t.detail,o=e.resources[0];D.show("sys_file",o.identifier,"","","",e.trigger,e.event)}).bindTo(document),new l(p.show,t=>{const o=t.detail.resources[0];c.openInfoPopup("_"+o.type.toUpperCase(),o.identifier)}).bindTo(document),new l(p.download,t=>{const e=t.detail,o=e.resources[0];this.triggerDownload([o],e.url,e.trigger)}).bindTo(document),new l(p.updateOnlineMedia,t=>{const e=t.detail,o=e.resources[0];this.updateOnlineMedia(o,e.url)}).bindTo(document),C.ready().then(()=>{c.processTriggers(),this.registerPaginationEvents(),new l("click",(t,e)=>{t.preventDefault(),document.dispatchEvent(new CustomEvent(w,{detail:{actionUrl:e.href,identifier:e.dataset.identifier,mode:e.dataset.mode}}))}).delegateTo(document,".t3js-element-browser")}),new l("multiRecordSelection:action:edit",this.editFileMetadata).bindTo(document),new l("multiRecordSelection:action:delete",this.deleteMultiple).bindTo(document),new l("multiRecordSelection:action:download",this.downloadFilesAndFolders).bindTo(document),new l("multiRecordSelection:action:copyMarked",t=>{c.submitClipboardFormWithCommand("copyMarked",t.target)}).bindTo(document),new l("multiRecordSelection:action:removeMarked",t=>{c.submitClipboardFormWithCommand("removeMarked",t.target)}).bindTo(document);const n=document.querySelector([u.fileListFormSelector,u.searchFieldSelector].join(" "))?.value!=="";new l("search",t=>{const e=t.target;e.value===""&&n&&e.closest(u.fileListFormSelector)?.submit()}).delegateTo(document,u.searchFieldSelector)}static submitClipboardFormWithCommand(n,t){const e=t.closest(u.fileListFormSelector);if(!e)return;const o=e.querySelector(u.commandSelector);if(o){if(o.value=n,n==="copyMarked"||n==="removeMarked"){const i=e.querySelector(u.pointerFieldSelector),r=c.parseQueryParameters(document.location).pointer;i&&r&&(i.value=r)}e.submit()}}static openInfoPopup(n,t){E.showItem(n,t)}static processTriggers(){const n=document.querySelector(".filelist-main");n!==null&&U.update("media",n.dataset.filelistCurrentIdentifier)}static parseQueryParameters(n){const t=new URLSearchParams(n.search);return Object.fromEntries(t.entries())}static getReturnUrl(n){if(n===""){const t=top.list_frame.document.forms.namedItem("fileListForm");t!==null?n=t.action:n=top.list_frame.document.location.pathname+top.list_frame.document.location.search}return n}deleteMultiple(n){n.preventDefault();const e=n.detail.configuration;h.advanced({title:e.title||"Delete",content:e.content||"Are you sure you want to delete those files and folders?",severity:S.warning,buttons:[{text:F.get("close"),active:!0,btnClass:"btn-default",trigger:(o,i)=>i.hideModal()},{text:e.ok||F.get("ok"),btnClass:"btn-"+R.getCssClass(S.warning),trigger:(o,i)=>{c.submitClipboardFormWithCommand("delete",n.target),i.hideModal()}}]})}editFileMetadata(n){n.preventDefault();const t=n.detail,e=t.configuration;if(!e||!e.idField||!e.table)return;const o=[];if(t.checkboxes.forEach(i=>{const r=i.closest(k.elementSelector);r!==null&&r.dataset[e.idField]&&o.push(r.dataset[e.idField])}),o.length){const i=new URL(top.TYPO3.settings.FormEngine.moduleUrl,window.location.origin);i.searchParams.set("edit["+e.table+"]["+o.join(",")+"]","edit"),i.searchParams.set("returnUrl",c.getReturnUrl(e.returnUrl||"")),(e.columnsOnly||[]).forEach((a,s)=>{i.searchParams.set("columnsOnly["+e.table+"]["+s+"]",a)}),window.location.href=i.toString()}else d.warning("The selected elements can not be edited.")}triggerDownload(n,t,e){if(n.length===1){const a=n.at(0);if(a.type==="file"){this.invokeDownload(a.url,a.name);return}}d.info(m.get("file_download.prepare"),"",2);const o=e?.innerHTML;e&&(e.setAttribute("disabled","disabled"),y.getIcon("spinner-circle",y.sizes.small).then(a=>{e.innerHTML=a}));const i=this.getProgress();i.start();const r=n.map(a=>a.identifier);new v(t).post({items:r}).then(async a=>{let s=a.response.headers.get("Content-Disposition");if(!s){const b=await a.resolve();b.success===!1&&b.status==="noFiles"?d.warning(m.get("file_download.noFiles"),m.get("file_download.noFiles.message"),10):d.error(m.get("file_download.error"));return}s=s.substring(s.indexOf(" filename=")+10);const f=await a.raw().arrayBuffer(),L=new Blob([f],{type:a.raw().headers.get("Content-Type")}),P=URL.createObjectURL(L);this.invokeDownload(P,s),d.success(m.get("file_download.success"),"",2)}).catch(()=>{d.error(m.get("file_download.error"))}).finally(()=>{i.done(),e&&(e.removeAttribute("disabled"),e.innerHTML=o)})}updateOnlineMedia(n,t){if(!t||!n.uid||n.type!=="file")return;const e=this.getProgress();e.start(),new v(t).post({resource:n}).then(()=>{d.success(m.get("online_media.update.success"))}).catch(()=>{d.error(m.get("online_media.update.error"))}).finally(()=>{e.done(),window.location.reload()})}invokeDownload(n,t){const e=document.createElement("a");e.href=n,e.download=t,document.body.appendChild(e),e.click(),URL.revokeObjectURL(n),document.body.removeChild(e)}getProgress(){return(!this.progressBar||!this.progressBar.isConnected)&&(this.progressBar=document.createElement("typo3-backend-progress-bar"),document.querySelector(".module-loading-indicator").appendChild(this.progressBar)),this.progressBar}}export{c as default,w as fileListOpenElementBrowser};