From ee34003756478720b6f4875ba1d00aa0e6817342 Mon Sep 17 00:00:00 2001 From: Jim Mason Date: Wed, 2 Oct 2024 19:16:04 +0100 Subject: [PATCH] refactored list body generation from PlaylistBuilder to twig --- ui/PlaylistBuilder.php | 16 ++--- ui/Playlists.php | 9 +-- .../default/list/{item.html => body.html} | 63 ++++++++++--------- ui/templates/default/list/editor.html | 4 +- 4 files changed, 49 insertions(+), 43 deletions(-) rename ui/templates/default/list/{item.html => body.html} (77%) diff --git a/ui/PlaylistBuilder.php b/ui/PlaylistBuilder.php index a4737c9d..dc2c0210 100644 --- a/ui/PlaylistBuilder.php +++ b/ui/PlaylistBuilder.php @@ -31,8 +31,9 @@ class PlaylistBuilder extends PlaylistObserver { private const PARAMS = [ "action", "editMode", "authUser" ]; - protected $params; protected $template; + protected $params; + protected $break; public static function newInstance(array $params) { // validate all parameters are present @@ -51,31 +52,32 @@ public static function newInstance(array $params) { protected function renderBlock($block, $entry) { return $this->template->renderBlock($block, [ "params" => $this->params, + "break" => $this->break, "entry" => $entry ]); } protected function __construct(array $params) { $templateFact = new TemplateFactoryUI(); - $this->template = $templateFact->load('list/item.html'); + $this->template = $templateFact->load('list/body.html'); $this->params = $params; - $this->params['break'] = false; $this->params['usLocale'] = UI::isUsLocale(); + $this->break = false; $this->on('comment', function($entry) { $fragment = $this->renderBlock('comment', $entry); - $this->params['break'] = false; + $this->break = false; return $fragment; })->on('logEvent', function($entry) { $fragment = $this->renderBlock('logEvent', $entry); - $this->params['break'] = !$this->params['authUser']; + $this->break = !$this->params['authUser']; return $fragment; })->on('setSeparator', function($entry) { $fragment = $this->renderBlock('setSeparator', $entry); - $this->params['break'] = true; + $this->break = true; return $fragment; })->on('spin', function($entry) { $fragment = $this->renderBlock('spin', $entry); - $this->params['break'] = false; + $this->break = false; return $fragment; }); } diff --git a/ui/Playlists.php b/ui/Playlists.php index da245dcb..8101fa6f 100644 --- a/ui/Playlists.php +++ b/ui/Playlists.php @@ -266,17 +266,18 @@ private function emitPlaylistBody($playlist, $editMode) { $tracks = $api->getTracks($playlist['id'], $editMode)->asArray(); Engine::api(ILibrary::class)->markAlbumsReviewed($tracks); - $observer = PlaylistBuilder::newInstance([ + $params = [ "action" => $this->subaction, "editMode" => $editMode, - "authUser" => $this->session->isAuth("u") - ]); + "authUser" => $this->session->isAuth("u"), + "usLocale" => UI::isUsLocale() + ]; $entries = array_map(function($track) { return new PlaylistEntry($track); }, $tracks); - $this->addVar("observer", $observer); + $this->addVar("params", $params); $this->addVar("entries", $entries); $this->addVar("editMode", $editMode); $this->addVar("isLive", $api->isNowWithinShow($playlist)); diff --git a/ui/templates/default/list/item.html b/ui/templates/default/list/body.html similarity index 77% rename from ui/templates/default/list/item.html rename to ui/templates/default/list/body.html index 87f60683..ce1c6914 100644 --- a/ui/templates/default/list/item.html +++ b/ui/templates/default/list/body.html @@ -1,8 +1,3 @@ -{# - This template is rendered only by block. - - See UI::PlaylistBuilder - #} {% macro makeTime(params, entry) %} {%~ set created = entry.getCreatedTimestamp() %} {#~ colon is included in 24hr format for symmetry with fxtime #} @@ -10,7 +5,7 @@ {{ created ? created | date(format) }} {%- endmacro %} -{% macro makeEditDiv(params, entry) %} +{%- macro makeEditDiv(params, entry) %} {%~ if params.editMode %} {%~ set href = "?id=" ~ entry.getId() | e('url') ~ "&subaction=" ~ params.action | e('url') ~ "&seq=editTrack" %} {%~ set editLink = "" %} @@ -22,7 +17,7 @@ {{~ _self.makeTime(params, entry) }} {%- endmacro %} -{% macro makeAlbumLink(entry, includeLabel) %} +{%- macro makeAlbumLink(entry, includeLabel) %} {%~ set albumName = entry.getAlbum() %} {%~ set labelName = entry.getLabel() %} {%~ if albumName | length or labelName | length %} @@ -34,45 +29,55 @@ {%~ endif %} {% endmacro %} -{% block comment %} +{%- set break = false %} +{% for entry in entries %} + {%~ set type = entry.getType() %} + {%~ if type is constant('TYPE_SPIN', entry) %} + {%~ block spin %} + {%~ set reviewCell = entry.getReviewed() ? "
" %} + {%~ set artistName = entry.getTag() ? entry.swapNames(entry.getArtist()) : entry.getArtist() %} + + {{~ _self.makeEditDiv(params, entry) }} + {{ artistName | smartURL }} + {{ entry.getTrack() | smartURL }} + {{ reviewCell | raw }} + {{ _self.makeAlbumLink(entry, true) }} + + {%~ endblock %} + {%~ set break = false %} + {%~ elseif type is constant('TYPE_COMMENT', entry) %} + {%~ block comment %} {{~ _self.makeEditDiv(params, entry) }} {{ entry.getComment() | markdown }} -{% endblock %} - -{% block logEvent %} + {%~ endblock %} + {%~ set break = false %} + {%~ elseif type is constant('TYPE_LOG_EVENT', entry) %} + {%~ block logEvent %} {%~ if params.authUser %} {{~ _self.makeEditDiv(params, entry) }} {{ entry.getLogEventType() }} {{ entry.getLogEventCode() }} - {%~ elseif not params.break %} + {%~ elseif not break %} {{~ _self.makeTime(params, entry) }}
{%~ endif %} -{% endblock %} - -{% block setSeparator %} - {%~ if params.editMode or not params.break %} + {%~ endblock %} + {%~ set break = not params.authUser %} + {%~ else %} + {%~ block setSeparator %} + {%~ if params.editMode or not break %} {{~ _self.makeEditDiv(params, entry) }}
{%~ endif %} -{% endblock %} - -{% block spin %} - {%~ set reviewCell = entry.getReviewed() ? "
" %} - {%~ set artistName = entry.getTag() ? entry.swapNames(entry.getArtist()) : entry.getArtist() %} - - {{~ _self.makeEditDiv(params, entry) }} - {{ artistName | smartURL }} - {{ entry.getTrack() | smartURL }} - {{ reviewCell | raw }} - {{ _self.makeAlbumLink(entry, true) }} - -{% endblock %} + {%~ endblock %} + {%~ set break = true %} + {%~ endif %} +{% endfor %} diff --git a/ui/templates/default/list/editor.html b/ui/templates/default/list/editor.html index 9fce5c92..3ca2bbc8 100644 --- a/ui/templates/default/list/editor.html +++ b/ui/templates/default/list/editor.html @@ -164,9 +164,7 @@

You have reached the end time of your show.

- {%~ for entry in entries %} - {{~ observer.observe(entry) | raw }} - {%~ endfor %} + {%~ include 'list/body.html' %} {% if editMode %}