Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 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 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 18 | (function () { |
Simon Hunt | e6f6461 | 2017-04-28 00:01:48 -0700 | [diff] [blame] | 19 | 'use strict'; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 20 | |
| 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 Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 27 | O: { id: 'topo2-summary-tog', gid: 'm_summary', isel: true }, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 28 | I: { id: 'topo2-instance-tog', gid: 'm_uiAttached', isel: true }, |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 29 | 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 Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 32 | P: { id: 'topo2-ports-tog', gid: 'm_ports', isel: true }, |
Steven Burrows | 9f7a2d7 | 2017-03-29 13:59:07 +0100 | [diff] [blame] | 33 | B: { id: 'topo2-bkgrnd-tog', gid: 'm_map', isel: true }, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 34 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 35 | // 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 Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 39 | E: { id: 'topo2-eqMaster-btn', gid: 'm_eqMaster' }, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | angular.module('ovTopo2') |
| 43 | .factory('Topo2ToolbarService', [ |
| 44 | 'FnService', 'ToolbarService', 'Topo2KeyCommandService', |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 45 | 'Topo2OverlayService', |
| 46 | function (fs, tbs, t2kcs, t2ov) { |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 47 | |
| 48 | var Toolbar = function () { |
| 49 | instance = this; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | Toolbar.prototype = { |
| 53 | |
| 54 | className: 'topo2-toolbar', |
| 55 | |
| 56 | init: function () { |
Steven Burrows | 9053fdb | 2017-04-12 12:08:16 -0700 | [diff] [blame] | 57 | this.el = tbs.createToolbar(this.className); |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 58 | this.radioSet; |
| 59 | this.ovIndex; |
| 60 | |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 61 | this.initKeyData(); |
| 62 | this.addFirstRow(); |
| 63 | this.el.addRow(); |
| 64 | this.addSecondRow(); |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 65 | this.addOverlays(); |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 66 | this.el.hide(); |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 67 | }, |
| 68 | initKeyData: function () { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 69 | _.each(k2b, function (value, key) { |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 70 | var data = t2kcs.getActionEntry(key); |
| 71 | if (data) { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 72 | // on-click callback |
| 73 | value.cb = data[0]; |
| 74 | // tooltip |
| 75 | value.tt = data[1] + ' (' + key + ')'; |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 76 | } |
| 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 Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 90 | var v = this.getKey(key); |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 91 | v.btn = this.el.addButton(v.id, v.gid, v.cb, v.tt); |
| 92 | }, |
| 93 | addToggle: function (key, suppressIfMobile) { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 94 | var v = this.getKey(key); |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 95 | if (suppressIfMobile && fs.isMobile()) { return; } |
| 96 | v.tog = this.el.addToggle(v.id, v.gid, v.isel, v.cb, v.tt); |
| 97 | }, |
| 98 | |
Simon Hunt | 47f8fb7 | 2017-04-11 13:49:31 -0700 | [diff] [blame] | 99 | toggle: function () { |
| 100 | this.el.toggle(); |
| 101 | }, |
| 102 | |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 103 | 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 Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 119 | addFirstRow: function () { |
| 120 | this.addToggle('I'); |
| 121 | this.addToggle('O'); |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 122 | this.addToggle('D'); |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 123 | this.el.addSeparator(); |
| 124 | |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 125 | this.addToggle('H'); |
| 126 | this.addToggle('M'); |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 127 | this.addToggle('P', true); |
| 128 | this.addToggle('B'); |
| 129 | }, |
| 130 | addSecondRow: function () { |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 131 | // this.addButton('N'); |
| 132 | this.addButton('L'); |
| 133 | this.addButton('R'); |
| 134 | this.el.addSeparator(); |
| 135 | this.addButton('E'); |
| 136 | }, |
| 137 | |
Steven Burrows | 58bdbdd | 2018-02-13 12:56:43 +0000 | [diff] [blame] | 138 | 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 Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 160 | 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 Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 164 | }, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | return instance || new Toolbar(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 168 | }, |
Steven Burrows | c846893 | 2017-03-17 16:13:41 +0000 | [diff] [blame] | 169 | ]); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 170 | }()); |