8b83295c5467bb10a35a878c9a5261b4076889cb
[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 # static constructors
23
24 sub from_descriptor {
25   my ($class, $descriptor) = @_;
26   require SL::Layout::ActionBar::Separator;
27   require SL::Layout::ActionBar::ComboBox;
28
29   return {
30      separator => SL::Layout::ActionBar::Separator->new,
31      combobox  => SL::Layout::ActionBar::ComboBox->new,
32   }->{$descriptor} or die 'unknown descriptor';
33 }
34
35 # TODO: this necessary?
36 sub simple {
37   my ($class, $data) = @_;
38
39   my ($text, %params) = @$data;
40
41   if ($params{submit}) {
42     require SL::Layout::ActionBar::Submit;
43     return SL::Layout::ActionBar::Submit->new(text => $text, params => \%params);
44   }
45
46   if ($params{function}) {
47     require SL::Layout::ActionBar::ScriptButton;
48     return SL::Layout::ActionBar::ScriptButton->new(text => $text, params => \%params);
49   }
50
51   if ($params{actions}) {
52     require SL::Layout::ActionBar::ComboBox;
53     return SL::Layout::ActionBar::ComboBox->new(text => $text, %params);
54   }
55 }
56
57 # shortcut for presenter
58
59 sub p {
60   SL::Presenter->get
61 }
62
63 # unique id to tie div and javascript together
64 sub init_id {
65   $_[0]->p->name_to_id('action[]')
66 }
67
68
69 1;
70
71 __END__
72
73 =head 1
74
75 planned options for clickables:
76
77 - checks => [ ... ] (done)
78
79 a list of functions that need to return true before submitting
80
81 - submit => [ form-selector, { params } ] (done)
82
83 on click submit the form specified by form-selector with the additional params
84
85 - function => function-name (done)
86
87 on click call the specified function (is this a special case of checks?)
88
89 - disabled => true/false (done)
90
91 TODO:
92
93 - runtime disable/enable
94