1
0
mirror of https://github.com/beestat/app.git synced 2025-07-09 03:04:07 -04:00

Code cleanup; removed redundant state variables

This commit is contained in:
Jon Ziebell 2022-08-10 21:09:34 -04:00
parent 52ffa740e2
commit a579e26b14
5 changed files with 49 additions and 43 deletions

View File

@ -11,7 +11,6 @@ beestat.component.card.floor_plan_editor = function(thermostat_id) {
var change_function = beestat.debounce(function() { var change_function = beestat.debounce(function() {
// todo replace these with (if entity set active false?) // todo replace these with (if entity set active false?)
delete self.state_.active_group; delete self.state_.active_group;
delete self.state_.active_room;
self.rerender(); self.rerender();
@ -252,7 +251,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_drawing_pane_ = func
* @param {rocket.Elements} parent * @param {rocket.Elements} parent
*/ */
beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_ = function(parent) { beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_ = function(parent) {
if (this.state_.active_room !== undefined) { if (this.state_.active_room_entity !== undefined) {
this.decorate_info_pane_room_(parent); this.decorate_info_pane_room_(parent);
} else { } else {
this.decorate_info_pane_floor_(parent); this.decorate_info_pane_floor_(parent);
@ -389,16 +388,16 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu
}) })
.render(div); .render(div);
if (this.state_.active_room.name !== undefined) { if (this.state_.active_room_entity.get_room().name !== undefined) {
name_input.set_value(this.state_.active_room.name); name_input.set_value(this.state_.active_room_entity.get_room().name);
} }
name_input.addEventListener('input', function() { name_input.addEventListener('input', function() {
self.state_.active_room.name = name_input.get_value(); self.state_.active_room_entity.get_room().name = name_input.get_value();
self.floor_plan_.update_infobox(); self.floor_plan_.update_infobox();
}); });
name_input.addEventListener('change', function() { name_input.addEventListener('change', function() {
self.state_.active_room.name = name_input.get_value(); self.state_.active_room_entity.get_room().name = name_input.get_value();
self.update_floor_plan_(); self.update_floor_plan_();
}); });
@ -408,7 +407,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu
const elevation_input = new beestat.component.input.text() const elevation_input = new beestat.component.input.text()
.set_label('Elevation (feet)') .set_label('Elevation (feet)')
.set_placeholder(this.state_.active_group.elevation / 12) .set_placeholder(this.state_.active_group.elevation / 12)
.set_value(this.state_.active_room.elevation / 12 || '') .set_value(this.state_.active_room_entity.get_room().elevation / 12 || '')
.set_width('100%') .set_width('100%')
.set_maxlength('5') .set_maxlength('5')
.set_requirements({ .set_requirements({
@ -418,7 +417,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu
elevation_input.addEventListener('change', function() { elevation_input.addEventListener('change', function() {
if (elevation_input.meets_requirements() === true) { if (elevation_input.meets_requirements() === true) {
self.state_.active_room.elevation = elevation_input.get_value() * 12; self.state_.active_room_entity.get_room().elevation = elevation_input.get_value() * 12;
self.update_floor_plan_(); self.update_floor_plan_();
self.rerender(); self.rerender();
} else { } else {
@ -432,7 +431,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu
const height_input = new beestat.component.input.text() const height_input = new beestat.component.input.text()
.set_label('Ceiling Height (feet)') .set_label('Ceiling Height (feet)')
.set_placeholder(this.state_.active_group.height / 12) .set_placeholder(this.state_.active_group.height / 12)
.set_value(this.state_.active_room.height / 12 || '') .set_value(this.state_.active_room_entity.get_room().height / 12 || '')
.set_width('100%') .set_width('100%')
.set_maxlength('4') .set_maxlength('4')
.set_requirements({ .set_requirements({
@ -443,7 +442,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu
height_input.addEventListener('change', function() { height_input.addEventListener('change', function() {
if (height_input.meets_requirements() === true) { if (height_input.meets_requirements() === true) {
self.state_.active_room.height = height_input.get_value() * 12; self.state_.active_room_entity.get_room().height = height_input.get_value() * 12;
self.update_floor_plan_(); self.update_floor_plan_();
} else { } else {
height_input.set_value(''); height_input.set_value('');
@ -483,17 +482,17 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu
sensor_input.render(div); sensor_input.render(div);
if (self.state_.active_room.sensor_id !== undefined) { if (self.state_.active_room_entity.get_room().sensor_id !== undefined) {
sensor_input.set_value(self.state_.active_room.sensor_id); sensor_input.set_value(self.state_.active_room_entity.get_room().sensor_id);
} else { } else {
sensor_input.set_value(''); sensor_input.set_value('');
} }
sensor_input.addEventListener('change', function() { sensor_input.addEventListener('change', function() {
if (sensor_input.get_value() === '') { if (sensor_input.get_value() === '') {
delete self.state_.active_room.sensor_id; delete self.state_.active_room_entity.get_room().sensor_id;
} else { } else {
self.state_.active_room.sensor_id = Number(sensor_input.get_value()); self.state_.active_room_entity.get_room().sensor_id = Number(sensor_input.get_value());
} }
self.update_floor_plan_(); self.update_floor_plan_();
}); });

View File

@ -102,13 +102,13 @@ beestat.component.floor_plan.prototype.render = function(parent) {
this.keydown_handler_ = function(e) { this.keydown_handler_ = function(e) {
if (e.target.nodeName === 'BODY') { if (e.target.nodeName === 'BODY') {
if (e.key === 'Escape') { if (e.key === 'Escape') {
if (self.state_.active_room !== undefined) { if (self.state_.active_room_entity !== undefined) {
self.clear_room_(); self.clear_room_();
} }
} else if (e.key === 'Delete') { } else if (e.key === 'Delete') {
if (self.state_.active_point !== undefined) { if (self.state_.active_point_entity !== undefined) {
self.remove_point_(); self.remove_point_();
} else if (self.state_.active_room !== undefined) { } else if (self.state_.active_room_entity !== undefined) {
self.remove_room_(); self.remove_room_();
} }
} else if (e.key.toLowerCase() === 'r') { } else if (e.key.toLowerCase() === 'r') {
@ -121,7 +121,9 @@ beestat.component.floor_plan.prototype.render = function(parent) {
e.key.toLowerCase() === 'c' && e.key.toLowerCase() === 'c' &&
e.ctrlKey === true e.ctrlKey === true
) { ) {
self.state_.copied_room = beestat.clone(self.state_.active_room); self.state_.copied_room = beestat.clone(
self.state_.active_room_entity.get_room()
);
} else if ( } else if (
e.key.toLowerCase() === 'v' && e.key.toLowerCase() === 'v' &&
e.ctrlKey === true e.ctrlKey === true
@ -437,7 +439,7 @@ beestat.component.floor_plan.prototype.update_toolbar = function() {
.set_background_color(beestat.style.color.bluegray.base); .set_background_color(beestat.style.color.bluegray.base);
this.button_group_.add_button(remove_room_button); this.button_group_.add_button(remove_room_button);
if (this.state_.active_room !== undefined) { if (this.state_.active_room_entity !== undefined) {
remove_room_button remove_room_button
.set_background_hover_color(beestat.style.color.bluegray.light) .set_background_hover_color(beestat.style.color.bluegray.light)
.set_text_color(beestat.style.color.red.base) .set_text_color(beestat.style.color.red.base)
@ -472,8 +474,8 @@ beestat.component.floor_plan.prototype.update_toolbar = function() {
this.button_group_.add_button(remove_point_button); this.button_group_.add_button(remove_point_button);
if ( if (
this.state_.active_point !== undefined && this.state_.active_point_entity !== undefined &&
this.state_.active_room.points.length > 3 this.state_.active_room_entity.get_room().points.length > 3
) { ) {
remove_point_button remove_point_button
.set_background_hover_color(beestat.style.color.bluegray.light) .set_background_hover_color(beestat.style.color.bluegray.light)
@ -648,10 +650,10 @@ beestat.component.floor_plan.prototype.update_toolbar = function() {
*/ */
beestat.component.floor_plan.prototype.update_infobox = function() { beestat.component.floor_plan.prototype.update_infobox = function() {
const parts = []; const parts = [];
if (this.state_.active_room !== undefined) { if (this.state_.active_room_entity !== undefined) {
parts.push(this.state_.active_room.name || 'Unnamed Room'); parts.push(this.state_.active_room_entity.get_room().name || 'Unnamed Room');
parts.push( parts.push(
beestat.floor_plan.get_area_room(this.state_.active_room) beestat.floor_plan.get_area_room(this.state_.active_room_entity.get_room())
.toLocaleString() + ' sqft' .toLocaleString() + ' sqft'
); );
} else { } else {
@ -746,8 +748,8 @@ beestat.component.floor_plan.prototype.remove_room_ = function() {
const self = this; const self = this;
const index = this.state_.active_group.rooms.findIndex(function(active_room) { const index = this.state_.active_group.rooms.findIndex(function(room) {
return active_room === self.state_.active_room; return room === self.state_.active_room_entity.get_room();
}); });
if (this.state_.active_room_entity !== undefined) { if (this.state_.active_room_entity !== undefined) {
@ -785,10 +787,10 @@ beestat.component.floor_plan.prototype.clear_room_ = function() {
beestat.component.floor_plan.prototype.remove_point_ = function() { beestat.component.floor_plan.prototype.remove_point_ = function() {
this.save_buffer(); this.save_buffer();
if (this.state_.active_room.points.length > 3) { if (this.state_.active_room_entity.get_room().points.length > 3) {
for (let i = 0; i < this.state_.active_room.points.length; i++) { for (let i = 0; i < this.state_.active_room_entity.get_room().points.length; i++) {
if (this.state_.active_point === this.state_.active_room.points[i]) { if (this.state_.active_point_entity.get_point() === this.state_.active_room_entity.get_room().points[i]) {
this.state_.active_room.points.splice(i, 1); this.state_.active_room_entity.get_room().points.splice(i, 1);
if (this.state_.active_point_entity !== undefined) { if (this.state_.active_point_entity !== undefined) {
this.state_.active_point_entity.set_active(false); this.state_.active_point_entity.set_active(false);
} }

View File

@ -337,7 +337,7 @@ beestat.component.floor_plan_entity.point.prototype.set_active = function(active
// Inactivate any other active point. // Inactivate any other active point.
if ( if (
this.state_.active_point_entity !== undefined && this.state_.active_point_entity !== undefined &&
this.state_.active_point !== this.point_ this.state_.active_point_entity.get_point() !== this.point_
) { ) {
this.state_.active_point_entity.set_active(false); this.state_.active_point_entity.set_active(false);
} }
@ -349,12 +349,10 @@ beestat.component.floor_plan_entity.point.prototype.set_active = function(active
this.state_.active_wall_entity.set_active(false); this.state_.active_wall_entity.set_active(false);
} }
this.state_.active_point = this.point_;
this.state_.active_point_entity = this; this.state_.active_point_entity = this;
this.dispatchEvent('activate'); this.dispatchEvent('activate');
} else { } else {
delete this.state_.active_point;
delete this.state_.active_point_entity; delete this.state_.active_point_entity;
this.dispatchEvent('inactivate'); this.dispatchEvent('inactivate');

View File

@ -66,7 +66,6 @@ beestat.component.floor_plan_entity.prototype.decorate_polygon_ = function(paren
// Activate room on click if the mouse didn't move. // Activate room on click if the mouse didn't move.
if (this.enabled_ === true) { if (this.enabled_ === true) {
const mousedown_handler = function(e) { const mousedown_handler = function(e) {
console.log('mousedown');
self.mousedown_mouse_ = { self.mousedown_mouse_ = {
'x': e.clientX || e.touches[0].clientX, 'x': e.clientX || e.touches[0].clientX,
'y': e.clientY || e.touches[0].clientY 'y': e.clientY || e.touches[0].clientY
@ -76,7 +75,6 @@ beestat.component.floor_plan_entity.prototype.decorate_polygon_ = function(paren
// this.polygon_.addEventListener('touchstart', mousedown_handler); // this.polygon_.addEventListener('touchstart', mousedown_handler);
const mouseup_handler = function(e) { const mouseup_handler = function(e) {
console.log('mouseup');
if ( if (
self.mousedown_mouse_ !== undefined && self.mousedown_mouse_ !== undefined &&
(e.clientX || e.changedTouches[0].clientX) === self.mousedown_mouse_.x && (e.clientX || e.changedTouches[0].clientX) === self.mousedown_mouse_.x &&
@ -147,7 +145,10 @@ beestat.component.floor_plan_entity.prototype.decorate_points_ = function(parent
}); });
// Activate the currently active point (mostly for rerenders). // Activate the currently active point (mostly for rerenders).
if (self.state_.active_point === point) { if (
self.state_.active_point_entity !== undefined &&
self.state_.active_point_entity.get_point() === point
) {
point_entity.set_active(true); point_entity.set_active(true);
} }
@ -264,12 +265,11 @@ beestat.component.floor_plan_entity.room.prototype.set_active = function(active)
// Inactivate any other active room. // Inactivate any other active room.
if ( if (
this.state_.active_room_entity !== undefined && this.state_.active_room_entity !== undefined &&
this.state_.active_room !== this.room_ this.state_.active_room_entity.get_room() !== this.room_
) { ) {
this.state_.active_room_entity.set_active(false); this.state_.active_room_entity.set_active(false);
} }
this.state_.active_room = this.room_;
this.state_.active_room_entity = this; this.state_.active_room_entity = this;
this.dispatchEvent('activate'); this.dispatchEvent('activate');
@ -277,7 +277,6 @@ beestat.component.floor_plan_entity.room.prototype.set_active = function(active)
this.bring_to_front_(); this.bring_to_front_();
} else { } else {
delete this.state_.active_room;
delete this.state_.active_room_entity; delete this.state_.active_room_entity;
if (this.state_.active_wall_entity !== undefined) { if (this.state_.active_wall_entity !== undefined) {

View File

@ -98,7 +98,15 @@ beestat.component.floor_plan_entity.wall.prototype.add_point = function(e) {
projected_point projected_point
); );
this.state_.active_point = projected_point; const projected_point_entity = new beestat.component.floor_plan_entity.point(
this.floor_plan_,
this.state_
)
.set_room(room)
.set_point(projected_point);
// this.state_.active_point = projected_point;
this.state_.active_point_entity = projected_point_entity;
if (this.state_.active_wall_entity !== undefined) { if (this.state_.active_wall_entity !== undefined) {
this.state_.active_wall_entity.set_active(false); this.state_.active_wall_entity.set_active(false);
} }
@ -183,8 +191,8 @@ beestat.component.floor_plan_entity.wall.prototype.project_point_ = function(p,
dot = ((b.x - a.x) * (p.y - a.y)) - ((b.y - a.y) * (p.x - a.x)); dot = ((b.x - a.x) * (p.y - a.y)) - ((b.y - a.y) * (p.x - a.x));
return { return {
'x': a.x + (atob.x * t), 'x': Math.round(a.x + (atob.x * t)),
'y': a.y + (atob.y * t) 'y': Math.round(a.y + (atob.y * t))
}; };
}; };
@ -565,7 +573,7 @@ beestat.component.floor_plan_entity.wall.prototype.set_active = function(active)
this.state_.active_wall_entity = this; this.state_.active_wall_entity = this;
// Deactivate the active point. // Deactivate the active point.
if (this.state_.active_point !== undefined) { if (this.state_.active_point_entity !== undefined) {
this.state_.active_point_entity.set_active(false); this.state_.active_point_entity.set_active(false);
} }