blob: f4b92481962ff8fad42b685154f968b5d3449158 [file] [log] [blame]
Simon Hunt737c89f2015-01-28 12:23:19 -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 -- SVG -- Util Service
19 */
20
21/*
22 The SVG Util Service provides a miscellany of utility functions.
23 */
24
25(function () {
26 'use strict';
27
28 // injected references
29 var $log, fs;
30
31 angular.module('onosSvg')
32 .factory('SvgUtilService', ['$log', 'FnService',
33 function (_$log_, _fs_) {
34 $log = _$log_;
35 fs = _fs_;
36
Simon Hunt08f841d02015-02-10 14:39:20 -080037 // TODO: change 'force' ref to be 'force.alpha' ref.
Simon Hunta11b4eb2015-01-28 16:20:50 -080038 function createDragBehavior(force, selectCb, atDragEnd,
39 dragEnabled, clickEnabled) {
40 var draggedThreshold = d3.scale.linear()
41 .domain([0, 0.1])
42 .range([5, 20])
43 .clamp(true),
44 drag,
45 fSel = fs.isF(selectCb),
46 fEnd = fs.isF(atDragEnd),
47 fDEn = fs.isF(dragEnabled),
48 fCEn = fs.isF(clickEnabled),
49 bad = [];
50
51 function naf(what) {
52 return 'SvgUtilService: createDragBehavior(): ' + what +
53 ' is not a function';
54 }
55
56 if (!fSel) {
57 bad.push(naf('selectCb'));
58 }
59 if (!fEnd) {
60 bad.push(naf('atDragEnd'));
61 }
62 if (!fDEn) {
63 bad.push(naf('dragEnabled'));
64 }
65 if (!fCEn) {
66 bad.push(naf('clickEnabled'));
67 }
68
69 if (bad.length) {
70 $log.error(bad.join('\n'));
71 return null;
72 }
73
74
75 function dragged(d) {
76 var threshold = draggedThreshold(force.alpha()),
77 dx = d.oldX - d.px,
78 dy = d.oldY - d.py;
79 if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) {
80 d.dragged = true;
81 }
82 return d.dragged;
83 }
84
85 drag = d3.behavior.drag()
86 .origin(function(d) { return d; })
87 .on('dragstart', function(d) {
88 if (clickEnabled() || dragEnabled()) {
89 d3.event.sourceEvent.stopPropagation();
90
91 d.oldX = d.x;
92 d.oldY = d.y;
93 d.dragged = false;
94 d.fixed |= 2;
95 d.dragStarted = true;
96 }
97 })
98 .on('drag', function(d) {
99 if (dragEnabled()) {
100 d.px = d3.event.x;
101 d.py = d3.event.y;
102 if (dragged(d)) {
103 if (!force.alpha()) {
104 force.alpha(.025);
105 }
106 }
107 }
108 })
109 .on('dragend', function(d) {
110 if (d.dragStarted) {
111 d.dragStarted = false;
112 if (!dragged(d)) {
113 // consider this the same as a 'click'
114 // (selection of a node)
115 if (clickEnabled()) {
Simon Hunt445e8152015-02-06 13:00:12 -0800116 selectCb.call(this, d);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800117 }
118 }
119 d.fixed &= ~6;
120
121 // hook at the end of a drag gesture
122 if (dragEnabled()) {
Simon Hunt445e8152015-02-06 13:00:12 -0800123 atDragEnd.call(this, d);
Simon Hunta11b4eb2015-01-28 16:20:50 -0800124 }
125 }
126 });
127
Simon Hunt0ee28682015-02-12 20:48:11 -0800128 return drag;
129 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800130
Simon Hunt0ee28682015-02-12 20:48:11 -0800131
132 function loadGlow(defs, r, g, b, id) {
133 var glow = defs.append('filter')
134 .attr('x', '-50%')
135 .attr('y', '-50%')
136 .attr('width', '200%')
137 .attr('height', '200%')
138 .attr('id', id);
139
140 glow.append('feColorMatrix')
141 .attr('type', 'matrix')
142 .attr('values',
143 '0 0 0 0 ' + r + ' ' +
144 '0 0 0 0 ' + g + ' ' +
145 '0 0 0 0 ' + b + ' ' +
146 '0 0 0 1 0 ');
147
148 glow.append('feGaussianBlur')
149 .attr('stdDeviation', 3)
150 .attr('result', 'coloredBlur');
151
152 glow.append('feMerge').selectAll('feMergeNode')
153 .data(['coloredBlur', 'SourceGraphic'])
154 .enter().append('feMergeNode')
155 .attr('in', String);
156 }
157
158 function loadGlowDefs(defs) {
159 loadGlow(defs, 0.0, 0.0, 0.7, 'blue-glow');
160 loadGlow(defs, 1.0, 1.0, 0.3, 'yellow-glow');
Simon Hunt737c89f2015-01-28 12:23:19 -0800161 }
162
Simon Hunt48e61672015-01-30 14:48:25 -0800163 // --- Ordinal scales for 7 values.
Simon Hunt48e61672015-01-30 14:48:25 -0800164
165 // blue brown brick red sea green purple dark teal lime
166 var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'],
167 lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'],
168
Simon Hunt27e153a2015-02-02 18:45:44 -0800169 darkNorm = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700'],
Simon Hunt5724fb42015-02-05 16:59:40 -0800170 darkMute = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700'];
Simon Hunt48e61672015-01-30 14:48:25 -0800171
172 var colors= {
173 light: {
174 norm: d3.scale.ordinal().range(lightNorm),
175 mute: d3.scale.ordinal().range(lightMute)
176 },
177 dark: {
178 norm: d3.scale.ordinal().range(darkNorm),
179 mute: d3.scale.ordinal().range(darkMute)
180 }
181 };
182
Simon Hunt737c89f2015-01-28 12:23:19 -0800183 function cat7() {
Simon Hunt48e61672015-01-30 14:48:25 -0800184 var tcid = 'd3utilTestCard';
185
186 function getColor(id, muted, theme) {
187 // NOTE: since we are lazily assigning domain ids, we need to
188 // get the color from all 4 scales, to keep the domains
189 // in sync.
190 var ln = colors.light.norm(id),
191 lm = colors.light.mute(id),
192 dn = colors.dark.norm(id),
193 dm = colors.dark.mute(id);
194 if (theme === 'dark') {
195 return muted ? dm : dn;
196 } else {
197 return muted ? lm : ln;
198 }
199 }
200
201 function testCard(svg) {
202 var g = svg.select('g#' + tcid),
203 dom = d3.range(7),
204 k, muted, theme, what;
205
206 if (!g.empty()) {
207 g.remove();
208
209 } else {
210 g = svg.append('g')
211 .attr('id', tcid)
212 .attr('transform', 'scale(4)translate(20,20)');
213
214 for (k=0; k<4; k++) {
215 muted = k%2;
216 what = muted ? ' muted' : ' normal';
217 theme = k < 2 ? 'light' : 'dark';
218 dom.forEach(function (id, i) {
219 var x = i * 20,
220 y = k * 20,
221 f = get(id, muted, theme);
222 g.append('circle').attr({
223 cx: x,
224 cy: y,
225 r: 5,
226 fill: f
227 });
228 });
229 g.append('rect').attr({
230 x: 140,
231 y: k * 20 - 5,
232 width: 32,
233 height: 10,
234 rx: 2,
235 fill: '#888'
236 });
237 g.append('text').text(theme + what)
238 .attr({
239 x: 142,
240 y: k * 20 + 2,
241 fill: 'white'
242 })
243 .style('font-size', '4pt');
244 }
245 }
246 }
247
248 return {
249 testCard: testCard,
250 getColor: getColor
251 };
Simon Hunt737c89f2015-01-28 12:23:19 -0800252 }
253
Simon Huntc9b73162015-01-29 14:02:15 -0800254 function translate(x, y) {
255 if (fs.isA(x) && x.length === 2 && !y) {
256 return 'translate(' + x[0] + ',' + x[1] + ')';
257 }
258 return 'translate(' + x + ',' + y + ')';
259 }
260
Simon Hunt4b668592015-01-29 17:33:53 -0800261 function stripPx(s) {
262 return s.replace(/px$/,'');
263 }
264
Simon Hunt18bf9822015-02-12 17:35:45 -0800265 function safeId(s) {
266 return s.replace(/[^a-z0-9]/gi, '-');
267 }
268
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800269 function makeVisible(el, b) {
270 el.style('visibility', (b ? 'visible' : 'hidden'));
271 }
272
Simon Hunt18bf9822015-02-12 17:35:45 -0800273 function isVisible(el) {
274 return el.style('visibility') === 'visible';
Simon Huntac4c6f72015-02-03 19:50:53 -0800275 }
276
Simon Hunt737c89f2015-01-28 12:23:19 -0800277 return {
278 createDragBehavior: createDragBehavior,
Simon Hunt0ee28682015-02-12 20:48:11 -0800279 loadGlowDefs: loadGlowDefs,
Simon Huntc9b73162015-01-29 14:02:15 -0800280 cat7: cat7,
Simon Hunt4b668592015-01-29 17:33:53 -0800281 translate: translate,
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800282 stripPx: stripPx,
Simon Hunt18bf9822015-02-12 17:35:45 -0800283 safeId: safeId,
284 visible: function (el, x) {
285 if (x === undefined) {
286 return isVisible(el);
287 } else {
288 makeVisible(el, x);
289 }
290 }
Simon Hunt737c89f2015-01-28 12:23:19 -0800291 };
292 }]);
293}());