Skip to content

Commit

Permalink
Merge pull request #6 from Tom-Hirschberger/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Tom-Hirschberger authored Nov 1, 2022
2 parents d42437c + 84f0dc7 commit 095af7b
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.project
node_modules
package-lock.json
package-lock.json
icons/*
scripts/*
147 changes: 130 additions & 17 deletions MMM-TouchButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,175 @@
Module.register('MMM-TouchButton', {

defaults: {
animationSpeed: 0,
classes: null,
buttons: [],
addEmptyTitle: false,
buttons: []
},

getScripts: function () {
return [this.file('node_modules/js-uuid/js-uuid.js')];
},

getStyles: function() {
return ['font-awesome.css', 'touch-button.css']
},

validateCondition: function(source, value, type){
if (type == "eq"){
return source === value
} else if (type == "incl"){
return source.includes(value)
} else if (type == "mt") {
return new RegExp(value).test(source)
} else if (type == "lt"){
return source < value
} else if (type == "le"){
return source <= value
} else if (type == "gt"){
return source > value
} else if (type == "ge"){
return source >= value
}

return false
},

getCurrentButtonProps: function(buttonConfig, buttonResults){
const self = this
let icon = buttonConfig["icon"] || null
let imgIcon = buttonConfig["imgIcon"] || null
let classes = []
if (typeof buttonConfig["classes"] !== "undefined"){
classes = buttonConfig["classes"].split(" ")
}

if (typeof buttonConfig["conditions"] !== "undefined"){
for (let curCondition of buttonConfig["conditions"]){
let source = curCondition["source"] || null
let type = curCondition["type"] || null
let value = curCondition["value"] || null

if((source != null) && (type != null) && (value != null)){
let curSource = buttonResults[source]

if(curSource != null){
if(self.validateCondition(curSource, value, type)){
icon = curCondition["icon"] || icon
imgIcon = curCondition["imgIcon"] || imgIcon
classes = []
if(typeof curCondition["classes"] !== "undefined"){
curCondition["classes"].split(" ").forEach(element => classes.push(element))
}
break
}
}
}
}
}

return [icon, imgIcon, classes]
},

getDom: function() {
const self = this
const wrapper = document.createElement('div')
wrapper.className = "touchButtonRootWrapper"
let moduleClasses = []

if(typeof self.config["classes"] !== "undefined"){
self.config["classes"].split(" ").forEach(element => moduleClasses.push(element))
}

wrapper.classList.add("touchButtonRootWrapper")
moduleClasses.forEach(element => wrapper.classList.add(element))

for(let curId = 0; curId < self.config.buttons.length; curId++){
let curButtonConfig = self.config.buttons[curId]

if ((typeof curButtonConfig["profiles"] !== "undefined") && (self.currentProfile != null)){
if (!curButtonConfig["profiles"].includes(self.currentProfile)) {
continue
}
}

let curCondButtonConfig = self.getCurrentButtonProps(curButtonConfig, self.results[curId] || {})

var buttonWrapper = document.createElement("div")
buttonWrapper.className="touchButton buttonWrapper"

let curTitle = null
if ((typeof curButtonConfig.title === "undefined") ||
(curButtonConfig.title === null)){
if (self.config.addEmptyTitle){
curTitle = "&nbsp;"
}
(curButtonConfig.title === null)){
if (self.config.addEmptyTitle){
curTitle = "&nbsp;"
}
} else {
curTitle = curButtonConfig.title
}

if (curTitle !== null){
curTitleObj = document.createElement("div")
curTitleObj.className = "touchButton button title title-"+curButtonConfig.name
curTitleObj.innerHTML = curTitle
let curTitleObj = document.createElement("div")
curTitleObj.className = "touchButton button title title-"+curButtonConfig.name
curTitleObj.innerHTML = curTitle

buttonWrapper.appendChild(curTitleObj)
}

let curButton = document.createElement("i")
curButton.className = curButtonConfig.icon+" touchButton button icon button-"+curButtonConfig.name
let curButton = null
if (curCondButtonConfig[1] != null ){
curButton = document.createElement("img")
curButton.setAttribute("src", curCondButtonConfig[1])
curButton.classList.add("imgIcon")
} else if (curCondButtonConfig[0] != null){
curButton = document.createElement("i")
curButton.className = curCondButtonConfig[0]
curButton.classList.add("icon")
}



if(curButton != null){
curButton.classList.add("touchButton")
curButton.classList.add("button")
curButton.classList.add("button-"+curButtonConfig.name)
curCondButtonConfig[2].forEach(element => curButton.classList.add(element))

curButton.addEventListener("click", ()=>{ self.sendSocketNotification("BUTTON_PRESSED", {"id": curId}) })
buttonWrapper.appendChild(curButton)
curButton.addEventListener("click", ()=>{ self.sendSocketNotification("BUTTON_PRESSED", {"moduleId": self.moduleId, "id": curId}) })
buttonWrapper.appendChild(curButton)
}
wrapper.appendChild(buttonWrapper)
}
return wrapper;
},

start: function () {
const self = this
self.moduleId = uuid.v4()
Log.info("Starting module: " + self.name);
self.sendSocketNotification('CONFIG', self.config)
self.sendSocketNotification('CONFIG', [self.moduleId, self.config])
self.results = {}
self.currentProfile = null
},

notificationReceived: function (notification, payload) {
const self = this
if (notification === "CHANGED_PROFILE") {
self.currentProfile = payload.to
self.updateDom(self.config.animationSpeed)
}
},

socketNotificationReceived: function (notification, payload) {
const self = this

if(notification === "SEND_NOTIFICATION"){
console.log("Sending notification to all other modules")
self.sendNotification(payload.notification, payload.payload)
if (self.moduleId === payload["moduleId"]){
if(notification === "SEND_NOTIFICATION"){
console.log(self.name+": Sending notification to all other modules")
self.sendNotification(payload.notification, payload.payload)
} else if (notification === "RESULT"){
self.results[payload.id] = payload
self.updateDom(self.config.animationSpeed)
}
}
},
})
Loading

0 comments on commit 095af7b

Please sign in to comment.