blob: 445c1075b14c572e7b6d2aafd1189b6c646d21a5 [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
23 var $log, fs, is;
24
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080025 var btnSize = 25;
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080026
27 function noop() {}
28
29 function createDiv(div, cls, id) {
30 return div.append('div')
31 .classed(cls, true)
32 .attr('id', id);
33 }
34
35 function button(div, id, gid, cb, tooltip) {
Bri Prebilic Colec4403322015-02-23 10:29:22 -080036 if (!div) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080037 $log.warn('Button cannot append to div');
38 return null;
39 }
40
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080041 var btnDiv = createDiv(div, 'button', id),
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080042 cbFnc = fs.isF(cb) || noop;
43
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080044 is.loadIcon(btnDiv, gid, btnSize, true);
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080045
46 btnDiv.on('click', cbFnc);
47
48 return {
49 id: id,
50 click: cbFnc,
51 el: btnDiv
52 }
53 }
54
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080055 function toggle(div, id, gid, initState, cb, tooltip) {
Bri Prebilic Colec4403322015-02-23 10:29:22 -080056 if (!div) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080057 $log.warn('Toggle cannot append to div');
58 return null;
59 }
60
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080061 var sel = !!initState,
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080062 togDiv = createDiv(div, 'toggleButton', 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(togDiv, gid, btnSize, true);
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080066 togDiv.classed('selected', sel);
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080067
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080068 function _toggle(b) {
69 if (b === undefined) {
70 sel = !sel;
71 } else {
72 sel = !!b;
73 }
74 cbFnc(sel);
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080075 togDiv.classed('selected', sel);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080076 }
77
78 togDiv.on('click', _toggle);
79
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080080 return {
81 id: id,
82 el: togDiv,
83 selected: function () { return sel; },
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080084 toggle: _toggle
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080085 }
86 }
87
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080088 // TODO: fix radio button on click selecting functionality
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080089 function radioSet(div, id, rset) {
Bri Prebilic Colec4403322015-02-23 10:29:22 -080090 if (!div) {
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080091 $log.warn('Radio buttons cannot append to div');
92 return null;
93 }
94 if (!fs.isA(rset)) {
95 $log.warn('Radio button set is not an array');
96 return null;
97 }
98 if (rset.length === 0) {
99 $log.warn('Cannot create radio button set from empty array');
100 return null;
101 }
Bri Prebilic Colef7280d02015-02-24 16:20:47 -0800102 var rDiv = div.append('div').classed('radioSet', true),
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800103 sel = 0,
104 rads = [];
105
106 rset.forEach(function (btn, index) {
107 var rid = {id: id + '-' + index},
108 rbtn = angular.extend({}, btn, rid),
109 istate = (index === 0),
110 rtog = toggle(rDiv, rbtn.id, rbtn.gid, istate,
111 rbtn.cb, rbtn.tooltip);
112
Bri Prebilic Colef7280d02015-02-24 16:20:47 -0800113 rtog.el.classed('radioButton', true);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800114 rads.push(rtog);
115 });
116
117 return {
118 rads: rads,
119 selected: function (i) {
120 if (i === undefined) { return sel; }
121 else if (i < 0 || i >= rads.length) {
122 $log.error('Cannot select radio button of index ' + i);
123 }
124 else {
125 if (i !== sel) {
126 rads[sel].toggle(false);
127 rads[i].toggle(true);
128 sel = i;
129 }
130 }
131 }
132 }
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800133 }
134
135 angular.module('onosWidget')
136 .factory('ButtonService', ['$log', 'FnService', 'IconService',
137 function (_$log_, _fs_, _is_) {
138 $log = _$log_;
139 fs = _fs_;
140 is = _is_;
141
142 return {
143 button: button,
144 toggle: toggle,
145 radioSet: radioSet
146 };
147 }]);
148
149}());