blob: 803ec5915c976e17df911fbfee7673d0df04e243 [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
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 Hunt97f2dbb2015-11-06 13:39:58 -080040 function createDialogContent(devs) {
Simon Huntfb658672015-11-09 13:05:54 -080041 var content = tds.createDiv('my-content-class'),
42 items;
Simon Hunt97f2dbb2015-11-06 13:39:58 -080043 content.append('p').text('Do something to these devices?');
Simon Huntfb658672015-11-09 13:05:54 -080044 items = content.append('div');
Simon Hunt97f2dbb2015-11-06 13:39:58 -080045 devs.forEach(function (d) {
Simon Huntfb658672015-11-09 13:05:54 -080046 items.append('p').text(d);
Simon Hunt97f2dbb2015-11-06 13:39:58 -080047 });
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 Huntfb658672015-11-09 13:05:54 -080059 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 Huntb9c495e2015-11-05 15:08:06 -080067 // === ---------------------------
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 Huntfb658672015-11-09 13:05:54 -080096 // this example dialog invoked from the details panel, when one or more
97 // devices have been selected
Simon Hunt97f2dbb2015-11-06 13:39:58 -080098 function deviceDialog() {
99 var ctx = tss.selectionContext();
100
Simon Huntfb658672015-11-09 13:05:54 -0800101 $log.debug('device dialog invoked with context:', ctx);
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800102
103 // only if at least one device was selected
104 if (ctx.devices.length) {
105 tds.openDialog()
Simon Huntfb658672015-11-09 13:05:54 -0800106 .setTitle('Process Devices')
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800107 .addContent(createDialogContent(ctx.devices))
Simon Huntffb98482016-02-04 13:51:31 -0800108 .addCancel(dCancel) // 'esc' key bound to 'Cancel' button
109 .addOk(dOk) // 'enter' key bound to 'OK' button
110 .bindKeys();
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800111 }
112 }
113
Simon Huntfb658672015-11-09 13:05:54 -0800114 // 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 Hunta06c85b2016-02-05 09:44:31 -0800121 .addOk(dOk, 'Gotcha') // custom text for "OK" button
Simon Huntffb98482016-02-04 13:51:31 -0800122 .bindKeys();
Simon Huntfb658672015-11-09 13:05:54 -0800123 }
124
Simon Huntb9c495e2015-11-05 15:08:06 -0800125 // === ---------------------------
126 // === Module Factory Definition
127
128 angular.module('ovUiRefTopov', [])
129 .factory('UiRefTopovDemoService',
130 ['$log', 'FnService', 'FlashService', 'WebSocketService',
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800131 'TopoSelectService', 'TopoDialogService',
Simon Huntb9c495e2015-11-05 15:08:06 -0800132
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800133 function (_$log_, _fs_, _flash_, _wss_, _tss_, _tds_) {
Simon Huntb9c495e2015-11-05 15:08:06 -0800134 $log = _$log_;
135 fs = _fs_;
136 flash = _flash_;
137 wss = _wss_;
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800138 tss = _tss_;
139 tds = _tds_;
Simon Huntb9c495e2015-11-05 15:08:06 -0800140
141 return {
142 startDisplay: startDisplay,
143 updateDisplay: updateDisplay,
Simon Hunt97f2dbb2015-11-06 13:39:58 -0800144 stopDisplay: stopDisplay,
145
Simon Huntfb658672015-11-09 13:05:54 -0800146 deviceDialog: deviceDialog,
147 listDialog: listDialog
Simon Huntb9c495e2015-11-05 15:08:06 -0800148 };
149 }]);
150}());