0b82d811aec5da8bace137ccb46fb3741c5474c4
[onosfw.git] /
1 /*
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  Sample Demo module. This contains the "business logic" for the topology
19  overlay that we are implementing.
20  */
21
22 (function () {
23     'use strict';
24
25     // injected refs
26     var $log, fs, flash, wss;
27
28     // constants
29     var displayStart = 'sampleDisplayStart',
30         displayUpdate = 'sampleDisplayUpdate',
31         displayStop = 'sampleDisplayStop';
32
33     // internal state
34     var currentMode = null;
35
36
37     // === ---------------------------
38     // === Helper functions
39
40     function sendDisplayStart(mode) {
41         wss.sendEvent(displayStart, {
42             mode: mode
43         });
44     }
45
46     function sendDisplayUpdate(what) {
47         wss.sendEvent(displayUpdate, {
48             id: what ? what.id : ''
49         });
50     }
51
52     function sendDisplayStop() {
53         wss.sendEvent(displayStop);
54     }
55
56     // === ---------------------------
57     // === Main API functions
58
59     function startDisplay(mode) {
60         if (currentMode === mode) {
61             $log.debug('(in mode', mode, 'already)');
62         } else {
63             currentMode = mode;
64             sendDisplayStart(mode);
65             flash.flash('Starting display mode: ' + mode);
66         }
67     }
68
69     function updateDisplay(m) {
70         if (currentMode) {
71             sendDisplayUpdate(m);
72         }
73     }
74
75     function stopDisplay() {
76         if (currentMode) {
77             currentMode = null;
78             sendDisplayStop();
79             flash.flash('Canceling display mode');
80             return true;
81         }
82         return false;
83     }
84
85     // === ---------------------------
86     // === Module Factory Definition
87
88     angular.module('ovSampleTopov', [])
89         .factory('SampleTopovDemoService',
90         ['$log', 'FnService', 'FlashService', 'WebSocketService',
91
92         function (_$log_, _fs_, _flash_, _wss_) {
93             $log = _$log_;
94             fs = _fs_;
95             flash = _flash_;
96             wss = _wss_;
97
98             return {
99                 startDisplay: startDisplay,
100                 updateDisplay: updateDisplay,
101                 stopDisplay: stopDisplay
102             };
103         }]);
104 }());