blob: 3139f2cb9f451f06b2db704dfb674950c505c873 [file] [log] [blame]
Simon Hunte33889d2015-02-05 11:39:28 -08001/*
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 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
27 var $log, $timeout;
28
29 // configuration
30 var defaultSettings = {
31 fade: 200,
32 showFor: 1200
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 Huntf41f3092015-04-16 10:33:26 -070044 data = [],
45 enabled;
Simon Hunte33889d2015-02-05 11:39:28 -080046
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,
73 viewBox: vbox
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',
96 opacity: 0
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 Huntf41f3092015-04-16 10:33:26 -0700127 if (!enabled) return;
128
Simon Hunte33889d2015-02-05 11:39:28 -0800129 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 Huntf41f3092015-04-16 10:33:26 -0700142 function enable(b) {
143 enabled = !!b;
144 }
145
Simon Huntf4ef6dd2016-02-03 17:05:14 -0800146 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 Hunte33889d2015-02-05 11:39:28 -0800153 angular.module('onosLayer')
154 .factory('FlashService', ['$log', '$timeout',
155 function (_$log_, _$timeout_) {
156 $log = _$log_;
157 $timeout = _$timeout_;
158
159 function initFlash(opts) {
160 settings = angular.extend({}, defaultSettings, opts);
161 flashDiv = d3.select('#flash');
Simon Huntf41f3092015-04-16 10:33:26 -0700162 enabled = true;
Simon Hunte33889d2015-02-05 11:39:28 -0800163 }
164
165 return {
166 initFlash: initFlash,
Simon Huntf41f3092015-04-16 10:33:26 -0700167 flash: flash,
Simon Huntf4ef6dd2016-02-03 17:05:14 -0800168 enable: enable,
169 tempDiv: tempDiv
Simon Hunte33889d2015-02-05 11:39:28 -0800170 };
171 }]);
172
173}());