blob: 407d3944cf713ef45d2b3e72af3f5f7a4fbe5781 [file] [log] [blame]
Thomas Vachuska26be4f32016-03-31 01:10:27 -07001/*
2 * Copyright 2016 Open Networking Laboratory
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 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,
62 tint: tintCheck.property('checked') ? 'on' : 'off'
63 };
64 setMap(p);
65 $log.debug('Dialog OK button clicked');
66 }
67
68 function dClose() {
69 $log.debug('Dialog Close button clicked (or Esc pressed)');
70 }
71
72 function selectMap() {
73 map = maps[this.options[this.selectedIndex].value];
74 $log.info('Selected map', map);
75 }
76
77 function createListContent() {
78 var content = tds.createDiv('map-list'),
79 form = content.append('form'),
80 current = currentMap();
81 map = maps[current.mapid];
82 mapItems = form.append('select').on('change', selectMap);
83 order.forEach(function (id) {
84 var m = maps[id];
85 mapItems.append('option')
86 .attr('value', m.id)
87 .attr('selected', m.id === current.mapid ? true : null)
88 .text(m.description);
89 });
90 var p = form.append('p');
91 tintCheck = p.append('input').attr('type', 'checkbox').attr('name', 'tint');
92 if (current.tint == 'on') {
93 tintCheck.attr('checked', 'true');
94 }
95 p.append('span').text('Enable map tint');
96 return content;
97 }
98
99 function handleMapResponse(data) {
100 $log.info('Got response', data);
101 order = data.order;
102 maps = data.maps;
103 tds.openDialog()
104 .setTitle('Select Map')
105 .addContent(createListContent())
106 .addOk(dOk, 'OK')
107 .addCancel(dClose, 'Close')
108 .bindKeys();
109 }
110
111 function toggleMap() {
112 delegate.toggleMap();
113 }
114
115 function currentMap() {
116 return delegate.currentMap();
117 }
118
119 function setMap(map) {
120 delegate.setMap(map);
121 }
122
123 // === -----------------------------------------------------
124 // === MODULE DEFINITION ===
125
126 angular.module('ovTopo')
127 .factory('TopoMapService',
128 ['$log', '$location', 'FnService', 'FlashService', 'WebSocketService',
129 'TopoDialogService',
130
131 function (_$log_, _$loc_, _fs_, _flash_, _wss_, _tds_) {
132 $log = _$log_;
133 $loc = _$loc_;
134 fs = _fs_;
135 flash = _flash_;
136 wss = _wss_;
137 tds = _tds_;
138
139 msgHandlers = {
140 mapSelectorResponse: handleMapResponse
141 };
142
143 return {
144 toggleMap: toggleMap,
145 currentMap: currentMap,
146 setMap: setMap,
147
148 openMapSelection: openMapSelection,
149 closeMapSelection: closeMapSelection,
150 start: start,
151 stop: stop
152 };
153 }]);
154
155}());