Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -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 -- Toolbar Service |
| 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 23 | var $log, ps; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 24 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 25 | var toolBtnIds = {}, |
| 26 | toolbarPanel, |
| 27 | toolbarDiv; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 28 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 29 | function init() { |
| 30 | toolBtnIds = {}; |
| 31 | } |
| 32 | |
| 33 | function addButton(btn) { |
| 34 | // pass in button object and pass separate the pieces in this function, |
| 35 | // to be passed to ButtonService |
| 36 | var btndiv = toolbarDiv.append('div').attr('class', 'btn'); |
| 37 | // use ButtonService to create btn with other arguments and btndiv |
| 38 | } |
| 39 | |
| 40 | function addToggle(tog) { |
| 41 | var togdiv = toolbarDiv.append('div').attr('class', 'tog'); |
| 42 | // use ButtonService to create btn with other arguments and togdiv |
| 43 | } |
| 44 | |
| 45 | function addRadio(rad) { |
| 46 | var raddiv = toolbarDiv.append('div').attr('class', 'rad'); |
| 47 | // use ButtonService to create btn with other arguments and raddiv |
| 48 | } |
| 49 | |
| 50 | function addSeparator() { |
| 51 | toolbarDiv.append('div') |
| 52 | .attr('class', 'sep') |
| 53 | .style({'width': '2px', |
| 54 | 'border-width': '1px', |
| 55 | 'border-style': 'solid'}); |
| 56 | } |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 57 | |
| 58 | function makeButton(id, gid, cb) { |
| 59 | return { |
| 60 | t: 'btn', |
| 61 | id: id, |
| 62 | gid: gid, |
| 63 | cb: cb |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | function makeToggle(id, gid, cb) { |
| 68 | return { |
| 69 | t: 'tog', |
| 70 | id: id, |
| 71 | gid: gid, |
| 72 | cb: cb |
| 73 | }; |
| 74 | } |
| 75 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 76 | function makeRadio(id, gid, cb) { |
| 77 | (id in toolBtnIds) ? toolBtnIds[id] += 1 : toolBtnIds[id] = 0; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 78 | return { |
| 79 | t: 'rad', |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 80 | id: id + '-' + toolBtnIds[id], |
| 81 | rid: toolBtnIds[id] + '', |
| 82 | gid: gid, |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 83 | cb: cb |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | function separator() { |
| 88 | return { |
| 89 | t: 'sep' |
| 90 | }; |
| 91 | } |
| 92 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 93 | function createToolbar(tbarId, tools) { |
| 94 | var api; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 95 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 96 | if (!tbarId) { |
| 97 | $log.warn('createToolbar: no ID given'); |
| 98 | return null; |
| 99 | } |
| 100 | if (!tools || tools.constructor !== Array || !tools.length) { |
| 101 | $log.warn('createToolbar: no tools given'); |
| 102 | return null; |
| 103 | } |
| 104 | |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 105 | for (var i = 0; i < tools.length; i += 1) { |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 106 | if (tools[i].t === 'rad' || tools[i].t === 'sep') { |
| 107 | continue; |
| 108 | } else { |
| 109 | if (tools[i].id in toolBtnIds) { |
| 110 | $log.warn('createToolbar: item with id ' |
| 111 | + tools[i].id + ' already exists'); |
| 112 | return null; |
| 113 | } |
| 114 | toolBtnIds[tools[i].id] = 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // need to pass in an object with settings for where the toolbar will go |
| 119 | toolbarPanel = ps.createPanel(tbarId, {}); |
| 120 | toolbarPanel.classed('toolbar', true); |
| 121 | toolbarDiv = toolbarPanel.el(); |
| 122 | |
| 123 | tools.forEach(function (tool) { |
| 124 | switch (tool['t']) { |
| 125 | case 'btn': addButton(tool); break; |
| 126 | case 'tog': addToggle(tool); break; |
| 127 | case 'rad': addRadio(tool); break; |
| 128 | default: addSeparator(); break; |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | // api functions to be returned defined here |
| 133 | |
| 134 | function size(arr) { |
| 135 | return arr.length; |
| 136 | } |
| 137 | |
| 138 | api = { |
| 139 | size: size |
| 140 | }; |
| 141 | |
| 142 | return api; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | angular.module('onosWidget') |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 146 | .factory('ToolbarService', ['$log', 'PanelService', |
| 147 | function (_$log_, _ps_) { |
| 148 | $log = _$log_; |
| 149 | ps = _ps_; |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 150 | |
Bri Prebilic Cole | 751804e | 2015-02-18 15:44:28 -0800 | [diff] [blame] | 151 | return { |
| 152 | init: init, |
| 153 | makeButton: makeButton, |
| 154 | makeToggle: makeToggle, |
| 155 | makeRadio: makeRadio, |
| 156 | separator: separator, |
| 157 | createToolbar: createToolbar |
| 158 | }; |
| 159 | }]); |
Bri Prebilic Cole | 2e3f856 | 2015-02-17 17:21:31 -0800 | [diff] [blame] | 160 | |
| 161 | }()); |