@avlcodemonkey/nilla-components 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +219 -3
- package/THIRD_PARTY_NOTICES.md +48 -0
- package/css/alert.css +48 -0
- package/css/autocomplete.css +38 -0
- package/css/dialog.css +61 -0
- package/css/file-to-textarea.css +8 -0
- package/css/html-editor.css +172 -0
- package/css/json.css +44 -0
- package/css/list.css +37 -0
- package/css/nav-toggle.css +92 -0
- package/css/nilla.css +16 -0
- package/css/pjax.css +58 -0
- package/css/slider-input.css +16 -0
- package/css/table.css +99 -0
- package/css/tokens.css +44 -0
- package/css/unsaved-changes.css +10 -0
- package/css/vendor/wysi.css +406 -0
- package/docs/llms.txt +319 -0
- package/package.json +121 -5
- package/src/components/Alert.js +24 -0
- package/src/components/Autocomplete.js +127 -0
- package/src/components/BaseComponent.js +78 -0
- package/src/components/BaseDialog.js +151 -0
- package/src/components/ConfirmDialog.js +34 -0
- package/src/components/CopyToClipboard.js +66 -0
- package/src/components/DateFormatter.js +51 -0
- package/src/components/DateTimeInput.js +63 -0
- package/src/components/DisplayToggle.js +53 -0
- package/src/components/FileToTextarea.js +81 -0
- package/src/components/HtmlEditor.js +309 -0
- package/src/components/InfoDialog.js +38 -0
- package/src/components/JsonFormatter.js +23 -0
- package/src/components/List.js +237 -0
- package/src/components/ListSortable.js +8 -0
- package/src/components/NavToggle.js +119 -0
- package/src/components/PJax.js +888 -0
- package/src/components/Share.js +41 -0
- package/src/components/SliderInput.js +26 -0
- package/src/components/Table.js +758 -0
- package/src/components/TimeOffsetSelect.js +29 -0
- package/src/components/UnsavedChanges.js +187 -0
- package/src/constants/Fetch.js +7 -0
- package/src/constants/HttpHeaders.js +19 -0
- package/src/constants/HttpMethods.js +14 -0
- package/src/constants/SortDirection.js +11 -0
- package/src/constants/TableElements.js +25 -0
- package/src/constants/TableSettings.js +14 -0
- package/src/errors/FetchError.js +7 -0
- package/src/events/ListReorderEvent.js +19 -0
- package/src/events/PJaxErrorEvent.js +38 -0
- package/src/events/PJaxNavigatingEvent.js +46 -0
- package/src/index.js +26 -0
- package/src/locales/en.json +55 -0
- package/src/locales/es.js +9 -0
- package/src/locales/es.json +55 -0
- package/src/models/TableSort.js +20 -0
- package/src/utils/ajaxHeaders.js +10 -0
- package/src/utils/announce.js +40 -0
- package/src/utils/defineElement.js +13 -0
- package/src/utils/editor/blockTools.js +256 -0
- package/src/utils/editor/elements.js +18 -0
- package/src/utils/editor/input.js +350 -0
- package/src/utils/editor/selection.js +125 -0
- package/src/utils/editor/tableTools.js +497 -0
- package/src/utils/escape.js +20 -0
- package/src/utils/fetchJsonArray.js +29 -0
- package/src/utils/filterArray.js +18 -0
- package/src/utils/formActionUrl.js +10 -0
- package/src/utils/formData.js +118 -0
- package/src/utils/index.js +18 -0
- package/src/utils/indexedCompare.js +18 -0
- package/src/utils/isSameOrigin.js +12 -0
- package/src/utils/locale.js +36 -0
- package/src/utils/notifyChange.js +10 -0
- package/src/utils/numberSetting.js +14 -0
- package/src/utils/pageLanguage.js +13 -0
- package/src/utils/parseUtcDate.js +22 -0
- package/src/utils/randomId.js +17 -0
- package/src/utils/registerTabTrap.js +60 -0
- package/src/utils/renderTemplate.js +44 -0
- package/src/utils/requestTimeout.js +12 -0
- package/src/utils/response.js +28 -0
- package/src/utils/sortedCompare.js +67 -0
- package/src/utils/storage.js +40 -0
- package/src/vendor/autocomplete.js +515 -0
- package/src/vendor/formatDate.js +236 -0
- package/src/vendor/licenses/autocomplete.LICENSE.md +21 -0
- package/src/vendor/licenses/fecha.LICENSE.md +21 -0
- package/src/vendor/licenses/pretty-print-json.LICENSE.md +21 -0
- package/src/vendor/licenses/wysi.LICENSE.md +21 -0
- package/src/vendor/prettyPrintJson.js +108 -0
- package/src/vendor/wyzi.js +2024 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import escape from '../utils/escape.js';
|
|
2
|
+
import BaseComponent from './BaseComponent.js';
|
|
3
|
+
import randomId from '../utils/randomId.js';
|
|
4
|
+
import registerTabTrap from '../utils/registerTabTrap.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extend the base component to provide common functionality for custom dialogs.
|
|
8
|
+
*/
|
|
9
|
+
class BaseDialog extends BaseComponent {
|
|
10
|
+
/** @type {HTMLDialogElement} */
|
|
11
|
+
#dialog;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Cleanup function for the active tab trap.
|
|
15
|
+
* @type {(() => void)|undefined}
|
|
16
|
+
*/
|
|
17
|
+
#tabTrapCleanup;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Confirmed element being clicked again, whose click goes through untouched. It's shared by every dialog component,
|
|
21
|
+
* since components can be nested around the same element.
|
|
22
|
+
* @type {Element|undefined}
|
|
23
|
+
*/
|
|
24
|
+
static #confirmedElement;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Clean up when the component is removed from the document.
|
|
28
|
+
*/
|
|
29
|
+
disconnectedCallback() {
|
|
30
|
+
// cleanup should have been called when the dialog closed, but better safe
|
|
31
|
+
this.#tabTrapCleanup?.();
|
|
32
|
+
this.#tabTrapCleanup = undefined;
|
|
33
|
+
|
|
34
|
+
super.disconnectedCallback();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Handles clicks on the links and buttons inside the component, including those added later. A click on one with
|
|
39
|
+
* `data-dialog-content` is stopped before any other listener gets it and passed on, except one made by `clickThrough`.
|
|
40
|
+
* @param {(element: HTMLElement, data: DOMStringMap) => void} onClick Function to run with the link or button that was
|
|
41
|
+
* clicked, and the dataset holding the dialog text.
|
|
42
|
+
*/
|
|
43
|
+
interceptClicks(onClick) {
|
|
44
|
+
this.addEventListener('click', (event) => {
|
|
45
|
+
if (BaseDialog.#confirmedElement && event.target === BaseDialog.#confirmedElement) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const target = /** @type {Element} */ (event.target);
|
|
50
|
+
const clicked = /** @type {HTMLElement|null} */ (target.closest('a, button'));
|
|
51
|
+
const textElement = /** @type {HTMLElement|null} */ (target.closest('[data-dialog-content]'));
|
|
52
|
+
if (!(clicked && this.contains(clicked) && textElement?.dataset.dialogContent)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
event.stopImmediatePropagation();
|
|
58
|
+
onClick(clicked, textElement.dataset);
|
|
59
|
+
}, { capture: true });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Clicks a link or button for the user, without asking again. A canceled event stays canceled, so it takes a new click
|
|
64
|
+
* for the link to be followed or the form submitted.
|
|
65
|
+
* @param {HTMLElement|SVGElement} element Element to click.
|
|
66
|
+
*/
|
|
67
|
+
clickThrough(element) {
|
|
68
|
+
const previous = BaseDialog.#confirmedElement;
|
|
69
|
+
BaseDialog.#confirmedElement = element;
|
|
70
|
+
try {
|
|
71
|
+
// SVG links have no click method, so they're sent the event instead
|
|
72
|
+
if (typeof element.click === 'function') {
|
|
73
|
+
element.click();
|
|
74
|
+
} else {
|
|
75
|
+
element.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, composed: true }));
|
|
76
|
+
}
|
|
77
|
+
} finally {
|
|
78
|
+
BaseDialog.#confirmedElement = previous;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Create a dialog and show it as a modal.
|
|
84
|
+
* @param {string} content Text to display in the body of the dialog. It's shown as text, never parsed as HTML.
|
|
85
|
+
* Attribute values come here already entity-decoded, so parsing them would let encoded markup run.
|
|
86
|
+
* @param {string} ok Label for the ok button.
|
|
87
|
+
* @param {string|undefined} cancel Label for the cancel button.
|
|
88
|
+
* @param {(returnValue: string) => void|undefined} onClose Function to run after dialog closes.
|
|
89
|
+
*/
|
|
90
|
+
showDialog(content, ok, cancel = undefined, onClose = undefined) {
|
|
91
|
+
if (this.#dialog) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const div = document.createElement('div');
|
|
96
|
+
div.innerHTML = this.#makeDialogHtml(content, ok, cancel);
|
|
97
|
+
this.#dialog = /** @type {HTMLDialogElement} */ (div.firstChild);
|
|
98
|
+
document.body.appendChild(this.#dialog);
|
|
99
|
+
|
|
100
|
+
this.#tabTrapCleanup = registerTabTrap(this.#dialog);
|
|
101
|
+
|
|
102
|
+
this.#dialog.addEventListener('close', () => {
|
|
103
|
+
const { returnValue } = this.#dialog;
|
|
104
|
+
|
|
105
|
+
this.#tabTrapCleanup?.();
|
|
106
|
+
this.#tabTrapCleanup = undefined;
|
|
107
|
+
this.#dialog.remove();
|
|
108
|
+
this.#dialog = undefined;
|
|
109
|
+
|
|
110
|
+
if (onClose) {
|
|
111
|
+
onClose(returnValue);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
this.#dialog.showModal();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Create the HTML for the dialog element.
|
|
119
|
+
* @param {string} content Text to display in the body of the dialog.
|
|
120
|
+
* @param {string} ok Text label for the ok button.
|
|
121
|
+
* @param {string|undefined} cancel Text label for the cancel button
|
|
122
|
+
* @returns {string} HTML for the dialog.
|
|
123
|
+
*/
|
|
124
|
+
#makeDialogHtml(content, ok, cancel) {
|
|
125
|
+
const id = `nilla-dialog-${randomId()}`;
|
|
126
|
+
return `<dialog data-nilla-dialog aria-labelledby="${id}">
|
|
127
|
+
<article>
|
|
128
|
+
<p id="${id}">${escape(content ?? '')}</p>
|
|
129
|
+
<form method="dialog">
|
|
130
|
+
<footer data-dialog-footer>${this.#makeDialogButtons(ok, cancel)}</footer>
|
|
131
|
+
</form>
|
|
132
|
+
</article>
|
|
133
|
+
</dialog>`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Create the HTML for the dialog button elements.
|
|
138
|
+
* @param {string} ok Label for the ok button.
|
|
139
|
+
* @param {string|undefined} cancel Label for the cancel button
|
|
140
|
+
* @returns {string} HTML for the buttons.
|
|
141
|
+
*/
|
|
142
|
+
#makeDialogButtons(ok, cancel) {
|
|
143
|
+
if (cancel) {
|
|
144
|
+
return `<button type="submit" value="cancel" autofocus>${escape(cancel)}</button>
|
|
145
|
+
<button type="submit" value="ok">${escape(ok)}</button>`;
|
|
146
|
+
}
|
|
147
|
+
return `<button type="submit" value="ok" autofocus>${escape(ok)}</button>`;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default BaseDialog;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import BaseDialog from './BaseDialog.js';
|
|
2
|
+
import defineElement from '../utils/defineElement.js';
|
|
3
|
+
import { getText } from '../utils/locale.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Web component that wraps confirm logic around any links or buttons it contains.
|
|
7
|
+
*/
|
|
8
|
+
class ConfirmDialog extends BaseDialog {
|
|
9
|
+
/**
|
|
10
|
+
* Initialize component.
|
|
11
|
+
*/
|
|
12
|
+
constructor() {
|
|
13
|
+
super();
|
|
14
|
+
|
|
15
|
+
this.interceptClicks((element, data) => this.#confirm(element, data));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Ask before letting a click through.
|
|
20
|
+
* @param {HTMLElement} element Link or button that was clicked.
|
|
21
|
+
* @param {DOMStringMap} data Dialog text from the clicked element.
|
|
22
|
+
*/
|
|
23
|
+
#confirm(element, { dialogContent, dialogOk, dialogCancel }) {
|
|
24
|
+
this.showDialog(dialogContent, dialogOk || getText('ok', this), dialogCancel || getText('cancel', this), (returnValue) => {
|
|
25
|
+
if (returnValue === 'ok') {
|
|
26
|
+
this.clickThrough(element);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
defineElement('nilla-confirm', ConfirmDialog);
|
|
33
|
+
|
|
34
|
+
export default ConfirmDialog;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import BaseComponent from './BaseComponent.js';
|
|
2
|
+
import announce from '../utils/announce.js';
|
|
3
|
+
import defineElement from '../utils/defineElement.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How long to show that the text was copied, in milliseconds.
|
|
7
|
+
* @type {number}
|
|
8
|
+
*/
|
|
9
|
+
const StatusTime = 3000;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Copy to clipboard web component.
|
|
13
|
+
*/
|
|
14
|
+
class CopyToClipboard extends BaseComponent {
|
|
15
|
+
/**
|
|
16
|
+
* Timer for hiding the status.
|
|
17
|
+
* @type {number|undefined}
|
|
18
|
+
*/
|
|
19
|
+
#statusTimer;
|
|
20
|
+
|
|
21
|
+
constructor() {
|
|
22
|
+
super('copy');
|
|
23
|
+
|
|
24
|
+
const btn = this.getElement('button');
|
|
25
|
+
if (btn) {
|
|
26
|
+
btn.addEventListener('click', this);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Copy the text to clipboard when the button is clicked.
|
|
32
|
+
*/
|
|
33
|
+
async handleEvent() {
|
|
34
|
+
const contentInput = this.getElement('content');
|
|
35
|
+
if (contentInput) {
|
|
36
|
+
const textToCopy = (contentInput.value || contentInput.textContent || '').trim();
|
|
37
|
+
if (textToCopy) {
|
|
38
|
+
try {
|
|
39
|
+
await navigator.clipboard.writeText(textToCopy);
|
|
40
|
+
this.#showStatus();
|
|
41
|
+
} catch {
|
|
42
|
+
// do nothing if clipboard write fails
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Shows that the text was copied, for a moment.
|
|
50
|
+
*/
|
|
51
|
+
#showStatus() {
|
|
52
|
+
const status = this.getElement('status');
|
|
53
|
+
if (status) {
|
|
54
|
+
status.hidden = false;
|
|
55
|
+
announce(status.textContent);
|
|
56
|
+
clearTimeout(this.#statusTimer);
|
|
57
|
+
this.#statusTimer = setTimeout(() => {
|
|
58
|
+
status.hidden = true;
|
|
59
|
+
}, StatusTime);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
defineElement('nilla-copy-to-clipboard', CopyToClipboard);
|
|
65
|
+
|
|
66
|
+
export default CopyToClipboard;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { formatDate } from '../vendor/formatDate.js';
|
|
2
|
+
import defineElement from '../utils/defineElement.js';
|
|
3
|
+
import pageLanguage from '../utils/pageLanguage.js';
|
|
4
|
+
import parseUtcDate, { DateOnlyPattern } from '../utils/parseUtcDate.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Web component for displaying dates in a friendly way.
|
|
8
|
+
*/
|
|
9
|
+
class DateFormatter extends HTMLElement {
|
|
10
|
+
/** @type {string} */
|
|
11
|
+
#dateFormat;
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
|
|
16
|
+
this.#dateFormat = this.dataset.dateFormat;
|
|
17
|
+
this.#updateTextContent(this.textContent || this.dataset.dateValue);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static get observedAttributes() {
|
|
21
|
+
return ['data-date-value'];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
25
|
+
if (name === 'data-date-value' && oldValue !== newValue) {
|
|
26
|
+
this.#updateTextContent(newValue);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#updateTextContent(dateString) {
|
|
31
|
+
if (dateString) {
|
|
32
|
+
try {
|
|
33
|
+
// a date alone is that day everywhere. times without a time zone are UTC; JS converts them to local time
|
|
34
|
+
const text = dateString.trim();
|
|
35
|
+
const dateOnly = DateOnlyPattern.test(text);
|
|
36
|
+
const date = dateOnly ? new Date(`${text}T00:00`) : parseUtcDate(text);
|
|
37
|
+
if (!Number.isNaN(date.getTime())) {
|
|
38
|
+
const lang = pageLanguage(this);
|
|
39
|
+
const options = dateOnly ? { dateStyle: 'short' } : undefined;
|
|
40
|
+
this.textContent = this.#dateFormat ? formatDate(date, this.#dateFormat, lang) : date.toLocaleString(lang, options);
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
/* empty */
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
defineElement('nilla-date', DateFormatter);
|
|
50
|
+
|
|
51
|
+
export default DateFormatter;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { formatDate } from '../vendor/formatDate.js';
|
|
2
|
+
import BaseComponent from './BaseComponent.js';
|
|
3
|
+
import parseUtcDate from '../utils/parseUtcDate.js';
|
|
4
|
+
import defineElement from '../utils/defineElement.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Web component that synchronizes a datetime-local input with a hidden UTC datetime input.
|
|
8
|
+
*/
|
|
9
|
+
class DateTimeInput extends BaseComponent {
|
|
10
|
+
constructor() {
|
|
11
|
+
super('datetime');
|
|
12
|
+
|
|
13
|
+
const localInput = this.getElement('local');
|
|
14
|
+
const hiddenInput = this.getElement('hidden');
|
|
15
|
+
if (localInput && hiddenInput) {
|
|
16
|
+
if (hiddenInput.value && !localInput.value) {
|
|
17
|
+
DateTimeInput.#showValue(localInput, hiddenInput.value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// the hidden input can be changed from code, such as when a table restores its filters
|
|
21
|
+
hiddenInput.addEventListener('change', () => DateTimeInput.#showValue(localInput, hiddenInput.value));
|
|
22
|
+
|
|
23
|
+
localInput.addEventListener('change', () => {
|
|
24
|
+
if (!localInput.value) {
|
|
25
|
+
hiddenInput.value = '';
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// convert value from date input to UTC for saving
|
|
30
|
+
const date = new Date(localInput.value);
|
|
31
|
+
if (Number.isNaN(date.getTime())) {
|
|
32
|
+
hiddenInput.value = '';
|
|
33
|
+
} else {
|
|
34
|
+
hiddenInput.value = date.toISOString();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Shows a UTC value in the date input as local time.
|
|
42
|
+
* @param {HTMLInputElement} localInput Date input.
|
|
43
|
+
* @param {string} value UTC date.
|
|
44
|
+
*/
|
|
45
|
+
static #showValue(localInput, value) {
|
|
46
|
+
const date = parseUtcDate(value);
|
|
47
|
+
if (Number.isNaN(date.getTime())) {
|
|
48
|
+
localInput.value = '';
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// the browser only accepts whole minutes by default, so a value with seconds would block the form
|
|
53
|
+
if (date.getSeconds() && !localInput.step) {
|
|
54
|
+
localInput.step = 'any';
|
|
55
|
+
}
|
|
56
|
+
// input expects date in format "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".
|
|
57
|
+
localInput.value = formatDate(date, 'YYYY-MM-DDTHH:mm:ss');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
defineElement('nilla-datetime-input', DateTimeInput);
|
|
62
|
+
|
|
63
|
+
export default DateTimeInput;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import BaseComponent from './BaseComponent.js';
|
|
2
|
+
import defineElement from '../utils/defineElement.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Conditionally show/hide content.
|
|
6
|
+
*/
|
|
7
|
+
class DisplayToggle extends BaseComponent {
|
|
8
|
+
constructor() {
|
|
9
|
+
super('display');
|
|
10
|
+
|
|
11
|
+
this.getElements('toggle-trigger').forEach((toggle) => {
|
|
12
|
+
toggle.addEventListener('change', this);
|
|
13
|
+
this.#toggleContent(toggle);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Conditionally show/hide content based on the state of the trigger.
|
|
19
|
+
* @param {Event} event Change event.
|
|
20
|
+
*/
|
|
21
|
+
handleEvent(event) {
|
|
22
|
+
/** @type {HTMLSelectElement} */
|
|
23
|
+
const selectEl = event.target;
|
|
24
|
+
if (!selectEl) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.#toggleContent(selectEl);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Show/hide content based on the selected target.
|
|
33
|
+
* @param {HTMLSelectElement} selectEl Select element that triggered the change.
|
|
34
|
+
* @private
|
|
35
|
+
*/
|
|
36
|
+
#toggleContent(selectEl) {
|
|
37
|
+
/** @type {string} */
|
|
38
|
+
const target = selectEl.options[selectEl.selectedIndex]?.dataset.displayToggleTarget;
|
|
39
|
+
|
|
40
|
+
this.getElements('toggle').forEach((el) => {
|
|
41
|
+
const toggles = (el.dataset.displayToggle || '').split(',').map(x => x.trim());
|
|
42
|
+
if (target && toggles.includes(target)) {
|
|
43
|
+
el.hidden = false;
|
|
44
|
+
} else {
|
|
45
|
+
el.hidden = true;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
defineElement('nilla-display-toggle', DisplayToggle);
|
|
52
|
+
|
|
53
|
+
export default DisplayToggle;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import BaseComponent from './BaseComponent.js';
|
|
2
|
+
import defineElement from '../utils/defineElement.js';
|
|
3
|
+
import { getText } from '../utils/locale.js';
|
|
4
|
+
import notifyChange from '../utils/notifyChange.js';
|
|
5
|
+
import numberSetting from '../utils/numberSetting.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Reads a chosen text file into a textarea, so the file's content is submitted with the form as text.
|
|
9
|
+
* Expects a `[data-file-input]` file input and a `textarea` inside. Optional attributes:
|
|
10
|
+
* - `data-max-bytes`: largest file accepted, in bytes.
|
|
11
|
+
* - `data-too-large`: message shown in `[data-file-error]` when the file is larger than that.
|
|
12
|
+
*/
|
|
13
|
+
class FileToTextarea extends BaseComponent {
|
|
14
|
+
/**
|
|
15
|
+
* File chosen last. Only it may fill the textarea.
|
|
16
|
+
* @type {File|undefined}
|
|
17
|
+
*/
|
|
18
|
+
#latestFile;
|
|
19
|
+
|
|
20
|
+
constructor() {
|
|
21
|
+
super('file');
|
|
22
|
+
|
|
23
|
+
const input = this.getElement('input');
|
|
24
|
+
if (input) {
|
|
25
|
+
input.addEventListener('change', this);
|
|
26
|
+
}
|
|
27
|
+
// read out when it's shown
|
|
28
|
+
this.getElement('error')?.setAttribute('role', 'alert');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Load the selected file into the textarea.
|
|
33
|
+
*/
|
|
34
|
+
async handleEvent() {
|
|
35
|
+
const input = this.getElement('input');
|
|
36
|
+
const textarea = this.querySelector('textarea');
|
|
37
|
+
const file = input?.files?.[0];
|
|
38
|
+
|
|
39
|
+
// reads for earlier selections must not finish after this one and overwrite it
|
|
40
|
+
this.#latestFile = file;
|
|
41
|
+
if (!file || !textarea) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.#setError('');
|
|
46
|
+
if (file.size > numberSetting(this.dataset.maxBytes, Infinity)) {
|
|
47
|
+
input.value = '';
|
|
48
|
+
this.#setError(this.dataset.tooLarge || getText('fileTooLarge', this));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let text;
|
|
53
|
+
try {
|
|
54
|
+
text = await file.text();
|
|
55
|
+
} catch (ex) {
|
|
56
|
+
console.error(ex);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (this.#latestFile !== file) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
textarea.value = text;
|
|
63
|
+
notifyChange(textarea);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Show or clear the error message.
|
|
68
|
+
* @param {string} message - Message to show. Empty to hide.
|
|
69
|
+
*/
|
|
70
|
+
#setError(message) {
|
|
71
|
+
const error = this.getElement('error');
|
|
72
|
+
if (error) {
|
|
73
|
+
error.textContent = message;
|
|
74
|
+
error.hidden = !message;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
defineElement('nilla-file-to-textarea', FileToTextarea);
|
|
80
|
+
|
|
81
|
+
export default FileToTextarea;
|