blob: 6b4ca28112308e5c11621b981251a4a88acfbc5d [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 Cole40be6b22015-02-19 17:12:23 -080024 var $log, fs, is;
25
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
52
53 // === BUTTON =================================================
54
55 // div is where to put the button (d3.selection of a DIV element)
56 // id should be globally unique
57 // gid is glyph ID (from Glyph Service)
58 // cb is callback function on click
59 // tooltip is text for tooltip
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080060 function button(div, id, gid, cb, tooltip) {
Simon Hunt85fa2b82015-02-26 14:33:23 -080061 if (!divExists(div, 'button')) return null;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080062
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080063 var btnDiv = createDiv(div, 'button', id),
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080064 cbFnc = fs.isF(cb) || noop;
65
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080066 is.loadIcon(btnDiv, gid, btnSize, true);
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 Cole40be6b22015-02-19 17:12:23 -080094
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080095 function _toggle(b) {
Simon Hunt85fa2b82015-02-26 14:33:23 -080096 sel = (b === undefined) ? !sel : !!b;
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080097 togDiv.classed('selected', sel);
Simon Hunt85fa2b82015-02-26 14:33:23 -080098 cbFnc(sel);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080099 }
100
101 togDiv.on('click', _toggle);
102
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800103 return {
104 id: id,
Simon Hunt85fa2b82015-02-26 14:33:23 -0800105 width: buttonWidth,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800106 selected: function () { return sel; },
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800107 toggle: _toggle
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800108 }
109 }
110
Simon Hunt85fa2b82015-02-26 14:33:23 -0800111
112 // === RADIO BUTTON SET =======================================
113
114
115 // div is where to put the button (d3.selection of a DIV element)
116 // id should be globally unique
117 // rset is an array of button descriptors of the following form:
118 // {
119 // gid: glyphId,
120 // tooltip: tooltipText,
121 // cb: callbackFunction
122 // }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800123 function radioSet(div, id, rset) {
Simon Hunt85fa2b82015-02-26 14:33:23 -0800124 if (!divExists(div, 'radio button set')) return null;
125
126 if (!fs.isA(rset) || !rset.length) {
127 $log.warn('invalid array (radio button set)');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800128 return null;
129 }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800130
131 var rDiv = createDiv(div, 'radioSet', id),
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800132 rads = [],
Simon Hunt85fa2b82015-02-26 14:33:23 -0800133 idxByKey = {},
134 currIdx = 0;
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800135
Simon Hunt85fa2b82015-02-26 14:33:23 -0800136 function rsetWidth() {
137 return ((btnSize + btnPadding) * rads.length) + btnPadding;
138 }
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800139
Simon Hunt85fa2b82015-02-26 14:33:23 -0800140 function rbclick() {
141 var id = d3.select(this).attr('id'),
142 m = /^.*-(\d+)$/.exec(id),
143 idx = Number(m[1]);
144
145 if (idx !== currIdx) {
146 rads[currIdx].el.classed('selected', false);
147 currIdx = idx;
148 rads[currIdx].el.classed('selected', true);
149 invokeCurrent();
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800150 }
151 }
152
Simon Hunt85fa2b82015-02-26 14:33:23 -0800153 // {
154 // gid: gid,
155 // tooltip: ..., (optional)
156 // key: ..., (optional)
157 // cb: cb
158 // id: ... (added by us)
159 // index: ... (added by us)
160 // }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800161
162 rset.forEach(function (btn, index) {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800163
Simon Hunt85fa2b82015-02-26 14:33:23 -0800164 if (!fs.isO(btn)) {
165 $log.warn('radio button descriptor at index ' + index +
166 ' not an object');
167 return;
168 }
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800169
Simon Hunt85fa2b82015-02-26 14:33:23 -0800170 var rid = id + '-' + index,
171 initSel = (index === 0),
172 rbdiv = createDiv(rDiv, 'radioButton', rid);
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800173
Simon Hunt85fa2b82015-02-26 14:33:23 -0800174 rbdiv.classed('selected', initSel);
175 rbdiv.on('click', rbclick);
176 is.loadIcon(rbdiv, btn.gid, btnSize, true);
177 angular.extend(btn, {
178 el: rbdiv,
179 id: rid,
180 cb: fs.isF(btn.cb) || noop,
181 index: index
182 });
183
184 if (btn.key) {
185 idxByKey[btn.key] = index;
186 }
187
188 rads.push(btn);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800189 });
Simon Hunt85fa2b82015-02-26 14:33:23 -0800190
191
192 function invokeCurrent() {
193 var curr = rads[currIdx];
194 curr.cb(curr.index, curr.key);
195 }
196
197 function selected(x) {
198 var curr = rads[currIdx],
199 idx;
200
201 if (x === undefined) {
202 return curr.key || curr.index;
203 } else {
204 idx = idxByKey[x];
205 if (idx === undefined) {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800206 $log.warn('no radio button with key:', x);
Simon Hunt85fa2b82015-02-26 14:33:23 -0800207 } else {
208 selectedIndex(idx);
209 }
210 }
211 }
212
213 function selectedIndex(x) {
214 if (x === undefined) {
215 return currIdx;
216 } else {
217 if (x >= 0 && x < rads.length) {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800218 if (currIdx !== x) {
219 currIdx = x;
220 invokeCurrent();
221 } else {
222 $log.warn('current index already selected:', x);
223 }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800224 } else {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800225 $log.warn('invalid radio button index:', x);
Simon Hunt85fa2b82015-02-26 14:33:23 -0800226 }
227 }
228 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800229
230 return {
Simon Hunt85fa2b82015-02-26 14:33:23 -0800231 width: rsetWidth,
232 selected: selected,
233 selectedIndex: selectedIndex
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800234 }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800235 }
236
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800237
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800238 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800239 .factory('ButtonService',
240 ['$log', 'FnService', 'IconService',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800241
Simon Hunt69252862015-02-26 11:26:08 -0800242 function (_$log_, _fs_, _is_) {
243 $log = _$log_;
244 fs = _fs_;
245 is = _is_;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800246
Simon Hunt69252862015-02-26 11:26:08 -0800247 return {
248 button: button,
249 toggle: toggle,
Simon Hunt85fa2b82015-02-26 14:33:23 -0800250 radioSet: radioSet
Simon Hunt69252862015-02-26 11:26:08 -0800251 };
252 }]);
253
254}());