@maxgraph/core
Version:
maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.
116 lines (112 loc) • 3.73 kB
JavaScript
/*
Copyright 2021-present The maxGraph project Contributors
Copyright (c) 2006-2015, JGraph Ltd
Copyright (c) 2006-2015, Gaudenz Alder
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Implements a mechanism for temporary cell Ids.
* @class CellPath
*/
class CellPath {
constructor() {
throw new Error("Static class can't be instantiated!");
}
/**
* Creates the cell path for the given cell. The cell path is a
* concatenation of the indices of all ancestors on the (finite) path to
* the root, eg. "0.0.0.1".
*
* @param cell Cell whose path should be returned.
*/
static create(cell) {
let result = '';
let parent = cell.getParent();
while (parent) {
const index = parent.getIndex(cell);
result = index + CellPath.PATH_SEPARATOR + result;
cell = parent;
parent = cell.getParent();
}
// Remove trailing separator
const n = result.length;
if (n > 1) {
result = result.substring(0, n - 1);
}
return result;
}
/**
* Returns the path for the parent of the cell represented by the given
* path. Returns null if the given path has no parent.
*
* @param path Path whose parent path should be returned.
*/
static getParentPath(path) {
const index = path.lastIndexOf(CellPath.PATH_SEPARATOR);
if (index >= 0) {
return path.substring(0, index);
}
if (path.length > 0) {
return '';
}
return null;
}
/**
* Returns the cell for the specified cell path using the given root as the
* root of the path.
*
* @param root Root cell of the path to be resolved.
* @param path String that defines the path.
*/
static resolve(root, path) {
let parent = root;
const tokens = path.split(CellPath.PATH_SEPARATOR);
for (let i = 0; i < tokens.length; i += 1) {
parent = parent.getChildAt(parseInt(tokens[i]));
}
return parent;
}
/**
* Compares the given cell paths and returns -1 if p1 is smaller, 0 if
* p1 is equal and 1 if p1 is greater than p2.
*/
static compare(p1, p2) {
const min = Math.min(p1.length, p2.length);
let comp = 0;
for (let i = 0; i < min; i += 1) {
if (p1[i] !== p2[i]) {
if (p1[i].length === 0 || p2[i].length === 0) {
comp = p1[i] === p2[i] ? 0 : p1[i] > p2[i] ? 1 : -1;
}
else {
const t1 = parseInt(p1[i]);
const t2 = parseInt(p2[i]);
comp = t1 === t2 ? 0 : t1 > t2 ? 1 : -1;
}
break;
}
}
// Compares path length if both paths are equal to this point
if (comp === 0) {
const t1 = p1.length;
const t2 = p2.length;
if (t1 !== t2) {
comp = t1 > t2 ? 1 : -1;
}
}
return comp;
}
}
/**
* Defines the separator between the path components. Default is ".".
*/
CellPath.PATH_SEPARATOR = '.';
export default CellPath;