blob: 28b79ace064417ce3c8c4acab3a3e13c2903fd95 [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
Simon Huntef31fb22014-12-19 13:16:44 -080019 */
20
21(function () {
22 'use strict';
Simon Hunt6cc53692015-01-07 11:33:45 -080023
24 var moduleDependencies = [
25 'onosUtil',
Simon Hunt1e4a0012015-01-21 11:36:08 -080026 'onosSvg',
27 'onosRemote'
Simon Hunt6cc53692015-01-07 11:33:45 -080028 ];
29
30 // references to injected services etc.
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080031 var $log, fs, ks, zs, gs, ms, sus, tfs;
Simon Hunt6cc53692015-01-07 11:33:45 -080032
33 // DOM elements
Simon Hunt7c8ab8d2015-02-03 15:05:15 -080034 var ovtopo, svg, defs, zoomLayer, mapG, forceG, noDevsLayer;
Simon Hunt6cc53692015-01-07 11:33:45 -080035
36 // Internal state
Simon Huntedf5c0e2015-01-29 15:00:53 -080037 var zoomer;
Simon Hunt6cc53692015-01-07 11:33:45 -080038
39 // Note: "exported" state should be properties on 'self' variable
40
Simon Huntcacce342015-01-07 16:13:05 -080041 // --- Short Cut Keys ------------------------------------------------
42
Simon Hunt6cc53692015-01-07 11:33:45 -080043 var keyBindings = {
Simon Huntcacce342015-01-07 16:13:05 -080044 W: [logWarning, '(temp) log a warning'],
45 E: [logError, '(temp) log an error'],
46 R: [resetZoom, 'Reset pan / zoom']
Simon Hunt6cc53692015-01-07 11:33:45 -080047 };
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 Huntcacce342015-01-07 16:13:05 -080059 function resetZoom() {
60 zoomer.reset();
61 }
62
Simon Hunt6cc53692015-01-07 11:33:45 -080063 function setUpKeys() {
64 ks.keyBindings(keyBindings);
65 }
66
Simon Huntcacce342015-01-07 16:13:05 -080067
68 // --- Glyphs, Icons, and the like -----------------------------------
69
Simon Hunt6cc53692015-01-07 11:33:45 -080070 function setUpDefs() {
Simon Huntcacce342015-01-07 16:13:05 -080071 defs = svg.append('defs');
Simon Hunt6cc53692015-01-07 11:33:45 -080072 gs.loadDefs(defs);
73 }
74
75
Simon Huntcacce342015-01-07 16:13:05 -080076 // --- 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 Hunt0541fb82015-01-14 18:59:57 -080088 // keep the map lines constant width while zooming
Simon Hunt737c89f2015-01-28 12:23:19 -080089 mapG.style('stroke-width', (2.0 / sc) + 'px');
Simon Huntcacce342015-01-07 16:13:05 -080090 }
91
92 function setUpZoom() {
Simon Hunta7b6a6b2015-01-13 19:53:09 -080093 zoomLayer = svg.append('g').attr('id', 'topo-zoomlayer');
Simon Huntcacce342015-01-07 16:13:05 -080094 zoomer = zs.createZoomer({
95 svg: svg,
96 zoomLayer: zoomLayer,
97 zoomEnabled: zoomEnabled,
98 zoomCallback: zoomCallback
99 });
100 }
101
102
Simon Hunt0541fb82015-01-14 18:59:57 -0800103 // callback invoked when the SVG view has been resized..
Simon Huntb0ec1e52015-01-28 18:13:49 -0800104 function svgResized(dim) {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800105 tfs.resize(dim);
Simon Hunt0541fb82015-01-14 18:59:57 -0800106 }
107
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800108 // --- Background Map ------------------------------------------------
109
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800110 function setUpNoDevs() {
111 var g, box;
112 noDevsLayer = svg.append('g').attr({
113 id: 'topo-noDevsLayer',
114 transform: sus.translate(500,500)
115 });
116 // Note, SVG viewbox is '0 0 1000 1000', defined in topo.html.
117 // We are translating this layer to have its origin at the center
118
119 g = noDevsLayer.append('g');
120 gs.addGlyph(g, 'bird', 100).attr('class', 'noDevsBird');
121 g.append('text').text('No devices are connected')
122 .attr({ x: 120, y: 80});
123
124 box = g.node().getBBox();
125 box.x -= box.width/2;
126 box.y -= box.height/2;
127 g.attr('transform', sus.translate(box.x, box.y));
128
129 showNoDevs(true);
130 }
131
132 function showNoDevs(b) {
133 sus.makeVisible(noDevsLayer, b);
134 }
135
Simon Hunt0541fb82015-01-14 18:59:57 -0800136 function showCallibrationPoints() {
Simon Hunt426bd862015-01-14 16:48:41 -0800137 // temp code for calibration
138 var points = [
139 [0, 0], [0, 1000], [1000, 0], [1000, 1000]
140 ];
Simon Hunt737c89f2015-01-28 12:23:19 -0800141 mapG.selectAll('circle')
Simon Hunt426bd862015-01-14 16:48:41 -0800142 .data(points)
143 .enter()
144 .append('circle')
145 .attr('cx', function (d) { return d[0]; })
146 .attr('cy', function (d) { return d[1]; })
147 .attr('r', 5)
148 .style('fill', 'red');
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800149 }
150
Simon Hunt0541fb82015-01-14 18:59:57 -0800151 function setUpMap() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800152 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Hunt0541fb82015-01-14 18:59:57 -0800153 //ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5});
Simon Hunt737c89f2015-01-28 12:23:19 -0800154 ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800155 //showCallibrationPoints();
156 }
157
Simon Hunt737c89f2015-01-28 12:23:19 -0800158 // --- Force Layout --------------------------------------------------
159
160 function setUpForce() {
161 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800162 tfs.initForce(forceG, svg.attr('width'), svg.attr('height'));
Simon Hunt737c89f2015-01-28 12:23:19 -0800163 }
164
Simon Huntcacce342015-01-07 16:13:05 -0800165 // --- Controller Definition -----------------------------------------
166
Simon Hunt6cc53692015-01-07 11:33:45 -0800167 angular.module('ovTopo', moduleDependencies)
168
169 .controller('OvTopoCtrl', [
Simon Hunt54442fa2015-01-26 14:17:38 -0800170 '$scope', '$log', '$location', '$timeout',
Simon Hunt4b668592015-01-29 17:33:53 -0800171 'FnService', 'MastService', 'KeyService', 'ZoomService',
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800172 'GlyphService', 'MapService', 'SvgUtilService',
Simon Huntb0ec1e52015-01-28 18:13:49 -0800173 'TopoEventService', 'TopoForceService', 'TopoPanelService',
Simon Hunt4b668592015-01-29 17:33:53 -0800174 'TopoInstService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800175
Simon Hunta11b4eb2015-01-28 16:20:50 -0800176 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800177 _ks_, _zs_, _gs_, _ms_, _sus_, tes, _tfs_, tps, tis) {
Simon Hunt6cc53692015-01-07 11:33:45 -0800178 var self = this;
Simon Hunt6cc53692015-01-07 11:33:45 -0800179 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800180 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800181 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800182 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800183 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800184 ms = _ms_;
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800185 sus = _sus_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800186 tfs = _tfs_;
Simon Huntef31fb22014-12-19 13:16:44 -0800187
Simon Hunt426bd862015-01-14 16:48:41 -0800188 self.notifyResize = function () {
Simon Huntb0ec1e52015-01-28 18:13:49 -0800189 svgResized(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800190 };
Simon Huntef31fb22014-12-19 13:16:44 -0800191
Simon Hunt54442fa2015-01-26 14:17:38 -0800192 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800193 $scope.$on('$destroy', function () {
194 $log.log('OvTopoCtrl is saying Buh-Bye!');
Simon Huntfd1231a2015-01-26 22:14:51 -0800195 tes.closeSock();
Simon Hunt626d2102015-01-29 11:54:50 -0800196 tps.destroyPanels();
Simon Hunt4b668592015-01-29 17:33:53 -0800197 tis.destroyInst();
Simon Hunt584122a2015-01-21 15:32:40 -0800198 });
199
Simon Huntcacce342015-01-07 16:13:05 -0800200 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800201 ovtopo = d3.select('#ov-topo');
202 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800203 // set the svg size to match that of the window, less the masthead
204 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800205
Simon Hunt6cc53692015-01-07 11:33:45 -0800206 setUpKeys();
207 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800208 setUpZoom();
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800209 setUpNoDevs();
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800210 setUpMap();
Simon Hunt737c89f2015-01-28 12:23:19 -0800211 setUpForce();
Simon Huntfd1231a2015-01-26 22:14:51 -0800212
Simon Hunt4b668592015-01-29 17:33:53 -0800213 tis.initInst();
Simon Hunt626d2102015-01-29 11:54:50 -0800214 tps.initPanels();
Simon Huntfd1231a2015-01-26 22:14:51 -0800215 tes.openSock();
Simon Hunt6cc53692015-01-07 11:33:45 -0800216
217 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800218 }]);
219}());