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 |
| 13 | var displayStart = 'uiRefTopovDisplayStart', |
| 14 | displayUpdate = 'uiRefTopovDisplayUpdate', |
| 15 | displayStop = 'uiRefTopovDisplayStop'; |
| 16 | |
| 17 | // internal state |
| 18 | var currentMode = null; |
| 19 | |
| 20 | |
| 21 | // === --------------------------- |
| 22 | // === Helper functions |
| 23 | |
| 24 | function sendDisplayStart(mode) { |
| 25 | wss.sendEvent(displayStart, { |
| 26 | mode: mode |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | function sendDisplayUpdate(what) { |
| 31 | wss.sendEvent(displayUpdate, { |
| 32 | id: what ? what.id : '' |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | function sendDisplayStop() { |
| 37 | wss.sendEvent(displayStop); |
| 38 | } |
| 39 | |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 40 | function createDialogContent(devs) { |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 41 | var content = tds.createDiv('my-content-class'), |
| 42 | items; |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 43 | content.append('p').text('Do something to these devices?'); |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 44 | items = content.append('div'); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 45 | devs.forEach(function (d) { |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 46 | items.append('p').text(d); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 47 | }); |
| 48 | return content; |
| 49 | } |
| 50 | |
| 51 | function dCancel() { |
| 52 | $log.debug('Dialog CANCEL button pressed'); |
| 53 | } |
| 54 | |
| 55 | function dOk() { |
| 56 | $log.debug('Dialog OK button pressed'); |
| 57 | } |
| 58 | |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 59 | function createListContent() { |
| 60 | var content = tds.createDiv('my-list-class'), |
| 61 | items; |
| 62 | // TODO: figure out best way to inject selectable list |
| 63 | content.append('p').text('(Selectable list to show here...)'); |
| 64 | return content; |
| 65 | } |
| 66 | |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 67 | // === --------------------------- |
| 68 | // === Main API functions |
| 69 | |
| 70 | function startDisplay(mode) { |
| 71 | if (currentMode === mode) { |
| 72 | $log.debug('(in mode', mode, 'already)'); |
| 73 | } else { |
| 74 | currentMode = mode; |
| 75 | sendDisplayStart(mode); |
| 76 | flash.flash('Starting display mode: ' + mode); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | function updateDisplay(m) { |
| 81 | if (currentMode) { |
| 82 | sendDisplayUpdate(m); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | function stopDisplay() { |
| 87 | if (currentMode) { |
| 88 | currentMode = null; |
| 89 | sendDisplayStop(); |
| 90 | flash.flash('Canceling display mode'); |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 96 | // this example dialog invoked from the details panel, when one or more |
| 97 | // devices have been selected |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 98 | function deviceDialog() { |
| 99 | var ctx = tss.selectionContext(); |
| 100 | |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 101 | $log.debug('device dialog invoked with context:', ctx); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 102 | |
| 103 | // only if at least one device was selected |
| 104 | if (ctx.devices.length) { |
| 105 | tds.openDialog() |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 106 | .setTitle('Process Devices') |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 107 | .addContent(createDialogContent(ctx.devices)) |
Simon Hunt | ffb9848 | 2016-02-04 13:51:31 -0800 | [diff] [blame] | 108 | .addCancel(dCancel) // 'esc' key bound to 'Cancel' button |
| 109 | .addOk(dOk) // 'enter' key bound to 'OK' button |
| 110 | .bindKeys(); |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 114 | // this example dialog invoked from the toolbar |
| 115 | function listDialog() { |
| 116 | $log.debug('list dialog invoked'); |
| 117 | |
| 118 | tds.openDialog() |
| 119 | .setTitle('A list of stuff') |
| 120 | .addContent(createListContent()) |
Simon Hunt | a06c85b | 2016-02-05 09:44:31 -0800 | [diff] [blame] | 121 | .addOk(dOk, 'Gotcha') // custom text for "OK" button |
Simon Hunt | ffb9848 | 2016-02-04 13:51:31 -0800 | [diff] [blame] | 122 | .bindKeys(); |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 125 | // === --------------------------- |
| 126 | // === Module Factory Definition |
| 127 | |
| 128 | angular.module('ovUiRefTopov', []) |
| 129 | .factory('UiRefTopovDemoService', |
| 130 | ['$log', 'FnService', 'FlashService', 'WebSocketService', |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 131 | 'TopoSelectService', 'TopoDialogService', |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 132 | |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 133 | function (_$log_, _fs_, _flash_, _wss_, _tss_, _tds_) { |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 134 | $log = _$log_; |
| 135 | fs = _fs_; |
| 136 | flash = _flash_; |
| 137 | wss = _wss_; |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 138 | tss = _tss_; |
| 139 | tds = _tds_; |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 140 | |
| 141 | return { |
| 142 | startDisplay: startDisplay, |
| 143 | updateDisplay: updateDisplay, |
Simon Hunt | 97f2dbb | 2015-11-06 13:39:58 -0800 | [diff] [blame] | 144 | stopDisplay: stopDisplay, |
| 145 | |
Simon Hunt | fb65867 | 2015-11-09 13:05:54 -0800 | [diff] [blame] | 146 | deviceDialog: deviceDialog, |
| 147 | listDialog: listDialog |
Simon Hunt | b9c495e | 2015-11-05 15:08:06 -0800 | [diff] [blame] | 148 | }; |
| 149 | }]); |
| 150 | }()); |