-
-
Save kasperkamperman/97ed04b4e6f33e10acfff85dd8f726f5 to your computer and use it in GitHub Desktop.
if I add multiple of these fields they influence eachother... So changing time in one changes it in all fields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<field-dayopeningtime> | |
<style> | |
label | |
{ | |
display:block; | |
} | |
</style> | |
<div class="uk-grid uk-grid-match uk-grid-gutter"> | |
<div class="uk-width-medium-1-1 uk-grid uk-grid-match"> | |
<div class="uk-width-medium-1-6"> | |
<label><i class="uk-icon-clock-o"></i> <span class="uk-text-bold">{ value.dayOfWeek }</span></label> | |
</div> | |
<div class="uk-width-medium-2-6"> | |
<label><span class="uk-text-bold">{ App.i18n.get('Opening') }</span></label> | |
<cp-field id="open_field" type="time" bind="value.opens"></cp-field> | |
</div> | |
<div class="uk-width-medium-2-6"> | |
<label><span class="uk-text-bold">{ App.i18n.get('Closing') }</span></label> | |
<cp-field id="closes_field" type="time" bind="value.closes"></cp-field> | |
</div> | |
<div class="uk-width-medium-1-6"> | |
<label for="{ id }"><span class="uk-text-bold">{ App.i18n.get('Closed all day') }</span></label> | |
<div> | |
<input id="{ id }" type="checkbox"/> | |
</div> | |
</div> | |
</div> | |
</div> | |
<script> | |
var $this = this; | |
this.id = 'switch' + Math.ceil(Math.random() * 10000000); | |
this.value = {}; | |
// remember | |
this.lastValue = {}; | |
riot.util.bind(this); | |
this.$updateValue = function(value, field) { | |
console.log("updateValue") | |
if (!App.Utils.isObject(value) || Array.isArray(value)) { | |
value = { opens: null, closes: null, dayOfWeek: null }; | |
} | |
if (JSON.stringify(this.value) != JSON.stringify(value)) { | |
this.value = value; | |
this.update(); | |
} | |
//console.log(this.value); | |
}.bind(this); | |
this.on('bindingupdated', function() { | |
console.log("bindingupdated"); | |
// if we enter a value, uncheck box | |
// this is jquery but ref attribute can work too | |
//this.refs.input.setAttribute('required', 'required'); | |
//if(this.value.opens != null || this.value.closed !=null ) $('#' + $this.id).prop("checked", false); | |
$this.$setValue(this.value); | |
}); | |
this.on("mount", function () { | |
console.log("mount"); | |
if (opts.weekDayNumber !== undefined && opts.weekDayNumber !== null) { | |
var weekday = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; | |
this.value.dayOfWeek = weekday[opts.weekDayNumber]; | |
console.log(opts.weekDayNumber + ":" + this.value.dayOfWeek); | |
this.update(); | |
} | |
}); | |
$(document).ready(function () { | |
$('#' + $this.id).change(function () { | |
console.log("checkbox changed:"+ $this.id); | |
if ($(this).is(':checked')) { | |
// remember values already filled in by making a copy | |
$this.lastValue = Object.assign({}, $this.value); | |
$this.value.opens = null; | |
$this.value.closes = null; | |
$this.$setValue($this.value); | |
} | |
else { | |
// put remembered values back | |
$this.value = $this.lastValue; | |
$this.$setValue($this.value); | |
} | |
}); | |
}); | |
</script> | |
</field-dayopeningtime> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment