blob: 3e6f12ad0c517118fedddd5027968dd695e688aa [file] [log] [blame]
Steven Burrows0616e802016-10-06 21:45:07 -05001/*
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 Hunt95f4b422017-03-03 13:49:05 -080018 ONOS GUI -- Topology Zoom Module.
19 Module that handles Zoom events.
Steven Burrows0616e802016-10-06 21:45:07 -050020 */
21
22(function () {
Steven Burrows0616e802016-10-06 21:45:07 -050023 'use strict';
24
Simon Hunt95f4b422017-03-03 13:49:05 -080025 // injected references
26 var fs, zs, ps;
Steven Burrows1742bb82016-10-27 10:36:25 -050027
Simon Hunt95f4b422017-03-03 13:49:05 -080028 // internal state
Steven Burrows1742bb82016-10-27 10:36:25 -050029 var zoomer,
30 zoomEventListeners = [];
Steven Burrows0616e802016-10-06 21:45:07 -050031
Simon Hunt95f4b422017-03-03 13:49:05 -080032 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 Burrowsaf96a212016-12-28 12:57:02 +000048 function getZoomer() {
49 return zoomer;
50 }
51
Steven Burrows0616e802016-10-06 21:45:07 -050052 function findZoomEventListener(ev) {
Simon Hunt95f4b422017-03-03 13:49:05 -080053 for (var i = 0, len = zoomEventListeners.length; i < len; i++) {
Steven Burrows0616e802016-10-06 21:45:07 -050054 if (zoomEventListeners[i] === ev) return i;
55 }
Steven Burrows0616e802016-10-06 21:45:07 -050056 return -1;
57 }
58
59 function addZoomEventListener(callback) {
60 zoomEventListeners.push(callback);
61 }
62
63 function removeZoomEventListener(callback) {
Steven Burrows0616e802016-10-06 21:45:07 -050064 var evIndex = findZoomEventListener(callback);
65
66 if (evIndex !== -1) {
67 zoomEventListeners.splice(evIndex);
68 }
69 }
70
Steven Burrowsad74af82017-04-28 15:27:19 -040071 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 Burrows0616e802016-10-06 21:45:07 -050084 function scale() {
85 return zoomer.scale();
86 }
87
Steven Burrows7d1efef2017-03-25 02:07:43 +000088 function panAndZoom(translate, scale, transition) {
89 zoomer.panZoom(translate, scale, transition);
Steven Burrows1742bb82016-10-27 10:36:25 -050090 }
91
Steven Burrows0616e802016-10-06 21:45:07 -050092 angular.module('ovTopo2')
Steven Burrowsaf96a212016-12-28 12:57:02 +000093 .factory('Topo2ZoomService', [
Simon Hunt95f4b422017-03-03 13:49:05 -080094 'FnService', 'ZoomService', 'PrefsService',
95 function (_fs_, _zs_, _ps_) {
Steven Burrows0616e802016-10-06 21:45:07 -050096
Simon Hunt95f4b422017-03-03 13:49:05 -080097 fs = _fs_;
Steven Burrows0616e802016-10-06 21:45:07 -050098 zs = _zs_;
99 ps = _ps_;
100
101 return {
Steven Burrowsaf96a212016-12-28 12:57:02 +0000102 getZoomer: getZoomer,
Steven Burrows0616e802016-10-06 21:45:07 -0500103 createZoomer: createZoomer,
104 addZoomEventListener: addZoomEventListener,
105 removeZoomEventListener: removeZoomEventListener,
106
Steven Burrows1742bb82016-10-27 10:36:25 -0500107 scale: scale,
Steven Burrowsad74af82017-04-28 15:27:19 -0400108 adjustmentScale: adjustmentScale,
Steven Burrows1742bb82016-10-27 10:36:25 -0500109 panAndZoom: panAndZoom
Steven Burrows0616e802016-10-06 21:45:07 -0500110 };
111 }]);
112})();