Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions client-side/dependentSelectBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,35 @@
var signalLink = element.data(dsb.settings.dataLinkName);
var parents = element.data(dsb.settings.dataParentsName);

if (signalLink == undefined) {
if (signalLink === undefined) {
return false;
}

$.each(parents, function (name, id) {
var parentElement = $('#' + id);

if (parentElement.length > 0) {
var val;

if (parentElement.prop('type') === 'checkbox') {
val = parentElement.prop('checked') ? 1 : 0;

} else {
val = $(parentElement).val();
if (!val) {
return;
}
}
signalLink = signalLink + '&' + name + '=' + val;


if (val instanceof Array) {
$.each(val, function (key, value) {
signalLink += '&' + name + '[]=' + value;
});

} else {
signalLink += '&' + name + '=' + val;
}
}
});

Expand Down
247 changes: 124 additions & 123 deletions src/Controls/DependentMultiSelectBox.php
Original file line number Diff line number Diff line change
@@ -1,123 +1,124 @@
<?php

/**
* This file is part of the NasExt extensions of Nette Framework
*
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com)
*
* For the full copyright and license information, please view
* the file license.md that was distributed with this source code.
*/

namespace NasExt\Forms\Controls;

use NasExt;
use Nette;


/**
* @author Jáchym Toušek
* @author Dusan Hudak
* @author Ales Wita
* @license MIT
*/
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver
{
use NasExt\Forms\DependentTrait;

/** @var string */
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME;


/**
* @param string
* @param array[Nette\Forms\IControl]
*/
public function __construct($label, array $parents)
{
$this->parents = $parents;
parent::__construct($label);
}


/**
* @throws
* @param bool
* @return self
*/
public function setDisabled($value = true)
{
if (is_array($value)) {
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!');
}
return parent::setDisabled($value);
}


/**
* @param string
* @return void
*/
public function signalReceived($signal)
{
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');

if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
$parentsNames = [];
foreach ($this->parents as $parent) {
$value = $presenter->getParameter($this->getNormalizeName($parent));
$parent->setValue($value);

$parentsNames[$parent->getName()] = $parent->getValue();
}

$data = $this->getDependentData([$parentsNames]);
$presenter->payload->dependentselectbox = [
'id' => $this->getHtmlId(),
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled),
'value' => $data->getValue(),
'prompt' => false,
'disabledWhenEmpty' => $this->disabledWhenEmpty,
];
$presenter->sendPayload();
}
}


/**
* @return void
*/
private function tryLoadItems()
{
if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) {
$parentsValues = [];
foreach ($this->parents as $parent) {
$parentsValues[$parent->getName()] = $parent->getValue();
}

$data = $this->getDependentData([$parentsValues]);
$items = $data->getItems();


if ($this->getForm()->isSubmitted()) {
$this->setValue($this->value);

} elseif ($this->tempValue !== null) {
$this->setValue($this->tempValue);

} else {
$this->setValue($data->getValue());
}


$this->loadHttpData();
$this->setItems($items);

if (count($items) === 0) {
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) {
$this->setDisabled();
}
}
}
}
}
<?php

/**
* This file is part of the NasExt extensions of Nette Framework
*
* Copyright (c) 2013 Dusan Hudak (http://dusan-hudak.com)
*
* For the full copyright and license information, please view
* the file license.md that was distributed with this source code.
*/

namespace NasExt\Forms\Controls;

use NasExt;
use Nette;


/**
* @author Jáchym Toušek
* @author Dusan Hudak
* @author Ales Wita
* @license MIT
*/
class DependentMultiSelectBox extends Nette\Forms\Controls\MultiSelectBox implements Nette\Application\UI\ISignalReceiver
{
use NasExt\Forms\DependentTrait;

/** @var string */
const SIGNAL_NAME = DependentSelectBox::SIGNAL_NAME;


/**
* @param string $label
* @param array<int, Nette\Forms\IControl> $parents
*/
public function __construct($label, array $parents)
{
$this->parents = $parents;
parent::__construct($label);
}


/**
* @throws $value
* @param bool
* @return self
*/
public function setDisabled($value = true)
{
if (is_array($value)) {
throw new Nette\InvalidArgumentException('NasExt\\Forms\\Controls\\DependentMultiSelectBox not supported disabled items!');
}

return parent::setDisabled($value);
}


/**
* @param string $signal
* @return void
*/
public function signalReceived($signal)
{
$presenter = $this->lookup('Nette\\Application\\UI\\Presenter');

if ($presenter->isAjax() && $signal === self::SIGNAL_NAME && !$this->isDisabled()) {
$parentsNames = [];
foreach ($this->parents as $parent) {bdump($presenter->getParameters());
$value = $presenter->getParameter($this->getNormalizeName($parent));
$parent->setValue($value);

$parentsNames[$parent->getName()] = $parent->getValue();
}

$data = $this->getDependentData([$parentsNames]);
$presenter->payload->dependentselectbox = [
'id' => $this->getHtmlId(),
'items' => $data->getPreparedItems(!is_array($this->disabled) ?: $this->disabled),
'value' => $data->getValue(),
'prompt' => false,
'disabledWhenEmpty' => $this->disabledWhenEmpty,
];

$presenter->sendPayload();
}
}


/**
* @return void
*/
private function tryLoadItems()
{
if ($this->parents === array_filter($this->parents, function ($p) {return !$p->hasErrors();})) {
$parentsValues = [];
foreach ($this->parents as $parent) {
$parentsValues[$parent->getName()] = $parent->getValue();
}

$data = $this->getDependentData([$parentsValues]);
$items = $data->getItems();

if ($this->getForm()->isSubmitted()) {
$this->setValue($this->value);

} elseif ($this->tempValue !== null) {
$this->setValue($this->tempValue);

} else {
$this->setValue($data->getValue());
}


$this->loadHttpData();
$this->setItems($items);

if (count($items) === 0) {
if ($this->disabledWhenEmpty === true && !$this->isDisabled()) {
$this->setDisabled();
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/Controls/DependentSelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class DependentSelectBox extends Nette\Forms\Controls\SelectBox implements Nette


/**
* @param string
* @param array[Nette\Forms\IControl]
* @param string $label
* @param array<int, Nette\Forms\IControl> $parents
*/
public function __construct($label, array $parents)
{
Expand All @@ -41,7 +41,7 @@ public function __construct($label, array $parents)


/**
* @param string
* @param string $signal
* @return void
*/
public function signalReceived($signal)
Expand Down
Loading