blob: 7740ccdf161dc06d691c222c64e1dcb29cbaf6e4 [file] [log] [blame]
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -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 -- Toolbar Service
19 */
20(function () {
21 'use strict';
22
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080023 var $log, fs, ps, bns;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080024
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080025 var toolBtnIds = {},
26 toolbarPanel,
27 toolbarDiv;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080028
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080029 var ids = [],
30 tbarId,
31 tbarPanel,
32 tbarDiv;
33
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080034 function init() {
35 toolBtnIds = {};
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080036 ids = [];
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080037 }
38
39 function addButton(btn) {
40 // pass in button object and pass separate the pieces in this function,
41 // to be passed to ButtonService
42 var btndiv = toolbarDiv.append('div').attr('class', 'btn');
43 // use ButtonService to create btn with other arguments and btndiv
44 }
45
46 function addToggle(tog) {
47 var togdiv = toolbarDiv.append('div').attr('class', 'tog');
48 // use ButtonService to create btn with other arguments and togdiv
49 }
50
51 function addRadio(rad) {
52 var raddiv = toolbarDiv.append('div').attr('class', 'rad');
53 // use ButtonService to create btn with other arguments and raddiv
54 }
55
56 function addSeparator() {
57 toolbarDiv.append('div')
58 .attr('class', 'sep')
59 .style({'width': '2px',
60 'border-width': '1px',
61 'border-style': 'solid'});
62 }
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080063
64 function makeButton(id, gid, cb) {
65 return {
66 t: 'btn',
67 id: id,
68 gid: gid,
69 cb: cb
70 };
71 }
72
73 function makeToggle(id, gid, cb) {
74 return {
75 t: 'tog',
76 id: id,
77 gid: gid,
78 cb: cb
79 };
80 }
81
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080082 function makeRadio(id, gid, cb) {
83 (id in toolBtnIds) ? toolBtnIds[id] += 1 : toolBtnIds[id] = 0;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080084 return {
85 t: 'rad',
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080086 id: id + '-' + toolBtnIds[id],
87 rid: toolBtnIds[id] + '',
88 gid: gid,
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080089 cb: cb
90 };
91 }
92
93 function separator() {
94 return {
95 t: 'sep'
96 };
97 }
98
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080099 function validId(id, caller) {
100 if (fs.inArray(id, ids) !== -1) {
101 $log.warn(caller + ': ID already exists');
102 return false;
103 }
104 return true;
105 }
106
107 function addButton1(id, gid, cb, tooltip) {
108 var btnId = tbarId + '-' + id;
109 if (!validId(btnId, 'addButton')) {
110 return null;
111 }
112 ids.push(btnId);
113 return bns.button(tbarDiv, btnId, gid, cb, tooltip);
114 }
115
116 function addToggle1(id, gid, initState, cb, tooltip) {
117 var togId = tbarId + '-' + id;
118 if (!validId(togId, 'addToggle')) {
119 return null;
120 }
121 ids.push(togId);
122 return bns.toggle(tbarDiv, togId, gid, initState, cb, tooltip);
123 }
124
125 function addRadioSet(id, rset) {
126 var radId = tbarId + '-' + id;
127 if (!validId(radId, 'addRadioSet')) {
128 return null;
129 }
130 ids.push(radId);
131 return bns.radioSet(tbarDiv, radId, rset);
132 }
133
134 // TODO: finish this and remove unneeded code
135 function addSeparator1() {
136
137 }
138
139 function createToolbar1(id, settings) {
140 if (!id) {
141 $log.warn('createToolbar: no ID given');
142 return null;
143 }
144 tbarId = 'tbar-' + id;
145 var opts = fs.isO(settings) || {}; // default settings should be put here
146
147 if (!validId(tbarId, 'createToolbar')) {
148 return null;
149 }
150 ids.push(tbarId);
151
152 tbarPanel = ps.createPanel(tbarId, opts);
153 tbarDiv = tbarPanel.classed('toolbar', true);
154
155 // TODO: change names of functions
156 return {
157 addButton1: addButton1,
158 addToggle1: addToggle1,
159 addRadioSet: addRadioSet,
160 addSeparator1: addSeparator1
161 }
162 }
163
164 // function currently not working
165 function destroyToolbar(id) {
166 ps.destroyPanel(id);
167 }
168
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800169 function createToolbar(tbarId, tools) {
170 var api;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800171
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800172 if (!tbarId) {
173 $log.warn('createToolbar: no ID given');
174 return null;
175 }
176 if (!tools || tools.constructor !== Array || !tools.length) {
177 $log.warn('createToolbar: no tools given');
178 return null;
179 }
180
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800181 for (var i = 0; i < tools.length; i += 1) {
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800182 if (tools[i].t === 'rad' || tools[i].t === 'sep') {
183 continue;
184 } else {
185 if (tools[i].id in toolBtnIds) {
186 $log.warn('createToolbar: item with id '
187 + tools[i].id + ' already exists');
188 return null;
189 }
190 toolBtnIds[tools[i].id] = 1;
191 }
192 }
193
194 // need to pass in an object with settings for where the toolbar will go
195 toolbarPanel = ps.createPanel(tbarId, {});
196 toolbarPanel.classed('toolbar', true);
197 toolbarDiv = toolbarPanel.el();
198
199 tools.forEach(function (tool) {
200 switch (tool['t']) {
201 case 'btn': addButton(tool); break;
202 case 'tog': addToggle(tool); break;
203 case 'rad': addRadio(tool); break;
204 default: addSeparator(); break;
205 }
206 });
207
208 // api functions to be returned defined here
209
210 function size(arr) {
211 return arr.length;
212 }
213
214 api = {
215 size: size
216 };
217
218 return api;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800219 }
220
221 angular.module('onosWidget')
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800222 .factory('ToolbarService', ['$log', 'FnService',
223 'PanelService', 'ButtonService',
224 function (_$log_, _fs_, _ps_, _bns_) {
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800225 $log = _$log_;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800226 fs = _fs_;
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800227 ps = _ps_;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800228 bns = _bns_;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800229
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800230 return {
231 init: init,
232 makeButton: makeButton,
233 makeToggle: makeToggle,
234 makeRadio: makeRadio,
235 separator: separator,
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800236 createToolbar: createToolbar,
237 createToolbar1: createToolbar1,
238 destroyToolbar: destroyToolbar
Bri Prebilic Cole751804e2015-02-18 15:44:28 -0800239 };
240 }]);
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800241
242}());