From 9836938f40f4738dd4e1fabd585e24be5b2bffba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Wed, 20 Jan 2021 11:07:04 +0100 Subject: [PATCH] Layout::Base: besseres sub_layout javascript/css dispatching MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit sub_layouts werden jetzt mit add/use aggregiert. add/use gibt aber immer auch die der sub_layouts zurück. Statt die zu überschreiben gibt es jetzt die neuen callbacks static_javascripts static_stylesheets die zusätzlich zurückgeliefert werden. Die alten einstiegspunkte javascripts stylesheets machen jetzt die Auflösung in die Webpfade, so dass nur das oberste layout den Mechanismus überschreiben braucht wenn benötigt. --- SL/Layout/Base.pm | 50 ++++++++++++++++++++++++++++++++--------- SL/Layout/Dispatcher.pm | 13 +++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/SL/Layout/Base.pm b/SL/Layout/Base.pm index aa7881250..a1df9ebef 100644 --- a/SL/Layout/Base.pm +++ b/SL/Layout/Base.pm @@ -103,9 +103,12 @@ sub init_sub_layouts_by_name { +{} } ######################################### -# Interface +# Stylesheets ######################################## +# override in sub layouts +sub static_stylesheets {} + sub add_stylesheets { &use_stylesheet; } @@ -113,7 +116,7 @@ sub add_stylesheets { sub use_stylesheet { my $self = shift; push @{ $self->{stylesheets} ||= [] }, @_ if @_; - @{ $self->{stylesheets} ||= [] }; + (map { $_->use_stylesheet } $self->sub_layouts), $self->static_stylesheets, @{ $self->{stylesheets} ||= [] }; } sub stylesheets { @@ -121,7 +124,7 @@ sub stylesheets { my $css_path = $self->get_stylesheet_for_user; return uniq grep { $_ } map { $self->_find_stylesheet($_, $css_path) } - $self->use_stylesheet, map { $_->stylesheets } $self->sub_layouts; + $self->use_stylesheet; } sub _find_stylesheet { @@ -150,6 +153,13 @@ sub get_stylesheet_for_user { return $css_path; } +######################################### +# Javascripts +######################################## + +# override in sub layouts +sub static_javascripts {} + sub add_javascripts { &use_javascript } @@ -157,14 +167,14 @@ sub add_javascripts { sub use_javascript { my $self = shift; push @{ $self->{javascripts} ||= [] }, @_ if @_; - @{ $self->{javascripts} ||= [] }; + map({ $_->use_javascript } $self->sub_layouts), $self->static_javascripts, @{ $self->{javascripts} ||= [] }; } sub javascripts { my ($self) = @_; return uniq grep { $_ } map { $self->_find_javascript($_) } - map({ $_->javascripts } $self->sub_layouts), $self->use_javascript; + $self->use_javascript; } sub _find_javascript { @@ -285,15 +295,33 @@ For the C<*_content> callbacks this works if you just remember to dispatch to th $_[0]->SUPER::post_content } -For the stylesheet and javascript callbacks things are hard, because of the -backwards compatibility, and the built-in sanity checks. The best way currently -is to just add your content and dispatch to the base method. - sub stylesheets { - $_[0]->add_stylesheets(qw(mystyle1.css mystyle2.css); - $_[0]->SUPER::stylesheets; +Stylesheets and Javascripts can be added to every layout and sub-layout at +runtime with L and +L (C and +C are aliases for backwards compatibility): + + $layout->add_stylesheets("custom.css"); + $layout->add_javascripts("app.js", "widget.js"); + +Or they can be overwritten in sub layouts with the calls +L and +L: + + sub static_stylesheets { + "custom.css" + } + + sub static_javascripts { + qw(app.css widget.js) } +Note how these are relative to the base dirs of the currently selected +stylesheets. Javascripts are resolved relative to the C basedir. + +Setting directly with C and C is eprecated. + + =head1 GORY DETAILS ABOUT JAVASCRIPT AND STYLESHEET OVERLOADING The original code used to store one stylesheet in C<< $form->{stylesheet} >> and diff --git a/SL/Layout/Dispatcher.pm b/SL/Layout/Dispatcher.pm index b125a2668..fa6333762 100644 --- a/SL/Layout/Dispatcher.pm +++ b/SL/Layout/Dispatcher.pm @@ -167,6 +167,12 @@ Non-existing files will be pruned from the list. Backwards compatible alias for C. Deprecated. +=item C + +Can be overwritten in sub-layouts to return a list of needed stylesheets. The +values will be resolved by the actual layout in addition to the +C accumulator. + =item C Adds the list of arguments to the list of used javascripts. @@ -179,6 +185,13 @@ Non-existing files will be pruned from the list. Backwards compatible alias for C. Deprecated. + +=item C + +Can be overwritten in sub-layouts to return a list of needed javascripts. The +values will be resolved by the actual layout in addition to the +C accumulator. + =item C Add a snippet of javascript. -- 2.20.1