blob: 587c78d476e60acf5a6101abed493846048385e0 [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
94 function adjustZoomLayer(translate, scale) {
95 settings.zoomLayer.attr('transform',
96 'translate(' + translate + ')scale(' + scale + ')');
Steven Burrows0616e802016-10-06 21:45:07 -050097 settings.zoomCallback(translate, scale);
Simon Huntcacce342015-01-07 16:13:05 -080098 }
99
100 zoomer = {
101 panZoom: function (translate, scale) {
102 zoom.translate(translate).scale(scale);
103 adjustZoomLayer(translate, scale);
104 },
105
106 reset: function () {
107 zoomer.panZoom([0,0], 1);
108 },
109
110 translate: function () {
111 return zoom.translate();
112 },
113
114 scale: function () {
115 return zoom.scale();
116 },
117
118 scaleExtent: function () {
119 return zoom.scaleExtent();
120 }
121 };
122
123 // apply the zoom behavior to the SVG element
124 settings.svg && settings.svg.call(zoom);
Steven Burrowsa3fca812016-10-14 15:11:04 -0500125
126 // Remove zoom on double click (prevents a
127 // false zoom navigating regions)
128 settings.svg.on("dblclick.zoom", null);
Simon Huntcacce342015-01-07 16:13:05 -0800129 return zoomer;
130 }
131
Simon Hunt7ac7be92015-01-06 10:47:56 -0800132 return {
Simon Huntcacce342015-01-07 16:13:05 -0800133 createZoomer: createZoomer
Simon Hunt7ac7be92015-01-06 10:47:56 -0800134 };
135 }]);
136
137}());