Show (iconized) links to attachments on parts table.

This commit is contained in:
Jan Böhmer 2019-11-10 19:16:39 +01:00
parent 3e4f5a1391
commit 05d6b0aa93
5 changed files with 114 additions and 5 deletions

View file

@ -679,6 +679,16 @@ table.dataTable {
} }
/*******************************
Parts datatable styling
******************************/
.attach-table-icon {
margin-right: 0.7em;
color: var(--gray);
}
.attach-table-icon:hover {
color: var(--gray-dark);
}
/********************************* /*********************************
Workarounds Workarounds

View file

@ -526,14 +526,13 @@ class AjaxUI {
$(document).trigger('ajaxUI:dt_loaded'); $(document).trigger('ajaxUI:dt_loaded');
//Attach event listener to update links after new page selection: //Attach event listener to update links after new page selection:
$('#dt').on('draw.dt', function() { $('#dt').on('draw.dt column-visibility.dt', function() {
ajaxUI.registerLinks(); ajaxUI.registerLinks();
$(document).trigger('ajaxUI:dt_loaded'); $(document).trigger('ajaxUI:dt_loaded');
}); });
}); });
}); });
console.debug('Datatables inited.'); console.debug('Datatables inited.');
} }
} }

View file

@ -116,7 +116,7 @@ $(document).on("ajaxUI:reload", function () {
}); });
//Use bootstrap tooltips for the most tooltips //Use bootstrap tooltips for the most tooltips
$(document).on("ajaxUI:start ajaxUI:reload", function () { $(document).on("ajaxUI:start ajaxUI:reload ajaxUI:dt_loaded", function () {
$(".tooltip").remove(); $(".tooltip").remove();
$('a[title], button[title], span[title], h6[title], i.fas[title]') $('a[title], button[title], span[title], h6[title], i.fas[title]')
//@ts-ignore //@ts-ignore

View file

@ -0,0 +1,94 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
namespace App\DataTables\Column;
use App\Entity\Attachments\Attachment;
use App\Entity\Parts\Part;
use App\Services\Attachments\AttachmentURLGenerator;
use App\Services\EntityURLGenerator;
use App\Services\FAIconGenerator;
use Omines\DataTablesBundle\Column\AbstractColumn;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PartAttachmentsColumn extends AbstractColumn
{
protected $FAIconGenerator;
protected $urlGenerator;
public function __construct(FAIconGenerator $FAIconGenerator, AttachmentURLGenerator $urlGenerator)
{
$this->FAIconGenerator = $FAIconGenerator;
$this->urlGenerator = $urlGenerator;
}
/**
* The normalize function is responsible for converting parsed and processed data to a datatables-appropriate type.
*
* @param mixed $value The single value of the column
* @return mixed
*/
public function normalize($value)
{
return $value;
}
public function render($value, $context)
{
if (!$context instanceof Part) {
throw new \RuntimeException('$context must be a Part object!');
}
$tmp = "";
$attachments = $context->getAttachments()->filter(function (Attachment $attachment) {
return $attachment->getShowInTable();
});
$count = 5;
foreach ($attachments as $attachment) {
//Only show the first 5 attachments
if (--$count <= 0) {
continue;
}
/** @var Attachment $attachment */
$tmp .= sprintf(
'<a href="%s" title="%s" class="attach-table-icon" target="_blank" rel="noopener" data-no-ajax>%s</a>',
$this->urlGenerator->getViewURL($attachment),
$attachment->getName() . ': ' . $attachment->getFilename(),
$this->FAIconGenerator->generateIconHTML(
$this->FAIconGenerator->fileExtensionToFAType($attachment->getExtension()),
'fas',
'fa-2x'
)
);
}
return $tmp;
}
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
}
}

View file

@ -23,6 +23,8 @@ namespace App\DataTables;
use App\DataTables\Column\EntityColumn; use App\DataTables\Column\EntityColumn;
use App\DataTables\Column\LocaleDateTimeColumn; use App\DataTables\Column\LocaleDateTimeColumn;
use App\DataTables\Column\PartAttachmentsColumn;
use App\Entity\Attachments\Attachment;
use App\Entity\Parts\Category; use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint; use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Manufacturer;
@ -44,6 +46,7 @@ use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\DataTable; use Omines\DataTablesBundle\DataTable;
use Omines\DataTablesBundle\DataTableTypeInterface; use Omines\DataTablesBundle\DataTableTypeInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function foo\func;
class PartsDataTable implements DataTableTypeInterface class PartsDataTable implements DataTableTypeInterface
{ {
@ -59,8 +62,7 @@ class PartsDataTable implements DataTableTypeInterface
public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator, public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator,
TreeBuilder $treeBuilder, AmountFormatter $amountFormatter, TreeBuilder $treeBuilder, AmountFormatter $amountFormatter,
PartPreviewGenerator $previewGenerator, AttachmentURLGenerator $attachmentURLGenerator, PartPreviewGenerator $previewGenerator, AttachmentURLGenerator $attachmentURLGenerator)
FAIconGenerator $FAIconGenerator)
{ {
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->translator = $translator; $this->translator = $translator;
@ -275,6 +277,10 @@ class PartsDataTable implements DataTableTypeInterface
'label' => $this->translator->trans('part.table.tags'), 'label' => $this->translator->trans('part.table.tags'),
'visible' => false, 'visible' => false,
]) ])
->add('attachments', PartAttachmentsColumn::class, [
'label' => $this->translator->trans('part.table.attachments'),
'visible' => false,
])
->addOrderBy('name') ->addOrderBy('name')
->createAdapter(ORMAdapter::class, [ ->createAdapter(ORMAdapter::class, [