Skip to content

Commit

Permalink
Fix adding new switches. #97
Browse files Browse the repository at this point in the history
  • Loading branch information
theyosh committed Feb 11, 2018
1 parent c1489fc commit 2453f81
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
7 changes: 6 additions & 1 deletion static/js/terrariumpi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions views/switch_settings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,19 @@
<div class="row timer" style="display:none;">
<div class="col-md-3 col-sm-3 col-xs-12 form-group">
<label for="switch_[nr]_timer_start">{{_('Timer start time')}}</label>
<input class="form-control" name="switch_[nr]_timer_start" placeholder="{{_('Timer start time')}}" required="required" type="text" pattern="[0-9:APM]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_start')}}">
<input class="form-control" name="switch_[nr]_timer_start" placeholder="{{_('Timer start time')}}" required="required" type="text" value="00:00" pattern="[0-9:APM]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_start')}}">
</div>
<div class="col-md-3 col-sm-3 col-xs-12 form-group">
<label for="switch_[nr]_timer_stop">{{_('Timer stop time')}}</label>
<input class="form-control" name="switch_[nr]_timer_stop" placeholder="{{_('Timer stop time')}}" required="required" type="text" pattern="[0-9:APM]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_stop')}}">
<input class="form-control" name="switch_[nr]_timer_stop" placeholder="{{_('Timer stop time')}}" required="required" type="text" value="00:00" pattern="[0-9:APM]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_stop')}}">
</div>
<div class="col-md-3 col-sm-2 col-xs-12 form-group">
<label for="switch_[nr]_timer_on_duration">{{_('Timer on duration')}}</label>
<input class="form-control" name="switch_[nr]_timer_on_duration" placeholder="{{_('Timer period on duration in minutes')}}" required="required" type="text" pattern="[0-9]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_on_duration')}}">
<input class="form-control" name="switch_[nr]_timer_on_duration" placeholder="{{_('Timer period on duration in minutes')}}" required="required" type="text" value="0" pattern="[0-9]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_on_duration')}}">
</div>
<div class="col-md-3 col-sm-2 col-xs-12 form-group">
<label for="switch_[nr]_timer_off_duration">{{_('Timer off duration')}}</label>
<input class="form-control" name="switch_[nr]_timer_off_duration" placeholder="{{_('Timer period off duration in minutes')}}" required="required" type="text" pattern="[0-9]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_off_duration')}}">
<input class="form-control" name="switch_[nr]_timer_off_duration" placeholder="{{_('Timer period off duration in minutes')}}" required="required" type="text" value="0" pattern="[0-9]+" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="{{translations.get_translation('switch_field_timer_off_duration')}}">
</div>
</div>
</div>
Expand All @@ -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');
Expand All @@ -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) {
Expand Down

0 comments on commit 2453f81

Please sign in to comment.