blob: 2c952b81e3782822e6440bb81624c7de75bbfacd [file] [log] [blame]
Thomas Vachuska26be4f32016-03-31 01:10:27 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska26be4f32016-03-31 01:10:27 -07003 *
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 Map Module.
19 Defines behavior for loading geographical maps into the map layer.
20 */
21
22(function () {
23 'use strict';
24
25 // injected refs
26 var $log, $loc, fs, flash, wss, tds, delegate;
27
28 // constants
29 var mapRequest = 'mapSelectorRequest';
30
31 // internal state
32 var order, maps, map, mapItems, tintCheck, msgHandlers;
33
34 // === ---------------------------
35 // === Helper functions
36
37
38 // === ---------------------------
39 // === Main API functions
40
41 function openMapSelection() {
42 wss.sendEvent(mapRequest);
43 }
44
45 function closeMapSelection() {
46 tds.closeDialog();
47 }
48
49 function start(d) {
50 delegate = d;
51 wss.bindHandlers(msgHandlers);
52 }
53
54 function stop() {
55 wss.unbindHandlers(msgHandlers);
56 }
57
58 function dOk() {
59 var p = {
60 mapid: map.id,
61 mapscale: map.scale,
Steven Burrows3a9a6442016-05-05 15:31:16 +010062 mapfilepath: map.filePath,
Thomas Vachuska26be4f32016-03-31 01:10:27 -070063 tint: tintCheck.property('checked') ? 'on' : 'off'
64 };
65 setMap(p);
66 $log.debug('Dialog OK button clicked');
67 }
68
69 function dClose() {
70 $log.debug('Dialog Close button clicked (or Esc pressed)');
71 }
72
73 function selectMap() {
74 map = maps[this.options[this.selectedIndex].value];
75 $log.info('Selected map', map);
76 }
77
78 function createListContent() {
79 var content = tds.createDiv('map-list'),
80 form = content.append('form'),
81 current = currentMap();
82 map = maps[current.mapid];
83 mapItems = form.append('select').on('change', selectMap);
84 order.forEach(function (id) {
85 var m = maps[id];
86 mapItems.append('option')
87 .attr('value', m.id)
88 .attr('selected', m.id === current.mapid ? true : null)
89 .text(m.description);
90 });
91 var p = form.append('p');
92 tintCheck = p.append('input').attr('type', 'checkbox').attr('name', 'tint');
93 if (current.tint == 'on') {
94 tintCheck.attr('checked', 'true');
95 }
96 p.append('span').text('Enable map tint');
97 return content;
98 }
99
100 function handleMapResponse(data) {
101 $log.info('Got response', data);
102 order = data.order;
103 maps = data.maps;
104 tds.openDialog()
105 .setTitle('Select Map')
106 .addContent(createListContent())
107 .addOk(dOk, 'OK')
108 .addCancel(dClose, 'Close')
109 .bindKeys();
110 }
111
112 function toggleMap() {
113 delegate.toggleMap();
114 }
115
116 function currentMap() {
117 return delegate.currentMap();
118 }
119
120 function setMap(map) {
121 delegate.setMap(map);
122 }
123
124 // === -----------------------------------------------------
125 // === MODULE DEFINITION ===
126
127 angular.module('ovTopo')
128 .factory('TopoMapService',
129 ['$log', '$location', 'FnService', 'FlashService', 'WebSocketService',
130 'TopoDialogService',
131
132 function (_$log_, _$loc_, _fs_, _flash_, _wss_, _tds_) {
133 $log = _$log_;
134 $loc = _$loc_;
135 fs = _fs_;
136 flash = _flash_;
137 wss = _wss_;
138 tds = _tds_;
139
140 msgHandlers = {
141 mapSelectorResponse: handleMapResponse
142 };
143
144 return {
145 toggleMap: toggleMap,
146 currentMap: currentMap,
147 setMap: setMap,
148
149 openMapSelection: openMapSelection,
150 closeMapSelection: closeMapSelection,
151 start: start,
152 stop: stop
153 };
154 }]);
155
156}());