wisemapping-frontend/packages/mindplot/src/components/ScreenManager.js

153 lines
4.3 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-12-03 19:58:25 +01:00
import { $assert } from '@wisemapping/core-js';
import web2d from '@wisemapping/web2d';
2021-07-16 16:41:58 +02:00
2021-12-03 19:58:25 +01:00
class ScreenManager {
constructor(divElement) {
2021-10-05 02:05:34 +02:00
$assert(divElement, 'can not be null');
this._divContainer = divElement;
this._padding = { x: 0, y: 0 };
// Ignore default click event propagation. Prevent 'click' event on drag.
this._clickEvents = [];
this._divContainer.bind('click', (event) => {
event.stopPropagation();
});
this._divContainer.bind('dblclick', (event) => {
event.stopPropagation();
event.preventDefault();
});
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
setScale(scale) {
$assert(scale, 'Screen scale can not be null');
this._scale = scale;
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
addEvent(event, listener) {
if (event === 'click') this._clickEvents.push(listener);
2021-10-05 02:05:34 +02:00
else this._divContainer.bind(event, listener);
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
removeEvent(event, listener) {
if (event === 'click') {
2021-10-05 02:05:34 +02:00
this._clickEvents.remove(listener);
} else {
this._divContainer.unbind(event, listener);
}
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
fireEvent(type, event) {
if (type === 'click') {
2021-10-05 02:05:34 +02:00
_.each(this._clickEvents, (listener) => {
listener(type, event);
});
} else {
this._divContainer.trigger(type, event);
}
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
_getElementPosition(elem) {
// Retrieve current element position.
const elementPosition = elem.getPosition();
let { x } = elementPosition;
let { y } = elementPosition;
// Add workspace offset.
x -= this._padding.x;
y -= this._padding.y;
// Scale coordinate in order to be relative to the workspace. That's coord/size;
x /= this._scale;
y /= this._scale;
// Remove decimal part..
return { x, y };
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
getWorkspaceIconPosition(e) {
// Retrieve current icon position.
const image = e.getImage();
const elementPosition = image.getPosition();
const imageSize = e.getSize();
// Add group offset
const iconGroup = e.getGroup();
const group = iconGroup.getNativeElement();
const coordOrigin = group.getCoordOrigin();
const groupSize = group.getSize();
const coordSize = group.getCoordSize();
const scale = {
x: coordSize.width / parseInt(groupSize.width, 10),
y: coordSize.height / parseInt(groupSize.height, 10),
};
2021-10-05 02:05:34 +02:00
let x = (elementPosition.x - coordOrigin.x - parseInt(imageSize.width, 10) / 2) / scale.x;
let y = (elementPosition.y - coordOrigin.y - parseInt(imageSize.height, 10) / 2) / scale.y;
2021-10-05 02:05:34 +02:00
// Retrieve iconGroup Position
const groupPosition = iconGroup.getPosition();
x += groupPosition.x;
y += groupPosition.y;
// Retrieve topic Position
const topic = iconGroup.getTopic();
const topicPosition = this._getElementPosition(topic);
topicPosition.x -= parseInt(topic.getSize().width, 10) / 2;
2021-10-05 02:05:34 +02:00
// Remove decimal part..
return { x: x + topicPosition.x, y: y + topicPosition.y };
2021-12-03 19:58:25 +01:00
}
2021-10-05 02:05:34 +02:00
getWorkspaceMousePosition(event) {
// Retrieve current mouse position.
let x = event.clientX;
let y = event.clientY;
// FIXME: paulo: why? Subtract div position.
/* var containerPosition = this.getContainer().position();
2021-07-16 16:41:58 +02:00
x = x - containerPosition.x;
2021-10-05 02:05:34 +02:00
y = y - containerPosition.y; */
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Scale coordinate in order to be relative to the workspace. That's coordSize/size;
x *= this._scale;
y *= this._scale;
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Add workspace offset.
x += this._padding.x;
y += this._padding.y;
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Remove decimal part..
return new web2d.Point(x, y);
2021-12-03 19:58:25 +01:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
getContainer() {
return this._divContainer;
2021-12-03 19:58:25 +01:00
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
setOffset(x, y) {
this._padding.x = x;
this._padding.y = y;
2021-12-03 19:58:25 +01:00
}
}
2021-07-16 16:41:58 +02:00
export default ScreenManager;