3707dcf8b666143c7520c98e024aad8cea7e0184
[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 sub callable { 0 }
34
35 # shortcut for presenter
36
37 sub p {
38   SL::Presenter->get
39 }
40
41 sub init_params {
42   +{}
43 }
44
45 # unique id to tie div and javascript together
46 sub init_id {
47   $_[0]->params->{id} //
48   $_[0]->p->name_to_id('action[]')
49 }
50
51
52 1;
53
54 __END__
55
56 =head 1
57
58 planned options for clickables:
59
60 - checks => [ ... ] (done)
61
62 a list of functions that need to return true before submitting
63
64 - submit => [ form-selector, { params } ] (done)
65
66 on click submit the form specified by form-selector with the additional params
67
68 - function => function-name (done)
69
70 on click call the specified function (is this a special case of checks?)
71
72 - disabled => true/false/tooltip explaning why disabled (done)
73
74 TODO:
75
76 - runtime disable/enable