diff options
Diffstat (limited to 'src/js/temperature.js')
-rw-r--r-- | src/js/temperature.js | 116 |
1 files changed, 69 insertions, 47 deletions
diff --git a/src/js/temperature.js b/src/js/temperature.js index 3a394a1..1e35ddf 100644 --- a/src/js/temperature.js +++ b/src/js/temperature.js @@ -14,8 +14,10 @@ * limitations under the License. */ -var left = 22; -var right = 22; +var values = { + leftTemperature: 22, + rightTemperature: 22, +} var lowTemperature = 15; var hiTemperature = 30; @@ -24,6 +26,12 @@ var temperatures = []; var isScrolling; var elementHeight; +var controls = { + leftTemperatureNode: null, + rightTemperatureNode: null, +}; + + function createTemperatureElement() { var element = document.createElement('div'); element.classList = ['temperature']; @@ -32,7 +40,21 @@ function createTemperatureElement() { return element; } -function update(node, index) { +export function update(path, value) { + var temperature, node; + if (path == PATHS.rightSeatTemperature) { + values.rightTemperature = value; + temperature = values.rightTemperature; + node = controls.rightTemperatureNode; + } else { + values.leftTemperature = value; + temperature = values.leftTemperature; + node = controls.leftTemperatureNode; + } + setSemperature(node, temperatures.indexOf(temperature)); +} + +function setSemperature(node, index) { node.scrollTop = index*elementHeight; for( var i=0; i<node.children.length; i++) { @@ -41,49 +63,49 @@ function update(node, index) { node.children[index].setAttribute('enabled', true); } -module.exports = { - left: function(node) { - clearTimeout(isScrolling); - - isScrolling = setTimeout(function(){ - var index = Math.round(node.scrollTop/elementHeight); - left = temperatures[index]; - update(node, index); - console.log('LEFT', left); - }, 100); - }, - right: function(node) { - clearTimeout(isScrolling); - - isScrolling = setTimeout(function(){ - var index = Math.round(node.scrollTop/elementHeight); - right = temperatures[index]; - update(node, index); - console.log('RIGHT', right); - }, 100); - }, - init: function() { - var leftTemperature = document.getElementById('lefttemperature'); - var rightTemperature = document.getElementById('righttemperature'); - elementHeight = leftTemperature.offsetHeight/2; - - for( var i=lowTemperature; i<=hiTemperature; i++) { - var element = createTemperatureElement(); - if( i === lowTemperature) { - element.innerHTML = 'LO'; - } else if( i === hiTemperature ) { - element.innerHTML = 'HI'; - } else { - element.innerHTML = i+'º'; - } - leftTemperature.appendChild(element); - rightTemperature.appendChild(element.cloneNode(true)); - temperatures[temperatures.length] = i; - } - leftTemperature.appendChild(createTemperatureElement()); - rightTemperature.appendChild(createTemperatureElement()); +export function left() { + clearTimeout(isScrolling); + + isScrolling = setTimeout(function() { + var index = Math.round(controls.leftTemperatureNode.scrollTop / elementHeight); + values.leftTemperature = temperatures[index]; + console.log('LEFT', values.leftTemperature); + KUKSA.set(PATHS.leftSeatTemperature, values.leftTemperature); + }, 100); +} + +export function right() { + clearTimeout(isScrolling); + + isScrolling = setTimeout(function() { + var index = Math.round(controls.rightTemperatureNode.scrollTop / elementHeight); + values.rightTemperature = temperatures[index]; + console.log('RIGHT', values.rightTemperature); + KUKSA.set(PATHS.rightSeatTemperature, values.rightTemperature); + }, 100); +} - update(leftTemperature, temperatures.indexOf(left)); - update(rightTemperature, temperatures.indexOf(right)); +export function init() { + controls.leftTemperatureNode = document.getElementById('lefttemperature'); + controls.rightTemperatureNode = document.getElementById('righttemperature'); + elementHeight = controls.leftTemperatureNode.offsetHeight / 2; + + for (var i = lowTemperature; i <= hiTemperature; i++) { + var element = createTemperatureElement(); + if (i === lowTemperature) { + element.innerHTML = 'LO'; + } else if (i === hiTemperature) { + element.innerHTML = 'HI'; + } else { + element.innerHTML = i + 'º'; + } + controls.leftTemperatureNode.appendChild(element); + controls.rightTemperatureNode.appendChild(element.cloneNode(true)); + temperatures[temperatures.length] = i; } -}
\ No newline at end of file + controls.leftTemperatureNode.appendChild(createTemperatureElement()); + controls.rightTemperatureNode.appendChild(createTemperatureElement()); + + setSemperature(controls.leftTemperatureNode, temperatures.indexOf(values.leftTemperature)); + setSemperature(controls.rightTemperatureNode, temperatures.indexOf(values.rightTemperature)); +} |