From a579e26b14ba9f0480e8cb04f6b50077feadb139 Mon Sep 17 00:00:00 2001 From: Jon Ziebell Date: Wed, 10 Aug 2022 21:09:34 -0400 Subject: [PATCH] Code cleanup; removed redundant state variables --- js/component/card/floor_plan_editor.js | 27 ++++++++++---------- js/component/floor_plan.js | 34 +++++++++++++------------ js/component/floor_plan_entity/point.js | 4 +-- js/component/floor_plan_entity/room.js | 11 ++++---- js/component/floor_plan_entity/wall.js | 16 +++++++++--- 5 files changed, 49 insertions(+), 43 deletions(-) diff --git a/js/component/card/floor_plan_editor.js b/js/component/card/floor_plan_editor.js index 918e7c3..37b469b 100644 --- a/js/component/card/floor_plan_editor.js +++ b/js/component/card/floor_plan_editor.js @@ -11,7 +11,6 @@ beestat.component.card.floor_plan_editor = function(thermostat_id) { var change_function = beestat.debounce(function() { // todo replace these with (if entity set active false?) delete self.state_.active_group; - delete self.state_.active_room; self.rerender(); @@ -252,7 +251,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_drawing_pane_ = func * @param {rocket.Elements} 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); } else { this.decorate_info_pane_floor_(parent); @@ -389,16 +388,16 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu }) .render(div); - if (this.state_.active_room.name !== undefined) { - name_input.set_value(this.state_.active_room.name); + if (this.state_.active_room_entity.get_room().name !== undefined) { + name_input.set_value(this.state_.active_room_entity.get_room().name); } 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(); }); 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_(); }); @@ -408,7 +407,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu const elevation_input = new beestat.component.input.text() .set_label('Elevation (feet)') .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_maxlength('5') .set_requirements({ @@ -418,7 +417,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu elevation_input.addEventListener('change', function() { 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.rerender(); } 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() .set_label('Ceiling Height (feet)') .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_maxlength('4') .set_requirements({ @@ -443,7 +442,7 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu height_input.addEventListener('change', function() { 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_(); } else { height_input.set_value(''); @@ -483,17 +482,17 @@ beestat.component.card.floor_plan_editor.prototype.decorate_info_pane_room_ = fu sensor_input.render(div); - if (self.state_.active_room.sensor_id !== undefined) { - sensor_input.set_value(self.state_.active_room.sensor_id); + if (self.state_.active_room_entity.get_room().sensor_id !== undefined) { + sensor_input.set_value(self.state_.active_room_entity.get_room().sensor_id); } else { sensor_input.set_value(''); } sensor_input.addEventListener('change', function() { if (sensor_input.get_value() === '') { - delete self.state_.active_room.sensor_id; + delete self.state_.active_room_entity.get_room().sensor_id; } 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_(); }); diff --git a/js/component/floor_plan.js b/js/component/floor_plan.js index 39d65ea..a63c831 100644 --- a/js/component/floor_plan.js +++ b/js/component/floor_plan.js @@ -102,13 +102,13 @@ beestat.component.floor_plan.prototype.render = function(parent) { this.keydown_handler_ = function(e) { if (e.target.nodeName === 'BODY') { if (e.key === 'Escape') { - if (self.state_.active_room !== undefined) { + if (self.state_.active_room_entity !== undefined) { self.clear_room_(); } } else if (e.key === 'Delete') { - if (self.state_.active_point !== undefined) { + if (self.state_.active_point_entity !== undefined) { self.remove_point_(); - } else if (self.state_.active_room !== undefined) { + } else if (self.state_.active_room_entity !== undefined) { self.remove_room_(); } } else if (e.key.toLowerCase() === 'r') { @@ -121,7 +121,9 @@ beestat.component.floor_plan.prototype.render = function(parent) { e.key.toLowerCase() === 'c' && 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 ( e.key.toLowerCase() === 'v' && e.ctrlKey === true @@ -437,7 +439,7 @@ beestat.component.floor_plan.prototype.update_toolbar = function() { .set_background_color(beestat.style.color.bluegray.base); this.button_group_.add_button(remove_room_button); - if (this.state_.active_room !== undefined) { + if (this.state_.active_room_entity !== undefined) { remove_room_button .set_background_hover_color(beestat.style.color.bluegray.light) .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); if ( - this.state_.active_point !== undefined && - this.state_.active_room.points.length > 3 + this.state_.active_point_entity !== undefined && + this.state_.active_room_entity.get_room().points.length > 3 ) { remove_point_button .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() { const parts = []; - if (this.state_.active_room !== undefined) { - parts.push(this.state_.active_room.name || 'Unnamed Room'); + if (this.state_.active_room_entity !== undefined) { + parts.push(this.state_.active_room_entity.get_room().name || 'Unnamed Room'); 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' ); } else { @@ -746,8 +748,8 @@ beestat.component.floor_plan.prototype.remove_room_ = function() { const self = this; - const index = this.state_.active_group.rooms.findIndex(function(active_room) { - return active_room === self.state_.active_room; + const index = this.state_.active_group.rooms.findIndex(function(room) { + return room === self.state_.active_room_entity.get_room(); }); 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() { this.save_buffer(); - if (this.state_.active_room.points.length > 3) { - for (let i = 0; i < this.state_.active_room.points.length; i++) { - if (this.state_.active_point === this.state_.active_room.points[i]) { - this.state_.active_room.points.splice(i, 1); + if (this.state_.active_room_entity.get_room().points.length > 3) { + for (let i = 0; i < this.state_.active_room_entity.get_room().points.length; i++) { + if (this.state_.active_point_entity.get_point() === this.state_.active_room_entity.get_room().points[i]) { + this.state_.active_room_entity.get_room().points.splice(i, 1); if (this.state_.active_point_entity !== undefined) { this.state_.active_point_entity.set_active(false); } diff --git a/js/component/floor_plan_entity/point.js b/js/component/floor_plan_entity/point.js index d50c76d..c1e71db 100644 --- a/js/component/floor_plan_entity/point.js +++ b/js/component/floor_plan_entity/point.js @@ -337,7 +337,7 @@ beestat.component.floor_plan_entity.point.prototype.set_active = function(active // Inactivate any other active point. if ( 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); } @@ -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_point = this.point_; this.state_.active_point_entity = this; this.dispatchEvent('activate'); } else { - delete this.state_.active_point; delete this.state_.active_point_entity; this.dispatchEvent('inactivate'); diff --git a/js/component/floor_plan_entity/room.js b/js/component/floor_plan_entity/room.js index 2500e66..dfb5e22 100644 --- a/js/component/floor_plan_entity/room.js +++ b/js/component/floor_plan_entity/room.js @@ -66,7 +66,6 @@ beestat.component.floor_plan_entity.prototype.decorate_polygon_ = function(paren // Activate room on click if the mouse didn't move. if (this.enabled_ === true) { const mousedown_handler = function(e) { - console.log('mousedown'); self.mousedown_mouse_ = { 'x': e.clientX || e.touches[0].clientX, '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); const mouseup_handler = function(e) { - console.log('mouseup'); if ( self.mousedown_mouse_ !== undefined && (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). - 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); } @@ -264,12 +265,11 @@ beestat.component.floor_plan_entity.room.prototype.set_active = function(active) // Inactivate any other active room. if ( 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 = this.room_; this.state_.active_room_entity = this; this.dispatchEvent('activate'); @@ -277,7 +277,6 @@ beestat.component.floor_plan_entity.room.prototype.set_active = function(active) this.bring_to_front_(); } else { - delete this.state_.active_room; delete this.state_.active_room_entity; if (this.state_.active_wall_entity !== undefined) { diff --git a/js/component/floor_plan_entity/wall.js b/js/component/floor_plan_entity/wall.js index 0e8d5d0..3522b96 100644 --- a/js/component/floor_plan_entity/wall.js +++ b/js/component/floor_plan_entity/wall.js @@ -98,7 +98,15 @@ beestat.component.floor_plan_entity.wall.prototype.add_point = function(e) { 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) { 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)); return { - 'x': a.x + (atob.x * t), - 'y': a.y + (atob.y * t) + 'x': Math.round(a.x + (atob.x * 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; // 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); }