blob: c9e5285dd45cffda9db6eba40a470388b18a661d [file] [log] [blame]
Simon Hunte6f64612017-04-28 00:01:48 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Simon Hunte6f64612017-04-28 00:01:48 -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
Steven Burrowsc8468932017-03-17 16:13:41 +000018(function () {
Simon Hunte6f64612017-04-28 00:01:48 -070019 'use strict';
Steven Burrowsc8468932017-03-17 16:13:41 +000020
21 var instance;
22
23 // TODO: Commented k2b map and addToggles are not implement in Topo2 yet.
24
25 // key to button mapping data
26 var k2b = {
Steven Burrows1c2a9682017-07-14 16:52:46 +010027 O: { id: 'topo2-summary-tog', gid: 'm_summary', isel: true },
Steven Burrowsc8468932017-03-17 16:13:41 +000028 I: { id: 'topo2-instance-tog', gid: 'm_uiAttached', isel: true },
Steven Burrows58bdbdd2018-02-13 12:56:43 +000029 D: { id: 'details-tog', gid: 'm_details', isel: true },
30 H: { id: 'hosts-tog', gid: 'm_endstation', isel: false },
31 M: { id: 'offline-tog', gid: 'm_switch', isel: true },
Steven Burrowsc8468932017-03-17 16:13:41 +000032 P: { id: 'topo2-ports-tog', gid: 'm_ports', isel: true },
Steven Burrows9f7a2d72017-03-29 13:59:07 +010033 B: { id: 'topo2-bkgrnd-tog', gid: 'm_map', isel: true },
Steven Burrowsc8468932017-03-17 16:13:41 +000034
Steven Burrowsc8468932017-03-17 16:13:41 +000035 // N: { id: 'filters-btn', gid: 'm_filters' },
36 L: { id: 'topo2-cycleLabels-btn', gid: 'm_cycleLabels' },
37 R: { id: 'topo2-resetZoom-btn', gid: 'm_resetZoom' },
38
Steven Burrows1c2a9682017-07-14 16:52:46 +010039 E: { id: 'topo2-eqMaster-btn', gid: 'm_eqMaster' },
Steven Burrowsc8468932017-03-17 16:13:41 +000040 };
41
42 angular.module('ovTopo2')
43 .factory('Topo2ToolbarService', [
44 'FnService', 'ToolbarService', 'Topo2KeyCommandService',
Steven Burrows58bdbdd2018-02-13 12:56:43 +000045 'Topo2OverlayService',
46 function (fs, tbs, t2kcs, t2ov) {
Steven Burrowsc8468932017-03-17 16:13:41 +000047
48 var Toolbar = function () {
49 instance = this;
Steven Burrowsc8468932017-03-17 16:13:41 +000050 };
51
52 Toolbar.prototype = {
53
54 className: 'topo2-toolbar',
55
56 init: function () {
Steven Burrows9053fdb2017-04-12 12:08:16 -070057 this.el = tbs.createToolbar(this.className);
Steven Burrows58bdbdd2018-02-13 12:56:43 +000058 this.radioSet;
59 this.ovIndex;
60
Steven Burrowsc8468932017-03-17 16:13:41 +000061 this.initKeyData();
62 this.addFirstRow();
63 this.el.addRow();
64 this.addSecondRow();
Steven Burrows58bdbdd2018-02-13 12:56:43 +000065 this.addOverlays();
Simon Hunt47f8fb72017-04-11 13:49:31 -070066 this.el.hide();
Steven Burrowsc8468932017-03-17 16:13:41 +000067 },
68 initKeyData: function () {
Steven Burrows1c2a9682017-07-14 16:52:46 +010069 _.each(k2b, function (value, key) {
Steven Burrowsc8468932017-03-17 16:13:41 +000070 var data = t2kcs.getActionEntry(key);
71 if (data) {
Steven Burrows1c2a9682017-07-14 16:52:46 +010072 // on-click callback
73 value.cb = data[0];
74 // tooltip
75 value.tt = data[1] + ' (' + key + ')';
Steven Burrowsc8468932017-03-17 16:13:41 +000076 }
77 });
78 },
79 getKey: function (key) {
80 return k2b[key];
81 },
82 keyListener: function (key) {
83 var v = this.getKey(key);
84
85 if (v && v.tog) {
86 v.tog.toggleNoCb();
87 }
88 },
89 addButton: function (key) {
Steven Burrows1c2a9682017-07-14 16:52:46 +010090 var v = this.getKey(key);
Steven Burrowsc8468932017-03-17 16:13:41 +000091 v.btn = this.el.addButton(v.id, v.gid, v.cb, v.tt);
92 },
93 addToggle: function (key, suppressIfMobile) {
Steven Burrows1c2a9682017-07-14 16:52:46 +010094 var v = this.getKey(key);
Steven Burrowsc8468932017-03-17 16:13:41 +000095 if (suppressIfMobile && fs.isMobile()) { return; }
96 v.tog = this.el.addToggle(v.id, v.gid, v.isel, v.cb, v.tt);
97 },
98
Simon Hunt47f8fb72017-04-11 13:49:31 -070099 toggle: function () {
100 this.el.toggle();
101 },
102
Steven Burrows58bdbdd2018-02-13 12:56:43 +0000103 selectOverlay: function (ovid) {
104 var index = this.ovIndex[defaultOverlay] || 0,
105 pidx = (ovid === null) ? 0 : this.ovIndex[ovid] || -1;
106 if (pidx >= 0 && pidx < this.radioSet.size()) {
107 index = pidx;
108 }
109
110 this.radioSet.selectedIndex(index);
111 },
112
113 fnKey: function (index) {
114 if (index < this.radioSet.size() && index !== this.radioSet.selectedIndex()) {
115 this.radioSet.selectedIndex(index);
116 }
117 },
118
Steven Burrowsc8468932017-03-17 16:13:41 +0000119 addFirstRow: function () {
120 this.addToggle('I');
121 this.addToggle('O');
Steven Burrows58bdbdd2018-02-13 12:56:43 +0000122 this.addToggle('D');
Steven Burrowsc8468932017-03-17 16:13:41 +0000123 this.el.addSeparator();
124
Steven Burrows58bdbdd2018-02-13 12:56:43 +0000125 this.addToggle('H');
126 this.addToggle('M');
Steven Burrowsc8468932017-03-17 16:13:41 +0000127 this.addToggle('P', true);
128 this.addToggle('B');
129 },
130 addSecondRow: function () {
Steven Burrowsc8468932017-03-17 16:13:41 +0000131 // this.addButton('N');
132 this.addButton('L');
133 this.addButton('R');
134 this.el.addSeparator();
135 this.addButton('E');
136 },
137
Steven Burrows58bdbdd2018-02-13 12:56:43 +0000138 switchOverlayActions: function () {
139 // TODO: see TopoToolbar.js
140 // NOTE: Should add overlay buttons on the third row
141 },
142
143 addOverlays: function () {
144 var _this = this; // Keep context in callback
145 this.el.addSeparator();
146
147 // generate radio button set for overlays; start with 'none'
148 var rset = [{
149 gid: 'm_unknown',
150 tooltip: 'ov_tt_none',
151 cb: function () {
152 t2ov.tbSelection(null, _this.switchOverlayActions);
153 },
154 }];
155
156 t2ov.augmentRbset(rset, this.switchOverlayActions);
157 this.radioSet = this.el.addRadioSet('topo-overlays', rset);
158 },
159
Steven Burrowsc8468932017-03-17 16:13:41 +0000160 destroy: function () {
161 // TODO: Should the tbs remove button id's in the destroyToolbar method?
162 // If you go topo2 -> topo -> topo2 there's a button id conflict
163 tbs.destroyToolbar(this.className);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100164 },
Steven Burrowsc8468932017-03-17 16:13:41 +0000165 };
166
167 return instance || new Toolbar();
Steven Burrows1c2a9682017-07-14 16:52:46 +0100168 },
Steven Burrowsc8468932017-03-17 16:13:41 +0000169 ]);
Steven Burrows1c2a9682017-07-14 16:52:46 +0100170}());