Skip to content

Commit

Permalink
Merge pull request #16 from CottaCush/features/rework-widgets
Browse files Browse the repository at this point in the history
(BaseWidget): Add `beginDiv` and `endDiv`
  • Loading branch information
otaruMendez authored Jul 27, 2018
2 parents b47dc29 + 1860eaa commit 690338c
Show file tree
Hide file tree
Showing 15 changed files with 338 additions and 33 deletions.
15 changes: 15 additions & 0 deletions src/Assets/EmptyStateAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace CottaCush\Yii2\Assets;

/**
* Class EmptyStateAsset
* @package CottaCush\Yii2\Assets
* @author Olawale Lawal <wale@cottacush.com>
*/
class EmptyStateAsset extends LocalAssetBundle
{
public $css = [
'css/empty-state.css',
];
}
22 changes: 20 additions & 2 deletions src/Helpers/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,30 @@ public static function svgImg($src, $options = [])
if (is_bool($fallback) && is_string($src)) {
$fallback = rtrim($src, 'svg') . $fallbackExt;
$options['onerror'] = "this.src = '$fallback'; this.onerror = '';";
}
else if (is_string($fallback)) {
} else if (is_string($fallback)) {
$options['onerror'] = "this.src = '$fallback'; this.onerror = '';";
}
}

return self::img($src, $options);
}

/**
* @param array|string $classNames
* @param array $options
* @return string
*/
public static function beginDiv($classNames = [], $options = [])
{
self::addCssClass($options, $classNames);
return self::beginTag('div', $options) . "\n";
}

/**
* @return string
*/
public static function endDiv()
{
return self::endTag('div') . "\n";
}
}
6 changes: 3 additions & 3 deletions src/Widgets/ActionButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ActionButtons extends BaseWidget
/**
* @var string the actions button dropdown label
*/
public $actionsButtonLabel = 'Actions';
public $actionsButtonLabel = 'Actions ';
/**
* @var string caret html for the action button
*/
Expand Down Expand Up @@ -152,14 +152,14 @@ private function renderActionButton()
private function beginButtonGroup()
{
if ($this->groupLinks) {
echo Html::beginTag('div', ['class' => 'btn-group']);
echo $this->beginDiv('btn-group');
}
}

private function endButtonGroup()
{
if ($this->groupLinks) {
echo Html::endTag('div');
echo $this->endDiv();
}
}
}
56 changes: 56 additions & 0 deletions src/Widgets/BaseContentHeaderButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace CottaCush\Yii2\Widgets;

use CottaCush\Yii2\Helpers\Html;
use yii\helpers\ArrayHelper;

/**
* Class BaseContentHeaderButton
* @author Olajide Oye <jide@cottacush.com>
* @package app\widgets
*/
class BaseContentHeaderButton extends BaseWidget
{
public $button;
public $buttonClass;

protected function setButton()
{
if (is_string($this->button)) {
return;
}
if (is_array($this->button)) {
$this->buildButton($this->button);
return;
}

$buttonDetails = ArrayHelper::getValue($this->view->params, 'content-header-button', '');
if (is_string($buttonDetails)) {
$this->button = $buttonDetails;
return;
}
$this->buildButton($buttonDetails);
}

private function buildButton($buttonArray)
{
if (!is_array($buttonArray)) {
return;
}

$text = ArrayHelper::getValue($buttonArray, 'label', 'Add New');
$tag = ArrayHelper::getValue($buttonArray, 'tag', 'a');
$iconName = ArrayHelper::getValue($buttonArray, 'icon');
$icon = '';
if ($iconName) {
$icon = Html::faIcon($iconName);
}
$options = ArrayHelper::getValue($buttonArray, 'options', []);

Html::addCssClass($options, $this->buttonClass);

$this->button = Html::tag($tag, "$icon $text", $options);

}
}
5 changes: 2 additions & 3 deletions src/Widgets/BaseFormWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CottaCush\Yii2\Widgets;

use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;

