BUG Fix jstree when duplicating subtrees

This commit is contained in:
Damian Mooyman 2014-04-14 16:50:51 +12:00
parent bd39dc2d84
commit 0b4f62dfd8
3 changed files with 86 additions and 22 deletions

View File

@ -253,11 +253,19 @@
parentNode = data.ParentID ? self.getNodeByID(data.ParentID) : false,
newNode = $(html);
// Extract the state for the new node from the properties taken from the provided HTML template.
// This will correctly initialise the behaviour of the node for ajax loading of children.
var properties = {data: ''};
if(newNode.hasClass('jstree-open')) {
properties.state = 'open';
} else if(newNode.hasClass('jstree-closed')) {
properties.state = 'closed';
}
this.jstree(
'create_node',
parentNode.length ? parentNode : -1,
'last',
'',
properties,
function(node) {
var origClasses = node.attr('class');
// Copy attributes
@ -265,6 +273,7 @@
var attr = newNode[0].attributes[i];
node.attr(attr.name, attr.value);
}
// Substitute html from request for that generated by jstree
node.addClass(origClasses).html(newNode.html());
callback(node);
}
@ -357,12 +366,9 @@
// a tree "open"/"select" event, while at the same time creating a new node
self.getNodeByID(node.data('id')).not(node).remove();
// Select this node
self.jstree('deselect_all');
self.jstree('select_node', node);
// Similar to jstree's correct_state, but doesn't remove children
var hasChildren = (node.children('ul').length > 0);
node.toggleClass('jstree-leaf', !hasChildren);
if(!hasChildren) node.removeClass('jstree-closed jstree-open');
};
// TODO 'initially_opened' config doesn't apply here
@ -387,7 +393,7 @@
if(node.length) {
self.updateNode(node, nodeData.html, nodeData);
setTimeout(function() {
correctStateFn(node) ;
correctStateFn(node);
}, 500);
} else {
includesNewNode = true;

View File

@ -275,12 +275,12 @@ class Hierarchy extends DataExtension {
if($children) {
foreach($children as $child) {
$markingMatches = $this->markingFilterMatches($child);
if($markingMatches) {
// Filtered results should always show opened, since actual matches
// might be hidden by non-matching parent nodes.
if($this->markingFilter && $markingMatches) {
if($this->markingFilter) {
$child->markOpened();
}
if(!$this->markingFilter || $markingMatches) {
if($child->$numChildrenMethod()) {
$child->markUnexpanded();
} else {
@ -310,18 +310,24 @@ class Hierarchy extends DataExtension {
}
/**
* Return CSS classes of 'unexpanded', 'closed', both, or neither, depending on
* the marking of this DataObject.
* Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a
* 'jstree-*' state depending on the marking of this DataObject.
*
* @return string
*/
public function markingClasses() {
$classes = '';
if(!$this->isExpanded()) {
$classes .= " unexpanded jstree-closed";
$classes .= " unexpanded";
}
if($this->isTreeOpened()) {
if($this->numChildren() > 0) $classes .= " jstree-open";
// Set jstree open state, or mark it as a leaf (closed) if there are no children
if(!$this->numChildren()) {
$classes .= " jstree-leaf closed";
} elseif($this->isTreeOpened()) {
$classes .= " jstree-open";
} else {
$classes .= " closed";
$classes .= " jstree-closed closed";
}
return $classes;
}

View File

@ -180,13 +180,65 @@ class CmsUiContext extends BehatContext {
}
/**
* @When /^I click on "([^"]*)" in the tree$/
* Applies a specific action to an element
*
* @param NodeElement $element Element to act on
* @param string $action Action, which may be one of 'hover', 'double click', 'right click', or 'left click'
* The default 'click' behaves the same as left click
*/
public function stepIClickOnElementInTheTree($text) {
protected function interactWithElement($element, $action = 'click') {
switch($action) {
case 'hover':
$element->mouseOver();
break;
case 'double click':
$element->doubleClick();
break;
case 'right click':
$element->rightClick();
break;
case 'left click':
case 'click':
default:
$element->click();
break;
}
}
/**
* @When /^I (?P<method>(?:(?:double |right |left |)click)|hover) on "(?P<link>[^"]*)" in the context menu/
*/
public function stepIClickOnElementInTheContextMenu($method, $link) {
$context = $this->getMainContext();
// Wait until context menu has appeared
$this->getSession()->wait(
1000,
"window.jQuery && window.jQuery('.jstree-apple-context').size() > 0"
);
$regionObj = $context->getRegionObj('.jstree-apple-context');
assertNotNull($regionObj, "Context menu could not be found");
$linkObj = $regionObj->findLink($link);
if (empty($linkObj)) {
throw new \Exception(sprintf(
'The link "%s" was not found in the context menu on the page %s',
$link,
$this->getSession()->getCurrentUrl()
));
}
$this->interactWithElement($linkObj, $method);
}
/**
* @When /^I (?P<method>(?:(?:double |right |left |)click)|hover) on "(?P<text>[^"]*)" in the tree$/
*/
public function stepIClickOnElementInTheTree($method, $text) {
$treeEl = $this->getCmsTreeElement();
$treeNode = $treeEl->findLink($text);
assertNotNull($treeNode, sprintf('%s not found', $text));
$treeNode->click();
$this->interactWithElement($treeNode, $method);
}
/**