Skip to content

Commit

Permalink
Merge pull request #236 from magento-tango/MAGETWO-46357
Browse files Browse the repository at this point in the history
[Tango] UI Components improvements
  • Loading branch information
magicbunneh committed Dec 11, 2015
2 parents b5bfc54 + 997962b commit 52fdb4a
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 80 deletions.
111 changes: 109 additions & 2 deletions app/code/Magento/Ui/Component/AbstractComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Ui\Component;

use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponent\DataSourceInterface;
Expand Down Expand Up @@ -85,19 +87,92 @@ public function getName()
*/
public function prepare()
{
if ($this->getData(UiComponentFactory::IMPORT_CHILDREN_FROM_META)) {
$children = (array)$this->getContext()->getDataProvider()->getMeta();
foreach ($children as $name => $childData) {
$this->createChildComponent($name, $childData);
}
}

$jsConfig = $this->getJsConfig($this);
if (isset($jsConfig['provider'])) {
unset($jsConfig['extends']);
$this->getContext()->addComponentDefinition($this->getName(), $jsConfig);
} else {
$this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
}

if ($this->hasData('actions')) {
$this->getContext()->addActions($this->getData('actions'), $this);
}

if ($this->hasData('buttons')) {
$this->getContext()->addButtons($this->getData('buttons'), $this);
}
$this->getContext()->getProcessor()->notify($this->getComponentName());
}

/**
* Create child Ui Component
*
* @param string $name
* @param array $childData
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function createChildComponent($name, array $childData)
{
if (empty($childData)) {
return $this;
}

$childComponent = $this->getComponent($name);
if ($childComponent === null) {
$argument = [
'context' => $this->getContext(),
'data' => [
'name' => $name,
'config' => $childData
]
];

if (!isset($childData['componentType'])) {
throw new LocalizedException(
__('The configuration parameter "componentType" is a required for "%1" component.', $name)
);
}

$childComponent = $this->getContext()
->getUiComponentFactory()
->create($name, $childData['componentType'], $argument);
$this->prepareChildComponent($childComponent);
$this->addComponent($name, $childComponent);
} else {
$this->updateComponent($childData, $childComponent);
}

return $this;
}

/**
* Call prepare method in the component UI
*
* @param UiComponentInterface $component
* @return $this
*/
protected function prepareChildComponent(UiComponentInterface $component)
{
$childComponents = $component->getChildComponents();
if (!empty($childComponents)) {
foreach ($childComponents as $child) {
$this->prepareChildComponent($child);
}
}
$component->prepare();

return $this;
}

/**
* Produce and return block's html output
*
Expand Down Expand Up @@ -182,7 +257,7 @@ public function getTemplate()
*/
public function getConfiguration()
{
return (array) $this->getData('config');
return (array)$this->getData('config');
}

/**
Expand All @@ -194,7 +269,7 @@ public function getConfiguration()
*/
public function getJsConfig(UiComponentInterface $component)
{
$jsConfig = (array) $component->getData('js_config');
$jsConfig = (array)$component->getData('js_config');
if (!isset($jsConfig['extends'])) {
$jsConfig['extends'] = $component->getContext()->getNamespace();
}
Expand Down Expand Up @@ -264,4 +339,36 @@ protected function initObservers(array & $data = [])
}
}
}

/**
* Update component data
*
* @param array $componentData
* @param UiComponentInterface $component
* @return $this
*/
protected function updateComponent(array $componentData, UiComponentInterface $component)
{
$config = $component->getData('config');
// XML data configuration override configuration coming from the DB
$config = array_replace_recursive($componentData, $config);
$component->setData('config', $config);

return $this;
}

/**
* Update DataScope
*
* @param array $data
* @param string $name
* @return array
*/
protected function updateDataScope(array $data, $name)
{
if (!isset($data['dataScope'])) {
$data['dataScope'] = $name;
}
return $data;
}
}
7 changes: 6 additions & 1 deletion app/code/Magento/Ui/Component/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public function prepare()
(array) $this->getData('config')
)
);
$this->wrappedComponent->prepare();

foreach ($this->components as $nameComponent => $component) {
$this->wrappedComponent->addComponent($nameComponent, $component);
}
$this->prepareChildComponent($this->wrappedComponent);

$this->components = $this->wrappedComponent->getChildComponents();
// Merge JS configuration with wrapped component configuration
$wrappedComponentConfig = $this->getJsConfig($this->wrappedComponent);
Expand Down
34 changes: 6 additions & 28 deletions app/code/Magento/Ui/Component/Layout/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Element\UiComponent\LayoutInterface;
use Magento\Framework\View\Element\UiComponent\BlockWrapperInterface;

/**
* Class Tabs
Expand All @@ -22,16 +23,6 @@ class Tabs extends \Magento\Framework\View\Layout\Generic implements LayoutInter
*/
protected $navContainerName;

