Skip to content

Commit

Permalink
Basic grid framework
Browse files Browse the repository at this point in the history
  • Loading branch information
czubehead committed May 20, 2018
1 parent f987113 commit 7b10055
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/BootstrapContainer.php
Expand Up @@ -21,7 +21,5 @@
*/
class BootstrapContainer extends Container
{


use BootstrapContainerTrait;
}
105 changes: 105 additions & 0 deletions src/Grid/BootstrapCell.php
@@ -0,0 +1,105 @@
<?php
/**
* Created by Petr Čech (czubehead).
* Timestamp: 20.5.18 17:12
*/

namespace Czubehead\BootstrapForms\Grid;


use Czubehead\BootstrapForms\Traits\BootstrapContainerTrait;
use LogicException;
use Nette\ComponentModel\IComponent;
use Nette\SmartObject;
use Nette\Utils\Html;


/**
* Class BootstrapCell.
* Represents a row-column pair = table cell in Bootstrap grid system. This is the part with col-*-* class.
* Only one component can be present.
* @package Czubehead\BootstrapForms\Grid
* @property-read int $numOfColumns Number of Bootstrap columns to occupy
* @property-read bool $occupied if a control is already set and thus no other can be added.
* @property-read Html $elementPrototype the Html div that will be rendered. You may define additional
* properties.
*/
class BootstrapCell
{
use SmartObject;
use BootstrapContainerTrait;

/**
* @var int
*/
private $numOfColumns;
/**
* If is already hosting a control
* @var bool
*/
private $isOccupied = FALSE;
/**
* @var BootstrapRow
*/
private $row;
/**
* @var Html
*/
private $elementPrototype;

/**
* BootstrapRow constructor.
* @param BootstrapRow $row Row this is a child of
* @param int $numOfColumns Number of Bootstrap columns to occupy
*/
public function __construct(BootstrapRow $row, $numOfColumns)
{
$this->numOfColumns = $numOfColumns;
$this->row = $row;

$this->elementPrototype = Html::el('div');
}

/**
* Gets the prototype of this cell so you can define additional attributes. Col-* class is added during
* rendering and is not present, so don't add it...
* @return Html
*/
public function getElementPrototype()
{
return $this->elementPrototype;
}

/**
* @return int
*/
public function getNumOfColumns()
{
return $this->numOfColumns;
}

/**
* @return bool
*/
public function isOccupied()
{
return $this->isOccupied;
}

/**
* Delegate to underlying component.
* @param IComponent $component
* @param $name
* @param null $insertBefore
*/
protected function addComponent(IComponent $component, $name, $insertBefore = NULL)
{
if ($this->isOccupied) {
throw new LogicException('a control for this cell has already been set, you cannot add another one');
}

/** @noinspection PhpInternalEntityUsedInspection */
$this->row->addComponent($component, $name, $insertBefore);
$this->isOccupied = TRUE;
}
}
150 changes: 150 additions & 0 deletions src/Grid/BootstrapRow.php
@@ -0,0 +1,150 @@
<?php
/**
* Created by Petr Čech (czubehead).
* Timestamp: 20.5.18 17:01
*/

namespace Czubehead\BootstrapForms\Grid;

use Nette\ComponentModel\IComponent;
use Nette\Forms\Container;
use Nette\InvalidArgumentException;
use Nette\SmartObject;
use Nette\Utils\Html;


/**
* Class BootstrapRow.
* Represents a row in Bootstrap grid system.
* @package Czubehead\BootstrapForms\Grid
* @property string $gridBreakpoint Bootstrap breakpoint - usually xs, sm, md, lg. md by
* default.
* @property-read string[] $ownedNames list of names of components which were added to this row
* @property-read BootstrapCell[] $cells cells in this row
* @property-read Html $elementPrototype the Html div that will be rendered. You may define
* additional properties.
*/
class BootstrapRow
{
use SmartObject;

/**
* Number of columns in Bootstrap grid. Default is 12, but it can be customized.
* @var int
*/
public $numOfColumns = 12;
/**
* Number of columns used by added cells.
* @var int
*/
private $columnsOccupied = 0;

/**
* Form or container this belong to
* @var Container
*/
private $container;
/**
* @var string
*/
private $gridBreakpoint = 'md';
/**
* @var string[]
*/
private $ownedNames = [];
/**
* @var BootstrapCell[]
*/
private $cells = [];
/**
* @var Html
*/
private $elementPrototype;

/**
* BootstrapRow constructor.
* @param Container $container Form or container this belong to. Components will be added to this
* component
*/
public function __construct(Container $container)
{
$this->container = $container;
$this->elementPrototype = Html::el('div', [
'class' => ['row'],
]);
}

/**
* Adds a new cell to which a control can be added.
* @param int $numOfColumns Number of grid columns to use up
*/
public function addCell($numOfColumns)
{
if ($this->columnsOccupied + $numOfColumns > $this->numOfColumns) {
throw new InvalidArgumentException(
"the given number of columns with combination of already used"
. " columns exceeds column limit ({$this->numOfColumns})");
}

$cell = new BootstrapCell($this, $numOfColumns);
$this->cells[] = $cell;
}

/**
* Delegate to underlying container and remember it.
* @param IComponent $component
* @param $name
* @param null $insertBefore
* @internal
*/
public function addComponent(IComponent $component, $name, $insertBefore = NULL)
{
$this->container->addComponent($component, $name, $insertBefore);
$this->ownedNames[] = $name;
}

/**
* @return BootstrapCell[]
*/
public function getCells()
{
return $this->cells;
}

/**
* The container without content
* @return Html
*/
public function getElementPrototype()
{
return $this->elementPrototype;
}

/**
* @return string
*/
public function getGridBreakpoint()
{
return $this->gridBreakpoint;
}

/**
* Sets the xs, sm, md, lg part
* @param string $gridBreakpoint
* @return BootstrapRow
*/
public function setGridBreakpoint($gridBreakpoint)
{
$this->gridBreakpoint = $gridBreakpoint;

return $this;
}

/**
* @return string[]
*/
public function getOwnedNames()
{
return $this->ownedNames;
}
}

0 comments on commit 7b10055

Please sign in to comment.