Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 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 | ONOS GUI -- Layer -- Flash Service |
| 19 | |
| 20 | Provides a mechanism to flash short informational messages to the screen |
| 21 | to alert the user of something, e.g. "Hosts visible" or "Hosts hidden". |
| 22 | */ |
| 23 | (function () { |
| 24 | 'use strict'; |
| 25 | |
| 26 | // injected references |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 27 | var $timeout; |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 28 | |
| 29 | // configuration |
| 30 | var defaultSettings = { |
| 31 | fade: 200, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 32 | showFor: 1200, |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 33 | }, |
| 34 | w = '100%', |
| 35 | h = 200, |
| 36 | xpad = 20, |
| 37 | ypad = 10, |
| 38 | rx = 10, |
| 39 | vbox = '-200 -' + (h/2) + ' 400 ' + h; |
| 40 | |
| 41 | // internal state |
| 42 | var settings, |
| 43 | timer = null, |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 44 | data = [], |
| 45 | enabled; |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 46 | |
| 47 | // DOM elements |
| 48 | var flashDiv, svg; |
| 49 | |
| 50 | |
| 51 | function computeBox(el) { |
| 52 | var text = el.select('text'), |
| 53 | box = text.node().getBBox(); |
| 54 | |
| 55 | // center |
| 56 | box.x = -box.width / 2; |
| 57 | box.y = -box.height / 2; |
| 58 | |
| 59 | // add some padding |
| 60 | box.x -= xpad; |
| 61 | box.width += xpad * 2; |
| 62 | box.y -= ypad; |
| 63 | box.height += ypad * 2; |
| 64 | |
| 65 | return box; |
| 66 | } |
| 67 | |
| 68 | function updateFlash() { |
| 69 | if (!svg) { |
| 70 | svg = flashDiv.append('svg').attr({ |
| 71 | width: w, |
| 72 | height: h, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 73 | viewBox: vbox, |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 74 | }); |
| 75 | } |
| 76 | |
| 77 | var items = svg.selectAll('.flashItem') |
| 78 | .data(data); |
| 79 | |
| 80 | // this is when there is an existing item |
| 81 | items.each(function (msg) { |
| 82 | var el = d3.select(this), |
| 83 | box; |
| 84 | |
| 85 | el.select('text').text(msg); |
| 86 | box = computeBox(el); |
| 87 | el.select('rect').attr(box); |
| 88 | }); |
| 89 | |
| 90 | |
| 91 | // this is when there is no existing item |
| 92 | var entering = items.enter() |
| 93 | .append('g') |
| 94 | .attr({ |
| 95 | class: 'flashItem', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 96 | opacity: 0, |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 97 | }) |
| 98 | .transition() |
| 99 | .duration(settings.fade) |
| 100 | .attr('opacity', 1); |
| 101 | |
| 102 | entering.each(function (msg) { |
| 103 | var el = d3.select(this), |
| 104 | box; |
| 105 | |
| 106 | el.append('rect').attr('rx', rx); |
| 107 | el.append('text').text(msg); |
| 108 | box = computeBox(el); |
| 109 | el.select('rect').attr(box); |
| 110 | }); |
| 111 | |
| 112 | items.exit() |
| 113 | .transition() |
| 114 | .duration(settings.fade) |
| 115 | .attr('opacity', 0) |
| 116 | .remove(); |
| 117 | |
| 118 | if (svg && data.length === 0) { |
| 119 | svg.transition() |
| 120 | .delay(settings.fade + 10) |
| 121 | .remove(); |
| 122 | svg = null; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | function flash(msg) { |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 127 | if (!enabled) return; |
| 128 | |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 129 | if (timer) { |
| 130 | $timeout.cancel(timer); |
| 131 | } |
| 132 | |
| 133 | timer = $timeout(function () { |
| 134 | data = []; |
| 135 | updateFlash(); |
| 136 | }, settings.showFor); |
| 137 | |
| 138 | data = [msg]; |
| 139 | updateFlash(); |
| 140 | } |
| 141 | |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 142 | function enable(b) { |
| 143 | enabled = !!b; |
| 144 | } |
| 145 | |
Simon Hunt | f4ef6dd | 2016-02-03 17:05:14 -0800 | [diff] [blame] | 146 | function tempDiv(ms) { |
| 147 | var div = d3.select('body').append('div').classed('centered', true), |
| 148 | delay = (ms === undefined || ms < 100) ? 3000 : ms; |
| 149 | $timeout(function () { div.remove(); }, delay); |
| 150 | return div; |
| 151 | } |
| 152 | |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 153 | angular.module('onosLayer') |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 154 | .factory('FlashService', ['$timeout', |
| 155 | function (_$timeout_) { |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 156 | $timeout = _$timeout_; |
| 157 | |
| 158 | function initFlash(opts) { |
| 159 | settings = angular.extend({}, defaultSettings, opts); |
| 160 | flashDiv = d3.select('#flash'); |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 161 | enabled = true; |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return { |
| 165 | initFlash: initFlash, |
Simon Hunt | f41f309 | 2015-04-16 10:33:26 -0700 | [diff] [blame] | 166 | flash: flash, |
Simon Hunt | f4ef6dd | 2016-02-03 17:05:14 -0800 | [diff] [blame] | 167 | enable: enable, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 168 | tempDiv: tempDiv, |
Simon Hunt | e33889d | 2015-02-05 11:39:28 -0800 | [diff] [blame] | 169 | }; |
| 170 | }]); |
| 171 | |
| 172 | }()); |