142 lines
4.0 KiB
TypeScript
Raw Normal View History

2021-07-16 11:41:58 -03:00
/*
2021-12-25 14:39:34 -08:00
* Copyright [2021] [wisemapping]
2021-07-16 11:41:58 -03:00
*
* 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 10:58:25 -08:00
import { $assert } from '@wisemapping/core-js';
2021-12-19 08:31:29 -08:00
import { Point } from '@wisemapping/web2d';
2021-07-16 11:41:58 -03:00
2021-12-03 10:58:25 -08:00
class ScreenManager {
2022-01-10 10:52:11 -08:00
private _divContainer: JQuery;
private _padding: { x: number; y: number };
2022-01-31 00:05:22 +00:00
private _clickEvents;
2022-01-10 10:52:11 -08:00
private _scale: number;
constructor(divElement: JQuery) {
2021-10-04 17:05:34 -07: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: { stopPropagation: () => void }) => {
2021-10-04 17:05:34 -07:00
event.stopPropagation();
});
this._divContainer.bind(
'dblclick',
(event: { stopPropagation: () => void; preventDefault: () => void }) => {
event.stopPropagation();
event.preventDefault();
},
);
2021-12-03 10:58:25 -08:00
}
2021-10-04 17:05:34 -07:00
2022-01-10 10:52:11 -08:00
/**
* Return the current visibile area in the browser.
*/
getVisibleBrowserSize(): { width: number; height: number } {
2022-01-10 10:52:11 -08:00
return {
width: window.innerWidth,
2022-01-10 12:12:15 -08:00
height: window.innerHeight - Number.parseInt(this._divContainer.css('top'), 10),
};
2022-01-10 10:52:11 -08:00
}
setScale(scale: number) {
2021-10-04 17:05:34 -07:00
$assert(scale, 'Screen scale can not be null');
this._scale = scale;
2021-12-03 10:58:25 -08:00
}
2021-10-04 17:05:34 -07:00
2022-01-31 00:05:22 +00:00
addEvent(eventType: string, listener) {
if (eventType === 'click') this._clickEvents.push(listener);
else this._divContainer.bind(eventType, listener);
2021-12-03 10:58:25 -08:00
}
2021-10-04 17:05:34 -07:00
2022-01-31 00:05:22 +00:00
removeEvent(event: string, listener) {
if (event === 'click') {
2021-10-04 17:05:34 -07:00
this._clickEvents.remove(listener);
} else {
this._divContainer.unbind(event, listener);
}
2021-12-03 10:58:25 -08:00
}
2021-10-04 17:05:34 -07:00
fireEvent(type: string, event: UIEvent = null) {
if (type === 'click') {
2022-02-09 19:44:55 -08:00
this._clickEvents.forEach((listener) => {
2021-10-04 17:05:34 -07:00
listener(type, event);
});
} else {
this._divContainer.trigger(type, event);
}
2021-12-03 10:58:25 -08:00
}
2021-10-04 17:05:34 -07:00
private mouseEvents = ['mousedown', 'mouseup', 'mousemove', 'dblclick', 'click'];
private tocuchEvents = ['touchstart', 'touchend', 'touchmove'];
// the received type was changed from MouseEvent to "any", because we must support touch events
getWorkspaceMousePosition(event: any) {
let x;
let y;
if (this.mouseEvents.includes(event.type)) {
// Retrieve current mouse position.
x = event.clientX;
y = event.clientY;
} else if (this.tocuchEvents.includes(event.type)) {
x = event.touches[0].clientX;
y = event.touches[0].clientY;
}
// if value is zero assert throws error
if (x !== 0) {
$assert(x, `clientX can not be null, eventType= ${event.type}`);
}
if (y !== 0) {
$assert(y, `clientY can not be null, eventType= ${event.type}`);
}
2021-10-04 17:05:34 -07:00
// Adjust the deviation of the container positioning ...
const containerPosition = this.getContainer().position();
x -= containerPosition.left;
y -= containerPosition.top;
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
// Scale coordinate in order to be relative to the workspace. That's coordSize/size;
x *= this._scale;
y *= this._scale;
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
// Add workspace offset.
x += this._padding.x;
y += this._padding.y;
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
// Remove decimal part..
2021-12-19 08:31:29 -08:00
return new Point(x, y);
2021-12-03 10:58:25 -08:00
}
2021-07-16 11:41:58 -03:00
2021-10-04 17:05:34 -07:00
getContainer() {
return this._divContainer;
2021-12-03 10:58:25 -08:00
}
2021-07-16 11:41:58 -03:00
setOffset(x: number, y: number) {
2021-10-04 17:05:34 -07:00
this._padding.x = x;
this._padding.y = y;
2021-12-03 10:58:25 -08:00
}
}
2021-07-16 11:41:58 -03:00
export default ScreenManager;