Skip to content

Commit

Permalink
Merge pull request #813 from stopfstedt/tagging_agendas
Browse files Browse the repository at this point in the history
Makes deck tagging (slighly) more useful
  • Loading branch information
stopfstedt authored Dec 11, 2021
2 parents 7ae09bc + 3388314 commit b59b9d0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
19 changes: 11 additions & 8 deletions assets/js/ui.decks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

(function ui_decks(ui, $)
{
var tag_row_tpl = _.template('<span class="tag" data-tag="<%- tag %>"><%- tag %></span>');
var tag_button_tpl = _.template('<button type="button" class="btn btn-default btn-xs" data-toggle="button" data-tag="<%- tag %>"><%- tag %></button>');

ui.decks = [];

Expand All @@ -27,7 +29,7 @@
var div = elt.find('div.tags').empty();
tags.forEach(function (tag)
{
div.append($('<span class="tag">' + tag + '</span>'));
div.append($(tag_row_tpl({tag: tag})));
});

ui.update_tag_toggles();
Expand All @@ -47,7 +49,7 @@
{
event.preventDefault();
var ids = $('#tag_add_ids').val().split(/,/);
var tags = $('#tag_add_tags').val().split(/\s+/);
var tags = $('#tag_add_tags').val().trim().split(/\s+/);
if(!ids.length || !tags.length)
return;
ui.tag_process_any('tag_add', {ids: ids, tags: tags});
Expand Down Expand Up @@ -132,10 +134,11 @@
tags.push($(elt).data('tag'));
}
});
tags.sort();
$('#tag_toggles').empty();
_.uniq(tags).forEach(function (tag)
{
$('<button type="button" class="btn btn-default btn-xs" data-toggle="button" data-tag="' + tag + '">' + tag + '</button>').appendTo('#tag_toggles');
$(tag_button_tpl({tag: tag})).appendTo('#tag_toggles');
});
};

Expand All @@ -149,11 +152,11 @@
});
if(tags.length) {
$('#decks tr').hide();
tags.forEach(function (tag)
{
$('#decks span[data-tag="' + tag + '"]').each(function (index, elt)
{
$(elt).closest('tr').show();
tags.forEach(function (tag) {
$('#decks span.tag').each(function (index, elt) {
if ($(elt).data('tag') === tag) {
$(elt).closest('tr').show();
}
});
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/BuilderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function initbuildAction(
)
);
$pack = $agenda->getPack();
$tags[] = $agendaHelper->getMinorFactionCode($agenda);
$tags[] = $agendaHelper->agendaToTag($agenda);
}

/** @var UserInterface $user */
Expand Down
15 changes: 13 additions & 2 deletions src/Controller/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,20 @@ public function addAction(Request $request)
if ($this->getUser()->getId() != $deck->getUser()->getId()) {
continue;
}
$tags = array_unique(array_values(array_merge(preg_split('/\s+/', $deck->getTags()), $list_tag)));
$response['tags'][$deck->getId()] = $tags;
$tags = array_unique(
array_values(
array_merge(
preg_split(
'/\s+/',
$deck->getTags()
),
$list_tag
)
)
);
array_filter($tags);
$deck->setTags(implode(' ', $tags));
$response['tags'][$deck->getId()] = explode(' ', $deck->getTags());
}
$em->flush();

Expand Down
11 changes: 11 additions & 0 deletions src/Services/AgendaHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ public function getMinorFaction(CardInterface $agenda)
}
return null;
}

/**
* @param CardInterface $agenda
* @return string
* @todo added here for convenience, this should probably be part of a generic string helper class. [ST 2021/12/11]
*/
public function agendaToTag(CardInterface $agenda): string
{
$name = $agenda->getName();
return preg_replace('/[^a-zA-Z]/', '', strtolower($name));
}
}
2 changes: 1 addition & 1 deletion templates/Builder/decks.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
</div>
{% endif %}
<div class="tags">
{% for tag in deck.tags|split(' ') %}
{% for tag in deck.tags|trim|split(' ')|sort %}
<span class="tag" data-tag="{{ tag }}">{{ tag }}</span>
{% endfor %}
</div>
Expand Down

0 comments on commit b59b9d0

Please sign in to comment.