-
Notifications
You must be signed in to change notification settings - Fork 17
/
DetailsRow.php
92 lines (80 loc) · 2.83 KB
/
DetailsRow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace ViewComponents\Grids\Component;
use RuntimeException;
use ViewComponents\Grids\Grid;
use ViewComponents\ViewComponents\Base\Compound\PartInterface;
use ViewComponents\ViewComponents\Base\Compound\PartTrait;
use ViewComponents\ViewComponents\Base\DataViewComponentInterface;
use ViewComponents\ViewComponents\Base\Html\TagInterface;
use ViewComponents\ViewComponents\Component\Compound;
use ViewComponents\ViewComponents\Component\DataView;
use ViewComponents\ViewComponents\Resource\ResourceManager;
use ViewComponents\ViewComponents\Service\Services;
class DetailsRow extends SolidRow implements PartInterface
{
use PartTrait {
PartTrait::attachToCompound as private attachToCompoundInternal;
}
const ID = 'details_row';
protected $view;
/** @var ResourceManager */
private $resourceManager;
private $jquery;
public function __construct(DataViewComponentInterface $view, ResourceManager $resourceManager = null)
{
parent::__construct();
$this->getRowTag()
->setAttribute('style', 'display:none;')
->setAttribute('data-details-row', '1');
$this->addChild($this->view = $view);
$this->setDestinationParentId(Grid::COLLECTION_VIEW_ID);
$this->setId('details_row');
$this->resourceManager = $resourceManager ?: Services::resourceManager();
$this->jquery = $this->resourceManager->js('jquery');
}
public function render()
{
$this->view->setData($this->getGrid()->getCurrentRow());
return parent::render();
}
/**
* @return null|Grid
*/
protected function getGrid()
{
return $this->root;
}
public function attachToCompound(Compound $root, $prepend = false)
{
$isAlreadyAttached = $this->root !== null;
$this->attachToCompoundInternal($root, $prepend);
if ($isAlreadyAttached) {
return;
}
$tr = $this->getGrid()->getRecordView();
if (!$tr instanceof TagInterface) {
throw new RuntimeException(
"Details row works only with record_view components implementing TagInterface"
);
}
$tr->setAttribute('data-row-with-details', 1);
$this->getGrid()->children()
->add($this->jquery, 1)
->add($this->getScript());;
// fix zebra styled tables
$this->parent()->addChild(new DataView('<tr style="display: none"></tr>'));
}
protected function getScript()
{
$source = $this->getScriptSource();
return new DataView("<script>jQuery(function(){ $source });</script>");
}
protected function getScriptSource()
{
return '
jQuery(\'tr[data-row-with-details="1"]\').click(function() {
jQuery(this).next().toggle(\'slow\');
});
';
}
}