Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -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 -- Topology View Module |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function () { |
| 24 | 'use strict'; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 25 | |
| 26 | var moduleDependencies = [ |
| 27 | 'onosUtil', |
| 28 | 'onosSvg' |
| 29 | ]; |
| 30 | |
| 31 | // references to injected services etc. |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 32 | var $log, ks, zs, gs, ms; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 33 | |
| 34 | // DOM elements |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 35 | var ovtopo, svg, defs, zoomLayer, map; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 36 | |
| 37 | // Internal state |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 38 | var zoomer; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 39 | |
| 40 | // Note: "exported" state should be properties on 'self' variable |
| 41 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 42 | // --- Short Cut Keys ------------------------------------------------ |
| 43 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 44 | var keyBindings = { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 45 | W: [logWarning, '(temp) log a warning'], |
| 46 | E: [logError, '(temp) log an error'], |
| 47 | R: [resetZoom, 'Reset pan / zoom'] |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // ----------------- |
| 51 | // these functions are necessarily temporary examples.... |
| 52 | function logWarning() { |
| 53 | $log.warn('You have been warned!'); |
| 54 | } |
| 55 | function logError() { |
| 56 | $log.error('You are erroneous!'); |
| 57 | } |
| 58 | // ----------------- |
| 59 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 60 | function resetZoom() { |
| 61 | zoomer.reset(); |
| 62 | } |
| 63 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 64 | function setUpKeys() { |
| 65 | ks.keyBindings(keyBindings); |
| 66 | } |
| 67 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 68 | |
| 69 | // --- Glyphs, Icons, and the like ----------------------------------- |
| 70 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 71 | function setUpDefs() { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 72 | defs = svg.append('defs'); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 73 | gs.loadDefs(defs); |
| 74 | } |
| 75 | |
| 76 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 77 | // --- Pan and Zoom -------------------------------------------------- |
| 78 | |
| 79 | // zoom enabled predicate. ev is a D3 source event. |
| 80 | function zoomEnabled(ev) { |
| 81 | return (ev.metaKey || ev.altKey); |
| 82 | } |
| 83 | |
| 84 | function zoomCallback() { |
| 85 | var tr = zoomer.translate(), |
| 86 | sc = zoomer.scale(); |
| 87 | $log.log('ZOOM: translate = ' + tr + ', scale = ' + sc); |
| 88 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 89 | // keep the map lines constant width while zooming |
| 90 | map.style('stroke-width', (2.0 / sc) + 'px'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | function setUpZoom() { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 94 | zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 95 | zoomer = zs.createZoomer({ |
| 96 | svg: svg, |
| 97 | zoomLayer: zoomLayer, |
| 98 | zoomEnabled: zoomEnabled, |
| 99 | zoomCallback: zoomCallback |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 104 | // callback invoked when the SVG view has been resized.. |
| 105 | function svgResized(w, h) { |
| 106 | // not used now, but may be required later... |
| 107 | } |
| 108 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 109 | // --- Background Map ------------------------------------------------ |
| 110 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 111 | function showCallibrationPoints() { |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 112 | // temp code for calibration |
| 113 | var points = [ |
| 114 | [0, 0], [0, 1000], [1000, 0], [1000, 1000] |
| 115 | ]; |
| 116 | map.selectAll('circle') |
| 117 | .data(points) |
| 118 | .enter() |
| 119 | .append('circle') |
| 120 | .attr('cx', function (d) { return d[0]; }) |
| 121 | .attr('cy', function (d) { return d[1]; }) |
| 122 | .attr('r', 5) |
| 123 | .style('fill', 'red'); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 126 | function setUpMap() { |
| 127 | map = zoomLayer.append('g').attr('id', 'topo-map'); |
| 128 | //ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5}); |
| 129 | ms.loadMapInto(map, '*continental_us'); |
| 130 | //showCallibrationPoints(); |
| 131 | } |
| 132 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 133 | // --- Controller Definition ----------------------------------------- |
| 134 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 135 | angular.module('ovTopo', moduleDependencies) |
| 136 | |
| 137 | .controller('OvTopoCtrl', [ |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 138 | '$log', |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 139 | 'KeyService', 'ZoomService', 'GlyphService', 'MapService', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 140 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 141 | function (_$log_, _ks_, _zs_, _gs_, _ms_) { |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 142 | var self = this; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 143 | $log = _$log_; |
| 144 | ks = _ks_; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 145 | zs = _zs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 146 | gs = _gs_; |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame] | 147 | ms = _ms_; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 148 | |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 149 | self.notifyResize = function () { |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 150 | svgResized(svg.style('width'), svg.style('height')); |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 151 | }; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 152 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 153 | // svg layer and initialization of components |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 154 | ovtopo = d3.select('#ov-topo'); |
| 155 | svg = ovtopo.select('svg'); |
| 156 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 157 | setUpKeys(); |
| 158 | setUpDefs(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 159 | setUpZoom(); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 160 | setUpMap(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 161 | |
| 162 | $log.log('OvTopoCtrl has been created'); |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 163 | }]); |
| 164 | }()); |