diff --git a/js/component/card/settings.js b/js/component/card/settings.js index 753cb20..10e1b57 100644 --- a/js/component/card/settings.js +++ b/js/component/card/settings.js @@ -135,25 +135,26 @@ beestat.component.card.settings.prototype.decorate_contents_ = function(parent) var temperature_profiles_range_begin = new beestat.component.input.text() .set_maxlength(10) + .set_requirements({ + 'type': 'date' + }) .set_icon('calendar'); - var temperature_profiles_range_begin_m = - moment(beestat.setting(temperature_profiles_range_begin_key)); - - if (temperature_profiles_range_begin_m.isValid() === true) { + if ( + beestat.setting(temperature_profiles_range_begin_key) !== undefined && + beestat.setting(temperature_profiles_range_begin_key) !== null + ) { temperature_profiles_range_begin.set_value( - temperature_profiles_range_begin_m.format('M/D/YYYY') + beestat.setting(temperature_profiles_range_begin_key) ); } temperature_profiles_range_begin.addEventListener('change', function() { - var m = moment(this.get_value()); var temperature_profiles_range_begin_value; - if (m.isValid() === true) { - this.set_value(m.format('M/D/YYYY')); - temperature_profiles_range_begin_value = m.format('YYYY-MM-DD'); + if (temperature_profiles_range_begin.meets_requirements() === true) { + temperature_profiles_range_begin_value = this.get_value(); } else { - this.set_value(''); + this.set_value('', false); temperature_profiles_range_begin_value = null; } diff --git a/js/component/input.js b/js/component/input.js index 3329802..ab09880 100644 --- a/js/component/input.js +++ b/js/component/input.js @@ -112,6 +112,14 @@ beestat.component.input.prototype.meets_requirements = function() { ) { return false; } + + if ( + this.get_value() !== undefined && + this.requirements_.type === 'date' && + moment(this.get_value()).isValid() === false + ) { + return false; + } } return true; diff --git a/js/component/input/text.js b/js/component/input/text.js index c4b433f..ef0fc68 100644 --- a/js/component/input/text.js +++ b/js/component/input/text.js @@ -86,13 +86,18 @@ beestat.component.input.text.prototype.decorate_ = function(parent) { * Set the value in the input field. Do not rerender; it's unnecessary. * * @param {string} value + * @param {boolean} dispatch_event Whether or not to dispatch the change + * event. Useful in situations where you need to change the value *inside* the + * chagne event. * * @return {beestat.component.input.text} This. */ -beestat.component.input.text.prototype.set_value = function(value) { +beestat.component.input.text.prototype.set_value = function(value, dispatch_event = true) { this.input_.value = value; - this.dispatchEvent('change'); + if (dispatch_event === true) { + this.dispatchEvent('change'); + } return this; };