{"version":3,"file":"files_downloadlimit-public.mjs","sources":["../src/public.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { loadState } from '@nextcloud/initial-state'\nimport { translate as t, translatePlural as n } from '@nextcloud/l10n'\n\nimport '../css/public.css'\n\nimport { logger } from './logger.ts'\n\nconst { limit, downloads } = loadState('files_downloadlimit', 'download_limit', { limit: -1, downloads: 0 })\nlogger.debug('Download limit', { limit, downloads })\n\n// Global variables init on page load\nlet count = limit - downloads\nlet clicks = 0\n\n/**\n * Update the span counter message\n *\n * @param span html span element to update\n * @param count number of remaining downloads allowed\n */\nconst updateCounter = (span: HTMLSpanElement, count: number) => {\n\tif (count === 0) {\n\t\tspan.innerText = t('files_downloadlimit', 'You have reached the maximum amount of downloads allowed')\n\t} else {\n\t\tspan.innerText = n('files_downloadlimit', '1 remaining download allowed', '{count} remaining downloads allowed', count, { count })\n\t}\n}\n\nwindow.addEventListener('DOMContentLoaded', () => {\n\tif (limit < 1) {\n\t\treturn\n\t}\n\n\tconst container = document.getElementById('header-primary-action')\n\tif (!container) {\n\t\treturn\n\t}\n\n\tconst span = document.createElement('span')\n\tspan.setAttribute('style', 'color: var(--color-primary-text); padding: 0 10px;')\n\tupdateCounter(span, count)\n\tcontainer.prepend(span)\n\n\tconst publicContent = document.querySelector('#files-public-content')\n\tif (!publicContent) {\n\t\treturn\n\t}\n\n\t// Preventing mouse interaction\n\tpublicContent.oncontextmenu = (event) => {\n\t\tevent.preventDefault()\n\t\tevent.stopPropagation()\n\t\treturn false\n\t}\n\n\t// Adding double-download warning\n\tconst downloadButtons = Array.from(document.querySelectorAll('a[href*=\"/download/\"]'))\n\tnew Set(downloadButtons).forEach(button => {\n\t\tbutton.addEventListener('click', (event) => {\n\t\t\t// Warn about download limits\n\t\t\tif (clicks > 0) {\n\t\t\t\tif (!confirm(t('files_downloadlimit', 'This share has a limited number of downloads. Are you sure you want to trigger a new download?'))) {\n\t\t\t\t\tevent.preventDefault()\n\t\t\t\t\tevent.stopPropagation()\n\t\t\t\t\treturn\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Handle counts changes\n\t\t\tcount--\n\t\t\tclicks++\n\t\t\tupdateCounter(span, count)\n\n\t\t\t// Remove the buttons if share is now expired\n\t\t\tif (count === 0) {\n\t\t\t\t[...downloadButtons].forEach(button => button.remove())\n\t\t\t}\n\t\t})\n\t})\n})\n"],"names":["limit","downloads","loadState","logger","count","clicks","updateCounter","span","t","n","container","publicContent","event","downloadButtons","button"],"mappings":"6GAYA,KAAM,CAAE,MAAAA,EAAO,UAAAC,GAAcC,EAAU,sBAAuB,iBAAkB,CAAE,MAAO,GAAI,UAAW,CAAG,CAAA,EAC3GC,EAAO,MAAM,iBAAkB,CAAE,MAAAH,EAAO,UAAAC,CAAW,CAAA,EAGnD,IAAIG,EAAQJ,EAAQC,EAChBI,EAAS,EAQb,MAAMC,EAAgB,CAACC,EAAuBH,IAAkB,CAC3DA,IAAU,EACRG,EAAA,UAAYC,EAAE,sBAAuB,0DAA0D,EAE/FD,EAAA,UAAYE,EAAE,sBAAuB,+BAAgC,sCAAuCL,EAAO,CAAE,MAAAA,CAAAA,CAAO,CAEnI,EAEA,OAAO,iBAAiB,mBAAoB,IAAM,CACjD,GAAIJ,EAAQ,EACX,OAGK,MAAAU,EAAY,SAAS,eAAe,uBAAuB,EACjE,GAAI,CAACA,EACJ,OAGK,MAAAH,EAAO,SAAS,cAAc,MAAM,EACrCA,EAAA,aAAa,QAAS,oDAAoD,EAC/ED,EAAcC,EAAMH,CAAK,EACzBM,EAAU,QAAQH,CAAI,EAEhB,MAAAI,EAAgB,SAAS,cAA2B,uBAAuB,EACjF,GAAI,CAACA,EACJ,OAIaA,EAAA,cAAiBC,IAC9BA,EAAM,eAAe,EACrBA,EAAM,gBAAgB,EACf,IAIR,MAAMC,EAAkB,MAAM,KAAK,SAAS,iBAAoC,uBAAuB,C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