blob: 09cdd43a1280444af9efc9e40d0d4f0c0e9e342d [file] [log] [blame]
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -08001/*
2 * Copyright 2015 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 -- Widget -- Button Service
19 */
20(function () {
21 'use strict';
22
Simon Hunt69252862015-02-26 11:26:08 -080023 // injected refs
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070024 var $log, fs, is, tts;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080025
Simon Hunt69252862015-02-26 11:26:08 -080026 // configuration
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080027 var btnSize = 25,
28 btnPadding = 4;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080029
Simon Hunt85fa2b82015-02-26 14:33:23 -080030
31 // === Helper Functions
32
33 function divExists(div, msg) {
34 if (!div) {
35 $log.warn('div undefined (' + msg + ')');
36 }
37 return !!div;
38 }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080039
40 function createDiv(div, cls, id) {
41 return div.append('div')
42 .classed(cls, true)
43 .attr('id', id);
44 }
45
Simon Hunt85fa2b82015-02-26 14:33:23 -080046 function noop() {}
47
48 function buttonWidth() {
49 return btnSize + 2 * btnPadding;
50 }
51
Simon Hunt85fa2b82015-02-26 14:33:23 -080052 // === BUTTON =================================================
53
54 // div is where to put the button (d3.selection of a DIV element)
55 // id should be globally unique
56 // gid is glyph ID (from Glyph Service)
57 // cb is callback function on click
58 // tooltip is text for tooltip
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080059 function button(div, id, gid, cb, tooltip) {
Simon Hunt85fa2b82015-02-26 14:33:23 -080060 if (!divExists(div, 'button')) return null;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080061
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080062 var btnDiv = createDiv(div, 'button', id),
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080063 cbFnc = fs.isF(cb) || noop;
64
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080065 is.loadIcon(btnDiv, gid, btnSize, true);
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070066 if (tooltip) { tts.addTooltip(btnDiv, tooltip); }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080067
68 btnDiv.on('click', cbFnc);
69
70 return {
71 id: id,
Simon Hunt85fa2b82015-02-26 14:33:23 -080072 width: buttonWidth
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080073 }
74 }
75
Simon Hunt85fa2b82015-02-26 14:33:23 -080076
77 // === TOGGLE BUTTON ==========================================
78
79 // div is where to put the button (d3.selection of a DIV element)
80 // id should be globally unique
81 // gid is glyph ID (from Glyph Service)
82 // initState is whether the toggle is on or not to begin
83 // cb is callback function on click
84 // tooltip is text for tooltip
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080085 function toggle(div, id, gid, initState, cb, tooltip) {
Simon Hunt85fa2b82015-02-26 14:33:23 -080086 if (!divExists(div, 'toggle button')) return null;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080087
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080088 var sel = !!initState,
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080089 togDiv = createDiv(div, 'toggleButton', id),
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080090 cbFnc = fs.isF(cb) || noop;
91
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080092 is.loadIcon(togDiv, gid, btnSize, true);
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080093 togDiv.classed('selected', sel);
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070094 if (tooltip) { tts.addTooltip(togDiv, tooltip); }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080095
Simon Hunt09060142015-03-18 20:23:32 -070096 function _toggle(b, nocb) {
Simon Hunt85fa2b82015-02-26 14:33:23 -080097 sel = (b === undefined) ? !sel : !!b;
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080098 togDiv.classed('selected', sel);
Simon Hunt09060142015-03-18 20:23:32 -070099 nocb || cbFnc(sel);
100 }
101
102 // toggle the button state without invoking the callback
103 function toggleNoCb() {
104 _toggle(undefined, true);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800105 }
106
107 togDiv.on('click', _toggle);
108
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800109 return {
110 id: id,
Simon Hunt85fa2b82015-02-26 14:33:23 -0800111 width: buttonWidth,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800112 selected: function () { return sel; },
Simon Hunt09060142015-03-18 20:23:32 -0700113 toggle: _toggle,
114 toggleNoCb: toggleNoCb
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800115 }
116 }
117
Simon Hunt85fa2b82015-02-26 14:33:23 -0800118
119 // === RADIO BUTTON SET =======================================
120
121
122 // div is where to put the button (d3.selection of a DIV element)
123 // id should be globally unique
124 // rset is an array of button descriptors of the following form:
125 // {
126 // gid: glyphId,
127 // tooltip: tooltipText,
128 // cb: callbackFunction
129 // }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800130 function radioSet(div, id, rset) {
Simon Hunt85fa2b82015-02-26 14:33:23 -0800131 if (!divExists(div, 'radio button set')) return null;
132
133 if (!fs.isA(rset) || !rset.length) {
134 $log.warn('invalid array (radio button set)');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800135 return null;
136 }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800137
138 var rDiv = createDiv(div, 'radioSet', id),
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800139 rads = [],
Simon Hunt85fa2b82015-02-26 14:33:23 -0800140 idxByKey = {},
141 currIdx = 0;
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800142
Simon Hunt85fa2b82015-02-26 14:33:23 -0800143 function rsetWidth() {
144 return ((btnSize + btnPadding) * rads.length) + btnPadding;
145 }
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800146
Simon Hunt85fa2b82015-02-26 14:33:23 -0800147 function rbclick() {
148 var id = d3.select(this).attr('id'),
149 m = /^.*-(\d+)$/.exec(id),
150 idx = Number(m[1]);
151
152 if (idx !== currIdx) {
153 rads[currIdx].el.classed('selected', false);
154 currIdx = idx;
155 rads[currIdx].el.classed('selected', true);
156 invokeCurrent();
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800157 }
158 }
159
Simon Hunt85fa2b82015-02-26 14:33:23 -0800160 // {
161 // gid: gid,
162 // tooltip: ..., (optional)
163 // key: ..., (optional)
164 // cb: cb
165 // id: ... (added by us)
166 // index: ... (added by us)
167 // }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800168
169 rset.forEach(function (btn, index) {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800170
Simon Hunt85fa2b82015-02-26 14:33:23 -0800171 if (!fs.isO(btn)) {
172 $log.warn('radio button descriptor at index ' + index +
173 ' not an object');
174 return;
175 }
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800176
Simon Hunt85fa2b82015-02-26 14:33:23 -0800177 var rid = id + '-' + index,
178 initSel = (index === 0),
179 rbdiv = createDiv(rDiv, 'radioButton', rid);
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800180
Simon Hunt85fa2b82015-02-26 14:33:23 -0800181 rbdiv.classed('selected', initSel);
182 rbdiv.on('click', rbclick);
183 is.loadIcon(rbdiv, btn.gid, btnSize, true);
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700184 if (btn.tooltip) { tts.addTooltip(rbdiv, btn.tooltip); }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800185 angular.extend(btn, {
186 el: rbdiv,
187 id: rid,
188 cb: fs.isF(btn.cb) || noop,
189 index: index
190 });
191
192 if (btn.key) {
193 idxByKey[btn.key] = index;
194 }
195
196 rads.push(btn);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800197 });
Simon Hunt85fa2b82015-02-26 14:33:23 -0800198
199
200 function invokeCurrent() {
201 var curr = rads[currIdx];
202 curr.cb(curr.index, curr.key);
203 }
204
205 function selected(x) {
206 var curr = rads[currIdx],
207 idx;
208
209 if (x === undefined) {
210 return curr.key || curr.index;
211 } else {
212 idx = idxByKey[x];
213 if (idx === undefined) {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800214 $log.warn('no radio button with key:', x);
Simon Hunt85fa2b82015-02-26 14:33:23 -0800215 } else {
216 selectedIndex(idx);
217 }
218 }
219 }
220
221 function selectedIndex(x) {
222 if (x === undefined) {
223 return currIdx;
224 } else {
225 if (x >= 0 && x < rads.length) {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800226 if (currIdx !== x) {
227 currIdx = x;
228 invokeCurrent();
229 } else {
230 $log.warn('current index already selected:', x);
231 }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800232 } else {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800233 $log.warn('invalid radio button index:', x);
Simon Hunt85fa2b82015-02-26 14:33:23 -0800234 }
235 }
236 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800237
238 return {
Simon Hunt85fa2b82015-02-26 14:33:23 -0800239 width: rsetWidth,
240 selected: selected,
241 selectedIndex: selectedIndex
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800242 }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800243 }
244
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800245
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800246 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800247 .factory('ButtonService',
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700248 ['$log', 'FnService', 'IconService', 'TooltipService',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800249
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700250 function (_$log_, _fs_, _is_, _tts_) {
Simon Hunt69252862015-02-26 11:26:08 -0800251 $log = _$log_;
252 fs = _fs_;
253 is = _is_;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700254 tts = _tts_;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800255
Simon Hunt69252862015-02-26 11:26:08 -0800256 return {
257 button: button,
258 toggle: toggle,
Simon Hunt85fa2b82015-02-26 14:33:23 -0800259 radioSet: radioSet
Simon Hunt69252862015-02-26 11:26:08 -0800260 };
261 }]);
262
263}());