';
}
document.getElementById('divShadeList').innerHTML = divCfg;
let shadeControls = document.getElementById('divShadeControls');
shadeControls.innerHTML = divCtl;
// Attach the timer for setting the My Position for the shade.
let btns = shadeControls.querySelectorAll('div.my-button');
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener('mouseup', (event) => {
console.log(this);
console.log(event);
console.log('mouseup');
if (this.btnTimer) {
clearTimeout(this.btnTimer);
this.btnTimer = null;
}
let shadeId = parseInt(event.currentTarget.getAttribute('data-shadeid'), 10);
if (new Date().getTime() - this.btnDown > 2000) {
event.preventDefault();
}
else {
this.sendCommand(shadeId, 'my');
}
}, true);
btns[i].addEventListener('mousedown', (event) => {
if (this.btnTimer) return;
console.log(this);
console.log(event);
console.log('mousedown');
let shadeId = parseInt(event.currentTarget.getAttribute('data-shadeid'), 10);
let el = event.currentTarget.closest('.somfyShadeCtl');
this.btnDown = new Date().getTime();
if (parseInt(el.getAttribute('data-direction'), 10) === 0) {
this.btnTimer = setTimeout(() => {
// Open up the set My Position dialog. We will allow the user to change the position to match
// the desired position.
this.openSetMyPosition(shadeId);
}, 2000);
}
}, true);
btns[i].addEventListener('touchstart', (event) => {
let shadeId = parseInt(event.currentTarget.getAttribute('data-shadeid'), 10);
let el = event.currentTarget.closest('.somfyShadeCtl');
console.log('touchstart');
this.btnDown = new Date().getTime();
if (parseInt(el.getAttribute('data-direction'), 10) === 0) {
this.btnTimer = setTimeout(() => {
// Open up the set My Position dialog. We will allow the user to change the position to match
// the desired position.
this.openSetMyPosition(shadeId);
}, 2000);
}
}, true);
/*
btns[i].addEventListener('touchend', (event) => {
event.preventDefault(); // Make sure the idiot
console.log(this);
console.log(event);
if (this.btnTimer) {
clearTimeout(this.btnTimer);
this.btnTimer = null;
}
let shadeId = parseInt(event.currentTarget.getAttribute('data-shadeid'), 10);
if (new Date().getTime() - this.btnDown > 2000) {
event.preventDefault();
}
else {
this.sendCommand(shadeId, 'my');
}
}, true);
*/
}
};
closeShadePositioners() {
let ctls = document.querySelectorAll('.shade-positioner');
for (let i = 0; i < ctls.length; i++) {
console.log('Closing shade positioner');
ctls[i].remove();
}
}
openSetMyPosition(shadeId) {
if (typeof shadeId === 'undefined') {
return;
}
else {
let shade = document.querySelector(`div.somfyShadeCtl[data-shadeid="${shadeId}"]`);
if (shade) {
this.closeShadePositioners();
let currPos = parseInt(shade.getAttribute('data-position'), 10);
let myPos = parseInt(shade.getAttribute('data-mypos'), 10);
let elname = shade.querySelector(`.shadectl-name`);
let shadeName = elname.innerHTML;
let html = `
${shadeName}`;
if (myPos !== 255)
html += `
Current: ${myPos}%
`
html += `
`;
html += ``;
html += ``;
html += ``;
html += '
'
if (myPos === currPos)
html += ``;
else
html += ``;
html += ``;
html += `
`;
let div = document.createElement('div');
div.setAttribute('class', 'shade-positioner shade-my-positioner');
div.setAttribute('data-shadeid', shadeId);
div.style.height = 'auto';
div.innerHTML = html;
shade.appendChild(div);
let elTarget = div.querySelector('input#slidShadeTarget');
let elBtn = div.querySelector('button#btnSetMyPosition');
elTarget.addEventListener('change', (event) => {
console.log(`Target: ${elTarget.value} myPos: ${myPos}`);
div.querySelector('#spanShadeTarget').innerHTML = elTarget.value;
if (parseInt(elTarget.value, 10) === myPos) {
elBtn.innerHTML = 'Clear My Position';
elBtn.style.background = 'orangered';
}
else {
elBtn.innerHTML = 'Set My Position';
elBtn.style.background = '';
}
});
}
}
}
sendShadeMyPosition(shadeId, pos) {
console.log(`Sending My Position for shade id ${shadeId} to ${pos}`);
let overlay = waitMessage(document.getElementById('divContainer'));
putJSON('/setMyPosition', { shadeId: shadeId, target: pos }, (err, response) => {
this.closeShadePositioners();
overlay.remove();
console.log(response);
});
}
setLinkedRemotesList(shade) {
let divCfg = '';
for (let i = 0; i < shade.linkedRemotes.length; i++) {
let remote = shade.linkedRemotes[i];
divCfg += `