/**
* @var UiComponentInterface
*/
protected $component;

/**
* @var string
*/
protected $namespace;

/**
* @var array
*/
Expand All @@ -42,21 +33,17 @@ class Tabs extends \Magento\Framework\View\Layout\Generic implements LayoutInter
*/
protected $sortIncrement = 10;

/**
* @var UiComponentFactory
*/
protected $uiComponentFactory;

/**
* Constructor
*
* @param UiComponentFactory $uiComponentFactory
* @param null|string $navContainerName
* @param array $data
*/
public function __construct(UiComponentFactory $uiComponentFactory, $navContainerName = null)
public function __construct(UiComponentFactory $uiComponentFactory, $navContainerName = null, $data = [])
{
$this->navContainerName = $navContainerName;
$this->uiComponentFactory = $uiComponentFactory;
parent::__construct($uiComponentFactory, $data);
}

/**
Expand All @@ -72,15 +59,6 @@ public function build(UiComponentInterface $component)

$this->addNavigationBlock();

// Register html content element
$this->component->getContext()->addComponentDefinition(
'html_content',
[
'component' => 'Magento_Ui/js/form/components/html',
'extends' => $this->namespace
]
);

// Initialization of structure components
$this->initSections();
$this->initAreas();
Expand Down Expand Up @@ -185,11 +163,11 @@ protected function addChildren(array &$topNode, UiComponentInterface $component,
/**
* Add wrapped layout block
*
* @param \Magento\Ui\Component\Wrapper\Block $childComponent
* @param BlockWrapperInterface $childComponent
* @param array $areas
* @return void
*/
protected function addWrappedBlock(\Magento\Ui\Component\Wrapper\Block $childComponent, array &$areas)
protected function addWrappedBlock(BlockWrapperInterface $childComponent, array &$areas)
{
$name = $childComponent->getName();
/** @var TabInterface $block */
Expand Down
14 changes: 13 additions & 1 deletion app/code/Magento/Ui/Component/Wrapper/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use Magento\Framework\View\Element\BlockInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\AbstractComponent;
use Magento\Framework\View\Element\UiComponent\BlockWrapperInterface;

/**
* Class Block
*/
class Block extends AbstractComponent
class Block extends AbstractComponent implements BlockWrapperInterface
{
const NAME = 'blockWrapper';

Expand Down Expand Up @@ -66,4 +67,15 @@ public function render()
{
return $this->block->toHtml();
}

/**
* {@inheritdoc}
*/
public function getConfiguration()
{
return array_merge(
(array) $this->block->getData('config'),
(array) $this->getData('config')
);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Ui/Test/Unit/Component/Form/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function getWrappedComponentMock()
->with('config', $this->logicalNot($this->isEmpty()));
$wrappedComponentMock->expects($this->once())
->method('prepare');
$wrappedComponentMock->expects($this->once())
$wrappedComponentMock->expects($this->atLeastOnce())
->method('getChildComponents')
->willReturn($this->getComponentsMock());
$wrappedComponentMock->expects($this->any())
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Ui/etc/data_source.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<xs:attribute type="referenceAttributeType" name="name" use="required"/>
<xs:attribute type="textAttributeType" name="dataType" use="optional"/>
<xs:attribute type="textAttributeType" name="formElement" use="optional"/>
<xs:attribute type="textAttributeType" name="componentType" use="optional"/>
<xs:attribute type="textAttributeType" name="displayArea" use="optional"/>
<xs:attribute type="textAttributeType" name="fieldGroup" use="optional"/>
<xs:attribute type="textAttributeType" name="source" use="optional"/>
Expand Down
21 changes: 21 additions & 0 deletions app/code/Magento/Ui/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\View\Layout\Generic">
<arguments>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/components/html</item>
<item name="componentName" xsi:type="string">html_content</item>
<item name="panelComponentName" xsi:type="string">fieldset</item>
</item>
</argument>
</arguments>
</type>
<type name="Magento\Ui\Component\Layout\Tabs">
<arguments>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/components/html</item>
<item name="componentName" xsi:type="string">html_content</item>
</item>
</argument>
</arguments>
</type>
<virtualType name="arrayArgumentInterpreterProxy" type="Magento\Framework\Data\Argument\InterpreterInterface\Proxy">
<arguments>
<argument name="instanceName" xsi:type="string">Magento\Framework\Data\Argument\Interpreter\ArrayType</argument>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Element\UiComponent;

use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Framework\View\Element\BlockInterface;

/**
* Interface BlockWrapperInterface
*/
interface BlockWrapperInterface extends UiComponentInterface
{
/**
* Get wrapped block
*
* @return BlockInterface
*/
public function getBlock();
}
Loading

0 comments on commit 52fdb4a

Please sign in to comment.