wisemapping-frontend/packages/mindplot/lib/components/model/NodeModel.js

239 lines
6.2 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
* Copyright [2015] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* 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.
*/
2021-10-05 02:05:34 +02:00
const INodeModel = require('./INodeModel').default;
const TopicFeature = require('../TopicFeature').default;
2021-07-16 16:41:58 +02:00
const NodeModel = new Class(/** @lends NodeModel */{
2021-10-05 02:05:34 +02:00
Extends: INodeModel,
/**
2021-07-16 16:41:58 +02:00
* @constructs
* @param {String} type node type
* @param mindmap
* @param id
*/
2021-10-05 02:05:34 +02:00
initialize(type, mindmap, id) {
$assert(type, 'Node type can not be null');
$assert(mindmap, 'mindmap can not be null');
this._properties = {};
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this.parent(mindmap);
this.setId(id);
this.setType(type);
this.areChildrenShrunken(false);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
this._children = [];
this._feature = [];
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param type
* @param attributes
* @return {mindplot.model.FeatureModel} the created feature model
*/
2021-10-05 02:05:34 +02:00
createFeature(type, attributes) {
return TopicFeature.createModel(type, attributes);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param feature
* @throws will throw an error if feature is null or undefined
*/
2021-10-05 02:05:34 +02:00
addFeature(feature) {
$assert(feature, 'feature can not be null');
this._feature.push(feature);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/** */
getFeatures() {
return this._feature;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param feature
* @throws will throw an error if feature is null or undefined
* @throws will throw an error if the feature could not be removed
*/
2021-10-05 02:05:34 +02:00
removeFeature(feature) {
$assert(feature, 'feature can not be null');
const size = this._feature.length;
this._feature = this._feature.filter((f) => feature.getId() != f.getId());
$assert(size - 1 == this._feature.length, 'Could not be removed ...');
},
/**
2021-07-16 16:41:58 +02:00
* @param {String} type the feature type, e.g. icon or link
* @throws will throw an error if type is null or undefined
*/
2021-10-05 02:05:34 +02:00
findFeatureByType(type) {
$assert(type, 'type can not be null');
return this._feature.filter((feature) => feature.getType() == type);
},
/**
2021-07-16 16:41:58 +02:00
* @param {String} id
* @throws will throw an error if id is null or undefined
* @throws will throw an error if feature could not be found
* @return the feature with the given id
*/
2021-10-05 02:05:34 +02:00
findFeatureById(id) {
$assert($defined(id), 'id can not be null');
const result = this._feature.filter((feature) => feature.getId() == id);
$assert(result.length == 1, `Feature could not be found:${id}`);
return result[0];
},
/** */
getPropertiesKeys() {
return Object.keys(this._properties);
},
/**
2021-07-16 16:41:58 +02:00
* @param key
* @param value
* @throws will throw an error if key is null or undefined
*/
2021-10-05 02:05:34 +02:00
putProperty(key, value) {
$defined(key, 'key can not be null');
this._properties[key] = value;
},
/** */
getProperties() {
return this._properties;
},
/** */
getProperty(key) {
$defined(key, 'key can not be null');
const result = this._properties[key];
return !$defined(result) ? null : result;
},
/**
2021-07-16 16:41:58 +02:00
* @return {mindplot.model.NodeModel} an identical clone of the NodeModel
*/
2021-10-05 02:05:34 +02:00
clone() {
const result = new NodeModel(this.getType(), this._mindmap);
result._children = this._children.map((node) => {
const cnode = node.clone();
cnode._parent = result;
return cnode;
});
result._properties = Object.clone(this._properties);
result._feature = this._feature.clone();
return result;
},
/**
2021-07-16 16:41:58 +02:00
* Similar to clone, assign new id to the elements ...
* @return {mindplot.model.NodeModel}
*/
2021-10-05 02:05:34 +02:00
deepCopy() {
const result = new NodeModel(this.getType(), this._mindmap);
result._children = this._children.map((node) => {
const cnode = node.deepCopy();
cnode._parent = result;
return cnode;
});
const id = result.getId();
result._properties = Object.clone(this._properties);
result.setId(id);
result._feature = this._feature.clone();
return result;
},
/**
* @param {mindplot.model.NodeModel} child
2021-07-16 16:41:58 +02:00
* @throws will throw an error if child is null, undefined or not a NodeModel object
*/
2021-10-05 02:05:34 +02:00
append(child) {
$assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object');
this._children.push(child);
child._parent = this;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param {mindplot.model.NodeModel} child
* @throws will throw an error if child is null, undefined or not a NodeModel object
*/
2021-10-05 02:05:34 +02:00
removeChild(child) {
$assert(child && child.isNodeModel(), 'Only NodeModel can be appended to Mindmap object.');
this._children.erase(child);
child._parent = null;
},
/** */
getChildren() {
return this._children;
},
/** */
getParent() {
return this._parent;
},
/** */
setParent(parent) {
$assert(parent != this, 'The same node can not be parent and child if itself.');
this._parent = parent;
},
_isChildNode(node) {
let result = false;
if (node == this) {
result = true;
} else {
const children = this.getChildren();
for (let i = 0; i < children.length; i++) {
const child = children[i];
result = child._isChildNode(node);
if (result) {
break;
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
}
}
return result;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @id
* @return {mindplot.model.NodeModel} the node with the respective id
*/
2021-10-05 02:05:34 +02:00
findNodeById(id) {
let result = null;
if (this.getId() == id) {
result = this;
} else {
const children = this.getChildren();
for (let i = 0; i < children.length; i++) {
const child = children[i];
result = child.findNodeById(id);
if (result) {
break;
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
}
2021-07-16 16:41:58 +02:00
}
2021-10-05 02:05:34 +02:00
return result;
},
2021-07-16 16:41:58 +02:00
});
2021-10-05 02:05:34 +02:00
export default NodeModel;