blob: cb0c44e41fe046e8fb7a911750cde27057f56314 [file] [log] [blame]
Simon Huntef31fb22014-12-19 13:16:44 -08001/*
Simon Hunt8ead3a22015-01-06 11:00:15 -08002 * Copyright 2014,2015 Open Networking Laboratory
Simon Huntef31fb22014-12-19 13:16:44 -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 -- Topology View Module
19
20 @author Simon Hunt
21 */
22
23(function () {
24 'use strict';
Simon Hunt6cc53692015-01-07 11:33:45 -080025
26 var moduleDependencies = [
27 'onosUtil',
28 'onosSvg'
29 ];
30
31 // references to injected services etc.
Simon Hunt0541fb82015-01-14 18:59:57 -080032 var $log, ks, zs, gs, ms;
Simon Hunt6cc53692015-01-07 11:33:45 -080033
34 // DOM elements
Simon Hunt426bd862015-01-14 16:48:41 -080035 var ovtopo, svg, defs, zoomLayer, map;
Simon Hunt6cc53692015-01-07 11:33:45 -080036
37 // Internal state
Simon Huntcacce342015-01-07 16:13:05 -080038 var zoomer;
Simon Hunt6cc53692015-01-07 11:33:45 -080039
40 // Note: "exported" state should be properties on 'self' variable
41
Simon Huntcacce342015-01-07 16:13:05 -080042 // --- Short Cut Keys ------------------------------------------------
43
Simon Hunt6cc53692015-01-07 11:33:45 -080044 var keyBindings = {
Simon Huntcacce342015-01-07 16:13:05 -080045 W: [logWarning, '(temp) log a warning'],
46 E: [logError, '(temp) log an error'],
47 R: [resetZoom, 'Reset pan / zoom']
Simon Hunt6cc53692015-01-07 11:33:45 -080048 };
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 Huntcacce342015-01-07 16:13:05 -080060 function resetZoom() {
61 zoomer.reset();
62 }
63
Simon Hunt6cc53692015-01-07 11:33:45 -080064 function setUpKeys() {
65 ks.keyBindings(keyBindings);
66 }
67
Simon Huntcacce342015-01-07 16:13:05 -080068
69 // --- Glyphs, Icons, and the like -----------------------------------
70
Simon Hunt6cc53692015-01-07 11:33:45 -080071 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -080072 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -080073 gs.loadDefs(defs);
74 }
75
76
Simon Huntcacce342015-01-07 16:13:05 -080077 // --- 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 Hunt0541fb82015-01-14 18:59:57 -080089 // keep the map lines constant width while zooming
90 map.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -080091 }
92
93 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -080094 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -080095 zoomer = zs.createZoomer({
96 svg: svg,
97 zoomLayer: zoomLayer,
98 zoomEnabled: zoomEnabled,
99 zoomCallback: zoomCallback
100 });
101 }
102
103
Simon Hunt0541fb82015-01-14 18:59:57 -0800104 // 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 Hunta7b6a6b2015-01-13 19:53:09 -0800109 // --- Background Map ------------------------------------------------
110
Simon Hunt0541fb82015-01-14 18:59:57 -0800111 function showCallibrationPoints() {
Simon Hunt426bd862015-01-14 16:48:41 -0800112 // 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 Hunta7b6a6b2015-01-13 19:53:09 -0800124 }
125
Simon Hunt0541fb82015-01-14 18:59:57 -0800126 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 Huntcacce342015-01-07 16:13:05 -0800133 // --- Controller Definition -----------------------------------------
134
Simon Hunt6cc53692015-01-07 11:33:45 -0800135 angular.module('ovTopo', moduleDependencies)
136
137 .controller('OvTopoCtrl', [
Simon Hunt0541fb82015-01-14 18:59:57 -0800138 '$log',
Simon Hunt426bd862015-01-14 16:48:41 -0800139 'KeyService', 'ZoomService', 'GlyphService', 'MapService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800140
Simon Hunt0541fb82015-01-14 18:59:57 -0800141 function (_$log_, _ks_, _zs_, _gs_, _ms_) {
Simon Hunt6cc53692015-01-07 11:33:45 -0800142 var self = this;
Simon Hunt6cc53692015-01-07 11:33:45 -0800143 $log = _$log_;
144 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800145 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800146 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800147 ms = _ms_;
Simon Huntef31fb22014-12-19 13:16:44 -0800148
Simon Hunt426bd862015-01-14 16:48:41 -0800149 self.notifyResize = function () {
Simon Hunt0541fb82015-01-14 18:59:57 -0800150 svgResized(svg.style('width'), svg.style('height'));
Simon Hunt426bd862015-01-14 16:48:41 -0800151 };
Simon Huntef31fb22014-12-19 13:16:44 -0800152
Simon Huntcacce342015-01-07 16:13:05 -0800153 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800154 ovtopo = d3.select('#ov-topo');
155 svg = ovtopo.select('svg');
156
Simon Hunt6cc53692015-01-07 11:33:45 -0800157 setUpKeys();
158 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800159 setUpZoom();
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800160 setUpMap();
Simon Hunt6cc53692015-01-07 11:33:45 -0800161
162 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800163 }]);
164}());