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

425 lines
9.0 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.
*/
const INodeModel = new Class(
2021-10-05 02:05:34 +02:00
/** @lends INodeModel */ {
/**
2021-07-16 16:41:58 +02:00
* @constructs
* @param mindmap
*/
2021-10-05 02:05:34 +02:00
initialize(mindmap) {
$assert(mindmap && mindmap.getBranches, 'mindmap can not be null');
this._mindmap = mindmap;
},
/** */
getId() {
return this.getProperty('id');
},
/** */
setId(id) {
if ($defined(id) && id > INodeModel._uuid) {
INodeModel._uuid = id;
}
if (!$defined(id)) {
id = INodeModel._nextUUID();
}
this.putProperty('id', id);
},
/** */
getType() {
return this.getProperty('type');
},
/** */
setType(type) {
this.putProperty('type', type);
},
/** */
setText(text) {
this.putProperty('text', text);
},
/** */
getText() {
return this.getProperty('text');
},
/** */
setPosition(x, y) {
$assert(!isNaN(parseInt(x)), `x position is not valid:${x}`);
$assert(!isNaN(parseInt(y)), `y position is not valid:${y}`);
this.putProperty('position', `{x:${parseInt(x)},y:${parseInt(y)}}`);
},
/** */
getPosition() {
const value = this.getProperty('position');
let result = null;
if (value != null) {
result = eval(`(${value})`);
}
return result;
},
/** */
setImageSize(width, height) {
this.putProperty('imageSize', `{width:${width},height:${height}}`);
},
/** */
getImageSize() {
const value = this.getProperty('imageSize');
let result = null;
if (value != null) {
result = eval(`(${value})`);
}
return result;
},
/** */
setImageUrl(url) {
this.putProperty('imageUrl', url);
},
/** */
getMetadata() {
return this.getProperty('metadata');
},
/** */
setMetadata(json) {
this.putProperty('metadata', json);
},
/** */
getImageUrl() {
return this.getProperty('imageUrl');
},
/** */
getMindmap() {
return this._mindmap;
},
/**
2021-07-16 16:41:58 +02:00
* lets the mindmap handle the disconnect node operation
* @see mindplot.model.IMindmap.disconnect
*/
2021-10-05 02:05:34 +02:00
disconnect() {
const mindmap = this.getMindmap();
mindmap.disconnect(this);
},
/** */
getShapeType() {
return this.getProperty('shapeType');
},
/** */
setShapeType(type) {
this.putProperty('shapeType', type);
},
/** */
setOrder(value) {
$assert(
(typeof value === 'number' && isFinite(value)) || value == null,
'Order must be null or a number',
);
this.putProperty('order', value);
},
/** */
getOrder() {
return this.getProperty('order');
},
/** */
setFontFamily(fontFamily) {
this.putProperty('fontFamily', fontFamily);
},
/** */
getFontFamily() {
return this.getProperty('fontFamily');
},
/** */
setFontStyle(fontStyle) {
this.putProperty('fontStyle', fontStyle);
},
/** */
getFontStyle() {
return this.getProperty('fontStyle');
},
/** */
setFontWeight(weight) {
this.putProperty('fontWeight', weight);
},
/** */
getFontWeight() {
return this.getProperty('fontWeight');
},
/** */
setFontColor(color) {
this.putProperty('fontColor', color);
},
/** */
getFontColor() {
return this.getProperty('fontColor');
},
/** */
setFontSize(size) {
this.putProperty('fontSize', size);
},
/** */
getFontSize() {
return this.getProperty('fontSize');
},
/** */
getBorderColor() {
return this.getProperty('borderColor');
},
/** */
setBorderColor(color) {
this.putProperty('borderColor', color);
},
/** */
getBackgroundColor() {
return this.getProperty('backgroundColor');
},
/** */
setBackgroundColor(color) {
this.putProperty('backgroundColor', color);
},
/** */
areChildrenShrunken() {
const result = this.getProperty('shrunken');
return $defined(result) ? result : false;
},
/**
2021-07-16 16:41:58 +02:00
* @return {Boolean} true if the children nodes are hidden by the shrink option
*/
2021-10-05 02:05:34 +02:00
setChildrenShrunken(value) {
this.putProperty('shrunken', value);
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @return {Boolean} true
*/
2021-10-05 02:05:34 +02:00
isNodeModel() {
return true;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @return {Boolean} true if the node model has a parent assigned to it
*/
2021-10-05 02:05:34 +02:00
isConnected() {
return this.getParent() != null;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/** @abstract */
append(node) {
throw 'Unsupported operation';
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* lets the mindmap handle the connect node operation
* @throws will throw an error if parent is null or undefined
* @see mindplot.model.IMindmap.connect
*/
2021-10-05 02:05:34 +02:00
connectTo(parent) {
$assert(parent, 'parent can not be null');
const mindmap = this.getMindmap();
mindmap.connect(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 target
* @return target
*/
2021-10-05 02:05:34 +02:00
copyTo(target) {
const source = this;
// Copy properties ...
const keys = source.getPropertiesKeys();
_.each(keys, (key) => {
const value = source.getProperty(key);
target.putProperty(key, value);
});
// Copy children ...
const children = this.getChildren();
const tmindmap = target.getMindmap();
_.each((children, snode) => {
const tnode = tmindmap.createNode(snode.getType(), snode.getId());
snode.copyTo(tnode);
target.append(tnode);
});
return target;
},
/**
2021-07-16 16:41:58 +02:00
* lets parent handle the delete node operation, or, if none defined, calls the mindmap to
* remove the respective branch
*/
2021-10-05 02:05:34 +02:00
deleteNode() {
const mindmap = this.getMindmap();
// console.log("Before:" + mindmap.inspect());
const parent = this.getParent();
if ($defined(parent)) {
parent.removeChild(this);
} else {
// If it has not parent, it must be an isolate topic ...
mindmap.removeBranch(this);
}
// It's an isolated node. It must be a hole branch ...
// console.log("After:" + mindmap.inspect());
},
/** @abstract */
getPropertiesKeys() {
throw 'Unsupported operation';
},
/** @abstract */
putProperty(key, value) {
throw 'Unsupported operation';
},
/** @abstract */
setParent(parent) {
throw 'Unsupported operation';
},
/** @abstract */
getChildren() {
throw 'Unsupported operation';
},
/** @abstract */
getParent() {
throw 'Unsupported operation';
},
/** @abstract */
clone() {
throw 'Unsupported operation';
},
/** */
inspect() {
let result = `{ type: ${
this.getType()
} , id: ${
this.getId()
} , text: ${
this.getText()}`;
const children = this.getChildren();
if (children.length > 0) {
result = `${result}, children: {(size:${children.length}`;
_.each(children, (node) => {
result = `${result}=> (`;
const keys = node.getPropertiesKeys();
_.each(keys, (key) => {
const value = node.getProperty(key);
result = `${result + key}:${value},`;
});
result = `${result}}`;
});
}
result = `${result} }`;
return result;
},
/** @abstract */
removeChild(child) {
throw 'Unsupported operation';
},
},
2021-07-16 16:41:58 +02:00
);
/**
* @enum {String}
*/
const TopicShape = {
2021-10-05 02:05:34 +02:00
RECTANGLE: 'rectagle',
ROUNDED_RECT: 'rounded rectagle',
ELLIPSE: 'elipse',
LINE: 'line',
IMAGE: 'image',
2021-07-16 16:41:58 +02:00
};
/**
* @constant
* @type {String}
* @default
*/
INodeModel.CENTRAL_TOPIC_TYPE = 'CentralTopic';
/**
* @constant
* @type {String}
* @default
*/
INodeModel.MAIN_TOPIC_TYPE = 'MainTopic';
/**
* @constant
* @type {Number}
* @default
*/
INodeModel.MAIN_TOPIC_TO_MAIN_TOPIC_DISTANCE = 220;
/**
* @todo: This method must be implemented. (unascribed)
*/
INodeModel._nextUUID = function () {
2021-10-05 02:05:34 +02:00
if (!$defined(INodeModel._uuid)) {
INodeModel._uuid = 0;
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
INodeModel._uuid += 1;
return INodeModel._uuid;
2021-07-16 16:41:58 +02:00
};
INodeModel._uuid = 0;
export { TopicShape };
2021-09-02 18:32:23 +02:00
export default INodeModel;