Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move public script to TypeScript #227

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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æ <skjnldsv@protonmail.com>
*
* @author Christopher Ng <chrng8@gmail.com>
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @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())
}
})
})
})
Loading