wisemapping-frontend/packages/web2d/lib/components/Element.js

331 lines
8.6 KiB
JavaScript
Raw Normal View History

2021-10-03 19:55:57 +02:00
/*
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 01:56:40 +02:00
const Element = new Class({ // eslint-disable-line no-undef
initialize(peer, attributes) {
2021-10-05 02:17:02 +02:00
this.peer = peer;
2021-10-05 01:56:40 +02:00
if (peer == null) {
throw new Error('Element peer can not be null');
}
2021-10-06 04:08:42 +02:00
if ($defined(attributes)) {
2021-10-05 01:56:40 +02:00
this._initialize(attributes);
}
},
_initialize(attributes) {
const batchExecute = {};
// Collect arguments ...
2021-10-06 04:08:42 +02:00
for (const key in attributes) {
if (Object.prototype.hasOwnProperty.call(attributes, key)) {
const funcName = this._attributeNameToFuncName(key, 'set');
let funcArgs = batchExecute[funcName];
if (!$defined(funcArgs)) {
funcArgs = [];
}
const signature = Element._propertyNameToSignature[key];
const argPositions = signature[1];
if (argPositions !== Element._SIGNATURE_MULTIPLE_ARGUMENTS) {
funcArgs[argPositions] = attributes[key];
} else {
funcArgs = attributes[key].split(' ');
}
batchExecute[funcName] = funcArgs;
2021-10-05 01:56:40 +02:00
}
}
// Call functions ...
2021-10-06 04:08:42 +02:00
for (const key in batchExecute) {
if (Object.prototype.hasOwnProperty.call(batchExecute, key)) {
const func = this[key];
if (!$defined(func)) {
throw new Error(`Could not find function: ${key}`);
}
func.apply(this, batchExecute[key]);
2021-10-05 01:56:40 +02:00
}
}
},
setSize(width, height) {
2021-10-05 02:17:02 +02:00
this.peer.setSize(width, height);
2021-10-05 01:56:40 +02:00
},
setPosition(cx, cy) {
2021-10-05 02:17:02 +02:00
this.peer.setPosition(cx, cy);
2021-10-05 01:56:40 +02:00
},
/**
2021-07-16 16:41:58 +02:00
* Allows the registration of event listeners on the event target.
* type
* A string representing the event type to listen for.
* listener
2021-10-06 04:08:42 +02:00
* The object that receives a notification when an event of the specified type occurs. This must be an object
* implementing the EventListener interface, or simply a function in JavaScript.
2021-07-16 16:41:58 +02:00
*
* The following events types are supported:
*
*/
2021-10-05 01:56:40 +02:00
addEvent(type, listener) {
2021-10-05 02:17:02 +02:00
this.peer.addEvent(type, listener);
2021-10-05 01:56:40 +02:00
},
trigger(type, event) {
2021-10-05 02:17:02 +02:00
this.peer.trigger(type, event);
2021-10-05 01:56:40 +02:00
},
cloneEvents(from) {
2021-10-05 02:17:02 +02:00
this.peer.cloneEvents(from);
2021-10-05 01:56:40 +02:00
},
/**
2021-07-16 16:41:58 +02:00
*
* Allows the removal of event listeners from the event target.
*
* Parameters:
* type
* A string representing the event type being registered.
* listener
* The listener parameter takes an interface implemented by the user which contains the methods to be called when the event occurs.
* This interace will be invoked passing an event as argument and the 'this' referece in the function will be the element.
*/
2021-10-05 01:56:40 +02:00
removeEvent(type, listener) {
2021-10-05 02:17:02 +02:00
this.peer.removeEvent(type, listener);
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
/**
2021-07-16 16:41:58 +02:00
* /*
* Returns element type name.
*/
2021-10-05 01:56:40 +02:00
getType() {
throw new Error(
'Not implemeneted yet. This method must be implemented by all the inherited objects.',
);
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
/**
2021-07-16 16:41:58 +02:00
* Todo: Doc
*/
2021-10-05 01:56:40 +02:00
getFill() {
2021-10-05 02:17:02 +02:00
return this.peer.getFill();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
/**
2021-07-16 16:41:58 +02:00
* Used to define the fill element color and element opacity.
* color: Fill color
* opacity: Opacity of the fill. It must be less than 1.
*/
2021-10-05 01:56:40 +02:00
setFill(color, opacity) {
2021-10-05 02:17:02 +02:00
this.peer.setFill(color, opacity);
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
getPosition() {
2021-10-05 02:17:02 +02:00
return this.peer.getPosition();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
getNativePosition() {
2021-10-05 02:17:02 +02:00
return this.peer.getNativePosition();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
/*
2021-07-16 16:41:58 +02:00
* Defines the element stroke properties.
* width: stroke width
* style: "solid|dot|dash|dashdot|longdash".
* color: stroke color
* opacity: stroke visibility
*/
2021-10-05 01:56:40 +02:00
setStroke(width, style, color, opacity) {
if (
2021-10-06 04:08:42 +02:00
style !== null
&& style !== undefined
&& style !== 'dash'
&& style !== 'dot'
&& style !== 'solid'
&& style !== 'longdash'
&& style !== 'dashdot'
2021-10-05 01:56:40 +02:00
) {
throw new Error(`Unsupported stroke style: '${style}'`);
}
2021-10-05 02:17:02 +02:00
this.peer.setStroke(width, style, color, opacity);
2021-10-05 01:56:40 +02:00
},
_attributeNameToFuncName(attributeKey, prefix) {
const signature = Element._propertyNameToSignature[attributeKey];
2021-10-06 04:08:42 +02:00
if (!$defined(signature)) {
throw new Error(`Unsupported attribute: ${attributeKey}`);
2021-10-05 01:56:40 +02:00
}
const firstLetter = signature[0].charAt(0);
return prefix + firstLetter.toUpperCase() + signature[0].substring(1);
},
/**
2021-07-16 16:41:58 +02:00
* All element properties can be setted using either a method invocation or attribute invocation.
* key: size, width, height, position, x, y, stroke, strokeWidth, strokeStyle, strokeColor, strokeOpacity,
* fill, fillColor, fillOpacity, coordSize, coordSizeWidth, coordSizeHeight, coordOrigin, coordOriginX, coordOrigiY
*/
2021-10-05 01:56:40 +02:00
setAttribute(key, value) {
2021-10-06 04:08:42 +02:00
2021-10-05 01:56:40 +02:00
const funcName = this._attributeNameToFuncName(key, 'set');
const signature = Element._propertyNameToSignature[key];
if (signature == null) {
2021-10-06 04:08:42 +02:00
throw new Error(`Could not find the signature for:${key}`);
2021-10-05 01:56:40 +02:00
}
// Parse arguments ..
const argPositions = signature[1];
let args = [];
if (argPositions !== this._SIGNATURE_MULTIPLE_ARGUMENTS) {
args[argPositions] = value;
2021-10-06 04:08:42 +02:00
} else if (Array.isArray(value)) {
2021-10-05 01:56:40 +02:00
args = value;
} else {
const strValue = String(value);
args = strValue.split(' ');
}
// Look up method ...
const setter = this[funcName];
if (setter == null) {
2021-10-06 04:08:42 +02:00
throw new Error(`Could not find the function name:${funcName}`);
2021-10-05 01:56:40 +02:00
}
setter.apply(this, args);
},
getAttribute(key) {
const funcName = this._attributeNameToFuncName(key, 'get');
const signature = Element._propertyNameToSignature[key];
if (signature == null) {
2021-10-06 04:08:42 +02:00
throw new Error(`Could not find the signature for:${key}`);
2021-10-05 01:56:40 +02:00
}
const getter = this[funcName];
if (getter == null) {
2021-10-06 04:08:42 +02:00
throw new Error(`Could not find the function name:${funcName}`);
2021-10-05 01:56:40 +02:00
}
const getterResult = getter.apply(this, []);
const attibuteName = signature[2];
2021-10-06 04:08:42 +02:00
if (!$defined(attibuteName)) {
throw new Error(`Could not find attribute mapping for:${key}`);
2021-10-05 01:56:40 +02:00
}
const result = getterResult[attibuteName];
2021-10-06 04:08:42 +02:00
if (!$defined(result)) {
throw new Error(`Could not find attribute with name:${attibuteName}`);
2021-10-05 01:56:40 +02:00
}
return result;
},
/**
2021-07-16 16:41:58 +02:00
* Defines the element opacity.
* Parameters:
* opacity: A value between 0 and 1.
*/
2021-10-05 01:56:40 +02:00
setOpacity(opacity) {
2021-10-05 02:17:02 +02:00
this.peer.setStroke(null, null, null, opacity);
this.peer.setFill(null, opacity);
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
setVisibility(isVisible) {
2021-10-05 02:17:02 +02:00
this.peer.setVisibility(isVisible);
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
isVisible() {
2021-10-05 02:17:02 +02:00
return this.peer.isVisible();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
/**
2021-07-16 16:41:58 +02:00
* Move the element to the front
*/
2021-10-05 01:56:40 +02:00
moveToFront() {
2021-10-05 02:17:02 +02:00
this.peer.moveToFront();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
/**
2021-07-16 16:41:58 +02:00
* Move the element to the back
*/
2021-10-05 01:56:40 +02:00
moveToBack() {
2021-10-05 02:17:02 +02:00
this.peer.moveToBack();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
getStroke() {
2021-10-05 02:17:02 +02:00
return this.peer.getStroke();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
setCursor(type) {
2021-10-05 02:17:02 +02:00
this.peer.setCursor(type);
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
2021-10-05 01:56:40 +02:00
getParent() {
2021-10-05 02:17:02 +02:00
return this.peer.getParent();
2021-10-05 01:56:40 +02:00
},
2021-07-16 16:41:58 +02:00
});
Element._SIGNATURE_MULTIPLE_ARGUMENTS = -1;
Element._supportedEvents = [
2021-10-05 01:56:40 +02:00
'click',
'dblclick',
'mousemove',
'mouseout',
'mouseover',
'mousedown',
'mouseup',
2021-07-16 16:41:58 +02:00
];
Element._propertyNameToSignature = {
2021-10-05 01:56:40 +02:00
// Format: [attribute name, argument position on setter, attribute name on getter]
size: ['size', -1],
width: ['size', 0, 'width'],
height: ['size', 1, 'height'],
position: ['position', -1],
x: ['position', 0, 'x'],
y: ['position', 1, 'y'],
stroke: ['stroke', -1],
strokeWidth: ['stroke', 0, 'width'],
strokeStyle: ['stroke', 1, 'style'],
strokeColor: ['stroke', 2, 'color'],
strokeOpacity: ['stroke', 3, 'opacity'],
fill: ['fill', -1],
fillColor: ['fill', 0, 'color'],
fillOpacity: ['fill', 1, 'opacity'],
coordSize: ['coordSize', -1],
coordSizeWidth: ['coordSize', 0, 'width'],
coordSizeHeight: ['coordSize', 1, 'height'],
coordOrigin: ['coordOrigin', -1],
coordOriginX: ['coordOrigin', 0, 'x'],
coordOriginY: ['coordOrigin', 1, 'y'],
visibility: ['visibility', 0],
opacity: ['opacity', 0],
2021-07-16 16:41:58 +02:00
};
export default Element;