Skip to content

Commit

Permalink
Bugfix: Finaly fixed all Editor Problems, copied config.
Browse files Browse the repository at this point in the history
  • Loading branch information
mentalilll committed Aug 12, 2024
1 parent 8bb530f commit d5b003d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 68 deletions.
115 changes: 54 additions & 61 deletions dist/ha-vpd-chart-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,17 @@ export class HaVpdChartEditor extends HTMLElement {
setConfig(config) {
let loadedConfig = {...config};
this.config = this.initializeDefaults(loadedConfig);
//this.initializeDefaults();

/* this.config = config;
CONFIG_KEYS.forEach(key => {
if (key in config) {
this[key] = config[key];
}
});
if (this.config.calculateVPD) {
this.calculateVPD = new Function('Tleaf', 'Tair', 'RH', this.config.calculateVPD);
}*/
if (this.config.calculateVPD) {
this.calculateVPD = new Function('Tleaf', 'Tair', 'RH', this.config.calculateVPD);
}
}

handleValueChange = (ev) => {
const target = ev.target;
const configValue = target.getAttribute('data-configvalue');
let value = target.type === 'checkbox' ? target.checked : target.value;

let configCopy = this.copyConfig();
if (typeof value === 'string' && !isNaN(value)) {
value = this.toFixedNumber(value);
}
Expand All @@ -278,15 +268,13 @@ export class HaVpdChartEditor extends HTMLElement {
if (value === "") {
value = undefined;
}
if (Object.isExtensible(this.config)) {
if (this.config[configValue] !== value) {
this.config[configValue] = value;
fireEvent(this, 'config-changed', {config: this.config});
}
} else {
this.config = {...this.config, [configValue]: value};
fireEvent(this, 'config-changed', {config: this.config});

if (configCopy[configValue] !== value) {
configCopy[configValue] = value;
fireEvent(this, 'config-changed', {config: configCopy});
}
this.config = configCopy;

}

handleVPDPhaseChange = (ev) => {
Expand Down Expand Up @@ -548,24 +536,24 @@ export class HaVpdChartEditor extends HTMLElement {
this.multiRange.on("changed", (event) => {

if (event.detail.idx === undefined || event.detail.value === undefined) return;

let configCopy = this.copyConfig();
const idx = event.detail.idx;
let value = this.toFixedNumber(event.detail.value);
let newVpdPhases = [...this._vpd_phases];
if (newVpdPhases[idx + 1] !== undefined) {
newVpdPhases[idx + 1] = {
...newVpdPhases[idx + 1],

if (configCopy.vpd_phases[idx + 1] !== undefined) {
configCopy.vpd_phases[idx + 1] = {
...configCopy.vpd_phases[idx + 1],
lower: this.toFixedNumber(value),
};
}

if (newVpdPhases[idx] !== undefined) {
newVpdPhases[idx] = {
...newVpdPhases[idx],
if (configCopy.vpd_phases[idx] !== undefined) {
configCopy.vpd_phases[idx] = {
...configCopy.vpd_phases[idx],
upper: this.toFixedNumber(value),
};
}
this.config.vpd_phases = newVpdPhases;
this.config = configCopy;
fireEvent(this, 'config-changed', {config: this.config});
});

Expand Down Expand Up @@ -605,7 +593,7 @@ export class HaVpdChartEditor extends HTMLElement {
}

const updateSensors = (index, property, target) => {
let newSensors = [...this._sensors];
let configCopy = this.copyConfig();
let value = target.type === 'checkbox' ? target.checked : target.value;

if (typeof value === 'string' && !isNaN(value)) {
Expand All @@ -627,17 +615,19 @@ export class HaVpdChartEditor extends HTMLElement {
if (value === "") {
value = undefined;
}
newSensors[index][property] = value;
this.config.sensors = newSensors;


configCopy.sensors[index][property] = value;
this.config = configCopy;
fireEvent(this, 'config-changed', {config: this.config});
};
if (this._sensors.length !== 0) {
this._sensors.forEach((sensor, index) => {
if (this.config.sensors.length !== 0) {
this.config.sensors.forEach((sensor, index) => {
const container = document.createElement('div');
container.style = "border: 1px solid rgba(127,127,127,0.3); padding: 5px; border-radius: 15px;";

const fields = ['Name', 'Temperature Sensor*', 'Leaf Temperature Sensor', 'Humidity Sensor*', 'VPD Helper', 'Calculated RH?'];
const properties = ['name', 'temperature', 'leaf_temperature', 'humidity', 'vpd_helper', 'show_calculated_rh'];
const fields = ['Name', 'Temperature Sensor*', 'Leaf Temperature Sensor', 'Humidity Sensor*', /*'VPD Helper',*/ 'Calculated RH?'];
const properties = ['name', 'temperature', 'leaf_temperature', 'humidity', /*'vpd_helper',*/ 'show_calculated_rh'];

fields.forEach((field, i) => {
let element;
Expand All @@ -654,10 +644,10 @@ export class HaVpdChartEditor extends HTMLElement {
removeButton.innerHTML = 'X';
removeButton.className = "removeButton";
removeButton.addEventListener('click', () => {
if (this._sensors.length === 1) return;
let newSensors = [...this._sensors];
newSensors.splice(index, 1);
this.config.sensors = newSensors;
if (this.config.sensors.length === 1) return;
let copyConfig = this.copyConfig();
copyConfig.sensors.splice(index, 1);
this.config = copyConfig;
fireEvent(this, 'config-changed', {config: this.config});
this.initSensors();
});
Expand All @@ -669,12 +659,12 @@ export class HaVpdChartEditor extends HTMLElement {
addButton.innerHTML = 'Add Sensor';
addButton.className = 'addButton';
addButton.addEventListener('click', () => {
let newSensors = [...this._sensors];
newSensors[newSensors.length] = [
let configCopy = this.copyConfig();
configCopy.sensors[configCopy.sensors.length] = [
{name: '', temperature: '', humidity: '', leaf_temperature: null, vpd_helper: false, show_calculated_rh: false}
];

this.config.sensors = newSensors;
this.config = configCopy;
fireEvent(this, 'config-changed', {config: this.config});
this.initSensors();
sensorEditor.parentElement.parentElement.style.maxHeight = `fit-content`;
Expand Down Expand Up @@ -709,18 +699,17 @@ export class HaVpdChartEditor extends HTMLElement {
if (this._vpd_phases.length === 1) {
return;
}
let newVpdPhases = [...this._vpd_phases];
newVpdPhases.splice(index, 1);
this.config.vpd_phases = newVpdPhases;
let copyConfig = this.copyConfig();
copyConfig.vpd_phases.splice(index, 1);
this.config = copyConfig;
fireEvent(this, 'config-changed', {config: this.config});

let rangesArray = this.generateRangesArray(newVpdPhases);
let rangesArray = this.generateRangesArray(copyConfig.vpd_phases);
this.multiRange.update(rangesArray);
container.remove();
if (index === this._vpd_phases.length) {
let newVpdPhases = [...this._vpd_phases];
delete newVpdPhases[index - 1].upper;
this.config.vpd_phases = newVpdPhases;
delete copyConfig.vpd_phases[index - 1].upper;
this.config = copyConfig;
fireEvent(this, 'config-changed', {config: this.config});
}
this.initColorEditor();
Expand All @@ -734,17 +723,17 @@ export class HaVpdChartEditor extends HTMLElement {
if (ev.target.value == null) {
return;
}
let newVpdPhases = [...this._vpd_phases];
let idx = index;
newVpdPhases[idx] = {
...newVpdPhases[idx],
let copyConfig = this.copyConfig();
copyConfig.vpd_phases[idx] = {
...this.copyConfig().vpd_phases[idx],
color: ev.target.value
};

this.config.vpd_phases = newVpdPhases;
let vpdPhases = [...newVpdPhases];
let vpdPhases = [...copyConfig.vpd_phases];
let rangesArray = this.generateRangesArray(vpdPhases);
this.multiRange.update(rangesArray);
this.config = copyConfig;
fireEvent(this, 'config-changed', {config: this.config});
});
input.addEventListener('input', this.handleVPDPhaseChange);
Expand All @@ -765,7 +754,8 @@ export class HaVpdChartEditor extends HTMLElement {
addButton.innerHTML = 'Add Phase';
addButton.className = 'addButton';
addButton.addEventListener('click', () => {
let newVpdPhases = [...this._vpd_phases];
let copyConfig = this.copyConfig();
let newVpdPhases = [...copyConfig.vpd_phases];
if (newVpdPhases.length === 0) {
console.error('No phases exist');
return;
Expand All @@ -790,7 +780,8 @@ export class HaVpdChartEditor extends HTMLElement {
let rangesArray = this.generateRangesArray(newVpdPhases);

this.multiRange.update(rangesArray);
this.config.vpd_phases = newVpdPhases;
copyConfig.vpd_phases = newVpdPhases;
this.config = copyConfig;
fireEvent(this, 'config-changed', {
config: this.config
});
Expand All @@ -817,14 +808,16 @@ export class HaVpdChartEditor extends HTMLElement {
}

resortPhases() {
let newVpdPhases = [...this._vpd_phases];
let copyConfig = this.copyConfig();
let newVpdPhases = [...copyConfig.vpd_phases];
newVpdPhases.forEach((phase, index) => {
if (newVpdPhases[index + 1] !== undefined) {
newVpdPhases[index + 1].lower = phase.upper;
}
});

this.config.vpd_phases = newVpdPhases;
copyConfig.vpd_phases = newVpdPhases;
this.config = copyConfig;
fireEvent(this, 'config-changed', {config: this.config});
}

Expand Down
2 changes: 1 addition & 1 deletion dist/ha-vpd-chart.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Set version for the card
window.vpdChartVersion = "1.4.8";
window.vpdChartVersion = "1.4.9";

import {methods} from './methods.js';
import {chart} from './chart.js';
Expand Down
9 changes: 3 additions & 6 deletions dist/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ export const methods = {
}
return offset;
},
checkIfFileExists(url) {
let http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status != 404;
},
copyConfig() {
return JSON.parse(JSON.stringify(this.config));
}
}

0 comments on commit d5b003d

Please sign in to comment.