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