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 | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 32 | var $log, ks, zs, gs; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 33 | |
| 34 | // DOM elements |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 35 | var svg, defs; |
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 | |
| 89 | // TODO: keep the map lines constant width while zooming |
| 90 | //bgImg.style('stroke-width', 2.0 / scale + 'px'); |
| 91 | } |
| 92 | |
| 93 | function setUpZoom() { |
| 94 | var zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer'); |
| 95 | zoomer = zs.createZoomer({ |
| 96 | svg: svg, |
| 97 | zoomLayer: zoomLayer, |
| 98 | zoomEnabled: zoomEnabled, |
| 99 | zoomCallback: zoomCallback |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // --- Controller Definition ----------------------------------------- |
| 105 | |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 106 | angular.module('ovTopo', moduleDependencies) |
| 107 | |
| 108 | .controller('OvTopoCtrl', [ |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 109 | '$log', 'KeyService', 'ZoomService', 'GlyphService', |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 110 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 111 | function (_$log_, _ks_, _zs_, _gs_) { |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 112 | var self = this; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 113 | $log = _$log_; |
| 114 | ks = _ks_; |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 115 | zs = _zs_; |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 116 | gs = _gs_; |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 117 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 118 | // exported state |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 119 | self.message = 'Topo View Rocks!'; |
| 120 | |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 121 | // svg layer and initialization of components |
| 122 | svg = d3.select('#ov-topo svg'); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 123 | setUpKeys(); |
| 124 | setUpDefs(); |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame^] | 125 | setUpZoom(); |
Simon Hunt | 6cc5369 | 2015-01-07 11:33:45 -0800 | [diff] [blame] | 126 | |
| 127 | $log.log('OvTopoCtrl has been created'); |
Simon Hunt | ef31fb2 | 2014-12-19 13:16:44 -0800 | [diff] [blame] | 128 | }]); |
| 129 | }()); |