}
sub init_parts {
- $_[0]->get_models;
+ $_[0]->get_models(with_objects => [ qw(unit_obj) ]);
}
1;
use base qw(SL::DB::Helper::Manager);
use SL::DB::Helper::Sorted;
+use SL::DB::Helper::Filtered;
sub object_class { 'SL::DB::Unit' }
__PACKAGE__->make_manager_methods;
+__PACKAGE__->add_filter_specs(
+ convertible_to => sub {
+ my ($key, $value, $prefix) = @_;
+ return __PACKAGE__->convertible_to_filter($key, $value, $prefix);
+ },
+);
sub _sort_spec {
return ( default => [ 'sortkey', 1 ],
});
}
+sub convertible_to_filter {
+ my ($class, $key, $unit_name, $prefix) = @_;
+
+ return () unless $unit_name;
+
+ $prefix //= '';
+
+ my $unit = $class->find_by(name => $unit_name);
+ if (!$unit) {
+ $::lxdebug->warn("Unit manager: No unit with name $unit_name");
+ return ();
+ }
+
+ return ("${prefix}name" => [ map { $_->name } @{ $unit->convertible_units } ]);
+}
+
1;
my $ret =
$self->input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => 'part_autocomplete', type => 'hidden', id => $id) .
- join('', map { $params{$_} ? $self->input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(column type unit)) .
+ join('', map { $params{$_} ? $self->input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(column type unit convertible_unit)) .
$self->input_tag("", (ref $value && $value->can('description')) ? $value->description : '', id => "${id}_name", %params);
$self->html_tag('span', $ret, class => 'part_picker');
for autocompletion. You may comma separate multiple units as in
C<h,min>.
+If C<%params> contains C<convertible_unit> only parts with a unit
+that's convertible to unit will be used for autocompletion.
+
Obsolete parts will by default not displayed for selection. However they are
accepted as default values and can persist during updates. As with other
selectors though, they are not selectable once overridden.
var $dummy = $('#' + real_id + '_name');
var $type = $('#' + real_id + '_type');
var $unit = $('#' + real_id + '_unit');
+ var $convertible_unit = $('#' + real_id + '_convertible_unit');
var $column = $('#' + real_id + '_column');
var state = STATES.PICKED;
var last_real = $real.val();
var data = {
'filter.all:substr::ilike': term,
'filter.obsolete': 0,
- column: $column.val()===undefined ? '' : $column.val(),
+ 'filter.unit_obj.convertible_to': $convertible_unit && $convertible_unit.val() ? $convertible_unit.val() : '',
+ column: $column && $column.val() ? $column.val() : '',
current: $real.val(),
};
dummy: function() { return $dummy },
type: function() { return $type },
unit: function() { return $unit },
+ convertible_unit: function() { return $convertible_unit },
column: function() { return $column },
update_results: update_results,
set_item: set_item,