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 |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 23 | |
| 24 | var moduleDependencies = [ |
| 25 | 'onosUtil', |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 26 | 'onosSvg', |
| 27 | 'onosRemote' |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 28 | ]; |
| 29 | |
| 30 | // references to injected services etc. |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 31 | var $log, fs, ks, zs, gs, ms, tfs; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 32 | |
| 33 | // DOM elements |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 34 | var ovtopo, svg, defs, zoomLayer, mapG, forceG; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 35 | |
| 36 | // Internal state |
Simon Hunt | edf5c0e | 2015-01-29 15:00:53 -0800 | [diff] [blame^] | 37 | var zoomer; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 38 | |
| 39 | // Note: "exported" state should be properties on 'self' variable |
| 40 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 41 | // --- Short Cut Keys ------------------------------------------------ |
| 42 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 43 | var keyBindings = { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 44 | W: [logWarning, '(temp) log a warning'], |
| 45 | E: [logError, '(temp) log an error'], |
| 46 | R: [resetZoom, 'Reset pan / zoom'] |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // ----------------- |
| 50 | // these functions are necessarily temporary examples.... |
| 51 | function logWarning() { |
| 52 | $log.warn('You have been warned!'); |
| 53 | } |
| 54 | function logError() { |
| 55 | $log.error('You are erroneous!'); |
| 56 | } |
| 57 | // ----------------- |
| 58 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 59 | function resetZoom() { |
| 60 | zoomer.reset(); |
| 61 | } |
| 62 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 63 | function setUpKeys() { |
| 64 | ks.keyBindings(keyBindings); |
| 65 | } |
| 66 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 67 | |
| 68 | // --- Glyphs, Icons, and the like ----------------------------------- |
| 69 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 70 | function setUpDefs() { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 71 | defs = svg.append('defs'); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 72 | gs.loadDefs(defs); |
| 73 | } |
| 74 | |
| 75 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 76 | // --- Pan and Zoom -------------------------------------------------- |
| 77 | |
| 78 | // zoom enabled predicate. ev is a D3 source event. |
| 79 | function zoomEnabled(ev) { |
| 80 | return (ev.metaKey || ev.altKey); |
| 81 | } |
| 82 | |
| 83 | function zoomCallback() { |
| 84 | var tr = zoomer.translate(), |
| 85 | sc = zoomer.scale(); |
| 86 | $log.log('ZOOM: translate = ' + tr + ', scale = ' + sc); |
| 87 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 88 | // keep the map lines constant width while zooming |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 89 | mapG.style('stroke-width', (2.0 / sc) + 'px'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | function setUpZoom() { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 93 | zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 94 | zoomer = zs.createZoomer({ |
| 95 | svg: svg, |
| 96 | zoomLayer: zoomLayer, |
| 97 | zoomEnabled: zoomEnabled, |
| 98 | zoomCallback: zoomCallback |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 103 | // callback invoked when the SVG view has been resized.. |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 104 | function svgResized(dim) { |
| 105 | //$log.debug('TopoView just resized... ', dim); |
| 106 | tfs.resize(dim); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 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 | ]; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 116 | mapG.selectAll('circle') |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 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() { |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 127 | mapG = zoomLayer.append('g').attr('id', 'topo-map'); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 128 | //ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5}); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 129 | ms.loadMapInto(mapG, '*continental_us'); |
Simon Hunt | 0541fb8 | 2015-01-14 18:59:57 -0800 | [diff] [blame] | 130 | //showCallibrationPoints(); |
| 131 | } |
| 132 | |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 133 | // --- Force Layout -------------------------------------------------- |
| 134 | |
| 135 | function setUpForce() { |
| 136 | forceG = zoomLayer.append('g').attr('id', 'topo-force'); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 137 | tfs.initForce(forceG, svg.attr('width'), svg.attr('height')); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 140 | // --- Controller Definition ----------------------------------------- |
| 141 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 142 | angular.module('ovTopo', moduleDependencies) |
| 143 | |
| 144 | .controller('OvTopoCtrl', [ |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 145 | '$scope', '$log', '$location', '$timeout', |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 146 | 'FnService', 'MastService', |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 147 | 'KeyService', 'ZoomService', 'GlyphService', 'MapService', |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 148 | 'TopoEventService', 'TopoForceService', 'TopoPanelService', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 149 | |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 150 | function ($scope, _$log_, $loc, $timeout, _fs_, mast, |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 151 | _ks_, _zs_, _gs_, _ms_, tes, _tfs_, tps) { |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 152 | var self = this; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 153 | $log = _$log_; |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 154 | fs = _fs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 155 | ks = _ks_; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 156 | zs = _zs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 157 | gs = _gs_; |
Simon Hunt | 1e8eff4 | 2015-01-08 17:19:02 -0800 | [diff] [blame] | 158 | ms = _ms_; |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 159 | tfs = _tfs_; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 160 | |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 161 | self.notifyResize = function () { |
Simon Hunt | b0ec1e5 | 2015-01-28 18:13:49 -0800 | [diff] [blame] | 162 | svgResized(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 163 | }; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 164 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 165 | // Cleanup on destroyed scope.. |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 166 | $scope.$on('$destroy', function () { |
| 167 | $log.log('OvTopoCtrl is saying Buh-Bye!'); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 168 | tes.closeSock(); |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 169 | tps.destroyPanels(); |
Simon Hunt | 584122a | 2015-01-21 15:32:40 -0800 | [diff] [blame] | 170 | }); |
| 171 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 172 | // svg layer and initialization of components |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 173 | ovtopo = d3.select('#ov-topo'); |
| 174 | svg = ovtopo.select('svg'); |
Simon Hunt | a11b4eb | 2015-01-28 16:20:50 -0800 | [diff] [blame] | 175 | // set the svg size to match that of the window, less the masthead |
| 176 | svg.attr(fs.windowSize(mast.mastHeight())); |
Simon Hunt | 426bd86 | 2015-01-14 16:48:41 -0800 | [diff] [blame] | 177 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 178 | setUpKeys(); |
| 179 | setUpDefs(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 180 | setUpZoom(); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 181 | setUpMap(); |
Simon Hunt | 737c89f | 2015-01-28 12:23:19 -0800 | [diff] [blame] | 182 | setUpForce(); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 183 | |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 184 | tps.initPanels(); |
Simon Hunt | fd1231a | 2015-01-26 22:14:51 -0800 | [diff] [blame] | 185 | tes.openSock(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 186 | |
| 187 | $log.log('OvTopoCtrl has been created'); |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 188 | }]); |
| 189 | }()); |