Skip to content

Commit

Permalink
Events: Added parent parameter to rename-node event.
Browse files Browse the repository at this point in the history
  • Loading branch information
rathouz committed Aug 14, 2017
1 parent a41bfb9 commit bbf0f50
Showing 1 changed file with 51 additions and 50 deletions.
101 changes: 51 additions & 50 deletions src/Events/RenameNodeEvent.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
<?php

/**
* This file is part of the Pehape libraries (http://pehape.cz)
* Copyright (c) 2016 Tomas Rathouz <trathouz at gmail.com>
*/

namespace Pehape\DataTree\Events;

use Nette\Utils\ArrayHash;
use Pehape\DataTree\Components\DataTree;
use Pehape\DataTree\Exceptions;


/**
* RenameNodeEvent.
*
* @author Tomas Rathouz <trathouz at gmail.com>
*/
class RenameNodeEvent extends BaseEvent
{

/** @var array */
protected $parameters = [
'id' => 'data.node.id',
'text' => 'data.node.text',
'type' => 'data.node.type',
];

public function __construct()
{
parent::__construct();
$this->callback = $this->getDefaultCallback();
}

public function getDefaultCallback()
{
return function (DataTree $tree, ArrayHash $parameters) {
try {
$tree->getDataSource()->updateNode($parameters->id, ['name' => $parameters->text]);
} catch (Exceptions\DataSourceException $e) {
$tree->sendErrorResponse([]);
}

$tree->sendSuccessResponse([]);
};
}


}
<?php

/**
* This file is part of the Pehape libraries (http://pehape.cz)
* Copyright (c) 2016 Tomas Rathouz <trathouz at gmail.com>
*/

namespace Pehape\DataTree\Events;

use Nette\Utils\ArrayHash;
use Pehape\DataTree\Components\DataTree;
use Pehape\DataTree\Exceptions;


/**
* RenameNodeEvent.
*
* @author Tomas Rathouz <trathouz at gmail.com>
*/
class RenameNodeEvent extends BaseEvent
{

/** @var array */
protected $parameters = [
'id' => 'data.node.id',
'text' => 'data.node.text',
'type' => 'data.node.type',
'parent' => 'data.node.parent',
];

public function __construct()
{
parent::__construct();
$this->callback = $this->getDefaultCallback();
}

public function getDefaultCallback()
{
return function (DataTree $tree, ArrayHash $parameters) {
try {
$tree->getDataSource()->updateNode($parameters->id, ['name' => $parameters->text]);
} catch (Exceptions\DataSourceException $e) {
$tree->sendErrorResponse([]);
}

$tree->sendSuccessResponse([]);
};
}


}

0 comments on commit bbf0f50

Please sign in to comment.