blob: cebe8c74b3d5b10319d39d33528d53ac9505764d [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt7ac7be92015-01-06 10:47:56 -08003 *
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/*
18 ONOS GUI -- SVG -- Zoom Service
Simon Hunt7ac7be92015-01-06 10:47:56 -080019 */
20(function () {
21 'use strict';
22
Simon Huntcacce342015-01-07 16:13:05 -080023 // configuration
24 var defaultSettings = {
Thomas Vachuska52c98bd2015-05-27 20:54:02 -070025 zoomMin: 0.05,
Simon Huntcacce342015-01-07 16:13:05 -080026 zoomMax: 10,
27 zoomEnabled: function (ev) { return true; },
28 zoomCallback: function () {}
29 };
30
31 // injected references to services
Simon Hunt7ac7be92015-01-06 10:47:56 -080032 var $log;
33
34 angular.module('onosSvg')
Simon Huntcacce342015-01-07 16:13:05 -080035 .factory('ZoomService', ['$log',
36
37 function (_$log_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -080038 $log = _$log_;
39
Simon Huntcacce342015-01-07 16:13:05 -080040/*
41 NOTE: opts is an object:
42 {
43 svg: svg, D3 selection of <svg> element
44 zoomLayer: zoomLayer, D3 selection of <g> element
45 zoomEnabled: function (ev) { ... },
46 zoomCallback: function () { ... }
47 }
48
49 where:
50 * svg and zoomLayer should be D3 selections of DOM elements.
51 * zoomLayer <g> is a child of <svg> element.
52 * zoomEnabled is an optional predicate based on D3 event.
53 * default is always enabled.
54 * zoomCallback is an optional callback invoked each time we pan/zoom.
55 * default is do nothing.
56
57 Optionally, zoomMin and zoomMax also can be defined.
58 These default to 0.25 and 10 respectively.
59*/
60 function createZoomer(opts) {
61 var cz = 'ZoomService.createZoomer(): ',
62 d3s = ' (D3 selection) property defined',
Simon Hunt404f6b22015-01-21 14:00:56 -080063 settings = angular.extend({}, defaultSettings, opts),
Simon Huntcacce342015-01-07 16:13:05 -080064 zoom = d3.behavior.zoom()
65 .translate([0, 0])
66 .scale(1)
67 .scaleExtent([settings.zoomMin, settings.zoomMax])
68 .on('zoom', zoomed),
69 fail = false,
70 zoomer;
71
Steven Burrowsa3fca812016-10-14 15:11:04 -050072
Simon Huntcacce342015-01-07 16:13:05 -080073 if (!settings.svg) {
74 $log.error(cz + 'No "svg" (svg tag)' + d3s);
75 fail = true;
76 }
77 if (!settings.zoomLayer) {
78 $log.error(cz + 'No "zoomLayer" (g tag)' + d3s);
79 fail = true;
80 }
81
82 if (fail) {
83 return null;
84 }
85
86 // zoom events from mouse gestures...
87 function zoomed() {
88 var ev = d3.event.sourceEvent;
89 if (settings.zoomEnabled(ev)) {
90 adjustZoomLayer(d3.event.translate, d3.event.scale);
91 }
92 }
93
Steven Burrows1742bb82016-10-27 10:36:25 -050094 function adjustZoomLayer(translate, scale, transition) {
95
96 settings.zoomLayer.transition()
97 .duration(transition || 0)
98 .attr("transform",
99 'translate(' + translate + ')scale(' + scale + ')');
100
Steven Burrows0616e802016-10-06 21:45:07 -0500101 settings.zoomCallback(translate, scale);
Simon Huntcacce342015-01-07 16:13:05 -0800102 }
103
104 zoomer = {
Steven Burrows1742bb82016-10-27 10:36:25 -0500105 panZoom: function (translate, scale, transition) {
106
Simon Huntcacce342015-01-07 16:13:05 -0800107 zoom.translate(translate).scale(scale);
Steven Burrows1742bb82016-10-27 10:36:25 -0500108 adjustZoomLayer(translate, scale, transition);
Simon Huntcacce342015-01-07 16:13:05 -0800109 },
110
111 reset: function () {
112 zoomer.panZoom([0,0], 1);
113 },
114
115 translate: function () {
116 return zoom.translate();
117 },
118
119 scale: function () {
120 return zoom.scale();
121 },
122
123 scaleExtent: function () {
124 return zoom.scaleExtent();
125 }
126 };
127
128 // apply the zoom behavior to the SVG element
129 settings.svg && settings.svg.call(zoom);
Steven Burrowsa3fca812016-10-14 15:11:04 -0500130
131 // Remove zoom on double click (prevents a
132 // false zoom navigating regions)
133 settings.svg.on("dblclick.zoom", null);
Simon Huntcacce342015-01-07 16:13:05 -0800134 return zoomer;
135 }
136
Simon Hunt7ac7be92015-01-06 10:47:56 -0800137 return {
Simon Huntcacce342015-01-07 16:13:05 -0800138 createZoomer: createZoomer
Simon Hunt7ac7be92015-01-06 10:47:56 -0800139 };
140 }]);
141
142}());