Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 10 | var $log, fs, flash, wss, tss, tds; |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 11 | |
| 12 | // constants |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 13 | 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 Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 20 | |
| 21 | // internal state |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 22 | var currentMode = null, |
| 23 | ctx = { // chained dialog context |
| 24 | device: null, |
| 25 | port: -1, |
| 26 | foo: false, |
| 27 | bar: false |
| 28 | }, |
| 29 | handlers = {}; |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 30 | |
| 31 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 32 | // 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 Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 143 | // === --------------------------- |
| 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 Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 162 | function createDialogContent(devs) { |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 163 | var content = tds.createDiv('my-content-class'), |
| 164 | items; |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 165 | |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 166 | content.append('p').text('Do something to these devices?'); |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 167 | items = content.append('div'); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 168 | devs.forEach(function (d) { |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 169 | items.append('p').text(d); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 170 | }); |
| 171 | return content; |
| 172 | } |
| 173 | |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 174 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 175 | function createCustomContent() { |
| 176 | var content = tds.createDiv('my-div-class'); |
| 177 | content.append('p').text('(Some content goes here...)'); |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 178 | return content; |
| 179 | } |
| 180 | |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 181 | // === --------------------------- |
| 182 | // === Main API functions |
| 183 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 184 | function overlayActive(active) { |
| 185 | if (active) { |
| 186 | wss.bindHandlers(handlers); |
| 187 | } else { |
| 188 | stopDisplay(); |
| 189 | wss.unbindHandlers(handlers); |
| 190 | } |
| 191 | } |
| 192 | |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 193 | 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 Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 219 | // 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 Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 223 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 224 | $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 Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 233 | |
| 234 | // only if at least one device was selected |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 235 | if (sctx.devices.length) { |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 236 | tds.openDialog() |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 237 | .setTitle('Process Devices') |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 238 | .addContent(createDialogContent(sctx.devices)) |
Simon Hunt | ffb9848 | 2016-02-04 13:51:31 -0800 | [diff] [blame] | 239 | .addCancel(dCancel) // 'esc' key bound to 'Cancel' button |
| 240 | .addOk(dOk) // 'enter' key bound to 'OK' button |
| 241 | .bindKeys(); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 245 | // 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 Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 261 | // this example dialog invoked from the toolbar |
| 262 | function listDialog() { |
| 263 | $log.debug('list dialog invoked'); |
| 264 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 265 | function dOk() { |
| 266 | $log.debug('Dialog Gotcha button pressed'); |
| 267 | } |
| 268 | |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 269 | tds.openDialog() |
| 270 | .setTitle('A list of stuff') |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 271 | .addContent(createCustomContent()) |
Simon Hunt | a06c85b | 2016-02-05 09:44:31 -0800 | [diff] [blame] | 272 | .addOk(dOk, 'Gotcha') // custom text for "OK" button |
Simon Hunt | ffb9848 | 2016-02-04 13:51:31 -0800 | [diff] [blame] | 273 | .bindKeys(); |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 276 | // === --------------------------- |
| 277 | // === Module Factory Definition |
| 278 | |
| 279 | angular.module('ovUiRefTopov', []) |
| 280 | .factory('UiRefTopovDemoService', |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 281 | ['$log', 'FnService', 'FlashService', 'WebSocketService', |
| 282 | 'TopoSelectService', 'TopoDialogService', |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 283 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 284 | 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 Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 291 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 292 | return { |
| 293 | overlayActive: overlayActive, |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 294 | |
Simon Hunt | 92042c8 | 2016-05-27 19:52:39 -0700 | [diff] [blame] | 295 | startDisplay: startDisplay, |
| 296 | updateDisplay: updateDisplay, |
| 297 | stopDisplay: stopDisplay, |
| 298 | |
| 299 | chainedDialogs: chainedDialogs, |
| 300 | simpleDialog: simpleDialog, |
| 301 | listDialog: listDialog |
| 302 | }; |
| 303 | }]); |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 304 | }()); |