diff --git a/actions.js b/actions.js index 43e9caf150a2d90690651d436bdd0378048a5e8b..f66d62a81518984ecddadcd2f626936819945ddd 100644 --- a/actions.js +++ b/actions.js @@ -4,17 +4,19 @@ module.exports = function (self) { name: 'Trigger Pad', options: [ { - id: 'num', - type: 'number', - label: 'Test', - default: 5, - min: 0, - max: 100, + id: 'name', + type: 'textinput', + label: 'Pad Name', + default: '', }, ], callback: async (event) => { + const pad = self.currentProject.findPadByName(event.options.name); + if (!pad) { + self.log('warning', 'Cannot find a pad with name ' + event.options.name); + } const payload = { - 'pad': '8b7ef494-d735-45f9-ab84-c95e299298a6' + 'pad': pad.id }; self.sendToWebSocket('cart-action', payload) }, diff --git a/main.js b/main.js index 5e742221801027fce50cb5aa50ca07ec0399704b..4bd61306d56b5865a3b17dbaa869887a76658995 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,8 @@ const UpdateFeedbacks = require('./feedbacks') const UpdateVariableDefinitions = require('./variables') const WebSocket = require('ws') const ProjectUpdate = require("./receive/project_update"); +const Project = require("./project"); +const PadNameUpdate = require("./receive/pad_name_update"); class ModuleInstance extends InstanceBase { isInitialized = false @@ -12,10 +14,11 @@ class ModuleInstance extends InstanceBase { wsRegex = '^wss?:\\/\\/([\\da-z\\.-]+)(:\\d{1,5})?(?:\\/(.*))?$' messageHandlers = { - 'project-current': new ProjectUpdate() + 'project-current': new ProjectUpdate(), + 'pad-name-changed': new PadNameUpdate() }; - currentProject = {} + currentProject = new Project({}); constructor(internal) { super(internal) @@ -152,8 +155,14 @@ class ModuleInstance extends InstanceBase { } else { this.log('debug', `Cannot handle incoming message of type ${message.type}`) } + } else if (message.updateType != null) { + if (this.messageHandlers[message.updateType] != null) { + this.messageHandlers[message.updateType].handleMessage(this, message) + } else { + this.log('debug', `Cannot handle incoming message of updateType ${message.updateType}`) + } } else { - this.log('debug', `Cannot handle incoming message ${message}`) + this.log('debug', `Cannot handle incoming message ${data}`) } } } diff --git a/project.js b/project.js new file mode 100644 index 0000000000000000000000000000000000000000..7ce958babb34e9e31928a4238c68728ca208c137 --- /dev/null +++ b/project.js @@ -0,0 +1,35 @@ +module.exports = class Project { + constructor(data) { + this.data = data + this.padCache = {} + + // Prefill with current project + if (data) { + console.log('Create pad cache') + for (const pageId in data.pages) { + const page = data.pages[pageId] + for (const padId in page.pads) { + const pad = page.pads[padId]; + if (pad.name !== "") { + this.padCache[pad.name] = pad; + console.log(`Add pad "${pad.name}" to cache`) + } + } + } + } + } + + /** + * + * @param {string} oldName + * @param {string} newName + */ + updatePadName(oldName, newName) { + this.padCache[newName] = this.padCache[oldName]; + delete this.padCache[oldName]; + } + + findPadByName(name) { + return this.padCache[name]; + } +} \ No newline at end of file diff --git a/receive/message_executable.js b/receive/message_executable.js index 34ecf989b267fc5c523b6a5d63627759ce9b95db..5d0039fec3131ed7f3a94954de21b5f5ae17efc7 100644 --- a/receive/message_executable.js +++ b/receive/message_executable.js @@ -1,4 +1,9 @@ module.exports = class MessageExecutable { + /** + * + * @param {ModuleInstance} plugin + * @param {Object} message + */ handleMessage(plugin, message) { // Abstract method } diff --git a/receive/pad_name_update.js b/receive/pad_name_update.js new file mode 100644 index 0000000000000000000000000000000000000000..d7533f5e8ed0312732186edf0810105cef28e3ff --- /dev/null +++ b/receive/pad_name_update.js @@ -0,0 +1,8 @@ +const MessageExecutable = require("./message_executable"); + +module.exports = class PadNameUpdate extends MessageExecutable { + + handleMessage(plugin, message) { + plugin.currentProject.updatePadName(message.oldValue, message.newValue); + } +} \ No newline at end of file diff --git a/receive/project_update.js b/receive/project_update.js index 219e055f04b96e393e1b5a091d5ace6f8ff863fd..80a9ebebb94a593cad754d7e7253cb75ca0b5c8a 100644 --- a/receive/project_update.js +++ b/receive/project_update.js @@ -1,8 +1,9 @@ const MessageExecutable = require("./message_executable"); +const Project = require("../project"); module.exports = class ProjectUpdate extends MessageExecutable { handleMessage(plugin, message) { - plugin.currentProject = message.payload + plugin.currentProject = new Project(message) } } \ No newline at end of file