Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 1 | /* |
| 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 Cole | 5000a75 | 2015-02-23 17:20:53 -0800 | [diff] [blame] | 25 | var btnSize = 25; |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 26 | |
| 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 Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 36 | if (!div) { |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 37 | $log.warn('Button cannot append to div'); |
| 38 | return null; |
| 39 | } |
| 40 | |
Bri Prebilic Cole | f7280d0 | 2015-02-24 16:20:47 -0800 | [diff] [blame] | 41 | var btnDiv = createDiv(div, 'button', id), |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 42 | cbFnc = fs.isF(cb) || noop; |
| 43 | |
Bri Prebilic Cole | 5000a75 | 2015-02-23 17:20:53 -0800 | [diff] [blame] | 44 | is.loadIcon(btnDiv, gid, btnSize, true); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 45 | |
| 46 | btnDiv.on('click', cbFnc); |
| 47 | |
| 48 | return { |
| 49 | id: id, |
| 50 | click: cbFnc, |
| 51 | el: btnDiv |
| 52 | } |
| 53 | } |
| 54 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 55 | function toggle(div, id, gid, initState, cb, tooltip) { |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 56 | if (!div) { |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 57 | $log.warn('Toggle cannot append to div'); |
| 58 | return null; |
| 59 | } |
| 60 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 61 | var sel = !!initState, |
Bri Prebilic Cole | f7280d0 | 2015-02-24 16:20:47 -0800 | [diff] [blame] | 62 | togDiv = createDiv(div, 'toggleButton', id), |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 63 | cbFnc = fs.isF(cb) || noop; |
| 64 | |
Bri Prebilic Cole | 5000a75 | 2015-02-23 17:20:53 -0800 | [diff] [blame] | 65 | is.loadIcon(togDiv, gid, btnSize, true); |
Bri Prebilic Cole | f7280d0 | 2015-02-24 16:20:47 -0800 | [diff] [blame] | 66 | togDiv.classed('selected', sel); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 67 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 68 | function _toggle(b) { |
| 69 | if (b === undefined) { |
| 70 | sel = !sel; |
| 71 | } else { |
| 72 | sel = !!b; |
| 73 | } |
| 74 | cbFnc(sel); |
Bri Prebilic Cole | f7280d0 | 2015-02-24 16:20:47 -0800 | [diff] [blame] | 75 | togDiv.classed('selected', sel); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | togDiv.on('click', _toggle); |
| 79 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 80 | return { |
| 81 | id: id, |
| 82 | el: togDiv, |
| 83 | selected: function () { return sel; }, |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 84 | toggle: _toggle |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | function radioSet(div, id, rset) { |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 89 | if (!div) { |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 90 | $log.warn('Radio buttons cannot append to div'); |
| 91 | return null; |
| 92 | } |
| 93 | if (!fs.isA(rset)) { |
| 94 | $log.warn('Radio button set is not an array'); |
| 95 | return null; |
| 96 | } |
| 97 | if (rset.length === 0) { |
| 98 | $log.warn('Cannot create radio button set from empty array'); |
| 99 | return null; |
| 100 | } |
Bri Prebilic Cole | f7280d0 | 2015-02-24 16:20:47 -0800 | [diff] [blame] | 101 | var rDiv = div.append('div').classed('radioSet', true), |
Bri Prebilic Cole | 664d470 | 2015-02-25 12:16:14 -0800 | [diff] [blame^] | 102 | rads = [], |
| 103 | sel; |
| 104 | |
| 105 | function _selected() { |
| 106 | var curr = d3.select(this), |
| 107 | currId = curr.attr('id'); |
| 108 | |
| 109 | // I have it going by id's because I couldn't think of a way |
| 110 | // to get the radio button's index from the div element |
| 111 | // We could look at the end of the radio button id for its number |
| 112 | // but I didn't know how to get the end of the string's number |
| 113 | if (sel !== currId) { |
| 114 | var selIndex = _getIndex(), |
| 115 | currIndex = _getIndex(currId); |
| 116 | rads[selIndex].el.classed('selected', false); |
| 117 | curr.classed('selected', true); |
| 118 | rads[currIndex].cb(); |
| 119 | sel = currId; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // given the id, will get the index of element |
| 124 | // without the id, will get the index of sel |
| 125 | function _getIndex(id) { |
| 126 | if (!id) { |
| 127 | for (var i = 0; i < rads.length; i++) { |
| 128 | if (rads[i].id === sel) { return i; } |
| 129 | } |
| 130 | } else { |
| 131 | for (var j = 0; j < rads.length; j++) { |
| 132 | if (rads[j].id === id) { return j; } |
| 133 | } |
| 134 | } |
| 135 | } |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 136 | |
| 137 | rset.forEach(function (btn, index) { |
| 138 | var rid = {id: id + '-' + index}, |
| 139 | rbtn = angular.extend({}, btn, rid), |
| 140 | istate = (index === 0), |
Bri Prebilic Cole | 664d470 | 2015-02-25 12:16:14 -0800 | [diff] [blame^] | 141 | rBtnDiv = createDiv(rDiv, 'radioButton', rbtn.id); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 142 | |
Bri Prebilic Cole | 664d470 | 2015-02-25 12:16:14 -0800 | [diff] [blame^] | 143 | if (istate) { rBtnDiv.classed('selected', true); } |
| 144 | is.loadIcon(rBtnDiv, rbtn.gid, btnSize, true); |
| 145 | rbtn.el = rBtnDiv; |
| 146 | rbtn.cb = fs.isF(rbtn.cb) || noop; |
| 147 | |
| 148 | rBtnDiv.on('click', _selected); |
| 149 | |
| 150 | rads.push(rbtn); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 151 | }); |
Bri Prebilic Cole | 664d470 | 2015-02-25 12:16:14 -0800 | [diff] [blame^] | 152 | sel = rads[0].id; |
| 153 | rads[0].cb(); |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 154 | |
| 155 | return { |
| 156 | rads: rads, |
| 157 | selected: function (i) { |
Bri Prebilic Cole | 664d470 | 2015-02-25 12:16:14 -0800 | [diff] [blame^] | 158 | if (i === undefined) { _getIndex(); } |
| 159 | else { _selected(); } |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | angular.module('onosWidget') |
| 165 | .factory('ButtonService', ['$log', 'FnService', 'IconService', |
| 166 | function (_$log_, _fs_, _is_) { |
| 167 | $log = _$log_; |
| 168 | fs = _fs_; |
| 169 | is = _is_; |
| 170 | |
| 171 | return { |
| 172 | button: button, |
| 173 | toggle: toggle, |
| 174 | radioSet: radioSet |
| 175 | }; |
| 176 | }]); |
| 177 | |
| 178 | }()); |