Skip to content

Commit

Permalink
Merge 464ff1e into 8b5039c
Browse files Browse the repository at this point in the history
  • Loading branch information
tafid committed Nov 6, 2020
2 parents 8b5039c + 464ff1e commit bcb2e67
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/actions/Action.php
Expand Up @@ -31,6 +31,7 @@ class Action extends \yii\base\Action
const EVENT_BEFORE_LOAD = 'beforeLoad';
const EVENT_BEFORE_PERFORM = 'beforePerform';
const EVENT_AFTER_PERFORM = 'afterPerform';
const EXPECTED_AJAX_RESPONSE_HEADER_NAME = 'X-Expected-Ajax-Response';

/**
* @var Controller the controller that owns this action
Expand Down
28 changes: 0 additions & 28 deletions src/actions/RenderSummaryAction.php

This file was deleted.

34 changes: 34 additions & 0 deletions src/actions/VariantsAction.php
@@ -0,0 +1,34 @@
<?php
/**
* HiPanel core package
*
* @link https://hipanel.com/
* @package hipanel-core
* @license BSD-3-Clause
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
*/

namespace hipanel\actions;

use Yii;

class VariantsAction extends RenderAction
{
public array $variants;

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

public function run()
{
$headerName = $this->controller->request->headers->get(self::EXPECTED_AJAX_RESPONSE_HEADER_NAME);
foreach ($this->variants as $expected => $variant) {
if ($expected === $headerName) {
return $variant($this);
}
}
Yii::$app->end();
}
}
2 changes: 1 addition & 1 deletion src/base/SearchModelTrait.php
Expand Up @@ -12,7 +12,7 @@

use hiqdev\hiart\ActiveQuery;
use hiqdev\hiart\ActiveRecord;
use yii\data\ActiveDataProvider;
use hiqdev\hiart\ActiveDataProvider;
use yii\helpers\ArrayHelper;

/**
Expand Down
26 changes: 26 additions & 0 deletions src/widgets/CountEnabler.php
@@ -0,0 +1,26 @@
<?php

namespace hipanel\widgets;

use hiqdev\hiart\ActiveDataProvider;
use Yii;
use yii\base\Widget;
use yii\grid\GridView;

class CountEnabler extends Widget
{
public ActiveDataProvider $dataProvider;

public $content;

public function run()
{
$this->dataProvider->enableCount();
$grid = Yii::createObject([
'class' => GridView::class,
'dataProvider' => $this->dataProvider,
]);

return call_user_func($this->content, $grid);
}
}
8 changes: 8 additions & 0 deletions src/widgets/HookInterface.php
@@ -0,0 +1,8 @@
<?php

namespace hipanel\widgets;

interface HookInterface
{
public function registerJsHook(string $paramName): void;
}
25 changes: 25 additions & 0 deletions src/widgets/HookTrait.php
@@ -0,0 +1,25 @@
<?php

namespace hipanel\widgets;

use hipanel\actions\Action;
use yii\web\View;

trait HookTrait
{
public function registerJsHook(string $reqHeaderParamName): void
{
$id = $this->getId();
$headerName = Action::EXPECTED_AJAX_RESPONSE_HEADER_NAME;
$this->view->registerJs("$.ajax({
type: 'GET',
url: document.URL,
beforeSend: xhr => {
xhr.setRequestHeader('{$headerName}', '{$reqHeaderParamName}');
},
success: html => {
$('#{$id}').html(html);
}
});", View::POS_LOAD);
}
}
31 changes: 31 additions & 0 deletions src/widgets/PagerHook.php
@@ -0,0 +1,31 @@
<?php

namespace hipanel\widgets;

use Yii;
use yii\helpers\Html;
use yii\widgets\LinkPager;

class PagerHook extends LinkPager implements HookInterface
{
use HookTrait;

public string $tag = 'div';

public string $content;

public function init()
{
$this->registerJsHook('pager');
}

public function run()
{
return Html::tag($this->tag, $this->content ?? $this->getLoader(), array_merge(['id' => $this->getId()], $this->options));
}

private function getLoader(): string
{
return Html::tag('span', null, ['class' => 'fa fa-spinner fa-pulse', 'style' => 'margin-right: .7rem;']) . Yii::t('hipanel', 'loading pager...');
}
}
17 changes: 6 additions & 11 deletions src/widgets/SummaryHook.php
Expand Up @@ -5,10 +5,11 @@
use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use yii\web\View;

class SummaryHook extends Widget
class SummaryHook extends Widget implements HookInterface
{
use HookTrait;

public string $tag = 'div';

public array $options = [];
Expand All @@ -18,22 +19,16 @@ class SummaryHook extends Widget
public function init()
{
parent::init();
$this->registerJs();
$this->registerJsHook('summary');
}

public function run()
{
return Html::tag($this->tag, $this->content ?? $this->getLoader(), array_merge(['id' => $this->getId(), 'class' => 'summary'], $this->options));
}

private function registerJs(): void
{
$id = $this->getId();
$this->view->registerJs("$.get(document.URL, summary => { $('#{$id}').html(summary) });", View::POS_LOAD);
return Html::tag($this->tag, $this->content ?? $this->getLoader(), array_merge(['id' => $this->getId()], $this->options));
}

private function getLoader(): string
{
return Html::tag('span', null, ['class' => 'fa fa-spinner fa-pulse', 'style' => 'margin-right: .7rem;']) . Yii::t('hipanel', 'loading summary...');
return Html::tag('span', null, ['class' => 'fa fa-spinner fa-pulse', 'style' => 'margin: .7rem; 0 1rem']) . Yii::t('hipanel', 'loading summary...');
}
}
5 changes: 3 additions & 2 deletions src/widgets/SummaryWidget.php
Expand Up @@ -31,8 +31,9 @@ public function run()
$locals = $this->getSumsString($this->local_sums ?? []);
$totals = $this->getSumsString($this->total_sums ?? []);

return ($totals !== '' ? Yii::t('hipanel:stock', 'TOTAL: {sum}', ['sum' => $totals]) : null) .
($locals !== '' ? '<br><span class="text-muted">' . Yii::t('hipanel:stock', 'on screen: {sum}', ['sum' => $locals]) . '</span>' : null);
return '<div class="summary">' . ($totals !== '' ? Yii::t('hipanel:stock', 'TOTAL: {sum}', ['sum' => $totals]) : null) .
($locals !== '' ? '<br><span class="text-muted">' . Yii::t('hipanel:stock', 'on screen: {sum}', ['sum' => $locals]) . '</span>' : null)
. '</div>';
}

/**
Expand Down

0 comments on commit bcb2e67

Please sign in to comment.