blob: c9980ee876e16d298e1a20769c037fe53b3a05b6 [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
Simon Hunt09060142015-03-18 20:23:32 -070095 function _toggle(b, nocb) {
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 Hunt09060142015-03-18 20:23:32 -070098 nocb || cbFnc(sel);
99 }
100
101 // toggle the button state without invoking the callback
102 function toggleNoCb() {
103 _toggle(undefined, true);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800104 }
105
106 togDiv.on('click', _toggle);
107
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800108 return {
109 id: id,
Simon Hunt85fa2b82015-02-26 14:33:23 -0800110 width: buttonWidth,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800111 selected: function () { return sel; },
Simon Hunt09060142015-03-18 20:23:32 -0700112 toggle: _toggle,
113 toggleNoCb: toggleNoCb
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800114 }
115 }
116
Simon Hunt85fa2b82015-02-26 14:33:23 -0800117
118 // === RADIO BUTTON SET =======================================
119
120
121 // div is where to put the button (d3.selection of a DIV element)
122 // id should be globally unique
123 // rset is an array of button descriptors of the following form:
124 // {
125 // gid: glyphId,
126 // tooltip: tooltipText,
127 // cb: callbackFunction
128 // }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800129 function radioSet(div, id, rset) {
Simon Hunt85fa2b82015-02-26 14:33:23 -0800130 if (!divExists(div, 'radio button set')) return null;
131
132 if (!fs.isA(rset) || !rset.length) {
133 $log.warn('invalid array (radio button set)');
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800134 return null;
135 }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800136
137 var rDiv = createDiv(div, 'radioSet', id),
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800138 rads = [],
Simon Hunt85fa2b82015-02-26 14:33:23 -0800139 idxByKey = {},
140 currIdx = 0;
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800141
Simon Hunt85fa2b82015-02-26 14:33:23 -0800142 function rsetWidth() {
143 return ((btnSize + btnPadding) * rads.length) + btnPadding;
144 }
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800145
Simon Hunt85fa2b82015-02-26 14:33:23 -0800146 function rbclick() {
147 var id = d3.select(this).attr('id'),
148 m = /^.*-(\d+)$/.exec(id),
149 idx = Number(m[1]);
150
151 if (idx !== currIdx) {
152 rads[currIdx].el.classed('selected', false);
153 currIdx = idx;
154 rads[currIdx].el.classed('selected', true);
155 invokeCurrent();
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800156 }
157 }
158
Simon Hunt85fa2b82015-02-26 14:33:23 -0800159 // {
160 // gid: gid,
161 // tooltip: ..., (optional)
162 // key: ..., (optional)
163 // cb: cb
164 // id: ... (added by us)
165 // index: ... (added by us)
166 // }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800167
168 rset.forEach(function (btn, index) {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800169
Simon Hunt85fa2b82015-02-26 14:33:23 -0800170 if (!fs.isO(btn)) {
171 $log.warn('radio button descriptor at index ' + index +
172 ' not an object');
173 return;
174 }
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800175
Simon Hunt85fa2b82015-02-26 14:33:23 -0800176 var rid = id + '-' + index,
177 initSel = (index === 0),
178 rbdiv = createDiv(rDiv, 'radioButton', rid);
Bri Prebilic Cole664d4702015-02-25 12:16:14 -0800179
Simon Hunt85fa2b82015-02-26 14:33:23 -0800180 rbdiv.classed('selected', initSel);
181 rbdiv.on('click', rbclick);
182 is.loadIcon(rbdiv, btn.gid, btnSize, true);
183 angular.extend(btn, {
184 el: rbdiv,
185 id: rid,
186 cb: fs.isF(btn.cb) || noop,
187 index: index
188 });
189
190 if (btn.key) {
191 idxByKey[btn.key] = index;
192 }
193
194 rads.push(btn);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800195 });
Simon Hunt85fa2b82015-02-26 14:33:23 -0800196
197
198 function invokeCurrent() {
199 var curr = rads[currIdx];
200 curr.cb(curr.index, curr.key);
201 }
202
203 function selected(x) {
204 var curr = rads[currIdx],
205 idx;
206
207 if (x === undefined) {
208 return curr.key || curr.index;
209 } else {
210 idx = idxByKey[x];
211 if (idx === undefined) {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800212 $log.warn('no radio button with key:', x);
Simon Hunt85fa2b82015-02-26 14:33:23 -0800213 } else {
214 selectedIndex(idx);
215 }
216 }
217 }
218
219 function selectedIndex(x) {
220 if (x === undefined) {
221 return currIdx;
222 } else {
223 if (x >= 0 && x < rads.length) {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800224 if (currIdx !== x) {
225 currIdx = x;
226 invokeCurrent();
227 } else {
228 $log.warn('current index already selected:', x);
229 }
Simon Hunt85fa2b82015-02-26 14:33:23 -0800230 } else {
Bri Prebilic Cole18489172015-02-27 17:12:00 -0800231 $log.warn('invalid radio button index:', x);
Simon Hunt85fa2b82015-02-26 14:33:23 -0800232 }
233 }
234 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800235
236 return {
Simon Hunt85fa2b82015-02-26 14:33:23 -0800237 width: rsetWidth,
238 selected: selected,
239 selectedIndex: selectedIndex
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800240 }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800241 }
242
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800243
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800244 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800245 .factory('ButtonService',
246 ['$log', 'FnService', 'IconService',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800247
Simon Hunt69252862015-02-26 11:26:08 -0800248 function (_$log_, _fs_, _is_) {
249 $log = _$log_;
250 fs = _fs_;
251 is = _is_;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800252
Simon Hunt69252862015-02-26 11:26:08 -0800253 return {
254 button: button,
255 toggle: toggle,
Simon Hunt85fa2b82015-02-26 14:33:23 -0800256 radioSet: radioSet
Simon Hunt69252862015-02-26 11:26:08 -0800257 };
258 }]);
259
260}());