Skip to content

Commit

Permalink
refactored list body generation from PlaylistBuilder to twig
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketMan committed Oct 2, 2024
1 parent b37c5b6 commit ee34003
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
16 changes: 9 additions & 7 deletions ui/PlaylistBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
});
}
Expand Down
9 changes: 5 additions & 4 deletions ui/Playlists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
{#
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 #}
{%~ set format = params.usLocale ? "h:i a" : "H:i" %}
<td class='time' data-utc='{{ created }}'>{{ created ? created | date(format) }}</td>
{%- endmacro %}

{% macro makeEditDiv(params, entry) %}
{%- macro makeEditDiv(params, entry) %}
{%~ if params.editMode %}
{%~ set href = "?id=" ~ entry.getId() | e('url') ~ "&amp;subaction=" ~ params.action | e('url') ~ "&amp;seq=editTrack" %}
{%~ set editLink = "<a class='songEdit nav' href='" ~ href ~ "'><span class='fas fa-edit'></span></a>" %}
Expand All @@ -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 %}
Expand All @@ -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() ? "<div class='albumReview'></div>" %}
{%~ set artistName = entry.getTag() ? entry.swapNames(entry.getArtist()) : entry.getArtist() %}
<tr class='songRow'>
{{~ _self.makeEditDiv(params, entry) }}
<td>{{ artistName | smartURL }}</td>
<td>{{ entry.getTrack() | smartURL }}</td>
<td style='width: 15px'>{{ reviewCell | raw }}</td>
<td>{{ _self.makeAlbumLink(entry, true) }}</td>
</tr>
{%~ endblock %}
{%~ set break = false %}
{%~ elseif type is constant('TYPE_COMMENT', entry) %}
{%~ block comment %}
<tr class='commentRow {{~ params.editMode ? "Edit" }}'>
{{~ _self.makeEditDiv(params, entry) }}
<td colspan=4>{{ entry.getComment() | markdown }}</td>
</tr>
{% endblock %}

{% block logEvent %}
{%~ endblock %}
{%~ set break = false %}
{%~ elseif type is constant('TYPE_LOG_EVENT', entry) %}
{%~ block logEvent %}
{%~ if params.authUser %}
<tr class='logEntry {{~ params.editMode ? "Edit" }}'>
{{~ _self.makeEditDiv(params, entry) }}
<td>{{ entry.getLogEventType() }}</td>
<td colspan=3>{{ entry.getLogEventCode() }}</td>
</tr>
{%~ elseif not params.break %}
{%~ elseif not break %}
<tr class='songDivider'>
{{~ _self.makeTime(params, entry) }}
<td colspan=4><hr></td>
</tr>
{%~ 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 %}
<tr class='songDivider'>
{{~ _self.makeEditDiv(params, entry) }}
<td colspan=4><hr></td>
</tr>
{%~ endif %}
{% endblock %}

{% block spin %}
{%~ set reviewCell = entry.getReviewed() ? "<div class='albumReview'></div>" %}
{%~ set artistName = entry.getTag() ? entry.swapNames(entry.getArtist()) : entry.getArtist() %}
<tr class='songRow'>
{{~ _self.makeEditDiv(params, entry) }}
<td>{{ artistName | smartURL }}</td>
<td>{{ entry.getTrack() | smartURL }}</td>
<td style='width: 15px'>{{ reviewCell | raw }}</td>
<td>{{ _self.makeAlbumLink(entry, true) }}</td>
</tr>
{% endblock %}
{%~ endblock %}
{%~ set break = true %}
{%~ endif %}
{% endfor %}
4 changes: 1 addition & 3 deletions ui/templates/default/list/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ <h4>You have reached the end time of your show.</h4>
</tr>
</thead>
<tbody>
{%~ for entry in entries %}
{{~ observer.observe(entry) | raw }}
{%~ endfor %}
{%~ include 'list/body.html' %}
</tbody>
</table>
{% if editMode %}
Expand Down

0 comments on commit ee34003

Please sign in to comment.