blob: 2c47d449328ff93a9f3cf246b4ce593d84ed0c3b [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
128 return drag; }
Simon Hunt737c89f2015-01-28 12:23:19 -0800129
130 function loadGlow() {
131 $log.warn('SvgUtilService: loadGlow -- To Be Implemented');
132 }
133
Simon Hunt48e61672015-01-30 14:48:25 -0800134 // --- Ordinal scales for 7 values.
135 // TODO: tune colors for light and dark themes
136 // Note: These colors look good on the white background. Still, need to tune for dark.
137
138 // blue brown brick red sea green purple dark teal lime
139 var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'],
140 lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'],
141
Simon Hunt27e153a2015-02-02 18:45:44 -0800142 darkNorm = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700'],
Simon Hunt5724fb42015-02-05 16:59:40 -0800143 darkMute = ['#304860', '#664631', '#A8391B', '#00754B', '#77206D', '#005959', '#428700'];
Simon Hunt48e61672015-01-30 14:48:25 -0800144
145 var colors= {
146 light: {
147 norm: d3.scale.ordinal().range(lightNorm),
148 mute: d3.scale.ordinal().range(lightMute)
149 },
150 dark: {
151 norm: d3.scale.ordinal().range(darkNorm),
152 mute: d3.scale.ordinal().range(darkMute)
153 }
154 };
155
Simon Hunt737c89f2015-01-28 12:23:19 -0800156 function cat7() {
Simon Hunt48e61672015-01-30 14:48:25 -0800157 var tcid = 'd3utilTestCard';
158
159 function getColor(id, muted, theme) {
160 // NOTE: since we are lazily assigning domain ids, we need to
161 // get the color from all 4 scales, to keep the domains
162 // in sync.
163 var ln = colors.light.norm(id),
164 lm = colors.light.mute(id),
165 dn = colors.dark.norm(id),
166 dm = colors.dark.mute(id);
167 if (theme === 'dark') {
168 return muted ? dm : dn;
169 } else {
170 return muted ? lm : ln;
171 }
172 }
173
174 function testCard(svg) {
175 var g = svg.select('g#' + tcid),
176 dom = d3.range(7),
177 k, muted, theme, what;
178
179 if (!g.empty()) {
180 g.remove();
181
182 } else {
183 g = svg.append('g')
184 .attr('id', tcid)
185 .attr('transform', 'scale(4)translate(20,20)');
186
187 for (k=0; k<4; k++) {
188 muted = k%2;
189 what = muted ? ' muted' : ' normal';
190 theme = k < 2 ? 'light' : 'dark';
191 dom.forEach(function (id, i) {
192 var x = i * 20,
193 y = k * 20,
194 f = get(id, muted, theme);
195 g.append('circle').attr({
196 cx: x,
197 cy: y,
198 r: 5,
199 fill: f
200 });
201 });
202 g.append('rect').attr({
203 x: 140,
204 y: k * 20 - 5,
205 width: 32,
206 height: 10,
207 rx: 2,
208 fill: '#888'
209 });
210 g.append('text').text(theme + what)
211 .attr({
212 x: 142,
213 y: k * 20 + 2,
214 fill: 'white'
215 })
216 .style('font-size', '4pt');
217 }
218 }
219 }
220
221 return {
222 testCard: testCard,
223 getColor: getColor
224 };
Simon Hunt737c89f2015-01-28 12:23:19 -0800225 }
226
Simon Huntc9b73162015-01-29 14:02:15 -0800227 function translate(x, y) {
228 if (fs.isA(x) && x.length === 2 && !y) {
229 return 'translate(' + x[0] + ',' + x[1] + ')';
230 }
231 return 'translate(' + x + ',' + y + ')';
232 }
233
Simon Hunt4b668592015-01-29 17:33:53 -0800234 function stripPx(s) {
235 return s.replace(/px$/,'');
236 }
237
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800238 function makeVisible(el, b) {
239 el.style('visibility', (b ? 'visible' : 'hidden'));
240 }
241
Simon Huntac4c6f72015-02-03 19:50:53 -0800242 function safeId(s) {
243 return s.replace(/[^a-z0-9]/gi, '-');
244 }
245
Simon Hunt737c89f2015-01-28 12:23:19 -0800246 return {
247 createDragBehavior: createDragBehavior,
248 loadGlow: loadGlow,
Simon Huntc9b73162015-01-29 14:02:15 -0800249 cat7: cat7,
Simon Hunt4b668592015-01-29 17:33:53 -0800250 translate: translate,
Simon Hunt7c8ab8d2015-02-03 15:05:15 -0800251 stripPx: stripPx,
Simon Huntac4c6f72015-02-03 19:50:53 -0800252 makeVisible: makeVisible,
253 safeId: safeId
Simon Hunt737c89f2015-01-28 12:23:19 -0800254 };
255 }]);
256}());