blob: f8e954d3381702b80a9b1e82de728fcaa3565941 [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 Hunta11b4eb2015-01-28 16:20:50 -080037 function createDragBehavior(force, selectCb, atDragEnd,
38 dragEnabled, clickEnabled) {
39 var draggedThreshold = d3.scale.linear()
40 .domain([0, 0.1])
41 .range([5, 20])
42 .clamp(true),
43 drag,
44 fSel = fs.isF(selectCb),
45 fEnd = fs.isF(atDragEnd),
46 fDEn = fs.isF(dragEnabled),
47 fCEn = fs.isF(clickEnabled),
48 bad = [];
49
50 function naf(what) {
51 return 'SvgUtilService: createDragBehavior(): ' + what +
52 ' is not a function';
53 }
54
55 if (!fSel) {
56 bad.push(naf('selectCb'));
57 }
58 if (!fEnd) {
59 bad.push(naf('atDragEnd'));
60 }
61 if (!fDEn) {
62 bad.push(naf('dragEnabled'));
63 }
64 if (!fCEn) {
65 bad.push(naf('clickEnabled'));
66 }
67
68 if (bad.length) {
69 $log.error(bad.join('\n'));
70 return null;
71 }
72
73
74 function dragged(d) {
75 var threshold = draggedThreshold(force.alpha()),
76 dx = d.oldX - d.px,
77 dy = d.oldY - d.py;
78 if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) {
79 d.dragged = true;
80 }
81 return d.dragged;
82 }
83
84 drag = d3.behavior.drag()
85 .origin(function(d) { return d; })
86 .on('dragstart', function(d) {
87 if (clickEnabled() || dragEnabled()) {
88 d3.event.sourceEvent.stopPropagation();
89
90 d.oldX = d.x;
91 d.oldY = d.y;
92 d.dragged = false;
93 d.fixed |= 2;
94 d.dragStarted = true;
95 }
96 })
97 .on('drag', function(d) {
98 if (dragEnabled()) {
99 d.px = d3.event.x;
100 d.py = d3.event.y;
101 if (dragged(d)) {
102 if (!force.alpha()) {
103 force.alpha(.025);
104 }
105 }
106 }
107 })
108 .on('dragend', function(d) {
109 if (d.dragStarted) {
110 d.dragStarted = false;
111 if (!dragged(d)) {
112 // consider this the same as a 'click'
113 // (selection of a node)
114 if (clickEnabled()) {
115 selectCb(d, this);
116 // TODO: set 'this' context instead of param
117 }
118 }
119 d.fixed &= ~6;
120
121 // hook at the end of a drag gesture
122 if (dragEnabled()) {
123 atDragEnd(d, this);
124 // TODO: set 'this' context instead of param
125 }
126 }
127 });
128
129 return drag; }
Simon Hunt737c89f2015-01-28 12:23:19 -0800130
131 function loadGlow() {
132 $log.warn('SvgUtilService: loadGlow -- To Be Implemented');
133 }
134
Simon Hunt48e61672015-01-30 14:48:25 -0800135 // --- Ordinal scales for 7 values.
136 // TODO: tune colors for light and dark themes
137 // Note: These colors look good on the white background. Still, need to tune for dark.
138
139 // blue brown brick red sea green purple dark teal lime
140 var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'],
141 lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'],
142
Simon Hunt245a88e2015-02-02 13:26:04 -0800143 // let's try slightly brigher colors for dark normal..
144 darkNorm = ['#364D7F', '#7F5236', '#A93114', '#018D61', '#7E116D', '#02747A', '#378300'],
145 darkMute = ['#1B2645', '#432B1C', '#76220E', '#035433', '#560B4A', '#013A3E', '#2D6D00'];
Simon Hunt48e61672015-01-30 14:48:25 -0800146
147 var colors= {
148 light: {
149 norm: d3.scale.ordinal().range(lightNorm),
150 mute: d3.scale.ordinal().range(lightMute)
151 },
152 dark: {
153 norm: d3.scale.ordinal().range(darkNorm),
154 mute: d3.scale.ordinal().range(darkMute)
155 }
156 };
157
Simon Hunt737c89f2015-01-28 12:23:19 -0800158 function cat7() {
Simon Hunt48e61672015-01-30 14:48:25 -0800159 var tcid = 'd3utilTestCard';
160
161 function getColor(id, muted, theme) {
162 // NOTE: since we are lazily assigning domain ids, we need to
163 // get the color from all 4 scales, to keep the domains
164 // in sync.
165 var ln = colors.light.norm(id),
166 lm = colors.light.mute(id),
167 dn = colors.dark.norm(id),
168 dm = colors.dark.mute(id);
169 if (theme === 'dark') {
170 return muted ? dm : dn;
171 } else {
172 return muted ? lm : ln;
173 }
174 }
175
176 function testCard(svg) {
177 var g = svg.select('g#' + tcid),
178 dom = d3.range(7),
179 k, muted, theme, what;
180
181 if (!g.empty()) {
182 g.remove();
183
184 } else {
185 g = svg.append('g')
186 .attr('id', tcid)
187 .attr('transform', 'scale(4)translate(20,20)');
188
189 for (k=0; k<4; k++) {
190 muted = k%2;
191 what = muted ? ' muted' : ' normal';
192 theme = k < 2 ? 'light' : 'dark';
193 dom.forEach(function (id, i) {
194 var x = i * 20,
195 y = k * 20,
196 f = get(id, muted, theme);
197 g.append('circle').attr({
198 cx: x,
199 cy: y,
200 r: 5,
201 fill: f
202 });
203 });
204 g.append('rect').attr({
205 x: 140,
206 y: k * 20 - 5,
207 width: 32,
208 height: 10,
209 rx: 2,
210 fill: '#888'
211 });
212 g.append('text').text(theme + what)
213 .attr({
214 x: 142,
215 y: k * 20 + 2,
216 fill: 'white'
217 })
218 .style('font-size', '4pt');
219 }
220 }
221 }
222
223 return {
224 testCard: testCard,
225 getColor: getColor
226 };
Simon Hunt737c89f2015-01-28 12:23:19 -0800227 }
228
Simon Huntc9b73162015-01-29 14:02:15 -0800229 function translate(x, y) {
230 if (fs.isA(x) && x.length === 2 && !y) {
231 return 'translate(' + x[0] + ',' + x[1] + ')';
232 }
233 return 'translate(' + x + ',' + y + ')';
234 }
235
Simon Hunt4b668592015-01-29 17:33:53 -0800236 function stripPx(s) {
237 return s.replace(/px$/,'');
238 }
239
Simon Hunt737c89f2015-01-28 12:23:19 -0800240 return {
241 createDragBehavior: createDragBehavior,
242 loadGlow: loadGlow,
Simon Huntc9b73162015-01-29 14:02:15 -0800243 cat7: cat7,
Simon Hunt4b668592015-01-29 17:33:53 -0800244 translate: translate,
245 stripPx: stripPx
Simon Hunt737c89f2015-01-28 12:23:19 -0800246 };
247 }]);
248}());