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 | |
| 41 | var btnDiv = createDiv(div, 'btn', 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 | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 62 | togDiv = createDiv(div, 'tog', 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 | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 66 | |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 67 | function _toggle(b) { |
| 68 | if (b === undefined) { |
| 69 | sel = !sel; |
| 70 | } else { |
| 71 | sel = !!b; |
| 72 | } |
| 73 | cbFnc(sel); |
| 74 | } |
| 75 | |
| 76 | togDiv.on('click', _toggle); |
| 77 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 78 | return { |
| 79 | id: id, |
| 80 | el: togDiv, |
| 81 | selected: function () { return sel; }, |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 82 | toggle: _toggle |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | function radioSet(div, id, rset) { |
Bri Prebilic Cole | c440332 | 2015-02-23 10:29:22 -0800 | [diff] [blame] | 87 | if (!div) { |
Bri Prebilic Cole | 08381ce | 2015-02-20 17:01:50 -0800 | [diff] [blame] | 88 | $log.warn('Radio buttons cannot append to div'); |
| 89 | return null; |
| 90 | } |
| 91 | if (!fs.isA(rset)) { |
| 92 | $log.warn('Radio button set is not an array'); |
| 93 | return null; |
| 94 | } |
| 95 | if (rset.length === 0) { |
| 96 | $log.warn('Cannot create radio button set from empty array'); |
| 97 | return null; |
| 98 | } |
| 99 | var rDiv = div.append('div').classed('rset', true), |
| 100 | sel = 0, |
| 101 | rads = []; |
| 102 | |
| 103 | rset.forEach(function (btn, index) { |
| 104 | var rid = {id: id + '-' + index}, |
| 105 | rbtn = angular.extend({}, btn, rid), |
| 106 | istate = (index === 0), |
| 107 | rtog = toggle(rDiv, rbtn.id, rbtn.gid, istate, |
| 108 | rbtn.cb, rbtn.tooltip); |
| 109 | |
| 110 | rtog.el = (rtog.el).classed('tog', false).classed('rad', true); |
| 111 | rads.push(rtog); |
| 112 | }); |
| 113 | |
| 114 | return { |
| 115 | rads: rads, |
| 116 | selected: function (i) { |
| 117 | if (i === undefined) { return sel; } |
| 118 | else if (i < 0 || i >= rads.length) { |
| 119 | $log.error('Cannot select radio button of index ' + i); |
| 120 | } |
| 121 | else { |
| 122 | if (i !== sel) { |
| 123 | rads[sel].toggle(false); |
| 124 | rads[i].toggle(true); |
| 125 | sel = i; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | angular.module('onosWidget') |
| 133 | .factory('ButtonService', ['$log', 'FnService', 'IconService', |
| 134 | function (_$log_, _fs_, _is_) { |
| 135 | $log = _$log_; |
| 136 | fs = _fs_; |
| 137 | is = _is_; |
| 138 | |
| 139 | return { |
| 140 | button: button, |
| 141 | toggle: toggle, |
| 142 | radioSet: radioSet |
| 143 | }; |
| 144 | }]); |
| 145 | |
| 146 | }()); |