ActionBar: Auslassen von Actions über Parameter »only_if«/»not_if« steuern können
[kivitendo-erp.git] / SL / Layout / ActionBar / Action.pm
1 package SL::Layout::ActionBar::Action;
2
3 use strict;
4 use parent qw(Rose::Object);
5
6 use SL::Presenter;
7     require SL::Layout::ActionBar::Submit;
8
9 use Rose::Object::MakeMethods::Generic (
10   'scalar --get_set_init' => [ qw(id params text) ],
11 );
12
13 # subclassing interface
14
15 sub render {
16   die 'needs to be implemented';
17 }
18
19 sub script {
20   sprintf q|$('#%s').data('action', %s);|, $_[0]->id, JSON->new->allow_blessed->convert_blessed->encode($_[0]->params);
21 }
22
23 # this is mostly so that outside consumer don't need to load subclasses themselves
24 sub from_params {
25   my ($class, $data) = @_;
26
27   my ($text, %params) = @$data;
28   return if exists($params{only_if}) && !$params{only_if};
29   return if exists($params{not_if})  &&  $params{not_if};
30   return SL::Layout::ActionBar::Submit->new(text => $text, params => \%params);
31 }
32
33 # shortcut for presenter
34
35 sub p {
36   SL::Presenter->get
37 }
38
39 sub init_params {
40   +{}
41 }
42
43 # unique id to tie div and javascript together
44 sub init_id {
45   $_[0]->params->{id} //
46   $_[0]->p->name_to_id('action[]')
47 }
48
49
50 1;
51
52 __END__
53
54 =head 1
55
56 planned options for clickables:
57
58 - checks => [ ... ] (done)
59
60 a list of functions that need to return true before submitting
61
62 - submit => [ form-selector, { params } ] (done)
63
64 on click submit the form specified by form-selector with the additional params
65
66 - function => function-name (done)
67
68 on click call the specified function (is this a special case of checks?)
69
70 - disabled => true/false/tooltip explaning why disabled (done)
71
72 TODO:
73
74 - runtime disable/enable