blob: a22e7ba095958bacad67c946b6a1e78432b95ec4 [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 Hunta11b4eb2015-01-28 16:20:50 -080031 var $log, fs, ks, zs, gs, ms, ps, tes, tfs;
Simon Hunt6cc53692015-01-07 11:33:45 -080032
33 // DOM elements
Simon Hunt737c89f2015-01-28 12:23:19 -080034 var ovtopo, svg, defs, zoomLayer, mapG, forceG;
Simon Hunt6cc53692015-01-07 11:33:45 -080035
36 // Internal state
Simon Huntfd1231a2015-01-26 22:14:51 -080037 var zoomer, evDispatcher;
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..
104 function svgResized(w, h) {
Simon Hunta11b4eb2015-01-28 16:20:50 -0800105 $log.debug('TopoView just resized... ' + w + 'x' + h);
106 tfs.resize(w, h);
Simon Hunt0541fb82015-01-14 18:59:57 -0800107 }
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 ];
Simon Hunt737c89f2015-01-28 12:23:19 -0800116 mapG.selectAll('circle')
Simon Hunt426bd862015-01-14 16:48:41 -0800117 .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() {
Simon Hunt737c89f2015-01-28 12:23:19 -0800127 mapG = zoomLayer.append('g').attr('id', 'topo-map');
Simon Hunt0541fb82015-01-14 18:59:57 -0800128 //ms.loadMapInto(map, '*continental_us', {mapFillScale:0.5});
Simon Hunt737c89f2015-01-28 12:23:19 -0800129 ms.loadMapInto(mapG, '*continental_us');
Simon Hunt0541fb82015-01-14 18:59:57 -0800130 //showCallibrationPoints();
131 }
132
Simon Hunt737c89f2015-01-28 12:23:19 -0800133 // --- Force Layout --------------------------------------------------
134
135 function setUpForce() {
136 forceG = zoomLayer.append('g').attr('id', 'topo-force');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800137 tfs.initForce(forceG, svg.attr('width'), svg.attr('height'));
Simon Hunt737c89f2015-01-28 12:23:19 -0800138 }
139
Simon Hunt1e4a0012015-01-21 11:36:08 -0800140
Simon Huntcacce342015-01-07 16:13:05 -0800141 // --- Controller Definition -----------------------------------------
142
Simon Hunt6cc53692015-01-07 11:33:45 -0800143 angular.module('ovTopo', moduleDependencies)
144
145 .controller('OvTopoCtrl', [
Simon Hunt54442fa2015-01-26 14:17:38 -0800146 '$scope', '$log', '$location', '$timeout',
Simon Hunta11b4eb2015-01-28 16:20:50 -0800147 'FnService', 'MastService',
Simon Hunt426bd862015-01-14 16:48:41 -0800148 'KeyService', 'ZoomService', 'GlyphService', 'MapService',
Simon Hunt737c89f2015-01-28 12:23:19 -0800149 'PanelService', 'TopoEventService', 'TopoForceService',
Simon Hunt6cc53692015-01-07 11:33:45 -0800150
Simon Hunta11b4eb2015-01-28 16:20:50 -0800151 function ($scope, _$log_, $loc, $timeout, _fs_, mast,
Simon Hunt737c89f2015-01-28 12:23:19 -0800152 _ks_, _zs_, _gs_, _ms_, _ps_, _tes_, _tfs_) {
Simon Hunt6cc53692015-01-07 11:33:45 -0800153 var self = this;
Simon Hunt6cc53692015-01-07 11:33:45 -0800154 $log = _$log_;
Simon Hunta11b4eb2015-01-28 16:20:50 -0800155 fs = _fs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800156 ks = _ks_;
Simon Huntcacce342015-01-07 16:13:05 -0800157 zs = _zs_;
Simon Hunt6cc53692015-01-07 11:33:45 -0800158 gs = _gs_;
Simon Hunt1e8eff42015-01-08 17:19:02 -0800159 ms = _ms_;
Simon Hunt54442fa2015-01-26 14:17:38 -0800160 ps = _ps_;
Simon Huntbb596362015-01-26 21:27:42 -0800161 tes = _tes_;
Simon Hunt737c89f2015-01-28 12:23:19 -0800162 tfs = _tfs_;
Simon Huntef31fb22014-12-19 13:16:44 -0800163
Simon Hunt426bd862015-01-14 16:48:41 -0800164 self.notifyResize = function () {
Simon Hunta11b4eb2015-01-28 16:20:50 -0800165 svgResized(svg.attr('width'), svg.attr('height'));
Simon Hunt426bd862015-01-14 16:48:41 -0800166 };
Simon Huntef31fb22014-12-19 13:16:44 -0800167
Simon Hunt54442fa2015-01-26 14:17:38 -0800168 // Cleanup on destroyed scope..
Simon Hunt584122a2015-01-21 15:32:40 -0800169 $scope.$on('$destroy', function () {
170 $log.log('OvTopoCtrl is saying Buh-Bye!');
Simon Huntfd1231a2015-01-26 22:14:51 -0800171 tes.closeSock();
Simon Hunt54442fa2015-01-26 14:17:38 -0800172 ps.destroyPanel('topo-p-summary');
Simon Hunt584122a2015-01-21 15:32:40 -0800173 });
174
Simon Huntcacce342015-01-07 16:13:05 -0800175 // svg layer and initialization of components
Simon Hunt426bd862015-01-14 16:48:41 -0800176 ovtopo = d3.select('#ov-topo');
177 svg = ovtopo.select('svg');
Simon Hunta11b4eb2015-01-28 16:20:50 -0800178 // set the svg size to match that of the window, less the masthead
179 svg.attr(fs.windowSize(mast.mastHeight()));
Simon Hunt426bd862015-01-14 16:48:41 -0800180
Simon Huntfd1231a2015-01-26 22:14:51 -0800181 // bind to topo event dispatcher..
182 evDispatcher = tes.bindDispatcher('TODO: topo-DOM-elements-here');
Simon Huntbb596362015-01-26 21:27:42 -0800183
Simon Hunt6cc53692015-01-07 11:33:45 -0800184 setUpKeys();
185 setUpDefs();
Simon Huntcacce342015-01-07 16:13:05 -0800186 setUpZoom();
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800187 setUpMap();
Simon Hunt737c89f2015-01-28 12:23:19 -0800188 setUpForce();
Simon Huntfd1231a2015-01-26 22:14:51 -0800189
190 // open up a connection to the server...
191 tes.openSock();
Simon Hunt6cc53692015-01-07 11:33:45 -0800192
Simon Hunt54442fa2015-01-26 14:17:38 -0800193 // TODO: remove this temporary code....
194 var p = ps.createPanel('topo-p-summary');
195 p.append('h1').text('Hello World');
196 p.show();
197 $timeout(function () { p.hide(); }, 2000);
198
Simon Hunt6cc53692015-01-07 11:33:45 -0800199 $log.log('OvTopoCtrl has been created');
Simon Huntef31fb22014-12-19 13:16:44 -0800200 }]);
201}());