Commit 4b1aeed9 authored by techird's avatar techird

add getCommonAncestor() method

parent dd6d4317
......@@ -291,3 +291,28 @@ var MinderNode = KityMinder.MinderNode = kity.createClass('MinderNode', {
return this;
}
});
MinderNode.getCommonAncestor = function(nodeA, nodeB) {
switch (arguments.length) {
case 1:
return nodeA;
case 2:
if (nodeA.contains(nodeB)) {
return nodeA;
}
if (nodeB.contains(nodeA)) {
return nodeB;
}
var ancestor = nodeA.parent;
while (ancestor && !ancestor.contains(nodeB)) {
ancestor = ancestor.parent;
}
return ancestor;
default:
return Array.prototype.reduce.call(arguments, function(prev, current) {
return MinderNode.getCommonAncestor(prev, current);
}, nodeA);
}
};
\ No newline at end of file
......@@ -172,22 +172,6 @@ var utils = Utils = KityMinder.Utils = {
if (obj.hasOwnProperty(key)) return false;
return true;
},
getNodeCommonAncestor: function(nodeA, nodeB) {
if (nodeA === nodeB) {
return nodeA.parent;
}
if (nodeA.contains(nodeB)) {
return nodeB;
}
if (nodeB.contains(nodeA)) {
return nodeB;
}
var parent = nodeA.parent;
while (!parent.contains(nodeB)) {
parent = parent.parent;
}
return parent;
},
loadFile: function() {
var tmpList = [];
......
......@@ -42,7 +42,7 @@ var RemoveNodeCommand = kity.createClass('RemoverNodeCommand', {
base: Command,
execute: function(km, text) {
var nodes = km.getSelectedNodes();
var ancestor = km.getSelectedAncestors()[0];
var ancestor = MinderNode.getCommonAncestor.apply(null, nodes);
nodes.forEach(function(node) {
km.removeNode(node);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment