@wcardinal/wcardinal-geditor
Version:
WebGL-based graphic editor, tester and viewer for supervisory systems
199 lines • 6.45 kB
JavaScript
import { EShapeBase } from "@wcardinal/wcardinal-ui";
/**
* {@link EShape} search utility.
*/
var UtilShapeSearch = /** @class */ (function () {
function UtilShapeSearch() {
}
/**
* Returns indices of the given shapes.
*
* @param shapes shapes
* @return indices
*/
UtilShapeSearch.toIndices = function (shapes) {
var result = [];
for (var i = 0, imax = shapes.length; i < imax; ++i) {
result.push(shapes[i].index);
}
return result;
};
/**
* Returns a depth of the given shape.
*
* @param shape a shape
* @return a depth
*/
UtilShapeSearch.toDepth = function (shape) {
var result = 0;
var parent = shape.parent;
while (parent instanceof EShapeBase) {
result += 1;
parent = parent.parent;
}
return result;
};
/**
* Returns a deepest shape on the path to the given shapes.
*
* @param shapeA a shape
* @param shapeB a shape
* @return a found shape
*/
UtilShapeSearch.toSharedParent = function (shapeA, shapeB) {
var depthA = this.toDepth(shapeA);
var depthB = this.toDepth(shapeB);
if (depthA < depthB) {
var parent_1 = shapeA.parent;
while (parent_1 instanceof EShapeBase) {
if (this.isParent(shapeB, parent_1)) {
return parent_1;
}
parent_1 = parent_1.parent;
}
return parent_1;
}
else {
var parent_2 = shapeB.parent;
while (parent_2 instanceof EShapeBase) {
if (this.isParent(shapeA, parent_2)) {
return parent_2;
}
parent_2 = parent_2.parent;
}
return parent_2;
}
};
/**
* Returns a shape on the path to the given shape whose parent is equals to the given parent.
* If there is no such shape, returns a root shape on the path.
*
* @param shape a shape
* @param parent a parent
* @returns a found shape
*/
UtilShapeSearch.toOfParent = function (shape, parent) {
var shapeParent = shape.parent;
while (shapeParent !== parent && shapeParent instanceof EShapeBase) {
shape = shapeParent;
shapeParent = shapeParent.parent;
}
return shape;
};
/**
* Returns true if the given target is on the path to the given shape.
*
* @param shape a shape
* @param target a check target
* @return true if the given target is on the path to the given shape
*/
UtilShapeSearch.isParent = function (shape, target) {
var parent = shape.parent;
while (parent instanceof EShapeBase) {
if (parent === target) {
return true;
}
parent = parent.parent;
}
return false;
};
/**
* Returns a selected shape on the path to the given shape.
* If there are more than one selected shapes, returns a deepest selected shape.
*
* @param shape a shape
* @return a found selected shape or null
*/
UtilShapeSearch.toSelected = function (shape) {
var target = shape;
while (target instanceof EShapeBase) {
if (target.selected) {
return target;
}
target = target.parent;
}
return null;
};
UtilShapeSearch.findChildById = function (shape, id, recursively) {
var children = shape.children;
for (var i = 0, imax = children.length; i < imax; ++i) {
var child = children[i];
if (child.id === id) {
return child;
}
if (recursively === true) {
var result = UtilShapeSearch.findChildById(child, id, recursively);
if (result != null) {
return result;
}
}
}
return null;
};
UtilShapeSearch.findChildByType = function (shape, type, recursively) {
var children = shape.children;
for (var i = 0, imax = children.length; i < imax; ++i) {
var child = children[i];
if (child.type === type) {
return child;
}
if (recursively === true) {
var result = UtilShapeSearch.findChildByType(child, type, recursively);
if (result != null) {
return result;
}
}
}
return null;
};
UtilShapeSearch.findChild = function (shape, matcher, recursively) {
var children = shape.children;
for (var i = 0, imax = children.length; i < imax; ++i) {
var child = children[i];
if (matcher(child)) {
return child;
}
if (recursively === true) {
var result = UtilShapeSearch.findChild(child, matcher, recursively);
if (result != null) {
return result;
}
}
}
return null;
};
UtilShapeSearch.findChildrenByType = function (shape, type, recursively, result) {
result = result || [];
var children = shape.children;
for (var i = 0, imax = children.length; i < imax; ++i) {
var child = children[i];
if (child.type === type) {
result.push(child);
}
if (recursively === true) {
UtilShapeSearch.findChildrenByType(child, type, recursively, result);
}
}
return result;
};
UtilShapeSearch.findChildren = function (shape, matcher, recursively, result) {
result = result || [];
var children = shape.children;
for (var i = 0, imax = children.length; i < imax; ++i) {
var child = children[i];
if (matcher(child)) {
result.push(child);
}
if (recursively === true) {
UtilShapeSearch.findChildren(child, matcher, recursively, result);
}
}
return result;
};
UtilShapeSearch.COMPARATOR_INDEX = function (a, b) {
return a.index - b.index;
};
return UtilShapeSearch;
}());
export { UtilShapeSearch };
//# sourceMappingURL=util-shape-search.js.map