Skip to content

Commit 7ffc03c

Browse files
committed
Port utility functions from 2.x of framework
1 parent 5300eaa commit 7ffc03c

10 files changed

+83
-0
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
"./elements/toast-dialog": "./src/elements/toast-dialog.js",
99
"./handlers/json-redirection": "./src/handlers/json-redirection.js",
1010
"./util/auto-generate-id": "./src/util/auto-generate-id.js",
11+
"./util/ensure-delcarative-open-modal-dialogs-are-modal": "./src/util/ensure-delcarative-open-modal-dialogs-are-modal.js",
12+
"./util/ensure-delcarative-open-modal-dialogs-are-modal": "./src/util/ensure-delcarative-open-modal-dialogs-are-modal.js",
13+
"./util/focus-on-ajax-success-event": "./src/util/focus-on-ajax-success-event.js",
14+
"./util/js-enabled-flag": "./src/util/js-enabled-flag.js",
15+
"./util/open-modal-dialog-on-cablecar-event": "./src/util/open-modal-dialog-on-cablecar-event.js",
16+
"./util/remove-disabled-since-when-loaded": "./src/util/remove-disabled-since-when-loaded.js",
17+
"./util/reset-on-ajax-success-event": "./src/util/reset-on-ajax-success-event.js",
18+
"./util/submit-form-on-command-enter": "./src/util/submit-form-on-command-enter.js",
19+
"./util/theme-preference": "./src/util/theme-preference.js",
1120
"./passkeys/elements/submit-form-after-reauthentication": "./src/passkeys/elements/submit-form-after-reauthentication.js",
1221
"./passkeys/elements/unlock-form-after-reauthentication": "./src/passkeys/elements/unlock-form-after-reauthentication.js",
1322
"./passkeys/handlers/authenticate": "./src/passkeys/handlers/authenticate.js",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function ensureDeclarativeOpenModalDialogsAreModal(event) {
2+
const openDialogs = event.target.querySelectorAll(`dialog[data-ensure-modal][open]`)
3+
openDialogs.forEach((dialog) => { dialog.close(); dialog.showModal() })
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function focusOnAjaxSuccessEvent(event) {
2+
const target = event.target
3+
const focusTargetSelector = target?.dataset?.focusOnAjaxSuccess
4+
if(!focusTargetSelector){ return }
5+
6+
const focusTarget = document.querySelector(focusTargetSelector)
7+
focusTarget?.focus()
8+
}

src/util/js-enabled-flag.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
document.body.dataset.jsEnabled = 'true';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function openModalDialogOnCableCarEvent(event) {
2+
const openModalDialogId = event?.detail?.openModalDialogId
3+
if(!openModalDialogId){return}
4+
const modalDialog = document.getElementById(openModalDialogId)
5+
if(!modalDialog){ return }
6+
if(modalDialog.open){ modalDialog.close() }
7+
modalDialog.showModal()
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function removeDisabledSinceJsLoaded(event) {
2+
const elementsToEnable = event.target.querySelectorAll(`[data-remove-disabled-when-js-loaded]`)
3+
elementsToEnable.forEach((element) => { element.removeAttribute(`disabled`) })
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function resetOnAjaxSuccessEvent(event) {
2+
const target = event.target
3+
if(!target?.dataset?.resetOnAjaxSuccess){ return }
4+
target.reset()
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export async function submitFormOnCommandEnter(event) {
2+
if(event.key == "Enter" && (event.metaKey || event.ctrlKey)){
3+
event.target.form.requestSubmit()
4+
}
5+
}

src/util/theme-preference.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const storageKey = 'theme-preference'
2+
const htmlDataAttributeName = `data-theme`
3+
4+
export function getColorPreference() {
5+
if(!localStorage){ return 'match-system' }
6+
//Has an explicit value been set? Use it if so
7+
if(localStorage.getItem(storageKey)){
8+
return localStorage.getItem(storageKey)
9+
} else {
10+
if(window.matchMedia(`(prefers-color-scheme:dark)`).matches){
11+
return 'dark'
12+
} else {
13+
return 'light'
14+
}
15+
}
16+
}
17+
18+
export function setColorPreference(value){
19+
localStorage.setItem(storageKey, value)
20+
reflectColorPreference()
21+
}
22+
23+
export function reflectColorPreference(){
24+
document.firstElementChild.setAttribute(htmlDataAttributeName, getColorPreference())
25+
}
26+
27+
28+
export function setDefaultColorPreference(defaultValue){
29+
if(defaultValue.toString().trim() == ""){ return }
30+
if(!localStorage){ return }
31+
if(localStorage.getItem(storageKey)){ return }
32+
setColorPreference(defaultValue)
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export async function throwAwayExistingCableReadyOperationEventHandler(event) {
2+
const existingSelector = event?.detail?.existingSelector
3+
if(!existingSelector){return}
4+
if(!document.querySelector(existingSelector)){ return }
5+
event.detail.cancel = true
6+
}

0 commit comments

Comments
 (0)