Add <tailbone-timepicker> component for oruga

This commit is contained in:
Lance Edgar 2024-05-14 12:17:50 -05:00
parent ec61444b3d
commit fb9bc01939
2 changed files with 76 additions and 0 deletions

View file

@ -27,6 +27,7 @@ Form Widgets
import json import json
import datetime import datetime
import decimal import decimal
import re
import colander import colander
from deform import widget as dfwidget from deform import widget as dfwidget
@ -249,6 +250,8 @@ class FalafelDateTimeWidget(dfwidget.DateTimeInputWidget):
""" """
template = 'datetime_falafel' template = 'datetime_falafel'
new_pattern = re.compile(r'^\d\d?:\d\d:\d\d [AP]M$')
def serialize(self, field, cstruct, **kw): def serialize(self, field, cstruct, **kw):
""" """ """ """
readonly = kw.get('readonly', self.readonly) readonly = kw.get('readonly', self.readonly)
@ -260,6 +263,13 @@ class FalafelDateTimeWidget(dfwidget.DateTimeInputWidget):
""" """ """ """
if pstruct == '': if pstruct == '':
return colander.null return colander.null
# nb. we now allow '4:20:00 PM' on the widget side, but the
# true node needs it to be '16:20:00' instead
if self.new_pattern.match(pstruct['time']):
time = datetime.datetime.strptime(pstruct['time'], '%I:%M:%S %p')
pstruct['time'] = time.strftime('%H:%M:%S')
return pstruct return pstruct

View file

@ -4,6 +4,7 @@
${self.make_numeric_input_component()} ${self.make_numeric_input_component()}
${self.make_tailbone_autocomplete_component()} ${self.make_tailbone_autocomplete_component()}
${self.make_tailbone_datepicker_component()} ${self.make_tailbone_datepicker_component()}
${self.make_tailbone_timepicker_component()}
</%def> </%def>
<%def name="make_numeric_input_component()"> <%def name="make_numeric_input_component()">
@ -461,3 +462,68 @@
</script> </script>
</%def> </%def>
<%def name="make_tailbone_timepicker_component()">
<% request.register_component('tailbone-timepicker', 'TailboneTimepicker') %>
<script type="text/x-template" id="tailbone-timepicker-template">
<o-timepicker :name="name"
v-model="orugaValue"
@update:model-value="orugaValueUpdated"
placeholder="Click to select ..."
icon="clock"
hour-format="12"
:time-formatter="formatTime" />
</script>
<script>
const TailboneTimepicker = {
template: '#tailbone-timepicker-template',
props: {
modelValue: [Date, String],
name: String,
},
data() {
return {
orugaValue: this.parseTime(this.modelValue),
}
},
watch: {
modelValue(to, from) {
this.orugaValue = this.parseTime(to)
},
},
methods: {
formatTime(value) {
if (!value) {
return null
}
return value.toLocaleTimeString('en-US')
},
parseTime(value) {
if (value.getHours) {
return value
}
let found = value.match(/^(\d\d):(\d\d):\d\d$/)
if (found) {
return new Date(null, null, null,
parseInt(found[1]), parseInt(found[2]))
}
},
orugaValueUpdated(value) {
this.$emit('update:modelValue', value)
},
},
}
</script>
</%def>