Skip to content

Commit 478b1e3

Browse files
committed
refactor: Move public script to TypeScript
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 9442784 commit 478b1e3

2 files changed

Lines changed: 103 additions & 90 deletions

File tree

src/public.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/public.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
3+
*
4+
* @author Christopher Ng <chrng8@gmail.com>
5+
* @author John Molakvoæ <skjnldsv@protonmail.com>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
import { loadState } from '@nextcloud/initial-state'
25+
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
26+
27+
import '../css/public.css'
28+
29+
import { logger } from './logger.ts'
30+
31+
const { limit, downloads } = loadState('files_downloadlimit', 'download_limit', { limit: -1, downloads: 0 })
32+
logger.debug('Download limit', { limit, downloads })
33+
34+
// Global variables init on page load
35+
let count = limit - downloads
36+
let clicks = 0
37+
38+
/**
39+
* Update the span counter message
40+
*
41+
* @param span html span element to update
42+
* @param count number of remaining downloads allowed
43+
*/
44+
const updateCounter = (span: HTMLSpanElement, count: number) => {
45+
if (count === 0) {
46+
span.innerText = t('files_downloadlimit', 'You have reached the maximum amount of downloads allowed')
47+
} else {
48+
span.innerText = n('files_downloadlimit', '1 remaining download allowed', '{count} remaining downloads allowed', count, { count })
49+
}
50+
}
51+
52+
window.addEventListener('DOMContentLoaded', () => {
53+
if (limit < 1) {
54+
return
55+
}
56+
57+
const container = document.getElementById('header-primary-action')
58+
if (!container) {
59+
return
60+
}
61+
62+
const span = document.createElement('span')
63+
span.setAttribute('style', 'color: var(--color-primary-text); padding: 0 10px;')
64+
updateCounter(span, count)
65+
container.prepend(span)
66+
67+
const publicContent = document.querySelector<HTMLElement>('#files-public-content')
68+
if (!publicContent) {
69+
return
70+
}
71+
72+
// Preventing mouse interaction
73+
publicContent.oncontextmenu = (event) => {
74+
event.preventDefault()
75+
event.stopPropagation()
76+
return false
77+
}
78+
79+
// Adding double-download warning
80+
const downloadButtons = document.querySelectorAll<HTMLAnchorElement>('a[href*="/download/"]') || []
81+
new Set(downloadButtons).forEach(button => {
82+
button.addEventListener('click', (event) => {
83+
// Warn about download limits
84+
if (clicks > 0) {
85+
if (!confirm(t('files_downloadlimit', 'This share has a limited number of downloads. Are you sure you want to trigger a new download?'))) {
86+
event.preventDefault()
87+
event.stopPropagation()
88+
return
89+
}
90+
}
91+
92+
// Handle counts changes
93+
count--
94+
clicks++
95+
updateCounter(span, count)
96+
97+
// Remove the buttons if share is now expired
98+
if (count === 0) {
99+
[...downloadButtons].forEach(button => button.remove())
100+
}
101+
})
102+
})
103+
})

0 commit comments

Comments
 (0)