SODashServer/app/js/parts/lights.js
2016-07-28 13:57:16 +01:00

123 lines
2.9 KiB
JavaScript

'use strict';
/**
*
* User: Martin Donnelly
* Date: 2016-07-27
* Time: 09:25
*
*/
var path = 'http://localhost:3000/';
var LightModel = Backbone.Model.extend({
urlRoot: path + 'api/v1/lighting/',
initialize: function() {
this.set('status',false);
this.set('enabled',true);
},
turnOn: function() {
this.set('id','on');
this.save({light: this.get('on')},{
type: 'POST'
});
},
turnOff: function() {
this.set('id','off');
this.save({light: this.get('off')},{
type: 'POST'
});
},
turnUp: function() {
this.set('id','cmd');
this.save({light: this.get('up')},{
type: 'POST'
});
},
turnDown: function() {
this.set('id','cmd');
this.save({light: this.get('down')},{
type: 'POST'
});
},
disable: function() {
this.set('enabled',false);
},
enable: function() {
this.set('enabled',true);
}
});
var Light = Backbone.View.extend({
tagName: 'div',
events: {
'click .lightOn': 'turnOn',
'click .lightOff': 'turnOff',
'click .lightUp': 'turnUp',
'click .lightDown': 'turnDown'
},
initialize: function() {
var tStr;
_.bindAll(this, 'render', 'update', 'turnOn','turnOff', 'turnUp','turnDown','updateStatus','updateEnabled');
this.model.bind('change', this.update);
this.$onButton = this.$('#' + this.model.get('device') + 'LightOn');
this.$offButton = this.$('#' + this.model.get('device') + 'LightOff');
this.$upButton = this.$('#' + this.model.get('device') + 'Up');
this.$downButton = this.$('#' + this.model.get('device') + 'Down');
tStr = this.model.get('device')[0].toUpperCase() + this.model.get('device').substring(1);
this.$aux = this.$('#aux' + tStr);
// This.render();
},
render: function() {
},
update: function() {
if (this.model.hasChanged('status')) {
this.updateStatus();
}
if (this.model.hasChanged('enabled')) {
this.updateEnabled();
}
},
updateStatus: function() {
if (this.model.get('status') === true) {
// Lights are on..
this.$onButton.hide();
this.$offButton.show();
this.$aux.fadeIn(500);
} else {
this.$onButton.show();
this.$offButton.hide();
this.$aux.fadeOut();
}
},
updateEnabled: function() {
var enabled = this.model.get('enabled');
if (enabled) {
this.$el.removeClass('lostConnection');
} else {
this.$el.addClass('lostConnection');
}
this.$onButton.attr('disabled', !enabled);
this.$offButton.attr('disabled', !enabled);
this.$upButton.attr('disabled', !enabled);
this.$downButton.attr('disabled', !enabled);
},
turnOn: function() {
this.model.turnOn();
},
turnOff: function() {
this.model.turnOff();
},
turnUp: function() {
this.model.turnUp();
},
turnDown: function() {
this.model.turnDown();
}
});