Add cleared/selected callbacks for jquery autocomplete in forms2

This commit is contained in:
Lance Edgar 2017-12-05 20:36:57 -06:00
parent bb12c5107c
commit 789bdef190
3 changed files with 42 additions and 2 deletions

View file

@ -35,7 +35,7 @@ from sqlalchemy import orm
from sqlalchemy.ext.associationproxy import AssociationProxy, ASSOCIATION_PROXY from sqlalchemy.ext.associationproxy import AssociationProxy, ASSOCIATION_PROXY
from rattail.time import localtime from rattail.time import localtime
from rattail.util import prettify, pretty_boolean, pretty_hours from rattail.util import prettify, pretty_boolean, pretty_hours, pretty_quantity
import colander import colander
import deform import deform
@ -472,6 +472,8 @@ class Form(object):
self.set_widget(key, dfwidget.CheckboxWidget()) self.set_widget(key, dfwidget.CheckboxWidget())
elif type_ == 'currency': elif type_ == 'currency':
self.set_renderer(key, self.render_currency) self.set_renderer(key, self.render_currency)
elif type_ == 'quantity':
self.set_renderer(key, self.render_quantity)
elif type_ == 'enum': elif type_ == 'enum':
self.set_renderer(key, self.render_enum) self.set_renderer(key, self.render_enum)
elif type_ == 'codeblock': elif type_ == 'codeblock':
@ -552,6 +554,7 @@ class Form(object):
# create form # create form
form = deform.Form(schema, **kwargs) form = deform.Form(schema, **kwargs)
form.tailbone_form = self
# set readonly widget where applicable # set readonly widget where applicable
for field in self.readonly_fields: for field in self.readonly_fields:
@ -627,6 +630,12 @@ class Form(object):
return "(${:0,.2f})".format(0 - value) return "(${:0,.2f})".format(0 - value)
return "${:0,.2f}".format(value) return "${:0,.2f}".format(value)
def render_quantity(self, obj, field):
value = self.obtain_value(obj, field)
if value is None:
return ""
return pretty_quantity(value)
def render_enum(self, record, field_name): def render_enum(self, record, field_name):
value = self.obtain_value(record, field_name) value = self.obtain_value(record, field_name)
if value is None: if value is None:

View file

@ -41,7 +41,9 @@ class ReadonlyWidget(dfwidget.HiddenWidget):
def serialize(self, field, cstruct, **kw): def serialize(self, field, cstruct, **kw):
if cstruct in (colander.null, None): if cstruct in (colander.null, None):
cstruct = '' cstruct = ''
return HTML.tag('span', cstruct) + tags.hidden(field.name, value=cstruct, id=field.oid) # TODO: is this hacky?
text = field.parent.tailbone_form.render_field_value(field.name)
return HTML.tag('span', text) + tags.hidden(field.name, value=cstruct, id=field.oid)
class JQueryDateWidget(dfwidget.DateInputWidget): class JQueryDateWidget(dfwidget.DateInputWidget):
@ -93,6 +95,8 @@ class JQueryAutocompleteWidget(dfwidget.AutocompleteInputWidget):
requirements = None requirements = None
field_display = "" field_display = ""
service_url = None service_url = None
cleared_callback = None
selected_callback = None
default_options = ( default_options = (
('autoFocus', True), ('autoFocus', True),
@ -117,6 +121,8 @@ class JQueryAutocompleteWidget(dfwidget.AutocompleteInputWidget):
kw['options'] = json.dumps(options) kw['options'] = json.dumps(options)
kw['field_display'] = self.field_display kw['field_display'] = self.field_display
kw['cleared_callback'] = self.cleared_callback
kw['selected_callback'] = self.selected_callback
tmpl_values = self.get_template_values(field, cstruct, kw) tmpl_values = self.get_template_values(field, cstruct, kw)
template = readonly and self.readonly_template or self.template template = readonly and self.readonly_template or self.template
return field.renderer(template, **tmpl_values) return field.renderer(template, **tmpl_values)

View file

@ -39,6 +39,8 @@
$('#' + oid + '-display span:first').text(ui.item.label); $('#' + oid + '-display span:first').text(ui.item.label);
$('#' + oid + '-textbox').hide(); $('#' + oid + '-textbox').hide();
$('#' + oid + '-display').show(); $('#' + oid + '-display').show();
$('#' + oid + '-textbox').trigger('autocompletevalueselected',
[ui.item.value, ui.item.label]);
return false; return false;
}); });
@ -50,12 +52,35 @@
show(); show();
focus(); focus();
} }
$('#' + oid + '-textbox').trigger('autocompletevaluecleared');
}); });
} }
); );
</script> </script>
<script tal:condition="cleared_callback" type="text/javascript">
deform.addCallback(
'${oid}',
function (oid) {
$('#' + oid + '-textbox').on('autocompletevaluecleared', function() {
${cleared_callback}();
});
}
);
</script>
<script tal:condition="selected_callback" type="text/javascript">
deform.addCallback(
'${oid}',
function (oid) {
$('#' + oid + '-textbox').on('autocompletevalueselected', function(event, uuid, label) {
${selected_callback}(uuid, label);
});
}
);
</script>
<script tal:condition="cstruct" type="text/javascript"> <script tal:condition="cstruct" type="text/javascript">
deform.addCallback( deform.addCallback(
'${oid}', '${oid}',