Skip to content

Commit

Permalink
Implemented siblingOf in live preview #21
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamminf committed Jun 5, 2016
1 parent 311439a commit 59482be
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion neo/models/Neo_CriteriaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ private function _runCriteria()
}
}

private function _getBlock($block)
{
if(is_int($block))
{
$block = craft()->neo->getBlockById($block);
}

if($block instanceof Neo_BlockModel)
{
return $block;
}

return false;
}

private function _indexOfBlock($elements, Neo_BlockModel $block)
{
foreach($elements as $i => $element)
{
if($element->id === $block->id)
{
return $i;
}
}

return -1;
}

/*
* Criteria methods
*/
Expand Down Expand Up @@ -307,6 +335,7 @@ protected function __offset($elements, $value)
return array_slice($elements, $value);
}

// Finds all blocks after the root node of the tree
protected function __positionedAfter($elements, $value)
{
if(!$value)
Expand Down Expand Up @@ -359,12 +388,50 @@ protected function __search($elements, $value)

protected function __siblingOf($elements, $value)
{
$value = $this->_getBlock($value);

if(!$value)
{
return $elements;
}

return []; // TODO
$newElements = [];
$mid = $this->_indexOfBlock($elements, $value);
$total = count($elements);

// Previous siblings
for($i = $mid - 1; $i >= 0; $i--)
{
$element = $elements[$i];

if($element->level < $value->level)
{
break;
}

if($element->level == $value->level)
{
array_unshift($newElements, $element);
}
}

// Next siblings
for($i = $mid + 1; $i < $total; $i++)
{
$element = $elements[$i];

if($element->level < $value->level)
{
break;
}

if($element->level == $value->level)
{
$newElements[] = $element;
}
}

return $newElements;
}

protected function __status($elements, $value)
Expand Down

0 comments on commit 59482be

Please sign in to comment.