blob: 5ae0ca95c94008efb2a0d8ee8dcc0896766ca7b7 [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,
44 data = [];
45
46 // DOM elements
47 var flashDiv, svg;
48
49
50 function computeBox(el) {
51 var text = el.select('text'),
52 box = text.node().getBBox();
53
54 // center
55 box.x = -box.width / 2;
56 box.y = -box.height / 2;
57
58 // add some padding
59 box.x -= xpad;
60 box.width += xpad * 2;
61 box.y -= ypad;
62 box.height += ypad * 2;
63
64 return box;
65 }
66
67 function updateFlash() {
68 if (!svg) {
69 svg = flashDiv.append('svg').attr({
70 width: w,
71 height: h,
72 viewBox: vbox
73 });
74 }
75
76 var items = svg.selectAll('.flashItem')
77 .data(data);
78
79 // this is when there is an existing item
80 items.each(function (msg) {
81 var el = d3.select(this),
82 box;
83
84 el.select('text').text(msg);
85 box = computeBox(el);
86 el.select('rect').attr(box);
87 });
88
89
90 // this is when there is no existing item
91 var entering = items.enter()
92 .append('g')
93 .attr({
94 class: 'flashItem',
95 opacity: 0
96 })
97 .transition()
98 .duration(settings.fade)
99 .attr('opacity', 1);
100
101 entering.each(function (msg) {
102 var el = d3.select(this),
103 box;
104
105 el.append('rect').attr('rx', rx);
106 el.append('text').text(msg);
107 box = computeBox(el);
108 el.select('rect').attr(box);
109 });
110
111 items.exit()
112 .transition()
113 .duration(settings.fade)
114 .attr('opacity', 0)
115 .remove();
116
117 if (svg && data.length === 0) {
118 svg.transition()
119 .delay(settings.fade + 10)
120 .remove();
121 svg = null;
122 }
123 }
124
125 function flash(msg) {
126 if (timer) {
127 $timeout.cancel(timer);
128 }
129
130 timer = $timeout(function () {
131 data = [];
132 updateFlash();
133 }, settings.showFor);
134
135 data = [msg];
136 updateFlash();
137 }
138
139 angular.module('onosLayer')
140 .factory('FlashService', ['$log', '$timeout',
141 function (_$log_, _$timeout_) {
142 $log = _$log_;
143 $timeout = _$timeout_;
144
145 function initFlash(opts) {
146 settings = angular.extend({}, defaultSettings, opts);
147 flashDiv = d3.select('#flash');
148 }
149
150 return {
151 initFlash: initFlash,
152 flash: flash
153 };
154 }]);
155
156}());