Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 18 | ONOS GUI -- Topology Zoom Module. |
| 19 | Module that handles Zoom events. |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 23 | 'use strict'; |
| 24 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 25 | // injected references |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 26 | var fs, zs, ps; |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 27 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 28 | // internal state |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 29 | var zoomer, |
| 30 | zoomEventListeners = []; |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 31 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 32 | function createZoomer(options) { |
| 33 | // need to wrap the original zoom callback to extend its behavior |
| 34 | var origCallback = fs.isF(options.zoomCallback) || function () {}; |
| 35 | |
| 36 | options.zoomCallback = function () { |
| 37 | origCallback(); |
| 38 | |
| 39 | angular.forEach(zoomEventListeners, function (ev) { |
| 40 | ev(zoomer); |
| 41 | }); |
| 42 | }; |
| 43 | |
| 44 | zoomer = zs.createZoomer(options); |
| 45 | return zoomer; |
| 46 | } |
| 47 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 48 | function getZoomer() { |
| 49 | return zoomer; |
| 50 | } |
| 51 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 52 | function findZoomEventListener(ev) { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 53 | for (var i = 0, len = zoomEventListeners.length; i < len; i++) { |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 54 | if (zoomEventListeners[i] === ev) return i; |
| 55 | } |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | function addZoomEventListener(callback) { |
| 60 | zoomEventListeners.push(callback); |
| 61 | } |
| 62 | |
| 63 | function removeZoomEventListener(callback) { |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 64 | var evIndex = findZoomEventListener(callback); |
| 65 | |
| 66 | if (evIndex !== -1) { |
| 67 | zoomEventListeners.splice(evIndex); |
| 68 | } |
| 69 | } |
| 70 | |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 71 | function adjustmentScale(min, max) { |
| 72 | var _scale = 1, |
| 73 | size = (min + max) / 2; |
| 74 | |
| 75 | if (size * scale() < max) { |
| 76 | _scale = min / (size * scale()); |
| 77 | } else if (size * scale() > max) { |
| 78 | _scale = min / (size * scale()); |
| 79 | } |
| 80 | |
| 81 | return _scale; |
| 82 | } |
| 83 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 84 | function scale() { |
| 85 | return zoomer.scale(); |
| 86 | } |
| 87 | |
Steven Burrows | 7d1efef | 2017-03-25 02:07:43 +0000 | [diff] [blame] | 88 | function panAndZoom(translate, scale, transition) { |
| 89 | zoomer.panZoom(translate, scale, transition); |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 92 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 93 | .factory('Topo2ZoomService', [ |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 94 | 'FnService', 'ZoomService', 'PrefsService', |
| 95 | function (_fs_, _zs_, _ps_) { |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 96 | |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 97 | fs = _fs_; |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 98 | zs = _zs_; |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 99 | ps = _ps_; |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 100 | |
| 101 | return { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 102 | getZoomer: getZoomer, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 103 | createZoomer: createZoomer, |
| 104 | addZoomEventListener: addZoomEventListener, |
| 105 | removeZoomEventListener: removeZoomEventListener, |
| 106 | |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 107 | scale: scale, |
Steven Burrows | ad74af8 | 2017-04-28 15:27:19 -0400 | [diff] [blame] | 108 | adjustmentScale: adjustmentScale, |
Simon Hunt | 5c3ed73 | 2017-07-20 19:03:28 +0000 | [diff] [blame] | 109 | panAndZoom: panAndZoom |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 110 | }; |
| 111 | }]); |
| 112 | })(); |