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