From 43ac16465b7539b82664f2ddb5937939d9c5db09 Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Sat, 7 Oct 2023 11:41:59 +0200
Subject: [PATCH] #187 - Add variables with pad names for current page

---
 main.js                        | 15 +++++++++++++++
 project.js                     | 27 +++++++++++++++++++++++++++
 receive/current_page_update.js |  2 ++
 receive/pad_name_update.js     |  6 ++++++
 receive/project_update.js      |  3 +++
 5 files changed, 53 insertions(+)

diff --git a/main.js b/main.js
index e3233ca..349696b 100644
--- a/main.js
+++ b/main.js
@@ -187,6 +187,21 @@ class ModuleInstance extends InstanceBase {
             this.log('debug', `Cannot handle incoming message ${data}`)
         }
     }
+
+    updateVariables() {
+        this.setVariableDefinitions([...Array(this.currentProject.getPadCount()).keys()].map(index => {
+            return {variableId: `pad-${index}`, name: `Pad Index ${index + 1}`}
+        }))
+    }
+
+    updateVariablesForCurrentPage() {
+        let pads = this.currentProject.getAllPadsOfCurrentPage();
+        for (let i in pads) {
+            let pad = pads[i];
+            this.log('debug', `Update variable 'pad-${pad.position}'`);
+            this.setVariableValues({ [`pad-${pad.position}`]: pad.name});
+        }
+    }
 }
 
 runEntrypoint(ModuleInstance, UpgradeScripts)
diff --git a/project.js b/project.js
index 0f54e19..bb3e2d7 100644
--- a/project.js
+++ b/project.js
@@ -44,6 +44,33 @@ module.exports = class Project {
     Pads
      */
 
+    /**
+     *
+     * @returns {number}
+     */
+    getPadCount() {
+        return this.data.settings.columns * this.data.settings.rows;
+    }
+
+    /**
+     *
+     * @returns {*[]}
+     */
+    getAllPadsOfCurrentPage() {
+        let pads = [];
+
+        for (const pageId in this.data.pages) {
+            const page = this.data.pages[pageId]
+            if (page.position === this.currentPage) {
+                for (const padId in page.pads) {
+                    const pad = page.pads[padId];
+                    pads.push(pad);
+                }
+            }
+        }
+        return pads;
+    }
+
     /**
      *
      * @param {string} oldName
diff --git a/receive/current_page_update.js b/receive/current_page_update.js
index a03d936..abcfe42 100644
--- a/receive/current_page_update.js
+++ b/receive/current_page_update.js
@@ -5,5 +5,7 @@ module.exports = class CurrentPageUpdate extends MessageExecutable {
     handleMessage(plugin, message) {
         plugin.currentProject.updateCurrentPage(message.newPage);
         plugin.checkFeedbacks();
+
+        plugin.updateVariablesForCurrentPage();
     }
 }
\ No newline at end of file
diff --git a/receive/pad_name_update.js b/receive/pad_name_update.js
index d7533f5..91f5f0f 100644
--- a/receive/pad_name_update.js
+++ b/receive/pad_name_update.js
@@ -4,5 +4,11 @@ module.exports = class PadNameUpdate extends MessageExecutable {
 
     handleMessage(plugin, message) {
         plugin.currentProject.updatePadName(message.oldValue, message.newValue);
+
+        let pad = plugin.currentProject.findPadById(message.pad);
+        if (pad.page === plugin.currentProject.getCurrentPage()) {
+            plugin.log('debug', `Update variable 'pad-${pad.position}'`);
+            plugin.setVariableValues({[`pad-${pad.position}`]: message.newValue});
+        }
     }
 }
\ No newline at end of file
diff --git a/receive/project_update.js b/receive/project_update.js
index 3520055..18518f6 100644
--- a/receive/project_update.js
+++ b/receive/project_update.js
@@ -6,5 +6,8 @@ module.exports = class ProjectUpdate extends MessageExecutable {
     handleMessage(plugin, message) {
         plugin.currentProject = new Project(message)
         plugin.checkFeedbacks();
+
+        plugin.updateVariables()
+        plugin.updateVariablesForCurrentPage();
     }
 }
\ No newline at end of file
-- 
GitLab