This repository has been archived on 2026-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
huegasm/app/pods/components/add-group-modal/component.js
2015-10-25 22:55:09 -07:00

63 lines
1.5 KiB
JavaScript

import Em from 'ember';
export default Em.Component.extend({
actions: {
close: function(){
this.sendAction();
},
save: function(){
var newGroupData = {"name": this.get('groupName'), "lights": this.get('selectedLights')}, newGroupsData = this.get('groupsData');
Em.$.ajax(this.get('apiURL') + '/groups', {
data: JSON.stringify(newGroupData),
contentType: 'application/json',
type: 'POST'
});
// crappy code to redraw the lights
newGroupsData['9999'] = newGroupData;
this.setProperties({
updateGroupsData: true,
groupsData: newGroupsData
});
this.sendAction();
},
clickLight: function(id) {
var selectedLights = this.get('selectedLights');
if(selectedLights.contains(id)){
selectedLights.removeObject(id);
} else {
selectedLights.pushObject(id);
}
}
},
didInsertElement: function() {
var self = this;
Em.$(document).keypress(function(event) {
if(!self.get('saveDisabled') && event.which === 13) {
self.send('save');
}
});
},
groupName: null,
selectedLights: [],
onIsShowingModalChange: function(){
if(this.get('isShowingModal')){
this.setProperties({
selectedLights: [],
groupName: null
});
}
}.observes('isShowingModal'),
saveDisabled: function(){
return Em.isNone(this.get('groupName')) || Em.isEmpty(this.get('selectedLights')) || Em.isEmpty(this.get('groupName').trim());
}.property('groupName', 'selectedLights.[]')
});