dom objekte reduziert, spacer entfernt, rendering auf 150ms beschleunigt
[kivitendo-erp.git] / bin / mozilla / menu.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 ######################################################################
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors: Christopher Browne
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #######################################################################
30 #
31 # the frame layout with refractured menu
32 #
33 # CHANGE LOG:
34 #   DS. 2002-03-25  Created
35 #  2004-12-14 - New Optik - Marco Welter <mawe@linux-studio.de>
36 #  2010-08-19 - Icons for sub entries and single click behavior, unlike XUL-Menu
37 #               JS switchable HTML-menu - Sven Donath <lxo@dexo.de>
38 #######################################################################
39
40 use strict;
41
42 use SL::Menu;
43 use URI;
44
45 use List::MoreUtils qw(apply);
46
47 my $nbsp     = '&nbsp;';
48
49 # end of main
50
51 sub display {
52   $::lxdebug->enter_sub;
53
54   my $callback  = $::form->unescape($::form->{callback});
55   $callback     = URI->new($callback)->rel($callback) if $callback;
56   $callback     = "login.pl?action=company_logo"      if $callback =~ /^(\.\/)?$/;
57   my $framesize = _calc_framesize();
58
59   $::form->header(doctype => 'frameset');
60
61   print qq|
62 <frameset rows="28px,*" cols="*" framespacing="0" frameborder="0">
63   <frame  src="controller.pl?action=FrameHeader/header" scrolling="NO">
64   <frameset cols="$framesize,*" framespacing="0" frameborder="0" border="0" id="menuframe" name="menuframe">
65     <frame src="$::form->{script}?action=acc_menu" name="acc_menu"  scrolling="auto" noresize marginwidth="0">
66     <frame src="$callback" name="main_window" scrolling="auto">
67   </frameset>
68   <noframes>
69   You need a browser that can read frames to see this page.
70   </noframes>
71 </frameset>
72 </HTML>
73 |;
74
75   $::lxdebug->leave_sub;
76 }
77
78 sub acc_menu {
79   $::lxdebug->enter_sub;
80
81   my $framesize    = _calc_framesize() - 2;
82   my $menu         = Menu->new("menu.ini");
83   $::form->{title} = $::locale->text('kivitendo');
84   $::form->header;
85
86   my $sections = section_menu($menu);
87
88   print $::form->parse_html_template('menu/menu', {
89     framesize => $framesize,
90     sections  => $sections,
91   });
92
93   $::lxdebug->leave_sub;
94 }
95
96 sub section_menu {
97   $::lxdebug->enter_sub;
98   my ($menu, $level) = @_;
99   my @menuorder = $menu->access_control(\%::myconfig, $level);
100   my @items;
101
102   for my $item (@menuorder) {
103     my $menuitem   = $menu->{$item};
104     my $label      = apply { s/.*--// } $item;
105     my $ml         = apply { s/--.*// } $item;
106     my $spacer     = "spacer" . (0 + $item =~ s/--/--/g);
107     my $label_icon = $level . "--" . $label . ".png";
108
109     next if $level && $item ne "$level--$label";
110
111     $label         = $::locale->text($label);
112
113     $menuitem->{module} ||= $::form->{script};
114     $menuitem->{action} ||= "section_menu";
115     $menuitem->{target} ||= "main_window";
116     $menuitem->{href}   ||= "$menuitem->{module}?action=$menuitem->{action}";
117
118     # add other params
119     foreach my $key (keys %$menuitem) {
120       next if $key =~ /target|module|action|href/;
121       $menuitem->{href} .= "&" . $::form->escape($key, 1) . "=";
122       my ($value, $conf) = split(/=/, $menuitem->{$key}, 2);
123       $value = $::myconfig{$value} . "/$conf" if ($conf);
124       $menuitem->{href} .= $::form->escape($value, 1);
125     }
126
127     my $anchor = $menuitem->{href};
128
129     if (!$level) { # toplevel
130       push @items, make_item(
131         img      =>  make_image(icon => $item . '.png', size => 24, label => $label),
132         label    => $label,
133         height   => 24,
134         class    => 'menu',
135         spacer   => $spacer,
136         subitems => section_menu($menu, $item)
137       );
138     } elsif ($menuitem->{submenu}) {
139       push @items, make_item(
140         target   => $menuitem->{target},
141         spacer   => $spacer,
142         img      => make_image(submenu => 1),
143         label    => $label,
144         class    => 'submenu',
145         subitems => section_menu($menu, $item),
146       );
147     } elsif ($menuitem->{module}) {
148       push @items, make_item(
149         target => $menuitem->{target},
150         img    => make_image(label => $label, icon => $label_icon),
151         href   => $anchor,
152         spacer => $spacer,
153         label  => $label,
154         class  => 'item',
155       );
156     }
157   }
158   $::lxdebug->leave_sub;
159   return \@items;
160 }
161
162 sub make_item {
163   my %params = @_;
164   $params{spacer} ||= '';
165   $params{height} ||= 16;
166
167   return {
168     %params,
169 #    chunks => [ multiline($params{label}) ],
170   };
171 }
172
173 # multi line hack, sschoeling jul06
174 # if a label is too long, try to split it at whitespaces, then join it to chunks of less
175 # than 20 chars and store it in an array.
176 # use this array later instead of the &nbsp;-ed label
177 sub multiline {
178   my ($label) = @_;
179   my @chunks;
180   my $l = 20;
181   for (split / /, $label) {
182     $l += length $_;
183     if ($l < 20) {
184       $chunks[-1] .= " $_";
185     } else {
186       $l = length $_;
187       push @chunks, $_;
188     }
189   }
190   return @chunks;
191 }
192
193 sub make_image {
194   my (%params) = @_;
195
196   my $icon   = $params{icon};
197   my $size   = $params{size}   || 16;
198
199   return unless _show_images();
200
201   my $icon_found = $icon && -f _icon_path($icon, $size);
202   my $padding    = $size == 16 && $icon_found ? $nbsp x 2
203                  : $size == 24                ? $nbsp
204                  :                            '';
205
206   return  {
207     src     => $icon_found ? _icon_path($icon, $size) : "image/unterpunkt.png",
208     alt     => $params{label},
209     width   => $icon_found ? $size : 24,
210     height  => $icon_found ? $size : 15,
211   }
212 }
213
214 sub _calc_framesize {
215   my $is_lynx_browser   = $ENV{HTTP_USER_AGENT} =~ /links/i;
216   my $is_mobile_browser = $ENV{HTTP_USER_AGENT} =~ /mobile/i;
217   my $is_mobile_style   = $::form->{stylesheet} =~ /mobile/i;
218
219   return  $is_mobile_browser && $is_mobile_style ?  130
220         : $is_lynx_browser                       ?  240
221         :                                           200;
222 }
223
224 sub _show_images {
225   # don't show images in links
226   _calc_framesize() != 240;
227 }
228
229 sub _icon_path {
230   my ($label, $size) = @_;
231
232   $size ||= 16;
233
234   return "image/icons/${size}x${size}/$label";
235 }
236
237 1;
238
239 __END__