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