blob: 27ce6c84e3cfc92820fd14a4d0978235add841f1 [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
72 if (!settings.svg) {
73 $log.error(cz + 'No "svg" (svg tag)' + d3s);
74 fail = true;
75 }
76 if (!settings.zoomLayer) {
77 $log.error(cz + 'No "zoomLayer" (g tag)' + d3s);
78 fail = true;
79 }
80
81 if (fail) {
82 return null;
83 }
84
85 // zoom events from mouse gestures...
86 function zoomed() {
87 var ev = d3.event.sourceEvent;
88 if (settings.zoomEnabled(ev)) {
89 adjustZoomLayer(d3.event.translate, d3.event.scale);
90 }
91 }
92
93 function adjustZoomLayer(translate, scale) {
94 settings.zoomLayer.attr('transform',
95 'translate(' + translate + ')scale(' + scale + ')');
Steven Burrows0616e802016-10-06 21:45:07 -050096 settings.zoomCallback(translate, scale);
Simon Huntcacce342015-01-07 16:13:05 -080097 }
98
99 zoomer = {
100 panZoom: function (translate, scale) {
101 zoom.translate(translate).scale(scale);
102 adjustZoomLayer(translate, scale);
103 },
104
105 reset: function () {
106 zoomer.panZoom([0,0], 1);
107 },
108
109 translate: function () {
110 return zoom.translate();
111 },
112
113 scale: function () {
114 return zoom.scale();
115 },
116
117 scaleExtent: function () {
118 return zoom.scaleExtent();
119 }
120 };
121
122 // apply the zoom behavior to the SVG element
123 settings.svg && settings.svg.call(zoom);
124 return zoomer;
125 }
126
Simon Hunt7ac7be92015-01-06 10:47:56 -0800127 return {
Simon Huntcacce342015-01-07 16:13:05 -0800128 createZoomer: createZoomer
Simon Hunt7ac7be92015-01-06 10:47:56 -0800129 };
130 }]);
131
132}());