-
- Message
-
+
Message
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');
});
diff --git a/src/com.elgato.template.sdPlugin/libs/js/utils.js b/src/com.elgato.template.sdPlugin/libs/js/utils.js
index c59fa6b..8342899 100644
--- a/src/com.elgato.template.sdPlugin/libs/js/utils.js
+++ b/src/com.elgato.template.sdPlugin/libs/js/utils.js
@@ -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 ?? '';
+ }
});
}