Expand All @@ -11,11 +10,11 @@
* @author Damilola Olaleye <damilola@cottacush.com>
* @package CottaCush\Yii2\Widgets
*/
class BaseFormWidget extends Widget
class BaseFormWidget extends BaseWidget
{
public $formRoute;
public $formOptions = [];
public $formMethod ='post';
public $formMethod = 'post';

public function run()
{
Expand Down
10 changes: 6 additions & 4 deletions src/Widgets/BaseModalWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class BaseModalWidget extends BaseWidget
public $title;
public $id;
public $route;
public $footerSubmit;
public $footerSubmit = 'Submit';
public $footerCancel = 'Cancel';
public $formMethod;
public $formOptions;
public $modalHeaderClass;
public $modalCancelFooterClass;
public $modalSubmitFooterClass;
public $modalCancelFooterClass = 'btn btn-default';
public $modalSubmitFooterClass = 'btn btn-primary';

/** @var ActiveForm $form */
protected $form;

Expand All @@ -45,7 +47,7 @@ public function beginModal()
'id' => $this->id
],
'footer' =>
Html::button('Cancel', ['class' => $this->modalCancelFooterClass, 'data-dismiss' => 'modal']) .
Html::button($this->footerCancel, ['class' => $this->modalCancelFooterClass, 'data-dismiss' => 'modal']) .
Html::input(
'submit',
'',
Expand Down
17 changes: 17 additions & 0 deletions src/Widgets/BaseWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CottaCush\Yii2\Widgets;

use CottaCush\Yii2\Helpers\Html;
use yii\base\Widget;

/**
Expand All @@ -12,5 +13,21 @@
*/
class BaseWidget extends Widget
{
/**
* @param array|string $classNames
* @param array $options
* @return string
*/
protected function beginDiv($classNames = [], $options = [])
{
return Html::beginDiv($classNames, $options);
}

/**
* @return string
*/
protected function endDiv()
{
return Html::endDiv();
}
}
69 changes: 69 additions & 0 deletions src/Widgets/ContentHeaderWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace CottaCush\Yii2\Widgets;

use CottaCush\Yii2\Helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\widgets\Breadcrumbs;

/**
* Class ContentHeaderWidget
* @author Olajide Oye <jide@cottacush.com>
* @package CottaCush\Yii2\Widgets
*/
class ContentHeaderWidget extends BaseContentHeaderButton
{
public $title;
public $breadcrumbs;
public $buttonClass = 'btn btn-sm content-header-btn';
public $contentRight;
public $icon;

public function init()
{
parent::init();

$view = $this->view;

if (is_null($this->title)) {
$this->title = ArrayHelper::getValue($view, 'params.pageTitle', $view->title);
}
if (is_null($this->breadcrumbs)) {
$this->breadcrumbs = ArrayHelper::getValue($view, 'params.breadcrumbs', $this->title);
}
if (is_null($this->contentRight)) {
$this->contentRight = ArrayHelper::getValue($view, 'params.contentHeaderRight', '');
}

$this->setButton();
}

public function run()
{
echo Html::beginTag('section', ['class' => 'content-header']);
echo $this->beginDiv(' clearfix');
echo $this->beginDiv(' content-header__left');
echo Html::beginTag('h1', ['class' => 'content-header-title']);

echo ($this->icon) ? Html::faIcon($this->icon) . ' ' . $this->title : $this->title;

echo Html::endTag('h1');

echo $this->endDiv();
echo Html::tag('div', $this->contentRight, ['class' => 'content-header__right']);
if (ArrayHelper::getValue($this->view->params, 'show-content-header-button', true)) {
echo $this->button;
}
echo $this->endDiv();
echo Breadcrumbs::widget([
'tag' => 'ol',
'homeLink' => [
'label' => 'Home',
'url' => Url::toRoute('/')
],
'links' => $this->breadcrumbs
]);
echo Html::endTag('section');
}
}
22 changes: 11 additions & 11 deletions src/Widgets/DropzoneWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace CottaCush\Yii2\Widgets;

use CottaCush\Yii2\Assets\DropzoneImageUploadAsset;
use yii\base\Widget;
use yii\helpers\Html;

/**
Expand All @@ -22,7 +21,7 @@
*
* @author Adeyemi Olaoye <yemi@cottacush.com>
*/
class DropzoneWidget extends Widget
class DropzoneWidget extends BaseWidget
{
public $imageUrl = null;
public $uploadUrl;
Expand All @@ -47,9 +46,9 @@ public function init()

public function run()
{
echo Html::beginTag('div', ['class' => 'clearfix']);
echo $this->beginDiv('clearfix');

echo Html::beginTag('div', ['class' => 'dropzone-placeholder']);
echo $this->beginDiv('dropzone-placeholder');

$placeholderImage = Html::img($this->imageUrl, ['class' => 'img-responsive dropzone-placeholder__image']);
$removeImageLabel = Html::a($this->removeImageLabel, '#', [
Expand All @@ -71,7 +70,8 @@ public function run()
]
);

echo Html::endTag('div');
echo $this->endDiv(); //.dropzone-placeholder
echo $this->endDiv(); //.clearfix


echo Html::script('var dropzoneOptions =' . json_encode([
Expand All @@ -88,20 +88,20 @@ public function run()

private function getDefaultPreviewTemplate()
{
$previewTemplate = Html::beginTag('div', ['class' => 'dz-preview dz-image-preview']);
$previewTemplate .= Html::beginTag('div', ['class' => 'dz-image']);
$previewTemplate = $this->beginDiv('dz-preview dz-image-preview');
$previewTemplate .= $this->beginDiv('dz-image');
$previewTemplate .= Html::img('', ['data-dz-thumbnail' => '']);
$previewTemplate .= Html::endTag('div');
$previewTemplate .= Html::beginTag('div', ['class' => 'dz-error-message']);
$previewTemplate .= $this->endDiv();
$previewTemplate .= $this->beginDiv('dz-error-message');
$previewTemplate .= Html::tag('span', '', ['data-dz-errormessage' => '']);
$previewTemplate .= Html::endTag('div');
$previewTemplate .= $this->endDiv();
$previewTemplate .= Html::tag(
'span',
'',
['id' => 'dropzone_upload_info', 'class' => 'text-center dropzone_upload_info']
);
$previewTemplate .= Html::tag('a', '', ['class' => 'dz-remove', 'data-dz-remove']);
$previewTemplate .= Html::endTag('div');
$previewTemplate .= $this->endDiv();
return $previewTemplate;
}
}
42 changes: 42 additions & 0 deletions src/Widgets/EmptyStateWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace CottaCush\Yii2\Widgets;

use CottaCush\Yii2\Assets\EmptyStateAsset;
use CottaCush\Yii2\Assets\FontAwesomeAsset;
use CottaCush\Yii2\Helpers\Html;

/**
* Class EmptyStateWidget
* @package CottaCush\Yii2\Widgets
* @author Olajide Oye <jide@cottacush.com>
* @author Olawale Lawal <wale@cottacush.com>
*/
class EmptyStateWidget extends BaseWidget
{
public $icon;
public $title;
public $description;
public $buttonClass = 'btn btn-primary';

public $showButton = true;
public $button;

public function run()
{
echo Html::beginTag('section', ['class' => 'empty-state']);

echo Html::faIcon($this->icon, ['class' => 'empty-state__icon']);
echo Html::tag('h2', $this->title, ['class' => 'empty-state__title']);
echo Html::tag('p', $this->description, ['class' => 'empty-state__description']);

if ($this->showButton) {
echo Html::tag('div', $this->button, ['class' => 'empty-state__btn']);
}

echo Html::endTag('section');

EmptyStateAsset::register($this->getView());
FontAwesomeAsset::register($this->view);
}
}
1 change: 1 addition & 0 deletions src/Widgets/GridViewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private function setupDefaultConfigs()
{
$this->setLayout();
$this->setSummary();

if (!$this->isEmpty()) {
$this->setActionBar();
}
Expand Down
Loading

0 comments on commit 690338c

Please sign in to comment.