From 4a7f7eef5f93003c90c4a28df9fbfdb67bddfac2 Mon Sep 17 00:00:00 2001 From: Tyler Date: Tue, 21 May 2019 19:16:00 -0400 Subject: [PATCH] Resolve a few property inspector issues --- plugin/manifest.json | 2 +- plugin/pi/index_pi.js | 149 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 124 insertions(+), 27 deletions(-) diff --git a/plugin/manifest.json b/plugin/manifest.json index 3dc90ef..e57a6ca 100644 --- a/plugin/manifest.json +++ b/plugin/manifest.json @@ -38,7 +38,7 @@ "Name": "Remote", "Icon": "images/pluginIcon", "URL": "https://www.elgato.com/gaming/stream-deck", - "Version": "1.2", + "Version": "1.2.1", "SDKVersion": 2, "OS": [ { diff --git a/plugin/pi/index_pi.js b/plugin/pi/index_pi.js index fc7f32e..c36da6d 100644 --- a/plugin/pi/index_pi.js +++ b/plugin/pi/index_pi.js @@ -4,8 +4,11 @@ var websocket = null, uuid = null, actionInfo = {}, settings = {}, + globalSettings = {}, isQT = navigator.appVersion.includes('QtWebEngine'); // 'oninput'; // change this, if you want interactive elements act on any change, or while they're modified +const websiteAction = 'tf.meow.remote.website'; + function connectSocket ( inPort, inUUID, @@ -50,12 +53,25 @@ function connectElgatoStreamDeckSocket (inPort, inUUID, inRegisterEvent, inInfo, }; websocket.send(JSON.stringify(json)); + + if (isAction(websiteAction)) { + getGlobalSettings(); + } }; websocket.onmessage = function (evt) { // Received message from Stream Deck - var jsonObj = JSON.parse(evt.data); - var event = jsonObj['event']; + let jsonObj = JSON.parse(evt.data); + + let event = jsonObj['event']; + + console.log('Got event', event); + + switch (event) { + case 'didReceiveGlobalSettings': + didReceiveGlobalSettings(jsonObj); + break; + } }; } @@ -66,43 +82,71 @@ function initPropertyInspector(initDelay) { } Object.keys(settings).forEach(function (item) { - $('#' + item).val(settings[item]); + let $item = $('#' + item), + value = settings[item]; + + console.log('Load setting', item, 'value', value); + + switch ($item.attr('type')) { + case 'checkbox': + let itemVal = $item.attr('value'); + + if (itemVal == 'false' || itemVal == 'true') { + itemVal = (/^true$/i).test(itemVal); + } + + if (itemVal === value) { + $item.prop('checked', true); + } + break; + default: + $item.val(value); + } }); $('input').each(function() { - var $this = $(this), + let $this = $(this), id = $this.attr('id'); let $item = $this.closest('.sdpi-item'); - $this.on('change', function(e) { + $this.on('change', function() { const type = $this.attr('type'); - if (type) { - const info = $item.find('.sdpi-file-info'); - - if (info) { - const s = decodeURIComponent($this.val().replace(/^C:\\fakepath\\/, '')).split('/').pop(); - - info.text(s.length > 28 - ? s.substr(0, 10) - + '...' - + s.substr(s.length - 10, s.length) - : s); - } - } - let val = $this.val(); switch (type) { case 'checkbox': + // If unchecked, unset the setting + if (!this.checked) { + removeSetting(id); + return; + } + if (val == 'false' || val == 'true') { val = (/^true$/i).test(val); } break; + case 'file': + const info = $item.find('.sdpi-file-info'); + + if (info) { + const s = decodeURIComponent($this.val().replace(/^C:\\fakepath\\/, '')).split('/').pop(); + + info.text(s.length > 28 + ? s.substr(0, 10) + + '...' + + s.substr(s.length - 10, s.length) + : s); + } + break; } updateSetting(id, val); + + if (isAction(websiteAction) && (id == 'remote_host' || id == 'remote_token')) { + updateGlobalSetting(id, val); + } }); }); @@ -111,19 +155,20 @@ function initPropertyInspector(initDelay) { // Hide passphrase field $('#ssh_key_passphrase_container').hide(); $('#ssh_password_container').show(); + return; } else { $('#ssh_password_container').hide(); } - var f = e.target.files[0]; + let f = e.target.files[0]; - var reader = new FileReader(); + let reader = new FileReader(); // Closure to capture the file information. reader.onload = function(e) { - var result = e.target.result; + let result = e.target.result; - var pki = forge.pki; + let pki = forge.pki; try { pki.privateKeyFromPem(result); @@ -259,8 +304,18 @@ function updateSetting(setting, value) { setSettings(settings); } +function removeSetting(setting) { + if (!settings) { + settings = {}; + } + + delete settings[setting]; + + setSettings(settings); +} + function setSettings(settings) { - var json = { + let json = { "event": "setSettings", "context": uuid, "payload": settings @@ -268,7 +323,49 @@ function setSettings(settings) { if (websocket) { websocket.send(JSON.stringify(json)); - } else { - console.log('Update:', json); } +} + +function updateGlobalSetting(id, val) { + globalSettings[id] = val; + + setGlobalSettings(globalSettings); +} + +function getGlobalSettings() { + let json = { + "event": "getGlobalSettings", + "context": uuid + }; + + if (websocket) { + websocket.send(JSON.stringify(json)); + } +} + +function setGlobalSettings(settings) { + let json = { + "event": "setGlobalSettings", + "context": uuid, + "payload": settings + }; + + if (websocket) { + websocket.send(JSON.stringify(json)); + } +} + +function didReceiveGlobalSettings(obj) { + globalSettings = getPropFromString(obj, 'payload.settings'); + + // Load defaults for fields not set + Object.keys(globalSettings).forEach(function(item) { + if (!(item in settings)) { + $('#' + item).val(globalSettings[item]); + } + }); +} + +function isAction(action) { + return actionInfo['action'] === action; } \ No newline at end of file