Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DetailsRow & AjaxDetailsRow components added
- Loading branch information
Showing
3 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace ViewComponents\Grids\Component; | ||
|
||
use ViewComponents\ViewComponents\Component\DataView; | ||
use ViewComponents\ViewComponents\Resource\ResourceManager; | ||
|
||
class AjaxDetailsRow extends DetailsRow | ||
{ | ||
|
||
protected $urlGenerator; | ||
|
||
public function __construct(callable $urlGenerator, ResourceManager $resourceManager = null) | ||
{ | ||
parent::__construct(new DataView(null, function ($url) { | ||
return "<div data-details-container='1' data-details-url='$url'></div>"; | ||
}), $resourceManager); | ||
$this->setUrlGenerator($urlGenerator); | ||
} | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
public function getUrlGenerator() | ||
{ | ||
return $this->urlGenerator; | ||
} | ||
|
||
/** | ||
* @param callable $urlGenerator | ||
* @return $this | ||
*/ | ||
public function setUrlGenerator(callable $urlGenerator) | ||
{ | ||
$this->urlGenerator = $urlGenerator; | ||
return $this; | ||
} | ||
|
||
public function render() | ||
{ | ||
$this->view->setData(call_user_func($this->getUrlGenerator(), $this->getGrid()->getCurrentRow())); | ||
return SolidRow::render(); | ||
} | ||
|
||
protected function getScriptSource() | ||
{ | ||
return parent::getScriptSource() . ' | ||
jQuery(\'tr[data-row-with-details="1"]\').click(function() { | ||
var $detailsRow = jQuery(this).next(); | ||
var $detailsContainer; | ||
if (!$detailsRow.data(\'loaded\')) { | ||
$detailsRow.data(\'loaded\', 1); | ||
$detailsContainer = $detailsRow.find(\'[data-details-container="1"]\'); | ||
$detailsContainer.load($detailsContainer.data("details-url")); | ||
} | ||
}); | ||
'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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\'); | ||
}); | ||
'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters