a28e1a9fbe3b2f7a8a445f9c7c341443785135b2
[kivitendo-erp.git] / SL / Layout / ActionBar.pm
1 package SL::Layout::ActionBar;
2
3 use strict;
4 use parent qw(SL::Layout::Base);
5
6 use Carp;
7 use Scalar::Util qw(blessed);
8 use SL::Layout::ActionBar::Action;
9 use SL::Layout::ActionBar::ComboBox;
10 use SL::Layout::ActionBar::Separator;
11
12 use constant HTML_CLASS => 'layout-actionbar';
13
14 use Rose::Object::MakeMethods::Generic (
15   'scalar --get_set_init' => [ qw(actions) ],
16 );
17
18 my %class_descriptors = (
19   action    => { class => 'SL::Layout::ActionBar::Action',    num_params => 1, },
20   combobox  => { class => 'SL::Layout::ActionBar::ComboBox',  num_params => 1, },
21   separator => { class => 'SL::Layout::ActionBar::Separator', num_params => 0, },
22 );
23
24 ###### Layout overrides
25
26 sub pre_content {
27   my ($self) = @_;
28
29   my $content = join '', map { $_->render } @{ $self->actions };
30   return if !$content;
31   $::request->presenter->html_tag('div', $content, class => HTML_CLASS);
32 }
33
34 sub javascripts_inline {
35   join '', map { $_->script } @{ $_[0]->actions };
36 }
37
38 sub javascripts {
39   'kivi.ActionBar.js'
40 }
41
42 ###### interface
43
44 sub add {
45   my ($self, @actions) = @_;
46
47   push @{ $self->actions }, $self->parse_actions(@actions);
48
49   return $self->actions->[-1];
50 }
51
52 sub parse_actions {
53   my ($self_or_class, @actions) = @_;
54
55   my @parsed;
56
57   while (my $type = shift(@actions)) {
58     if (blessed($type) && $type->isa('SL::Layout::ActionBar::Action')) {
59       push @parsed, $type;
60       continue;
61     }
62
63     my $descriptor = $class_descriptors{lc $type} || croak("Unknown action type '${type}'");
64     my @params     = splice(@actions, 0, $descriptor->{num_params});
65
66     push @parsed, $descriptor->{class}->from_params(@params);
67   }
68
69   return @parsed;
70 }
71
72 sub init_actions {
73   []
74 }
75
76 1;
77
78 __END__
79
80 =encoding utf-8
81
82 =head1 NAME
83
84 SL::Layout::ActionBar - Unified action buttons for controllers
85
86 =head1 CONCEPT
87
88 This is a layout block that does a unified action bar for any controller who
89 wants to use it. It's designed to be rendered above the content and to be
90 fixed when scrolling.
91
92 While it can be used as a generic widget container, it's designed to be able to
93 provide commonly used functionality as a short cut. These shortcuts include:
94
95 =over 4
96
97 =item *
98
99 Calling a controller with parameters
100
101 =item *
102
103 Submitting a form with added parameters
104
105 =item *
106
107 Arrangement utility
108
109 =back
110
111
112 =head1 METHODS
113
114 =over 4
115
116 =item C<add_actions LIST>
117
118 Dispatches each each argument to C<add_action>
119
120 =item C<add_action>
121
122
123 =item C<add_separator>
124
125 =item C<add_
126
127 =head1 ACCESS FROM CODE
128
129 This is accessable through
130
131   $::request->layout->get('actionbar')
132
133 =head1 DOM MODEL
134
135 The entire block is rendered into a div with the class 'layout-actionbar'.
136
137 =head1 ACTION WIDGETS
138
139 Each individual action must be an instance of C<SL::Layout::ActionBar::Action>.
140
141 =head1 BUGS
142
143 none yet. :)
144
145 =head1 AUTHOR
146
147 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
148
149 =cut