From b26a8f8e0a7eef54b555fc0d8b21bfbc808e5fbb Mon Sep 17 00:00:00 2001 From: Denis Kopylov Date: Wed, 19 Jun 2019 13:35:20 +0300 Subject: [PATCH] [Ui] Allow to define listing configuration via ui component xml --- .../web/js/variations/paging/sizes.js | 30 +++--- app/code/Magento/Ui/Component/Paging.php | 94 ++++++++++++------- .../Ui/Test/Unit/Component/PagingTest.php | 24 +---- .../Ui/view/base/web/js/grid/paging/paging.js | 13 ++- .../Ui/view/base/web/js/grid/paging/sizes.js | 23 ----- .../View/Layout/Generator/UiComponent.php | 12 +-- 6 files changed, 92 insertions(+), 104 deletions(-) diff --git a/app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/paging/sizes.js b/app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/paging/sizes.js index a8b8f95f6536f..155b176bd431b 100644 --- a/app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/paging/sizes.js +++ b/app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/paging/sizes.js @@ -9,21 +9,21 @@ define([ return Sizes.extend({ defaults: { - excludedOptions: ['100', '200'] - }, - - /** - * @override - */ - initialize: function () { - this._super(); - - this.excludedOptions.forEach(function (excludedOption) { - delete this.options[excludedOption]; - }, this); - this.updateArray(); - - return this; + options: { + '20': { + value: 20, + label: 20 + }, + '30': { + value: 30, + label: 30 + }, + '50': { + value: 50, + label: 50 + } + }, + value: 20 } }); }); diff --git a/app/code/Magento/Ui/Component/Paging.php b/app/code/Magento/Ui/Component/Paging.php index ae83bfc3916cc..57d0ec53777e2 100644 --- a/app/code/Magento/Ui/Component/Paging.php +++ b/app/code/Magento/Ui/Component/Paging.php @@ -3,9 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Ui\Component; /** + * Class Paging + * * @api * @since 100.0.2 */ @@ -13,6 +16,41 @@ class Paging extends AbstractComponent { const NAME = 'paging'; + /** + * Default paging options + * + * @var array + */ + private $defaultOptions = [ + '20' => [ + 'value' => 20, + 'label' => 20 + ], + '30' => [ + 'value' => 30, + 'label' => 30 + ], + '50' => [ + 'value' => 50, + 'label' => 50 + ], + '100' => [ + 'value' => 100, + 'label' => 100 + ], + '200' => [ + 'value' => 200, + 'label' => 200 + ], + ]; + + /** + * Default page size + * + * @var int + */ + private $defaultPageSize = 20; + /** * Default component data * @@ -20,29 +58,6 @@ class Paging extends AbstractComponent */ protected $_data = [ 'config' => [ - 'options' => [ - '20' => [ - 'value' => 20, - 'label' => 20 - ], - '30' => [ - 'value' => 30, - 'label' => 30 - ], - '50' => [ - 'value' => 50, - 'label' => 50 - ], - '100' => [ - 'value' => 100, - 'label' => 100 - ], - '200' => [ - 'value' => 200, - 'label' => 200 - ], - ], - 'pageSize' => 20, 'current' => 1 ] ]; @@ -65,13 +80,13 @@ public function getComponentName() public function prepare() { $this->prepareOptions(); + $this->preparePageSize(); $paging = $this->getContext()->getRequestParam('paging'); if (!isset($paging['notLimits'])) { $this->getContext() ->getDataProvider() ->setLimit($this->getOffset($paging), $this->getSize($paging)); } - parent::prepare(); } @@ -83,12 +98,26 @@ public function prepare() protected function prepareOptions() { $config = $this->getData('config'); - if (isset($config['options'])) { - $config['options'] = array_values($config['options']); - foreach ($config['options'] as &$item) { - $item['value'] = (int) $item['value']; - } - unset($item); + if (!isset($config['options'])) { + $config['options'] = $this->defaultOptions; + } + foreach ($config['options'] as &$item) { + $item['value'] = (int)$item['value']; + } + unset($item); + $this->setData('config', $config); + } + + /** + * Prepare page size + * + * @return void + */ + private function preparePageSize() + { + $config = $this->getData('config'); + if (!isset($config['pageSize'])) { + $config['pageSize'] = $this->defaultPageSize; $this->setData('config', $config); } } @@ -102,7 +131,7 @@ protected function prepareOptions() protected function getOffset($paging) { $defaultPage = $this->getData('config/current') ?: 1; - return (int) (isset($paging['current']) ? $paging['current'] : $defaultPage); + return (int)(isset($paging['current']) ? $paging['current'] : $defaultPage); } /** @@ -113,7 +142,6 @@ protected function getOffset($paging) */ protected function getSize($paging) { - $defaultLimit = $this->getData('config/pageSize') ?: 20; - return (int) (isset($paging['pageSize']) ? $paging['pageSize'] : $defaultLimit); + return (int)(isset($paging['pageSize']) ? $paging['pageSize'] : $this->getData('config/pageSize')); } } diff --git a/app/code/Magento/Ui/Test/Unit/Component/PagingTest.php b/app/code/Magento/Ui/Test/Unit/Component/PagingTest.php index 44dca689eef99..97df719ef14cb 100644 --- a/app/code/Magento/Ui/Test/Unit/Component/PagingTest.php +++ b/app/code/Magento/Ui/Test/Unit/Component/PagingTest.php @@ -81,31 +81,11 @@ public function testPrepare() ], 'config' => [ 'options' => [ - [ - 'value' => 20, - 'label' => 20 - ], - [ - 'value' => 30, - 'label' => 30 - ], - [ - 'value' => 50, - 'label' => 50 - ], - [ - 'value' => 100, - 'label' => 100 - ], - [ - 'value' => 200, - 'label' => 200 - ], - [ + 'options1' => [ 'value' => 20, 'label' => 'options1' ], - [ + 'options2' => [ 'value' => 40, 'label' => 'options2' ], diff --git a/app/code/Magento/Ui/view/base/web/js/grid/paging/paging.js b/app/code/Magento/Ui/view/base/web/js/grid/paging/paging.js index 8e6f1496495c7..534d292809ed1 100644 --- a/app/code/Magento/Ui/view/base/web/js/grid/paging/paging.js +++ b/app/code/Magento/Ui/view/base/web/js/grid/paging/paging.js @@ -20,7 +20,6 @@ define([ template: 'ui/grid/paging/paging', totalTmpl: 'ui/grid/paging-total', totalRecords: 0, - pageSize: 20, pages: 1, current: 1, selectProvider: 'ns = ${ $.ns }, index = ids', @@ -35,7 +34,6 @@ define([ }, imports: { - pageSize: '${ $.sizesConfig.name }:value', totalSelected: '${ $.selectProvider }:totalSelected', totalRecords: '${ $.provider }:data.totalRecords', filters: '${ $.provider }:params.filters' @@ -46,6 +44,11 @@ define([ current: '${ $.provider }:params.paging.current' }, + links: { + options: '${ $.sizesConfig.name }:options', + pageSize: '${ $.sizesConfig.name }:value' + }, + statefull: { pageSize: true, current: true @@ -231,10 +234,10 @@ define([ * previous and current page size values. */ updateCursor: function () { - var cursor = this.current - 1, - size = this.pageSize, + var cursor = this.current - 1, + size = this.pageSize, oldSize = _.isUndefined(this.previousSize) ? this.pageSize : this.previousSize, - delta = cursor * (oldSize - size) / size; + delta = cursor * (oldSize - size) / size; delta = size > oldSize ? Math.ceil(delta) : diff --git a/app/code/Magento/Ui/view/base/web/js/grid/paging/sizes.js b/app/code/Magento/Ui/view/base/web/js/grid/paging/sizes.js index 4941eabbc4ee6..3af5c196173b8 100644 --- a/app/code/Magento/Ui/view/base/web/js/grid/paging/sizes.js +++ b/app/code/Magento/Ui/view/base/web/js/grid/paging/sizes.js @@ -17,31 +17,8 @@ define([ return Element.extend({ defaults: { template: 'ui/grid/paging/sizes', - value: 20, minSize: 1, maxSize: 999, - options: { - '20': { - value: 20, - label: 20 - }, - '30': { - value: 30, - label: 30 - }, - '50': { - value: 50, - label: 50 - }, - '100': { - value: 100, - label: 100 - }, - '200': { - value: 200, - label: 200 - } - }, statefull: { options: true, value: true diff --git a/lib/internal/Magento/Framework/View/Layout/Generator/UiComponent.php b/lib/internal/Magento/Framework/View/Layout/Generator/UiComponent.php index 1a336f2e398c0..8a6ea85a9b7ef 100644 --- a/lib/internal/Magento/Framework/View/Layout/Generator/UiComponent.php +++ b/lib/internal/Magento/Framework/View/Layout/Generator/UiComponent.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\View\Layout\Generator; use Magento\Framework\View\Element\BlockFactory; @@ -65,7 +66,7 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritdoc */ public function getType() { @@ -90,8 +91,6 @@ public function process(ReaderContext $readerContext, GeneratorContext $generato $layout = $generatorContext->getLayout(); // Instantiate blocks and collect all actions data - /** @var $blocks \Magento\Framework\View\Element\AbstractBlock[] */ - $blocks = []; foreach ($scheduledElements as $elementName => $element) { list($elementType, $data) = $element; @@ -99,9 +98,10 @@ public function process(ReaderContext $readerContext, GeneratorContext $generato continue; } - $block = $this->generateComponent($structure, $elementName, $data, $layout); - $blocks[$elementName] = $block; - $layout->setBlock($elementName, $block); + $layout->setBlock( + $elementName, + $this->generateComponent($structure, $elementName, $data, $layout) + ); $scheduledStructure->unsetElement($elementName); }