|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ViewComponents\Grids\Component; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
use ViewComponents\Grids\Grid; |
|
7
|
|
|
use ViewComponents\ViewComponents\Base\Compound\PartInterface; |
|
8
|
|
|
use ViewComponents\ViewComponents\Base\Compound\PartTrait; |
|
9
|
|
|
use ViewComponents\ViewComponents\Base\DataViewComponentInterface; |
|
10
|
|
|
use ViewComponents\ViewComponents\Base\Html\TagInterface; |
|
11
|
|
|
use ViewComponents\ViewComponents\Component\Compound; |
|
12
|
|
|
use ViewComponents\ViewComponents\Component\DataView; |
|
13
|
|
|
use ViewComponents\ViewComponents\Resource\ResourceManager; |
|
14
|
|
|
use ViewComponents\ViewComponents\Service\Services; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This component adds hidden rows after each existing table row |
|
18
|
|
|
* and 'onclick' handlers for regular table rows that displays associated details row when clicking on it. |
|
19
|
|
|
* |
|
20
|
|
|
* DetailsRow includes jQuery on page if it was not included via view-components resource manager before. |
|
21
|
|
|
* If jQuery included to your page not via resource manager, it's possible to tell resource manager to ignore it, |
|
22
|
|
|
* see link below for instructions. |
|
23
|
|
|
* @link https://github.com/view-components/view-components/blob/master/doc/cookbook.md |
|
24
|
|
|
*/ |
|
25
|
|
|
class DetailsRow extends SolidRow implements PartInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use PartTrait { |
|
28
|
|
|
PartTrait::attachToCompound as private attachToCompoundInternal; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
const ID = 'details_row'; |
|
32
|
|
|
|
|
33
|
|
|
protected $view; |
|
34
|
|
|
/** @var ResourceManager */ |
|
35
|
|
|
private $resourceManager; |
|
36
|
|
|
private $jquery; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* DetailsRow constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param DataViewComponentInterface $view details row content, will be initialized by data row |
|
42
|
|
|
* @param ResourceManager|null $resourceManager |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(DataViewComponentInterface $view, ResourceManager $resourceManager = null) |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct(); |
|
47
|
|
|
$this->getRowTag() |
|
48
|
|
|
->setAttribute('style', 'display:none;') |
|
49
|
|
|
->setAttribute('data-details-row', '1'); |
|
50
|
|
|
$this->addChild($this->view = $view); |
|
51
|
|
|
$this->setDestinationParentId(Grid::COLLECTION_VIEW_ID); |
|
52
|
|
|
$this->setId(static::ID); |
|
53
|
|
|
$this->resourceManager = $resourceManager ?: Services::resourceManager(); |
|
54
|
|
|
$this->jquery = $this->resourceManager->js('jquery'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function render() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->view->setData($this->getGrid()->getCurrentRow()); |
|
|
|
|
|
|
60
|
|
|
return parent::render(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return null|Grid |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getGrid() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->root; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function attachToCompound(Compound $root, $prepend = false) |
|
72
|
|
|
{ |
|
73
|
|
|
$isAlreadyAttached = $this->root !== null; |
|
74
|
|
|
$this->attachToCompoundInternal($root, $prepend); |
|
75
|
|
|
if ($isAlreadyAttached) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
$tr = $this->getGrid()->getRecordView(); |
|
|
|
|
|
|
79
|
|
|
if (!$tr instanceof TagInterface) { |
|
80
|
|
|
throw new RuntimeException( |
|
81
|
|
|
"Details row works only with record_view components implementing TagInterface" |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
$tr->setAttribute('data-row-with-details', 1); |
|
85
|
|
|
|
|
86
|
|
|
$this->getGrid()->children() |
|
87
|
|
|
->add($this->jquery, 1) |
|
88
|
|
|
->add($this->getScript()); |
|
89
|
|
|
|
|
90
|
|
|
// fix zebra styled tables |
|
91
|
|
|
$this->parent()->addChild(new DataView('<tr style="display: none"></tr>')); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function getScript() |
|
95
|
|
|
{ |
|
96
|
|
|
$source = $this->getScriptSource(); |
|
97
|
|
|
return new DataView("<script>jQuery(function(){ $source });</script>"); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getScriptSource() |
|
101
|
|
|
{ |
|
102
|
|
|
return ' |
|
103
|
|
|
jQuery(\'tr[data-row-with-details="1"]\').click(function() { |
|
104
|
|
|
jQuery(this).next().toggle(\'slow\'); |
|
105
|
|
|
}); |
|
106
|
|
|
'; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: