Skip to content

Commit

Permalink
fix dropzone numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Jun 26, 2024
1 parent 4a72cba commit 335b526
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
6 changes: 3 additions & 3 deletions web_src/js/features/dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {clippie} from 'clippie';
import {showTemporaryTooltip} from '../modules/tippy.js';
import {GET, POST} from '../modules/fetch.js';
import {showErrorToast} from '../modules/toast.js';
import {createElementFromHTML, createElement} from '../utils/dom.js';
import {createElementFromHTML, createElement, elemGetAttributeNumber} from '../utils/dom.js';

const {csrfToken, i18n} = window.config;

Expand Down Expand Up @@ -52,8 +52,8 @@ export async function initDropzone(dropzoneEl) {
const dzInst = await createDropzone(dropzoneEl, {
url: dropzoneEl.getAttribute('data-upload-url'),
headers: {'X-Csrf-Token': csrfToken},
maxFiles: dropzoneEl.getAttribute('data-max-file'),
maxFilesize: dropzoneEl.getAttribute('data-max-size'),
maxFiles: elemGetAttributeNumber('data-max-file', null), // match dropzone default value, no limit
maxFilesize: elemGetAttributeNumber('data-max-size', 256), // match dropzone default value: 256 MiB
acceptedFiles: ['*/*', ''].includes(dropzoneEl.getAttribute('data-accepts')) ? null : dropzoneEl.getAttribute('data-accepts'),
addRemoveLinks: true,
dictDefaultMessage: dropzoneEl.getAttribute('data-default-message'),
Expand Down
7 changes: 7 additions & 0 deletions web_src/js/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,10 @@ export function createElement(tagName, attrs) {
}
return el;
}

export function elemGetAttributeNumber(el, attr, def) {
if (!el.hasAttribute(attr)) return def;
const v = parseInt(el.getAttribute(attr));
if (Number.isNaN(v)) throw new Error(`Attribute "${attr}" is not a number`);
return v;
}
12 changes: 11 additions & 1 deletion web_src/js/utils/dom.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createElement, createElementFromHTML} from './dom.js';
import {createElement, createElementFromHTML, elemGetAttributeNumber} from './dom.js';

test('createElementFromHTML', () => {
expect(createElementFromHTML('<a>foo<span>bar</span></a>').outerHTML).toEqual('<a>foo<span>bar</span></a>');
Expand All @@ -14,3 +14,13 @@ test('createElement', () => {
});
expect(el.outerHTML).toEqual('<button id="the-id" class="cls-1 cls-2" data-foo="the-data" disabled=""></button>');
});

test('elemGetAttributeNumber', () => {
expect(elemGetAttributeNumber(createElementFromHTML('<a>foo</a>'), `data-key`)).toEqual(undefined);
expect(elemGetAttributeNumber(createElementFromHTML('<a>foo</a>'), `data-key`, null)).toEqual(null);
expect(elemGetAttributeNumber(createElementFromHTML('<a>foo</a>'), `data-key`, 1)).toEqual(1);
expect(elemGetAttributeNumber(createElementFromHTML('<a data-key="2">foo</a>'), `data-key`)).toEqual(2);
expect(() => {
elemGetAttributeNumber(createElementFromHTML('<a data-key="abc">foo</a>'), `data-key`);
}).toThrowError('Attribute "data-key" is not a number');
});

0 comments on commit 335b526

Please sign in to comment.