124 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-10-04 16:56:40 -07:00
/*
2021-12-02 20:38:53 -08:00
* Copyright [2021] [wisemapping]
2021-10-04 16:56:40 -07: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-02 21:23:14 -08:00
import { $defined } from '@wisemapping/core-js';
import ElementPeer from './ElementPeer';
import EventUtils from '../utils/EventUtils';
2021-12-04 15:39:20 -08:00
class WorkspacePeer extends ElementPeer {
constructor(element) {
const svgElement = window.document.createElementNS(ElementPeer.svgNamespace, 'svg');
super(svgElement);
2021-10-04 16:56:40 -07:00
this._element = element;
this._native.setAttribute('focusable', 'true');
2021-12-04 15:39:20 -08:00
// this._native.setAttribute('id', 'workspace');
2021-10-04 16:56:40 -07:00
this._native.setAttribute('preserveAspectRatio', 'none');
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
/**
* http://www.w3.org/TR/SVG/coords.html 7.7 The viewBox attribute
* It is often desirable to specify that a given set of graphics
* stretch to fit a particular container element. The viewBox attribute
* provides this capability.
2021-10-04 16:56:40 -07:00
*
* All elements that establish a new viewport (see elements that establish viewports),
* plus the 'marker', 'pattern' and 'view' elements have attribute viewBox.
* The value of the viewBox attribute is a list of four numbers <min-x>, <min-y>,
* <width> and <height>, separated by whitespace and/or a comma, which specify a rectangle
* in user space which should be mapped to the bounds of the viewport established by
* the given element, taking into account attribute preserveAspectRatio. If specified,
* an additional transformation is applied to all descendants of the given element to
* achieve the specified effect.
2021-10-04 16:56:40 -07:00
*
* A negative value for <width> or <height> is an error (see Error processing).
* A value of zero disables rendering of the element.
2021-10-04 16:56:40 -07:00
*
*/
setCoordSize(width, height) {
const viewBox = this._native.getAttribute('viewBox');
2021-12-30 15:03:59 -08:00
2021-10-04 16:56:40 -07:00
let coords = [0, 0, 0, 0];
if (viewBox != null) {
coords = viewBox.split(/ /);
}
2021-12-02 21:23:14 -08:00
if ($defined(width)) {
2021-12-30 15:03:59 -08:00
coords[2] = width.toFixed(5);
2021-10-04 16:56:40 -07:00
}
2021-12-02 21:23:14 -08:00
if ($defined(height)) {
2021-12-30 15:03:59 -08:00
coords[3] = height.toFixed(5);
2021-10-04 16:56:40 -07:00
}
this._native.setAttribute('viewBox', coords.join(' '));
this._native.setAttribute('preserveAspectRatio', 'none');
EventUtils.broadcastChangeEvent(this, 'strokeStyle');
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getCoordSize() {
const viewBox = this._native.getAttribute('viewBox');
let coords = [1, 1, 1, 1];
if (viewBox != null) {
coords = viewBox.split(/ /);
}
return { width: coords[2], height: coords[3] };
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
setCoordOrigin(x, y) {
const viewBox = this._native.getAttribute('viewBox');
// ViewBox min-x ,min-y by default initializated with 0 and 0.
let coords = [0, 0, 0, 0];
if (viewBox != null) {
coords = viewBox.split(/ /);
}
2021-12-02 21:23:14 -08:00
if ($defined(x)) {
2021-10-04 16:56:40 -07:00
coords[0] = x;
}
2021-12-02 21:23:14 -08:00
if ($defined(y)) {
2021-10-04 16:56:40 -07:00
coords[1] = y;
}
this._native.setAttribute('viewBox', coords.join(' '));
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
append(child) {
2021-12-04 15:39:20 -08:00
super.append(child);
2021-10-04 16:56:40 -07:00
EventUtils.broadcastChangeEvent(child, 'onChangeCoordSize');
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
getCoordOrigin() {
2021-10-04 16:56:40 -07:00
const viewBox = this._native.getAttribute('viewBox');
let coords = [1, 1, 1, 1];
if (viewBox != null) {
coords = viewBox.split(/ /);
}
const x = parseFloat(coords[0]);
const y = parseFloat(coords[1]);
return { x, y };
2021-12-04 15:39:20 -08:00
}
2021-10-04 16:56:40 -07:00
2021-12-04 15:39:20 -08:00
// eslint-disable-next-line class-methods-use-this
2021-10-04 16:56:40 -07:00
getPosition() {
return { x: 0, y: 0 };
2021-12-04 15:39:20 -08:00
}
}
2021-10-04 16:56:40 -07:00
export default WorkspacePeer;