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