Skip to content

Commit

Permalink
refactor: Move public script to TypeScript
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Mar 6, 2024
1 parent 42f7569 commit f9a1acb
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 90 deletions.
90 changes: 0 additions & 90 deletions src/public.js

This file was deleted.

103 changes: 103 additions & 0 deletions src/public.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @copyright Copyright (c) 2021 John Molakvoæ <[email protected]>
*
* @author Christopher Ng <[email protected]>
* @author John Molakvoæ <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import { loadState } from '@nextcloud/initial-state'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'

import '../css/public.css'

import { logger } from './logger.ts'

const { limit, downloads } = loadState('files_downloadlimit', 'download_limit', { limit: -1, downloads: 0 })
logger.debug('Download limit', { limit, downloads })

// Global variables init on page load
let count = limit - downloads
let clicks = 0

/**
* Update the span counter message
*
* @param span html span element to update
* @param count number of remaining downloads allowed
*/
const updateCounter = (span: HTMLSpanElement, count: number) => {
if (count === 0) {
span.innerText = t('files_downloadlimit', 'You have reached the maximum amount of downloads allowed')
} else {
span.innerText = n('files_downloadlimit', '1 remaining download allowed', '{count} remaining downloads allowed', count, { count })
}
}

window.addEventListener('DOMContentLoaded', () => {
if (limit < 1) {
return
}

const container = document.getElementById('header-primary-action')
if (!container) {
return
}

const span = document.createElement('span')
span.setAttribute('style', 'color: var(--color-primary-text); padding: 0 10px;')
updateCounter(span, count)
container.prepend(span)

const publicContent = document.querySelector<HTMLElement>('#files-public-content')
if (!publicContent) {
return
}

// Preventing mouse interaction
publicContent.oncontextmenu = (event) => {
event.preventDefault()
event.stopPropagation()
return false
}

// Adding double-download warning
const downloadButtons = document.querySelectorAll<HTMLAnchorElement>('a[href*="/download/"]') || []
new Set(downloadButtons).forEach(button => {
button.addEventListener('click', (event) => {
// Warn about download limits
if (clicks > 0) {
if (!confirm(t('files_downloadlimit', 'This share has a limited number of downloads. Are you sure you want to trigger a new download?'))) {
event.preventDefault()
event.stopPropagation()
return
}
}

// Handle counts changes
count--
clicks++
updateCounter(span, count)

// Remove the buttons if share is now expired
if (count === 0) {
[...downloadButtons].forEach(button => button.remove())
}
})
})
})

0 comments on commit f9a1acb

Please sign in to comment.