updated form utils

This commit is contained in:
Zack Hoherchak 2022-02-17 18:47:41 -05:00
commit da160bf227
4 changed files with 55 additions and 42 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@
/src/DistributionTool
com.elgato.template.streamDeckPlugin
.vscode/**
package.json

View file

@ -13,22 +13,14 @@
</head>
<body>
<div class="sdpi-wrapper">
<!--TODOZ: add one of each control from pi-samples to test FormUtils & CSS -->
<form id="property-inspector">
<div class="sdpi-item">
<div data-localize class="sdpi-item-label">Name</div>
<input
class="sdpi-item-value"
name="name"
type="text"
value=""
/>
<input class="sdpi-item-value" name="name" type="text" value="" />
</div>
<div type="textarea" class="sdpi-item">
<div class="sdpi-item-label up20" data-localize>
Message
</div>
<div class="sdpi-item-label up20" data-localize>Message</div>
<div class="sdpi-item-value textarea">
<textarea
name="message"

View file

@ -2,23 +2,24 @@
/// <reference path="../../../libs/js/utils.js" />
StreamDeck.onConnected((jsn) => {
StreamDeck.loadLocalization('../../../');
StreamDeck.loadLocalization('../../../');
const form = document.querySelector('#property-inspector');
const {actionInfo, appInfo, connection, messageType, port, uuid} = jsn;
const {payload, context} = actionInfo;
const {settings} = payload;
const form = document.querySelector('#property-inspector');
const { actionInfo, appInfo, connection, messageType, port, uuid } = jsn;
const { payload, context } = actionInfo;
const { settings } = payload;
Utils.setFormValue(settings, form);
Utils.setFormValue(settings, form);
form.addEventListener(
'input',
Utils.debounce(150, () => {
const value = Utils.getFormValue(form);
StreamDeck.sendToPlugin(value, context);
StreamDeck.setSettings(value);
})
);
form.addEventListener(
'input',
Utils.debounce(150, () => {
const value = Utils.getFormValue(form);
StreamDeck.sendToPlugin(value, context);
StreamDeck.setSettings(value);
console.log(value);
})
);
});
/**
@ -26,9 +27,9 @@ StreamDeck.onConnected((jsn) => {
* (this can be removed if the external window is not used)
*/
window.sendToInspector = (data) => {
console.log(data);
console.log(data);
};
document.querySelector('#open-external').addEventListener('click', () => {
window.open('../../../external.html');
window.open('../../../external.html');
});

View file

@ -1,49 +1,68 @@
// TODOZ: test other form controls like radio and checkboxes (See previous template code);
class Utils {
/**
* Returns the value from a form using the form controls name property
* @param {*} form
* @returns
* @param {*} form
* @returns
*/
static getFormValue(form) {
if (typeof form === "string") {
if (typeof form === 'string') {
form = document.querySelector(form);
}
const elements = form?.elements;
if (!elements) {
throw "Could not find form!";
throw 'Could not find form!';
}
return Array.from(elements)
.filter((element) => element?.name)
.reduce((value, element) => {
value[element.name] = element.value;
return value;
}, {});
const formData = new FormData(form);
let formValue = {};
formData.forEach((value, key) => {
if (!Reflect.has(formValue, key)) {
formValue[key] = value;
return;
}
if (!Array.isArray(formValue[key])) {
formValue[key] = [formValue[key]];
}
formValue[key].push(value);
});
return formValue;
}
/**
* Sets the value of form controls using their name attribute and the jsn object key
* @param {*} jsn
* @param {*} form
* @param {*} jsn
* @param {*} form
*/
static setFormValue(jsn, form) {
if (typeof form === "string") {
if (typeof form === 'string') {
form = document.querySelector(form);
}
const elements = form?.elements;
if (!elements) {
throw "Could not find form!";
throw 'Could not find form!';
}
Array.from(elements)
.filter((element) => element?.name)
.forEach((element) => {
element.value = jsn[element.name] ?? "";
const { name, type } = element;
const value = jsn[name];
const isCheckOrRadio = type === 'checkbox' || type === 'radio';
console.log(element.type)
if (isCheckOrRadio) {
const isSingle = value === element.value;
if (isSingle || (Array.isArray(value) && value.includes(element.value))) {
element.checked = true;
}
} else {
element.value = value ?? '';
}
});
}