@difizen/mana-app
Version:
155 lines (151 loc) • 5.86 kB
JavaScript
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
export var Tree = Symbol('Tree');
/**
* The tree - an abstract data type.
*/
/**
* The tree node.
*/
export var TreeNode;
(function (_TreeNode) {
function is(node) {
return !!node && _typeof(node) === 'object' && 'id' in node && 'parent' in node;
}
_TreeNode.is = is;
function equals(left, right) {
return left === right || !!left && !!right && left.id === right.id;
}
_TreeNode.equals = equals;
function isVisible(node) {
return !!node && (node.visible === undefined || node.visible);
}
_TreeNode.isVisible = isVisible;
})(TreeNode || (TreeNode = {}));
/**
* The composite tree node.
*/
export var CompositeTreeNode;
(function (_CompositeTreeNode) {
function is(node) {
return !!node && 'children' in node;
}
_CompositeTreeNode.is = is;
function getFirstChild(parent) {
return parent.children[0];
}
_CompositeTreeNode.getFirstChild = getFirstChild;
function getLastChild(parent) {
return parent.children[parent.children.length - 1];
}
_CompositeTreeNode.getLastChild = getLastChild;
function isAncestor(parent, child) {
if (!child) {
return false;
}
if (TreeNode.equals(parent, child.parent)) {
return true;
}
return isAncestor(parent, child.parent);
}
_CompositeTreeNode.isAncestor = isAncestor;
function indexOf(parent, node) {
if (!node) {
return -1;
}
return parent.children.findIndex(function (child) {
return TreeNode.equals(node, child);
});
}
_CompositeTreeNode.indexOf = indexOf;
function addChildren(parent, children) {
var _iterator = _createForOfIteratorHelper(children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var child = _step.value;
addChild(parent, child);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return parent;
}
_CompositeTreeNode.addChildren = addChildren;
function addChild(parent, child) {
var children = parent.children;
var index = children.findIndex(function (value) {
return value.id === child.id;
});
if (index !== -1) {
children.splice(index, 1, child);
setParent(child, index, parent);
} else {
children.push(child);
setParent(child, parent.children.length - 1, parent);
}
return parent;
}
_CompositeTreeNode.addChild = addChild;
function removeChild(parent, child) {
var children = parent.children;
var index = children.findIndex(function (value) {
return value.id === child.id;
});
if (index === -1) {
return;
}
children.splice(index, 1);
var previousSibling = child.previousSibling,
nextSibling = child.nextSibling;
if (previousSibling) {
Object.assign(previousSibling, {
nextSibling: nextSibling
});
}
if (nextSibling) {
Object.assign(nextSibling, {
previousSibling: previousSibling
});
}
}
_CompositeTreeNode.removeChild = removeChild;
function setParent(child, index, parent) {
var previousSibling = parent.children[index - 1];
var nextSibling = parent.children[index + 1];
Object.assign(child, {
parent: parent,
previousSibling: previousSibling,
nextSibling: nextSibling
});
if (previousSibling) {
Object.assign(previousSibling, {
nextSibling: child
});
}
if (nextSibling) {
Object.assign(nextSibling, {
previousSibling: child
});
}
}
_CompositeTreeNode.setParent = setParent;
})(CompositeTreeNode || (CompositeTreeNode = {}));
/**
* Representation of a tree node row.
*/
export var TreeNodeComponents = Symbol('TreeNodeComponents');
/**
* Bare minimum common interface of the keyboard and the mouse event with respect to the key maskings.
*/
/**
* The default tree properties.
*/
export var DefaultTreeProps = {
leftPadding: 8,
expansionTogglePadding: 18
};