All files / frontend/components/Schema zoomAndPan.js

97.72% Statements 43/44
70% Branches 21/30
100% Functions 11/11
97.43% Lines 38/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                                                                  43x                               1x       1x     6x               6x           3x         2x 2x 2x 2x 2x 2x 2x 2x 2x     29x 44x   12x     3x 2x 2x   1x 1x   1x 1x   2x 2x     2x 1x 1x 1x     3x 1x 1x 1x 1x 1x     74x        
/*
  Copyright (C) 2021 owner Roman Piontik R.Piontik@mail.ru
 
  Copyright (C) 2022 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
  http://www.apache.org/licenses/LICENSE-2.0
 
  In any derivative products, you must retain the information of
  owner of the original code and provide clear attribution to the project
 
  https://dochub.info
 
  The use of this product or its derivatives for any purpose cannot be a secret.
 
 
  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.
 
  Maintainers:
      R.Piontik <r.piontik@mail.ru>
 
  Contributors:
      R.Piontik <r.piontik@mail.ru> - 2023
*/
 
export default {
	data: () => ({
		zoomAndPan: {
			zoomKeys: ['ctrlKey', 'shiftKey'],  // Кнопка для зума
			panKeys: ['ctrlKey', 'shiftKey'],
			isMove: false, 			 // Признак перемещения схемы
			cacheViewBox: null,
			moveX: 0,
			moveY: 0,
			zoom: {
				value: 1,   // Текущий зум
				step: 0.1  	// Шаг зума
			}
		}
	}),
	computed: {
		koofScreenX() {
			return this.zoomAndPanElement ? this.ZoomAndPanViewBox.width / this.zoomAndPanElement.clientWidth : 1;
		},
		// Коэффициент преобразования реальных точек во внутренние по высоте
		koofScreenY() {
			return this.zoomAndPanElement ? this.ZoomAndPanViewBox.height / this.zoomAndPanElement.clientHeight : 1;
		},
		ZoomAndPanViewBox() {
			Iif (!this.zoomAndPanElement) {
				return {
					x: 0,
					y : 0,
					width : 0,
					height : 0
				};
			} else
				return this.zoomAndPan.cacheViewBox ?
					this.zoomAndPan.cacheViewBox
					// eslint-disable-next-line vue/no-side-effects-in-computed-properties
					: this.zoomAndPan.cacheViewBox = this.zoomAndPanElement.viewBox.baseVal;
		},
		zoomAndPanElement() {
			return this.$refs.zoomAndPan;
		}
	},
	methods: {
		doZoom(value, x, y) {
			const kX = x / (this.zoomAndPanElement.clientWidth || x);
			const kY = y / (this.zoomAndPanElement.clientHeight || y);
			let resizeWidth = value * this.ZoomAndPanViewBox.width;
			let resizeHeight = value * this.ZoomAndPanViewBox.height;
			this.ZoomAndPanViewBox.x -= resizeWidth * kX;
			this.ZoomAndPanViewBox.width += resizeWidth;
			this.ZoomAndPanViewBox.y -= resizeHeight * kY;
			this.ZoomAndPanViewBox.height += resizeHeight;
			this.zoomAndPan.cacheViewBox = null;
		},
		isPushKey(event, keys) {
			for (let i = 0; i < keys.length; i++) {
				if (event[keys[i]]) return true;
			}
			return false;
		},
		zoomAndPanWheelHandler(event) {
			if (!this.isPushKey(event, this.zoomAndPan.zoomKeys)) return;
			let e = window.event || event;
			switch (Math.max(-1, Math.min(1, (e.deltaY || -e.detail)))) {
				case 1:
					this.doZoom(this.zoomAndPan.zoom.step, event.offsetX, event.offsetY);
					break;
				case -1:
					this.doZoom(-this.zoomAndPan.zoom.step, event.offsetX, event.offsetY);
					break;
			}
      event.preventDefault();
			event.stopPropagation();
		},
		zoomAndPanMouseDown(event) {
			if (!this.isPushKey(event, this.zoomAndPan.panKeys)) return;
			this.zoomAndPan.isMove = true;
			this.zoomAndPan.moveX = event.clientX;
			this.zoomAndPan.moveY = event.clientY;
		},
		zoomAndPanMouseMove(event) {
			if (!this.zoomAndPan.isMove) return;
			this.ZoomAndPanViewBox.x += (this.zoomAndPan.moveX - event.clientX) * (this.koofScreenX || 0);
			this.ZoomAndPanViewBox.y += (this.zoomAndPan.moveY - event.clientY) * (this.koofScreenY || 0);
			this.zoomAndPan.moveX = event.clientX;
			this.zoomAndPan.moveY = event.clientY;
			this.zoomAndPan.cacheViewBox = null;
		},
		zoomAndPanMouseUp() {
			this.zoomAndPan.isMove = false;
		}
	}
};