blob: 839633fb3da73806ef40ac565372908e37f5af58 [file] [log] [blame]
Simon Huntb9c495e2015-11-05 15:08:06 -08001/*
2 Sample Demo module. This contains the "business logic" for the topology
3 overlay that we are implementing.
4 */
5
6(function () {
7 'use strict';
8
9 // injected refs
Simon Hunt97f2dbb2015-11-06 13:39:58 -080010 var $log, fs, flash, wss, tss, tds;
Simon Huntb9c495e2015-11-05 15:08:06 -080011
12 // constants
Simon Hunt92042c82016-05-27 19:52:39 -070013 var pfx = 'uiRefTopov',
14 displayStart = pfx + 'DisplayStart',
15 displayUpdate = pfx + 'DisplayUpdate',
16 displayStop = pfx + 'DisplayStop',
17 portsReq = pfx + 'DevicePortsReq',
18 portsResp = pfx + 'DevicePortsResp',
19 portsOp = pfx + 'DevicePortFakeOp';
Simon Huntb9c495e2015-11-05 15:08:06 -080020
21 // internal state
Simon Hunt92042c82016-05-27 19:52:39 -070022 var currentMode = null,
23 ctx = { // chained dialog context
24 device: null,
25 port: -1,
26 foo: false,
27 bar: false
28 },
29 handlers = {};
Simon Huntb9c495e2015-11-05 15:08:06 -080030
31
Simon Hunt92042c82016-05-27 19:52:39 -070032 // Handle device ports response from server:
33 // This will be invoked in response to a device selected and the
34 // "chain" button pressed on the details dialog, once the response
35 // comes back from the server.
36 // We are going to open a dialog and ask the user to select one
37 // of the ports for the device...
38 handlers[portsResp] = function (data) {
39 $log.debug('hey! Port Data from the server!', data);
40 ctx.device = data.id;
41
42 // invoked when the OK button is pressed on this dialog
43 function dOk() {
44 $log.debug('Dialog OK button pressed');
45 portOptionsDialog();
46 }
47
48 tds.openDialog()
49 .setTitle('Choose Port')
50 .addContent(createPortChoiceContent(data.ports))
51 .addCancel()
52 .addOkChained(dOk) // NOTE: we use the "chained" version here
53 .bindKeys();
54 };
55
56 function createPortChoiceContent(ports) {
57 var content = tds.createDiv('port-list'),
58 form,
59 portSelect;
60
61 content.append('p').text('Select port of device ' + ctx.device);
62 form = content.append('form');
63 form.append('span').text('port number: ');
64
65 // onchange function for selection widget
66 function selectPort() {
67 ctx.port = this.options[this.selectedIndex].value;
68 }
69
70 portSelect = form.append('select').on('change', selectPort);
71 ports.forEach(function (p) {
72 portSelect.append('option')
73 .attr('value', p.id)
74 .text(p.id);
75 });
76
77 ctx.port = -1; // clear state from any previous invocations
78
79 return content;
80 }
81
82
83 // the function that is called if OK is pressed on our ports dialog
84 function portOptionsDialog() {
85
86 // invoked when the OK button is pressed on this dialog
87 function dOk() {
88 $log.debug('Port Options Dialog OK button pressed');
89 $log.debug('Sending event', portsOp, ctx);
90 wss.sendEvent(portsOp, ctx);
91 }
92
93 tds.openDialog()
94 .setTitle('Select Port Options')
95 .addContent(createPortOptionsContent())
96 .addCancel()
97 .addOk(dOk) // NOTE: NOT the "chained" version!
98 .bindKeys();
99 }
100
101 function createPortOptionsContent() {
102 var content = tds.createDiv('port-opts'),
103 form;
104
105 // helper function to add a paragraph
106 function para(text) {
107 content.append('p').text(text);
108 }
109
110 para('Device ' + ctx.device);
111 para('Port ' + ctx.port);
112
113 form = content.append('form');
114
115 // helper function to add a checkbox to the form, which updates the
116 // context when the user toggles the checked state of the box.
117 function cbox(name, val) {
118
119 // onchange function for checkbox widget
120 function onchange() {
121 ctx[val] = this.checked;
122 }
123
124 form.append('input').attr({
125 type: 'checkbox',
126 name: name,
127 value: val
128 }).on('change', onchange);
129
130 ctx[val] = false; // clear state from any previous invocations
131
132 form.append('span').text(name);
133 form.append('br');
134 }
135
136 // add two checkboxes...
137 cbox('Foo', 'foo');
138 cbox('Bar', 'bar');
139
140 return content;
141 }
142
Simon Huntb9c495e2015-11-05 15:08:06 -0800143 // === ---------------------------
144 // === Helper functions
145
146 function sendDisplayStart(mode) {
147 wss.sendEvent(displayStart, {
148 mode: mode
149 });
150 }
151
152 function sendDisplayUpdate(what) {
153 wss.sendEvent(displayUpdate, {
154 id: what ? what.id : ''
155 });
156 }
157
158 function sendDisplayStop() {
159 wss.sendEvent(displayStop);
160 }
161
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800162 function createDialogContent(devs) {
Simon Huntfb658672015-11-09 13:05:54 -0800163 var content = tds.createDiv('my-content-class'),
164 items;
Simon Hunt92042c82016-05-27 19:52:39 -0700165
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800166 content.append('p').text('Do something to these devices?');
Simon Huntfb658672015-11-09 13:05:54 -0800167 items = content.append('div');
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800168 devs.forEach(function (d) {
Simon Huntfb658672015-11-09 13:05:54 -0800169 items.append('p').text(d);
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800170 });
171 return content;
172 }
173
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800174
Simon Hunt92042c82016-05-27 19:52:39 -0700175 function createCustomContent() {
176 var content = tds.createDiv('my-div-class');
177 content.append('p').text('(Some content goes here...)');
Simon Huntfb658672015-11-09 13:05:54 -0800178 return content;
179 }
180
Simon Huntb9c495e2015-11-05 15:08:06 -0800181 // === ---------------------------
182 // === Main API functions
183
Simon Hunt92042c82016-05-27 19:52:39 -0700184 function overlayActive(active) {
185 if (active) {
186 wss.bindHandlers(handlers);
187 } else {
188 stopDisplay();
189 wss.unbindHandlers(handlers);
190 }
191 }
192
Simon Huntb9c495e2015-11-05 15:08:06 -0800193 function startDisplay(mode) {
194 if (currentMode === mode) {
195 $log.debug('(in mode', mode, 'already)');
196 } else {
197 currentMode = mode;
198 sendDisplayStart(mode);
199 flash.flash('Starting display mode: ' + mode);
200 }
201 }
202
203 function updateDisplay(m) {
204 if (currentMode) {
205 sendDisplayUpdate(m);
206 }
207 }
208
209 function stopDisplay() {
210 if (currentMode) {
211 currentMode = null;
212 sendDisplayStop();
213 flash.flash('Canceling display mode');
214 return true;
215 }
216 return false;
217 }
218
Simon Hunt92042c82016-05-27 19:52:39 -0700219 // this example dialog is invoked from the details panel, when one or more
220 // devices have been selected, and the "banner" button is pressed.
221 function simpleDialog() {
222 var sctx = tss.selectionContext();
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800223
Simon Hunt92042c82016-05-27 19:52:39 -0700224 $log.debug('SIMPLE: device dialog invoked with context:', sctx);
225
226 function dCancel() {
227 $log.debug('Dialog CANCEL button pressed');
228 }
229
230 function dOk() {
231 $log.debug('Dialog OK button pressed');
232 }
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800233
234 // only if at least one device was selected
Simon Hunt92042c82016-05-27 19:52:39 -0700235 if (sctx.devices.length) {
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800236 tds.openDialog()
Simon Huntfb658672015-11-09 13:05:54 -0800237 .setTitle('Process Devices')
Simon Hunt92042c82016-05-27 19:52:39 -0700238 .addContent(createDialogContent(sctx.devices))
Simon Huntffb98482016-02-04 13:51:31 -0800239 .addCancel(dCancel) // 'esc' key bound to 'Cancel' button
240 .addOk(dOk) // 'enter' key bound to 'OK' button
241 .bindKeys();
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800242 }
243 }
244
Simon Hunt92042c82016-05-27 19:52:39 -0700245 // this example dialog is invoked from the details panel, when a single
246 // device has been selected and the "chain" button is pressed.
247 function chainedDialogs() {
248 var sctx = tss.selectionContext();
249
250 $log.debug('CHAINED: device dialog invoked with context:', sctx);
251
252 // only if exactly one device was selected...
253 if (sctx.devices.length === 1) {
254 // send a request for port information about the device to server
255 wss.sendEvent(portsReq, {
256 id: sctx.devices[0]
257 });
258 }
259 }
260
Simon Huntfb658672015-11-09 13:05:54 -0800261 // this example dialog invoked from the toolbar
262 function listDialog() {
263 $log.debug('list dialog invoked');
264
Simon Hunt92042c82016-05-27 19:52:39 -0700265 function dOk() {
266 $log.debug('Dialog Gotcha button pressed');
267 }
268
Simon Huntfb658672015-11-09 13:05:54 -0800269 tds.openDialog()
270 .setTitle('A list of stuff')
Simon Hunt92042c82016-05-27 19:52:39 -0700271 .addContent(createCustomContent())
Simon Hunta06c85b2016-02-05 09:44:31 -0800272 .addOk(dOk, 'Gotcha') // custom text for "OK" button
Simon Huntffb98482016-02-04 13:51:31 -0800273 .bindKeys();
Simon Huntfb658672015-11-09 13:05:54 -0800274 }
275
Simon Huntb9c495e2015-11-05 15:08:06 -0800276 // === ---------------------------
277 // === Module Factory Definition
278
279 angular.module('ovUiRefTopov', [])
280 .factory('UiRefTopovDemoService',
Simon Hunt92042c82016-05-27 19:52:39 -0700281 ['$log', 'FnService', 'FlashService', 'WebSocketService',
282 'TopoSelectService', 'TopoDialogService',
Simon Huntb9c495e2015-11-05 15:08:06 -0800283
Simon Hunt92042c82016-05-27 19:52:39 -0700284 function (_$log_, _fs_, _flash_, _wss_, _tss_, _tds_) {
285 $log = _$log_;
286 fs = _fs_;
287 flash = _flash_;
288 wss = _wss_;
289 tss = _tss_;
290 tds = _tds_;
Simon Huntb9c495e2015-11-05 15:08:06 -0800291
Simon Hunt92042c82016-05-27 19:52:39 -0700292 return {
293 overlayActive: overlayActive,
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800294
Simon Hunt92042c82016-05-27 19:52:39 -0700295 startDisplay: startDisplay,
296 updateDisplay: updateDisplay,
297 stopDisplay: stopDisplay,
298
299 chainedDialogs: chainedDialogs,
300 simpleDialog: simpleDialog,
301 listDialog: listDialog
302 };
303 }]);
Simon Huntb9c495e2015-11-05 15:08:06 -0800304}());