| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace Dcat\Admin\Layout;
- use Dcat\Admin\Grid;
- use Illuminate\Contracts\Support\Renderable;
- class Column implements Buildable
- {
- /**
- * grid system prefix width.
- *
- * @var array
- */
- protected $width = [];
- /**
- * @var array
- */
- protected $contents = [];
- /**
- * Column constructor.
- *
- * @param $content
- * @param int $width
- */
- public function __construct($content, $width = 12)
- {
- if ($content instanceof \Closure) {
- call_user_func($content, $this);
- } else {
- $this->append($content);
- }
- ///// set width.
- // if null, or $this->width is empty array, set as "md" => "12"
- if (is_null($width) || (is_array($width) && count($width) === 0)) {
- $this->width['md'] = 12;
- }
- // $this->width is number(old version), set as "md" => $width
- elseif (is_numeric($width)) {
- $this->width['md'] = $width;
- } else {
- $this->width = $width;
- }
- }
- /**
- * Append content to column.
- *
- * @param $content
- *
- * @return $this
- */
- public function append($content)
- {
- $this->contents[] = $content;
- return $this;
- }
- /**
- * Add a row for column.
- *
- * @param $content
- *
- * @return Column
- */
- public function row($content)
- {
- if (!$content instanceof \Closure) {
- $row = new Row($content);
- } else {
- $row = new Row();
- call_user_func($content, $row);
- }
- ob_start();
- $row->build();
- $contents = ob_get_contents();
- ob_end_clean();
- return $this->append($contents);
- }
- /**
- * Build column html.
- */
- public function build()
- {
- $this->startColumn();
- foreach ($this->contents as $content) {
- if ($content instanceof Renderable || $content instanceof Grid) {
- echo $content->render();
- } else {
- echo (string) $content;
- }
- }
- $this->endColumn();
- }
- /**
- * Start column.
- */
- protected function startColumn()
- {
- // get class name using width array
- $classnName = collect($this->width)->map(function ($value, $key) {
- return "col-$key-$value";
- })->implode(' ');
- echo "<div class=\"{$classnName}\">";
- }
- /**
- * End column.
- */
- protected function endColumn()
- {
- echo '</div>';
- }
- }
|