From 2453f817d7bdc83bfaaefa0aae1063ee31d01d47 Mon Sep 17 00:00:00 2001 From: TheYOSH Date: Sun, 11 Feb 2018 20:28:19 +0100 Subject: [PATCH] Fix adding new switches. #97 --- static/js/terrariumpi.js | 7 ++++++- views/switch_settings.tpl | 24 +++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/static/js/terrariumpi.js b/static/js/terrariumpi.js index a5a81dac2..387058849 100644 --- a/static/js/terrariumpi.js +++ b/static/js/terrariumpi.js @@ -1707,7 +1707,7 @@ function update_power_switch(data) { var name = item.name.replace(/switch_[0-9]+_/g,''); var field_value = $(item).val(); try { - if (field_value === '' || field_value === null) { + if (field_value === '' || field_value === null || field_value == 0 || field_value == '00:00') { var value = data[name]; if (!$.isArray(value)) { // Cast explicit to string to fix dropdown options @@ -1759,6 +1759,11 @@ function add_power_switch_setting_row(data) { }).on('change',function() { if (this.name.indexOf('_timer_enabled') >= 0) { $(this).parents('.x_content').find('.row.timer').toggle('true' === this.value); + if ('true' === this.value) { + $(this).parents('.x_content').find('.row.timer input').attr('required','required'); + } else { + $(this).parents('.x_content').find('.row.timer input').removeAttr('required'); + } } }); // Add on the bottom before the submit row diff --git a/views/switch_settings.tpl b/views/switch_settings.tpl index 55d83c6f1..0c4509b1c 100644 --- a/views/switch_settings.tpl +++ b/views/switch_settings.tpl @@ -169,19 +169,19 @@ @@ -208,7 +208,15 @@ minimumResultsForSearch: Infinity }).on('change',function() { if ('switch_[nr]_hardwaretype' === this.name) { - $(this).parents('.x_content').find('.row.dimmer').toggle('pwm-dimmer' === this.value || 'remote-dimmer' === this.value); + var dimmer = 'pwm-dimmer' === this.value || 'remote-dimmer' === this.value; + if (dimmer) { + $(this).parents('.x_content').find('.row.dimmer input').attr('required','required'); + } else { + $(this).parents('.x_content').find('.row.dimmer input').removeAttr('required'); + } + + + $(this).parents('.x_content').find('.row.dimmer').toggle(dimmer); var address_field = $("input[name='" + this.name.replace('hardwaretype','address') + "']"); address_field.off('change'); @@ -218,10 +226,16 @@ }); } } else if ('switch_[nr]_timer_enabled' === this.name) { + if ('true' === this.value) { + $(this).parents('.x_content').find('.row.timer input').attr('required','required'); + } else { + $(this).parents('.x_content').find('.row.timer input').removeAttr('required'); + } $(this).parents('.x_content').find('.row.timer').toggle('true' === this.value); } }).val(null).trigger('change'); + // Load existing switches $.get($('form').attr('action'),function(json_data){ $.each(json_data.switches, function(index,switch_data) {