Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectArrayGently plugin #658

Merged
merged 8 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/ngrest/plugins/SelectArrayGently.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace luya\admin\ngrest\plugins;

use luya\admin\helpers\Angular;
use luya\admin\traits\LazyDataLoadTrait;
use luya\helpers\ArrayHelper;

/**
* Nondestructive analogue of SelectArray plugin.
*
* Create a selection based on an assoc array provided via $data attribute.
* Will NOT override the default values from the database.
*
*
* Example usage:
*
* ```php
* public function ngRestAttributeTypes()
* {
* 'genres' => ['selectArrayGently', 'data' => [1 => 'Male', 2 => 'Female']],
* }
* ```
* Or use a closure for lazy data load:
*
* ```php
* public function ngRestAttributeTypes()
* {
* return [
* 'genres' => ['selectArrayGently', 'data' => function () {
* return new Query()->all();
* }],
* ];
* }
* ```
*
* @property array $data Setter/Getter for the dropdown values.
*
* @author Anton Ikonnikov <antikon2@yandex.ru>
* @since 4.1.0
*/
class SelectArrayGently extends Select
{
use LazyDataLoadTrait;

private $_data;


Antikon marked this conversation as resolved.
Show resolved Hide resolved
public $assignAfterFind = false;

/**
* @inheritdoc
*/
public function renderList($id, $ngModel)
{
if ($this->scheduling && $this->renderContext->canUpdate()) {
return $this->createSchedulerListTag($ngModel, $this->getData(), 'item');
}

$options = Angular::optionsFilter([
'options' => $this->getServiceName('selectdata'),
Antikon marked this conversation as resolved.
Show resolved Hide resolved
]);

return $this->createTag('select-array-gently', null, ArrayHelper::merge(['model' => $ngModel], $options));
}

/**
* Setter method for Data.
*
* @param array $data
*/
public function setData(array $data)
{
$this->_data = $data;
}

/**
*
* {@inheritDoc}
* @see \luya\admin\ngrest\plugins\Select::getData()
*/
public function getData()
{
$cleandata = [];

foreach ($this->lazyLoadData($this->_data) as $key => $value) {
$cleandata[] = [
'value' => $key,
'label' => $value,
];
}

return ArrayHelper::typeCast($cleandata);
}
}
2 changes: 1 addition & 1 deletion src/resources/dist/main.js

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/resources/js/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -3925,3 +3925,44 @@ zaa.directive('pagination', function () {
`,
};
});

/**
* Supporting directive to SelectArrayGently plugin
*
*/
zaa.directive('selectArrayGently', function () {
return {
restrict: 'E',
scope: {
'model': '=',
'options': '=',
'optionsvalue': '@optionsvalue',
'optionslabel': '@optionslabel',
},
controller: ['$rootScope', '$scope', function ($rootScope, $scope) {
if ($scope.optionsvalue === undefined) {
$scope.optionsvalue = 'value';
}
if ($scope.optionslabel === undefined) {
$scope.optionslabel = 'label';
}

$scope.getSelectedLabel = function () {
// Keep raw value by default
var selectedLabel = $scope.model;
angular.forEach($scope.options, function (item) {
if ($scope.model === item[$scope.optionsvalue]) {
selectedLabel = item[$scope.optionslabel];
}
});

return selectedLabel;
};
}],


template: function () {
return '<span>{{getSelectedLabel()}}</span>';
}
};
});
36 changes: 36 additions & 0 deletions tests/admin/ngrest/plugins/SelectArrayGentlyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace admintests\admin\ngrest\plugins;

use admintests\AdminTestCase;
use luya\admin\ngrest\plugins\SelectArrayGently;

class SelectArrayGentlyTest extends AdminTestCase
{
public function testBasicMethods()
{
$plugin = new SelectArrayGently([
'name' => 'testName',
'alias' => 'test',
'i18n' => false,
'data' => [
1 => 'Mr.',
2 => 'Mrs.',
3 => 'Dr.'
],
]);

$this->assertSame(
'<select-array-gently model="someModel" options="service.testName.selectdata"></select-array-gently>',
$plugin->renderList(1, 'someModel')
);

$this->assertSame([
0 => ['value' => 1, 'label' => 'Mr.'],
1 => ['value' => 2, 'label' => 'Mrs.'],
2 => ['value' => 3, 'label' => 'Dr.'],
], $plugin->getData());

unset($plugin);
}
}