Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 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 | /* |
| 18 | ONOS GUI -- SVG -- Zoom Service |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 23 | // configuration |
| 24 | var defaultSettings = { |
Thomas Vachuska | 52c98bd | 2015-05-27 20:54:02 -0700 | [diff] [blame] | 25 | zoomMin: 0.05, |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 26 | zoomMax: 50, |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 27 | zoomEnabled: function (ev) { return true; }, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 28 | zoomCallback: function () {}, |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | // injected references to services |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 32 | var $log; |
| 33 | |
| 34 | angular.module('onosSvg') |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 35 | .factory('ZoomService', ['$log', |
| 36 | |
| 37 | function (_$log_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 38 | $log = _$log_; |
| 39 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 40 | /* |
| 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 Hunt | 404f6b2 | 2015-01-21 14:00:56 -0800 | [diff] [blame] | 63 | settings = angular.extend({}, defaultSettings, opts), |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 64 | 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 Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 72 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 73 | 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 Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 94 | function adjustZoomLayer(translate, scale, transition) { |
| 95 | |
| 96 | settings.zoomLayer.transition() |
| 97 | .duration(transition || 0) |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 98 | .attr('transform', |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 99 | 'translate(' + translate + ')scale(' + scale + ')'); |
| 100 | |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 101 | settings.zoomCallback(translate, scale); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | zoomer = { |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 105 | panZoom: function (translate, scale, transition) { |
| 106 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 107 | zoom.translate(translate).scale(scale); |
Steven Burrows | 1742bb8 | 2016-10-27 10:36:25 -0500 | [diff] [blame] | 108 | adjustZoomLayer(translate, scale, transition); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 109 | }, |
| 110 | |
| 111 | reset: function () { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 112 | zoomer.panZoom([0, 0], 1); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 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(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 125 | }, |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | // apply the zoom behavior to the SVG element |
| 129 | settings.svg && settings.svg.call(zoom); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 130 | |
| 131 | // Remove zoom on double click (prevents a |
| 132 | // false zoom navigating regions) |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 133 | settings.svg.on('dblclick.zoom', null); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 134 | return zoomer; |
| 135 | } |
| 136 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 137 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 138 | createZoomer: createZoomer, |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 139 | }; |
| 140 | }]); |
| 141 | |
| 142 | }()); |